From eddddc3d0bd4af6e8da5df110a73208ce24345cb Mon Sep 17 00:00:00 2001 From: lqt0223 Date: Sat, 25 Feb 2017 22:45:24 +0800 Subject: [PATCH] Collections in Java by lqt0223 564673292 --- .../564673292/com/coding/basic/ArrayList.java | 92 +++++++++++++ .../com/coding/basic/BinaryTreeNode.java | 72 ++++++++++ .../564673292/com/coding/basic/Iterable.java | 5 + .../564673292/com/coding/basic/Iterator.java | 7 + .../com/coding/basic/LinkedList.java | 123 ++++++++++++++++++ group18/564673292/com/coding/basic/List.java | 9 ++ group18/564673292/com/coding/basic/Queue.java | 30 +++++ group18/564673292/com/coding/basic/Stack.java | 50 +++++++ 8 files changed, 388 insertions(+) create mode 100644 group18/564673292/com/coding/basic/ArrayList.java create mode 100644 group18/564673292/com/coding/basic/BinaryTreeNode.java create mode 100644 group18/564673292/com/coding/basic/Iterable.java create mode 100644 group18/564673292/com/coding/basic/Iterator.java create mode 100644 group18/564673292/com/coding/basic/LinkedList.java create mode 100644 group18/564673292/com/coding/basic/List.java create mode 100644 group18/564673292/com/coding/basic/Queue.java create mode 100644 group18/564673292/com/coding/basic/Stack.java diff --git a/group18/564673292/com/coding/basic/ArrayList.java b/group18/564673292/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4bb16148b7 --- /dev/null +++ b/group18/564673292/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterable{ + private E[] array; + private int lastIndex; + private int length; + //Constructor + @SuppressWarnings("unchecked") + public ArrayList(){ + length = 10; + array = (E[])new Object[length]; + lastIndex = 0; + } + + public void add(E object){ + if(lastIndex == length){ + this.grow(); + } + array[lastIndex] = object; + lastIndex++; + } + + @SuppressWarnings("unchecked") + private void grow(){ + E[] tempArray = (E[])new Object[length + 10]; + System.arraycopy(array, 0, tempArray, 0, length); + array = tempArray; + length = length + 10; + } + + public void insert(int index, E o) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + for (int i = lastIndex; i > index; i--) { + array[i] = array[i - 1]; + } + array[index] = o; + length++; + } + + public E get(int index) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + return array[index]; + } + + public E remove(int index){ + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + E removed = array[index]; + for (int i = index; i < lastIndex - 1; i++) { + array[i] = array[i + 1]; + } + lastIndex--; + array[lastIndex] = null; + return removed; + } + + public int size(){ + return lastIndex; + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private ArrayList arrayList; + // constructor + public Itr(ArrayList arrayList){ + this.arrayList = arrayList; + itrCurIndex = -1; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > lastIndex - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.arrayList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.arrayList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/BinaryTreeNode.java b/group18/564673292/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..713e8e4409 --- /dev/null +++ b/group18/564673292/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +// This is a node in a customized binaryTree. The tree have 2 extra features comparing to general binary trees. +// 1. The data of each node are in number class. +// 2. The left child node has a smaller number data than root node, and the right child node has a larger number data that root node. + +package com.coding.basic; + +public class BinaryTreeNode { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + // constructor + public BinaryTreeNode(E data){ + this.data = data; + } + + public E getData() { + return this.data; + } + + public void setData(E data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return this.left; + } + + public boolean setLeft(BinaryTreeNode left) { + if(this.compareWithRoot(left.data) >= 0 || this.left != null){ + System.err.println("The left node data should be smaller than root node."); + return false; + }else{ + this.left = left; + return true; + } + } + + public BinaryTreeNode getRight() { + return this.right; + } + + public boolean setRight(BinaryTreeNode right) { + if(this.compareWithRoot(right.data) <= 0 || this.right != null) { + System.err.println("The right node data should be larger than root node."); + + return false; + }else{ + this.right = right; + return true; + } + } + + private int compareWithRoot(E o){ + return (Integer)o - (Integer)this.getData(); + } + + @SuppressWarnings("unchecked") + public void insert(E o){ + BinaryTreeNode newNode = new BinaryTreeNode(o); + if(!this.setLeft(newNode)){ + if(!this.setRight(newNode)){ + if(this.left.getData() == o){ + this.right.insert(o); + }else{ + this.left.insert(o); + } + } + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterable.java b/group18/564673292/com/coding/basic/Iterable.java new file mode 100644 index 0000000000..e80308012a --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterable.java @@ -0,0 +1,5 @@ +package com.coding.basic; + +public interface Iterable{ + public Iterator iterator(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterator.java b/group18/564673292/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..27d475265e --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public E remove(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/LinkedList.java b/group18/564673292/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..19b12d12da --- /dev/null +++ b/group18/564673292/com/coding/basic/LinkedList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterable { + + private Node head; + private Node last; + private int length; + + private class Node{ + public E data; + public Node next; + + // constructor + private Node(E o, Node n){ + data = o; + next = n; + } + } + + // constructor + public LinkedList(){ + head = new Node(null, null); + last = head; + length = 0; + } + + public void add(E o){ + Node newNode = new Node(o, null); + last.next = newNode; + last = newNode; + length++; + } + + public void insert(int index , E o){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index); + Node nodeToInsert = new Node(o,nextNode); + prevNode.next = nodeToInsert; + length++; + } + + private Node getNode(int index){ + int count = 0; + Node currentNode = head; + while(currentNode.next != null && count <= index){ + currentNode = currentNode.next; + count++; + } + return currentNode; + } + + public E get(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeAtIndex = this.getNode(index); + return nodeAtIndex.data; + } + + public E remove(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeToRemove = this.getNode(index); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index + 1); + prevNode.next = nextNode; + E removedData = nodeToRemove.data; + nodeToRemove = null; + length--; + return removedData; + } + + public int size(){ + return length; + } + + public void addFirst(E o){ + this.insert(0, o); + } + public void addLast(E o){ + this.add(o); + + } + public E removeFirst(){ + return this.remove(0); + } + public E removeLast(){ + return this.remove(length - 1); + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private Node currentNode; + private LinkedList linkedList; + + public Itr(LinkedList linkedList){ + itrCurIndex = -1; + currentNode = head; + this.linkedList = linkedList; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > length - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.linkedList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.linkedList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/List.java b/group18/564673292/com/coding/basic/List.java new file mode 100644 index 0000000000..04a7ac992e --- /dev/null +++ b/group18/564673292/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void insert(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Queue.java b/group18/564673292/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b40f06afc8 --- /dev/null +++ b/group18/564673292/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedList; + + // constructor + public Queue(){ + linkedList = new LinkedList(); + } + + public void enQueue(E o){ + linkedList.addLast(o); + } + + public E deQueue(){ + return linkedList.removeFirst(); + } + + public E peek(){ + return linkedList.get(0); + } + + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + public int size(){ + return linkedList.size(); + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Stack.java b/group18/564673292/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b1b129fc40 --- /dev/null +++ b/group18/564673292/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack{ + private ArrayList arrayList; + + // constructor + public Stack(){ + arrayList = new ArrayList(); + } + + public void push(E o){ + arrayList.add(o); + } + + public E pop(){ + return arrayList.remove(arrayList.size() - 1); + } + + public E peek(){ + return arrayList.get(arrayList.size() - 1); + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true: false; + } + + public int size(){ + return arrayList.size(); + } + + // public Iterator iterator(){ + // return new Itr(); + // } + + // private class Itr implements Iterator{ + // Iterator arrayListItr = arrayList.iterator(); + // public boolean hasNext(){ + // return arrayListItr.hasNext(); + // } + + // public E next(){ + // return arrayListItr.next(); + // } + + // @Override // Stack iterator can only remove the last element + // public E remove(){ + // return arrayList.pop(); + // } + // } +} \ No newline at end of file