diff --git a/group26/89460886/src/week01/ArrayList.java b/group26/89460886/src/week01/ArrayList.java new file mode 100644 index 0000000000..7437aca7f6 --- /dev/null +++ b/group26/89460886/src/week01/ArrayList.java @@ -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); + } + } + +} diff --git a/group26/89460886/src/week01/BinaryTree.java b/group26/89460886/src/week01/BinaryTree.java new file mode 100644 index 0000000000..712276be02 --- /dev/null +++ b/group26/89460886/src/week01/BinaryTree.java @@ -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; + } + } + +} diff --git a/group26/89460886/src/week01/Iterator.java b/group26/89460886/src/week01/Iterator.java new file mode 100644 index 0000000000..b79feac764 --- /dev/null +++ b/group26/89460886/src/week01/Iterator.java @@ -0,0 +1,12 @@ +package list; + +/** + * @author jiaxun + */ +public interface Iterator { + + boolean hasNext(); + Object next(); + Object remove(); + +} diff --git a/group26/89460886/src/week01/List.java b/group26/89460886/src/week01/List.java new file mode 100644 index 0000000000..685daafab3 --- /dev/null +++ b/group26/89460886/src/week01/List.java @@ -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(); + +} diff --git a/group26/89460886/src/week01/Queue.java b/group26/89460886/src/week01/Queue.java new file mode 100644 index 0000000000..5bcd9af149 --- /dev/null +++ b/group26/89460886/src/week01/Queue.java @@ -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(); + } + } +} diff --git a/group26/89460886/src/week01/SinglyLinkedList.java b/group26/89460886/src/week01/SinglyLinkedList.java new file mode 100644 index 0000000000..1fed093242 --- /dev/null +++ b/group26/89460886/src/week01/SinglyLinkedList.java @@ -0,0 +1,185 @@ +package list; + +/** + * @author jiaxun + */ +public class SinglyLinkedList implements List { + + private Node head; + private int size; + + public SinglyLinkedList() { + size = 0; + } + + public void addFirst(Object data) { + Node node = new Node(data); + node.setNext(head); + head = node; + size++; + } + + public Node removeFirst() { + Node object = head; + head = object.getNext(); + size--; + return object; + } + + public Node removeLast() { + Node curr = head; + Node prev = null; + while (curr != null) { + prev = curr; + curr = curr.getNext(); + } + if (prev != null) { + prev.setNext(null); + } + size--; + return curr; + } + + public Node get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException(); + } + Node curr = head; + while (curr != null) { + if (index == 0) + break; + curr = curr.getNext(); + index--; + + } + return curr; + } + + public Node remove(int index) { + Node curr = head; + Node prev = null; + while (curr != null) { + if (index == 0) + break; + prev = curr; + curr = curr.getNext(); + index--; + } + if (prev != null) { + prev.setNext(curr.getNext()); + curr.setNext(null); + } + size--; + return curr; + } + + public void addLast(Object object) { + if (head == null) { + head = new Node(object); + } else { + Node curr = head; + Node prev = null; + while (curr != null) { + prev = curr; + curr = curr.getNext(); + } + prev.setNext(new Node(object)); + } + size++; + } + + @Override + public void add(Object o) { + + } + + public void add(int index, Object object) { + Node curr = head; + Node prev = null; + while (curr != null) { + if (index == 0) + break; + prev = curr; + curr = curr.getNext(); + index--; + } + if (prev != null) { + Node newNode = new Node(object); + newNode.setNext(curr); + prev.setNext(newNode); + size++; + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new SinglyLinkedListIterator(this); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + Node current = head; + while (current != null) { + builder.append(current.toString()); + current = current.getNext(); + } + return builder.toString(); + } + + private class SinglyLinkedListIterator implements Iterator { + + private SinglyLinkedList linkedList; + private int currentPosition = 0; + + public SinglyLinkedListIterator(SinglyLinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return currentPosition < size; + } + + @Override + public Object next() { + return linkedList.get(currentPosition++); + } + + @Override + public Object remove() { + return linkedList.remove(--currentPosition); + } + } + + private static class Node { + + private Object data; + private Node next; + + public Node(Object data) { + this.data = data; + } + + public Object getData() { + return data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + @Override + public String toString() { + return "[data is " + getData() + "]"; + } + } + +} diff --git a/group26/89460886/src/week01/Stack.java b/group26/89460886/src/week01/Stack.java new file mode 100644 index 0000000000..f6b4b052d0 --- /dev/null +++ b/group26/89460886/src/week01/Stack.java @@ -0,0 +1,40 @@ +package list; + +/** + * @author jiaxun + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object object) { + elementData.add(object); + } + + public Object pop() { + 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 stringBuilder = new StringBuilder(); + if (size() > 0) { + for (int i = 0, len = size(); i < len; i++) { + stringBuilder.append("[data is ").append(elementData.get(i)).append("]"); + } + } + return stringBuilder.toString(); + } +}