Skip to content

Commit

Permalink
Merge pull request #6 from eloiseSJTU/dev
Browse files Browse the repository at this point in the history
finish basic data structures, pass all unit tests
  • Loading branch information
Rong Huang authored Feb 25, 2017
2 parents b0fb337 + 021a19d commit 2ff2c32
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 0 deletions.
3 changes: 3 additions & 0 deletions group02/851113375/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
.classpath
.project
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();
}
}
}
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();
}
}

}
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();
}
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();
}
}
}
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();
}
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;
}
}
Loading

0 comments on commit 2ff2c32

Please sign in to comment.