diff --git a/group20/404130810/src/com/basic/datastructure/ArrayList.java b/group20/404130810/src/com/basic/datastructure/ArrayList.java index 8aa4575882..86c001b2c1 100644 --- a/group20/404130810/src/com/basic/datastructure/ArrayList.java +++ b/group20/404130810/src/com/basic/datastructure/ArrayList.java @@ -97,12 +97,14 @@ private Object[] growIfNeeded() { public static void main(String[] args) { ArrayList list = new ArrayList(); - for (int i = 1; i <= 11; i++) { + for (int i = 0; i <= 11; i++) { list.add(String.valueOf(i)); } - list.add(10,"test"); - list.get(10); - list.remove(10); - System.out.println(list); + System.out.println(list.get(11)); + //list.add(10,"test"); + //list.get(10); + //list.remove(10); + //System.out.println(list); } + } diff --git a/group20/404130810/src/com/basic/datastructure/LinkedList.java b/group20/404130810/src/com/basic/datastructure/LinkedList.java index c0981aa8c4..43ba7ddd59 100644 --- a/group20/404130810/src/com/basic/datastructure/LinkedList.java +++ b/group20/404130810/src/com/basic/datastructure/LinkedList.java @@ -6,27 +6,32 @@ public class LinkedList implements List { private int size; - public void add(Object o) { - addLast(o); + public void add(Object item) { + addLast(item); } - public void add(int index, Object o) { + public void add(int index, Object item) { checkRange(index); if (index == 0) { - addFirst(o); + addFirst(item); } else if (index == size) { - addLast(o); + addLast(item); } else { - + Node tmpNode = new Node(item); + Node preNode = get(index - 1); + Node nextNode = get(index + 1); + preNode.next = tmpNode; + tmpNode.next = nextNode; + size ++; } } - public Object get(int index) { + public Node get(int index) { checkRange(index); if(size > 0){ int loopTimes = 0; Node p = first; - while(index > loopTimes){ + while(index > loopTimes){ p = p.next; loopTimes ++; } @@ -37,31 +42,45 @@ public Object get(int index) { } public Object remove(int index) { - return null; + checkRange(index); + Node tmpNode = null; + if(index == 0){ + removeFirst(); + }else if(index == size -1){ + removeLast(); + }else{ + tmpNode = get(index); + Node preNode = get(index-1); + Node nextNode = get(index + 1); + preNode.next = nextNode; + size --; + } + + return tmpNode; } public int size() { return size; } - private void addFirst(Object o) { + public void addFirst(Object item) { if (size == 0) { - first = new Node(o); + first = new Node(item); last = first; } else { - Node tmpNode = new Node(o); + Node tmpNode = new Node(item); tmpNode.next = first; first = tmpNode; } size++; } - private void addLast(Object o) { + public void addLast(Object item) { if (size == 0) { - first = new Node(o); + first = new Node(item); last = first; } else { - last.next = new Node(o); + last.next = new Node(item); last = last.next; } size++; @@ -69,15 +88,31 @@ private void addLast(Object o) { public Object removeFirst() { Node tmpNode = first; - first = first.next; - size--; + if(tmpNode == null){ + last = null; + }else if(size == 2){ + first = last; + last = null; + size --; + }else{ + first = first.next; + size--; + } return tmpNode; } public Object removeLast() { - Node tmpNode = last; - - return null; + Node tmpNode = null; + if(size == 1){ + this.removeFirst(); + }else if(size >0){ + int index = size - 1; + tmpNode = last; + get(index - 1).next = null; + last = get(index - 1); + size --; + } + return tmpNode; } private void checkRange(int index) { @@ -99,19 +134,43 @@ public String toString() { private static class Node { private Object item; private Node next; - Node(Object item) { this.item = item; } } public static void main(String[] args) { + + /*Test add LinkedList list = new LinkedList(); - for (int i = 0; i < 5; i++) { + for (int i = 0; i <= 5; i++) { list.add(i); - } - list.get(5); - + } + list.add(3, "test"); + System.out.println(list); + */ + + /*Test remove + list.remove(3); + System.out.println(list); + */ + + /*Test removeLast and removeFirst + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); System.out.println(list); + */ + + /*Test from Java API + java.util.LinkedList linkedList = new java.util.LinkedList(); + linkedList.add("test"); + linkedList.removeFirst(); + System.out.println(linkedList); + */ + } } diff --git a/group20/404130810/src/com/basic/datastructure/Queue.java b/group20/404130810/src/com/basic/datastructure/Queue.java new file mode 100644 index 0000000000..f83d937e5c --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/Queue.java @@ -0,0 +1,33 @@ +package com.basic.datastructure; + +public class Queue { + LinkedList list = new LinkedList(); + private int size; + + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + size --; + return list.removeLast(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return size; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + queue.deQueue(); + System.out.println("Finished"); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/Stack.java b/group20/404130810/src/com/basic/datastructure/Stack.java new file mode 100644 index 0000000000..7a58fd49e6 --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/Stack.java @@ -0,0 +1,41 @@ +package com.basic.datastructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + public Object pop(){ + size --; + return elementData.remove(elementData.size() - 1); + } + + /** + * Looks at the object at the top of this stack without removing it from the stack. + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + stack.push(i); + } + System.out.println(stack.peek()); + + stack.pop(); + System.out.println("Finished"); + } + +}