Skip to content

Commit

Permalink
Merge pull request #8 from lqt0223/master
Browse files Browse the repository at this point in the history
Collections in Java
  • Loading branch information
haolipeng authored Feb 27, 2017
2 parents 91eeb6e + eddddc3 commit c399cd9
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 0 deletions.
92 changes: 92 additions & 0 deletions group18/564673292/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.coding.basic;

public class ArrayList<E> implements List<E>, Iterable<E>{
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<E> iterator(){
return new Itr(this);
}

private class Itr implements Iterator<E>{
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);
}
}
}
72 changes: 72 additions & 0 deletions group18/564673292/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -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<E> {

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<E> 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<E> 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);
}
}
}
}
}
5 changes: 5 additions & 0 deletions group18/564673292/com/coding/basic/Iterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coding.basic;

public interface Iterable<E>{
public Iterator<E> iterator();
}
7 changes: 7 additions & 0 deletions group18/564673292/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coding.basic;

public interface Iterator<E> {
public boolean hasNext();
public E next();
public E remove();
}
123 changes: 123 additions & 0 deletions group18/564673292/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.coding.basic;

public class LinkedList<E> implements List<E>, Iterable<E> {

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<E> iterator(){
return new Itr(this);
}

private class Itr implements Iterator<E>{
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);
}
}
}
9 changes: 9 additions & 0 deletions group18/564673292/com/coding/basic/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coding.basic;

public interface List<E> {
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();
}
30 changes: 30 additions & 0 deletions group18/564673292/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.coding.basic;

public class Queue<E> {
private LinkedList<E> linkedList;

// constructor
public Queue(){
linkedList = new LinkedList<E>();
}

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

0 comments on commit c399cd9

Please sign in to comment.