forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #14 from sawyerwu/master
the first homework on 2.26
- Loading branch information
Showing
8 changed files
with
350 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,3 @@ | ||
/bin/ | ||
/.classpath | ||
/.project |
84 changes: 84 additions & 0 deletions
84
group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java
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,84 @@ | ||
package com.ace.coding; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
checkArrayLength(); | ||
elementData[size++] = o; | ||
} | ||
|
||
private void checkArrayLength(){ | ||
if(elementData.length < size() + 1){ | ||
// expand the origin length of the array | ||
int newLength = size * 2 + 1; | ||
elementData = Arrays.copyOf(elementData, newLength); | ||
} | ||
} | ||
|
||
private void checkIndex(int index){ | ||
if(index < 0 || index >= size()){ | ||
throw new IndexOutOfBoundsException("Index " + index + " is invalid."); | ||
} | ||
} | ||
|
||
public void add(int index, Object o){ | ||
checkIndex(index); | ||
checkArrayLength(); | ||
System.arraycopy(elementData, index, elementData, index+1, size() - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
checkIndex(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
checkIndex(index); | ||
Object obj = elementData[index]; | ||
if(index == size() - 1){ | ||
elementData[index] = null; | ||
} else { | ||
System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); | ||
} | ||
size--; | ||
return obj; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
// return new ListIterator(); | ||
} | ||
|
||
/*private class ListIterator implements Iterator{ | ||
private int index = 0; | ||
@Override | ||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
return index != size; | ||
} | ||
@Override | ||
public Object next() { | ||
// TODO Auto-generated method stub | ||
if(index >= size){ | ||
throw new IndexOutOfBoundsException("There's no next element."); | ||
} | ||
return elementData[index++]; | ||
} | ||
}*/ | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java
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,61 @@ | ||
package com.ace.coding; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private BinaryTreeNode rootNode; | ||
|
||
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 newNode = new BinaryTreeNode(); | ||
newNode.setData(o); | ||
|
||
if(rootNode == null){ | ||
rootNode = newNode; | ||
rootNode.data = data; | ||
left = null; | ||
right = null; | ||
} else { | ||
BinaryTreeNode currentNode = rootNode; | ||
while(true){ | ||
BinaryTreeNode pNode = currentNode; | ||
if((int)newNode.getData() > (int)currentNode.getData()){ | ||
currentNode = currentNode.right; | ||
if(currentNode.right == null){ | ||
pNode.right = newNode; | ||
return newNode; | ||
} | ||
} else { | ||
currentNode = currentNode.left; | ||
if(currentNode.left == null){ | ||
pNode.left = newNode; | ||
return newNode; | ||
} | ||
} | ||
} | ||
|
||
} | ||
return newNode; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group03/664269713/DataStructure/src/com/ace/coding/Iterator.java
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 com.ace.coding; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java
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,123 @@ | ||
package com.ace.coding; | ||
|
||
public class LinkedList implements List { | ||
private Node head = null; | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
if(head == null){ | ||
head = newNode; | ||
} else { | ||
Node pNode = head; | ||
while(pNode.next != null){ | ||
pNode = pNode.next; | ||
} | ||
pNode.next = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
public void add(int index , Object o){ | ||
checkLinkedListIndex(index); | ||
|
||
Node newNode = new Node(); | ||
newNode.data = o; | ||
Node pNode = getNode(index); | ||
newNode.next = pNode.next; | ||
pNode.next = newNode; | ||
|
||
size++; | ||
} | ||
|
||
private Node getNode(int index){ | ||
Node pNode = head; | ||
for(int i = 0; i < index; i++){ | ||
pNode = pNode.next; | ||
} | ||
return pNode; | ||
} | ||
|
||
public Object get(int index){ | ||
Node pNode = getNode(index); | ||
return pNode.data; | ||
} | ||
public Object remove(int index){ | ||
checkLinkedListIndex(index); | ||
|
||
Node pNode = head; | ||
for(int i = 0; i < index - 1; i++){ | ||
pNode = pNode.next; | ||
} | ||
Node tempNode = getNode(index); | ||
pNode.next = tempNode.next; | ||
size--; | ||
return tempNode.data; | ||
} | ||
|
||
|
||
public void addFirst(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
newNode.next = head; | ||
head = newNode; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
Node pNode = getNode(size() - 1); | ||
pNode.next = newNode; | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
Node pNode = head; | ||
head = pNode.next; | ||
size--; | ||
return pNode.data; | ||
} | ||
public Object removeLast(){ | ||
Object obj = remove(size() - 1); | ||
return obj; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
private void checkLinkedListIndex(int index){ | ||
if(index < 0 || index >= size()){ | ||
throw new IndexOutOfBoundsException("The index " + index + " is invalid."); | ||
} | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
// return new ListIterator(); | ||
} | ||
|
||
/*private class ListIterator implements Iterator{ | ||
private Node pNode = head; | ||
@Override | ||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
return pNode.next != null; | ||
} | ||
@Override | ||
public Object next() { | ||
// TODO Auto-generated method stub | ||
Object obj = pNode.data; | ||
pNode = pNode.next; | ||
return obj; | ||
} | ||
}*/ | ||
|
||
private static class Node{ | ||
Object data; | ||
Node 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,9 @@ | ||
package com.ace.coding; | ||
|
||
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(); | ||
} |
27 changes: 27 additions & 0 deletions
27
group03/664269713/DataStructure/src/com/ace/coding/Queue.java
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 com.ace.coding; | ||
|
||
public class Queue { | ||
private ArrayList arrayList = new ArrayList(); | ||
private int size = 0; | ||
|
||
public void enQueue(Object data){ | ||
arrayList.add(size(), data); | ||
size++; | ||
} | ||
|
||
public Object deQueue(){ | ||
if(isEmpty()){ | ||
throw new IndexOutOfBoundsException("The Queue is Empty."); | ||
} | ||
size--; | ||
return arrayList.remove(0); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return size()>0; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
group03/664269713/DataStructure/src/com/ace/coding/Stack.java
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,36 @@ | ||
package com.ace.coding; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
private int size = 0; | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
size++; | ||
} | ||
|
||
public Object pop(){ | ||
checkStack(); | ||
Object obj = elementData.remove(size()-1); | ||
size--; | ||
return obj; | ||
} | ||
|
||
public Object peek(){ | ||
checkStack(); | ||
return elementData.get(size()-1); | ||
} | ||
|
||
private void checkStack(){ | ||
if(isEmpty()){ | ||
throw new IndexOutOfBoundsException("This stack is empty"); | ||
} | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return size == 0; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
} |