From 97248d3ee35c82038ae8c164a24a7f706f854341 Mon Sep 17 00:00:00 2001 From: lqingchenl <953840070@qq.com> Date: Sun, 26 Feb 2017 08:53:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/ArrayList.java | 104 ++++++++++++++ .../lqingchenl/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 127 ++++++++++++++++++ .../lqingchenl/coding2017/basic/List.java | 9 ++ .../lqingchenl/coding2017/basic/Queue.java | 39 ++++++ .../lqingchenl/coding2017/basic/Stack.java | 46 +++++++ 6 files changed, 332 insertions(+) create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1019222a9e --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java @@ -0,0 +1,104 @@ +package com.github.lqingchenl.coding2017.basic; + +import com.github.lqingchenl.coding2017.basic.List; +import org.junit.Test; + +import java.util.Arrays; + + +public class ArrayList implements List { + private int size = 0; + + private Object[] elementData = new Object[3]; + + /** + * 添加一个元素 + * + * @param o + */ + public void add(Object o) { + elementData[size] = o; + size = size + 1; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + /** + * 往固定位置添加一个元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + size = size + 1; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + // 将指定索引出后面的元素集体向后移动一格 +// for (int i = size; i >= index; i--) { +// elementData[i + 1] = elementData[i]; +// } + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + /** + * 获取元素 + * @param index + * @return + */ + public Object get(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + return elementData[index]; + } + + /** + * 移除元素 + * @param index + * @return + */ + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("索引越界"); + } + Object deleteData = elementData[index]; + if (index == size - 1){ + elementData[index] = null; + }else{ + int movedCount = size - index - 1; + System.arraycopy(elementData, index + 1, elementData, index, movedCount); + } + return deleteData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + /** + * 测试添加、移除、当前大小 + */ +// @Test + public void testArrayList() { + for (int j = 0; j < 3; j++) { + add(j); + } + add(2, 66); + + for (Object z : elementData) { + System.out.println(z); + } + + System.out.println(size()); + System.out.println(remove(3)); + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..086e1cd342 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.lqingchenl.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..9583ef43de --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java @@ -0,0 +1,127 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node nextNode = head; + while (nextNode.next != null) { + nextNode = nextNode.next; + } + nextNode.next = new Node(o); + } + size++; + } + + public void add(int index, Object o) { + Node oldNode = getNode(index); + Node newNode = new Node(o); + newNode.next = oldNode.next; + oldNode.next = newNode; + size++; + } + + public Object get(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Node getNode(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + public Object remove(int index) { + if (index == 1){ + removeFirst(); + } + Node fatherNode = getNode(index -2); + Node oldNode = getNode(index -1); + fatherNode.next = oldNode.next; + size--; + + return oldNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + if (head == null){ + addFirst(o); + } + Node newNode = new Node(o); + Node lastNode = getNode(size -1); + lastNode.next = newNode; + size++; + } + + public Object removeFirst() { + Node oldHead = head; + Node secondNode = head.next; + head = secondNode; + size--; + return oldHead.data; + } + + public Object removeLast() { + if (size == 1){ + removeFirst(); + } + Object data = get(size - 1); + Node oldNode = getNode(size - 2); + oldNode.next = null; + size--; + return data; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } + + @Test + public void testLinkedList() { + add(1); + add(2); + add(3); + add(4); + remove(3); + addFirst(6); + for (int i = 0; i < size; i++) { + System.out.println("=="+get(i) +getNode(i)); + } + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java new file mode 100644 index 0000000000..d993812b9a --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.lqingchenl.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(); +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java new file mode 100644 index 0000000000..c617de0929 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java @@ -0,0 +1,39 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class Queue { + private LinkedList queue = new LinkedList(); + private int size; + + public void enQueue(Object o) { + queue.addLast(o); + size++; + } + + public Object deQueue() { + Object o = queue.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + if (queue.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + + @Test + public void testStack() { + enQueue(1); + enQueue(2); + enQueue(3); + enQueue(4); + System.out.println(deQueue()); //出队列第一个元素 + System.out.println(size()); + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java new file mode 100644 index 0000000000..d3d6caa4dc --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java @@ -0,0 +1,46 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + size--; + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + + @Test + public void testStack(){ + push(1); + push(2); + push(3); + push(4); + System.out.println(pop()); + System.out.println(peek()); + System.out.println(isEmpty()); + System.out.println(size()); + } +} From 571f8a931c96344fdb0f858b4370f64317b21f7d Mon Sep 17 00:00:00 2001 From: lqingchenl <953840070@qq.com> Date: Sun, 26 Feb 2017 13:22:00 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/ArrayList.java | 42 ++---- .../coding2017/basic/ArrayListTest.java | 69 ++++++++++ .../coding2017/basic/LinkedList.java | 37 ++---- .../coding2017/basic/LinkedListTest.java | 125 ++++++++++++++++++ .../lqingchenl/coding2017/basic/Queue.java | 11 -- .../coding2017/basic/QueueTest.java | 57 ++++++++ .../lqingchenl/coding2017/basic/Stack.java | 13 -- .../coding2017/basic/StackTest.java | 64 +++++++++ 8 files changed, 341 insertions(+), 77 deletions(-) create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java index 1019222a9e..286c039e06 100644 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java @@ -1,8 +1,5 @@ package com.github.lqingchenl.coding2017.basic; -import com.github.lqingchenl.coding2017.basic.List; -import org.junit.Test; - import java.util.Arrays; @@ -31,21 +28,21 @@ public void add(Object o) { * @param o */ public void add(int index, Object o) { - size = size + 1; + if (get(index - 1) == null) { //原来为空,添加到指定位置 + add(o); + return; + } + size++; if (size >= elementData.length) { elementData = Arrays.copyOf(elementData, elementData.length * 2); } - // 将指定索引出后面的元素集体向后移动一格 -// for (int i = size; i >= index; i--) { -// elementData[i + 1] = elementData[i]; -// } - System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = o; } /** * 获取元素 + * * @param index * @return */ @@ -59,20 +56,22 @@ public Object get(int index) { /** * 移除元素 + * * @param index * @return */ public Object remove(int index) { - if (index < 0 || index > size - 1) { + if (index < 0 || index > size) { throw new IndexOutOfBoundsException("索引越界"); } Object deleteData = elementData[index]; - if (index == size - 1){ + if (index == size - 1) { elementData[index] = null; - }else{ - int movedCount = size - index - 1; + } else { + int movedCount = size - index; System.arraycopy(elementData, index + 1, elementData, index, movedCount); } + size--; return deleteData; } @@ -84,21 +83,4 @@ public Iterator iterator() { return null; } - /** - * 测试添加、移除、当前大小 - */ -// @Test - public void testArrayList() { - for (int j = 0; j < 3; j++) { - add(j); - } - add(2, 66); - - for (Object z : elementData) { - System.out.println(z); - } - - System.out.println(size()); - System.out.println(remove(3)); - } } diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..ebf29cf406 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java @@ -0,0 +1,69 @@ +package com.github.lqingchenl.coding2017.basic; + +import com.github.lqingchenl.coding2017.basic.ArrayList; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * ArrayList Tester. + */ +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { + testArray.add(1, 1); + testArray.add(2, 2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testArray.add(1); + assertEquals(1, testArray.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.remove(0)); + assertEquals(2, testArray.remove(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(2, testArray.size()); + } + +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java index 9583ef43de..316cad2ee5 100644 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java @@ -1,7 +1,5 @@ package com.github.lqingchenl.coding2017.basic; -import org.junit.Test; - public class LinkedList implements List { private int size = 0; @@ -22,7 +20,11 @@ public void add(Object o) { } public void add(int index, Object o) { - Node oldNode = getNode(index); + if (index == 0) { + addFirst(o); + return; + } + Node oldNode = getNode(index - 1); Node newNode = new Node(o); newNode.next = oldNode.next; oldNode.next = newNode; @@ -46,11 +48,11 @@ public Node getNode(int index) { } public Object remove(int index) { - if (index == 1){ - removeFirst(); + if (index == 1) { + return removeFirst(); } - Node fatherNode = getNode(index -2); - Node oldNode = getNode(index -1); + Node fatherNode = getNode(index - 2); + Node oldNode = getNode(index - 1); fatherNode.next = oldNode.next; size--; @@ -69,11 +71,12 @@ public void addFirst(Object o) { } public void addLast(Object o) { - if (head == null){ + if (head == null) { addFirst(o); + return; } Node newNode = new Node(o); - Node lastNode = getNode(size -1); + Node lastNode = getNode(size - 1); lastNode.next = newNode; size++; } @@ -87,8 +90,8 @@ public Object removeFirst() { } public Object removeLast() { - if (size == 1){ - removeFirst(); + if (size == 1) { + return removeFirst(); } Object data = get(size - 1); Node oldNode = getNode(size - 2); @@ -112,16 +115,4 @@ public Node(Object data) { } - @Test - public void testLinkedList() { - add(1); - add(2); - add(3); - add(4); - remove(3); - addFirst(6); - for (int i = 0; i < size; i++) { - System.out.println("=="+get(i) +getNode(i)); - } - } } diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..0ef6290d28 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java @@ -0,0 +1,125 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class LinkedListTest { + + private static LinkedList testLinkedList = new LinkedList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAdd() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndex() throws Exception { + testLinkedList.add(0, 0); + testLinkedList.add(1, 1); + testLinkedList.add(2, 2); + assertEquals(0, testLinkedList.get(0)); + assertEquals(1, testLinkedList.get(1)); + assertEquals(2, testLinkedList.get(2)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: getNode(int index) + */ + @Test + public void testGetNode() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertNotNull(testLinkedList.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(2, testLinkedList.size()); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { + testLinkedList.addLast(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { + testLinkedList.addFirst(1); + testLinkedList.addFirst(2); + assertEquals(2, testLinkedList.removeFirst()); + assertEquals(1, testLinkedList.removeFirst()); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.removeLast()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + } + + +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java index c617de0929..eb7f7e3cb0 100644 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java @@ -1,7 +1,5 @@ package com.github.lqingchenl.coding2017.basic; -import org.junit.Test; - public class Queue { private LinkedList queue = new LinkedList(); private int size; @@ -27,13 +25,4 @@ public int size() { return size; } - @Test - public void testStack() { - enQueue(1); - enQueue(2); - enQueue(3); - enQueue(4); - System.out.println(deQueue()); //出队列第一个元素 - System.out.println(size()); - } } diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..9b26b3cdcf --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java @@ -0,0 +1,57 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Queue Tester. + */ +public class QueueTest { + + private static Queue testQueue = new Queue(); + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(1, testQueue.deQueue()); + assertEquals(2, testQueue.deQueue()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + assertEquals(true, testQueue.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(2, testQueue.size()); + } + + +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java index d3d6caa4dc..8b25283b40 100644 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java @@ -1,7 +1,5 @@ package com.github.lqingchenl.coding2017.basic; -import org.junit.Test; - public class Stack { private ArrayList elementData = new ArrayList(); private int size = 0; @@ -32,15 +30,4 @@ public int size() { return size; } - @Test - public void testStack(){ - push(1); - push(2); - push(3); - push(4); - System.out.println(pop()); - System.out.println(peek()); - System.out.println(isEmpty()); - System.out.println(size()); - } } diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..b2d4935c4e --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java @@ -0,0 +1,64 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Stack Tester. + */ +public class StackTest { + + private static Stack testStack = new Stack(); + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { + testStack.push(1); + assertEquals(1, testStack.pop()); + } + + /** + * Method: peek() + */ + @Test + public void testPeek() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + testStack.push(2); + assertEquals(2, testStack.peek()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testStack.push(1); + assertEquals(false, testStack.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testStack.push(1); + testStack.push(2); + assertEquals(2, testStack.size()); + } + +} From abd1eca4700b2c234193ca77dbf0fe5692b74acf Mon Sep 17 00:00:00 2001 From: lqingchenl <953840070@qq.com> Date: Sun, 26 Feb 2017 13:36:54 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/ArrayList.java | 86 ------------ .../coding2017/basic/ArrayListTest.java | 69 ---------- .../lqingchenl/coding2017/basic/Iterator.java | 7 - .../coding2017/basic/LinkedList.java | 118 ----------------- .../coding2017/basic/LinkedListTest.java | 125 ------------------ .../lqingchenl/coding2017/basic/List.java | 9 -- .../lqingchenl/coding2017/basic/Queue.java | 28 ---- .../coding2017/basic/QueueTest.java | 57 -------- .../lqingchenl/coding2017/basic/Stack.java | 33 ----- .../coding2017/basic/StackTest.java | 64 --------- 10 files changed, 596 deletions(-) delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java delete mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java deleted file mode 100644 index 286c039e06..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -import java.util.Arrays; - - -public class ArrayList implements List { - private int size = 0; - - private Object[] elementData = new Object[3]; - - /** - * 添加一个元素 - * - * @param o - */ - public void add(Object o) { - elementData[size] = o; - size = size + 1; - if (size >= elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 2); - } - } - - /** - * 往固定位置添加一个元素 - * - * @param index - * @param o - */ - public void add(int index, Object o) { - if (get(index - 1) == null) { //原来为空,添加到指定位置 - add(o); - return; - } - size++; - if (size >= elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 2); - } - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - } - - /** - * 获取元素 - * - * @param index - * @return - */ - public Object get(int index) { - - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("索引越界"); - } - return elementData[index]; - } - - /** - * 移除元素 - * - * @param index - * @return - */ - public Object remove(int index) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("索引越界"); - } - Object deleteData = elementData[index]; - if (index == size - 1) { - elementData[index] = null; - } else { - int movedCount = size - index; - System.arraycopy(elementData, index + 1, elementData, index, movedCount); - } - size--; - return deleteData; - } - - public int size() { - return size; - } - - public Iterator iterator() { - return null; - } - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java deleted file mode 100644 index ebf29cf406..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -import com.github.lqingchenl.coding2017.basic.ArrayList; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.assertEquals; - -/** - * ArrayList Tester. - */ -public class ArrayListTest { - - private static ArrayList testArray = new ArrayList(); - - /** - * Method: add(Object o) - */ - @Test - public void testAddO() throws Exception { - testArray.add(1); - testArray.add(2); - assertEquals(1, testArray.get(0)); - assertEquals(2, testArray.get(1)); - } - - /** - * Method: add(int index, Object o) - */ - @Test - public void testAddForIndexO() throws Exception { - testArray.add(1, 1); - testArray.add(2, 2); - assertEquals(1, testArray.get(0)); - assertEquals(2, testArray.get(1)); - } - - /** - * Method: get(int index) - */ - @Test - public void testGet() throws Exception { - testArray.add(1); - assertEquals(1, testArray.get(0)); - } - - /** - * Method: remove(int index) - */ - @Test - public void testRemove() throws Exception { - testArray.add(1); - testArray.add(2); - assertEquals(1, testArray.remove(0)); - assertEquals(2, testArray.remove(0)); - } - - /** - * Method: size() - */ - @Test - public void testSize() throws Exception { - testArray.add(1); - testArray.add(2); - assertEquals(2, testArray.size()); - } - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java deleted file mode 100644 index 086e1cd342..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java deleted file mode 100644 index 316cad2ee5..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -public class LinkedList implements List { - - private int size = 0; - - private Node head; - - public void add(Object o) { - if (head == null) { - head = new Node(o); - } else { - Node nextNode = head; - while (nextNode.next != null) { - nextNode = nextNode.next; - } - nextNode.next = new Node(o); - } - size++; - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - return; - } - Node oldNode = getNode(index - 1); - Node newNode = new Node(o); - newNode.next = oldNode.next; - oldNode.next = newNode; - size++; - } - - public Object get(int index) { - Node node = head; - for (int i = 0; i < index; i++) { - node = node.next; - } - return node.data; - } - - public Node getNode(int index) { - Node node = head; - for (int i = 0; i < index; i++) { - node = node.next; - } - return node; - } - - public Object remove(int index) { - if (index == 1) { - return removeFirst(); - } - Node fatherNode = getNode(index - 2); - Node oldNode = getNode(index - 1); - fatherNode.next = oldNode.next; - size--; - - return oldNode.data; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - Node newNode = new Node(o); - newNode.next = head; - head = newNode; - size++; - } - - public void addLast(Object o) { - if (head == null) { - addFirst(o); - return; - } - Node newNode = new Node(o); - Node lastNode = getNode(size - 1); - lastNode.next = newNode; - size++; - } - - public Object removeFirst() { - Node oldHead = head; - Node secondNode = head.next; - head = secondNode; - size--; - return oldHead.data; - } - - public Object removeLast() { - if (size == 1) { - return removeFirst(); - } - Object data = get(size - 1); - Node oldNode = getNode(size - 2); - oldNode.next = null; - size--; - return data; - } - - public Iterator iterator() { - return null; - } - - - private static class Node { - Object data; - Node next; - - public Node(Object data) { - this.data = data; - } - - } - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java deleted file mode 100644 index 0ef6290d28..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class LinkedListTest { - - private static LinkedList testLinkedList = new LinkedList(); - - /** - * Method: add(Object o) - */ - @Test - public void testAdd() throws Exception { - testLinkedList.add(1); - testLinkedList.add(2); - assertEquals(1, testLinkedList.get(0)); - assertEquals(2, testLinkedList.get(1)); - } - - /** - * Method: add(int index, Object o) - */ - @Test - public void testAddForIndex() throws Exception { - testLinkedList.add(0, 0); - testLinkedList.add(1, 1); - testLinkedList.add(2, 2); - assertEquals(0, testLinkedList.get(0)); - assertEquals(1, testLinkedList.get(1)); - assertEquals(2, testLinkedList.get(2)); - } - - /** - * Method: get(int index) - */ - @Test - public void testGet() throws Exception { - testLinkedList.add(1); - testLinkedList.add(2); - assertEquals(1, testLinkedList.get(0)); - assertEquals(2, testLinkedList.get(1)); - } - - /** - * Method: getNode(int index) - */ - @Test - public void testGetNode() throws Exception { - testLinkedList.add(1); - testLinkedList.add(2); - assertNotNull(testLinkedList.get(0)); - } - - /** - * Method: remove(int index) - */ - @Test - public void testRemove() throws Exception { - testLinkedList.add(1); - testLinkedList.add(2); - assertEquals(1, testLinkedList.get(0)); - } - - /** - * Method: size() - */ - @Test - public void testSize() throws Exception { - testLinkedList.add(1); - testLinkedList.add(2); - assertEquals(2, testLinkedList.size()); - } - - /** - * Method: addFirst(Object o) - */ - @Test - public void testAddFirst() throws Exception { - testLinkedList.addFirst(1); - assertEquals(1, testLinkedList.get(0)); - } - - /** - * Method: addLast(Object o) - */ - @Test - public void testAddLast() throws Exception { - testLinkedList.addLast(1); - assertEquals(1, testLinkedList.get(0)); - } - - /** - * Method: removeFirst() - */ - @Test - public void testRemoveFirst() throws Exception { - testLinkedList.addFirst(1); - testLinkedList.addFirst(2); - assertEquals(2, testLinkedList.removeFirst()); - assertEquals(1, testLinkedList.removeFirst()); - } - - /** - * Method: removeLast() - */ - @Test - public void testRemoveLast() throws Exception { - testLinkedList.addFirst(1); - assertEquals(1, testLinkedList.removeLast()); - } - - /** - * Method: iterator() - */ - @Test - public void testIterator() throws Exception { - } - - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java deleted file mode 100644 index d993812b9a..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.lqingchenl.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(); -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java deleted file mode 100644 index eb7f7e3cb0..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -public class Queue { - private LinkedList queue = new LinkedList(); - private int size; - - public void enQueue(Object o) { - queue.addLast(o); - size++; - } - - public Object deQueue() { - Object o = queue.removeFirst(); - size--; - return o; - } - - public boolean isEmpty() { - if (queue.size() == 0) - return true; - return false; - } - - public int size() { - return size; - } - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java deleted file mode 100644 index 9b26b3cdcf..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/QueueTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.assertEquals; - -/** - * Queue Tester. - */ -public class QueueTest { - - private static Queue testQueue = new Queue(); - - /** - * Method: enQueue(Object o) - */ - @Test - public void testEnQueue() throws Exception { - testQueue.enQueue(1); - assertEquals(1, testQueue.deQueue()); - } - - /** - * Method: deQueue() - */ - @Test - public void testDeQueue() throws Exception { - testQueue.enQueue(1); - testQueue.enQueue(2); - assertEquals(1, testQueue.deQueue()); - assertEquals(2, testQueue.deQueue()); - } - - /** - * Method: isEmpty() - */ - @Test - public void testIsEmpty() throws Exception { - testQueue.enQueue(1); - assertEquals(1, testQueue.deQueue()); - assertEquals(true, testQueue.isEmpty()); - } - - /** - * Method: size() - */ - @Test - public void testSize() throws Exception { - testQueue.enQueue(1); - testQueue.enQueue(2); - assertEquals(2, testQueue.size()); - } - - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java deleted file mode 100644 index 8b25283b40..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size = 0; - - public void push(Object o) { - elementData.add(o); - size++; - } - - public Object pop() { - Object o = elementData.get(size - 1); - elementData.remove(size - 1); - size--; - return o; - } - - public Object peek() { - return elementData.get(size - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) - return true; - return false; - } - - public int size() { - return size; - } - -} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java deleted file mode 100644 index b2d4935c4e..0000000000 --- a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/StackTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.github.lqingchenl.coding2017.basic; - -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.assertEquals; - -/** - * Stack Tester. - */ -public class StackTest { - - private static Stack testStack = new Stack(); - - /** - * Method: push(Object o) - */ - @Test - public void testPush() throws Exception { - testStack.push(1); - assertEquals(1, testStack.peek()); - } - - /** - * Method: pop() - */ - @Test - public void testPop() throws Exception { - testStack.push(1); - assertEquals(1, testStack.pop()); - } - - /** - * Method: peek() - */ - @Test - public void testPeek() throws Exception { - testStack.push(1); - assertEquals(1, testStack.peek()); - testStack.push(2); - assertEquals(2, testStack.peek()); - } - - /** - * Method: isEmpty() - */ - @Test - public void testIsEmpty() throws Exception { - testStack.push(1); - assertEquals(false, testStack.isEmpty()); - } - - /** - * Method: size() - */ - @Test - public void testSize() throws Exception { - testStack.push(1); - testStack.push(2); - assertEquals(2, testStack.size()); - } - -} From 5cc9d7c19b9ba1f7a4d50ceb3f6300b9e55a9dd9 Mon Sep 17 00:00:00 2001 From: lqingchenl <953840070@qq.com> Date: Sun, 26 Feb 2017 13:45:09 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/ArrayList.java | 86 ++++++++++++ .../coding2017/basic/ArrayListTest.java | 69 ++++++++++ .../lqingchenl/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 118 +++++++++++++++++ .../coding2017/basic/LinkedListTest.java | 125 ++++++++++++++++++ .../lqingchenl/coding2017/basic/List.java | 9 ++ .../lqingchenl/coding2017/basic/Queue.java | 28 ++++ .../coding2017/basic/QueueTest.java | 57 ++++++++ .../lqingchenl/coding2017/basic/Stack.java | 33 +++++ .../coding2017/basic/StackTest.java | 64 +++++++++ 10 files changed, 596 insertions(+) create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayList.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java create mode 100644 group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayList.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..286c039e06 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayList.java @@ -0,0 +1,86 @@ +package com.github.lqingchenl.coding2017.basic; + +import java.util.Arrays; + + +public class ArrayList implements List { + private int size = 0; + + private Object[] elementData = new Object[3]; + + /** + * 添加一个元素 + * + * @param o + */ + public void add(Object o) { + elementData[size] = o; + size = size + 1; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + /** + * 往固定位置添加一个元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + if (get(index - 1) == null) { //原来为空,添加到指定位置 + add(o); + return; + } + size++; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + /** + * 获取元素 + * + * @param index + * @return + */ + public Object get(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + return elementData[index]; + } + + /** + * 移除元素 + * + * @param index + * @return + */ + public Object remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + Object deleteData = elementData[index]; + if (index == size - 1) { + elementData[index] = null; + } else { + int movedCount = size - index; + System.arraycopy(elementData, index + 1, elementData, index, movedCount); + } + size--; + return deleteData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..ebf29cf406 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java @@ -0,0 +1,69 @@ +package com.github.lqingchenl.coding2017.basic; + +import com.github.lqingchenl.coding2017.basic.ArrayList; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * ArrayList Tester. + */ +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { + testArray.add(1, 1); + testArray.add(2, 2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testArray.add(1); + assertEquals(1, testArray.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.remove(0)); + assertEquals(2, testArray.remove(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(2, testArray.size()); + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..086e1cd342 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.lqingchenl.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..316cad2ee5 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java @@ -0,0 +1,118 @@ +package com.github.lqingchenl.coding2017.basic; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node nextNode = head; + while (nextNode.next != null) { + nextNode = nextNode.next; + } + nextNode.next = new Node(o); + } + size++; + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + return; + } + Node oldNode = getNode(index - 1); + Node newNode = new Node(o); + newNode.next = oldNode.next; + oldNode.next = newNode; + size++; + } + + public Object get(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Node getNode(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + public Object remove(int index) { + if (index == 1) { + return removeFirst(); + } + Node fatherNode = getNode(index - 2); + Node oldNode = getNode(index - 1); + fatherNode.next = oldNode.next; + size--; + + return oldNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + if (head == null) { + addFirst(o); + return; + } + Node newNode = new Node(o); + Node lastNode = getNode(size - 1); + lastNode.next = newNode; + size++; + } + + public Object removeFirst() { + Node oldHead = head; + Node secondNode = head.next; + head = secondNode; + size--; + return oldHead.data; + } + + public Object removeLast() { + if (size == 1) { + return removeFirst(); + } + Object data = get(size - 1); + Node oldNode = getNode(size - 2); + oldNode.next = null; + size--; + return data; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..0ef6290d28 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java @@ -0,0 +1,125 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class LinkedListTest { + + private static LinkedList testLinkedList = new LinkedList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAdd() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndex() throws Exception { + testLinkedList.add(0, 0); + testLinkedList.add(1, 1); + testLinkedList.add(2, 2); + assertEquals(0, testLinkedList.get(0)); + assertEquals(1, testLinkedList.get(1)); + assertEquals(2, testLinkedList.get(2)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: getNode(int index) + */ + @Test + public void testGetNode() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertNotNull(testLinkedList.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(2, testLinkedList.size()); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { + testLinkedList.addLast(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { + testLinkedList.addFirst(1); + testLinkedList.addFirst(2); + assertEquals(2, testLinkedList.removeFirst()); + assertEquals(1, testLinkedList.removeFirst()); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.removeLast()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + } + + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java new file mode 100644 index 0000000000..d993812b9a --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.lqingchenl.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(); +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java new file mode 100644 index 0000000000..eb7f7e3cb0 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java @@ -0,0 +1,28 @@ +package com.github.lqingchenl.coding2017.basic; + +public class Queue { + private LinkedList queue = new LinkedList(); + private int size; + + public void enQueue(Object o) { + queue.addLast(o); + size++; + } + + public Object deQueue() { + Object o = queue.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + if (queue.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..9b26b3cdcf --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java @@ -0,0 +1,57 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Queue Tester. + */ +public class QueueTest { + + private static Queue testQueue = new Queue(); + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(1, testQueue.deQueue()); + assertEquals(2, testQueue.deQueue()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + assertEquals(true, testQueue.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(2, testQueue.size()); + } + + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java new file mode 100644 index 0000000000..8b25283b40 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java @@ -0,0 +1,33 @@ +package com.github.lqingchenl.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + size--; + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..b2d4935c4e --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java @@ -0,0 +1,64 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Stack Tester. + */ +public class StackTest { + + private static Stack testStack = new Stack(); + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { + testStack.push(1); + assertEquals(1, testStack.pop()); + } + + /** + * Method: peek() + */ + @Test + public void testPeek() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + testStack.push(2); + assertEquals(2, testStack.peek()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testStack.push(1); + assertEquals(false, testStack.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testStack.push(1); + testStack.push(2); + assertEquals(2, testStack.size()); + } + +}