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 #9 from jiaxun1990/master
week01 homework commit
- Loading branch information
Showing
7 changed files
with
476 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,107 @@ | ||
package list; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public class ArrayList implements List { | ||
|
||
private static final int DEFAULT_CAPACITY = 10; | ||
|
||
private Object[] elementData; | ||
private int size; | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_CAPACITY]; | ||
size = 0; | ||
} | ||
|
||
public void add(Object object) { | ||
ensureCapacity(); | ||
elementData[size] = object; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object object) { | ||
if (index > size || index < 0) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
ensureCapacity(); | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = object; | ||
size++; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index > size) { | ||
return null; | ||
} | ||
Object object = get(index); | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
size--; | ||
return object; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Object get(int index) { | ||
if (index >= size || index < 0) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public void set(int index, Object object) { | ||
if (index >= size) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
elementData[index] = object; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private void ensureCapacity() { | ||
if (size + 1 > elementData.length) { | ||
ensureCapacity(elementData.length + 1); | ||
} | ||
} | ||
|
||
private void ensureCapacity(int length) { | ||
Object[] newElementData = new Object[length]; | ||
System.arraycopy(elementData, 0, newElementData, 0, size); | ||
elementData = newElementData; | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private ArrayList arrayList; | ||
private int currentPosition = 0; | ||
|
||
private ArrayListIterator(ArrayList arrayList) { | ||
this.arrayList = arrayList; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return currentPosition < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[currentPosition++]; | ||
} | ||
|
||
@Override | ||
public Object remove() { | ||
return arrayList.remove(--currentPosition); | ||
} | ||
} | ||
|
||
} |
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,80 @@ | ||
package list; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public class BinaryTree { | ||
|
||
private Node root; | ||
|
||
public BinaryTree() { | ||
root = null; | ||
} | ||
|
||
public void insert(int value) { | ||
if (root == null) { | ||
root = new Node(value); | ||
} else { | ||
Node newNode = new Node(value); | ||
Node curr = root; | ||
Node prev = null; | ||
boolean right = true; | ||
|
||
while (curr != null) { | ||
prev = curr; | ||
if (curr.getData() > value) { | ||
curr = curr.getRight(); | ||
right = true; | ||
} else if (curr.getData() < value) { | ||
curr = curr.getLeft(); | ||
right = false; | ||
} else { | ||
prev = null; | ||
break; | ||
} | ||
} | ||
if (prev != null) { | ||
if (right) { | ||
prev.setRight(newNode); | ||
} else { | ||
prev.setLeft(newNode); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return root == null; | ||
} | ||
|
||
private static class Node { | ||
int data; | ||
Node left; | ||
Node right; | ||
|
||
public Node(int data) { | ||
this.data = data; | ||
} | ||
|
||
public int getData() { | ||
return data; | ||
} | ||
|
||
public Node getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(Node left) { | ||
this.left = left; | ||
} | ||
|
||
public Node getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(Node 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,12 @@ | ||
package list; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public interface Iterator { | ||
|
||
boolean hasNext(); | ||
Object next(); | ||
Object remove(); | ||
|
||
} |
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,14 @@ | ||
package list; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
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,38 @@ | ||
package list; | ||
|
||
/** | ||
* @author jiaxun | ||
*/ | ||
public class Queue { | ||
|
||
private SinglyLinkedList linkedList = new SinglyLinkedList(); | ||
|
||
public void enQueue(Object object) { | ||
linkedList.addLast(object); | ||
} | ||
|
||
public Object deQueue() { | ||
return linkedList.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return linkedList.size() == 0; | ||
} | ||
|
||
public int size() { | ||
return linkedList.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (size() > 0) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (int i = 0, len = size(); i < len; i++) { | ||
stringBuilder.append("[").append(linkedList.get(i)).append("]"); | ||
} | ||
return stringBuilder.toString(); | ||
} else { | ||
return super.toString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.