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 NaixiaoZhang/master
Add week 01 homework
- Loading branch information
Showing
14 changed files
with
732 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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>coding2017</artifactId> | ||
<groupId>me.sidzh</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>Week01</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.stefanbirkner</groupId> | ||
<artifactId>system-rules</artifactId> | ||
<version>1.16.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
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,102 @@ | ||
public class ArrayList implements List{ | ||
|
||
private Object[] elements; | ||
|
||
private static final int INITIAL_SIZE = 16; | ||
|
||
public static final int MAX_LIST_SIZE = 48; | ||
|
||
private int size = 0; | ||
|
||
private int capacity = 0; | ||
|
||
public ArrayList() { | ||
elements = new Object[INITIAL_SIZE]; | ||
capacity = INITIAL_SIZE; | ||
} | ||
|
||
public void add(int index, Object obj) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
ensureSpace(); | ||
if (index == size) { | ||
add(obj); | ||
} else { | ||
System.arraycopy(elements, index, elements, index + 1, size - index); | ||
elements[index] = obj; | ||
size++; | ||
} | ||
} | ||
|
||
public void add(Object obj) { | ||
ensureSpace(); | ||
elements[size++] = obj; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder("List: [ "); | ||
for (int i = 0; i < size; ++ i) { | ||
builder.append(elements[i]).append(" "); | ||
} | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
private void ensureSpace() { | ||
if (size == capacity) { | ||
if (size == MAX_LIST_SIZE) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
int newCapacity = capacity*2 > MAX_LIST_SIZE ? MAX_LIST_SIZE : capacity*2; | ||
grow(newCapacity); | ||
} | ||
} | ||
|
||
private void grow(int newLength) { | ||
Object[] newElements = new Object[newLength]; | ||
System.arraycopy(elements, 0, newElements, 0, elements.length); | ||
elements = newElements; | ||
capacity = newLength; | ||
} | ||
|
||
public Object get(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return elements[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
Object toRemove = elements[index]; | ||
System.arraycopy(elements, index + 1, elements, index, size - index -1); | ||
--size; | ||
return toRemove; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int pos = 0; | ||
|
||
public boolean hasNext() { | ||
return pos < size(); | ||
} | ||
|
||
public Object next() { | ||
return elements[pos++]; | ||
} | ||
} | ||
} |
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,29 @@ | ||
public class BinaryTree { | ||
|
||
private BinaryTreeNode root; | ||
|
||
private static class BinaryTreeNode { | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
} | ||
|
||
public BinaryTree getLeft() { | ||
|
||
|
||
return null; | ||
} | ||
|
||
public void setLeft() { | ||
|
||
} | ||
|
||
public BinaryTree getRight() { | ||
|
||
return null; | ||
} | ||
|
||
public void setRight() { | ||
|
||
} | ||
} |
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 @@ | ||
/** | ||
* Created by spike on 2/19/17. | ||
*/ | ||
public interface Iterator { | ||
boolean hasNext(); | ||
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,128 @@ | ||
/** | ||
* Created by spike on 2/19/17. | ||
*/ | ||
public class LinkedList implements List { | ||
|
||
private LinkedListNode head; | ||
private LinkedListNode tail; | ||
private int size; | ||
|
||
private static class LinkedListNode { | ||
private Object data; | ||
private LinkedListNode prev; | ||
private LinkedListNode next; | ||
|
||
private LinkedListNode(Object data, LinkedListNode prev, LinkedListNode next) { | ||
this.data = data; | ||
this.prev = prev; | ||
this.next = next; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder("List: [ "); | ||
LinkedListNode idx = head; | ||
while (idx != null) { | ||
builder.append(idx.data); | ||
builder.append(" "); | ||
idx = idx.next; | ||
} | ||
|
||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
public void add(int index, Object object) { | ||
if (index < 0 || index > size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (index == size) { // insert after | ||
add(object); | ||
} else { // insert before | ||
LinkedListNode target = findNodeByIndex(index); | ||
LinkedListNode nd = new LinkedListNode(object, target.prev, target); | ||
if (head == target) { | ||
head = nd; | ||
} else { | ||
target.prev.next = nd; | ||
} | ||
} | ||
++size; | ||
} | ||
|
||
public void add(Object object) { | ||
if (head == null) { | ||
LinkedListNode nd = new LinkedListNode(object, null, null); | ||
head = tail = nd; | ||
} else { | ||
LinkedListNode nd = new LinkedListNode(object, tail, null); | ||
tail.next = nd; | ||
tail = nd; | ||
} | ||
++size; | ||
} | ||
|
||
public Object get(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
LinkedListNode target = findNodeByIndex(index); | ||
return target.data; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
LinkedListNode target = findNodeByIndex(index); | ||
if (target == head) { | ||
if (head == tail) { | ||
head = tail = null; | ||
} else { | ||
head = head.next; | ||
head.prev = null; | ||
} | ||
} else if (target == tail) { | ||
tail = tail.prev; | ||
tail.next = null; | ||
} else { | ||
target.prev.next = target.next; | ||
target.next.prev = target.prev; | ||
} | ||
target.prev = target.next = null; | ||
--size; | ||
return target.data; | ||
} | ||
|
||
private LinkedListNode findNodeByIndex(int index) { | ||
LinkedListNode target = head; | ||
for (int i = 0; i != index; ++i) { | ||
target = target.next; | ||
} | ||
return target; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
|
||
LinkedListNode cursor = head; | ||
|
||
public boolean hasNext() { | ||
return cursor != null; | ||
} | ||
|
||
public Object next() { | ||
Object toRet = cursor.data; | ||
cursor = cursor.next; | ||
return toRet; | ||
} | ||
} | ||
} |
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 @@ | ||
public interface List { | ||
void add(int index, Object object); | ||
void add(Object object); | ||
Object get(int i); | ||
Object remove(int i); | ||
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,35 @@ | ||
public class Queue { | ||
|
||
private LinkedList llist = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
llist.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
if (llist.size() == 0) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
return llist.remove(0); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return llist.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return llist.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder("Queue: [ "); | ||
Iterator iter = llist.iterator(); | ||
while (iter.hasNext()) { | ||
builder.append(iter.next()); | ||
builder.append(" "); | ||
} | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
} |
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 @@ | ||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
if (elementData.size() == 0) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
return elementData.remove(elementData.size()-1); | ||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size()-1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder("Stack: [ "); | ||
Iterator iter = elementData.iterator(); | ||
while (iter.hasNext()) { | ||
builder.append(iter.next()); | ||
builder.append(" "); | ||
} | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
} |
Oops, something went wrong.