From 4550485e298ac7f53b6ce81e606d092569f043c6 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 23 Feb 2017 16:09:38 +0800 Subject: [PATCH 01/37] Add my first code except BinaryTreeNode --- .../coding2017/basic/ArrayListTest.java | 14 ++ .../HarryHook/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedListTest.java | 93 ++++++++ .../HarryHook/coding2017/basic/List.java | 14 ++ .../HarryHook/coding2017/basic/ListTest.java | 118 ++++++++++ .../coding2017/basic/MyArrayList.java | 153 ++++++++++++ .../coding2017/basic/MyLinkedList.java | 219 ++++++++++++++++++ .../HarryHook/coding2017/basic/MyQueue.java | 54 +++++ .../HarryHook/coding2017/basic/MyStack.java | 59 +++++ .../HarryHook/coding2017/basic/QueueTest.java | 33 +++ .../HarryHook/coding2017/basic/StackTest.java | 40 ++++ 11 files changed, 804 insertions(+) create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..bd15f2a404 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.HarryHook.coding2017.basic; + +import org.junit.Before; +import com.github.HarryHook.coding2017.basic.MyArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new MyArrayList(); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3fae84a22f --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.HarryHook.coding2017.basic; + +public interface Iterator +{ + public boolean hasNext(); + public Object next(); +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..314c774dea --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.HarryHook.coding2017.basic.MyLinkedList; + +public class LinkedListTest extends ListTest{ + + private MyLinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new MyLinkedList(); + aLinkedList = new MyLinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java new file mode 100644 index 0000000000..f2299e8e83 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java @@ -0,0 +1,14 @@ +package com.github.HarryHook.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(); + + public Iterator iterator(); +} + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java new file mode 100644 index 0000000000..b5680990ea --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java @@ -0,0 +1,118 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.HarryHook.coding2017.basic.Iterator; +import com.github.HarryHook.coding2017.basic.List; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + //protected static List aList; + + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + } + + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..3ceb922c53 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java @@ -0,0 +1,153 @@ +/* + * created by Harry 2017-2-20 18:53:38 + * 实现简单的ArrayList,具有基本的增删改查功能 + */ +package com.github.HarryHook.coding2017.basic; + +import java.util.Arrays; + +public class MyArrayList implements List +{ + private int size = 0; //数组元素个数 + + private Object[] elementData = new Object[10]; //初始化数组大小为10 + + //将元素添加到数组尾部 + public void add(Object o) + { //需要判断数组空间是否够用 + ensureCapacity(size + 1); + elementData[size++] = o; + } + //在指定位置添加元素 + public void add(int index, Object o) + { + //判断下标记是否越界 + if (index > size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + ensureCapacity(size + 1); + //判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 + if(elementData[index] == null) + { + elementData[index] = o; + } + else + { + for(int i=elementData.length-1; i>index; i--) + { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size++; + /* + //判断索引位置是否正确 + if (index > size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + //扩容检测 + ensureCapacity(size+1); + /* + * 对源数组进行复制处理(位移),从index + 1到size-index。 + * 主要目的就是空出index位置供数据插入, + * 即向右移动当前位于该位置的元素以及所有后续元素。 + + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + //在指定位置赋值 + elementData[index] = 0; + size++; + + */ + } + + public Object get(int index) + { + return elementData[index]; + } + + public Object remove(int index) + { //涉及到元素移位 + Object oldValue = elementData[index]; + for(int i=index; i oldCapacity) + { + Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + //返回数组的大小 + public int size() + { + return size; + } + + public Iterator iterator() + { + return new MyArrayListIterator(); + } + + private class MyArrayListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + Object next = get(cursor); + cursor ++; + return next; + } + } + + public static void main(String[] args) + { + MyArrayList myArrays = new MyArrayList(); + + for(int i = 0; i < 19; i++) + myArrays.add(i, 55); + + myArrays.add(3); + myArrays.add(0, 11); + myArrays.add(1, 2); + myArrays.add(3, 5); + myArrays.add(2, 1); + myArrays.add(7); + System.out.println("获取指定位置元素: " + myArrays.get(2)); + System.out.println("删除指定位置元素: " + myArrays.remove(1)); + System.out.println("当前元素个数:" + myArrays.size()); + + Print(myArrays); + + } + public static void Print(MyArrayList myArrays) + { + Iterator it = myArrays.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myArrays.size()); + + } + +} + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..5aeab57496 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java @@ -0,0 +1,219 @@ +/* + * created by Harry 2017-2-21 14:43:41 + * 实现简单的LinkedList + */ + +package com.github.HarryHook.coding2017.basic; + +public class MyLinkedList implements List +{ + private Node head = null; //头指针 + private int size = 0; + private static class Node + { + Object data; + Node next; + } + public void add(Object o) + { + addLast(o); + } + //在指定位置添加元素 + public void add(int index , Object o) + { + + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //存在插入头结点的情况 + if(index == 0) + addFirst(o); + else + { //即 index != 0 的情况 + // p保存待插入节点的前一节点,x指向要插入的节点 + Node x = head; + Node p = null; + int i = 0; + while(i < index) + { + p = x; + x = x.next; + i++; + } + Node n = new Node(); + p.next = n; + n.next = x; + n.data = o; + size++; + } + + } + //返回指定位置元素 + public Object get(int index) + { + Node x = head; + int i = 0; + while(i < index && x != null) + { + x = x.next; + i++; + } + return x.data; + } + + //移除指定位置节点 + public Object remove(int index) + { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //先判断是否是头节点 + if( index == 0) + { + return removeFirst(); + } + else + { + Node x = head; + Node pre = null; + int i = 0; + while(i < index) + { + pre = x; + x = x.next; + i++; + } + Object Data = pre.next.data; + pre.next = x.next; + x = null; + size--; + return Data; + } + + } + //头部添加节点 + public void addFirst(Object o) + { + Node n = new Node(); + n.next = head; + head = n; + n.data = o; + size++; + } + //尾部添加节点 + public void addLast(Object o) + { + if (head == null) + { + head = new Node(); + head.data = o; + } + else + { + Node x = head; + while(x.next != null) + { + x = x.next; + } + Node n = new Node(); + x.next = n; + n.next = null; + n.data = o; + } + size++; + } + //移除第一个节点 + public Object removeFirst() + { + Node n = head; + Object Data = n.data; + head = head.next; + n = null; + size--; + return Data; + } + + //移除最后一个节点 + //removeLast()方法存在bug + public Object removeLast() + { + Node x = head; + Node p = null; + if(x.next == null) + { + return removeFirst(); + } + else + { + while(x.next != null) + { + p = x; + x = x.next; + } + Object Data = x.data; + p.next = null; + x = null; //删除最后一个节点 + size--; + return Data; + } + } + public int size(){ + return size; + } + public Iterator iterator() + { + return new MyLinkedListIterator(); + } + private class MyLinkedListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + Object next = get(cursor); + cursor ++; + return next; + } + } + public static void main(String[] args) + { + MyLinkedList myList = new MyLinkedList(); + myList.add(3); + myList.add(5); + myList.add(0, 4); + myList.add(2, 7); + myList.addFirst(1); + myList.addLast(6); + + Print(myList); + + System.out.println("当前指定位置元素: " + myList.get(1)); + System.out.println("移除指定位置元素: " + myList.remove(4)); + Print(myList); + + System.out.println("移除第一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + + } + public static void Print(MyLinkedList myList) + { + Iterator it = myList.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myList.size()); + System.out.println(""); + } + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..9b713eaea9 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java @@ -0,0 +1,54 @@ +/* + * created by Harry 2017-2-22 13:06:43 + * 实现简单的队列 + */ +package com.github.HarryHook.coding2017.basic; +import java.util.*; +public class MyQueue +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + //入队 + public void enQueue(Object o) + { + elementData.add(o); + size++; + } + //出队 + public Object deQueue() + { + if(isEmpty()) + throw new NoSuchElementException(); + Object Data = elementData.remove(0); + size--; + return Data; + } + //判断队列是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //队列中元素个数 + public int size() + { + return size; + } + public static void main(String[] args) + { + MyQueue mq = new MyQueue(); + mq.enQueue(1); + mq.enQueue(2); + mq.enQueue(3); + mq.enQueue(4); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + //System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + //System.out.println("队列中元素个数: " + mq.size()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..c7f87c04e6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java @@ -0,0 +1,59 @@ +/* + * created by Harry 2017-2-22 10:48:34 + * 实现简单的Stack + */ +package com.github.HarryHook.coding2017.basic; + +import java.util.*; + +public class MyStack +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + + //入栈操作 + public void push(Object o) + { + elementData.add(o); + size++; + } + //出栈操作 + public Object pop() + { + Object obj = peek(); + elementData.remove(size() - 1); + size--; + return obj; + } + //获取当前栈顶元素,不用出栈 + public Object peek() + { + if(isEmpty()) + throw new EmptyStackException(); + return elementData.get(size() - 1); + } + //判断栈是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //返回栈内元素个数 + public int size(){ + return size; + } + + public static void main(String[] args) + { + MyStack ms = new MyStack(); + + ms.push(1); + ms.push(2); + ms.push(13); + System.out.println("当前栈顶元素是: " + ms.peek()); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("出栈元素是: " + ms.pop()); + ms.push(12); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("当前栈顶元素是: " + ms.peek()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..340f79d240 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.HarryHook.coding2017.basic.MyQueue; + +public class QueueTest { + private MyQueue queue; + + @Before + public void setUpQueue() { + queue = new MyQueue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..26160faef6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.MyStack; + +public class StackTest { + + private MyStack stack; + + @Before + public void setUpStack() { + stack = new MyStack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From f3f4984007781bd25d41e53a1fdf1329434aabfd Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 23 Feb 2017 18:22:54 +0800 Subject: [PATCH 02/37] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FelixCJF/coding2017/basic/ArrayList.java | 97 ++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++ .../FelixCJF/coding2017/basic/Iterator.java | 8 + .../FelixCJF/coding2017/basic/LinkedList.java | 214 ++++++++++++++++++ .../FelixCJF/coding2017/basic/List.java | 11 + .../FelixCJF/coding2017/basic/Queue.java | 53 +++++ .../FelixCJF/coding2017/basic/Stack.java | 36 +++ .../coding2017/basic/test/ArrayListTest.java | 14 ++ .../coding2017/basic/test/LinkedListTest.java | 97 ++++++++ .../coding2017/basic/test/ListTest.java | 118 ++++++++++ .../coding2017/basic/test/QueueTest.java | 34 +++ .../coding2017/basic/test/StackTest.java | 41 ++++ 12 files changed, 755 insertions(+) create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java create mode 100644 group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e287d1779a --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.github.FelixCJF.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //容量增加 + ensureCapacity(size + 1); + //添加 + elementData[size++] = o; + } + + public void add(int index, Object o){ + //容量增加 + ensureCapacity(size + 1); + //临时变量 + Object[] elementData3 = new Object[size + 1]; + //将index前数据复制 + for (int i = 0; i < index + 1 ; i++) { + elementData3[i] = elementData[i]; + } + //插入的数据 + elementData3 [index + 1] = o; + //插入数据之后的后半段复制 + for (int i = index + 2 ; i < elementData3.length; i++) { + elementData3[i] = elementData[i-1]; + } + elementData = elementData3; + size++; + } + + public Object get(int index){ + if (index < 0 || index >= this.size) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= this.size) { + throw new IndexOutOfBoundsException(); + } + Object oldValue = elementData[index]; + Object[] elementData4 = new Object[size - 1]; + for (int i = 0; i < index; i++) { + elementData4[i] = elementData[i]; + } + for (int i = index; i < elementData4.length; i++) { + elementData4[i] = elementData[i + 1]; + } + elementData = elementData4; + size--; + return oldValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + //内部类,实现Iterator + private class ArrayListIterator implements Iterator{ + + private int currentIndex = 0; //当前索引 + + public boolean hasNext() { + if (currentIndex >= size) { + return false; + } + return true; + } + + public Object next() { + Object object = elementData[currentIndex]; + currentIndex ++ ; + return object; + } + } + public void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + int newCapacity = (oldCapacity * 3) / 2 + 1; + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6dccc25dcb --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.FelixCJF.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3a1b9abf8c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.github.FelixCJF.coding2017.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..d86e970b8a --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java @@ -0,0 +1,214 @@ +package com.github.FelixCJF.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head;//头指针 + private Node last;//尾指针 + private int size = 0; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + + if (index == size) { + addLast(o); + } else { + final Node pred = indexNode.prv; + + final Node newNode = new Node(); + newNode.data = o; + newNode.next = indexNode; + newNode.prv = pred; + + indexNode.prv = newNode; + + if (pred == null) { + head = newNode; + } else { + pred.next = newNode; + } + } + size ++; + } + public Object get(int index){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + + return indexNode.data; + } + public Object remove(int index){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + Object element = indexNode.data; + Node pre = indexNode.prv; + Node next = indexNode.next; + + if (pre == null) { + head = next; + } else { + pre.next = next; + indexNode.prv = null; + } + + if (next == null) { + last = pre; + } else { + next.prv = pre; + indexNode.next = null; + } + + indexNode.data = null; + + size --; + + return element; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + //节点变量存放原来的头指针 + final Node oldHead = head; + //创建新的节点对象 + final Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + newNode.prv = null; + //判断oldhead是否为null + if (oldHead == null) { + last = newNode; + }else { + //头指针指向新创建的节点对象 + oldHead.prv = newNode; + } + //将newNode变为头指针 + head = newNode; + size ++; + } + public void addLast(Object o){ + //节点新变量放原先的尾指针 + final Node oldLast = last; + //创建新节点,加入要添加的对象 + final Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + newNode.prv = oldLast; + if (oldLast == null) { + head = newNode; + } else { + //尾指针指向新创建的节点 + oldLast.next = newNode; + } + //newNode变为尾指针 + last = newNode; + size++; + } + public Object removeFirst(){ + //通过头指针创建头节点 + final Node hNode = head; + if (hNode == null) { + throw new NoSuchElementException(); + } + final Node next = hNode.next; + final Object element = hNode.data; + + //移除 + hNode.data = null; + hNode.next = null; + head = next; + //判断是否为尾节点 + if (next == null) { + last = null; + }else { + next.prv = null; + } + size --; + return element; + } + public Object removeLast(){ + //通过尾指针创建节点 + final Node lastNode = last; + if (lastNode == null) { + throw new NoSuchElementException(); + } + final Object element = lastNode.data; + final Node prve = lastNode.prv; + + //移除 + lastNode.data = null; + lastNode.prv = null; + last = prve; + + if (prve == null) { + head = null; + } else { + prve.next = null; + } + size --; + return element; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + + private Node currentNode = head;//当前节点 + + public boolean hasNext() { + if (currentNode == null) { + return false; + } + return true; + } + + public Object next() { + Object element = currentNode.data; + currentNode = currentNode.next; + return element; + } + + } + + //查找index节点,并返回该节点 + Node node(int index) { + // assert isElementIndex(index); + + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prv; + return x; + } + } + //检查索引 + private void checkIndex(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + private static class Node{ + Object data; + Node next; + Node prv; + } +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java new file mode 100644 index 0000000000..ecbb657597 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java @@ -0,0 +1,11 @@ +package com.github.FelixCJF.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(); + public Iterator iterator(); +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java new file mode 100644 index 0000000000..c2661ce35f --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java @@ -0,0 +1,53 @@ +package com.github.FelixCJF.coding2017.basic; + + +public class Queue { + + private Node head;//头节点 + private Node last;//尾节点 + private int size;//记录节点 + + public void enQueue(Object o){ + //设置一个节点变量存放原先的尾节点 + final Node oldLast = last; + //创建一个新的节点 + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + //添加到队列 + if (isEmpty()) { + head = newNode; + } else { + oldLast.next = newNode; + } + //新节点变为尾节点 + last = newNode; + size ++; + } + + public Object deQueue(){ + + Object object = head.data; + + head = head.next; + + if (isEmpty()) { + last = null; + } + size --; + return object; + } + + public boolean isEmpty(){ + return head == null; + } + + public int size(){ + return size; + } + + private static class Node{ + Object data; + Node next; + } +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java new file mode 100644 index 0000000000..cbb7e0683c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java @@ -0,0 +1,36 @@ +package com.github.FelixCJF.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + + //存放栈内元素的容器 + private ArrayList elementData = new ArrayList(); + //记录栈内元素个数 + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(size - 1); + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.get(size - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return size; + } +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..51f2c1115c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import org.junit.Before; + +import com.github.FelixCJF.coding2017.basic.ArrayList; + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..b990f0327e --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,97 @@ +package com.github.FelixCJF.coding2017.basic.test; + + + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..d9c52595d1 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java @@ -0,0 +1,118 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.FelixCJF.coding2017.basic.Iterator; +import com.github.FelixCJF.coding2017.basic.List; + +public class ListTest { + + + protected static List aList; + + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..49506c2b35 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java @@ -0,0 +1,34 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..6bb53571a5 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java @@ -0,0 +1,41 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.Stack; + + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From 478e4c475e9b9284d795dfc96b8e28ba57b2ef49 Mon Sep 17 00:00:00 2001 From: thomas_young Date: Fri, 24 Feb 2017 07:34:35 +0800 Subject: [PATCH 03/37] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=EF=BC=8C=E5=B0=9A=E6=9C=AA=E5=AE=8C=E6=88=90=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group02/812350401/.gitignore | 2 + .../coding2017/basic/ArrayList.java | 77 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++++ .../miniyk2012/coding2017/basic/Iterator.java | 6 + .../coding2017/basic/IteratorImp.java | 24 ++++ .../coding2017/basic/LinkedList.java | 103 ++++++++++++++++ .../miniyk2012/coding2017/basic/List.java | 10 ++ .../miniyk2012/coding2017/basic/Queue.java | 23 ++++ .../miniyk2012/coding2017/basic/Stack.java | 25 ++++ .../coding2017/basic/test/ArrayListTest.java | 14 +++ .../basic/test/BinaryTreeNodeTest.java | 33 +++++ .../coding2017/basic/test/LinkedListTest.java | 93 ++++++++++++++ .../coding2017/basic/test/ListTest.java | 115 ++++++++++++++++++ .../coding2017/basic/test/QueueTest.java | 33 +++++ .../coding2017/basic/test/StackTest.java | 40 ++++++ 15 files changed, 630 insertions(+) create mode 100644 group02/812350401/.gitignore create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore new file mode 100644 index 0000000000..e6416c5be6 --- /dev/null +++ b/group02/812350401/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/lib/ diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e6bd69b5d8 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.github.miniyk2012.coding2017.basic; + +import java.util.Arrays; + + +public class ArrayList implements List { + + private int size = 0; + private int DEFAULT_LENGTH = 10; + + private Object[] elementData = new Object[DEFAULT_LENGTH]; + + public void add(Object o){ + ensureCapcacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + ensurnPosition(index); + ensureCapcacity(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + ensureIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + ensureIndex(index); + Object temp = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index); + size--; + return temp; + } + + public void clear() { + elementData = new Object[DEFAULT_LENGTH]; + size = 0; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + Iterator iterator = new IteratorImp(this); + return iterator; + } + + private void ensureCapcacity() { + int oldLength = elementData.length; + if (size+1 > oldLength) { + elementData = Arrays.copyOf(elementData, 2*oldLength); + } + } + + private void ensureIndex(int index) { + if (index >= size || index < 0) + throw new ArrayIndexOutOfBoundsException(String.format("index %d out of bounds [0-%d)", index, size)); + } + + private void ensurnPosition(int index) { + if (index <0 || index>size) + throw new ArrayIndexOutOfBoundsException(String.format("position %d out of position [0-%d)", index, size)); + } + + @Override + public String toString() { + Object[] tempArray = Arrays.copyOf(elementData, size); + return Arrays.toString(tempArray); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2b47a098ff --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.miniyk2012.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..229ba41b05 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java new file mode 100644 index 0000000000..cfca8eaac7 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java @@ -0,0 +1,24 @@ +package com.github.miniyk2012.coding2017.basic; + +public class IteratorImp implements Iterator { + + private List aList; + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != aList.size(); + } + + @Override + public Object next() { + int i = cursor; + Object next = aList.get(i); + cursor = i+1; + return next; + } + + public IteratorImp(List aList) { + this.aList = aList; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..e34d76001b --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java @@ -0,0 +1,103 @@ +package com.github.miniyk2012.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + /** + * node接收的index一定是范围内的值,不可能越界 + * @param index + * @return a Node + */ + Node node(int index) { + Node x = head; + for (int i=0; i= size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + + private void checkPositionIndex(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java new file mode 100644 index 0000000000..ff6718f462 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java @@ -0,0 +1,10 @@ +package com.github.miniyk2012.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(); + public Iterator iterator(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java new file mode 100644 index 0000000000..bcb9d0f9e4 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java @@ -0,0 +1,23 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Queue { + + // 队列入口 -》1,2,3,4 -》队列出口 + private LinkedList aList = new LinkedList(); + + public void enQueue(Object o){ + aList.addFirst(o); + } + + public Object deQueue(){ + return aList.removeLast(); + } + + public boolean isEmpty(){ + return aList.size() == 0; + } + + public int size(){ + return aList.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ddfe76f774 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Stack { + + // 栈顶 《-》 1,2,3,4 栈底 + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0, o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..b308c6bfef --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import org.junit.Before; +import com.github.miniyk2012.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..99798b3222 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + assertEquals(4, binaryTreeNode.getData()); + assertEquals(1, binaryTreeNode.getLeft().getData()); + assertEquals(5, binaryTreeNode.getRight().getData()); + assertEquals(3, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(2, binaryTreeNode.getLeft().getRight().getLeft().getData()); + // binaryTreeNode.print(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..487fd2ee00 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..3d871f832d --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java @@ -0,0 +1,115 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.basic.Iterator; +import com.github.miniyk2012.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..67035c1dcf --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..a9b4e14afd --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.miniyk2012.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From c707d7dae24aceb5b2a3bd0b33fb7794138cafe5 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 24 Feb 2017 10:22:11 +0800 Subject: [PATCH 04/37] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FelixCJF/coding2017/basic/ArrayList.java | 96 ++++++++++--------- .../FelixCJF/coding2017/basic/Stack.java | 8 +- .../coding2017/basic/test/ArrayListTest.java | 4 +- .../coding2017/basic/test/LinkedListTest.java | 3 + .../coding2017/basic/test/ListTest.java | 10 +- 5 files changed, 68 insertions(+), 53 deletions(-) diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java index e287d1779a..133db97491 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java @@ -1,6 +1,5 @@ package com.github.FelixCJF.coding2017.basic; -import java.util.Arrays; public class ArrayList implements List { @@ -12,50 +11,44 @@ public void add(Object o){ //容量增加 ensureCapacity(size + 1); //添加 - elementData[size++] = o; + elementData[size ++] = o; } public void add(int index, Object o){ - //容量增加 - ensureCapacity(size + 1); - //临时变量 - Object[] elementData3 = new Object[size + 1]; - //将index前数据复制 - for (int i = 0; i < index + 1 ; i++) { - elementData3[i] = elementData[i]; - } - //插入的数据 - elementData3 [index + 1] = o; - //插入数据之后的后半段复制 - for (int i = index + 2 ; i < elementData3.length; i++) { - elementData3[i] = elementData[i-1]; - } - elementData = elementData3; - size++; + + //检查是否越界 + rangeCheck(index); + // 进行扩容检查 + ensureCapacity(size + 1); + // 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置 + System. arraycopy(elementData, index, elementData, index + 1, + size - index); + // 将指定的index位置赋值为Object o + elementData[index] = o; + // 自增一位长度 + size++; } public Object get(int index){ - if (index < 0 || index >= this.size) { - throw new IndexOutOfBoundsException(); - } + rangeCheck(index); return elementData[index]; } public Object remove(int index){ - if (index < 0 || index >= this.size) { - throw new IndexOutOfBoundsException(); - } - Object oldValue = elementData[index]; - Object[] elementData4 = new Object[size - 1]; - for (int i = 0; i < index; i++) { - elementData4[i] = elementData[i]; - } - for (int i = index; i < elementData4.length; i++) { - elementData4[i] = elementData[i + 1]; - } - elementData = elementData4; - size--; - return oldValue; + // 数组越界检查 + rangeCheck(index); + // 取出要删除位置的元素,供返回使用 + Object oldValue = elementData[index]; + // 计算数组要复制的数量 + int numMoved = size - index - 1; + // 数组复制,就是将index之后的元素往前移动一个位置 + if (numMoved > 0) + System. arraycopy(elementData, index+1, elementData, index, + numMoved); + // 将数组最后一个元素置空(因为删除了一个元素,然后index后面的元素都向前移动了,所以最后一个就没用了),好让gc尽快回收 + // 不要忘了size减一 + elementData[--size] = null; + return oldValue; } public int size(){ @@ -84,14 +77,29 @@ public Object next() { return object; } } - public void ensureCapacity(int minCapacity) { - int oldCapacity = elementData.length; - if (minCapacity > oldCapacity) { - int newCapacity = (oldCapacity * 3) / 2 + 1; - if (newCapacity < minCapacity) - newCapacity = minCapacity; - elementData = Arrays.copyOf(elementData, newCapacity); - } + //扩容 + public void ensureCapacity( int minCapacity) { + // 当前数组的长度 + int oldCapacity = elementData .length; + // 最小需要的容量大于当前数组的长度则进行扩容 + if (minCapacity > oldCapacity) { + // 扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1); + // 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + //数组复制 + Object[] elementData2 = new Object[newCapacity]; + for (int i = 0; i < oldCapacity; i++) { + elementData2[i] = elementData[i]; + } + elementData = elementData2; + } + } + //检查是否越界 + private void rangeCheck(int index){ + if (index < 0 || index >= this.size) { + throw new IndexOutOfBoundsException("index :" + index + "size :" + size); + } } - } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java index cbb7e0683c..16684f7d92 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java @@ -7,30 +7,28 @@ public class Stack { //存放栈内元素的容器 private ArrayList elementData = new ArrayList(); //记录栈内元素个数 - private int size = 0; public void push(Object o){ elementData.add(o); - size ++; } public Object pop(){ if (isEmpty()) { throw new EmptyStackException(); } - return elementData.remove(size - 1); + return elementData.remove(elementData.size() - 1); } public Object peek(){ if (isEmpty()) { throw new EmptyStackException(); } - return elementData.get(size - 1); + return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ return elementData.size() == 0; } public int size(){ - return size; + return elementData.size(); } } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java index 51f2c1115c..9484ce1527 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java @@ -6,9 +6,9 @@ public class ArrayListTest extends ListTest { - @Before +/* @Before public void setUpArrayList() { aList = new ArrayList(); - } + }*/ } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java index b990f0327e..ce0c0d1c0d 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java @@ -7,7 +7,9 @@ import org.junit.Before; import org.junit.Test; +import com.github.FelixCJF.coding2017.basic.ArrayList; import com.github.FelixCJF.coding2017.basic.LinkedList; +import com.github.FelixCJF.coding2017.basic.List; public class LinkedListTest extends ListTest{ @@ -15,6 +17,7 @@ public class LinkedListTest extends ListTest{ @Before public void setUpLinkedList() { + List aList = new ArrayList(); aList = new LinkedList(); aLinkedList = new LinkedList(); } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java index d9c52595d1..b970372bbe 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java @@ -6,17 +6,18 @@ import org.junit.Test; import org.junit.rules.ExpectedException; +import com.github.FelixCJF.coding2017.basic.ArrayList; import com.github.FelixCJF.coding2017.basic.Iterator; import com.github.FelixCJF.coding2017.basic.List; public class ListTest { - protected static List aList; - + //protected static List aList = new ArrayList(); @Test public void testFunctional() { + List aList = new ArrayList(); aList.add(1); aList.add(2); assertEquals(1, aList.get(0)); @@ -41,6 +42,7 @@ public void testFunctional() { @Test public void testAdd() { + List aList = new ArrayList(); for (int i=0; i<100; i++) aList.add(i); assertEquals(0, aList.get(0)); @@ -50,6 +52,7 @@ public void testAdd() { @Test public void testRemove() { + List aList = new ArrayList(); aList.add(1); aList.add(2); aList.add(3); @@ -73,6 +76,7 @@ public void testRemove() { @Test public void testSize() { + List aList = new ArrayList(); for (int i=0; i<10; i++) aList.add(i*2); assertEquals(10, aList.size()); @@ -83,6 +87,7 @@ public void testSize() { @Test public void testException() { + List aList = new ArrayList(); expectedEx.expect(Exception.class); aList.remove(1); @@ -93,6 +98,7 @@ public void testException() { @Test public void testIterator() { + List aList = new ArrayList(); Iterator it = aList.iterator(); assertEquals(false, it.hasNext()); From 250c46efb36079a16d68b398194d209ec5ba5dcf Mon Sep 17 00:00:00 2001 From: thomas_young Date: Sat, 25 Feb 2017 10:02:24 +0800 Subject: [PATCH 05/37] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group02/812350401/.gitignore | 1 + .../coding2017/basic/BinaryTreeNode.java | 49 ++++++++++++++----- .../basic/test/BinaryTreeNodeTest.java | 16 +++--- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore index e6416c5be6..c24866af6d 100644 --- a/group02/812350401/.gitignore +++ b/group02/812350401/.gitignore @@ -1,2 +1,3 @@ /bin/ /lib/ +/src/java_training diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java index 2b47a098ff..5b1f17342b 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -1,32 +1,55 @@ package com.github.miniyk2012.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode > { + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { + public BinaryTreeNode(E x) { + data = x; + } + public E getData() { return data; } - public void setData(Object data) { + public void setData(E data) { this.data = data; } - public BinaryTreeNode getLeft() { + public BinaryTreeNode getLeft() { return left; } - public void setLeft(BinaryTreeNode left) { + public void setLeft(BinaryTreeNode left) { this.left = left; } - public BinaryTreeNode getRight() { + public BinaryTreeNode getRight() { return right; } - public void setRight(BinaryTreeNode right) { + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; + public BinaryTreeNode insert(E o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode currentNode = this; + BinaryTreeNode previousNode = null; + + while (currentNode != null) { + previousNode = currentNode; + int compareTo = node.getData().compareTo(currentNode.getData()); + if (compareTo <= 0) { // 小于,往左插入 + currentNode = currentNode.left; + left = true; + } else { + currentNode = currentNode.right; + left = false; + } + } + if (left) { + previousNode.left = node; + } else { + previousNode.right = node; + } + return node; } } diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java index 99798b3222..36f5dff484 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -8,26 +8,24 @@ public class BinaryTreeNodeTest { - private BinaryTreeNode binaryTreeNode; + private BinaryTreeNode binaryTreeNode; @Before public void setUpBinaryTreeNode() { - binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode = new BinaryTreeNode(4); } @Test public void testBinaryTreeNodeFunctional() { - binaryTreeNode.insert(4); binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); - assertEquals(4, binaryTreeNode.getData()); - assertEquals(1, binaryTreeNode.getLeft().getData()); - assertEquals(5, binaryTreeNode.getRight().getData()); - assertEquals(3, binaryTreeNode.getLeft().getRight().getData()); - assertEquals(2, binaryTreeNode.getLeft().getRight().getLeft().getData()); - // binaryTreeNode.print(); + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); } } From c27c036fe16bc949bce4ccee4ac8718bef8e12ef Mon Sep 17 00:00:00 2001 From: fei9009 Date: Fri, 24 Feb 2017 22:48:11 -0600 Subject: [PATCH 06/37] implementation of basic data structures --- .../fei9009/coding2017/basic/ArrayList.java | 71 +++++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 57 ++++++++++++ .../fei9009/coding2017/basic/Iterator.java | 7 ++ .../fei9009/coding2017/basic/LinkedList.java | 91 +++++++++++++++++++ .../github/fei9009/coding2017/basic/List.java | 9 ++ .../fei9009/coding2017/basic/Queue.java | 21 +++++ .../fei9009/coding2017/basic/Stack.java | 26 ++++++ 7 files changed, 282 insertions(+) create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/List.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Queue.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Stack.java diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..855e25b257 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.github.fei9009.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + private int capacity; + + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.capacity = 20; + } + + private void extend() { + Object[] updatedElementData = new Object[this.elementData.length + this.capacity]; + System.arraycopy(this.elementData, 0, updatedElementData, 0, this.elementData.length); + this.elementData = updatedElementData; + } + + public void add(Object o){ + if (this.size == elementData.length) { + extend(); + } + elementData[size] = o; + this.size++; + } + + public void add(int index, Object o){ + if (this.size == elementData.length) { + extend(); + } + int i; + for (i = this.size - 1; i >= index; i--) { + this.elementData[i + 1] = this.elementData[i]; + } + this.elementData[i + 1] = o; + this.size++; + } + + public Object get(int index){ + if (index >= 0 && index < this.size) { + return this.elementData[index]; + }else { + return null; + } + } + + public Object remove(int index){ + if (index >= 0 && index < this.size) { + int i = 0; + Object deletedElement = this.elementData[index]; + for (i = index + 1; i < this.size; i++) { + this.elementData[i - 1] = this.elementData[i]; + } + this.elementData[i] = null; + this.size--; + return deletedElement; + }else { + return null; + } + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6dbc7ee012 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package com.github.fei9009.coding2017.basic; + +public class BinaryTreeNode > { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode cur = this; + BinaryTreeNode pre = null; + while (cur != null) { + pre = cur; + if (node.getData().compareTo(cur.getData()) <= 0) { + cur = cur.left; + left = true; + } else { + cur = cur.right; + left = false; + } + } + if(left) { + pre.left = node; + } else { + pre.right = node; + } + return node; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..e2f03b8247 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.fei9009.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..1ed5923ee9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java @@ -0,0 +1,91 @@ +package com.github.fei9009.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node dummy = node(index); + Node newNode = new Node(o); + newNode.next = dummy; + if (index == 0) { + head = newNode; + } + else { + Node before = node(index-1); + before.next = newNode; + } + this.size++; + + } + + public Object get(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node node = node(index); + return node.data; + + } + public Object remove(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node delNode = node(index); + if (index == 0) + head = delNode.next; + else { + Node before = node(index-1); + before.next = delNode.next; + } + size--; + return delNode.data; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(this.size -1); + } + public Iterator iterator(){ + return null; + } + + Node node(int index) { + Node x = head; + for (int i=0; i Date: Sat, 25 Feb 2017 13:09:30 +0800 Subject: [PATCH 07/37] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/test/BinaryTreeNodeTest.java | 49 +++++++++++++++++-- .../coding2017/basic/test/ListTest.java | 4 +- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java index 36f5dff484..57be8bbfef 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -3,29 +3,68 @@ import static org.junit.Assert.assertEquals; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; + import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; public class BinaryTreeNodeTest { private BinaryTreeNode binaryTreeNode; + /** + // 4 + // 1 5 + // 2 3 + */ @Before public void setUpBinaryTreeNode() { binaryTreeNode = new BinaryTreeNode(4); - } - - @Test - public void testBinaryTreeNodeFunctional() { binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); + } + + @Test + public void testBinaryTreeNodeFunctional1() { assertEquals(new Integer(4), binaryTreeNode.getData()); assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); } - + + @Test + public void testBinaryTreeFunctional2() { + BinaryTreeNode node1 = binaryTreeNode.getLeft(); + assertEquals(new Integer(1), node1.getData()); + assertEquals(new Integer(3), node1.getRight().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testBinaryTreeFunctional3() + { + BinaryTreeNode treeNode = new BinaryTreeNode(100); + treeNode.insert(10); + binaryTreeNode.setRight(treeNode); + // 4 + // 1 100 + // 2 3 10 + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); + + expectedEx.expect(Exception.class); + binaryTreeNode.getRight().getRight().getRight(); // null exception + binaryTreeNode.getRight().getRight().getLeft(); // null exception + } } diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java index 3d871f832d..587bf17f4d 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java @@ -85,7 +85,6 @@ public void testException() { aList.remove(1); aList.add(3); aList.add(2, 5); - expectedEx.expect(Exception.class); } @Test @@ -110,6 +109,9 @@ public void testIterator() { assertEquals(1, it.next()); assertEquals(3, it.next()); assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); } } From 0b24942964290e05b390b3711b08700fcd39fe1d Mon Sep 17 00:00:00 2001 From: FengYang <447809161@qq.com> Date: Sat, 25 Feb 2017 17:10:31 +0800 Subject: [PATCH 08/37] =?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 --- .../hwjcc969/coding2017/basic/ArrayList.java | 62 ++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++++ .../hwjcc969/coding2017/basic/Iterator.java | 7 ++ .../hwjcc969/coding2017/basic/LinkedList.java | 117 ++++++++++++++++++ .../hwjcc969/coding2017/basic/List.java | 9 ++ .../hwjcc969/coding2017/basic/Queue.java | 27 ++++ .../hwjcc969/coding2017/basic/Stack.java | 31 +++++ 7 files changed, 285 insertions(+) create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..c2acf20e4d --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java @@ -0,0 +1,62 @@ +package com.github.hwjcc969.coding2017.basic; + +import javax.lang.model.element.Element; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (o == null){ + throw new NullPointerException("空对象"); + } + if (size >= 100){ + throw new RuntimeException("越界"); + } + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (o == null){ + throw new NullPointerException("空对象"); + } + if (index >= 99){ + throw new RuntimeException("越界"); + } + for (int i = size; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index >= 100 || index < 0){ + throw new RuntimeException("越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index >= 100 || index < 0){ + throw new RuntimeException("越界"); + } + Object o = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return null; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..0381f88c69 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.hwjcc969.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..0e8394844b --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.hwjcc969.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..d000967c19 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java @@ -0,0 +1,117 @@ +package com.github.hwjcc969.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + Node newNode = new Node(o); + if (head == null) { + head = newNode; + } + else{ + Node last = head.next; + while (last != null){ + last = last.next; + } + last = newNode; + } + size++; + + } + public void add(int index , Object o){ + Node dummy = new Node(); + dummy.next = head; + Node pre = dummy; + while (index > 0) { + pre = pre.next; + } + Node cur = new Node(o); + cur.next = pre.next; + pre.next = cur; + size++; + } + public Object get(int index){ + Node cur = head; + while (index > 0 && cur != null) { + cur = cur.next; + } + return cur; + } + public Object remove(int index){ + Node dummy = new Node(); + dummy.next = head; + Node pre = dummy; + while (index > 0) { + pre = pre.next; + } + Node cur = pre.next; + Node next = pre.next.next; + pre.next = next; + size--; + return cur; + + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node firstNode = new Node(o); + firstNode.next = head; + head = firstNode; + } + public void addLast(Object o){ + Node pre = new Node(); + while (pre.next != null){ + pre = pre.next; + } + Node lastNode = new Node(o); + pre.next = lastNode; + } + public Object removeFirst(){ + Node dummy = new Node(); + dummy.next = head; + dummy.next = head.next; + return dummy.next; + } + public Object removeLast(){ + Node pre = new Node(); + while (pre.next.next != null) { + pre = pre.next; + } + Node lastNode = pre.next.next; + pre.next = null; + return lastNode; + + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + public Node(Object o) { + // TODO Auto-generated constructor stub + } + public Node() { + // TODO Auto-generated constructor stub + } + Object data; + Node next; + + } + + + public boolean isEmpty() { + // TODO Auto-generated method stub + if (size() == 0){ + return false; + } + return true; + } + + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java new file mode 100644 index 0000000000..56d67bdf29 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.hwjcc969.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/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java new file mode 100644 index 0000000000..f7cb7b1ad4 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java @@ -0,0 +1,27 @@ +package com.github.hwjcc969.coding2017.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue(){ + if (!list.isEmpty()){ + return list.removeFirst(); + } + return "空队列"; + + } + + public boolean isEmpty(){ + if (list.size() == 0){ + return false; + } + return true; + } + + public int size(){ + return list.size(); + } +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java new file mode 100644 index 0000000000..a4229a7d12 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.hwjcc969.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + if (elementData.size() <= 99) + elementData.add(o); + size++; + + } + + public Object pop(){ + size--; + return elementData.remove(elementData.size() - 1); + + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + if (size == 0) + return false; + return true; + } + public int size(){ + return size; + } +} From 692b81293abbbce56bcebf932f84da5c01adb9ef Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 25 Feb 2017 23:51:58 +0800 Subject: [PATCH 09/37] Add my first code v1.0 BinaryTreeNode has a bug --- .../coding2017/basic/ArrayListTest.java | 3 +- .../coding2017/basic/BinaryTreeNode.java | 121 ++++++++++++++++++ .../coding2017/basic/BinaryTreeNodeTest.java | 40 ++++++ .../coding2017/basic/LinkedListTest.java | 2 +- .../HarryHook/coding2017/basic/ListTest.java | 8 +- .../coding2017/basic/MyArrayList.java | 32 +++-- .../src/com/coding/basic/BinaryTreeNode.java | 93 +++++++++++++- liuxin/src/com/coding/basic/insert.java | 37 ++++++ 8 files changed, 317 insertions(+), 19 deletions(-) create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java create mode 100644 liuxin/src/com/coding/basic/insert.java diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java index bd15f2a404..0f22f6ba80 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java @@ -7,7 +7,8 @@ public class ArrayListTest extends ListTest { @Before - public void setUpArrayList() { + public void setUpArrayList() + { aList = new MyArrayList(); } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..09ad8c0fd5 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,121 @@ +/* + * Created by Harry 2017-2-23 10:50:39 + * 实现二叉树,并按二叉查找树插入节点 + * 能实现基本的功能,但测试时存在问题,传进去的数据为空 2017-2-2523:49:43 + */ +package com.github.HarryHook.coding2017.basic; + +public class BinaryTreeNode +{ + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + //中序遍历二叉树 + public void inOrder(BinaryTreeNode node) + { + if(node != null) + { + inOrder(node.left); + System.out.print(" " + node.data); + inOrder(node.right); + } + } + + //获取给节点的值 + public Integer getData() + { + return this.data; + } + //给一个节点赋值 + public void setData(Integer data) + { + this.data = data; + } + //获取左节点 + public BinaryTreeNode getLeft() + { + return left; + } + //指定左节点 + public void setLeft(BinaryTreeNode left) + { + this.left = left; + } + + //获取右节点 + public BinaryTreeNode getRight() + { + return right; + } + //指定右节点 + public void setRight(BinaryTreeNode right) + { + this.right = right; + } + + //在二叉树中插入一个节点,需要判断 + public BinaryTreeNode insert(Integer obj) + { + // 新增节点 + BinaryTreeNode newNode = new BinaryTreeNode(); + // 当前节点,保留根的值 + BinaryTreeNode current = this; + // 上个节点 + BinaryTreeNode parent = null; + // 如果根节点为空 + if (this.data == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + return newNode; + }else + { + while (true) + { + parent = current; + if (obj < current.data) + { + current = current.left; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.right = newNode; + return newNode; + } + } + } + } + } + + public static void main(String[] args) + { + BinaryTreeNode BTN = new BinaryTreeNode(); + + BTN = BTN.insert(5); + System.out.print(BTN.getData() + " "); + System.out.print(BTN.insert(2).getData() + " "); + System.out.print(BTN.insert(1).getData() + " "); + System.out.print(BTN.insert(4).getData() + " "); + System.out.print(BTN.insert(6).getData() + " "); + System.out.print(BTN.insert(8).getData() + " "); + System.out.println(""); + System.out.println("中序遍历二叉树: "); + BTN.inOrder(BTN); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..7f706c24b5 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest +{ + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + + @Before + public void setUpBinaryTreeNode() + { + //binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() + { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + + //节点为空 说明值没有插进去 + binaryTreeNode.inOrder(binaryTreeNode); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java index 314c774dea..7c754e37af 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java @@ -77,7 +77,7 @@ public void testLinkedListFunctional() { assertEquals(5, aLinkedList.size()); assertEquals(5, aLinkedList.get(0)); assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); + assertEquals(0, aLinkedList.get(3)); aLinkedList.remove(3); // [5, 4, 1, 3] assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java index b5680990ea..92f84b687c 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java @@ -88,9 +88,9 @@ public void testException() { } @Test - //protected static List aList; - public void testIterator() { + public void testIterator() + { Iterator it = aList.iterator(); assertEquals(false, it.hasNext()); @@ -111,6 +111,10 @@ public void testIterator() { assertEquals(1, it.next()); assertEquals(3, it.next()); assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java index 3ceb922c53..dbfd8aae19 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java @@ -5,6 +5,7 @@ package com.github.HarryHook.coding2017.basic; import java.util.Arrays; +import java.util.NoSuchElementException; public class MyArrayList implements List { @@ -40,6 +41,7 @@ public void add(int index, Object o) elementData[index] = o; } size++; + /* //判断索引位置是否正确 if (index > size || index < 0) @@ -62,8 +64,12 @@ public void add(int index, Object o) } public Object get(int index) - { + { + //若index超出size应该抛出异常 + if(index >= size) + throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); return elementData[index]; + } public Object remove(int index) @@ -83,7 +89,7 @@ public void ensureCapacity(int minCapacity) int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { - Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 if (newCapacity < minCapacity) newCapacity = minCapacity; @@ -112,25 +118,33 @@ public boolean hasNext() } public Object next() { - Object next = get(cursor); - cursor ++; - return next; + try { + Object next = get(cursor); + cursor++; + return next; + + } catch (IndexOutOfBoundsException e) + { + throw new NoSuchElementException(); + } + } } public static void main(String[] args) { MyArrayList myArrays = new MyArrayList(); - - for(int i = 0; i < 19; i++) - myArrays.add(i, 55); - myArrays.add(3); myArrays.add(0, 11); myArrays.add(1, 2); myArrays.add(3, 5); myArrays.add(2, 1); myArrays.add(7); + Print(myArrays); + + for(int i = 0; i < 19; i++) + myArrays.add(i, 55); + System.out.println("获取指定位置元素: " + myArrays.get(2)); System.out.println("删除指定位置元素: " + myArrays.remove(1)); System.out.println("当前元素个数:" + myArrays.size()); diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index d7ac820192..736ae328c7 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,11 +1,51 @@ package com.coding.basic; -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - +public class BinaryTreeNode +{ + public static TreeNode root; //根节点 + public BinaryTreeNode() + { + this.root = null; + } + public TreeNode insert (int key) + { + // 新增节点 + TreeNode newNode = new TreeNode(key); + // 当前节点 + TreeNode current = root; + // 上个节点 + TreeNode parent = null; + // 如果根节点为空 + if (current == null) + { + root = newNode; + return newNode; + } + while (true) + { + parent = current; + if (key < current.value) + { + current = current.left; + if (current == null) + { + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + parent.right = newNode; + return newNode; + } + } + } + } + +} + public Object getData() { return data; } @@ -30,3 +70,44 @@ public BinaryTreeNode insert(Object o){ } } + +class TreeNode //树的节点 +{ + int value; + TreeNode left; + TreeNode right; + + public TreeNode(int value) + { + this.value = value; + left = null; + right = null; + } +} + +public class BinaryTreeNodeTest { + + public static void main(String[] args) { + BinaryTreeNode b = new BinaryTreeNode(); + b.insert(3);b.insert(8);b.insert(1);b.insert(4);b.insert(6); + b.insert(2);b.insert(10);b.insert(9);b.insert(20);b.insert(25); + + // 打印二叉树 + b.toString(b.root); + System.out.println(); + + // 是否存在节点值10 + TreeNode node01 = b.search(10); + System.out.println("是否存在节点值为10 => " + node01.value); + // 是否存在节点值11 + TreeNode node02 = b.search(11); + System.out.println("是否存在节点值为11 => " + node02); + + // 删除节点8 + TreeNode node03 = b.delete(8); + System.out.println("删除节点8 => " + node03.value); + b.toString(b.root); + + + } +} diff --git a/liuxin/src/com/coding/basic/insert.java b/liuxin/src/com/coding/basic/insert.java new file mode 100644 index 0000000000..9ded2fcfac --- /dev/null +++ b/liuxin/src/com/coding/basic/insert.java @@ -0,0 +1,37 @@ + + //2017年2月24日17:02:55 + public BinaryTreeNode insert (Integer key) + { + // 新增节点 + BinaryTreeNode newNode = new BinaryTreeNode(key); + // 当前节点 + BinaryTreeNode current = root; + // 上个节点 + BinaryTreeNode parent = null; + // 如果根节点为空 + if (current == null) { + root = newNode; + return newNode; + } + while (true) + { + parent = current; + if (key < current.value) + { + current = current.left; + if (current == null) + { + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + parent.right = newNode; + return newNode; + } + } + } + } From 021a19dca5a6ac62f0fa523cd1e72ddc9d6e903a Mon Sep 17 00:00:00 2001 From: eloise <851113375@qq.com> Date: Sat, 25 Feb 2017 23:57:51 +0800 Subject: [PATCH 10/37] finish basic data structures, pass all unit tests --- group02/851113375/.gitignore | 3 + .../coding2017/basic/ArrayList.java | 91 ++++++++++ .../coding2017/basic/BinaryTreeNode.java | 72 ++++++++ .../eloiseSJTU/coding2017/basic/Iterator.java | 6 + .../coding2017/basic/LinkedList.java | 170 ++++++++++++++++++ .../eloiseSJTU/coding2017/basic/List.java | 15 ++ .../eloiseSJTU/coding2017/basic/Queue.java | 28 +++ .../eloiseSJTU/coding2017/basic/Stack.java | 31 ++++ .../coding2017/basic/test/ArrayListTest.java | 15 ++ .../basic/test/BinaryTreeNodeTest.java | 34 ++++ .../coding2017/basic/test/LinkedListTest.java | 93 ++++++++++ .../coding2017/basic/test/ListTest.java | 120 +++++++++++++ .../coding2017/basic/test/QueueTest.java | 33 ++++ .../coding2017/basic/test/StackTest.java | 40 +++++ 14 files changed, 751 insertions(+) create mode 100644 group02/851113375/.gitignore create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java diff --git a/group02/851113375/.gitignore b/group02/851113375/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/851113375/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1aa396ce37 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java @@ -0,0 +1,91 @@ + +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + ensureCapacity(size + 1); + + if (index < size) { + System.arraycopy(elementData, index, elementData, index + 1, size - index); + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkBounds(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkBounds(index); + + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private int cur; + + @Override + public boolean hasNext() { + return cur != size; + } + + @Override + public Object next() { + if (cur >= size) { + throw new NoSuchElementException(); + } + return elementData[cur++]; + } + + } + + private void ensureCapacity(int capacity) { + if (capacity > elementData.length) { + capacity = elementData.length << 1; + elementData = Arrays.copyOf(elementData, capacity); + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..9aa88f2155 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode() {} + + public BinaryTreeNode(Integer data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Integer getData() { + return data; + } + + public void setData(Integer data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Integer o) { + if (data == null) { + data = o; + } else { + if (o < data) { + if (left == null) { + left = new BinaryTreeNode(o, null, null); + } else { + left = left.insert(o); + } + } else { + if (right == null) { + right = new BinaryTreeNode(o, null, null); + } else { + right = right.insert(o); + } + } + } + return this; + } + + public void print() { + if (left != null) { + left.print(); + } + System.out.println(data); + if (right != null) { + right.print(); + } + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..c37495db66 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..19833b1853 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node cur = head; + while (--index > 0) { + cur = cur.next; + } + Node newNode = new Node(o, cur.next); + cur.next = newNode; + size++; + } + } + + public Object get(int index) { + checkBounds(index); + + Node cur = head; + while (index-- > 0) { + cur = cur.next; + } + return cur.data; + } + + public Object remove(int index) { + checkBounds(index); + + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node cur = head; + int i = 0; + while (++i < index) { + cur = cur.next; + } + Node node = cur.next; + Object o = node.data; + cur.next = node.next; + node.data = null; + node.next = null; + size--; + return o; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + Node cur = head; + while (cur.next != null) { + cur = cur.next; + } + cur.next = newNode; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + + Object o = head.data; + Node node = head.next; + head.data = null; + head.next = null; + head = node; + size--; + return o; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + + if (head.next == null) { + return removeFirst(); + } + + Node cur = head; + int index = 0; + while (++index < size - 1) { + cur = cur.next; + } + Object o = cur.next.data; + cur.next.data = null; + cur.next = null; + size--; + return o; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private Node cur = head; + + @Override + public boolean hasNext() { + return cur != null; + } + + @Override + public Object next() { + if (cur == null) { + throw new NoSuchElementException(); + } + Object o = cur.data; + cur = cur.next; + return o; + } + + } + + private static class Node { + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java new file mode 100644 index 0000000000..2f6bdcc950 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.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(); + + public Iterator iterator(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java new file mode 100644 index 0000000000..96e6547548 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java @@ -0,0 +1,28 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Queue { + + private int size = 0; + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.addLast(o); + size++; + } + + public Object deQueue() { + Object o = elementData.get(0); + elementData.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java new file mode 100644 index 0000000000..22b5117842 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Stack { + + private int size = 0; + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(--size); + elementData.remove(size); + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..a2bd0cad69 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import org.junit.Before; + +import com.github.eloiseSJTU.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..3da3cf3fa0 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,34 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + binaryTreeNode.print(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..0ad905694c --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..956ecb9167 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java @@ -0,0 +1,120 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.eloiseSJTU.coding2017.basic.Iterator; +import com.github.eloiseSJTU.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i = 0; i < 1000; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(100, aList.get(100)); + assertEquals(999, aList.get(999)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i = 0; i < 10; i++) + aList.add(i * 2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + + aList.add(3); + + expectedEx.expect(Exception.class); + aList.add(2, 5); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..39023e8aaa --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..98d2bd9de9 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From dca48221d3371cf1f237c76959468758e8ef3c43 Mon Sep 17 00:00:00 2001 From: fei9009 Date: Sat, 25 Feb 2017 10:03:12 -0600 Subject: [PATCH 11/37] update junit test --- group02/527705641/.gitignore | 1 + .../coding2017/basic/ArrayListTest.java | 55 +++++++++++ .../coding2017/basic/LinkedListTest.java | 92 +++++++++++++++++++ .../fei9009/coding2017/basic/QueueTest.java | 35 +++++++ .../fei9009/coding2017/basic/StackTest.java | 37 ++++++++ 5 files changed, 220 insertions(+) create mode 100644 group02/527705641/.gitignore create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java diff --git a/group02/527705641/.gitignore b/group02/527705641/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group02/527705641/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..d623dd05e0 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java @@ -0,0 +1,55 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + @Before + public void setUp() throws Exception { + //testArray.clear(); + } + + @Test + public void testArrayList() { + //fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + testArray.add(10); + testArray.add(0, 3); + testArray.add(0, 2); + assertEquals(3, testArray.get(1)); + assertEquals(2, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testGet() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..1eb379a020 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java @@ -0,0 +1,92 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + +private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..3e2889edc0 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java @@ -0,0 +1,35 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + +private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..2872efb3e3 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java @@ -0,0 +1,37 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + +private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } +} From 8d011fa3948e4aa6e4a2f1b3003a83e53aec719a Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 11:51:09 +1100 Subject: [PATCH 12/37] Finish Except Tree --- group02/609990377/DataStructure/.gitignore | 3 + .../coding2017/basic/ArrayList.java | 113 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++++ .../coding2017/basic/Iterator.java | 8 + .../coding2017/basic/LinkedList.java | 163 ++++++++++++++++++ .../coding2017/basic/List.java | 9 + .../coding2017/basic/Queue.java | 30 ++++ .../coding2017/basic/Stack.java | 28 +++ 8 files changed, 386 insertions(+) create mode 100644 group02/609990377/DataStructure/.gitignore create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java diff --git a/group02/609990377/DataStructure/.gitignore b/group02/609990377/DataStructure/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/609990377/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cfdc5fefe1 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + + //check size limit + if(size + 1 > elementData.length){ + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + //Check Object class + if(size >= 1){ + if(o.getClass() != elementData[0].getClass()) + throw new InputMismatchException("Index:"+index+" Size:"+size); + } + + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + //check size limit + if(size + 1 > elementData.length){ + Object oldData[] = elementData; + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + for(int i = size-1; i >= index; i-- ){ + elementData[i] = elementData[i-1]; + } + + elementData[index] = o; + + } + + public Object get(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + return elementData[index]; + } + + public Object remove(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Object old = elementData[index]; + for(int i = index; i < size-1 ; i++ ){ + elementData[i] = elementData[i+1]; + } + elementData[--size] = null; + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + //index for next element to visit + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if(cursor >= size){ + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + @Override + public void remove() { + //Check bound + if(cursor == 0){ + throw new NoSuchElementException(); + } + + ArrayList.this.remove(--cursor); + + } + + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..74d7ae6f16 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.congcongcong250.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..9033ad33be --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..840bb14f7c --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java @@ -0,0 +1,163 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head.next = head; + head.previous = head; + size = 0; + } + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Node nx = this.find(index); + Node pr = nx.previous; + Node in = new Node(o,pr,nx); + nx.previous = in; + pr.next = in; + size++; + } + + public Object get(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + return this.find(index); + } + + public Object remove(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + Node rem = this.find(index); + + Node pr = rem.previous; + Node nx = rem.next; + pr.next = nx; + nx.previous = pr; + + Object ret = rem.data; + rem.previous = null; + rem.next = null; + rem.data = null; + size--; + return ret; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node nx = head.next; + Node in = new Node(o,head, nx); + head.next = in; + nx.previous = in; + size++; + } + + public void addLast(Object o){ + Node last = head.previous; + Node in = new Node(o,last,head); + last.next = in; + head.previous = in; + + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new ListItr(); + } + + private Node find(int index){ + Node tra = head; + + //If index < size/2 + if( index < (size >> 1)){ + for(int i = 0; i <= index; i++){ + tra = tra.next; + } + }else{ + for(int i = size; i >= index; i--){ + tra = tra.previous; + } + } + return tra; + } + + private static class Node{ + Object data; + Node next; + Node previous; + + public Node(Object obj,Node pre, Node nx){ + data = obj; + next = nx; + previous = pre; + } + + + } + + private class ListItr implements Iterator{ + //Point to next node + Node cursor; + int nextIndex; + + public ListItr(){ + cursor = head.next; + nextIndex = 0; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + Node re = cursor; + cursor = cursor.next; + nextIndex++; + return re; + } + + public Object previous() { + Node re = cursor.previous.previous; + cursor = cursor.previous; + nextIndex--; + return re; + } + + @Override + public void remove() { + //Check bound + if(nextIndex > size){ + throw new NoSuchElementException("Iterates to the end"); + } + LinkedList.this.remove(--nextIndex); + + } + + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java new file mode 100644 index 0000000000..fe05e60f37 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.congcongcong250.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/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java new file mode 100644 index 0000000000..2cdd329b0b --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Queue { + private LinkedList elementData; + + public Queue(){ + elementData = new LinkedList(); + } + + public void enQueue(Object o){ + elementData.addFirst(o); + } + + public Object deQueue(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return (elementData.size() == 0); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java new file mode 100644 index 0000000000..e3024202da --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public Stack(){ + elementData = new LinkedList(); + } + + public void push(Object o){ + elementData.addLast(o); + } + + public Object pop(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } +} 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 13/37] =?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 e6ef9f1b191ee66c6cb54d277e84545f83616009 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 26 Feb 2017 09:40:52 +0800 Subject: [PATCH 14/37] Add my first code v1.1 --- .../HarryHook/coding2017/basic/BinaryTreeNode.java | 4 ++-- .../HarryHook/coding2017/basic/BinaryTreeNodeTest.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java index 09ad8c0fd5..2d962d9083 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -25,7 +25,7 @@ public void inOrder(BinaryTreeNode node) //获取给节点的值 public Integer getData() { - return this.data; + return data; } //给一个节点赋值 public void setData(Integer data) @@ -64,7 +64,7 @@ public BinaryTreeNode insert(Integer obj) // 上个节点 BinaryTreeNode parent = null; // 如果根节点为空 - if (this.data == null) + if (current.data == null) { newNode.setData(obj); newNode.setLeft(null); diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java index 7f706c24b5..60d1979713 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java @@ -10,29 +10,29 @@ public class BinaryTreeNodeTest { - BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + BinaryTreeNode binaryTreeNode; @Before public void setUpBinaryTreeNode() { - //binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode = new BinaryTreeNode(); } @Test public void testBinaryTreeNodeFunctional() { - binaryTreeNode.insert(4); + binaryTreeNode = binaryTreeNode.insert(4); binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); - + assertEquals(true, 4 == binaryTreeNode.getData()); assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); assertEquals(true, 5 == binaryTreeNode.getRight().getData()); assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); - + //节点为空 说明值没有插进去 binaryTreeNode.inOrder(binaryTreeNode); } From 027588236ce5b3c24f1e3ae09a825c4f7c158bba Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 26 Feb 2017 09:41:03 +0800 Subject: [PATCH 15/37] add ignore --- .gitignore | 19 +++++++++++++++++++ group02/727171008/.gitignore | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 group02/727171008/.gitignore diff --git a/.gitignore b/.gitignore index ec55baf87d..ecebe0642e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,22 @@ hs_err_pid* #ide config .metadata .recommenders +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file diff --git a/group02/727171008/.gitignore b/group02/727171008/.gitignore new file mode 100644 index 0000000000..806b57f381 --- /dev/null +++ b/group02/727171008/.gitignore @@ -0,0 +1,36 @@ +/bin/ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file From 6d3967a597cb2074b80f81753ba57988b3b40dbd Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 13:23:33 +1100 Subject: [PATCH 16/37] BinaryTree --- .../coding2017/basic/BinaryTreeNode.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java index 74d7ae6f16..fb71311dc4 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -1,11 +1,23 @@ package com.github.congcongcong250.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode >{ private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + public BinaryTreeNode(){ + data = null; + left = null; + right = null; + } + + public BinaryTreeNode(Object obj){ + data = obj; + left = null; + right = null; + } + public Object getData() { return data; } @@ -26,7 +38,35 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Object o){ - return null; + //If is empty root + if(data == null){ + data = o; + return this; + } + + //If it is a normal root + BinaryTreeNode in; + + if(o.compareTo(data) <= 0){ + if(left == null){ + in = new BinaryTreeNode(o); + left = in; + }else{ + in = left.insert(o); + } + }else{ + if(right == null){ + in = new BinaryTreeNode(o); + right = in; + }else{ + in = right.insert(o); + } + } + + assert (in == null):"Insert error"; + return in; } + + } From 5c18ae53f878c365beab5b5aeb6b9d85b4b91d3e Mon Sep 17 00:00:00 2001 From: Ven13 <106614649@qq.com> Date: Sun, 26 Feb 2017 11:37:19 +0800 Subject: [PATCH 17/37] add all java --- .../Ven13/coding2017/basic/ArrayList.java | 97 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++++ .../Ven13/coding2017/basic/Iterator.java | 7 + .../Ven13/coding2017/basic/LinkedList.java | 148 ++++++++++++++++++ .../github/Ven13/coding2017/basic/List.java | 9 ++ .../github/Ven13/coding2017/basic/Queue.java | 30 ++++ .../github/Ven13/coding2017/basic/Stack.java | 30 ++++ 7 files changed, 353 insertions(+) create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..b636dc92fe --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.github.Ven13.coding2017.basic; + +public class ArrayList implements List { + + //���ؼ��ϴ�С + private int size = 0; + + //�ȸ���һ������Ϊ10������ + Object[] elementData = new Object[100]; + + @Override + //��̬����Ԫ�� + public void add(Object o) { + //���ж������Ƿ����� + if(size == elementData.length) { + Object[] newObjects = new Object[elementData.length * 2]; + System.arraycopy(elementData, 0, newObjects, 0, elementData.length); + elementData = newObjects; + } + + //Ϊ�����ӵ�Ԫ��ָ���±� + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + //���ж������Ƿ����� + if(size == elementData.length) { + Object[] newObjects = elementData; + this.elementData = new Object[elementData.length * 2]; + for(int j = 0; j < newObjects.length; j++) { + this.elementData[j] = newObjects[j]; + } + } + + for(int i = size - 1; i >= index; i--) { + elementData[i+1] = elementData[i]; + } + + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index > size) { + return null; + }; + + int moveSize = size - index - 1; + if (moveSize > 0) { + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + } + elementData[--size] = null; + + for(int i = index; i < elementData.length; i++) { + elementData[i] = elementData[i+1]; + } + size--; + + return elementData; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int currentIndex = 0; + + @Override + public boolean hasNext() { + if(currentIndex >= size) return false; + else return true; + } + + @Override + public Object next() { + Object o = elementData[currentIndex]; + currentIndex ++; + return o; + } + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..354b2ba7a0 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.Ven13.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3cf6540e5e --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.Ven13.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..0b605c28ee --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java @@ -0,0 +1,148 @@ +package com.github.Ven13.coding2017.basic; + +public class LinkedList implements List { + + //��ʾ�������ij��� + private int size; + + //������ͷԪ�� + private Node head; + //������βԪ�� + private Node tail; + + //ʹ���ڲ�����ʵ��������ÿһ���ڵ㣬ÿ���ڵ���һ��ָ����һ��Ԫ�ص�next���Լ�������data + private static class Node { + public Object data; + public Node next; + + public Node(Object data) { + this.data = data; + } + } + + //�����Ĺ��췽�� + public LinkedList() { + } + + @Override + public void add(Object o) { + add(size, o); + } + + @Override + public void add(int index, Object o) { + if(index == 0) { + addFirst(o); + } else { + if(index >= size) { + addLast(o); + } else { + Node node = head; + for (int i = 1; i < index; i++) { + head = head.next; + } + Node nextNode = node.next; + Node temp = new Node(o); + node.next = temp; + temp.next = nextNode; + size++; + } + } + } + + //����ǰ�� + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + if(tail == null) { + tail = head; + } + } + + //���Ӻ��� + public void addLast(Object o) { + if(tail == null) { + tail = head = new Node(o); + } else { + Node newNode = new Node(o); + tail.next = newNode; + tail = tail.next; + } + size++; + } + + + @Override + public Object get(int index) { + Node node = head; + for(int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + @Override + public Object remove(int index) { + if(index == 0) { + head.next = head; + return head.data; + } else { + if(index >= size) { + throw new java.util.NoSuchElementException(); + } else { + Node node = head; + for(int i = 1; i < index; i++) { + node = node.next; + } + Node temp = node.next; + node.next = temp.next; + size--; + return node.data; + } + } + + } + + @Override + public int size() { + return size; + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + private Node node = head.next; + + @Override + public boolean hasNext() { + return node != tail; + } + + @Override + public Object next() { + + if(!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Object nextData = node.data; + node = node.next; + return nextData; + } + + } + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java new file mode 100644 index 0000000000..525eb92857 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.Ven13.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/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java new file mode 100644 index 0000000000..112299b5e9 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + size++; + list.addLast(o); + } + + public Object deQueue(){ + size--; + return list.removeFirst(); + } + + public boolean isEmpty(){ + if(size == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return size; + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java new file mode 100644 index 0000000000..c0a2658bb3 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object pop(){ + Object o = null; + if(elementData.size() > 0) { + o = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + } + return o; + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } + +} From cb4ca62ace6022e547fd295eda8abee74cfb54e9 Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Sun, 26 Feb 2017 11:38:34 +0800 Subject: [PATCH 18/37] =?UTF-8?q?Revert=20"=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwjcc969/coding2017/basic/ArrayList.java | 62 ---------- .../coding2017/basic/BinaryTreeNode.java | 32 ----- .../hwjcc969/coding2017/basic/Iterator.java | 7 -- .../hwjcc969/coding2017/basic/LinkedList.java | 117 ------------------ .../hwjcc969/coding2017/basic/List.java | 9 -- .../hwjcc969/coding2017/basic/Queue.java | 27 ---- .../hwjcc969/coding2017/basic/Stack.java | 31 ----- 7 files changed, 285 deletions(-) delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java deleted file mode 100644 index c2acf20e4d..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -import javax.lang.model.element.Element; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - if (o == null){ - throw new NullPointerException("空对象"); - } - if (size >= 100){ - throw new RuntimeException("越界"); - } - elementData[size] = o; - size++; - } - public void add(int index, Object o){ - if (o == null){ - throw new NullPointerException("空对象"); - } - if (index >= 99){ - throw new RuntimeException("越界"); - } - for (int i = size; i >= index; i--) { - elementData[i + 1] = elementData[i]; - } - elementData[index] = o; - size++; - } - - public Object get(int index){ - if (index >= 100 || index < 0){ - throw new RuntimeException("越界"); - } - return elementData[index]; - } - - public Object remove(int index){ - if (index >= 100 || index < 0){ - throw new RuntimeException("越界"); - } - Object o = elementData[index]; - for (int i = index; i < size - 1; i++) { - elementData[i] = elementData[i + 1]; - } - size--; - return null; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index 0381f88c69..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java deleted file mode 100644 index 0e8394844b..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java deleted file mode 100644 index d000967c19..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class LinkedList implements List { - - private Node head; - private int size; - - public void add(Object o){ - Node newNode = new Node(o); - if (head == null) { - head = newNode; - } - else{ - Node last = head.next; - while (last != null){ - last = last.next; - } - last = newNode; - } - size++; - - } - public void add(int index , Object o){ - Node dummy = new Node(); - dummy.next = head; - Node pre = dummy; - while (index > 0) { - pre = pre.next; - } - Node cur = new Node(o); - cur.next = pre.next; - pre.next = cur; - size++; - } - public Object get(int index){ - Node cur = head; - while (index > 0 && cur != null) { - cur = cur.next; - } - return cur; - } - public Object remove(int index){ - Node dummy = new Node(); - dummy.next = head; - Node pre = dummy; - while (index > 0) { - pre = pre.next; - } - Node cur = pre.next; - Node next = pre.next.next; - pre.next = next; - size--; - return cur; - - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node firstNode = new Node(o); - firstNode.next = head; - head = firstNode; - } - public void addLast(Object o){ - Node pre = new Node(); - while (pre.next != null){ - pre = pre.next; - } - Node lastNode = new Node(o); - pre.next = lastNode; - } - public Object removeFirst(){ - Node dummy = new Node(); - dummy.next = head; - dummy.next = head.next; - return dummy.next; - } - public Object removeLast(){ - Node pre = new Node(); - while (pre.next.next != null) { - pre = pre.next; - } - Node lastNode = pre.next.next; - pre.next = null; - return lastNode; - - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - public Node(Object o) { - // TODO Auto-generated constructor stub - } - public Node() { - // TODO Auto-generated constructor stub - } - Object data; - Node next; - - } - - - public boolean isEmpty() { - // TODO Auto-generated method stub - if (size() == 0){ - return false; - } - return true; - } - - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java deleted file mode 100644 index 56d67bdf29..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.hwjcc969.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/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java deleted file mode 100644 index f7cb7b1ad4..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class Queue { - private LinkedList list = new LinkedList(); - public void enQueue(Object o){ - list.addLast(o); - } - - public Object deQueue(){ - if (!list.isEmpty()){ - return list.removeFirst(); - } - return "空队列"; - - } - - public boolean isEmpty(){ - if (list.size() == 0){ - return false; - } - return true; - } - - public int size(){ - return list.size(); - } -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java deleted file mode 100644 index a4229a7d12..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size = 0; - - public void push(Object o){ - if (elementData.size() <= 99) - elementData.add(o); - size++; - - } - - public Object pop(){ - size--; - return elementData.remove(elementData.size() - 1); - - } - - public Object peek(){ - return elementData.get(elementData.size() - 1); - } - public boolean isEmpty(){ - if (size == 0) - return false; - return true; - } - public int size(){ - return size; - } -} From 87dcc87e565193446da285d0bf716c558a4cda4f Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Sun, 26 Feb 2017 13:09:03 +0800 Subject: [PATCH 19/37] Revert "Master" --- .gitignore | 19 -- group02/727171008/.gitignore | 36 --- .../coding2017/basic/ArrayListTest.java | 15 -- .../coding2017/basic/BinaryTreeNode.java | 121 ---------- .../coding2017/basic/BinaryTreeNodeTest.java | 40 ---- .../HarryHook/coding2017/basic/Iterator.java | 7 - .../coding2017/basic/LinkedListTest.java | 93 -------- .../HarryHook/coding2017/basic/List.java | 14 -- .../HarryHook/coding2017/basic/ListTest.java | 122 ---------- .../coding2017/basic/MyArrayList.java | 167 ------------- .../coding2017/basic/MyLinkedList.java | 219 ------------------ .../HarryHook/coding2017/basic/MyQueue.java | 54 ----- .../HarryHook/coding2017/basic/MyStack.java | 59 ----- .../HarryHook/coding2017/basic/QueueTest.java | 33 --- .../HarryHook/coding2017/basic/StackTest.java | 40 ---- .../src/com/coding/basic/BinaryTreeNode.java | 93 +------- liuxin/src/com/coding/basic/insert.java | 37 --- 17 files changed, 6 insertions(+), 1163 deletions(-) delete mode 100644 group02/727171008/.gitignore delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java delete mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java delete mode 100644 liuxin/src/com/coding/basic/insert.java diff --git a/.gitignore b/.gitignore index ecebe0642e..ec55baf87d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,22 +14,3 @@ hs_err_pid* #ide config .metadata .recommenders -/bin/ -.idea/workspace.xml -.idea/dictionaries/myj.xml -.idea/ -.DS_Store -*.classpath -*.project -.settings -.project -.target -.classpath -**/.settings -**/.classpath -**/.eclipse -**/target/ -target/ -bin/ -.svn -*.iml \ No newline at end of file diff --git a/group02/727171008/.gitignore b/group02/727171008/.gitignore deleted file mode 100644 index 806b57f381..0000000000 --- a/group02/727171008/.gitignore +++ /dev/null @@ -1,36 +0,0 @@ -/bin/ -*.class - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -#ide config -.metadata -.recommenders -/bin/ -.idea/workspace.xml -.idea/dictionaries/myj.xml -.idea/ -.DS_Store -*.classpath -*.project -.settings -.project -.target -.classpath -**/.settings -**/.classpath -**/.eclipse -**/target/ -target/ -bin/ -.svn -*.iml \ No newline at end of file diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java deleted file mode 100644 index 0f22f6ba80..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import org.junit.Before; -import com.github.HarryHook.coding2017.basic.MyArrayList; - - -public class ArrayListTest extends ListTest { - - @Before - public void setUpArrayList() - { - aList = new MyArrayList(); - } - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index 2d962d9083..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Created by Harry 2017-2-23 10:50:39 - * 实现二叉树,并按二叉查找树插入节点 - * 能实现基本的功能,但测试时存在问题,传进去的数据为空 2017-2-2523:49:43 - */ -package com.github.HarryHook.coding2017.basic; - -public class BinaryTreeNode -{ - private Integer data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - //中序遍历二叉树 - public void inOrder(BinaryTreeNode node) - { - if(node != null) - { - inOrder(node.left); - System.out.print(" " + node.data); - inOrder(node.right); - } - } - - //获取给节点的值 - public Integer getData() - { - return data; - } - //给一个节点赋值 - public void setData(Integer data) - { - this.data = data; - } - //获取左节点 - public BinaryTreeNode getLeft() - { - return left; - } - //指定左节点 - public void setLeft(BinaryTreeNode left) - { - this.left = left; - } - - //获取右节点 - public BinaryTreeNode getRight() - { - return right; - } - //指定右节点 - public void setRight(BinaryTreeNode right) - { - this.right = right; - } - - //在二叉树中插入一个节点,需要判断 - public BinaryTreeNode insert(Integer obj) - { - // 新增节点 - BinaryTreeNode newNode = new BinaryTreeNode(); - // 当前节点,保留根的值 - BinaryTreeNode current = this; - // 上个节点 - BinaryTreeNode parent = null; - // 如果根节点为空 - if (current.data == null) - { - newNode.setData(obj); - newNode.setLeft(null); - newNode.setRight(null); - return newNode; - }else - { - while (true) - { - parent = current; - if (obj < current.data) - { - current = current.left; - if (current == null) - { - newNode.setData(obj); - newNode.setLeft(null); - newNode.setRight(null); - parent.left = newNode; - return newNode; - } - } else - { - current = current.right; - if (current == null) - { - newNode.setData(obj); - newNode.setLeft(null); - newNode.setRight(null); - parent.right = newNode; - return newNode; - } - } - } - } - } - - public static void main(String[] args) - { - BinaryTreeNode BTN = new BinaryTreeNode(); - - BTN = BTN.insert(5); - System.out.print(BTN.getData() + " "); - System.out.print(BTN.insert(2).getData() + " "); - System.out.print(BTN.insert(1).getData() + " "); - System.out.print(BTN.insert(4).getData() + " "); - System.out.print(BTN.insert(6).getData() + " "); - System.out.print(BTN.insert(8).getData() + " "); - System.out.println(""); - System.out.println("中序遍历二叉树: "); - BTN.inOrder(BTN); - } - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java deleted file mode 100644 index 60d1979713..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; - -import com.github.HarryHook.coding2017.basic.BinaryTreeNode; - -public class BinaryTreeNodeTest -{ - - BinaryTreeNode binaryTreeNode; - - @Before - public void setUpBinaryTreeNode() - { - binaryTreeNode = new BinaryTreeNode(); - } - - @Test - public void testBinaryTreeNodeFunctional() - { - binaryTreeNode = binaryTreeNode.insert(4); - binaryTreeNode.insert(1); - binaryTreeNode.insert(3); - binaryTreeNode.insert(5); - binaryTreeNode.insert(2); - - assertEquals(true, 4 == binaryTreeNode.getData()); - assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); - assertEquals(true, 5 == binaryTreeNode.getRight().getData()); - assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); - assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); - - //节点为空 说明值没有插进去 - binaryTreeNode.inOrder(binaryTreeNode); - } - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java deleted file mode 100644 index 3fae84a22f..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -public interface Iterator -{ - public boolean hasNext(); - public Object next(); -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java deleted file mode 100644 index 7c754e37af..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; -import com.github.HarryHook.coding2017.basic.MyLinkedList; - -public class LinkedListTest extends ListTest{ - - private MyLinkedList aLinkedList; - - @Before - public void setUpLinkedList() { - aList = new MyLinkedList(); - aLinkedList = new MyLinkedList(); - } - - @Test - public void testAddFirst() { - aLinkedList.addFirst(5); - assertEquals(5, aLinkedList.get(0)); - - aLinkedList.addFirst(6); - assertEquals(6, aLinkedList.get(0)); - assertEquals(5, aLinkedList.get(1)); - assertEquals(2, aLinkedList.size()); - } - - @Test - public void testAddLast() { - aLinkedList.addLast("hello"); - assertEquals("hello", aLinkedList.get(0)); - - aLinkedList.addLast("world"); - assertEquals("hello", aLinkedList.get(0)); - assertEquals("world", aLinkedList.get(1)); - assertEquals(2, aLinkedList.size()); - } - - @Test - public void testRemoveFirst() { - aLinkedList.addLast("hello"); - aLinkedList.addLast("world"); - - aLinkedList.removeFirst(); - assertEquals("world", aLinkedList.get(0)); - assertEquals(1, aLinkedList.size()); - - aLinkedList.removeFirst(); - assertEquals(0, aLinkedList.size()); - } - - @Test - public void testRemoveLast() { - aLinkedList.addFirst("world"); - aLinkedList.addFirst("hello"); - - aLinkedList.removeLast(); - assertEquals("hello", aLinkedList.get(0)); - assertEquals(1, aLinkedList.size()); - - aLinkedList.removeLast(); - assertEquals(0, aLinkedList.size()); - } - - @Test - public void testLinkedListFunctional() { - for (int i=1; i<4; i++) { - aLinkedList.add(i); // [1,2,3] - } - aLinkedList.remove(1); // [1,3] - - aLinkedList.add(1, 0); // [1,0,3] - for (int i=4; i<6; i++) { - aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] - } - assertEquals(5, aLinkedList.size()); - assertEquals(5, aLinkedList.get(0)); - assertEquals(1, aLinkedList.get(2)); - assertEquals(0, aLinkedList.get(3)); - - aLinkedList.remove(3); // [5, 4, 1, 3] - assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); - aLinkedList.removeLast(); // [5, 4, 1] - assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); - aLinkedList.removeFirst(); // [4,1] - - assertEquals(4, aLinkedList.get(0)); - assertEquals(1, aLinkedList.get(1)); - assertEquals(2, aLinkedList.size()); - } - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java deleted file mode 100644 index f2299e8e83..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.HarryHook.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(); - - public Iterator iterator(); -} - diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java deleted file mode 100644 index 92f84b687c..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import static org.junit.Assert.*; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import com.github.HarryHook.coding2017.basic.Iterator; -import com.github.HarryHook.coding2017.basic.List; - -public class ListTest { - - protected static List aList; - - @Test - public void testFunctional() { - aList.add(1); - aList.add(2); - assertEquals(1, aList.get(0)); - assertEquals(2, aList.get(1)); - - aList.add(3); - aList.add(0, 5); - aList.add(2, 11); - assertEquals(5, aList.get(0)); - assertEquals(11, aList.get(2)); - - aList.add("hi"); - assertEquals("hi", aList.get(5)); - assertEquals(6, aList.size()); - - aList.remove(1); - assertEquals(11, aList.get(1)); - assertEquals(2, aList.get(2)); - - assertEquals(5, aList.size()); - } - - @Test - public void testAdd() { - for (int i=0; i<100; i++) - aList.add(i); - assertEquals(0, aList.get(0)); - assertEquals(99, aList.get(99)); - assertEquals(44, aList.get(44)); - } - - @Test - public void testRemove() { - aList.add(1); - aList.add(2); - aList.add(3); - int u = (Integer)aList.remove(2); - assertEquals(3, u); - assertEquals(2, aList.size()); - - aList.add(1, 5); - u = (Integer)aList.remove(0); - assertEquals(1, u); - assertEquals(5, aList.get(0)); - assertEquals(2, aList.get(1)); - assertEquals(2, aList.size()); - - aList.remove(0); - aList.remove(0); - assertEquals(0, aList.size()); - - - } - - @Test - public void testSize() { - for (int i=0; i<10; i++) - aList.add(i*2); - assertEquals(10, aList.size()); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void testException() { - expectedEx.expect(Exception.class); - aList.remove(1); - aList.add(3); - aList.add(2, 5); - expectedEx.expect(Exception.class); - } - - @Test - - public void testIterator() - { - Iterator it = aList.iterator(); - assertEquals(false, it.hasNext()); - - aList.add(1); - aList.add(2); - aList.add(3); - - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(2, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - aList.remove(1); - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - expectedEx.expect(Exception.class); - it.next(); - - } - - - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java deleted file mode 100644 index dbfd8aae19..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * created by Harry 2017-2-20 18:53:38 - * 实现简单的ArrayList,具有基本的增删改查功能 - */ -package com.github.HarryHook.coding2017.basic; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -public class MyArrayList implements List -{ - private int size = 0; //数组元素个数 - - private Object[] elementData = new Object[10]; //初始化数组大小为10 - - //将元素添加到数组尾部 - public void add(Object o) - { //需要判断数组空间是否够用 - ensureCapacity(size + 1); - elementData[size++] = o; - } - //在指定位置添加元素 - public void add(int index, Object o) - { - //判断下标记是否越界 - if (index > size || index < 0) - throw new IndexOutOfBoundsException( - "Index: " + index + ", Size: " + size); - ensureCapacity(size + 1); - //判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 - if(elementData[index] == null) - { - elementData[index] = o; - } - else - { - for(int i=elementData.length-1; i>index; i--) - { - elementData[i] = elementData[i-1]; - } - elementData[index] = o; - } - size++; - - /* - //判断索引位置是否正确 - if (index > size || index < 0) - throw new IndexOutOfBoundsException( - "Index: " + index + ", Size: " + size); - //扩容检测 - ensureCapacity(size+1); - /* - * 对源数组进行复制处理(位移),从index + 1到size-index。 - * 主要目的就是空出index位置供数据插入, - * 即向右移动当前位于该位置的元素以及所有后续元素。 - - System.arraycopy(elementData, index, elementData, index + 1, - size - index); - //在指定位置赋值 - elementData[index] = 0; - size++; - - */ - } - - public Object get(int index) - { - //若index超出size应该抛出异常 - if(index >= size) - throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); - return elementData[index]; - - } - - public Object remove(int index) - { //涉及到元素移位 - Object oldValue = elementData[index]; - for(int i=index; i oldCapacity) - { - //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 - int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 - if (newCapacity < minCapacity) - newCapacity = minCapacity; - // minCapacity is usually close to size, so this is a win: - elementData = Arrays.copyOf(elementData, newCapacity); - } - } - - //返回数组的大小 - public int size() - { - return size; - } - - public Iterator iterator() - { - return new MyArrayListIterator(); - } - - private class MyArrayListIterator implements Iterator - { - private int cursor = 0; //记录索引位置 - public boolean hasNext() - { - return cursor != size; - } - public Object next() - { - try { - Object next = get(cursor); - cursor++; - return next; - - } catch (IndexOutOfBoundsException e) - { - throw new NoSuchElementException(); - } - - } - } - - public static void main(String[] args) - { - MyArrayList myArrays = new MyArrayList(); - myArrays.add(3); - myArrays.add(0, 11); - myArrays.add(1, 2); - myArrays.add(3, 5); - myArrays.add(2, 1); - myArrays.add(7); - Print(myArrays); - - for(int i = 0; i < 19; i++) - myArrays.add(i, 55); - - System.out.println("获取指定位置元素: " + myArrays.get(2)); - System.out.println("删除指定位置元素: " + myArrays.remove(1)); - System.out.println("当前元素个数:" + myArrays.size()); - - Print(myArrays); - - } - public static void Print(MyArrayList myArrays) - { - Iterator it = myArrays.iterator(); - System.out.println("对链表中的元素进行打印:"); - while(it.hasNext()) - System.out.print(it.next() + " "); - System.out.println(""); - System.out.println("当前元素个数: " + myArrays.size()); - - } - -} - diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java deleted file mode 100644 index 5aeab57496..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java +++ /dev/null @@ -1,219 +0,0 @@ -/* - * created by Harry 2017-2-21 14:43:41 - * 实现简单的LinkedList - */ - -package com.github.HarryHook.coding2017.basic; - -public class MyLinkedList implements List -{ - private Node head = null; //头指针 - private int size = 0; - private static class Node - { - Object data; - Node next; - } - public void add(Object o) - { - addLast(o); - } - //在指定位置添加元素 - public void add(int index , Object o) - { - - if (index > size || index < 0) - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - //存在插入头结点的情况 - if(index == 0) - addFirst(o); - else - { //即 index != 0 的情况 - // p保存待插入节点的前一节点,x指向要插入的节点 - Node x = head; - Node p = null; - int i = 0; - while(i < index) - { - p = x; - x = x.next; - i++; - } - Node n = new Node(); - p.next = n; - n.next = x; - n.data = o; - size++; - } - - } - //返回指定位置元素 - public Object get(int index) - { - Node x = head; - int i = 0; - while(i < index && x != null) - { - x = x.next; - i++; - } - return x.data; - } - - //移除指定位置节点 - public Object remove(int index) - { - if (index > size || index < 0) - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - //先判断是否是头节点 - if( index == 0) - { - return removeFirst(); - } - else - { - Node x = head; - Node pre = null; - int i = 0; - while(i < index) - { - pre = x; - x = x.next; - i++; - } - Object Data = pre.next.data; - pre.next = x.next; - x = null; - size--; - return Data; - } - - } - //头部添加节点 - public void addFirst(Object o) - { - Node n = new Node(); - n.next = head; - head = n; - n.data = o; - size++; - } - //尾部添加节点 - public void addLast(Object o) - { - if (head == null) - { - head = new Node(); - head.data = o; - } - else - { - Node x = head; - while(x.next != null) - { - x = x.next; - } - Node n = new Node(); - x.next = n; - n.next = null; - n.data = o; - } - size++; - } - //移除第一个节点 - public Object removeFirst() - { - Node n = head; - Object Data = n.data; - head = head.next; - n = null; - size--; - return Data; - } - - //移除最后一个节点 - //removeLast()方法存在bug - public Object removeLast() - { - Node x = head; - Node p = null; - if(x.next == null) - { - return removeFirst(); - } - else - { - while(x.next != null) - { - p = x; - x = x.next; - } - Object Data = x.data; - p.next = null; - x = null; //删除最后一个节点 - size--; - return Data; - } - } - public int size(){ - return size; - } - public Iterator iterator() - { - return new MyLinkedListIterator(); - } - private class MyLinkedListIterator implements Iterator - { - private int cursor = 0; //记录索引位置 - public boolean hasNext() - { - return cursor != size; - } - public Object next() - { - Object next = get(cursor); - cursor ++; - return next; - } - } - public static void main(String[] args) - { - MyLinkedList myList = new MyLinkedList(); - myList.add(3); - myList.add(5); - myList.add(0, 4); - myList.add(2, 7); - myList.addFirst(1); - myList.addLast(6); - - Print(myList); - - System.out.println("当前指定位置元素: " + myList.get(1)); - System.out.println("移除指定位置元素: " + myList.remove(4)); - Print(myList); - - System.out.println("移除第一个节点元素: " + myList.removeLast()); - Print(myList); - System.out.println("移除最后一个节点元素: " + myList.removeLast()); - Print(myList); - System.out.println("移除最后一个节点元素: " + myList.removeLast()); - Print(myList); - System.out.println("移除最后一个节点元素: " + myList.removeLast()); - Print(myList); - System.out.println("移除最后一个节点元素: " + myList.removeLast()); - Print(myList); - - } - public static void Print(MyLinkedList myList) - { - Iterator it = myList.iterator(); - System.out.println("对链表中的元素进行打印:"); - while(it.hasNext()) - System.out.print(it.next() + " "); - System.out.println(""); - System.out.println("当前元素个数: " + myList.size()); - System.out.println(""); - } - - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java deleted file mode 100644 index 9b713eaea9..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * created by Harry 2017-2-22 13:06:43 - * 实现简单的队列 - */ -package com.github.HarryHook.coding2017.basic; -import java.util.*; -public class MyQueue -{ - private MyArrayList elementData = new MyArrayList(); - private int size = 0; - //入队 - public void enQueue(Object o) - { - elementData.add(o); - size++; - } - //出队 - public Object deQueue() - { - if(isEmpty()) - throw new NoSuchElementException(); - Object Data = elementData.remove(0); - size--; - return Data; - } - //判断队列是否为空 - public boolean isEmpty() - { - return size() == 0; - } - //队列中元素个数 - public int size() - { - return size; - } - public static void main(String[] args) - { - MyQueue mq = new MyQueue(); - mq.enQueue(1); - mq.enQueue(2); - mq.enQueue(3); - mq.enQueue(4); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - //System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - //System.out.println("队列中元素个数: " + mq.size()); - } -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java deleted file mode 100644 index c7f87c04e6..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * created by Harry 2017-2-22 10:48:34 - * 实现简单的Stack - */ -package com.github.HarryHook.coding2017.basic; - -import java.util.*; - -public class MyStack -{ - private MyArrayList elementData = new MyArrayList(); - private int size = 0; - - //入栈操作 - public void push(Object o) - { - elementData.add(o); - size++; - } - //出栈操作 - public Object pop() - { - Object obj = peek(); - elementData.remove(size() - 1); - size--; - return obj; - } - //获取当前栈顶元素,不用出栈 - public Object peek() - { - if(isEmpty()) - throw new EmptyStackException(); - return elementData.get(size() - 1); - } - //判断栈是否为空 - public boolean isEmpty() - { - return size() == 0; - } - //返回栈内元素个数 - public int size(){ - return size; - } - - public static void main(String[] args) - { - MyStack ms = new MyStack(); - - ms.push(1); - ms.push(2); - ms.push(13); - System.out.println("当前栈顶元素是: " + ms.peek()); - System.out.println("出栈元素是: " + ms.pop()); - System.out.println("出栈元素是: " + ms.pop()); - ms.push(12); - System.out.println("出栈元素是: " + ms.pop()); - System.out.println("当前栈顶元素是: " + ms.peek()); - } -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java deleted file mode 100644 index 340f79d240..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; -import com.github.HarryHook.coding2017.basic.MyQueue; - -public class QueueTest { - private MyQueue queue; - - @Before - public void setUpQueue() { - queue = new MyQueue(); - } - - @Test - public void testQueueFunctional() { - assertEquals(true, queue.isEmpty()); - queue.enQueue(4); - queue.enQueue(2); - assertEquals(2, queue.size()); - assertEquals(false, queue.isEmpty()); - - int i = (Integer)queue.deQueue(); - assertEquals(4, i); - i = (Integer)queue.deQueue(); - assertEquals(2, i); - - assertEquals(0, queue.size()); - assertEquals(true, queue.isEmpty()); - } - -} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java deleted file mode 100644 index 26160faef6..0000000000 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.HarryHook.coding2017.basic; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -import com.github.HarryHook.coding2017.basic.MyStack; - -public class StackTest { - - private MyStack stack; - - @Before - public void setUpStack() { - stack = new MyStack(); - } - - @Test - public void testStackFunctional() { - assertEquals(true, stack.isEmpty()); - stack.push(4); - stack.push(2); - assertEquals(2, stack.size()); - assertEquals(false, stack.isEmpty()); - - int i = (Integer)stack.pop(); - assertEquals(2, i); - - i = (Integer)stack.peek(); - assertEquals(4, i); - - i = (Integer)stack.pop(); - assertEquals(4, i); - - assertEquals(0, stack.size()); - assertEquals(true, stack.isEmpty()); - } - -} diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index 736ae328c7..d7ac820192 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,51 +1,11 @@ package com.coding.basic; -public class BinaryTreeNode -{ - public static TreeNode root; //根节点 - public BinaryTreeNode() - { - this.root = null; - } - public TreeNode insert (int key) - { - // 新增节点 - TreeNode newNode = new TreeNode(key); - // 当前节点 - TreeNode current = root; - // 上个节点 - TreeNode parent = null; - // 如果根节点为空 - if (current == null) - { - root = newNode; - return newNode; - } - while (true) - { - parent = current; - if (key < current.value) - { - current = current.left; - if (current == null) - { - parent.left = newNode; - return newNode; - } - } else - { - current = current.right; - if (current == null) - { - parent.right = newNode; - return newNode; - } - } - } - } - -} - +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + public Object getData() { return data; } @@ -70,44 +30,3 @@ public BinaryTreeNode insert(Object o){ } } - -class TreeNode //树的节点 -{ - int value; - TreeNode left; - TreeNode right; - - public TreeNode(int value) - { - this.value = value; - left = null; - right = null; - } -} - -public class BinaryTreeNodeTest { - - public static void main(String[] args) { - BinaryTreeNode b = new BinaryTreeNode(); - b.insert(3);b.insert(8);b.insert(1);b.insert(4);b.insert(6); - b.insert(2);b.insert(10);b.insert(9);b.insert(20);b.insert(25); - - // 打印二叉树 - b.toString(b.root); - System.out.println(); - - // 是否存在节点值10 - TreeNode node01 = b.search(10); - System.out.println("是否存在节点值为10 => " + node01.value); - // 是否存在节点值11 - TreeNode node02 = b.search(11); - System.out.println("是否存在节点值为11 => " + node02); - - // 删除节点8 - TreeNode node03 = b.delete(8); - System.out.println("删除节点8 => " + node03.value); - b.toString(b.root); - - - } -} diff --git a/liuxin/src/com/coding/basic/insert.java b/liuxin/src/com/coding/basic/insert.java deleted file mode 100644 index 9ded2fcfac..0000000000 --- a/liuxin/src/com/coding/basic/insert.java +++ /dev/null @@ -1,37 +0,0 @@ - - //2017年2月24日17:02:55 - public BinaryTreeNode insert (Integer key) - { - // 新增节点 - BinaryTreeNode newNode = new BinaryTreeNode(key); - // 当前节点 - BinaryTreeNode current = root; - // 上个节点 - BinaryTreeNode parent = null; - // 如果根节点为空 - if (current == null) { - root = newNode; - return newNode; - } - while (true) - { - parent = current; - if (key < current.value) - { - current = current.left; - if (current == null) - { - parent.left = newNode; - return newNode; - } - } else - { - current = current.right; - if (current == null) - { - parent.right = newNode; - return newNode; - } - } - } - } 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 20/37] =?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 21/37] =?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 22/37] =?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()); + } + +} From 6a0535de15d91aa4df859dd8a7501abe711fdb74 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 26 Feb 2017 15:16:10 +0800 Subject: [PATCH 23/37] Submit my first code --- group02/727171008/.classpath | 7 + group02/727171008/.gitignore | 19 ++ group02/727171008/.project | 17 ++ .../coding2017/basic/ArrayListTest.java | 15 ++ .../coding2017/basic/BinaryTreeNode.java | 121 ++++++++++ .../coding2017/basic/BinaryTreeNodeTest.java | 40 ++++ .../HarryHook/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedListTest.java | 93 ++++++++ .../HarryHook/coding2017/basic/List.java | 14 ++ .../HarryHook/coding2017/basic/ListTest.java | 122 ++++++++++ .../coding2017/basic/MyArrayList.java | 167 +++++++++++++ .../coding2017/basic/MyLinkedList.java | 219 ++++++++++++++++++ .../HarryHook/coding2017/basic/MyQueue.java | 54 +++++ .../HarryHook/coding2017/basic/MyStack.java | 59 +++++ .../HarryHook/coding2017/basic/QueueTest.java | 33 +++ .../HarryHook/coding2017/basic/StackTest.java | 40 ++++ 16 files changed, 1027 insertions(+) create mode 100644 group02/727171008/.classpath create mode 100644 group02/727171008/.gitignore create mode 100644 group02/727171008/.project create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java diff --git a/group02/727171008/.classpath b/group02/727171008/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group02/727171008/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group02/727171008/.gitignore b/group02/727171008/.gitignore new file mode 100644 index 0000000000..0cfebaa908 --- /dev/null +++ b/group02/727171008/.gitignore @@ -0,0 +1,19 @@ +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file diff --git a/group02/727171008/.project b/group02/727171008/.project new file mode 100644 index 0000000000..0d10c0ccb2 --- /dev/null +++ b/group02/727171008/.project @@ -0,0 +1,17 @@ + + + 727171008Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..0f22f6ba80 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java @@ -0,0 +1,15 @@ +package com.github.HarryHook.coding2017.basic; + +import org.junit.Before; +import com.github.HarryHook.coding2017.basic.MyArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() + { + aList = new MyArrayList(); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b93790894f --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,121 @@ +/* + * Created by Harry 2017-2-23 10:50:39 + * 实现二叉树,并按二叉查找树插入节点 + * + */ +package com.github.HarryHook.coding2017.basic; + +public class BinaryTreeNode +{ + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + //中序遍历二叉树 + public void inOrder(BinaryTreeNode node) + { + if(node != null) + { + inOrder(node.left); + System.out.print(" " + node.data); + inOrder(node.right); + } + } + + //获取给节点的值 + public Integer getData() + { + return data; + } + //给一个节点赋值 + public void setData(Integer data) + { + this.data = data; + } + //获取左节点 + public BinaryTreeNode getLeft() + { + return left; + } + //指定左节点 + public void setLeft(BinaryTreeNode left) + { + this.left = left; + } + + //获取右节点 + public BinaryTreeNode getRight() + { + return right; + } + //指定右节点 + public void setRight(BinaryTreeNode right) + { + this.right = right; + } + + //在二叉树中插入一个节点,需要判断 + public BinaryTreeNode insert(Integer obj) + { + // 新增节点 + BinaryTreeNode newNode = new BinaryTreeNode(); + // 当前节点,保留根的值 + BinaryTreeNode current = this; + // 上个节点 + BinaryTreeNode parent = null; + // 如果根节点为空 + if (current.data == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + return newNode; + }else + { + while (true) + { + parent = current; + if (obj < current.data) + { + current = current.left; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.right = newNode; + return newNode; + } + } + } + } + } + + public static void main(String[] args) + { + BinaryTreeNode BTN = new BinaryTreeNode(); + + BTN = BTN.insert(5); + System.out.print(BTN.getData() + " "); + System.out.print(BTN.insert(2).getData() + " "); + System.out.print(BTN.insert(1).getData() + " "); + System.out.print(BTN.insert(4).getData() + " "); + System.out.print(BTN.insert(6).getData() + " "); + System.out.print(BTN.insert(8).getData() + " "); + System.out.println(""); + System.out.println("中序遍历二叉树: "); + BTN.inOrder(BTN); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..60d1979713 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest +{ + + BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() + { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() + { + binaryTreeNode = binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + + //节点为空 说明值没有插进去 + binaryTreeNode.inOrder(binaryTreeNode); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3fae84a22f --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.HarryHook.coding2017.basic; + +public interface Iterator +{ + public boolean hasNext(); + public Object next(); +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..7c754e37af --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.HarryHook.coding2017.basic.MyLinkedList; + +public class LinkedListTest extends ListTest{ + + private MyLinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new MyLinkedList(); + aLinkedList = new MyLinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java new file mode 100644 index 0000000000..f2299e8e83 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java @@ -0,0 +1,14 @@ +package com.github.HarryHook.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(); + + public Iterator iterator(); +} + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java new file mode 100644 index 0000000000..92f84b687c --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java @@ -0,0 +1,122 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.HarryHook.coding2017.basic.Iterator; +import com.github.HarryHook.coding2017.basic.List; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + + public void testIterator() + { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + + } + + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..dbfd8aae19 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java @@ -0,0 +1,167 @@ +/* + * created by Harry 2017-2-20 18:53:38 + * 实现简单的ArrayList,具有基本的增删改查功能 + */ +package com.github.HarryHook.coding2017.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class MyArrayList implements List +{ + private int size = 0; //数组元素个数 + + private Object[] elementData = new Object[10]; //初始化数组大小为10 + + //将元素添加到数组尾部 + public void add(Object o) + { //需要判断数组空间是否够用 + ensureCapacity(size + 1); + elementData[size++] = o; + } + //在指定位置添加元素 + public void add(int index, Object o) + { + //判断下标记是否越界 + if (index > size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + ensureCapacity(size + 1); + //判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 + if(elementData[index] == null) + { + elementData[index] = o; + } + else + { + for(int i=elementData.length-1; i>index; i--) + { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size++; + + /* + //判断索引位置是否正确 + if (index > size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + //扩容检测 + ensureCapacity(size+1); + /* + * 对源数组进行复制处理(位移),从index + 1到size-index。 + * 主要目的就是空出index位置供数据插入, + * 即向右移动当前位于该位置的元素以及所有后续元素。 + + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + //在指定位置赋值 + elementData[index] = 0; + size++; + + */ + } + + public Object get(int index) + { + //若index超出size应该抛出异常 + if(index >= size) + throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); + return elementData[index]; + + } + + public Object remove(int index) + { //涉及到元素移位 + Object oldValue = elementData[index]; + for(int i=index; i oldCapacity) + { + //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + //返回数组的大小 + public int size() + { + return size; + } + + public Iterator iterator() + { + return new MyArrayListIterator(); + } + + private class MyArrayListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + try { + Object next = get(cursor); + cursor++; + return next; + + } catch (IndexOutOfBoundsException e) + { + throw new NoSuchElementException(); + } + + } + } + + public static void main(String[] args) + { + MyArrayList myArrays = new MyArrayList(); + myArrays.add(3); + myArrays.add(0, 11); + myArrays.add(1, 2); + myArrays.add(3, 5); + myArrays.add(2, 1); + myArrays.add(7); + Print(myArrays); + + for(int i = 0; i < 19; i++) + myArrays.add(i, 55); + + System.out.println("获取指定位置元素: " + myArrays.get(2)); + System.out.println("删除指定位置元素: " + myArrays.remove(1)); + System.out.println("当前元素个数:" + myArrays.size()); + + Print(myArrays); + + } + public static void Print(MyArrayList myArrays) + { + Iterator it = myArrays.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myArrays.size()); + + } + +} + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..5aeab57496 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java @@ -0,0 +1,219 @@ +/* + * created by Harry 2017-2-21 14:43:41 + * 实现简单的LinkedList + */ + +package com.github.HarryHook.coding2017.basic; + +public class MyLinkedList implements List +{ + private Node head = null; //头指针 + private int size = 0; + private static class Node + { + Object data; + Node next; + } + public void add(Object o) + { + addLast(o); + } + //在指定位置添加元素 + public void add(int index , Object o) + { + + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //存在插入头结点的情况 + if(index == 0) + addFirst(o); + else + { //即 index != 0 的情况 + // p保存待插入节点的前一节点,x指向要插入的节点 + Node x = head; + Node p = null; + int i = 0; + while(i < index) + { + p = x; + x = x.next; + i++; + } + Node n = new Node(); + p.next = n; + n.next = x; + n.data = o; + size++; + } + + } + //返回指定位置元素 + public Object get(int index) + { + Node x = head; + int i = 0; + while(i < index && x != null) + { + x = x.next; + i++; + } + return x.data; + } + + //移除指定位置节点 + public Object remove(int index) + { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //先判断是否是头节点 + if( index == 0) + { + return removeFirst(); + } + else + { + Node x = head; + Node pre = null; + int i = 0; + while(i < index) + { + pre = x; + x = x.next; + i++; + } + Object Data = pre.next.data; + pre.next = x.next; + x = null; + size--; + return Data; + } + + } + //头部添加节点 + public void addFirst(Object o) + { + Node n = new Node(); + n.next = head; + head = n; + n.data = o; + size++; + } + //尾部添加节点 + public void addLast(Object o) + { + if (head == null) + { + head = new Node(); + head.data = o; + } + else + { + Node x = head; + while(x.next != null) + { + x = x.next; + } + Node n = new Node(); + x.next = n; + n.next = null; + n.data = o; + } + size++; + } + //移除第一个节点 + public Object removeFirst() + { + Node n = head; + Object Data = n.data; + head = head.next; + n = null; + size--; + return Data; + } + + //移除最后一个节点 + //removeLast()方法存在bug + public Object removeLast() + { + Node x = head; + Node p = null; + if(x.next == null) + { + return removeFirst(); + } + else + { + while(x.next != null) + { + p = x; + x = x.next; + } + Object Data = x.data; + p.next = null; + x = null; //删除最后一个节点 + size--; + return Data; + } + } + public int size(){ + return size; + } + public Iterator iterator() + { + return new MyLinkedListIterator(); + } + private class MyLinkedListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + Object next = get(cursor); + cursor ++; + return next; + } + } + public static void main(String[] args) + { + MyLinkedList myList = new MyLinkedList(); + myList.add(3); + myList.add(5); + myList.add(0, 4); + myList.add(2, 7); + myList.addFirst(1); + myList.addLast(6); + + Print(myList); + + System.out.println("当前指定位置元素: " + myList.get(1)); + System.out.println("移除指定位置元素: " + myList.remove(4)); + Print(myList); + + System.out.println("移除第一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + + } + public static void Print(MyLinkedList myList) + { + Iterator it = myList.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myList.size()); + System.out.println(""); + } + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..9b713eaea9 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java @@ -0,0 +1,54 @@ +/* + * created by Harry 2017-2-22 13:06:43 + * 实现简单的队列 + */ +package com.github.HarryHook.coding2017.basic; +import java.util.*; +public class MyQueue +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + //入队 + public void enQueue(Object o) + { + elementData.add(o); + size++; + } + //出队 + public Object deQueue() + { + if(isEmpty()) + throw new NoSuchElementException(); + Object Data = elementData.remove(0); + size--; + return Data; + } + //判断队列是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //队列中元素个数 + public int size() + { + return size; + } + public static void main(String[] args) + { + MyQueue mq = new MyQueue(); + mq.enQueue(1); + mq.enQueue(2); + mq.enQueue(3); + mq.enQueue(4); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + //System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + //System.out.println("队列中元素个数: " + mq.size()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..c7f87c04e6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java @@ -0,0 +1,59 @@ +/* + * created by Harry 2017-2-22 10:48:34 + * 实现简单的Stack + */ +package com.github.HarryHook.coding2017.basic; + +import java.util.*; + +public class MyStack +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + + //入栈操作 + public void push(Object o) + { + elementData.add(o); + size++; + } + //出栈操作 + public Object pop() + { + Object obj = peek(); + elementData.remove(size() - 1); + size--; + return obj; + } + //获取当前栈顶元素,不用出栈 + public Object peek() + { + if(isEmpty()) + throw new EmptyStackException(); + return elementData.get(size() - 1); + } + //判断栈是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //返回栈内元素个数 + public int size(){ + return size; + } + + public static void main(String[] args) + { + MyStack ms = new MyStack(); + + ms.push(1); + ms.push(2); + ms.push(13); + System.out.println("当前栈顶元素是: " + ms.peek()); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("出栈元素是: " + ms.pop()); + ms.push(12); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("当前栈顶元素是: " + ms.peek()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..340f79d240 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.HarryHook.coding2017.basic.MyQueue; + +public class QueueTest { + private MyQueue queue; + + @Before + public void setUpQueue() { + queue = new MyQueue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..26160faef6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.MyStack; + +public class StackTest { + + private MyStack stack; + + @Before + public void setUpStack() { + stack = new MyStack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From 5c888ed169a15cd2d09aa486f28e412fc60553c8 Mon Sep 17 00:00:00 2001 From: Ven13 <106614649@qq.com> Date: Sun, 26 Feb 2017 18:01:53 +0800 Subject: [PATCH 24/37] add Test junit --- .../Ven13/coding2017/basic/ArrayList.java | 8 +- .../Ven13/coding2017/basic/LinkedList.java | 30 +++- .../github/Ven13/coding2017/basic/List.java | 2 + .../coding2017/basic/test/ArrayListTest.java | 11 ++ .../coding2017/basic/test/LinkedListTest.java | 100 ++++++++++++++ .../Ven13/coding2017/basic/test/ListTest.java | 129 ++++++++++++++++++ .../coding2017/basic/test/QueueTest.java | 36 +++++ .../coding2017/basic/test/StackTest.java | 40 ++++++ 8 files changed, 348 insertions(+), 8 deletions(-) create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java index b636dc92fe..59ca0d34ee 100644 --- a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java @@ -54,15 +54,15 @@ public Object remove(int index) { }; int moveSize = size - index - 1; + if (moveSize > 0) { System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); } elementData[--size] = null; - for(int i = index; i < elementData.length; i++) { - elementData[i] = elementData[i+1]; - } - size--; + //for(int i = index; i < elementData.length; i++) { + // elementData[i] = elementData[i+1]; + //} return elementData; } diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java index 0b605c28ee..39edb53ef1 100644 --- a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java @@ -86,8 +86,11 @@ public Object get(int index) { @Override public Object remove(int index) { if(index == 0) { - head.next = head; - return head.data; + Node node = head; + Node temp = node.next; + head = temp; + size--; + return node.data; } else { if(index >= size) { throw new java.util.NoSuchElementException(); @@ -111,11 +114,30 @@ public int size() { } public Object removeFirst() { - return remove(0); + //ͨ��ͷָ�봴��ͷ�ڵ� + Node hNode = head; + if (hNode == null) { + throw new java.util.NoSuchElementException(); + } + Node nNode = hNode.next; + Object element = hNode.data; + + //�Ƴ� + hNode.data = null; + hNode.next = null; + head = nNode; + //�ж��Ƿ�Ϊβ�ڵ� + if (nNode == null) { + tail = null; + }else { + nNode = null; + } + size --; + return element; } public Object removeLast() { - return remove(size); + return remove(size - 1); } public Iterator iterator() { diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java index 525eb92857..02e4297a33 100644 --- a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java @@ -6,4 +6,6 @@ public interface List { public Object get(int index); public Object remove(int index); public int size(); + + public Iterator iterator(); } diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..7f3f179f3b --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,11 @@ +package com.github.Ven13.coding2017.basic.test; + +import org.junit.Before; + +import com.github.Ven13.coding2017.basic.*; + +public class ArrayListTest extends ListTest{ + + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..c6bc65698c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,100 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.ArrayList; +import com.github.Ven13.coding2017.basic.LinkedList; +import com.github.Ven13.coding2017.basic.List; + +public class LinkedListTest extends ListTest { + +private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + List aList = new ArrayList(); + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + //assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i = 1; i < 4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..6959da0421 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java @@ -0,0 +1,129 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.*; + +import com.github.Ven13.coding2017.basic.ArrayList; +import com.github.Ven13.coding2017.basic.Iterator; +import com.github.Ven13.coding2017.basic.List; + +public class ListTest { + + //protected static List aList; + + @Test + public void testFunctional() { + + List aList = new ArrayList(); + + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + + List aList = new ArrayList(); + + for (int i = 0; i < 100; i++) { + aList.add(i); + } + + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + + List aList = new ArrayList(); + + aList.add(1); + aList.add(2); + aList.add(3); + aList.remove(3); + assertEquals(2, aList.size()); + + } + + @Test + public void testSize() { + + List aList = new ArrayList(); + + for (int i = 0; i < 10; i++) { + aList.add(i * 2); + } + + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + + List aList = new ArrayList(); + + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + } + + @Test + public void testIterator() { + + List aList = new ArrayList(); + + Iterator it = aList.iterator(); + + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..965610cdce --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java @@ -0,0 +1,36 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.Queue; + +public class QueueTest { + +private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..9bbf41914f --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.Stack; + +public class StackTest { + +private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From 53bf53d3a6ff6cfa8cd32e37a23295797746caed Mon Sep 17 00:00:00 2001 From: 435994736 <435994736@qq.com> Date: Sun, 26 Feb 2017 18:34:09 +0800 Subject: [PATCH 25/37] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/MyArrayList.java | 83 ++++++++++++ .../coding2017/basic/MyLinkedList.java | 119 +++++++++++++++++ .../lhpmatlab/coding2017/basic/MyQueue.java | 29 +++++ .../lhpmatlab/coding2017/basic/MyStack.java | 30 +++++ .../coding2017/basic/MyArrayListTest.java | 113 ++++++++++++++++ .../coding2017/basic/MyLinkedListTest.java | 121 ++++++++++++++++++ .../coding2017/basic/MyQueueTest.java | 71 ++++++++++ .../coding2017/basic/MyStackTest.java | 104 +++++++++++++++ 8 files changed, 670 insertions(+) create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyLinkedListTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyQueueTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..03f6710788 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java @@ -0,0 +1,83 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyArrayList { + + private Object[] initialArray = {}; + private Object[] dataArray; + private int initSize = 10; + private int arraySize; + public MyArrayList() { + dataArray = initialArray; + } + + public MyArrayList(int init) { + dataArray = new Object[init]; + } + + public void ensureCapacity(int newCapacity) { + if (newCapacity < arraySize) + return; + + Object[] old = dataArray; + dataArray = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + dataArray[i] = old[i]; + } + } + + public void add(T element) { + add(size(), element); + } + + public void add(int index, T element) { + if (size() == dataArray.length) { + ensureCapacity(size()*2 + 1); + } + for(int i=arraySize;i>index;i--) { + dataArray[i] = dataArray[i - 1]; + } + dataArray[index] = element; + arraySize++; + } + + public T delete(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T removeElement = (T)dataArray[index]; + for (int i = index; i < size() -1; i++) { + dataArray[i] = dataArray[i + 1]; + } + arraySize--; + return removeElement; + } + + public T get(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + public T set(int index, T newElement) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T oldElement = (T) dataArray[index]; + dataArray[index] = newElement; + + return oldElement; + } + + public int size() { + return arraySize; + } + + public boolean isEmpty() { + return size() == 0; + } + +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..9e7eab3401 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java @@ -0,0 +1,119 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyLinkedList { + private class Node { + public Node pre; + public Node next; + public T data; + + public Node(Node pre,Node next,T data) { + this.pre = pre; + this.next = next; + this.data = data; + } + } + + private int dataSize; + + private Node head; + private Node tail; + + public MyLinkedList() { + head = new Node(null,null,null); + tail = new Node(head, null, null); + head.next = tail; + dataSize = 0; + } + + public void add(T t) { +// add(size(), t); + Node newNode = new Node<>(null, tail, t); + newNode.pre = tail.pre; + tail.pre.next = newNode; + tail.pre = newNode; + dataSize++; + + } + + /** + * 根据索引添加没有实现 + * @param index + * @param element + */ + public void add(int index,T element) { + //TODO 根据索引添加元素 +// addBefore(getNode(index,0,size()-1),element); +// if (index == dataSize) { +// add(element); +// } else { + // +// } + } + + public T get(int index) { + return getNode(index).data; + } + + public T set(int index, T newValue) { + Node node = getNode(index); + T oldData = node.data; + node.data = newValue; + return oldData; + } + + public T remove(int index) { + Node node = getNode(index); + node.next.pre = node.pre; + node.pre.next = node.next; + dataSize--; + + return node.data; + + } + + private void addBefore(Node node, T element) { +// newNode.pre.next = newNode; +// node.pre = newNode; + Node pre = node.pre; + Node newNode = new Node<>(node.pre, node, element); + node.pre = newNode; + pre.next = newNode; + + dataSize++; + } + + private Node getNode(int index) { + return getNode(index, 0, size()); + } + + private Node getNode(int index, int lower, int upper) { + Node p; + if (index < lower || index > upper) { + throw new IndexOutOfBoundsException(); + } + + if (index < size() / 2) { + p = head.next; + for (int i = 0; i < index; i++) { + p = p.next; + } + } else { + p = tail.pre; + for (int i = size()-1; i > index; i--) { + p = p.pre; + } + } + return p; + } + + public int size() { + return dataSize; + } + + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..6d0c970b65 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java @@ -0,0 +1,29 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyQueue { + private MyLinkedList link = new MyLinkedList<>(); + + public void enQueue(T t) { + link.add(t); + } + + public T deQueue() { + if (size() <= 0) { + return null; + } + T t = link.get(0); + link.remove(0); + return t; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return link.size(); + } +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..3fd9d2f5c2 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java @@ -0,0 +1,30 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyStack { + private MyArrayList list = new MyArrayList<>(); + + public void push(T t) { + list.add(t); + } + + public T pop() { + if (size() <= 0) { + throw new IndexOutOfBoundsException(); + } + return list.delete(size() - 1); + } + + public T peek() { + return list.get(size() - 1); + } + + public boolean isEmpty() { + return list.size() == 0; + } + public int size() { + return list.size(); + } +} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java new file mode 100644 index 0000000000..e29d41feac --- /dev/null +++ b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java @@ -0,0 +1,113 @@ +package com.github.lhpmatlab.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class MyArrayListTest { + private MyArrayList list; + + @Before + public void init(){ + list = new MyArrayList<>(); + } + + @Test + public void testEnsureCapacity() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.ensureCapacity(10); + assertEquals("ensureCapacity size is 10 ", list.size(),1); + } + + /** + * 在列表的末尾添加元素 + */ + @Test + public void testAddT() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.add("2"); + assertEquals("add list size ", list.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyLinkedListTest { + private MyLinkedList linkedList; + + @Before + public void before() throws Exception { + linkedList = new MyLinkedList<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: add(T t) + * + */ + @Test + public void testAddT() throws Exception { + assertEquals("init list size is 0 ", linkedList.size(), 0); + linkedList.add("1"); + linkedList.add("2"); + assertEquals("add list size ", linkedList.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyQueueTest { + private MyQueue queue; + + @Before + public void init() throws Exception { + queue = new MyQueue<>(); + } + + /** + * + * Method: enQueue(T t) + * + */ + @Test + public void testEnQueue() throws Exception { + queue.enQueue("1"); + assertEquals("size ", queue.size(), 1); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); +// queue.deQueue(); + assertEquals("dequeue element ",queue.deQueue(),"1"); + assertEquals("size ", queue.size(), 1); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("isEmpty method",queue.isEmpty(),true); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); + assertEquals("size method", queue.size(),2); + } + + +} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java new file mode 100644 index 0000000000..a90af5d720 --- /dev/null +++ b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java @@ -0,0 +1,104 @@ +package com.github.lhpmatlab.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.*; + +/** +* MyStack Tester. +* +* @author +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyStackTest { + + MyStack stack; + + + @Before + public void init() throws Exception { + stack = new MyStack<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(T t) + * + */ + @Test + public void testPush() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + assertEquals("pust stack ", stack.size(),1); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + stack.pop(); + assertEquals("after pop ",stack.size(),1); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("peek ", stack.peek(),"2"); + } + + /** + *测试判空方法 + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("stack is empty ", stack.isEmpty(), true); + } + + /** + *测试判空方法,不为空的情况 + * Method: isEmpty() + * + */ + @Test + public void testIsNotEmpty() throws Exception { + stack.push("1"); + assertEquals("stack is empty ", stack.isEmpty(), false); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("size is 2", stack.size(), 2); + } + + +} From f9fe90226f941d00ade3d4278d1bbcdf54f1d993 Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Sun, 26 Feb 2017 18:45:56 +0800 Subject: [PATCH 26/37] =?UTF-8?q?Revert=20"=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/MyArrayList.java | 83 ------------ .../coding2017/basic/MyLinkedList.java | 119 ----------------- .../lhpmatlab/coding2017/basic/MyQueue.java | 29 ----- .../lhpmatlab/coding2017/basic/MyStack.java | 30 ----- .../coding2017/basic/MyArrayListTest.java | 113 ---------------- .../coding2017/basic/MyLinkedListTest.java | 121 ------------------ .../coding2017/basic/MyQueueTest.java | 71 ---------- .../coding2017/basic/MyStackTest.java | 104 --------------- 8 files changed, 670 deletions(-) delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyLinkedListTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyQueueTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java deleted file mode 100644 index 03f6710788..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/18. - */ -public class MyArrayList { - - private Object[] initialArray = {}; - private Object[] dataArray; - private int initSize = 10; - private int arraySize; - public MyArrayList() { - dataArray = initialArray; - } - - public MyArrayList(int init) { - dataArray = new Object[init]; - } - - public void ensureCapacity(int newCapacity) { - if (newCapacity < arraySize) - return; - - Object[] old = dataArray; - dataArray = new Object[newCapacity]; - for (int i = 0; i < size(); i++) { - dataArray[i] = old[i]; - } - } - - public void add(T element) { - add(size(), element); - } - - public void add(int index, T element) { - if (size() == dataArray.length) { - ensureCapacity(size()*2 + 1); - } - for(int i=arraySize;i>index;i--) { - dataArray[i] = dataArray[i - 1]; - } - dataArray[index] = element; - arraySize++; - } - - public T delete(int index) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - T removeElement = (T)dataArray[index]; - for (int i = index; i < size() -1; i++) { - dataArray[i] = dataArray[i + 1]; - } - arraySize--; - return removeElement; - } - - public T get(int index) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - return (T)dataArray[index]; - } - - public T set(int index, T newElement) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - T oldElement = (T) dataArray[index]; - dataArray[index] = newElement; - - return oldElement; - } - - public int size() { - return arraySize; - } - - public boolean isEmpty() { - return size() == 0; - } - -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java deleted file mode 100644 index 9e7eab3401..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/18. - */ -public class MyLinkedList { - private class Node { - public Node pre; - public Node next; - public T data; - - public Node(Node pre,Node next,T data) { - this.pre = pre; - this.next = next; - this.data = data; - } - } - - private int dataSize; - - private Node head; - private Node tail; - - public MyLinkedList() { - head = new Node(null,null,null); - tail = new Node(head, null, null); - head.next = tail; - dataSize = 0; - } - - public void add(T t) { -// add(size(), t); - Node newNode = new Node<>(null, tail, t); - newNode.pre = tail.pre; - tail.pre.next = newNode; - tail.pre = newNode; - dataSize++; - - } - - /** - * 根据索引添加没有实现 - * @param index - * @param element - */ - public void add(int index,T element) { - //TODO 根据索引添加元素 -// addBefore(getNode(index,0,size()-1),element); -// if (index == dataSize) { -// add(element); -// } else { - // -// } - } - - public T get(int index) { - return getNode(index).data; - } - - public T set(int index, T newValue) { - Node node = getNode(index); - T oldData = node.data; - node.data = newValue; - return oldData; - } - - public T remove(int index) { - Node node = getNode(index); - node.next.pre = node.pre; - node.pre.next = node.next; - dataSize--; - - return node.data; - - } - - private void addBefore(Node node, T element) { -// newNode.pre.next = newNode; -// node.pre = newNode; - Node pre = node.pre; - Node newNode = new Node<>(node.pre, node, element); - node.pre = newNode; - pre.next = newNode; - - dataSize++; - } - - private Node getNode(int index) { - return getNode(index, 0, size()); - } - - private Node getNode(int index, int lower, int upper) { - Node p; - if (index < lower || index > upper) { - throw new IndexOutOfBoundsException(); - } - - if (index < size() / 2) { - p = head.next; - for (int i = 0; i < index; i++) { - p = p.next; - } - } else { - p = tail.pre; - for (int i = size()-1; i > index; i--) { - p = p.pre; - } - } - return p; - } - - public int size() { - return dataSize; - } - - public boolean isEmpty() { - return size() == 0; - } -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java deleted file mode 100644 index 6d0c970b65..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/22. - */ -public class MyQueue { - private MyLinkedList link = new MyLinkedList<>(); - - public void enQueue(T t) { - link.add(t); - } - - public T deQueue() { - if (size() <= 0) { - return null; - } - T t = link.get(0); - link.remove(0); - return t; - } - - public boolean isEmpty() { - return size() == 0; - } - - public int size() { - return link.size(); - } -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java deleted file mode 100644 index 3fd9d2f5c2..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/22. - */ -public class MyStack { - private MyArrayList list = new MyArrayList<>(); - - public void push(T t) { - list.add(t); - } - - public T pop() { - if (size() <= 0) { - throw new IndexOutOfBoundsException(); - } - return list.delete(size() - 1); - } - - public T peek() { - return list.get(size() - 1); - } - - public boolean isEmpty() { - return list.size() == 0; - } - public int size() { - return list.size(); - } -} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java deleted file mode 100644 index e29d41feac..0000000000 --- a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; - -public class MyArrayListTest { - private MyArrayList list; - - @Before - public void init(){ - list = new MyArrayList<>(); - } - - @Test - public void testEnsureCapacity() { - assertEquals("init list size is 0 ", list.size(), 0); - list.add("1"); - list.ensureCapacity(10); - assertEquals("ensureCapacity size is 10 ", list.size(),1); - } - - /** - * 在列表的末尾添加元素 - */ - @Test - public void testAddT() { - assertEquals("init list size is 0 ", list.size(), 0); - list.add("1"); - list.add("2"); - assertEquals("add list size ", list.size(), 2); - for (int i=0; i -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyLinkedListTest { - private MyLinkedList linkedList; - - @Before - public void before() throws Exception { - linkedList = new MyLinkedList<>(); - } - - @After - public void after() throws Exception { - } - - /** - * - * Method: add(T t) - * - */ - @Test - public void testAddT() throws Exception { - assertEquals("init list size is 0 ", linkedList.size(), 0); - linkedList.add("1"); - linkedList.add("2"); - assertEquals("add list size ", linkedList.size(), 2); - for (int i=0; i -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyQueueTest { - private MyQueue queue; - - @Before - public void init() throws Exception { - queue = new MyQueue<>(); - } - - /** - * - * Method: enQueue(T t) - * - */ - @Test - public void testEnQueue() throws Exception { - queue.enQueue("1"); - assertEquals("size ", queue.size(), 1); - } - - /** - * - * Method: deQueue() - * - */ - @Test - public void testDeQueue() throws Exception { - queue.enQueue("1"); - queue.enQueue("2"); -// queue.deQueue(); - assertEquals("dequeue element ",queue.deQueue(),"1"); - assertEquals("size ", queue.size(), 1); - - } - - /** - * - * Method: isEmpty() - * - */ - @Test - public void testIsEmpty() throws Exception { - assertEquals("isEmpty method",queue.isEmpty(),true); - } - - /** - * - * Method: size() - * - */ - @Test - public void testSize() throws Exception { - queue.enQueue("1"); - queue.enQueue("2"); - assertEquals("size method", queue.size(),2); - } - - -} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java deleted file mode 100644 index a90af5d720..0000000000 --- a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.*; - -/** -* MyStack Tester. -* -* @author -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyStackTest { - - MyStack stack; - - - @Before - public void init() throws Exception { - stack = new MyStack<>(); - } - - @After - public void after() throws Exception { - } - - /** - * - * Method: push(T t) - * - */ - @Test - public void testPush() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - assertEquals("pust stack ", stack.size(),1); - } - - /** - * - * Method: pop() - * - */ - @Test - public void testPop() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - stack.pop(); - assertEquals("after pop ",stack.size(),1); - } - - /** - * - * Method: peek() - * - */ - @Test - public void testPeek() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - assertEquals("peek ", stack.peek(),"2"); - } - - /** - *测试判空方法 - * Method: isEmpty() - * - */ - @Test - public void testIsEmpty() throws Exception { - assertEquals("stack is empty ", stack.isEmpty(), true); - } - - /** - *测试判空方法,不为空的情况 - * Method: isEmpty() - * - */ - @Test - public void testIsNotEmpty() throws Exception { - stack.push("1"); - assertEquals("stack is empty ", stack.isEmpty(), false); - } - - /** - * - * Method: size() - * - */ - @Test - public void testSize() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - assertEquals("size is 2", stack.size(), 2); - } - - -} From 5cfb6d580c5c88a37a6ec91a690d2d6da8f554c8 Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Sun, 26 Feb 2017 18:59:15 +0800 Subject: [PATCH 27/37] =?UTF-8?q?Revert=20"Revert=20"=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?""?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/MyArrayList.java | 83 ++++++++++++ .../coding2017/basic/MyLinkedList.java | 119 +++++++++++++++++ .../lhpmatlab/coding2017/basic/MyQueue.java | 29 +++++ .../lhpmatlab/coding2017/basic/MyStack.java | 30 +++++ .../coding2017/basic/MyArrayListTest.java | 113 ++++++++++++++++ .../coding2017/basic/MyLinkedListTest.java | 121 ++++++++++++++++++ .../coding2017/basic/MyQueueTest.java | 71 ++++++++++ .../coding2017/basic/MyStackTest.java | 104 +++++++++++++++ 8 files changed, 670 insertions(+) create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java create mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyLinkedListTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyQueueTest.java create mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..03f6710788 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java @@ -0,0 +1,83 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyArrayList { + + private Object[] initialArray = {}; + private Object[] dataArray; + private int initSize = 10; + private int arraySize; + public MyArrayList() { + dataArray = initialArray; + } + + public MyArrayList(int init) { + dataArray = new Object[init]; + } + + public void ensureCapacity(int newCapacity) { + if (newCapacity < arraySize) + return; + + Object[] old = dataArray; + dataArray = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + dataArray[i] = old[i]; + } + } + + public void add(T element) { + add(size(), element); + } + + public void add(int index, T element) { + if (size() == dataArray.length) { + ensureCapacity(size()*2 + 1); + } + for(int i=arraySize;i>index;i--) { + dataArray[i] = dataArray[i - 1]; + } + dataArray[index] = element; + arraySize++; + } + + public T delete(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T removeElement = (T)dataArray[index]; + for (int i = index; i < size() -1; i++) { + dataArray[i] = dataArray[i + 1]; + } + arraySize--; + return removeElement; + } + + public T get(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + public T set(int index, T newElement) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T oldElement = (T) dataArray[index]; + dataArray[index] = newElement; + + return oldElement; + } + + public int size() { + return arraySize; + } + + public boolean isEmpty() { + return size() == 0; + } + +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..9e7eab3401 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java @@ -0,0 +1,119 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyLinkedList { + private class Node { + public Node pre; + public Node next; + public T data; + + public Node(Node pre,Node next,T data) { + this.pre = pre; + this.next = next; + this.data = data; + } + } + + private int dataSize; + + private Node head; + private Node tail; + + public MyLinkedList() { + head = new Node(null,null,null); + tail = new Node(head, null, null); + head.next = tail; + dataSize = 0; + } + + public void add(T t) { +// add(size(), t); + Node newNode = new Node<>(null, tail, t); + newNode.pre = tail.pre; + tail.pre.next = newNode; + tail.pre = newNode; + dataSize++; + + } + + /** + * 根据索引添加没有实现 + * @param index + * @param element + */ + public void add(int index,T element) { + //TODO 根据索引添加元素 +// addBefore(getNode(index,0,size()-1),element); +// if (index == dataSize) { +// add(element); +// } else { + // +// } + } + + public T get(int index) { + return getNode(index).data; + } + + public T set(int index, T newValue) { + Node node = getNode(index); + T oldData = node.data; + node.data = newValue; + return oldData; + } + + public T remove(int index) { + Node node = getNode(index); + node.next.pre = node.pre; + node.pre.next = node.next; + dataSize--; + + return node.data; + + } + + private void addBefore(Node node, T element) { +// newNode.pre.next = newNode; +// node.pre = newNode; + Node pre = node.pre; + Node newNode = new Node<>(node.pre, node, element); + node.pre = newNode; + pre.next = newNode; + + dataSize++; + } + + private Node getNode(int index) { + return getNode(index, 0, size()); + } + + private Node getNode(int index, int lower, int upper) { + Node p; + if (index < lower || index > upper) { + throw new IndexOutOfBoundsException(); + } + + if (index < size() / 2) { + p = head.next; + for (int i = 0; i < index; i++) { + p = p.next; + } + } else { + p = tail.pre; + for (int i = size()-1; i > index; i--) { + p = p.pre; + } + } + return p; + } + + public int size() { + return dataSize; + } + + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..6d0c970b65 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java @@ -0,0 +1,29 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyQueue { + private MyLinkedList link = new MyLinkedList<>(); + + public void enQueue(T t) { + link.add(t); + } + + public T deQueue() { + if (size() <= 0) { + return null; + } + T t = link.get(0); + link.remove(0); + return t; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return link.size(); + } +} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..3fd9d2f5c2 --- /dev/null +++ b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java @@ -0,0 +1,30 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyStack { + private MyArrayList list = new MyArrayList<>(); + + public void push(T t) { + list.add(t); + } + + public T pop() { + if (size() <= 0) { + throw new IndexOutOfBoundsException(); + } + return list.delete(size() - 1); + } + + public T peek() { + return list.get(size() - 1); + } + + public boolean isEmpty() { + return list.size() == 0; + } + public int size() { + return list.size(); + } +} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java new file mode 100644 index 0000000000..e29d41feac --- /dev/null +++ b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java @@ -0,0 +1,113 @@ +package com.github.lhpmatlab.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class MyArrayListTest { + private MyArrayList list; + + @Before + public void init(){ + list = new MyArrayList<>(); + } + + @Test + public void testEnsureCapacity() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.ensureCapacity(10); + assertEquals("ensureCapacity size is 10 ", list.size(),1); + } + + /** + * 在列表的末尾添加元素 + */ + @Test + public void testAddT() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.add("2"); + assertEquals("add list size ", list.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyLinkedListTest { + private MyLinkedList linkedList; + + @Before + public void before() throws Exception { + linkedList = new MyLinkedList<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: add(T t) + * + */ + @Test + public void testAddT() throws Exception { + assertEquals("init list size is 0 ", linkedList.size(), 0); + linkedList.add("1"); + linkedList.add("2"); + assertEquals("add list size ", linkedList.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyQueueTest { + private MyQueue queue; + + @Before + public void init() throws Exception { + queue = new MyQueue<>(); + } + + /** + * + * Method: enQueue(T t) + * + */ + @Test + public void testEnQueue() throws Exception { + queue.enQueue("1"); + assertEquals("size ", queue.size(), 1); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); +// queue.deQueue(); + assertEquals("dequeue element ",queue.deQueue(),"1"); + assertEquals("size ", queue.size(), 1); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("isEmpty method",queue.isEmpty(),true); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); + assertEquals("size method", queue.size(),2); + } + + +} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java new file mode 100644 index 0000000000..a90af5d720 --- /dev/null +++ b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java @@ -0,0 +1,104 @@ +package com.github.lhpmatlab.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.*; + +/** +* MyStack Tester. +* +* @author +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyStackTest { + + MyStack stack; + + + @Before + public void init() throws Exception { + stack = new MyStack<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(T t) + * + */ + @Test + public void testPush() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + assertEquals("pust stack ", stack.size(),1); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + stack.pop(); + assertEquals("after pop ",stack.size(),1); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("peek ", stack.peek(),"2"); + } + + /** + *测试判空方法 + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("stack is empty ", stack.isEmpty(), true); + } + + /** + *测试判空方法,不为空的情况 + * Method: isEmpty() + * + */ + @Test + public void testIsNotEmpty() throws Exception { + stack.push("1"); + assertEquals("stack is empty ", stack.isEmpty(), false); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("size is 2", stack.size(), 2); + } + + +} From 63a23fd32c7776e6ca7e0021e7a437c31a6d02ae Mon Sep 17 00:00:00 2001 From: 435994736 <435994736@qq.com> Date: Sun, 26 Feb 2017 19:37:39 +0800 Subject: [PATCH 28/37] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=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/MyArrayList.java | 83 ++++++++++++ .../coding2017/basic/MyLinkedList.java | 119 +++++++++++++++++ .../lhpmatlab/coding2017/basic/MyQueue.java | 29 +++++ .../lhpmatlab/coding2017/basic/MyStack.java | 30 +++++ .../coding2017/basic/MyArrayListTest.java | 113 ++++++++++++++++ .../coding2017/basic/MyLinkedListTest.java | 121 ++++++++++++++++++ .../coding2017/basic/MyQueueTest.java | 71 ++++++++++ .../coding2017/basic/MyStackTest.java | 104 +++++++++++++++ 8 files changed, 670 insertions(+) create mode 100644 group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java create mode 100644 group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java create mode 100644 group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java create mode 100644 group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java create mode 100644 group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java create mode 100644 group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyLinkedListTest.java create mode 100644 group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyQueueTest.java create mode 100644 group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..03f6710788 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java @@ -0,0 +1,83 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyArrayList { + + private Object[] initialArray = {}; + private Object[] dataArray; + private int initSize = 10; + private int arraySize; + public MyArrayList() { + dataArray = initialArray; + } + + public MyArrayList(int init) { + dataArray = new Object[init]; + } + + public void ensureCapacity(int newCapacity) { + if (newCapacity < arraySize) + return; + + Object[] old = dataArray; + dataArray = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + dataArray[i] = old[i]; + } + } + + public void add(T element) { + add(size(), element); + } + + public void add(int index, T element) { + if (size() == dataArray.length) { + ensureCapacity(size()*2 + 1); + } + for(int i=arraySize;i>index;i--) { + dataArray[i] = dataArray[i - 1]; + } + dataArray[index] = element; + arraySize++; + } + + public T delete(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T removeElement = (T)dataArray[index]; + for (int i = index; i < size() -1; i++) { + dataArray[i] = dataArray[i + 1]; + } + arraySize--; + return removeElement; + } + + public T get(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + public T set(int index, T newElement) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T oldElement = (T) dataArray[index]; + dataArray[index] = newElement; + + return oldElement; + } + + public int size() { + return arraySize; + } + + public boolean isEmpty() { + return size() == 0; + } + +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..9e7eab3401 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java @@ -0,0 +1,119 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyLinkedList { + private class Node { + public Node pre; + public Node next; + public T data; + + public Node(Node pre,Node next,T data) { + this.pre = pre; + this.next = next; + this.data = data; + } + } + + private int dataSize; + + private Node head; + private Node tail; + + public MyLinkedList() { + head = new Node(null,null,null); + tail = new Node(head, null, null); + head.next = tail; + dataSize = 0; + } + + public void add(T t) { +// add(size(), t); + Node newNode = new Node<>(null, tail, t); + newNode.pre = tail.pre; + tail.pre.next = newNode; + tail.pre = newNode; + dataSize++; + + } + + /** + * 根据索引添加没有实现 + * @param index + * @param element + */ + public void add(int index,T element) { + //TODO 根据索引添加元素 +// addBefore(getNode(index,0,size()-1),element); +// if (index == dataSize) { +// add(element); +// } else { + // +// } + } + + public T get(int index) { + return getNode(index).data; + } + + public T set(int index, T newValue) { + Node node = getNode(index); + T oldData = node.data; + node.data = newValue; + return oldData; + } + + public T remove(int index) { + Node node = getNode(index); + node.next.pre = node.pre; + node.pre.next = node.next; + dataSize--; + + return node.data; + + } + + private void addBefore(Node node, T element) { +// newNode.pre.next = newNode; +// node.pre = newNode; + Node pre = node.pre; + Node newNode = new Node<>(node.pre, node, element); + node.pre = newNode; + pre.next = newNode; + + dataSize++; + } + + private Node getNode(int index) { + return getNode(index, 0, size()); + } + + private Node getNode(int index, int lower, int upper) { + Node p; + if (index < lower || index > upper) { + throw new IndexOutOfBoundsException(); + } + + if (index < size() / 2) { + p = head.next; + for (int i = 0; i < index; i++) { + p = p.next; + } + } else { + p = tail.pre; + for (int i = size()-1; i > index; i--) { + p = p.pre; + } + } + return p; + } + + public int size() { + return dataSize; + } + + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..6d0c970b65 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java @@ -0,0 +1,29 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyQueue { + private MyLinkedList link = new MyLinkedList<>(); + + public void enQueue(T t) { + link.add(t); + } + + public T deQueue() { + if (size() <= 0) { + return null; + } + T t = link.get(0); + link.remove(0); + return t; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return link.size(); + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..3fd9d2f5c2 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java @@ -0,0 +1,30 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyStack { + private MyArrayList list = new MyArrayList<>(); + + public void push(T t) { + list.add(t); + } + + public T pop() { + if (size() <= 0) { + throw new IndexOutOfBoundsException(); + } + return list.delete(size() - 1); + } + + public T peek() { + return list.get(size() - 1); + } + + public boolean isEmpty() { + return list.size() == 0; + } + public int size() { + return list.size(); + } +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java new file mode 100644 index 0000000000..e29d41feac --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java @@ -0,0 +1,113 @@ +package com.github.lhpmatlab.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class MyArrayListTest { + private MyArrayList list; + + @Before + public void init(){ + list = new MyArrayList<>(); + } + + @Test + public void testEnsureCapacity() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.ensureCapacity(10); + assertEquals("ensureCapacity size is 10 ", list.size(),1); + } + + /** + * 在列表的末尾添加元素 + */ + @Test + public void testAddT() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.add("2"); + assertEquals("add list size ", list.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyLinkedListTest { + private MyLinkedList linkedList; + + @Before + public void before() throws Exception { + linkedList = new MyLinkedList<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: add(T t) + * + */ + @Test + public void testAddT() throws Exception { + assertEquals("init list size is 0 ", linkedList.size(), 0); + linkedList.add("1"); + linkedList.add("2"); + assertEquals("add list size ", linkedList.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyQueueTest { + private MyQueue queue; + + @Before + public void init() throws Exception { + queue = new MyQueue<>(); + } + + /** + * + * Method: enQueue(T t) + * + */ + @Test + public void testEnQueue() throws Exception { + queue.enQueue("1"); + assertEquals("size ", queue.size(), 1); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); +// queue.deQueue(); + assertEquals("dequeue element ",queue.deQueue(),"1"); + assertEquals("size ", queue.size(), 1); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("isEmpty method",queue.isEmpty(),true); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); + assertEquals("size method", queue.size(),2); + } + + +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java new file mode 100644 index 0000000000..a90af5d720 --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java @@ -0,0 +1,104 @@ +package com.github.lhpmatlab.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.*; + +/** +* MyStack Tester. +* +* @author +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyStackTest { + + MyStack stack; + + + @Before + public void init() throws Exception { + stack = new MyStack<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(T t) + * + */ + @Test + public void testPush() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + assertEquals("pust stack ", stack.size(),1); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + stack.pop(); + assertEquals("after pop ",stack.size(),1); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("peek ", stack.peek(),"2"); + } + + /** + *测试判空方法 + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("stack is empty ", stack.isEmpty(), true); + } + + /** + *测试判空方法,不为空的情况 + * Method: isEmpty() + * + */ + @Test + public void testIsNotEmpty() throws Exception { + stack.push("1"); + assertEquals("stack is empty ", stack.isEmpty(), false); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("size is 2", stack.size(), 2); + } + + +} From a0ef984bc53b32faa28fc8da3103a2c475e73a3a Mon Sep 17 00:00:00 2001 From: ZhoufeifeiJAVA <793337535@qq.com> Date: Sun, 26 Feb 2017 19:42:24 +0800 Subject: [PATCH 29/37] MyArrayList,MyLinkedList,Queue,Stack --- .../coding2017/basic/Iterator.java | 7 + .../ZhoufeifeiJAVA/coding2017/basic/List.java | 9 ++ .../coding2017/basic/MyArrayList.java | 78 +++++++++++ .../coding2017/basic/MyArrayListTest.java | 34 +++++ .../coding2017/basic/MyLinkedList.java | 127 ++++++++++++++++++ .../coding2017/basic/MyLinkedListTest.java | 34 +++++ .../coding2017/basic/Queue.java | 26 ++++ .../coding2017/basic/QueueTest.java | 17 +++ .../coding2017/basic/Stack.java | 28 ++++ .../coding2017/basic/StackTest.java | 17 +++ 10 files changed, 377 insertions(+) create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayListTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedList.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..34d062b327 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java new file mode 100644 index 0000000000..7a13f14c83 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.ZhoufeifeiJAVA.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/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..0666530694 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java @@ -0,0 +1,78 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o)throws RuntimeException{ + if(index > size) + throw new RuntimeException("index is bigger than the size of MyArrayList"); + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + if(index == size) + add(o); + else{ + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size ++; + } + private Object[] arrayGrow(Object[] src,int size){ + Object[] target = new Object[src.length+size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + public Object get(int index){ + if(index >= size) + return null; + else + return elementData[index]; + } + + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index>=size || index<0) + throw new IndexOutOfBoundsException("index is bigger than the size of MyArrayList"); + Object removeObject = elementData[index]; + for(int i=index;isize || index<0) + throw new IndexOutOfBoundsException("the index is bigger than the size of MyLinkedList"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0){ + newNode.next = head; + head = newNode; + } + else{ + int i = 0; + Node pointer = head; + while(isize-1) + return null; + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index --; + } + return pointer.data; + + } + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index<0 || index>size-1) + throw new IndexOutOfBoundsException("the index is not legal"); + Node pointer = head; + if(index == 0){ + head = head.next; + size --; + return pointer.data; + } + else{ + while(index>1){ + pointer = pointer.next; + index --; + } + Node temp = pointer.next; + pointer.next = pointer.next.next; + size --; + return temp.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + + private static class Node{ + Object data; + Node next; + } + private class MyLinkedListIterator implements Iterator{ + private Node pointer; + MyLinkedListIterator(){ + pointer = head; + } + public boolean hasNext() { + if(pointer.next != null) + return true; + else + return false; + } + public Object next() { + Node temp = pointer; + pointer = pointer.next; + return temp.data; + } + } + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + //public void showData(int index){ + // System.out.println("the data of "+index+" is "+get(index).data); + //} +} + + + + + + + diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..6d5ca14e70 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java @@ -0,0 +1,34 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class MyLinkedListTest{ + public static void main(String[] args){ + MyLinkedList al = new MyLinkedList(); + al.add("string0"); + al.add("string1"); + al.add("string2"); + al.add("string3"); + al.add("string4"); + al.add("string5"); + al.add("string6"); + al.add("string7"); + al.add("string8"); + al.add("string9"); + al.add("string10"); + al.add("string11"); + al.add("string12"); + al.add("string13"); + al.add("string14"); + al.add(11,"string10.5"); + sop(al.remove(4)); + sop(al.get(10)); + sop(al.get(11)); + sop(al.get(12)); + sop("the size of al is "+al.size()); + sop("the iterator method used,so the result is as follows:"); + for(Iterator it = al.iterator();it.hasNext();){ + sop(it.next()); + } + } + public static void sop(Object o){ + System.out.println(o); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java new file mode 100644 index 0000000000..0ce735f150 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java @@ -0,0 +1,26 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Queue { + private MyLinkedList llist = null; + Queue(){ + llist = new MyLinkedList(); + } + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + return llist.removeFirst(); + } + + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + + public int size(){ + return llist.size(); + } +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..95cc5117ff --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class QueueTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("String0"); + q.enQueue("String1"); + q.enQueue("String2"); + sop("the queue is not empty "+q.isEmpty()); + sop("the size of queue is "+q.size()); + sop("out queue "+q.deQueue()); + sop("out queue "+q.deQueue()); + sop("the size of queue is "+q.size()); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3992f89f86 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Stack { + private MyLinkedList llist = null; + Stack(){ + llist = new MyLinkedList(); + } + public void push(Object o){ + llist.add(o); + } + + public Object pop(){ + return llist.removeLast(); + } + + public Object peek(){ + return llist.get(llist.size()-1); + } + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + public int size(){ + return llist.size(); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..4bcb60e35d --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class StackTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Stack s = new Stack(); + s.push("String0"); + s.push("String1"); + s.push("String2"); + sop("the queue is not empty "+s.isEmpty()); + sop("the size of queue is "+s.size()); + sop("out queue "+s.pop()); + sop("just watch queue,not delete "+s.peek()); + sop("the size of queue is "+s.size()); + } +} \ No newline at end of file From b073bb8f894e4b0ba469b5fdba591a92b1d2c154 Mon Sep 17 00:00:00 2001 From: lirenxn Date: Mon, 27 Feb 2017 02:02:18 +1100 Subject: [PATCH 30/37] ArrayList and LiknedList test pass --- .../coding2017/basic/ArrayList.java | 38 ++--- .../coding2017/basic/LinkedList.java | 55 ++++--- .../coding2017/basicTest/ArrayListTest.java | 115 ++++++++++++++ .../coding2017/basicTest/LinkedListTest.java | 143 ++++++++++++++++++ .../coding2017/basicTest/TestRunner.java | 30 ++++ .../coding2017/basicTest/testCase.java | 21 +++ 6 files changed, 365 insertions(+), 37 deletions(-) create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java index cfdc5fefe1..af4588763e 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java @@ -22,46 +22,34 @@ public void add(Object o){ } public void add(int index, Object o){ - //Check Object class - if(size >= 1){ - if(o.getClass() != elementData[0].getClass()) - throw new InputMismatchException("Index:"+index+" Size:"+size); - } - //index Check - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } + checkIndex(index); //check size limit if(size + 1 > elementData.length){ - Object oldData[] = elementData; int newlength = elementData.length * 3 / 2 + 1; elementData = Arrays.copyOf(elementData, newlength); } - for(int i = size-1; i >= index; i-- ){ + for(int i = ++size; i >= index; i-- ){ elementData[i] = elementData[i-1]; } elementData[index] = o; + } public Object get(int index){ //index Check - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } + checkIndex(index); return elementData[index]; } public Object remove(int index){ //index Check - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } + checkIndex(index); Object old = elementData[index]; for(int i = index; i < size-1 ; i++ ){ @@ -79,6 +67,18 @@ public Iterator iterator(){ return new Itr(); } + public void clear(){ + elementData = new Object[10]; + size = 0; + } + + private void checkIndex(int index){ + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + } + + private class Itr implements Iterator{ //index for next element to visit private int cursor = 0; @@ -106,8 +106,8 @@ public void remove() { ArrayList.this.remove(--cursor); } - - } + + } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java index 840bb14f7c..640d9ec974 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java @@ -8,8 +8,7 @@ public class LinkedList implements List { private int size; public LinkedList(){ - head.next = head; - head.previous = head; + head = new Node(); size = 0; } @@ -19,9 +18,7 @@ public void add(Object o){ public void add(int index , Object o){ //Check bound - if(index >= size){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } + checkIndex(index); Node nx = this.find(index); Node pr = nx.previous; @@ -33,17 +30,14 @@ public void add(int index , Object o){ public Object get(int index){ //Check bound - if(index >= size){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } - return this.find(index); + checkIndex(index); + + return this.find(index).data; } public Object remove(int index){ //Check bound - if(index >= size){ - throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); - } + checkIndex(index); Node rem = this.find(index); Node pr = rem.previous; @@ -88,6 +82,21 @@ public Object removeLast(){ public Iterator iterator(){ return new ListItr(); } + public void clear(){ + for (Node x = head; x != null; ) { + Node next = x.next; + x.data = null; + x.next = null; + x.previous = null; + x = next; + } + } + + private void checkIndex(int index){ + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + } private Node find(int index){ Node tra = head; @@ -98,7 +107,7 @@ private Node find(int index){ tra = tra.next; } }else{ - for(int i = size; i >= index; i--){ + for(int i = size; i > index; i--){ tra = tra.previous; } } @@ -110,6 +119,12 @@ private static class Node{ Node next; Node previous; + public Node(){ + data = null; + next = this; + previous = this; + } + public Node(Object obj,Node pre, Node nx){ data = obj; next = nx; @@ -136,28 +151,32 @@ public boolean hasNext() { @Override public Object next() { + checkBound(); Node re = cursor; cursor = cursor.next; nextIndex++; - return re; + return re.data; } public Object previous() { Node re = cursor.previous.previous; cursor = cursor.previous; nextIndex--; - return re; + return re.data; } @Override public void remove() { //Check bound - if(nextIndex > size){ - throw new NoSuchElementException("Iterates to the end"); - } + checkBound(); LinkedList.this.remove(--nextIndex); } + private void checkBound(){ + if(nextIndex >= size){ + throw new NoSuchElementException("Iterates to the end"); + } + } } } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java new file mode 100644 index 0000000000..647a0e9c4e --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java @@ -0,0 +1,115 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.ArrayList; +import com.github.congcongcong250.coding2017.basic.Iterator; + +public class ArrayListTest implements testCase { + + ArrayList testlist = new ArrayList(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 30; i++){ + testlist.add(i); + } + } + + @Override + @After + public void tearDown() { + testlist.clear(); + } + + @Override + @Test + public void testAdd() { + + assertEquals(0,testlist.get(0)); + assertEquals(11,testlist.get(11)); + assertEquals(20,testlist.get(20)); + assertEquals(29,testlist.get(29)); + assertEquals(30,testlist.size()); + + testlist.add(20, 100); + assertEquals(100,testlist.get(20)); + assertEquals(20,testlist.get(21)); + assertEquals(29,testlist.get(30)); + assertEquals(31,testlist.size()); + + } + + @Override + @Test + public void testRemove() { + + + assertEquals(6,testlist.get(6)); + assertEquals(30,testlist.size()); + testlist.remove(6); + assertEquals(7,testlist.get(6)); + assertEquals(29,testlist.size()); + assertEquals(21,testlist.get(20)); + assertEquals(5,testlist.get(5)); + } + + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetneg(){ + + ArrayList emptyList = new ArrayList(); + Object o = emptyList.get(-1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetout(){ + + Object o = testlist.get(31); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testremoveExp(){ + Object o = testlist.remove(31); + } + + @Override + @Test + public void testFunctional() { + Iterator itr = testlist.iterator(); + assertTrue(itr.hasNext()); + for(int i = 0; i < 20; i++){ + assertEquals(i, itr.next()); + } + itr.remove(); + + assertTrue(itr.hasNext()); + assertEquals(20, itr.next()); + assertEquals(29, testlist.size()); + + for(int i = 21; i < 30; i++){ + assertEquals(i, itr.next()); + } + assertFalse(itr.hasNext()); + + boolean hasExp = false; + try{ + itr.next(); + }catch (NoSuchElementException e){ + hasExp = true; + } + assertTrue(hasExp); + } + + + + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java new file mode 100644 index 0000000000..701bd54402 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java @@ -0,0 +1,143 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.ArrayList; +import com.github.congcongcong250.coding2017.basic.LinkedList; +import com.github.congcongcong250.coding2017.basic.Iterator; + +public class LinkedListTest implements testCase { + + LinkedList testlist = new LinkedList(); + + @Override + @Before + public void setUp() { + for(int i = 0; i < 20;i++){ + testlist.add(i); + } + } + + @Override + @After + public void tearDown() { + testlist.clear(); + } + + @Override + @Test + public void testAdd() { + assertEquals(20,testlist.size()); + assertEquals(0,testlist.get(0)); + assertEquals(19,testlist.get(19)); + + testlist.add(11, 100); + assertEquals(21,testlist.size()); + assertEquals(5,testlist.get(5)); + assertEquals(100,testlist.get(11)); + assertEquals(18,testlist.get(19)); + + testlist.addFirst(200); + assertEquals(22,testlist.size()); + assertEquals(200,testlist.get(0)); + assertEquals(4,testlist.get(5)); + assertEquals(100,testlist.get(12)); + assertEquals(17,testlist.get(19)); + + testlist.addLast(300); + assertEquals(23,testlist.size()); + assertEquals(200,testlist.get(0)); + assertEquals(4,testlist.get(5)); + assertEquals(100,testlist.get(12)); + assertEquals(17,testlist.get(19)); + assertEquals(300,testlist.get(22)); + + } + + @Override + @Test + public void testRemove() { + assertEquals(20,testlist.size()); + assertEquals(0,testlist.get(0)); + assertEquals(19,testlist.get(19)); + + testlist.remove(10); + assertEquals(19,testlist.size()); + assertEquals(4,testlist.get(4)); + assertEquals(9,testlist.get(9)); + assertEquals(11,testlist.get(10)); + assertEquals(19,testlist.get(18)); + + testlist.removeFirst(); + assertEquals(18,testlist.size()); + assertEquals(1,testlist.get(0)); + assertEquals(12,testlist.get(10)); + assertEquals(19,testlist.get(17)); + + testlist.removeLast(); + assertEquals(17,testlist.size()); + assertEquals(1,testlist.get(0)); + assertEquals(12,testlist.get(10)); + assertEquals(18,testlist.get(16)); + + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetneg(){ + + LinkedList emptyList = new LinkedList(); + Object o = emptyList.get(-2); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetout(){ + + Object o = testlist.get(31); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testremoveExp(){ + + Object o = testlist.remove(31); + } + + @Override + @Test + public void testFunctional() { + Iterator itr = testlist.iterator(); + + assertTrue(itr.hasNext()); + for(int i = 0; i < 12; i++){ + assertEquals(i, itr.next()); + } + + //previous() function not yet defined in interface + + itr.remove(); + + assertTrue(itr.hasNext()); + assertEquals(12, itr.next()); + assertEquals(19, testlist.size()); + + for(int i = 13; i < 20; i++){ + assertEquals(i, itr.next()); + } + assertFalse(itr.hasNext()); + + boolean hasExp = false; + try{ + itr.next(); + }catch (NoSuchElementException e){ + hasExp = true; + } + assertTrue(hasExp); + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java new file mode 100644 index 0000000000..768bb8145e --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java @@ -0,0 +1,30 @@ +package com.github.congcongcong250.coding2017.basicTest; + + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunner { + + public static void main(String[] args){ + ArrayListTest ALT = new ArrayListTest(); + test(ALT); + + LinkedListTest LLT = new LinkedListTest(); + test(LLT); + + + } + + private static Result test(testCase tc){ + + Result result = JUnitCore.runClasses(tc.getClass()); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + + return result; + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java new file mode 100644 index 0000000000..17df6ee30e --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java @@ -0,0 +1,21 @@ +package com.github.congcongcong250.coding2017.basicTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public interface testCase { + @Before + public void setUp(); + + @After + public void tearDown(); + + @Test + public void testAdd(); + + @Test + public void testRemove(); + + @Test + public void testFunctional(); +} From 91971831070eca7d59abd1b28827f8f948ef2a84 Mon Sep 17 00:00:00 2001 From: lirenxn Date: Mon, 27 Feb 2017 02:21:38 +1100 Subject: [PATCH 31/37] Stack and Queue test pass --- .../coding2017/basic/Queue.java | 4 + .../coding2017/basic/Stack.java | 4 + .../coding2017/basicTest/QueueTest.java | 76 +++++++++++++++++++ .../coding2017/basicTest/StackTest.java | 75 ++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java index 2cdd329b0b..0381c65fd8 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java @@ -27,4 +27,8 @@ public boolean isEmpty(){ public int size(){ return elementData.size(); } + + public void clear(){ + elementData.clear(); + } } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java index e3024202da..3e411cb0be 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java @@ -25,4 +25,8 @@ public boolean isEmpty(){ public int size(){ return elementData.size(); } + + public void clear(){ + elementData.clear(); + } } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java new file mode 100644 index 0000000000..a176a44f2c --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java @@ -0,0 +1,76 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.Queue; + +public class QueueTest implements testCase { + + Queue testqueue = new Queue(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 20; i++){ + testqueue.enQueue(i); + } + } + + @Override + @After + public void tearDown() { + testqueue.clear(); + } + + + @Override + @Test + public void testAdd() { + assertEquals(20,testqueue.size()); + assertEquals(0,testqueue.peek()); + assertEquals(20,testqueue.size()); + assertFalse(testqueue.isEmpty()); + } + + @Override + @Test + public void testRemove() { + assertEquals(20,testqueue.size()); + assertEquals(0,testqueue.deQueue()); + assertEquals(19,testqueue.size()); + assertEquals(1,testqueue.peek()); + assertFalse(testqueue.isEmpty()); + } + + @Override + @Test + public void testFunctional() { + for(int i = 0; i < 20; i++){ + testqueue.deQueue(); + } + assertTrue(testqueue.isEmpty()); + testqueue.enQueue(100); + testqueue.enQueue(200); + assertEquals(100,testqueue.deQueue()); + testqueue.enQueue(400); + assertEquals(200,testqueue.deQueue()); + assertFalse(testqueue.isEmpty()); + assertEquals(400,testqueue.deQueue()); + + boolean hasExp = false; + try{ + testqueue.deQueue(); + }catch (IndexOutOfBoundsException e){ + hasExp = true; + } + assertTrue(hasExp); + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java new file mode 100644 index 0000000000..9d6085ab03 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java @@ -0,0 +1,75 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.Stack; + +public class StackTest implements testCase { + + Stack teststack = new Stack(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 20; i++){ + teststack.push(i); + } + } + + @Override + @After + public void tearDown() { + teststack.clear(); + } + + + @Override + @Test + public void testAdd() { + assertEquals(20,teststack.size()); + assertEquals(19,teststack.peek()); + assertEquals(20,teststack.size()); + assertFalse(teststack.isEmpty()); + } + + @Override + @Test + public void testRemove() { + assertEquals(20,teststack.size()); + assertEquals(19,teststack.pop()); + assertEquals(19,teststack.size()); + assertEquals(18,teststack.peek()); + assertFalse(teststack.isEmpty()); + } + + @Override + @Test + public void testFunctional() { + for(int i = 0; i < 20; i++){ + teststack.pop(); + } + assertTrue(teststack.isEmpty()); + teststack.push(100); + teststack.push(200); + assertEquals(200,teststack.pop()); + teststack.push(400); + assertEquals(400,teststack.pop()); + assertFalse(teststack.isEmpty()); + assertEquals(100,teststack.pop()); + + + boolean hasExp = false; + try{ + teststack.pop(); + }catch (IndexOutOfBoundsException e){ + hasExp = true; + } + assertTrue(hasExp); + } + +} From b9b061cb2a5212c46ec01807a68297f451e7af67 Mon Sep 17 00:00:00 2001 From: lirenxn Date: Mon, 27 Feb 2017 02:38:24 +1100 Subject: [PATCH 32/37] Simple BinaryTreeNode test pass --- .../coding2017/basic/BinaryTreeNode.java | 6 ++ .../basicTest/BinaryTreeNodeTest.java | 69 +++++++++++++++++++ .../coding2017/basicTest/TestRunner.java | 10 ++- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java index fb71311dc4..9e0e5aa10e 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -37,6 +37,12 @@ public void setRight(BinaryTreeNode right) { this.right = right; } + public void destroy(){ + this.data = null; + this.left = null; + this.right = null; + } + public BinaryTreeNode insert(Object o){ //If is empty root if(data == null){ diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..f0e7e59ee3 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java @@ -0,0 +1,69 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest implements testCase { + + BinaryTreeNode node = new BinaryTreeNode(); + + @Override + @Before + public void setUp() { + node.insert(10); + + } + + @Override + @After + public void tearDown() { + node.destroy(); + } + + @Override + @Test + public void testAdd() { + assertEquals(10,node.getData()); + node.insert(5); + assertEquals(5,node.getLeft().getData()); + node.insert(1); + node.insert(2); + node.insert(6); + node.insert(19); + node.insert(18); + /* + * 10 + * 5 19 + * 1 6 18 + * 2 + * + * */ + assertEquals(1,node.getLeft().getLeft().getData()); + assertEquals(2,node.getLeft().getLeft().getRight().getData()); + assertEquals(6,node.getLeft().getRight().getData()); + assertEquals(19,node.getRight().getData()); + assertEquals(18,node.getRight().getLeft().getData()); + } + + @Override + @Test + public void testRemove() { + // TODO Auto-generated method stub + + } + + @Override + @Test + public void testFunctional() { + // TODO Auto-generated method stub + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java index 768bb8145e..c9a623e402 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java @@ -14,6 +14,14 @@ public static void main(String[] args){ LinkedListTest LLT = new LinkedListTest(); test(LLT); + StackTest STT = new StackTest(); + test(STT); + + QueueTest QT = new QueueTest(); + test(QT); + + BinaryTreeNodeTest BTNT = new BinaryTreeNodeTest(); + test(BTNT); } @@ -23,7 +31,7 @@ private static Result test(testCase tc){ for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } - System.out.println(result.wasSuccessful()); + System.out.println(tc.getClass().toString()+ "\n>>> Test status: "+result.wasSuccessful()); return result; } From ed776abfd0a5cc566864d68dcc13eab42101fcdb Mon Sep 17 00:00:00 2001 From: Ven13 <106614649@qq.com> Date: Sun, 26 Feb 2017 19:03:30 +0800 Subject: [PATCH 33/37] LinkedList add a throw --- .../src/com/github/Ven13/coding2017/basic/LinkedList.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java index 39edb53ef1..adf30d89c2 100644 --- a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java @@ -85,6 +85,9 @@ public Object get(int index) { @Override public Object remove(int index) { + if(size == 0) { + throw new java.util.NoSuchElementException(); + } if(index == 0) { Node node = head; Node temp = node.next; From f530c658e53b3b5d5af559a11208afbc38c66bc0 Mon Sep 17 00:00:00 2001 From: orajavac Date: Mon, 27 Feb 2017 09:53:27 +0800 Subject: [PATCH 34/37] 20170227_09:52 --- .../orajavac/coding2017/basic/ArrayList.java | 100 ++++++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++++ .../orajavac/coding2017/basic/Iterator.java | 7 ++ .../orajavac/coding2017/basic/LinkedList.java | 110 ++++++++++++++++++ .../orajavac/coding2017/basic/List.java | 9 ++ .../orajavac/coding2017/basic/Queue.java | 34 ++++++ .../orajavac/coding2017/basic/Stack.java | 49 ++++++++ 7 files changed, 341 insertions(+) create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/Iterator.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/LinkedList.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/List.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cc5bbc8adc --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.github.orajavac.coding2017.basic; + +public class ArrayList implements List,Iterator{ + + private int size = 0; + + private int i=0; + + private Object[] elementData = new Object[2]; + + public void add(Object o){ + size=size(); + if (size == elementData.length) + grow(elementData,size); + elementData[size]=o; + } + public void add(int index, Object o){ + Object old=elementData[index]; //[3] old value=c + size=size(); + size+=1; + if (size>elementData.length) + grow(elementData,size); + Object temp = null; + int len=elementData.length; + for (int i=0;iindex){ + temp=elementData[i]; + elementData[i]=old; //[4]=c + old=temp; + } + } + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (index==elementData.length-1){ //ɾ�����һ������Ԫ�����ֵ + elementData[index]=null; + }else{ + elementData[index]=null; + int len=elementData.length; + for (int i=0;iindex){ + if(i+1!=len){ + elementData[i]=elementData[i+1]; + }else{ //���Ǽ����������� 0-3����ô���鳤����4��3+1==4��elementData[i+1]�ᱨ�� + elementData[i]=null; + } + } + } + } + return null; + } + + public int size(){ + size=0; + for (int i=0;i "); + c=c.next; + } + System.out.println(); + } + public Iterator iterator(){ + LinkedList l = new LinkedList(); + l.head=this.head; + return l; + } + + public boolean hasNext(){ + current = head; + if (current!=null){ + head = current.next; + return true; + } + return false; + } + + public Object next(){ + return current.data; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java new file mode 100644 index 0000000000..129333b4da --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.orajavac.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/562768642/src/com/github/orajavac/coding2017/basic/Queue.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java new file mode 100644 index 0000000000..772806e391 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java @@ -0,0 +1,34 @@ +package com.github.orajavac.coding2017.basic; + +public class Queue { + +private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + Object obj = elementData.get(0); + elementData.remove(0); + return obj; + } + + public ArrayList getElementData() { + return elementData; + } + + public void setElementData(ArrayList elementData) { + this.elementData = elementData; + } + + public boolean isEmpty(){ + if (elementData.size()>0) + return true; + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java new file mode 100644 index 0000000000..62d9f2e278 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java @@ -0,0 +1,49 @@ +package com.github.orajavac.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int s=elementData.size(); + Object obj = null; + for (int i=s-1;i>=0;i--){ + if(elementData.get(i)!=null){ + obj = elementData.get(i); + elementData.remove(i); + break; + } + } + return obj; + } + + public Object peek(){ + int s=elementData.size(); + Object obj = null; + for (int i=s-1;i>=0;i--){ + if(elementData.get(i)!=null){ + obj = elementData.get(i); + break; + } + } + return obj; + } + public boolean isEmpty(){ + if (elementData.size()>0) + return true; + return false; + } + public int size(){ + return elementData.size(); + } + public ArrayList getElementData() { + return elementData; + } + + public void setElementData(ArrayList elementData) { + this.elementData = elementData; + } +} From 6799d3d7749065d184a28c6434f3d4a6dffa4e52 Mon Sep 17 00:00:00 2001 From: orajavac Date: Mon, 27 Feb 2017 09:57:47 +0800 Subject: [PATCH 35/37] 123 --- .../src/com/github/orajavac/coding2017/basic/ArrayList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java index cc5bbc8adc..167a28045d 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java @@ -8,6 +8,7 @@ public class ArrayList implements List,Iterator{ private Object[] elementData = new Object[2]; + //S public void add(Object o){ size=size(); if (size == elementData.length) From d54fe1ea448dc43f593987b3daa97ac1783e0306 Mon Sep 17 00:00:00 2001 From: orajavac Date: Mon, 27 Feb 2017 10:07:09 +0800 Subject: [PATCH 36/37] 999 --- .../src/com/github/orajavac/coding2017/basic/ArrayList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java index 167a28045d..73ac68069d 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/ArrayList.java @@ -8,7 +8,7 @@ public class ArrayList implements List,Iterator{ private Object[] elementData = new Object[2]; - //S + //SS public void add(Object o){ size=size(); if (size == elementData.length) From 4cd858a5692da1fe7cce815ca2f5b680b5694967 Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Mon, 27 Feb 2017 14:04:54 +0800 Subject: [PATCH 37/37] =?UTF-8?q?Revert=20"=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/MyArrayList.java | 83 ------------ .../coding2017/basic/MyLinkedList.java | 119 ----------------- .../lhpmatlab/coding2017/basic/MyQueue.java | 29 ----- .../lhpmatlab/coding2017/basic/MyStack.java | 30 ----- .../coding2017/basic/MyArrayListTest.java | 113 ---------------- .../coding2017/basic/MyLinkedListTest.java | 121 ------------------ .../coding2017/basic/MyQueueTest.java | 71 ---------- .../coding2017/basic/MyStackTest.java | 104 --------------- 8 files changed, 670 deletions(-) delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java delete mode 100644 group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyLinkedListTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyQueueTest.java delete mode 100644 group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java deleted file mode 100644 index 03f6710788..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/18. - */ -public class MyArrayList { - - private Object[] initialArray = {}; - private Object[] dataArray; - private int initSize = 10; - private int arraySize; - public MyArrayList() { - dataArray = initialArray; - } - - public MyArrayList(int init) { - dataArray = new Object[init]; - } - - public void ensureCapacity(int newCapacity) { - if (newCapacity < arraySize) - return; - - Object[] old = dataArray; - dataArray = new Object[newCapacity]; - for (int i = 0; i < size(); i++) { - dataArray[i] = old[i]; - } - } - - public void add(T element) { - add(size(), element); - } - - public void add(int index, T element) { - if (size() == dataArray.length) { - ensureCapacity(size()*2 + 1); - } - for(int i=arraySize;i>index;i--) { - dataArray[i] = dataArray[i - 1]; - } - dataArray[index] = element; - arraySize++; - } - - public T delete(int index) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - T removeElement = (T)dataArray[index]; - for (int i = index; i < size() -1; i++) { - dataArray[i] = dataArray[i + 1]; - } - arraySize--; - return removeElement; - } - - public T get(int index) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - return (T)dataArray[index]; - } - - public T set(int index, T newElement) { - if (index < 0 || index > arraySize) { - throw new ArrayIndexOutOfBoundsException(); - } - T oldElement = (T) dataArray[index]; - dataArray[index] = newElement; - - return oldElement; - } - - public int size() { - return arraySize; - } - - public boolean isEmpty() { - return size() == 0; - } - -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java deleted file mode 100644 index 9e7eab3401..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/18. - */ -public class MyLinkedList { - private class Node { - public Node pre; - public Node next; - public T data; - - public Node(Node pre,Node next,T data) { - this.pre = pre; - this.next = next; - this.data = data; - } - } - - private int dataSize; - - private Node head; - private Node tail; - - public MyLinkedList() { - head = new Node(null,null,null); - tail = new Node(head, null, null); - head.next = tail; - dataSize = 0; - } - - public void add(T t) { -// add(size(), t); - Node newNode = new Node<>(null, tail, t); - newNode.pre = tail.pre; - tail.pre.next = newNode; - tail.pre = newNode; - dataSize++; - - } - - /** - * 根据索引添加没有实现 - * @param index - * @param element - */ - public void add(int index,T element) { - //TODO 根据索引添加元素 -// addBefore(getNode(index,0,size()-1),element); -// if (index == dataSize) { -// add(element); -// } else { - // -// } - } - - public T get(int index) { - return getNode(index).data; - } - - public T set(int index, T newValue) { - Node node = getNode(index); - T oldData = node.data; - node.data = newValue; - return oldData; - } - - public T remove(int index) { - Node node = getNode(index); - node.next.pre = node.pre; - node.pre.next = node.next; - dataSize--; - - return node.data; - - } - - private void addBefore(Node node, T element) { -// newNode.pre.next = newNode; -// node.pre = newNode; - Node pre = node.pre; - Node newNode = new Node<>(node.pre, node, element); - node.pre = newNode; - pre.next = newNode; - - dataSize++; - } - - private Node getNode(int index) { - return getNode(index, 0, size()); - } - - private Node getNode(int index, int lower, int upper) { - Node p; - if (index < lower || index > upper) { - throw new IndexOutOfBoundsException(); - } - - if (index < size() / 2) { - p = head.next; - for (int i = 0; i < index; i++) { - p = p.next; - } - } else { - p = tail.pre; - for (int i = size()-1; i > index; i--) { - p = p.pre; - } - } - return p; - } - - public int size() { - return dataSize; - } - - public boolean isEmpty() { - return size() == 0; - } -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java deleted file mode 100644 index 6d0c970b65..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/22. - */ -public class MyQueue { - private MyLinkedList link = new MyLinkedList<>(); - - public void enQueue(T t) { - link.add(t); - } - - public T deQueue() { - if (size() <= 0) { - return null; - } - T t = link.get(0); - link.remove(0); - return t; - } - - public boolean isEmpty() { - return size() == 0; - } - - public int size() { - return link.size(); - } -} diff --git a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java deleted file mode 100644 index 3fd9d2f5c2..0000000000 --- a/group02/435994736Learning/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -/** - * Created by andy on 2017/2/22. - */ -public class MyStack { - private MyArrayList list = new MyArrayList<>(); - - public void push(T t) { - list.add(t); - } - - public T pop() { - if (size() <= 0) { - throw new IndexOutOfBoundsException(); - } - return list.delete(size() - 1); - } - - public T peek() { - return list.get(size() - 1); - } - - public boolean isEmpty() { - return list.size() == 0; - } - public int size() { - return list.size(); - } -} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java deleted file mode 100644 index e29d41feac..0000000000 --- a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; - -public class MyArrayListTest { - private MyArrayList list; - - @Before - public void init(){ - list = new MyArrayList<>(); - } - - @Test - public void testEnsureCapacity() { - assertEquals("init list size is 0 ", list.size(), 0); - list.add("1"); - list.ensureCapacity(10); - assertEquals("ensureCapacity size is 10 ", list.size(),1); - } - - /** - * 在列表的末尾添加元素 - */ - @Test - public void testAddT() { - assertEquals("init list size is 0 ", list.size(), 0); - list.add("1"); - list.add("2"); - assertEquals("add list size ", list.size(), 2); - for (int i=0; i -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyLinkedListTest { - private MyLinkedList linkedList; - - @Before - public void before() throws Exception { - linkedList = new MyLinkedList<>(); - } - - @After - public void after() throws Exception { - } - - /** - * - * Method: add(T t) - * - */ - @Test - public void testAddT() throws Exception { - assertEquals("init list size is 0 ", linkedList.size(), 0); - linkedList.add("1"); - linkedList.add("2"); - assertEquals("add list size ", linkedList.size(), 2); - for (int i=0; i -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyQueueTest { - private MyQueue queue; - - @Before - public void init() throws Exception { - queue = new MyQueue<>(); - } - - /** - * - * Method: enQueue(T t) - * - */ - @Test - public void testEnQueue() throws Exception { - queue.enQueue("1"); - assertEquals("size ", queue.size(), 1); - } - - /** - * - * Method: deQueue() - * - */ - @Test - public void testDeQueue() throws Exception { - queue.enQueue("1"); - queue.enQueue("2"); -// queue.deQueue(); - assertEquals("dequeue element ",queue.deQueue(),"1"); - assertEquals("size ", queue.size(), 1); - - } - - /** - * - * Method: isEmpty() - * - */ - @Test - public void testIsEmpty() throws Exception { - assertEquals("isEmpty method",queue.isEmpty(),true); - } - - /** - * - * Method: size() - * - */ - @Test - public void testSize() throws Exception { - queue.enQueue("1"); - queue.enQueue("2"); - assertEquals("size method", queue.size(),2); - } - - -} diff --git a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java deleted file mode 100644 index a90af5d720..0000000000 --- a/group02/435994736Learning/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.github.lhpmatlab.coding2017.basic; - -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import static org.junit.Assert.*; - -/** -* MyStack Tester. -* -* @author -* @since
���� 26, 2017
-* @version 1.0 -*/ -public class MyStackTest { - - MyStack stack; - - - @Before - public void init() throws Exception { - stack = new MyStack<>(); - } - - @After - public void after() throws Exception { - } - - /** - * - * Method: push(T t) - * - */ - @Test - public void testPush() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - assertEquals("pust stack ", stack.size(),1); - } - - /** - * - * Method: pop() - * - */ - @Test - public void testPop() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - stack.pop(); - assertEquals("after pop ",stack.size(),1); - } - - /** - * - * Method: peek() - * - */ - @Test - public void testPeek() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - assertEquals("peek ", stack.peek(),"2"); - } - - /** - *测试判空方法 - * Method: isEmpty() - * - */ - @Test - public void testIsEmpty() throws Exception { - assertEquals("stack is empty ", stack.isEmpty(), true); - } - - /** - *测试判空方法,不为空的情况 - * Method: isEmpty() - * - */ - @Test - public void testIsNotEmpty() throws Exception { - stack.push("1"); - assertEquals("stack is empty ", stack.isEmpty(), false); - } - - /** - * - * Method: size() - * - */ - @Test - public void testSize() throws Exception { - assertEquals("init stack ", stack.size(), 0); - stack.push("1"); - stack.push("2"); - assertEquals("size is 2", stack.size(), 2); - } - - -}