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/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); + } + + +} 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 index 806b57f381..0cfebaa908 100644 --- a/group02/727171008/.gitignore +++ b/group02/727171008/.gitignore @@ -1,21 +1,4 @@ /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/ 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/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java index 2d962d9083..b93790894f 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -1,7 +1,7 @@ /* * Created by Harry 2017-2-23 10:50:39 * 实现二叉树,并按二叉查找树插入节点 - * 能实现基本的功能,但测试时存在问题,传进去的数据为空 2017-2-2523:49:43 + * */ package com.github.HarryHook.coding2017.basic; 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()); + } + +} 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; - } - } - } - }