From 094f0bee1c07da3cbfde82cf1741b2a45f70d760 Mon Sep 17 00:00:00 2001 From: GallenZhang <1298552064@qq.com> Date: Fri, 24 Feb 2017 22:59:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?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 --- .../1298552064/src/week01/basic/Iterator.java | 8 + group01/1298552064/src/week01/basic/List.java | 13 ++ .../src/week01/basic/MyArrayList.java | 131 +++++++++++ .../src/week01/basic/MyBinaryTreeNode.java | 70 ++++++ .../src/week01/basic/MyLinkedList.java | 215 ++++++++++++++++++ .../1298552064/src/week01/basic/MyQueue.java | 22 ++ .../1298552064/src/week01/basic/MyStack.java | 25 ++ .../src/week01/test/MyArrayListTest.java | 53 +++++ .../src/week01/test/MyLinkedListTest.java | 78 +++++++ .../src/week01/test/MyQueueTest.java | 48 ++++ .../src/week01/test/MyStackTest.java | 53 +++++ 11 files changed, 716 insertions(+) create mode 100644 group01/1298552064/src/week01/basic/Iterator.java create mode 100644 group01/1298552064/src/week01/basic/List.java create mode 100644 group01/1298552064/src/week01/basic/MyArrayList.java create mode 100644 group01/1298552064/src/week01/basic/MyBinaryTreeNode.java create mode 100644 group01/1298552064/src/week01/basic/MyLinkedList.java create mode 100644 group01/1298552064/src/week01/basic/MyQueue.java create mode 100644 group01/1298552064/src/week01/basic/MyStack.java create mode 100644 group01/1298552064/src/week01/test/MyArrayListTest.java create mode 100644 group01/1298552064/src/week01/test/MyLinkedListTest.java create mode 100644 group01/1298552064/src/week01/test/MyQueueTest.java create mode 100644 group01/1298552064/src/week01/test/MyStackTest.java diff --git a/group01/1298552064/src/week01/basic/Iterator.java b/group01/1298552064/src/week01/basic/Iterator.java new file mode 100644 index 0000000000..e209875b54 --- /dev/null +++ b/group01/1298552064/src/week01/basic/Iterator.java @@ -0,0 +1,8 @@ +package week01.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group01/1298552064/src/week01/basic/List.java b/group01/1298552064/src/week01/basic/List.java new file mode 100644 index 0000000000..608c1b532b --- /dev/null +++ b/group01/1298552064/src/week01/basic/List.java @@ -0,0 +1,13 @@ +package week01.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/group01/1298552064/src/week01/basic/MyArrayList.java b/group01/1298552064/src/week01/basic/MyArrayList.java new file mode 100644 index 0000000000..c4f6572f1c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyArrayList.java @@ -0,0 +1,131 @@ +package week01.basic; + +import java.util.Arrays; + +public class MyArrayList 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) { + checkPositionIndex(index); + ensureCapacity(size + 1); + + if (index >= size) { + elementData[size++] = o; + } else { + System.arraycopy(elementData, index, elementData, index + 1, size + - index); + + elementData[index] = o; + + size++; + } + } + + public Object get(int index) { + checkElementIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkElementIndex(index); + Object removeElement = elementData[index]; + if (index == (size - 1)) { + elementData[index] = null; + size--; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + elementData[size - 1] = null; + size--; + } + return removeElement; + } + + public int size() { + return size; + } + + /** + * 保证数组空间充足 + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + int capacity = elementData.length; + if (minCapacity > capacity) { + capacity += capacity / 2; + grow(capacity); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private void grow(int newCapacity) { + elementData = Arrays.copyOf(elementData, newCapacity); + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private MyArrayList list; + private int position = 0; + + private ArrayListIterator(MyArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + @Override + public String toString() { + String elementStr = ""; + for (int i = 0; i < size; i++) { + elementStr += elementData[i] + ","; + } + return "MyArrayList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } +} diff --git a/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java new file mode 100644 index 0000000000..30e6c810a5 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java @@ -0,0 +1,70 @@ +package week01.basic; + +public class MyBinaryTreeNode { + + private Object data; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public MyBinaryTreeNode getLeft() { + return left; + } + + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + + public MyBinaryTreeNode getRight() { + return right; + } + + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Object o) { + if(this.getData() == null && this.getLeft() == null && this.getRight() == null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + } + + MyBinaryTreeNode node = new MyBinaryTreeNode(); + MyBinaryTreeNode currentNode = this; + while(true){ + if((Integer) o < (Integer) getData()){ + if(currentNode.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setLeft(node); + return this; + }else{ + currentNode = currentNode.getLeft(); + } + + }else{ + if(currentNode.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setRight(node); + return this; + }else{ + currentNode = currentNode.getRight(); + } + } + } + } +} diff --git a/group01/1298552064/src/week01/basic/MyLinkedList.java b/group01/1298552064/src/week01/basic/MyLinkedList.java new file mode 100644 index 0000000000..4894c5ff6c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyLinkedList.java @@ -0,0 +1,215 @@ +package week01.basic; + +public class MyLinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + // 空链表 + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = head; + while (p.next != null) { + p = p.next; + } + + Node target = new Node(); + target.data = o; + target.next = null; + p.next = target; + } + size++; + } + + public void add(int index, Object o) { + // index 是否合法 + checkPositionIndex(index); + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node p = new Node(); + Node p1 = head; + for (int i = 0; i < index - 1; i++) { + p1 = p1.next; + } + p.data = o; + p.next = p1.next; + p1.next = p; + + size++; + } + } + } + + public Object get(int index) { + checkElementIndex(index); + Node p = head; + for (int i = 0; i < index; i++) { + p = p.next; + } + return p.data; + } + + public Object remove(int index) { + checkElementRemove(); + checkElementIndex(index); + + Object removeObject = null; + if (index == 0) { + removeObject = removeFirst(); + } else if (index == (size - 1)) { + removeObject = removeLast(); + } else { + Node p = head; + for (int i = 1; i < index; i++) { + p = p.next; + } + removeObject = p.next.data; + p.next = p.next.next; + size--; + } + return removeObject; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = new Node(); + p.data = o; + p.next = head; + head = p; + } + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + checkElementRemove(); + Object removeObject = head.data; + head = head.next; + size--; + return removeObject; + } + + public Object removeLast() { + checkElementRemove(); + + Object removeObject = null; + + if (size == 1) { + removeObject = head.data; + head = head.next; + } else { + Node p = head; + for (int i = 0; i < size; i++) { + if (p.next.next == null) { + removeObject = p.next; + p.next = null; + break; + } else { + p = p.next; + } + } + } + size--; + return removeObject; + } + + private boolean isEmpty() { + return size == 0; + } + + private void checkElementRemove() { + if (isEmpty()) { + throw new NullPointerException("The list is empty."); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new MyLinkedListIterator(this); + } + + private class MyLinkedListIterator implements Iterator { + private MyLinkedList list = null; + private int position = 0; + + private MyLinkedListIterator(MyLinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + private static class Node { + Object data; + Node next; + } + + @Override + public String toString() { + String elementStr = ""; + Node p = head; + while (p != null) { + elementStr += p.data + ","; + p = p.next; + } + + return "MyLinkedList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } + +} diff --git a/group01/1298552064/src/week01/basic/MyQueue.java b/group01/1298552064/src/week01/basic/MyQueue.java new file mode 100644 index 0000000000..54008652ff --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyQueue.java @@ -0,0 +1,22 @@ +package week01.basic; + +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/basic/MyStack.java b/group01/1298552064/src/week01/basic/MyStack.java new file mode 100644 index 0000000000..aea4d94e24 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyStack.java @@ -0,0 +1,25 @@ +package week01.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(size() - 1); + } + + public Object peek() { + return elementData.get(size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/test/MyArrayListTest.java b/group01/1298552064/src/week01/test/MyArrayListTest.java new file mode 100644 index 0000000000..219035b46f --- /dev/null +++ b/group01/1298552064/src/week01/test/MyArrayListTest.java @@ -0,0 +1,53 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyArrayList; + + + +public class MyArrayListTest { + + private MyArrayList list = null; + + @Before + public void setUp() throws Exception { + list = new MyArrayList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAdd(){ + list.add(4, 10); + Assert.assertEquals("MyArrayList: { size=6, elementData=[1,2,3,4,10,5] }", list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(3), list.get(2)); + } + + @Test + public void testRemove(){ + list.remove(2); + Assert.assertEquals("MyArrayList: { size=4, elementData=[1,2,4,5] }", list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyLinkedListTest.java b/group01/1298552064/src/week01/test/MyLinkedListTest.java new file mode 100644 index 0000000000..b5d6c048d6 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyLinkedListTest.java @@ -0,0 +1,78 @@ +package week01.test; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyLinkedList; + +public class MyLinkedListTest { + + private MyLinkedList list = null; + + @Before + public void setUp() throws Exception { + list = new MyLinkedList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + + @Test + public void testAdd(){ + list.add(3,10); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,10,4,5] }",list.toString()); + } + + @Test + public void testAddFirst(){ + list.addFirst(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[100,1,2,3,4,5] }",list.toString()); + } + + @Test + public void testAddLast(){ + list.addLast(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,4,5,100] }",list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(5), list.get(4)); + } + + @Test + public void testRemove(){ + list.remove(3); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,5] }",list.toString()); + } + + @Test + public void testRemoveFirst(){ + list.removeFirst(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[2,3,4,5] }",list.toString()); + } + + @Test + public void testRemoveLast(){ + list.removeLast(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,4] }",list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } + +} diff --git a/group01/1298552064/src/week01/test/MyQueueTest.java b/group01/1298552064/src/week01/test/MyQueueTest.java new file mode 100644 index 0000000000..f9b7cb63f2 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyQueueTest.java @@ -0,0 +1,48 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyQueue; + +public class MyQueueTest { + + private MyQueue queue = null; + + @Before + public void setUp() throws Exception { + queue = new MyQueue(); + + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void testEnQueue(){ + queue.enQueue(4); + Assert.assertEquals((Object)new Integer(4), queue.size()); + } + + @Test + public void testDeQueue(){ + Assert.assertEquals((Object) new Integer(1), queue.deQueue()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(queue.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(3), queue.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyStackTest.java b/group01/1298552064/src/week01/test/MyStackTest.java new file mode 100644 index 0000000000..4efbc2b204 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyStackTest.java @@ -0,0 +1,53 @@ +package week01.test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyStack; + +public class MyStackTest { + private MyStack stack = null; + + @Before + public void setUp() throws Exception { + stack = new MyStack(); + + stack.push(1); + stack.push(2); + stack.push(3); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void tearPush() throws Exception { + stack.push(10); + Assert.assertEquals((Object) new Integer(4), stack.size()); + Assert.assertEquals((Object) new Integer(10), stack.peek()); + } + + @Test + public void testPop(){ + Assert.assertEquals((Object) new Integer(3), stack.pop()); + Assert.assertEquals((Object) new Integer(2), stack.size()); + } + + @Test + public void testPeek(){ + Assert.assertEquals((Object) new Integer(3), stack.peek()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(stack.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object) new Integer(3), stack.size()); + } +}