forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from QQ-ing/master
932886072提交作业
- Loading branch information
Showing
7 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package djj; | ||
|
||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by jerry on 2017/2/26. | ||
*/ | ||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
if(size>elementData.length*0.8){ | ||
Arrays.copyOf(elementData,elementData.length*2); | ||
} | ||
elementData[size]=o; | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
if (size>=index){ | ||
Object[] temp=new Object[elementData.length]; | ||
System.arraycopy(elementData,0,temp,0,index); | ||
temp[index]=o; | ||
System.arraycopy(elementData,index,temp,index+1,size-index); | ||
elementData=temp; | ||
}else if(size<index){ | ||
throw new RuntimeException("越界"); | ||
} | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index>size){ | ||
throw new RuntimeException("越界"); | ||
}else{ | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public Object remove(int index){ | ||
Object tempObj=null; | ||
if(index<=size){ | ||
Object[] temp=new Object[elementData.length]; | ||
System.arraycopy(elementData,0,temp,0,index); | ||
tempObj=elementData[index]; | ||
System.arraycopy(elementData,index+1,temp,index,size-index-1); | ||
elementData=temp; | ||
} | ||
size--; | ||
return tempObj; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package djj; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private BinaryTreeNode root; | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
|
||
return null; | ||
} | ||
|
||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package djj; | ||
|
||
/** | ||
* Created by jerry on 2017/2/26. | ||
*/ | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package djj; | ||
|
||
public class LinkedList implements List { | ||
//头节点 | ||
private Node head; | ||
//尾节点 | ||
// private Node tail; | ||
//当前游标节点 | ||
private Node curNode; | ||
public int size=0; | ||
|
||
|
||
public void add(Object o){ | ||
if(head==null){ | ||
head=new Node(o); | ||
head.next=null; | ||
}else{ | ||
curNode=head; | ||
while(curNode.next!=null){ | ||
curNode=curNode.next; | ||
} | ||
curNode.next=new Node(o); | ||
} | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
if(index>size||index<=0){ | ||
throw new RuntimeException("越界"); | ||
}else{ | ||
curNode=head; | ||
for(int i=0;i<index;i++){ | ||
curNode=curNode.next; | ||
} | ||
Node newNode=new Node(o); | ||
newNode.next=curNode.next; | ||
curNode.next=newNode; | ||
size++; | ||
} | ||
} | ||
public Object get(int index){ | ||
if(index>size||index<=0){ | ||
throw new RuntimeException("越界"); | ||
} | ||
Node temp=head; | ||
for(int i=0;i<index;i++){ | ||
temp=temp.next; | ||
} | ||
return temp.data; | ||
} | ||
public Object remove(int index){ | ||
if(index>size||index<=0){ | ||
throw new RuntimeException("越界"); | ||
} | ||
Node temp=head; | ||
for(int i=0;i<index-1;i++){ | ||
temp=temp.next; | ||
} | ||
Node delNode=temp.next; | ||
temp.next=delNode.next; | ||
delNode.next=null; | ||
size--; | ||
return delNode.data; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
add(1,o); | ||
} | ||
public void addLast(Object o){ | ||
add(size,o); | ||
} | ||
public Object removeFirst(){ | ||
return null; | ||
} | ||
public Object removeLast(){ | ||
return null; | ||
} | ||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object o){ | ||
data=o; | ||
next=null; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package djj; | ||
|
||
/** | ||
* Created by jerry on 2017/2/26. | ||
*/ | ||
public interface List { | ||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package djj; | ||
|
||
import java.util.*; | ||
|
||
//队列 | ||
public class Queue { | ||
private LinkedList elementList; | ||
|
||
//入队 | ||
public void enQueue(Object o){ | ||
elementList.add(o); | ||
} | ||
|
||
//出队 | ||
public Object deQueue(){ | ||
return elementList.remove(1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementList.size==0?true:false; | ||
|
||
} | ||
|
||
public int size(){ | ||
return elementList.size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package djj; | ||
//栈 | ||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
private int pointer=0; | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
pointer++; | ||
} | ||
|
||
public Object pop(){ | ||
Object temp=elementData.remove(pointer); | ||
pointer--; | ||
return temp; | ||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(pointer); | ||
} | ||
public boolean isEmpty(){ | ||
return pointer==0?true:false; | ||
} | ||
public int size(){ | ||
return pointer; | ||
} | ||
} |