-
Notifications
You must be signed in to change notification settings - Fork 641
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 #6 from SJsunshine/master
沈健
- Loading branch information
Showing
9 changed files
with
473 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,2 @@ | ||
/bin | ||
/.project/.classpath |
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,72 @@ | ||
package Collection; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
|
||
if(o==null) | ||
throw new RuntimeException("插入的值不能为null"); | ||
if((size+1)>=100){ | ||
throw new RuntimeException("数组越界异常"); | ||
} | ||
elementData[size]=o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
|
||
for(int i=index;i<size;i++){ | ||
elementData[i+1]=elementData[i]; | ||
} | ||
elementData[index]=o; | ||
} | ||
|
||
public Object get(int index){ | ||
|
||
if(index<0 || index>=size) | ||
throw new RuntimeException("索引异常"); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
if(index<0 || index>=size) | ||
throw new RuntimeException("索引异常"); | ||
Object temp=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
return temp; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
|
||
private int cursor=0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
|
||
return cursor!=size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
int i=cursor; | ||
cursor=i+1; | ||
return elementData[i]; | ||
} | ||
|
||
} | ||
} |
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,44 @@ | ||
package Collection; | ||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Object data){ | ||
this.data=data; | ||
left=null; | ||
right=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; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
|
||
if(left==null){ | ||
left=new BinaryTreeNode(o); | ||
return left; | ||
}else{ | ||
right=new BinaryTreeNode(o); | ||
return 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,5 @@ | ||
package Collection; | ||
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,195 @@ | ||
package Collection; | ||
|
||
public class LinkedList implements List { | ||
|
||
private int theSize=0;//表示该链表的长度 | ||
private Node beginMarker;//链表的头元素 | ||
private Node endMarker;//链表的尾元素 | ||
|
||
//链表的构造方法 | ||
public LinkedList () { | ||
beginMarker = new Node(null, null, null); | ||
endMarker = new Node(null, beginMarker, null); | ||
beginMarker.next=endMarker; | ||
} | ||
|
||
public void add(Object o){ | ||
add(size(),o); | ||
} | ||
public void add(int index , Object o){ | ||
addBefore(getNode(index),o); | ||
} | ||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
public Object remove(int index){ | ||
return remove(getNode(index)); | ||
} | ||
|
||
public int size(){ | ||
return theSize; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
addBefore(getNode(0),o); | ||
} | ||
public void addLast(Object o){ | ||
addBefore(getNode(theSize),o); | ||
} | ||
public Object removeFirst(){ | ||
return remove(getNode(0)); | ||
} | ||
public Object removeLast(){ | ||
return remove(getNode(theSize-1)); | ||
} | ||
public Iterator iterator(){ | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
|
||
private Node cursor=beginMarker.next; | ||
@Override | ||
public boolean hasNext() { | ||
|
||
return cursor!=endMarker; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
|
||
Object o=cursor.data; | ||
cursor=cursor.next; | ||
|
||
return o; | ||
} | ||
|
||
} | ||
private Object remove(Node node) { | ||
node.prev.next=node.next; | ||
node.next.prev=node.prev; | ||
theSize--; | ||
return node.data; | ||
} | ||
|
||
private Node getNode(int index){ | ||
Node p; | ||
if(index<0 || index>size()){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
if(index==-1){ | ||
return beginMarker; | ||
}else if(index==theSize){ | ||
return endMarker; | ||
} | ||
if(index<size()/2){ | ||
p=beginMarker.next; | ||
for(int i=0;i<index;i++){ | ||
p=p.next; | ||
} | ||
}else{ | ||
p=endMarker.prev; | ||
for(int i=size()-1;i>index;i--){ | ||
p=p.prev; | ||
} | ||
} | ||
return p; | ||
} | ||
|
||
private void addBefore(Node node,Object o){ | ||
Node newNode=new Node(o,node.prev,node); | ||
newNode.prev.next=newNode; | ||
node.prev=newNode; | ||
theSize++; | ||
//nodeCount++; | ||
} | ||
|
||
private static class Node{ | ||
public Object data; | ||
public Node next; | ||
public Node prev; | ||
public Node(Object data,Node prev,Node next){ | ||
this.data=data; | ||
this.prev=prev; | ||
this.next=next; | ||
} | ||
} | ||
|
||
|
||
//以下不用实现 | ||
/** | ||
* 把该链表逆置 | ||
* 例如链表为 3->7->10 , 逆置后变为 10->7->3 | ||
*/ | ||
public void reverse(){ | ||
|
||
} | ||
|
||
/** | ||
* 删除一个单链表的前半部分 | ||
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8 | ||
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 | ||
*/ | ||
public void removeFirstHalf(){ | ||
|
||
} | ||
|
||
/** | ||
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始 | ||
* @param i | ||
* @param length | ||
*/ | ||
public void remove(int i, int length){ | ||
|
||
} | ||
/** | ||
* 假定当前链表和listB均包含已升序排列的整数 | ||
* 从当前链表中取出那些listB所指定的元素 | ||
* 例如当前链表 = 11->101->201->301->401->501->601->701 | ||
* listB = 1->3->4->6 | ||
* 返回的结果应该是[101,301,401,601] | ||
* @param list | ||
*/ | ||
public int[] getElements(LinkedList list){ | ||
return null; | ||
} | ||
|
||
/** | ||
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 从当前链表中中删除在listB中出现的元素 | ||
* @param list | ||
*/ | ||
|
||
public void subtract(LinkedList list){ | ||
|
||
} | ||
|
||
/** | ||
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) | ||
*/ | ||
public void removeDuplicateValues(){ | ||
|
||
} | ||
|
||
/** | ||
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 | ||
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) | ||
* @param min | ||
* @param max | ||
*/ | ||
public void removeRange(int min, int max){ | ||
|
||
} | ||
|
||
/** | ||
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) | ||
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 | ||
* @param list | ||
*/ | ||
public LinkedList intersection( LinkedList list){ | ||
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,9 @@ | ||
package Collection; | ||
|
||
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,24 @@ | ||
package Collection; | ||
|
||
public class Queue { | ||
|
||
private LinkedList list=new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
list.addFirst(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
|
||
return list.removeLast(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
|
||
return list.size()==0; | ||
} | ||
|
||
public int size(){ | ||
return list.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,33 @@ | ||
package Collection; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
int index=elementData.size()-1; | ||
Object data=elementData.get(index); | ||
elementData.remove(index); | ||
return data; | ||
} | ||
|
||
public Object peek(){ | ||
|
||
int index=elementData.size()-1; | ||
return (Object)elementData.get(index); | ||
} | ||
public boolean isEmpty(){ | ||
if(elementData.size()==0) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |
Oops, something went wrong.