-
Notifications
You must be signed in to change notification settings - Fork 641
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 #6 from eloiseSJTU/dev
finish basic data structures, pass all unit tests
- Loading branch information
Showing
14 changed files
with
751 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 |
91 changes: 91 additions & 0 deletions
91
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/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,91 @@ | ||
|
||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
ensureCapacity(size + 1); | ||
|
||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkBoundsForAdd(index); | ||
|
||
ensureCapacity(size + 1); | ||
|
||
if (index < size) { | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
checkBounds(index); | ||
|
||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
checkBounds(index); | ||
|
||
Object o = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
elementData[--size] = null; | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator { | ||
|
||
private int cur; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cur != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (cur >= size) { | ||
throw new NoSuchElementException(); | ||
} | ||
return elementData[cur++]; | ||
} | ||
|
||
} | ||
|
||
private void ensureCapacity(int capacity) { | ||
if (capacity > elementData.length) { | ||
capacity = elementData.length << 1; | ||
elementData = Arrays.copyOf(elementData, capacity); | ||
} | ||
} | ||
|
||
private void checkBounds(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private void checkBoundsForAdd(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/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,72 @@ | ||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Integer data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode() {} | ||
|
||
public BinaryTreeNode(Integer data, BinaryTreeNode left, BinaryTreeNode right) { | ||
this.data = data; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
public Integer getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Integer 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(Integer o) { | ||
if (data == null) { | ||
data = o; | ||
} else { | ||
if (o < data) { | ||
if (left == null) { | ||
left = new BinaryTreeNode(o, null, null); | ||
} else { | ||
left = left.insert(o); | ||
} | ||
} else { | ||
if (right == null) { | ||
right = new BinaryTreeNode(o, null, null); | ||
} else { | ||
right = right.insert(o); | ||
} | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public void print() { | ||
if (left != null) { | ||
left.print(); | ||
} | ||
System.out.println(data); | ||
if (right != null) { | ||
right.print(); | ||
} | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/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,6 @@ | ||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
170 changes: 170 additions & 0 deletions
170
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/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,170 @@ | ||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Node head; | ||
|
||
public void add(Object o) { | ||
addLast(o); | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkBoundsForAdd(index); | ||
|
||
if (index == 0) { | ||
addFirst(o); | ||
} else if (index == size) { | ||
addLast(o); | ||
} else { | ||
Node cur = head; | ||
while (--index > 0) { | ||
cur = cur.next; | ||
} | ||
Node newNode = new Node(o, cur.next); | ||
cur.next = newNode; | ||
size++; | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
checkBounds(index); | ||
|
||
Node cur = head; | ||
while (index-- > 0) { | ||
cur = cur.next; | ||
} | ||
return cur.data; | ||
} | ||
|
||
public Object remove(int index) { | ||
checkBounds(index); | ||
|
||
if (index == 0) { | ||
return removeFirst(); | ||
} else if (index == size - 1) { | ||
return removeLast(); | ||
} else { | ||
Node cur = head; | ||
int i = 0; | ||
while (++i < index) { | ||
cur = cur.next; | ||
} | ||
Node node = cur.next; | ||
Object o = node.data; | ||
cur.next = node.next; | ||
node.data = null; | ||
node.next = null; | ||
size--; | ||
return o; | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node newNode = new Node(o, head); | ||
head = newNode; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node newNode = new Node(o, null); | ||
if (head == null) { | ||
head = newNode; | ||
} else { | ||
Node cur = head; | ||
while (cur.next != null) { | ||
cur = cur.next; | ||
} | ||
cur.next = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
public Object removeFirst() { | ||
if (head == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
Object o = head.data; | ||
Node node = head.next; | ||
head.data = null; | ||
head.next = null; | ||
head = node; | ||
size--; | ||
return o; | ||
} | ||
|
||
public Object removeLast() { | ||
if (head == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
if (head.next == null) { | ||
return removeFirst(); | ||
} | ||
|
||
Node cur = head; | ||
int index = 0; | ||
while (++index < size - 1) { | ||
cur = cur.next; | ||
} | ||
Object o = cur.next.data; | ||
cur.next.data = null; | ||
cur.next = null; | ||
size--; | ||
return o; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator { | ||
|
||
private Node cur = head; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cur != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (cur == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
Object o = cur.data; | ||
cur = cur.next; | ||
return o; | ||
} | ||
|
||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
public Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
} | ||
|
||
private void checkBounds(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private void checkBoundsForAdd(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.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,15 @@ | ||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
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(); | ||
|
||
public Iterator iterator(); | ||
} |
28 changes: 28 additions & 0 deletions
28
group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/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,28 @@ | ||
package com.github.eloiseSJTU.coding2017.basic; | ||
|
||
public class Queue { | ||
|
||
private int size = 0; | ||
|
||
private LinkedList elementData = new LinkedList(); | ||
|
||
public void enQueue(Object o) { | ||
elementData.addLast(o); | ||
size++; | ||
} | ||
|
||
public Object deQueue() { | ||
Object o = elementData.get(0); | ||
elementData.removeFirst(); | ||
size--; | ||
return o; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
} |
Oops, something went wrong.