forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
287 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,50 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayList { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void enlargeCapacity(int minCapacity){ | ||
int oldCapacity=elementData.length; | ||
if(oldCapacity<minCapacity){ | ||
oldCapacity=(oldCapacity*3)/2+1; | ||
} | ||
} | ||
|
||
public void add(Object o){ | ||
enlargeCapacity(elementData.length+1); | ||
elementData[elementData.length+1]=o; | ||
} | ||
public void add(int index, Object o){ | ||
enlargeCapacity(elementData.length+1); | ||
Object data[]= Arrays.copyOfRange(elementData, index, elementData.length); | ||
elementData[index] = o; | ||
System.arraycopy(data, 0, elementData, index+1, elementData.length); | ||
|
||
} | ||
|
||
public Object get(int index){ | ||
Object o; | ||
o=elementData[index]; | ||
return o; | ||
} | ||
|
||
public Object remove(int index){ | ||
Object o; | ||
o=elementData[index]; | ||
Object data[]= Arrays.copyOfRange(elementData, index+1, elementData.length); | ||
System.arraycopy(data, 0, elementData, index, elementData.length); | ||
return o; | ||
} | ||
|
||
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,47 @@ | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
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){ | ||
BinaryTreeNode node; | ||
do{ | ||
node=compare(o); | ||
}while(node==null); | ||
node.data=o; | ||
return node; | ||
} | ||
|
||
public BinaryTreeNode compare(Object o){ | ||
int a=(Integer)data; | ||
int b=(Integer)o; | ||
if(a>b){ | ||
return left; | ||
}else{ | ||
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,121 @@ | ||
public class LinkedList { | ||
|
||
private Node head; | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
|
||
public boolean hasNext(Node a){ | ||
if(a.next!=null){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Node getIndex(int index){ | ||
Node a=head.next; | ||
for(int i=0;i<index;i++){ | ||
a=a.next; | ||
} | ||
return a; | ||
} | ||
|
||
public void add(Object o){ | ||
Node a; | ||
if(!hasNext(head)){ | ||
a=head.next; | ||
a.data=o; | ||
}else{ | ||
a=head.next; | ||
while(a.next!=null){ | ||
a=a.next; | ||
} | ||
a=a.next; | ||
a.data=o; | ||
} | ||
} | ||
|
||
|
||
public void add(int index , Object o){ | ||
Node a=getIndex(index); | ||
Node b=null; | ||
b.data=o; | ||
b.next=a.next; | ||
a.next=b; | ||
} | ||
public Object get(int index){ | ||
Object o; | ||
Node a=getIndex(index); | ||
o=a.data; | ||
return o; | ||
} | ||
public Object remove(int index){ | ||
Node a=getIndex(index-1); | ||
Node b=a.next; | ||
Object c=b.data; | ||
a.next=b.next; | ||
b.next=null; | ||
return c; | ||
} | ||
|
||
public int size(){ | ||
Node a=head.next; | ||
int i=0; | ||
while(a.next==null){ | ||
a=a.next; | ||
i++; | ||
} | ||
return i; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node a=null; | ||
a.data=o; | ||
Node b=head.next; | ||
a.next=b; | ||
head.next=a; | ||
} | ||
public void addLast(Object o){ | ||
Node a; | ||
if(!hasNext(head)){ | ||
a=head.next; | ||
a.data=o; | ||
}else{ | ||
a=head.next; | ||
while(a.next!=null){ | ||
a=a.next; | ||
} | ||
a=a.next; | ||
a.data=o; | ||
} | ||
} | ||
public Object removeFirst(){ | ||
Node a=head.next; | ||
Node b=a.next; | ||
Object c=a.data; | ||
head.next=b; | ||
a.next=null; | ||
return c; | ||
} | ||
public Object removeLast(){ | ||
Node a=head.next; | ||
while(a.next.next!=null){ | ||
a=a.next; | ||
}; | ||
Object b=a.next.data; | ||
a.next=null; | ||
return b; | ||
|
||
} | ||
|
||
|
||
//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,39 @@ | ||
import java.util.Arrays; | ||
|
||
|
||
public class Queue { | ||
|
||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void enlargeCapacity(int minCapacity){ | ||
int oldCapacity=elementData.length; | ||
if(oldCapacity<minCapacity){ | ||
oldCapacity=(oldCapacity*3)/2+1; | ||
} | ||
} | ||
|
||
public void enQueue(Object o){ | ||
enlargeCapacity(elementData.length+1); | ||
elementData[elementData.length+1]=o; | ||
} | ||
|
||
public Object deQueue(){ | ||
Object a = elementData[0]; | ||
elementData=Arrays.copyOfRange(elementData, 1, elementData.length); | ||
return null; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(elementData.length==0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return elementData.length; | ||
} | ||
} |
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,30 @@ | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
private int size = 0; | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
Object o=elementData.get(elementData.size()); | ||
elementData.remove(elementData.size()); | ||
return o; | ||
} | ||
|
||
public Object peek(){ | ||
Object o=elementData.get(elementData.size()); | ||
return o; | ||
} | ||
public boolean isEmpty(){ | ||
if(elementData.size()==0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |