forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #11 from PreciousLiu/master
286674878 week01
- Loading branch information
Showing
8 changed files
with
426 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 @@ | ||
/bin/ |
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,101 @@ | ||
package week01; | ||
|
||
import java.util.Arrays; | ||
|
||
|
||
|
||
public class ArrayList implements List { | ||
/** | ||
* The current array's size. | ||
*/ | ||
private int size = 0; | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private Object[] elementData; | ||
/** | ||
* Return an empty Array instance if no initial elements has specified. | ||
*/ | ||
private static final Object[] EMPTY_ELEMENTDATA ={}; | ||
/** | ||
* Construct an ArrayList with default capacity. | ||
*/ | ||
public ArrayList(){ | ||
this(DEFAULT_CAPACITY); | ||
} | ||
/** | ||
* Construct an ArrayList instance with specified capacity. | ||
* @param initialSize the specified capacity | ||
*/ | ||
public ArrayList(int initialSize){ | ||
if(initialSize>0){ //Does it right >0 but > DEFAULTCAPACITY? | ||
this.elementData = new Object[initialSize]; | ||
}else if(initialSize==0){ | ||
this.elementData = EMPTY_ELEMENTDATA; | ||
}else{ | ||
throw new IllegalArgumentException("IllegalCapacity:" + initialSize); | ||
} | ||
} | ||
/** | ||
* Increase the capacity | ||
*/ | ||
public void grow(int capacity){ | ||
elementData = Arrays.copyOf(elementData, capacity); | ||
} | ||
/** | ||
* Add an element to ArrayList's tail | ||
*/ | ||
public boolean add(Object o){ | ||
if(this.size == elementData.length){ | ||
grow(size+10); | ||
} | ||
elementData[size++] = o; | ||
return true; | ||
} | ||
/** | ||
* Add an element to an Array with specified location | ||
*/ | ||
public boolean add(int index, Object o){ | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalIndex"); | ||
}else if(this.size+1>elementData.length){ | ||
grow(size+10); | ||
}else{ | ||
for(int i=this.size+1;i>index;i--){ | ||
elementData[i] = elementData[i-1]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Return an element that specified | ||
*/ | ||
public Object get(int index){ | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalIndex"); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public boolean remove(int index){ | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalIndex"); | ||
}else{ | ||
for(int i=index;i<=this.size;i++){ | ||
elementData[i] = elementData[i+1]; | ||
} | ||
size--; | ||
} | ||
return true; | ||
} | ||
|
||
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,81 @@ | ||
package week01; | ||
|
||
|
||
|
||
public class BinaryTree { | ||
class BinaryTreeNode { | ||
private int data; | ||
private BinaryTreeNode left = null; | ||
private BinaryTreeNode right = null; | ||
public BinaryTreeNode(int data){ | ||
this.data = data; | ||
} | ||
} | ||
//前序遍历 | ||
private BinaryTreeNode root; | ||
public void preorder(BinaryTreeNode bt){ | ||
if(bt!=null) | ||
{ | ||
getData(bt); | ||
preorder(bt.left); | ||
preorder(bt.right); | ||
} | ||
} | ||
public int getData(BinaryTreeNode bt){ | ||
preorder(bt); | ||
return bt.data; | ||
} | ||
public void setData(int data) { | ||
BinaryTreeNode newadd = new BinaryTree.BinaryTreeNode(data); | ||
Insert(newadd,data); | ||
|
||
} | ||
public BinaryTreeNode getLeft(BinaryTreeNode bt) { | ||
preorder(bt); | ||
return bt.left; | ||
} | ||
//public void setLeft(BinaryTreeNode left) { | ||
// this.left = left; | ||
//} | ||
public BinaryTreeNode getRight(BinaryTreeNode bt) { | ||
preorder(bt); | ||
return bt.right; | ||
} | ||
|
||
//public void setRight(BinaryTreeNode right) { | ||
// this.right = right; | ||
//} | ||
|
||
public boolean Insert(BinaryTreeNode bt,int data){ | ||
if(bt == null){ | ||
bt.data = data; | ||
return true; | ||
} | ||
else{ | ||
if(data==bt.data) | ||
{ | ||
System.out.println("The data has already exist"); | ||
return false;} | ||
else if(data < bt.data){ | ||
return Insert(bt.left,data); | ||
}else return Insert(bt.right,data); | ||
} | ||
|
||
} | ||
public BinaryTreeNode Search(BinaryTreeNode root, int data){ | ||
if(root==null){ | ||
//throw new IllegalArgumentException("The Tree is Empty "+root.data); | ||
System.out.println("The Tree is an empty tree."); | ||
return root; | ||
}else{ | ||
if(root.data==data) | ||
{return root;} | ||
else if(data>root.data) | ||
{return Search(root.right,data);} | ||
else | ||
{return Search(root.left,data);} | ||
} | ||
} | ||
|
||
} | ||
|
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,7 @@ | ||
package week01; | ||
|
||
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,145 @@ | ||
package week01; | ||
|
||
|
||
|
||
public class LinkedList implements List{ | ||
/** | ||
* Class Node is an inner class | ||
*/ | ||
class Node{ | ||
private Object data; | ||
private Node next = null; | ||
private Node prev = null; | ||
public Node(Object data){ | ||
this.data = data; | ||
} | ||
} | ||
private Node head; | ||
private Node tail; | ||
private int size = 0; | ||
|
||
/** | ||
* Add an element in the LinkedList | ||
*/ | ||
public boolean add(Object data){ | ||
if(head.next==null){ | ||
addFirst(data); | ||
}else{ | ||
addLast(data); | ||
} | ||
return true; | ||
} | ||
/** | ||
* Add an element in the LinkedList that with specified location | ||
*/ | ||
public boolean add(int index , Object data){ | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalArgument"+index); | ||
}else{ | ||
Node indexnodehere = findIndex(index); | ||
Node newadd = new Node(data); | ||
newadd.next = indexnodehere.next; | ||
newadd.prev = indexnodehere; | ||
indexnodehere.next.prev = newadd; | ||
indexnodehere.next = newadd; | ||
size++; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Find node according index | ||
* @param index | ||
* @return | ||
*/ | ||
public Node findIndex(int index){ | ||
Node indexnode = this.head; | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalArgument"+index); | ||
}else{ | ||
for(int i=1;i<index;i++){ | ||
indexnode=indexnode.next; | ||
} | ||
} | ||
return indexnode; | ||
} | ||
public Object get(int index){ | ||
Node nodeget =findIndex(index); | ||
return nodeget.data; | ||
} | ||
|
||
public boolean remove(int index){ | ||
if(index<0 || index>this.size){ | ||
throw new IllegalArgumentException("IllegalArgument"+index); | ||
}else{ | ||
Node indexnodehere2 = findIndex(index); | ||
if(indexnodehere2.prev == this.head){ | ||
removeFirst(); | ||
}else if(indexnodehere2.next == this.tail){ | ||
removeLast(); | ||
}else{ | ||
indexnodehere2.prev.next = indexnodehere2.next; | ||
indexnodehere2.next.prev = indexnodehere2.prev; | ||
size--; | ||
} | ||
|
||
} | ||
return true; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
/** | ||
* Add an new element in the beginning of an LinledList(just after head) | ||
* @param data | ||
*/ | ||
public void addFirst(Object data){ | ||
Node newfirstincrease = new Node(data); | ||
newfirstincrease.next = head.next; | ||
head.next = newfirstincrease; | ||
newfirstincrease.prev = head; | ||
newfirstincrease.next = tail; | ||
tail.prev = newfirstincrease; | ||
size++; | ||
} | ||
/** | ||
* Add an new element in the tail of an LinkedList | ||
* @param data | ||
*/ | ||
public void addLast(Object data){ | ||
Node newincrease = new Node(data); | ||
newincrease.next = tail; | ||
newincrease.prev = tail.prev; | ||
tail.prev.next = newincrease; | ||
tail.prev = newincrease; | ||
size++; | ||
} | ||
/** | ||
* Delete the first element(node) which just after the head | ||
* @return | ||
*/ | ||
public boolean removeFirst(){ | ||
head.next = head.next.next; | ||
head.next.next.prev = head; | ||
size--; | ||
return true; | ||
} | ||
/** | ||
* Delete the last element(node) which just before the tail | ||
* @return | ||
*/ | ||
public boolean removeLast(){ | ||
tail.prev = tail.prev.prev; | ||
tail.prev.prev.next= tail; | ||
size--; | ||
return true; | ||
} | ||
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,10 @@ | ||
package week01; | ||
|
||
public interface List { | ||
public boolean add(Object o); | ||
public boolean add(int index, Object o); | ||
public Object get(int index); | ||
public boolean 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,30 @@ | ||
package week01; | ||
|
||
import java.util.Arrays; | ||
//以后有时间要改成循环队列 | ||
public class Queue { | ||
private ArrayList elementData = new ArrayList(); | ||
private int front; | ||
private int rear; | ||
|
||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
|
||
return null; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(elementData.size()>0){ | ||
return false; | ||
}else return true; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.