From f21458d3f419f02ec5bb3e17dd2beb0b81ce1fab Mon Sep 17 00:00:00 2001 From: zheng <765324639@qq.com> Date: Thu, 23 Feb 2017 13:38:17 +0800 Subject: [PATCH 001/419] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=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 --- .../src/zavier/week01/basic/ArrayList.java | 85 ++++++++++ .../zavier/week01/basic/BinaryTreeNode.java | 63 ++++++++ .../src/zavier/week01/basic/Iterator.java | 8 + .../src/zavier/week01/basic/LinkedList.java | 148 ++++++++++++++++++ .../src/zavier/week01/basic/List.java | 13 ++ .../src/zavier/week01/basic/Queue.java | 25 +++ .../src/zavier/week01/basic/Stack.java | 33 ++++ .../src/zavier/week01/test/AllTests.java | 12 ++ .../src/zavier/week01/test/ArrayListTest.java | 91 +++++++++++ .../week01/test/BinaryTreeNodeTest.java | 33 ++++ .../zavier/week01/test/LinkedListTest.java | 109 +++++++++++++ .../src/zavier/week01/test/QueueTest.java | 49 ++++++ .../src/zavier/week01/test/StackTest.java | 60 +++++++ 13 files changed, 729 insertions(+) create mode 100644 group01/765324639/src/zavier/week01/basic/ArrayList.java create mode 100644 group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java create mode 100644 group01/765324639/src/zavier/week01/basic/Iterator.java create mode 100644 group01/765324639/src/zavier/week01/basic/LinkedList.java create mode 100644 group01/765324639/src/zavier/week01/basic/List.java create mode 100644 group01/765324639/src/zavier/week01/basic/Queue.java create mode 100644 group01/765324639/src/zavier/week01/basic/Stack.java create mode 100644 group01/765324639/src/zavier/week01/test/AllTests.java create mode 100644 group01/765324639/src/zavier/week01/test/ArrayListTest.java create mode 100644 group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java create mode 100644 group01/765324639/src/zavier/week01/test/LinkedListTest.java create mode 100644 group01/765324639/src/zavier/week01/test/QueueTest.java create mode 100644 group01/765324639/src/zavier/week01/test/StackTest.java diff --git a/group01/765324639/src/zavier/week01/basic/ArrayList.java b/group01/765324639/src/zavier/week01/basic/ArrayList.java new file mode 100644 index 0000000000..38e5739fb8 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/ArrayList.java @@ -0,0 +1,85 @@ +package zavier.week01.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + private void ensureCapacity(int size) { + if (size > elementData.length) { + grow(); + } + } + + private void grow() { + elementData = Arrays.copyOf(elementData, size * 2); + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object remove(int index) { + rangeCheck(index); + Object dest = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return dest; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + + private int index = 0; + + @Override + public Object next() { + return elementData[index++]; + } + + @Override + public boolean hasNext() { + return index < size; + } + }; + } + +} diff --git a/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java b/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6ef26e8f9a --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java @@ -0,0 +1,63 @@ +package zavier.week01.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data) { + this.data = data; + } + + 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 (o > this.data) { + + if (this.getRight() == null) { + BinaryTreeNode node = new BinaryTreeNode(o); + this.setRight(node); + return node; + } else { + return this.getRight().insert(o); + } + + } else { + + if (this.getLeft() == null) { + BinaryTreeNode node = new BinaryTreeNode(o); + this.setLeft(node); + return node; + } else { + return this.getLeft().insert(o); + } + + } + + } + +} diff --git a/group01/765324639/src/zavier/week01/basic/Iterator.java b/group01/765324639/src/zavier/week01/basic/Iterator.java new file mode 100644 index 0000000000..664983e0fd --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Iterator.java @@ -0,0 +1,8 @@ +package zavier.week01.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group01/765324639/src/zavier/week01/basic/LinkedList.java b/group01/765324639/src/zavier/week01/basic/LinkedList.java new file mode 100644 index 0000000000..c876b48f1b --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/LinkedList.java @@ -0,0 +1,148 @@ +package zavier.week01.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + @Override + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node tail = head; + while (tail.next != null) { + tail = tail.next; + } + Node node = new Node(o); + + tail.next = node; + } + size++; + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + if (index == 0) { + Node node = new Node(o); + node.next = head; + head = node; + } else { + Node preDest = head; + for (int i = 0; i < index - 1; i++) { + preDest = preDest.next; + } + Node node = new Node(o); + node.next = preDest.next; + preDest.next = node; + } + + size++; + } + + private void rangeCheckForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object get(int index) { + rangeCheck(index); + + Node dest = head; + for (int i = 0; i < index; i++) { + dest = dest.next; + } + return dest.data; + } + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object remove(int index) { + rangeCheck(index); + + Node preDest = head; + for (int i = 0; i < index - 1; i++) { + preDest = preDest.next; + } + Node dest = preDest.next; + preDest.next = dest.next; + + size--; + return dest.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + + public void addLast(Object o) { + Node lastNode = head; + while (lastNode.next != null) { + lastNode = lastNode.next; + } + + Node node = new Node(o); + lastNode.next = node; + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node target = head; + head = head.next; + size--; + return target.data; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + + Node preDest = head; + while (preDest.next.next != null) { + preDest = preDest.next; + } + Node dest = preDest.next; + preDest.next = null; + + size--; + return dest.data; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + Node(Object data) { + this.data = data; + next = null; + } + } +} diff --git a/group01/765324639/src/zavier/week01/basic/List.java b/group01/765324639/src/zavier/week01/basic/List.java new file mode 100644 index 0000000000..4f2d49bd73 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/List.java @@ -0,0 +1,13 @@ +package zavier.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/765324639/src/zavier/week01/basic/Queue.java b/group01/765324639/src/zavier/week01/basic/Queue.java new file mode 100644 index 0000000000..5a212d46c1 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Queue.java @@ -0,0 +1,25 @@ +package zavier.week01.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + if (list.size() == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group01/765324639/src/zavier/week01/basic/Stack.java b/group01/765324639/src/zavier/week01/basic/Stack.java new file mode 100644 index 0000000000..ebe4afb19f --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Stack.java @@ -0,0 +1,33 @@ +package zavier.week01.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek() { + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/765324639/src/zavier/week01/test/AllTests.java b/group01/765324639/src/zavier/week01/test/AllTests.java new file mode 100644 index 0000000000..c1755f6803 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/AllTests.java @@ -0,0 +1,12 @@ +package zavier.week01.test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ArrayListTest.class, LinkedListTest.class, QueueTest.class, StackTest.class, + BinaryTreeNodeTest.class}) +public class AllTests { + +} diff --git a/group01/765324639/src/zavier/week01/test/ArrayListTest.java b/group01/765324639/src/zavier/week01/test/ArrayListTest.java new file mode 100644 index 0000000000..6a475500df --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/ArrayListTest.java @@ -0,0 +1,91 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.ArrayList; +import zavier.week01.basic.Iterator; + +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + arrayList.add(i); + } + } + + @Test + public void testAddObject() { + for (int i = 0; i < 500; i++) { + arrayList.add(i); + } + } + + @Test + public void testAddIntObject() { + arrayList.add(100, -100); + Assert.assertEquals(-100, arrayList.get(100)); + Assert.assertEquals(100, arrayList.get(101)); + Assert.assertEquals(501, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIllegalIntObject() { + arrayList.add(1000, 5); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddNegativeIntObject() { + arrayList.add(-1, 5); + } + + @Test + public void testGet() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, ((Integer) arrayList.get(i)).intValue()); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalGet() { + arrayList.get(500); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testNegativeGet() { + arrayList.get(-10); + } + + @Test + public void testRemove() { + Assert.assertEquals(100, arrayList.remove(100)); + Assert.assertEquals(101, arrayList.get(100)); + Assert.assertEquals(499, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalRemove() { + arrayList.remove(500); + } + + @Test + public void testSize() { + Assert.assertEquals(500, arrayList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + int i = 0; + while (iterator.hasNext()) { + Assert.assertEquals(i, iterator.next()); + i++; + } + Assert.assertEquals(500, i); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java b/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..30a096a350 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Test; + +import zavier.week01.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode root = new BinaryTreeNode(5); + + @Test + public void testInsert() { + root.insert(2); + root.insert(7); + root.insert(1); + root.insert(6); + + Assert.assertEquals((Integer) 5, root.getData()); + Assert.assertEquals((Integer) 2, root.getLeft().getData()); + Assert.assertEquals((Integer) 1, root.getLeft().getLeft().getData()); + Assert.assertEquals(null, root.getLeft().getRight()); + Assert.assertEquals((Integer) 7, root.getRight().getData()); + Assert.assertEquals((Integer) 6, root.getRight().getLeft().getData()); + Assert.assertEquals(null, root.getRight().getRight()); + + root.insert(4); + root.insert(8); + Assert.assertEquals((Integer) 4, root.getLeft().getRight().getData()); + Assert.assertEquals((Integer) 8, root.getRight().getRight().getData()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/LinkedListTest.java b/group01/765324639/src/zavier/week01/test/LinkedListTest.java new file mode 100644 index 0000000000..de7436a350 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/LinkedListTest.java @@ -0,0 +1,109 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.LinkedList; + + +public class LinkedListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + linkedList.add(i); + } + } + + @Test + public void testAddObject() { + for (int i = 0; i < 100; i++) { + linkedList.add(i); + } + Assert.assertEquals(600, linkedList.size()); + } + + @Test + public void testAddIntObject() { + linkedList.add(100, -100); + Assert.assertEquals(-100, linkedList.get(100)); + Assert.assertEquals(100, linkedList.get(101)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIllegalIntObject() { + linkedList.add(1000, 10); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddNegativeIntObject() { + linkedList.add(-10, 10); + } + + @Test + public void testGet() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, ((Integer) linkedList.get(i)).intValue()); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalGet() { + linkedList.get(500); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testNegativeGet() { + linkedList.get(-10); + } + + @Test + public void testRemove() { + Assert.assertEquals(100, linkedList.remove(100)); + Assert.assertEquals(101, linkedList.get(100)); + Assert.assertEquals(499, linkedList.size()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, linkedList.size()); + linkedList.add(10); + Assert.assertEquals(501, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(-10); + Assert.assertEquals(-10, linkedList.get(0)); + linkedList.addFirst(-100); + Assert.assertEquals(-100, linkedList.get(0)); + Assert.assertEquals(-10, linkedList.get(1)); + } + + @Test + public void testAddLast() { + linkedList.addLast(-9); + Assert.assertEquals(-9, linkedList.get(linkedList.size() - 1)); + linkedList.addLast(-8); + Assert.assertEquals(-8, linkedList.get(linkedList.size() - 1)); + Assert.assertEquals(-9, linkedList.get(linkedList.size() - 2)); + } + + @Test + public void testRemoveFirst() { + Assert.assertEquals(0, linkedList.removeFirst()); + Assert.assertEquals(1, linkedList.removeFirst()); + Assert.assertEquals(498, linkedList.size()); + } + + @Test + public void testRemoveLast() { + Assert.assertEquals(499, linkedList.removeLast()); + Assert.assertEquals(498, linkedList.removeLast()); + Assert.assertEquals(498, linkedList.size()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/QueueTest.java b/group01/765324639/src/zavier/week01/test/QueueTest.java new file mode 100644 index 0000000000..99d6466c8a --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/QueueTest.java @@ -0,0 +1,49 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.Queue; + +public class QueueTest { + + private Queue queue = new Queue(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + queue.enQueue(i); + } + } + + @Test + public void testEnQueue() { + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + Assert.assertEquals(600, queue.size()); + } + + @Test + public void testDeQueue() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertNull(queue.deQueue()); + Assert.assertNull(queue.deQueue()); + } + + @Test + public void testIsEmpty() { + Assert.assertFalse(queue.isEmpty()); + Queue q = new Queue(); + Assert.assertTrue(q.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, queue.size()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/StackTest.java b/group01/765324639/src/zavier/week01/test/StackTest.java new file mode 100644 index 0000000000..8138f97d3d --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/StackTest.java @@ -0,0 +1,60 @@ +package zavier.week01.test; + +import java.util.EmptyStackException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.Stack; + + +public class StackTest { + + private Stack stack = new Stack(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + stack.push(i); + } + } + + @Test + public void testPush() { + for (int i = 0; i < 100; i++) { + stack.push(i); + } + Assert.assertEquals(600, stack.size()); + } + + @Test(expected = EmptyStackException.class) + public void testPop() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(499 - i, stack.pop()); + } + stack.pop(); + } + + @Test + public void testPeek() { + Assert.assertEquals(499, stack.peek()); + Assert.assertEquals(499, stack.peek()); + stack.pop(); + Assert.assertEquals(498, stack.peek()); + } + + @Test + public void testIsEmpty() { + Assert.assertFalse(stack.isEmpty()); + Assert.assertTrue(new Stack().isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, stack.size()); + stack.pop(); + Assert.assertEquals(499, stack.size()); + } + +} From 7bc4437517ef425f69554b1ce058a04fb31533ce Mon Sep 17 00:00:00 2001 From: Haochen Date: Thu, 23 Feb 2017 13:46:51 +0800 Subject: [PATCH 002/419] init --- .../src/datastructure/basic/ArrayList.java | 85 ++++ .../datastructure/basic/BinaryTreeNode.java | 32 ++ .../src/datastructure/basic/Iterator.java | 7 + .../src/datastructure/basic/LinkedList.java | 102 +++++ .../code/src/datastructure/basic/List.java | 9 + .../code/src/datastructure/basic/Queue.java | 72 ++++ .../code/src/datastructure/basic/Stack.java | 43 ++ .../datastructure/jdklist/MyArrayList.java | 304 ++++++++++++++ .../datastructure/jdklist/MyLinkedList.java | 382 ++++++++++++++++++ .../datastructure/nongeneric/MyBaseList.java | 137 +++++++ .../nongeneric/MyBinaryTree.java | 61 +++ .../src/datastructure/nongeneric/MyQueue.java | 43 ++ .../src/datastructure/nongeneric/MyStack.java | 47 +++ 13 files changed, 1324 insertions(+) create mode 100644 group01/895457260/code/src/datastructure/basic/ArrayList.java create mode 100644 group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java create mode 100644 group01/895457260/code/src/datastructure/basic/Iterator.java create mode 100644 group01/895457260/code/src/datastructure/basic/LinkedList.java create mode 100644 group01/895457260/code/src/datastructure/basic/List.java create mode 100644 group01/895457260/code/src/datastructure/basic/Queue.java create mode 100644 group01/895457260/code/src/datastructure/basic/Stack.java create mode 100644 group01/895457260/code/src/datastructure/jdklist/MyArrayList.java create mode 100644 group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java create mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java create mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java create mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyQueue.java create mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyStack.java diff --git a/group01/895457260/code/src/datastructure/basic/ArrayList.java b/group01/895457260/code/src/datastructure/basic/ArrayList.java new file mode 100644 index 0000000000..ed7df1a702 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/ArrayList.java @@ -0,0 +1,85 @@ +package datastructure.basic; + +public class ArrayList implements List { + + private int size = 0; + + Object[] elementData; + + public ArrayList() { + elementData = new Object[100]; + } + + public ArrayList(int initCapacity) { + elementData = new Object[initCapacity]; + } + + public void add(Object o){ + autoGrow(); + elementData[size()] = o; + size++; + } + + public void add(int index, Object o){ + autoGrow(); + System.arraycopy(elementData, index, elementData, index + 1, size() - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + checkIndex(index); + Object removed = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size() - index); + size--; + return removed; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + int index = -1; + @Override + public boolean hasNext() { + return index + 1 < size(); + } + + @Override + public Object next() { + index++; + return elementData[index]; + } + }; + } + + private void autoGrow() { + if (size >= elementData.length) { + Object[] newArray = new Object[nextCapacity()]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + + private int nextCapacity() { + return elementData.length * 2; + } + + private void checkIndex(int index) { + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); + } + } + + private String indexOutOfBoundMessage(int index) { + return "index: " + index + ", size: " + size(); + } + +} diff --git a/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java b/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d3a9ff377b --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package datastructure.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/group01/895457260/code/src/datastructure/basic/Iterator.java b/group01/895457260/code/src/datastructure/basic/Iterator.java new file mode 100644 index 0000000000..4787b60086 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Iterator.java @@ -0,0 +1,7 @@ +package datastructure.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/895457260/code/src/datastructure/basic/LinkedList.java b/group01/895457260/code/src/datastructure/basic/LinkedList.java new file mode 100644 index 0000000000..189280da30 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/LinkedList.java @@ -0,0 +1,102 @@ +package datastructure.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + head = new Node(); + } + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + Node pre = findNode(index - 1); + Node node = new Node(); + node.data = o; + addNode(node, pre); + } + + public Object get(int index){ + checkIndex(index); + return findNode(index); + } + public Object remove(int index){ + checkIndex(index); + Node pre = findNode(index - 1); + Node removed = pre.next; + removeNode(removed, pre); + return removed.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + addNode(node, head); + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + Node pre = findNode(size()); + addNode(node, pre); + } + public Object removeFirst(){ + Node removed = head.next; + removeNode(head.next, head); + return removed.data; + } + public Object removeLast(){ + return remove(size() - 1); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } + + private Node findNode(int index) { + if (index == -1) { + return head; + } else { + checkIndex(index); + } + Node node = head.next; + for (int i = 0; i < index; ++i) { + node = node.next; + } + return node; + } + + private void checkIndex(int index) { + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); + } + } + + private String indexOutOfBoundMessage(int index) { + return "index: " + index + ", size: " + size(); + } + + private void addNode(Node node, Node pre) { + node.next = pre.next; + pre.next = node; + size++; + } + + private void removeNode(Node node, Node pre) { + pre.next = node.next; + node.next = null; + size--; + } +} diff --git a/group01/895457260/code/src/datastructure/basic/List.java b/group01/895457260/code/src/datastructure/basic/List.java new file mode 100644 index 0000000000..39394c145e --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/List.java @@ -0,0 +1,9 @@ +package datastructure.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/895457260/code/src/datastructure/basic/Queue.java b/group01/895457260/code/src/datastructure/basic/Queue.java new file mode 100644 index 0000000000..ee96a1a83e --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Queue.java @@ -0,0 +1,72 @@ +package datastructure.basic; + +public class Queue { + //数组实现自增长的循环队列 + private Object[] array = new Object[10]; + private int head = 0; + private int rear = 0; + + public void enQueue(Object o){ + int target = mapIndex(rear); + autoGrow(); + array[target] = o; + rear++; + } + + public Object deQueue() { + Object obj = array[mapIndex(head)]; + head++; + return obj; + } + + public boolean isEmpty(){ + return head == rear; + } + + public int size(){ + return rear - head; + } + + private int capacity() { + return array.length; + } + + private void autoGrow() { + if (size() >= capacity()) { + Object[] newArray = new Object[nextCapacity()]; + System.arraycopy(array, 0, newArray, 0, capacity()); + + int increase = nextCapacity() - capacity(); + int moveCount = size() - mapIndex(rear); + System.arraycopy(newArray, mapIndex(head), newArray, mapIndex(head) + increase, moveCount); + array = newArray; + head += increase; + rear += increase; + } + } + + private int nextCapacity() { + return capacity() * 2; + } + + private int mapIndex(int index) { + return index >= capacity() ? index % capacity() : index; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + for (int i = 0; i < 22; ++i) { + queue.enQueue(i); + } + for (int i = 0; i < 6; ++i) { + System.out.print(queue.deQueue() + " "); + } + for (int i = 22; i < 41; ++i) { + queue.enQueue(i); + } + while (!queue.isEmpty()) { + System.out.print(queue.deQueue() + " "); + } + System.out.println(); + } +} diff --git a/group01/895457260/code/src/datastructure/basic/Stack.java b/group01/895457260/code/src/datastructure/basic/Stack.java new file mode 100644 index 0000000000..772ab16385 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Stack.java @@ -0,0 +1,43 @@ +package datastructure.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object peek = peek(); + elementData.remove(elementData.size() - 1); + return peek; + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + Stack stack = new Stack(); + for (int i = 0; i < 6; ++i) { + stack.push(i); + } + for (int i = 0; i < 4; ++i) { + System.out.print(stack.pop() + " "); + } + for (int i = 6; i < 21; ++i) { + stack.push(i); + } + while (!stack.isEmpty()) { + System.out.print(stack.pop() + " "); + } + } +} diff --git a/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java b/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java new file mode 100644 index 0000000000..3eb826c4cc --- /dev/null +++ b/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java @@ -0,0 +1,304 @@ +package datastructure.jdklist; + +import datastructure.nongeneric.MyBaseList; + +import java.util.*; + +/** + * Created by Haochen on 2017/2/15. + * TODO: + */ +public class MyArrayList extends MyBaseList { + + private Object[] array; + private int size; + private int initSize; + + public MyArrayList() { + initSize = 10; + this.array = new Object[initSize]; + } + + public MyArrayList(int initSize) { + this.initSize = initSize; + this.array = new Object[initSize]; + } + + protected int nextSize(int size) { + return size * 2; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private int index = -1; + @Override + public boolean hasNext() { + return index < size() - 1; + } + + @Override + public T next() { + index++; + return (T) array[index]; + } + + @Override + public void remove() { + MyArrayList.this.remove(index); + index--; + } + }; + } + + @Override + public T1[] toArray(T1[] a) { + if (a.length < size()) { + return (T1[]) Arrays.copyOf(array, size(), a.getClass()); + } else { + System.arraycopy(array, 0, a, 0, size()); + return a; + } + } + + private boolean isFull() { + return size() >= array.length; + } + + private void expand(int newCapacity) { + Object[] newArray = new Object[newCapacity]; + System.arraycopy(array, 0, newArray, 0, size()); + this.array = newArray; + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + if (index >= 0) { + remove(index); + } + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + int addCount = c.size(); + int capacityAfterAdd = array.length; + int countAfterAdd = size + addCount; + while (capacityAfterAdd < countAfterAdd) { + capacityAfterAdd = nextSize(capacityAfterAdd); + } + if (capacityAfterAdd > array.length) { + expand(capacityAfterAdd); + } + System.arraycopy(array, index, array, index + addCount, size - index); + for (Object o : c) { + array[size] = o; + size++; + } + return true; + } + + @Override + protected T getNoCheck(int index) { + return (T) array[index]; + } + + @Override + protected T setNoCheck(int index, T element) { + T t = (T) array[index]; + array[index] = element; + return t; + } + + @Override + protected void addNoCheck(int index, T element) { + if (isFull()) { + expand(nextSize(array.length)); + } + System.arraycopy(array, index, array, index + 1, size() - index); + array[index] = element; + size++; + } + + @Override + public void clear() { + array = new Object[10]; + size = 0; + } + + @Override + protected T removeNoCheck(int index) { + T t = (T) array[index]; + if (index < size() - 1) { + System.arraycopy(array, index + 1, array, index, size() - index); + } + size--; + return t; + } + + @Override + public int indexOf(Object o) { + for (int i = 0; i < size(); ++i) { + if (array[i] == null ? o == null : array[i].equals(o)) { + return i; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + for (int i = size - 1; i >= 0; --i) { + if (array[i] == null ? o == null : array[i].equals(o)) { + return i; + } + } + return -1; + } + + @Override + public ListIterator listIterator(int index) { + final int beginIndex = index; + return new ListIterator() { + private int point = beginIndex - 1; + @Override + public boolean hasNext() { + return point < size - 1; + } + + @Override + public T next() { + point++; + return (T) array[point]; + } + + @Override + public boolean hasPrevious() { + return point > beginIndex; + } + + @Override + public T previous() { + point--; + return (T) array[point]; + } + + @Override + public int nextIndex() { + return point + 1; + } + + @Override + public int previousIndex() { + return point - 1; + } + + @Override + public void remove() { + MyArrayList.this.remove(point); + point--; + } + + @Override + public void set(T t) { + MyArrayList.this.set(point, t); + } + + @Override + public void add(T t) { + MyArrayList.this.add(point + 1, t); + } + }; + } + + @Override + public List subList(int fromIndex, int toIndex) { + //这是错误实现 + MyArrayList result = new MyArrayList<>(toIndex - fromIndex + 1); + for (int i = fromIndex; i <= toIndex; ++i) { + result.array[result.size()] = array[i]; + result.size++; + } + return result; + } + + public static void main(String[] args) { + MyBaseList list = new MyArrayList<>(); + for (int i = 0; i < 10; ++i) { + list.add(i); + list.add(10 - i); + } + System.out.println("------------------size"); + System.out.println("size: " + list.size()); + + System.out.println("------------------for(int i)"); + for (int i = 0; i < list.size(); ++i) { + System.out.print(list.get(i) + " "); + } + + System.out.println("\n-----------------foreach"); + for (int i : list) { + System.out.print(i + " "); + } + + System.out.println("\n-----------------iterator"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + System.out.println("\n-----------------listIterator"); + iterator = list.listIterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + System.out.println("\n-----------------indexOf 0~10"); + for (int i = 0; i <= 10; ++i) { + System.out.print("[" + i + "]" + list.indexOf(i) + " "); + } + + System.out.println("\n-----------------lastIndexOf 0~10"); + for (int i = 0; i <= 10; ++i) { + System.out.print("[" + i + "]" + list.lastIndexOf(i) + " "); + } + + System.out.println("\n-----------------add at index 0 100~104"); + for (int i = 100; i < 105; ++i) { + list.add(0, i); + } + list.print(); + System.out.println("-----------------add at last 200~204"); + for (int i = 200; i < 205; ++i) { + list.add(list.size(), i); + } + list.print(); + + System.out.println("-----------------removeFirst x4"); + for (int i = 0; i < 4; ++i) { + list.remove(0); + } + list.print(); + + System.out.println("\n-----------------removeLast x4"); + for (int i = 0; i < 4; ++i) { + list.remove(list.size() - 1); + } + list.print(); + + System.out.println("-----------------iterator remove"); + iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "[" + list.size() + "] "); + iterator.remove(); + } + System.out.println(); + list.print(); + } +} diff --git a/group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java b/group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java new file mode 100644 index 0000000000..44b2368f11 --- /dev/null +++ b/group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java @@ -0,0 +1,382 @@ +package datastructure.jdklist; + +import datastructure.nongeneric.MyBaseList; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +/** + * Created by Haochen on 2017/2/15. + * TODO: + */ +public class MyLinkedList extends MyBaseList { + + private Node head; + private Node rear; + private int size; + + public MyLinkedList() { + head = new Node<>(); + rear = new Node<>(); + clear(); + } + + private static class Node { + T data; + Node next; + Node previous; + } + + @Override + public int size() { + return size; + } + + private void addNode(Node node, Node previous) { + node.next = previous.next; + node.previous = previous; + node.next.previous = node; + previous.next = node; + } + + private void removeNode(Node node) { + node.previous.next = node.next; + node.next.previous = node.previous; + node.previous = null; + node.next = null; + node.data = null; + } + + public void addFirst(T element) { + add(0, element); + } + + public void addLast(T element) { + add(size(), element); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size() - 1); + } + + @Override + public Iterator iterator() { + return new Iterator() { + private Node node = head; + @Override + public boolean hasNext() { + return node.next != rear; + } + + @Override + public T next() { + node = node.next; + return node.data; + } + + @Override + public void remove() { + if (node != head && node != rear) { + node = node.previous; + removeNode(node.next); + size--; + } + } + }; + } + + @Override + public T1[] toArray(T1[] a) { + Object[] result = a.length < size ? new Object[size] : a; + for (Node node = head.next; node != rear; node = node.next) { + result[result.length] = node.data; + } + return (T1[]) result; + } + + private Node findNode(Object o) { + for (Node node = head.next; node != rear; node = node.next) { + if (node.data == null ? o == null : node.data.equals(o)) { + return node; + } + } + return null; + } + + private Node findNode(int index) { + if (index == -1) { + return head; + } else if (index == size) { + return rear; + } else if (index < -1 || index > size) { + indexOuOfBound(index); + } + Node node; + if (index > size() / 2) { + node = rear.previous; + for (int i = 0; i < size() - index - 1; ++i) { + node = node.previous; + } + } else { + node = head.next; + for (int i = 0; i < index; ++i) { + node = node.next; + } + } + return node; + } + + @Override + public boolean remove(Object o) { + Node node = findNode(o); + removeNode(node); + size--; + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + Node target = findNode(index); + Node newLinkHead = new Node(); + Node newLinkRear = newLinkHead; + for (T t : c) { + newLinkRear.next = new Node(); + newLinkRear.next.data = t; + newLinkRear.next.previous = newLinkRear; + newLinkRear = newLinkRear.next; + } + if (newLinkRear != newLinkHead) { + newLinkRear.next = target.next; + target.next.previous = newLinkRear; + newLinkHead = newLinkHead.next; + newLinkHead.previous = target; + target.next = newLinkHead; + size += c.size(); + } + return true; + } + + @Override + protected T getNoCheck(int index) { + return findNode(index).data; + } + + @Override + protected T setNoCheck(int index, T element) { + Node node = findNode(index); + T t = node.data; + node.data = element; + return t; + } + + @Override + protected void addNoCheck(int index, T element) { + Node pre = findNode(index - 1); + Node node = new Node(); + node.data = element; + addNode(node, pre); + size++; + } + + @Override + public void clear() { + head.next = rear; + head.previous = rear; + rear.next = head; + rear.previous = head; + size = 0; + } + + @Override + protected T removeNoCheck(int index) { + Node node = findNode(index); + T t = node.data; + removeNode(node); + size--; + return t; + } + + @Override + public int indexOf(Object o) { + int index = 0; + Node node; + for (node = head.next; node != rear; node = node.next, index++) { + if (node.data == null ? o == null : node.data.equals(o)) { + break; + } + } + return node == rear ? -1 : index; + } + + @Override + public int lastIndexOf(Object o) { + int index = size - 1; + Node node; + for (node = rear.previous; node != head; node = node.previous, index--) { + if (node.data == null ? o == null : node.data.equals(o)) { + break; + } + } + return node == head ? -1 : index; + } + + @Override + public ListIterator listIterator(int index) { + final int beginIndex = index - 1; + return new ListIterator() { + private Node node = findNode(beginIndex); + private int index = beginIndex; + + @Override + public boolean hasNext() { + return node.next != rear; + } + + @Override + public T next() { + node = node.next; + index++; + return node.data; + } + + @Override + public boolean hasPrevious() { + return node.previous != head; + } + + @Override + public T previous() { + node = node.previous; + index--; + return node.data; + } + + @Override + public int nextIndex() { + return hasNext() ? index + 1 : index; + } + + @Override + public int previousIndex() { + return hasPrevious() ? index - 1 : -1; + } + + @Override + public void remove() { + if (node != head && node != rear) { + node = node.previous; + removeNode(node.next); + size--; + } + } + + @Override + public void set(T t) { + node.data = t; + } + + @Override + public void add(T t) { + Node node = new Node(); + node.data = t; + addNode(node, this.node); + size++; + } + }; + } + + @Override + public List subList(int fromIndex, int toIndex) { + //这是错误实现 + Node from = findNode(fromIndex); + Node to = findNode(toIndex); + List list = new MyLinkedList<>(); + if (from != null && to != null) { + for (Node node = from; node != to.next; node = node.next) { + list.add(node.data); + } + } + return list; + } + + public static void main(String[] args) { + MyLinkedList list = new MyLinkedList<>(); + for (int i = 0; i < 10; ++i) { + list.add(i); + list.add(10 - i); + } + System.out.println("------------------size"); + System.out.println("size: " + list.size()); + + System.out.println("------------------for(int i)"); + for (int i = 0; i < list.size(); ++i) { + System.out.print(list.get(i) + " "); + } + + System.out.println("\n-----------------foreach"); + for (int i : list) { + System.out.print(i + " "); + } + + System.out.println("\n-----------------iterator"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + System.out.println("\n-----------------listIterator"); + iterator = list.listIterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + System.out.println("\n-----------------indexOf 0~10"); + for (int i = 0; i <= 10; ++i) { + System.out.print("[" + i + "]" + list.indexOf(i) + " "); + } + + System.out.println("\n-----------------lastIndexOf 0~10"); + for (int i = 0; i <= 10; ++i) { + System.out.print("[" + i + "]" + list.lastIndexOf(i) + " "); + } + + System.out.println("\n-----------------addFirst 100~104"); + for (int i = 100; i < 105; ++i) { + list.addFirst(i); + } + list.print(); + System.out.println("-----------------addLast 200~204"); + for (int i = 200; i < 205; ++i) { + list.addLast(i); + } + list.print(); + + System.out.println("-----------------removeFirst x4"); + for (int i = 0; i < 4; ++i) { + list.removeFirst(); + } + list.print(); + + System.out.println("-----------------removeLast x4"); + for (int i = 0; i < 4; ++i) { + list.removeLast(); + } + list.print(); + + System.out.println("-----------------iterator remove"); + iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "[" + list.size() + "] "); + iterator.remove(); + } + System.out.println(); + list.print(); + } + +} diff --git a/group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java b/group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java new file mode 100644 index 0000000000..ca04443db2 --- /dev/null +++ b/group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java @@ -0,0 +1,137 @@ +package datastructure.nongeneric; + +import java.util.Collection; +import java.util.List; +import java.util.ListIterator; + +/** + * Created by Haochen on 2017/2/20. + * TODO: + */ +public abstract class MyBaseList implements List { + + protected final boolean checkGetIndex(int index) { + return index >= 0 && index < size(); + } + + protected final void checkGetIndexAndThrow(int index) { + if (!checkGetIndex(index)) { + indexOuOfBound(index); + } + } + + protected final boolean checkAddIndex(int index) { + return index >= 0 && index <= size(); + } + + protected final void checkAddIndexAndThrow(int index) { + if (!checkAddIndex(index)) { + indexOuOfBound(index); + } + } + + protected final void indexOuOfBound(int index) { + throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); + } + + @Override + public T remove(int index) { + checkGetIndexAndThrow(index); + return removeNoCheck(index); + } + + protected abstract T removeNoCheck(int index); + + protected final String indexOutOfBoundMessage(int index) { + return "Index: " + index + ", size: " + size(); + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public Object[] toArray() { + return toArray(new Object[size()]); + } + + @Override + public boolean add(T t) { + int before = size(); + add(size(), t); + return size() != before; + } + + @Override + public boolean containsAll(Collection c) { + for (Object o : c) { + if (!contains(o)) { + return false; + } + } + return true; + } + + @Override + public boolean addAll(Collection c) { + return addAll(size(), c); + } + + @Override + public boolean removeAll(Collection c) { + int before = size(); + c.parallelStream().forEach(this::remove); + return size() != before; + } + + @Override + public boolean retainAll(Collection c) { + int before = size(); + parallelStream().forEach((o) -> { + if (!c.contains(o)) { + remove(o); + } + }); + return size() != before; + } + + protected abstract T getNoCheck(int index); + protected abstract T setNoCheck(int index, T element); + protected abstract void addNoCheck(int index, T element); + + @Override + public T get(int index) { + checkGetIndexAndThrow(index); + return getNoCheck(index); + } + + @Override + public T set(int index, T element) { + checkGetIndexAndThrow(index); + return setNoCheck(index, element); + } + + @Override + public void add(int index, T element) { + checkAddIndexAndThrow(index); + addNoCheck(index, element); + } + + @Override + public boolean contains(Object o) { + return indexOf(o) >= 0; + } + + @Override + public ListIterator listIterator() { + return listIterator(0); + } + + public void print() { + for (T t : this) { + System.out.print(t + " "); + } + System.out.println("\nsize: " + size()); + } +} diff --git a/group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java b/group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java new file mode 100644 index 0000000000..893c1d88ca --- /dev/null +++ b/group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java @@ -0,0 +1,61 @@ +package datastructure.nongeneric; + +/** + * Created by Haochen on 2017/2/15. + * TODO: + */ +public class MyBinaryTree { + private static class Node { + Object data; + Node left; + Node right; + } + + private Node root = null; + + //不递归的写法 + public void add(T o) { + //根节点空,直接加入 + if (root == null) { + root = new Node(); + root.data = o; + } else { + Node target = root; + //从根结点不断向下比较target和o,o小则往左,o大则往右,相等不加入 + while (true) { + int compare = o.compareTo(target.data); + if (compare == 0) {//相等不加入 + return; + } else if (compare < 0) {//o小往左 + if (target.left == null) {//左空则加入 + target.left = new Node(); + target.left.data = o; + return; + } else {//不空继续比较 + target = target.left; + } + } else {//o大往右 + if (target.right == null) { + target.right = new Node(); + target.right.data = o; + return; + } else { + target = target.right; + } + } + } + } + } + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree<>(); + tree.add(5); + tree.add(2); + tree.add(1); + tree.add(7); + tree.add(6); + tree.add(4); + tree.add(8); + tree.add(8); + } +} diff --git a/group01/895457260/code/src/datastructure/nongeneric/MyQueue.java b/group01/895457260/code/src/datastructure/nongeneric/MyQueue.java new file mode 100644 index 0000000000..c7d6e2c13b --- /dev/null +++ b/group01/895457260/code/src/datastructure/nongeneric/MyQueue.java @@ -0,0 +1,43 @@ +package datastructure.nongeneric; + +import datastructure.jdklist.MyLinkedList; + +import java.util.List; + +/** + * Created by Haochen on 2017/2/15. + * TODO: + */ +public class MyQueue { + //先进先出,进在队尾,出在队头 + private List list = new MyLinkedList<>(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + Object o = list.get(0); + list.remove(0); + return o; + } + + public int size() { + return list.size(); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + for (int i = 0; i < 20; ++i) { + queue.enQueue(i); + } + System.out.println("size: " + queue.size()); + while (!queue.isEmpty()) { + System.out.print(queue.deQueue() + "[" + queue.size() + "] "); + } + } +} diff --git a/group01/895457260/code/src/datastructure/nongeneric/MyStack.java b/group01/895457260/code/src/datastructure/nongeneric/MyStack.java new file mode 100644 index 0000000000..16e65c304a --- /dev/null +++ b/group01/895457260/code/src/datastructure/nongeneric/MyStack.java @@ -0,0 +1,47 @@ +package datastructure.nongeneric; + +import datastructure.jdklist.MyLinkedList; + +import java.util.List; + +/** + * Created by Haochen on 2017/2/15. + * TODO: + */ +public class MyStack { + //后进先出,进出都在队头 + private List list = new MyLinkedList<>(); + + public void push(Object o) { + list.add(0, o); + } + + public Object pop() { + Object o = list.get(0); + list.remove(0); + return o; + } + + public Object peek() { + return list.get(0); + } + + public int size() { + return list.size(); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + for (int i = 0; i < 20; ++i) { + stack.push(i); + } + System.out.println("size: " + stack.size()); + while (!stack.isEmpty()) { + System.out.print(stack.pop() + "[" + stack.size() + "] "); + } + } +} From 237903198968004d3dcce3aab677d6edc9855c62 Mon Sep 17 00:00:00 2001 From: TonyHui Date: Thu, 23 Feb 2017 13:50:04 +0800 Subject: [PATCH 003/419] =?UTF-8?q?1.=20=E5=AE=8C=E6=88=90=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E4=B9=A0=E9=A2=98=202.=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=203.=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E6=B7=BB=E5=8A=A0delete()=EF=BC=8C=20search(?= =?UTF-8?q?)=EF=BC=8C=20min()=EF=BC=8C=20max()=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/954958168/954958168.md | 0 .../class01/BasicDataStructure/.gitignore | 28 ++ .../class01/BasicDataStructure/pom.xml | 24 ++ .../com/aaront/exercise/basic/ArrayList.java | 90 +++++++ .../com/aaront/exercise/basic/BinaryTree.java | 235 ++++++++++++++++ .../com/aaront/exercise/basic/Iterator.java | 9 + .../com/aaront/exercise/basic/LinkedList.java | 129 +++++++++ .../java/com/aaront/exercise/basic/List.java | 9 + .../java/com/aaront/exercise/basic/Queue.java | 26 ++ .../java/com/aaront/exercise/basic/Stack.java | 29 ++ .../exercise/generic/GenericArrayList.java | 98 +++++++ .../exercise/generic/GenericBinaryTree.java | 255 ++++++++++++++++++ .../exercise/generic/GenericIterator.java | 9 + .../exercise/generic/GenericLinkedList.java | 140 ++++++++++ .../aaront/exercise/generic/GenericList.java | 9 + .../aaront/exercise/generic/GenericQueue.java | 30 +++ .../aaront/exercise/generic/GenericStack.java | 33 +++ .../com/aaront/execrise/basic/AllTest.java | 19 ++ .../aaront/execrise/basic/ArrayListTest.java | 69 +++++ .../aaront/execrise/basic/BinaryTreeTest.java | 94 +++++++ .../aaront/execrise/basic/LinkListTest.java | 81 ++++++ .../com/aaront/execrise/basic/QueueTest.java | 33 +++ .../com/aaront/execrise/basic/StackTest.java | 46 ++++ .../execrise/generic/GenericAllTest.java | 19 ++ .../generic/GenericArrayListTest.java | 76 ++++++ .../generic/GenericBinaryTreeTest.java | 75 ++++++ .../generic/GenericLinkedListTest.java | 90 +++++++ .../execrise/generic/GenericQueueTest.java | 33 +++ .../execrise/generic/GenericStackTest.java | 46 ++++ 29 files changed, 1834 insertions(+) create mode 100644 group01/954958168/954958168.md create mode 100644 group01/954958168/class01/BasicDataStructure/.gitignore create mode 100644 group01/954958168/class01/BasicDataStructure/pom.xml create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java create mode 100644 group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java diff --git a/group01/954958168/954958168.md b/group01/954958168/954958168.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group01/954958168/class01/BasicDataStructure/.gitignore b/group01/954958168/class01/BasicDataStructure/.gitignore new file mode 100644 index 0000000000..eb95f3ba7f --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/.gitignore @@ -0,0 +1,28 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Idea Files # +.idea +*.iml + diff --git a/group01/954958168/class01/BasicDataStructure/pom.xml b/group01/954958168/class01/BasicDataStructure/pom.xml new file mode 100644 index 0000000000..a224b2116f --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.aaront.execrise + coding2017 + 1.0.0-SNAPSHOT + jar + + + + UTF-8 + 1.8 + + + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java new file mode 100644 index 0000000000..ae462ea905 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java @@ -0,0 +1,90 @@ +package com.aaront.exercise.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private static final double factor = 0.75; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + _ensureCapacityEnough(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("index超出边界"); + _ensureCapacityEnough(); + int i = size; + for (; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[i] = o; + size++; + } + + private void _ensureCapacityEnough() { + if (size >= elementData.length) { + dilatancy(); + } + } + + private void dilatancy() { + int newLength = elementData.length + (int) (elementData.length * factor); + elementData = Arrays.copyOf(elementData, newLength); + } + + public Object get(int index) { + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + Object element = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + size--; + return element; + + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + System.arraycopy(elementData, 0, objects, 0, size); + return objects; + } + + private static class ArrayListIterator implements Iterator { + + private ArrayList arrayList; + private int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + public boolean hasNext() { + return pos < arrayList.size(); + } + + public Object next() { + return arrayList.elementData[pos++]; + } + + public void remove() { + arrayList.remove(pos - 1); + pos--; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java new file mode 100644 index 0000000000..2c0d156561 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java @@ -0,0 +1,235 @@ +package com.aaront.exercise.basic; + +public class BinaryTree { + + private BinaryTreeNode head = new BinaryTreeNode(null); + private BinaryTreeNode root; + private int size; + private int index = 0; + + public static final int PREORDER = 0; + public static final int INORDER = 1; + public static final int POSTORDER = 2; + public static final int HIERARCHICAL = 3; + + public static final int RECURSION = 10; + public static final int ITERATION = 11; + + public void add(Integer o) { + BinaryTreeNode node = new BinaryTreeNode(o); + if (root == null) { + root = node; + head.setLeft(root); + } else { + insert(root, node); + } + size++; + } + + private void insert(BinaryTreeNode node, BinaryTreeNode newNode) { + // 要插入的节点插入当前节点的左子树 + if (node.getData() > newNode.getData()) { + if (node.getLeft() == null) { + node.setLeft(newNode); + } else { + insert(node.left, newNode); + } + } else { // 要插入的节点插入当前节点的右子树 + if (node.getRight() == null) { + node.setRight(newNode); + } else { + insert(node.right, newNode); + } + } + } + + public BinaryTreeNode search(int data) { + return search(data, ITERATION); + } + + public BinaryTreeNode search(int data, int method) { + switch (method) { + case RECURSION: + return findNodeRecursion(root, data); + case ITERATION: + return findNodeIteration(data); + default: + throw new IllegalArgumentException("不支持的查找方法"); + } + } + + private BinaryTreeNode findNodeRecursion(BinaryTreeNode node, int data) { + if (node == null) return null; + if (node.getData() == data) return node; + if (node.getData() > data) return findNodeRecursion(node.getLeft(), data); + return findNodeRecursion(node.getRight(), data); + } + + private BinaryTreeNode findNodeIteration(int data) { + BinaryTreeNode currentNode = root; + while (currentNode != null) { + if (currentNode.getData() == data) { + return currentNode; + } + if (currentNode.getData() > data) { + currentNode = currentNode.getLeft(); + } else { + currentNode = currentNode.getRight(); + } + } + return null; + } + + public BinaryTreeNode min() { + return findMin(root); + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node == null) return null; + if (node.getLeft() == null) return node; + return findMin(node.getLeft()); + } + + public BinaryTreeNode max() { + return findMax(root); + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node == null) return null; + if (node.getRight() == null) return node; + return findMax(node.getRight()); + } + + public void delete(Integer data) { + BinaryTreeNode node = search(data); + if (node == null) return; + BinaryTreeNode parentNode = searchParentNode(node); + if (parentNode == null) return; + // 删除叶子节点 + if (node.getLeft() == null && node.getRight() == null) { + if (parentNode.getLeft() == node) parentNode.setLeft(null); + else parentNode.setRight(null); + } else if (node.getLeft() != null && node.getRight() == null) { // 删除只有左子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getLeft()); + else parentNode.setRight(node.getLeft()); + } else if (node.getRight() != null && node.getLeft() == null) { // 删除只有右子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getRight()); + else parentNode.setRight(node.getRight()); + } else { // 删除有两个子树的节点 + BinaryTreeNode replace = findMin(node.getRight()); + BinaryTreeNode replaceParentNode = searchParentNode(replace); + replaceParentNode.setLeft(replace.getRight()); + node.setData(replace.getData()); + replace.setLeft(null); + replace.setRight(null); + } + size--; + } + + private BinaryTreeNode searchParentNode(BinaryTreeNode node) { + if (node == null) return null; + if (node == root) return head; + BinaryTreeNode current = root; + while (current != null) { + if (current.getLeft() == node || current.getRight() == node) return current; + if (current.getData().compareTo(node.getData()) > 0) current = current.getLeft(); + else current = current.getRight(); + } + return null; + } + + public int[] traversal() { + return traversal(PREORDER); + } + + public int[] traversal(int order) { + int[] datas = new int[size]; + if (order == PREORDER) { + preorderTraversal(root, datas); + } else if (order == INORDER) { + inorderTraversal(root, datas); + } else if (order == POSTORDER) { + postorderTraversal(root, datas); + } else { + hierarchicalTraversal(root, datas); + } + index = 0; + return datas; + } + + private void preorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + datas[index++] = node.getData(); + preorderTraversal(node.getLeft(), datas); + preorderTraversal(node.getRight(), datas); + } + + private void inorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + inorderTraversal(node.getLeft(), datas); + datas[index++] = node.getData(); + inorderTraversal(node.getRight(), datas); + } + + private void postorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + postorderTraversal(node.getLeft(), datas); + postorderTraversal(node.getRight(), datas); + datas[index++] = node.getData(); + } + + private void hierarchicalTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) return; + Queue queue = new Queue(); + queue.enQueue(node); + while (!queue.isEmpty()) { + BinaryTreeNode tmp = (BinaryTreeNode) queue.deQueue(); + datas[index++] = tmp.getData(); + if (tmp.getLeft() != null) queue.enQueue(tmp.getLeft()); + if (tmp.getRight() != null) queue.enQueue(tmp.getRight()); + } + } + + public class BinaryTreeNode { + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data) { + this.data = data; + } + + 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; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java new file mode 100644 index 0000000000..e446dd8f65 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.basic; + +public interface Iterator { + boolean hasNext(); + + Object next(); + + void remove(); +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java new file mode 100644 index 0000000000..504e73580b --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java @@ -0,0 +1,129 @@ +package com.aaront.exercise.basic; + +public class LinkedList implements List { + + private Node head = new Node(null); + private int size = 0; + + public void add(Object o) { + Node newNode = new Node(o); + Node first = head.next; + Node second = head; + while (first != null) { + second = first; + first = first.next; + } + second.next = newNode; + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node node = new Node(o); + node.next = first.next; + first.next = node; + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head.next; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + return first.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node element = first.next; + first.next = first.next.next; + size--; + return element.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); + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + Node first = head.next; + int pos = 0; + while (first!= null) { + objects[pos++] = first.data; + first = first.next; + } + return objects; + } + + private static class LinkedListIterator implements Iterator { + + private int pos = 0; + private LinkedList linkedList; + + private LinkedListIterator(LinkedList linkList) { + this.linkedList = linkList; + } + + @Override + public boolean hasNext() { + return pos < linkedList.size(); + } + + @Override + public Object next() { + return linkedList.get(pos++); + } + + @Override + public void remove() { + linkedList.remove(pos - 1); + pos--; + } + } + + + private static class Node { + private Object data; + private Node next; + + private Node(Object data) { + this.data = data; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java new file mode 100644 index 0000000000..0988b60f4a --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.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/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java new file mode 100644 index 0000000000..7c310bca9e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java @@ -0,0 +1,26 @@ +package com.aaront.exercise.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } + + public Object[] toArray() { + return linkedList.toArray(); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java new file mode 100644 index 0000000000..450d21ee89 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java @@ -0,0 +1,29 @@ +package com.aaront.exercise.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(elementData.size() - 1); + } + + public Object peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public Object[] toArray() { + return elementData.toArray(); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java new file mode 100644 index 0000000000..a099746b55 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java @@ -0,0 +1,98 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericArrayList implements GenericList { + + private int size = 0; + + private static final double factor = 0.75; + + private Object[] elementData = new Object[100]; + + public void add(T o) { + _ensureCapacityEnough(); + elementData[size++] = o; + } + + public void add(int index, T o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("index超出边界"); + _ensureCapacityEnough(); + int i = size; + for (; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[i] = o; + size++; + } + + private void _ensureCapacityEnough() { + if (size >= elementData.length) { + dilatancy(); + } + } + + private void dilatancy() { + int newLength = elementData.length + (int) (elementData.length * factor); + elementData = Arrays.copyOf(elementData, newLength); + } + + public T get(int index) { + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + return (T) elementData[index]; + } + + public T remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + Object element = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + size--; + return (T) element; + + } + + public int size() { + return size; + } + + public GenericIterator iterator() { + return new ArrayListGenericIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + System.arraycopy(elementData, 0, objects, 0, size); + return objects; + } + + public T[] toArray(T[] a) { + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + private static class ArrayListGenericIterator implements GenericIterator { + + private GenericArrayList genericArrayList; + private int pos = 0; + + private ArrayListGenericIterator(GenericArrayList genericArrayList) { + this.genericArrayList = genericArrayList; + } + + public boolean hasNext() { + return pos < genericArrayList.size(); + } + + public T next() { + return (T) genericArrayList.elementData[pos++]; + } + + public void remove() { + genericArrayList.remove(pos - 1); + pos--; + } + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java new file mode 100644 index 0000000000..e5cf3b439a --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java @@ -0,0 +1,255 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericBinaryTree> { + + private BinaryTreeNode head = new BinaryTreeNode<>(null); + private BinaryTreeNode root; + private int size; + private int index = 0; + public static final int PREORDER = 0; + public static final int INORDER = 1; + public static final int POSTORDER = 2; + public static final int HIERARCHICAL = 3; + + public static final int RECURSION = 10; + public static final int ITERATION = 11; + + public void add(T o) { + BinaryTreeNode node = new BinaryTreeNode<>(o); + if (root == null) { + root = node; + head.setLeft(root); + } else { + insert(root, node); + } + size++; + } + + private void insert(BinaryTreeNode node, BinaryTreeNode newNode) { + // 要插入的节点插入当前节点的左子树 + if (node.getData().compareTo(newNode.getData()) > 0) { + if (node.getLeft() == null) { + node.setLeft(newNode); + } else { + insert(node.left, newNode); + } + } else { // 要插入的节点插入当前节点的右子树 + if (node.getRight() == null) { + node.setRight(newNode); + } else { + insert(node.right, newNode); + } + } + } + + public BinaryTreeNode search(T data) { + return search(data, ITERATION); + } + + public BinaryTreeNode search(T data, int method) { + switch (method) { + case RECURSION: + return findNodeRecursion(root, data); + case ITERATION: + return findNodeIteration(data); + default: + throw new IllegalArgumentException("不支持的查找方法"); + } + } + + private BinaryTreeNode findNodeRecursion(BinaryTreeNode node, T data) { + if (node == null) return null; + if (node.getData().compareTo(data) == 0) return node; + if (node.getData().compareTo(data) > 0) return findNodeRecursion(node.getLeft(), data); + return findNodeRecursion(node.getRight(), data); + } + + private BinaryTreeNode findNodeIteration(T data) { + BinaryTreeNode currentNode = root; + while (currentNode != null) { + if (currentNode.getData().compareTo(data) == 0) { + return currentNode; + } + if (currentNode.getData().compareTo(data) > 0) { + currentNode = currentNode.getLeft(); + } else { + currentNode = currentNode.getRight(); + } + } + return null; + } + + public BinaryTreeNode min() { + return findMin(root); + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node == null) return null; + if (node.getLeft() == null) return node; + return findMin(node.getLeft()); + } + + public BinaryTreeNode max() { + return findMax(root); + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node == null) return null; + if (node.getRight() == null) return node; + return findMax(node.getRight()); + } + + public void delete(T data) { + BinaryTreeNode node = search(data); + if (node == null) return; + BinaryTreeNode parentNode = searchParentNode(node); + if (parentNode == null) return; + // 删除叶子节点 + if (node.getLeft() == null && node.getRight() == null) { + if (parentNode.getLeft() == node) parentNode.setLeft(null); + else parentNode.setRight(null); + } else if (node.getLeft() != null && node.getRight() == null) { // 删除只有左子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getLeft()); + else parentNode.setRight(node.getLeft()); + } else if (node.getRight() != null && node.getLeft() == null) { // 删除只有右子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getRight()); + else parentNode.setRight(node.getRight()); + } else { // 删除有两个子树的节点 + BinaryTreeNode replace = findMin(node.getRight()); + BinaryTreeNode replaceParentNode = searchParentNode(replace); + replaceParentNode.setLeft(replace.getRight()); + node.setData(replace.getData()); + replace.setLeft(null); + replace.setRight(null); + } + size--; + } + + private BinaryTreeNode searchParentNode(BinaryTreeNode node) { + if (node == null) return null; + if (node == root) return head; + BinaryTreeNode current = root; + while (current != null) { + if (current.getLeft() == node || current.getRight() == node) return current; + if (current.getData().compareTo(node.getData()) > 0) current = current.getLeft(); + else current = current.getRight(); + } + return null; + } + + public Object[] traversal() { + return traversal(PREORDER); + } + + public T[] traversal(T[] a) { + Object[] elementData = traversal(PREORDER); + return toArray(elementData, a); + } + + public T[] traversal(int order, T[] a) { + Object[] elementData = traversal(order); + return toArray(elementData, a); + } + + private T[] toArray(Object[] elementData, T[] a) { + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + public Object[] traversal(int order) { + Object[] datas = new Object[size]; + if (order == PREORDER) { + preorderTraversal(root, datas); + } else if (order == INORDER) { + inorderTraversal(root, datas); + } else if (order == POSTORDER) { + postorderTraversal(root, datas); + } else { + hierarchicalTraversal(root, datas); + } + index = 0; + return datas; + } + + private void preorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + datas[index++] = node.getData(); + preorderTraversal(node.getLeft(), datas); + preorderTraversal(node.getRight(), datas); + } + + private void inorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + inorderTraversal(node.getLeft(), datas); + datas[index++] = node.getData(); + inorderTraversal(node.getRight(), datas); + } + + private void postorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + postorderTraversal(node.getLeft(), datas); + postorderTraversal(node.getRight(), datas); + datas[index++] = node.getData(); + } + + private void hierarchicalTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) return; + GenericQueue> queue = new GenericQueue<>(); + queue.enQueue(node); + while (!queue.isEmpty()) { + BinaryTreeNode tmp = queue.deQueue(); + datas[index++] = tmp.getData(); + if (tmp.getLeft() != null) queue.enQueue(tmp.getLeft()); + if (tmp.getRight() != null) queue.enQueue(tmp.getRight()); + } + } + + + class BinaryTreeNode> { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data) { + this.data = data; + } + + public T getData() { + return data; + } + + public void setData(T 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; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java new file mode 100644 index 0000000000..565114dce7 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.generic; + +public interface GenericIterator { + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java new file mode 100644 index 0000000000..7caf32eae1 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java @@ -0,0 +1,140 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericLinkedList implements GenericList { + + private Node head = new Node<>(null); + private int size = 0; + + public void add(T o) { + Node newNode = new Node<>(o); + Node first = head.next; + Node second = head; + while (first != null) { + second = first; + first = first.next; + } + second.next = newNode; + size++; + } + + public void add(int index, T o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node node = new Node<>(o); + node.next = first.next; + first.next = node; + size++; + } + + public T get(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head.next; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + return first.data; + } + + public T remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node element = first.next; + first.next = first.next.next; + size--; + return element.data; + } + + public int size() { + return size; + } + + public void addFirst(T o) { + add(0, o); + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + public GenericIterator iterator() { + return new LinkedListGenericIterator<>(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + Node first = head.next; + int pos = 0; + while (first != null) { + objects[pos++] = first.data; + first = first.next; + } + return objects; + } + + public T[] toArray(T[] a) { + Object[] elementData = toArray(); + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + private static class LinkedListGenericIterator implements GenericIterator { + + private int pos = 0; + private GenericLinkedList genericLinkedList; + + private LinkedListGenericIterator(GenericLinkedList linkList) { + this.genericLinkedList = linkList; + } + + @Override + public boolean hasNext() { + return pos < genericLinkedList.size(); + } + + @Override + public T next() { + return genericLinkedList.get(pos++); + } + + @Override + public void remove() { + genericLinkedList.remove(pos - 1); + pos--; + } + } + + + private static class Node { + private T data; + private Node next; + + private Node(T data) { + this.data = data; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java new file mode 100644 index 0000000000..94dc9f8a98 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.generic; + +public interface GenericList { + public void add(T o); + public void add(int index, T o); + public T get(int index); + public T remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java new file mode 100644 index 0000000000..d5cf5681e5 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java @@ -0,0 +1,30 @@ +package com.aaront.exercise.generic; + +public class GenericQueue { + + private GenericLinkedList linkedList = new GenericLinkedList<>(); + + public void enQueue(T o) { + linkedList.add(o); + } + + public T deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } + + public Object[] toArray() { + return linkedList.toArray(); + } + + public T[] toArray(T[] a) { + return linkedList.toArray(a); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java new file mode 100644 index 0000000000..9efb2f2220 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java @@ -0,0 +1,33 @@ +package com.aaront.exercise.generic; + +public class GenericStack { + private GenericArrayList elementData = new GenericArrayList<>(); + + public void push(T o) { + elementData.add(o); + } + + public T pop() { + return elementData.remove(elementData.size() - 1); + } + + public T peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public Object[] toArray() { + return elementData.toArray(); + } + + public T[] toArray(T[] a) { + return elementData.toArray(a); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java new file mode 100644 index 0000000000..ebff633b23 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java @@ -0,0 +1,19 @@ +package com.aaront.execrise.basic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author tonyhui + * @since 17/2/21 + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ArrayListTest.class, + BinaryTreeTest.class, + LinkListTest.class, + QueueTest.class, + StackTest.class +}) +public class AllTest { +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java new file mode 100644 index 0000000000..dcc45d792e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java @@ -0,0 +1,69 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.ArrayList; +import com.aaront.exercise.basic.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void init() { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + } + + @Test + public void testAdd() { + Assert.assertEquals(arrayList.get(0), 1); + Assert.assertEquals(arrayList.get(1), 2); + Assert.assertEquals(arrayList.get(2), 3); + Assert.assertEquals(arrayList.size(), 3); + } + + @Test + public void testAddIndex() { + arrayList.add(1, 4); + arrayList.add(2, 5); + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 5, 2, 3}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(2), 3); + Assert.assertEquals(arrayList.get(0), 1); + Assert.assertEquals(arrayList.get(1), 2); + } + + @Test + public void testRemove() { + testAddIndex(); + arrayList.remove(2); + arrayList.add(4, 10); + arrayList.add(3, 9); + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 2, 9, 3, 10}); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java new file mode 100644 index 0000000000..11fb7ad66b --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java @@ -0,0 +1,94 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.BinaryTree; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class BinaryTreeTest { + + private BinaryTree binaryTree = null; + + @Before + public void init() { + int[] datas = new int[]{9, 4, 5, 7, 1, 2, 3, 10, 17, 9}; + binaryTree = new BinaryTree(); + for (int data : datas) { + binaryTree.add(data); + } + } + + + @Test + public void testAdd() { + int[] preorderDatas = binaryTree.traversal(BinaryTree.PREORDER); + Assert.assertArrayEquals(preorderDatas, new int[]{9, 4, 1, 2, 3, 5, 7, 10, 9, 17}); + int[] inorderDatas = binaryTree.traversal(BinaryTree.INORDER); + Assert.assertArrayEquals(inorderDatas, new int[]{1, 2, 3, 4, 5, 7, 9, 9, 10, 17}); + int[] postorderDatas = binaryTree.traversal(BinaryTree.POSTORDER); + Assert.assertArrayEquals(postorderDatas, new int[]{3, 2, 1, 7, 5, 4, 9, 17, 10, 9}); + int[] hierarchicalDatas = binaryTree.traversal(BinaryTree.HIERARCHICAL); + Assert.assertArrayEquals(hierarchicalDatas, new int[]{9, 4, 10, 1, 5, 9, 17, 2, 7, 3}); + } + + @Test + public void testSearch() { + BinaryTree.BinaryTreeNode node1 = binaryTree.search(5, BinaryTree.RECURSION); + Assert.assertTrue(node1.getData() == 5); + BinaryTree.BinaryTreeNode node2 = binaryTree.search(17, BinaryTree.RECURSION); + Assert.assertTrue(node2.getData() == 17); + BinaryTree.BinaryTreeNode node3 = binaryTree.search(100, BinaryTree.RECURSION); + Assert.assertTrue(node3 == null); + } + + @Test + public void testMin() { + BinaryTree.BinaryTreeNode min = binaryTree.min(); + Assert.assertTrue(min.getData() == 1); + } + + @Test + public void testMax() { + BinaryTree.BinaryTreeNode max = binaryTree.max(); + Assert.assertTrue(max.getData() == 17); + } + + @Test + public void testDelete() { + buildTree(new int[]{50, 25, 12, 11, 40, 14, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + // 删除叶子节点 + binaryTree.delete(11); + int[] preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + binaryTree.delete(88); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + + // 删除一个子节点的节点 + binaryTree.delete(70); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + binaryTree.delete(80); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + + // 删除两个子节点的节点 + binaryTree.delete(40); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + binaryTree.delete(50); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + } + + private void buildTree(int[] datas) { + binaryTree = new BinaryTree(); + for (int data : datas) { + binaryTree.add(data); + } + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java new file mode 100644 index 0000000000..b690c69c94 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java @@ -0,0 +1,81 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Iterator; +import com.aaront.exercise.basic.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class LinkListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void init() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + } + + @Test + public void testAdd() { + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testAddIndex() { + linkedList.add(1, 10); + linkedList.add(0, 8); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 1, 10, 2, 3}); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(-1); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{-1, 1, 2, 3}); + } + + @Test + public void testAddLast() { + linkedList.addLast(99); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3, 99}); + } + + @Test + public void testRemove() { + testAddIndex(); + linkedList.remove(1); + linkedList.remove(2); + linkedList.add(3, 3); + linkedList.add(1, 2); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 2, 10, 3, 3}); + } + + @Test + public void testRemoveFirst() { + linkedList.removeFirst(); + linkedList.removeFirst(); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{3}); + } + + @Test + public void testRemoveLast() { + linkedList.removeLast(); + linkedList.removeLast(); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1}); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java new file mode 100644 index 0000000000..0035a353ec --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Queue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class QueueTest { + private Queue queue = new Queue(); + + @Before + public void init() { + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + } + + @Test + public void testEnqueue() { + Assert.assertArrayEquals(queue.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testDequeue() { + queue.deQueue(); + queue.deQueue(); + Assert.assertArrayEquals(queue.toArray(), new Object[]{3}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java new file mode 100644 index 0000000000..3add6bcfdf --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java @@ -0,0 +1,46 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Stack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class StackTest { + + private Stack stack = new Stack(); + + @Before + public void init() { + stack.push(1); + stack.push(2); + stack.push(3); + } + + @Test + public void testPush() { + Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testPop() { + Object element1 = stack.pop(); + Assert.assertEquals(element1, 3); + Object element2 = stack.pop(); + Assert.assertEquals(element2, 2); + Assert.assertArrayEquals(stack.toArray(), new Object[]{1}); + } + + @Test + public void testPeek() { + Object element1 = stack.peek(); + Assert.assertEquals(element1, 3); + Object element2 = stack.peek(); + Assert.assertEquals(element2, 3); + Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + } + +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java new file mode 100644 index 0000000000..66c071cf5c --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java @@ -0,0 +1,19 @@ +package com.aaront.execrise.generic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author tonyhui + * @since 17/2/22 + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + GenericArrayListTest.class, + GenericLinkedListTest.class, + GenericQueueTest.class, + GenericStackTest.class, + GenericBinaryTreeTest.class +}) +public class GenericAllTest { +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java new file mode 100644 index 0000000000..8f97cbd3ea --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java @@ -0,0 +1,76 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericArrayList; +import com.aaront.exercise.generic.GenericIterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/22 + */ +public class GenericArrayListTest { + + private GenericArrayList arrayList = new GenericArrayList<>(); + + @Before + public void init() { + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + } + + + @Test + public void testAdd() { + Assert.assertEquals(arrayList.get(0), "1"); + Assert.assertEquals(arrayList.get(1), "2"); + Assert.assertEquals(arrayList.get(2), "3"); + Assert.assertEquals(arrayList.size(), 3); + } + + @Test + public void testAddIndex() { + arrayList.add(1, "4"); + arrayList.add(2, "5"); + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "5", "2", "3"}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testToGenericArray() { + Assert.assertArrayEquals(arrayList.toArray(new String[0]), new String[]{"1", "2", "3"}); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(2), "3"); + Assert.assertEquals(arrayList.get(0), "1"); + Assert.assertEquals(arrayList.get(1), "2"); + } + + @Test + public void testRemove() { + testAddIndex(); + arrayList.remove(2); + arrayList.add(4, "10"); + arrayList.add(3, "9"); + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "2", "9", "3", "10"}); + } + + @Test + public void testIterator() { + GenericIterator genericIterator = arrayList.iterator(); + while (genericIterator.hasNext()) { + genericIterator.next(); + genericIterator.remove(); + } + Assert.assertArrayEquals(arrayList.toArray(), new String[]{}); + } + +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java new file mode 100644 index 0000000000..41adbf6706 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java @@ -0,0 +1,75 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericBinaryTree; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class GenericBinaryTreeTest { + + @Before + public void init() { + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (String data : datas) { + binaryTree.add(data); + } + } + + @Test + public void testAdd() { + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (String data : datas) { + binaryTree.add(data); + } + String[] preorderDatas = binaryTree.traversal(GenericBinaryTree.PREORDER, new String[0]); + Assert.assertArrayEquals(preorderDatas, new String[]{"9", "4", "1", "2", "10", "17", "3", "5", "7", "9" }); + String[] inorderDatas = binaryTree.traversal(GenericBinaryTree.INORDER, new String[0]); + Assert.assertArrayEquals(inorderDatas, new String[]{"1", "10", "17", "2", "3", "4", "5", "7", "9", "9" }); + String[] postorderDatas = binaryTree.traversal(GenericBinaryTree.POSTORDER, new String[0]); + Assert.assertArrayEquals(postorderDatas, new String[]{"17", "10", "3", "2", "1", "7", "5", "4", "9", "9" }); + String[] hierarchicalDatas = binaryTree.traversal(GenericBinaryTree.HIERARCHICAL, new String[0]); + Assert.assertArrayEquals(hierarchicalDatas, new String[]{"9", "4", "9", "1", "5", "2", "7", "10", "3", "17" }); + } + + @Test + public void testDelete() { + GenericBinaryTree binaryTree = buildTree(new int[]{50, 25, 12, 11, 40, 14, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + // 删除叶子节点 + binaryTree.delete(11); + Object[] preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + binaryTree.delete(88); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + + // 删除一个子节点的节点 + binaryTree.delete(70); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + binaryTree.delete(80); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + + // 删除两个子节点的节点 + binaryTree.delete(40); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + binaryTree.delete(50); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + } + + private GenericBinaryTree buildTree(int[] datas) { + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (int data : datas) { + binaryTree.add(data); + } + return binaryTree; + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java new file mode 100644 index 0000000000..513119fa6e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java @@ -0,0 +1,90 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericLinkedList; +import com.aaront.exercise.generic.GenericIterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/22 + */ +public class GenericLinkedListTest { + private GenericLinkedList linkedList = new GenericLinkedList<>(); + + @Before + public void init() { + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + } + + @Test + public void testAdd() { + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testAddIndex() { + linkedList.add(1, "10"); + linkedList.add(0, "8"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "1", "10", "2", "3"}); + } + + @Test + public void testAddFirst() { + linkedList.addFirst("-1"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"-1", "1", "2", "3"}); + } + + @Test + public void testAddLast() { + linkedList.addLast("99"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3", "99"}); + } + + @Test + public void testRemove() { + testAddIndex(); + linkedList.remove(1); + linkedList.remove(2); + linkedList.add(3, "3"); + linkedList.add(1, "2"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "2", "10", "3", "3"}); + } + + @Test + public void testRemoveFirst() { + linkedList.removeFirst(); + linkedList.removeFirst(); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"3"}); + } + + @Test + public void testRemoveLast() { + linkedList.removeLast(); + linkedList.removeLast(); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1"}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testToGenericArray() { + Assert.assertArrayEquals(linkedList.toArray(new String[0]), new String[]{"1", "2", "3"}); + } + + @Test + public void testIterator() { + GenericIterator genericIterator = linkedList.iterator(); + while (genericIterator.hasNext()) { + genericIterator.next(); + genericIterator.remove(); + } + Assert.assertArrayEquals(linkedList.toArray(), new String[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java new file mode 100644 index 0000000000..6b33a4b3e0 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java @@ -0,0 +1,33 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericQueue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class GenericQueueTest { + private GenericQueue queue = new GenericQueue<>(); + + @Before + public void init() { + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + } + + @Test + public void testEnqueue() { + Assert.assertArrayEquals(queue.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testDequeue() { + queue.deQueue(); + queue.deQueue(); + Assert.assertArrayEquals(queue.toArray(), new String[]{"3"}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java new file mode 100644 index 0000000000..0b4b587704 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java @@ -0,0 +1,46 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericStack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class GenericStackTest { + + private GenericStack stack = new GenericStack<>(); + + @Before + public void init() { + stack.push("1"); + stack.push("2"); + stack.push("3"); + } + + @Test + public void testPush() { + Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testPop() { + String element1 = stack.pop(); + Assert.assertEquals(element1, "3"); + String element2 = stack.pop(); + Assert.assertEquals(element2, "2"); + Assert.assertArrayEquals(stack.toArray(), new String[]{"1"}); + } + + @Test + public void testPeek() { + String element1 = stack.peek(); + Assert.assertEquals(element1, "3"); + String element2 = stack.peek(); + Assert.assertEquals(element2, "3"); + Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + } + +} From 41057016ea7a29fbc746ad429396c4a1fd263ddc Mon Sep 17 00:00:00 2001 From: JayXu Date: Thu, 23 Feb 2017 13:54:26 +0800 Subject: [PATCH 004/419] xuweijay --- .../src/com/coding/basic/ArrayList.java | 66 ++++++++ .../com/coding/basic/BinarySearchTree.java | 130 +++++++++++++++ .../coding/basic/BinarySearchTreeNode.java | 36 +++++ .../coding/basic/BinarySearchTreeTest.java | 83 ++++++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/LinkedList.java | 150 ++++++++++++++++++ .../src/com/coding/basic/List.java | 12 ++ .../src/com/coding/basic/Queue.java | 36 +++++ .../src/com/coding/basic/Stack.java | 38 +++++ 9 files changed, 557 insertions(+) create mode 100644 group15/1511_714512544/src/com/coding/basic/ArrayList.java create mode 100644 group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java create mode 100644 group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java create mode 100644 group15/1511_714512544/src/com/coding/basic/BinarySearchTreeTest.java create mode 100644 group15/1511_714512544/src/com/coding/basic/Iterator.java create mode 100644 group15/1511_714512544/src/com/coding/basic/LinkedList.java create mode 100644 group15/1511_714512544/src/com/coding/basic/List.java create mode 100644 group15/1511_714512544/src/com/coding/basic/Queue.java create mode 100644 group15/1511_714512544/src/com/coding/basic/Stack.java diff --git a/group15/1511_714512544/src/com/coding/basic/ArrayList.java b/group15/1511_714512544/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3bc92837d1 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/ArrayList.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + private int size = 0; //表内元素个数 + private Object[] elementData = new Object[10]; //数据结构--数组 + + public void add(Object o){ //在后面追加 + if(size == elementData.length) elementData = grow(elementData, 2*elementData.length); //先扩容 + elementData[size++] = o; + } + + public void add(int index, Object o){ //在指定位置插入 + if(size == elementData.length) elementData = grow(elementData, 2*elementData.length); //先扩容 + if(index > size || index < 0) throw new RuntimeException("索引越界"); //保证最后一个元素后面也可以插 + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index] = o; + size ++; + } + + public Object get(int index){ //返回指定索引的元素 + if(index > size - 1 || index < 0) throw new RuntimeException("索引越界"); + return elementData[index]; + } + + public Object remove(int index){ //删除指定索引处的元素 + if(index > size - 1 || index < 0) throw new RuntimeException("索引越界"); + Object o = elementData[index]; + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + elementData[size-1] = null; + size --; + return o; + } + + public int size(){ //元素多少 + return size; + } + + public Iterator iterator(){ //迭代器 + return new ListIterator(); + } + + private class ListIterator implements Iterator{ //实例内部类 + private int i = 0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + if(size() == 0) throw new NoSuchElementException("堆栈下溢"); + return elementData[i++]; + } + } + + //数组扩容 + private Object[] grow(Object[] src, int newLength){ + assert src.length < newLength; //断言 + return Arrays.copyOf(src,newLength); + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java new file mode 100644 index 0000000000..b875c82b5c --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java @@ -0,0 +1,130 @@ +package com.coding.basic; + +/** + * 二叉树 + 5 + / \ + 2 7 + / \ /\ +1 4 6 8 + + 1.前序遍历: 5 2 1 4 7 6 8 + 2.中序遍历: 1 2 4 5 6 7 8 + 3.后序遍历: 1 4 2 6 8 7 5 + */ +public class BinarySearchTree> { + private BinarySearchTreeNode root; //根节点 + + public BinarySearchTreeNode getRoot() { + return root; + } + + public void setRoot(BinarySearchTreeNode root) { + this.root = root; + } + + /** + * 二叉树插入节点 + * @param data 节点元素 + * @return 插入的节点 + */ + public BinarySearchTreeNode insert(T data){ + if(root == null){ + root = new BinarySearchTreeNode(data); + return root; + } + //root非空 + BinarySearchTreeNode current = root; + while(true){ + //当前节点的数据小于data + if(current.getData().compareTo(data) >= 0){ + if(current.getLeft() != null){ + current = current.getLeft(); + }else { + current.setLeft(new BinarySearchTreeNode(data)); + return current.getLeft(); + } + }else {//当前节点数据大于root + if(current.getRight() != null){ + current = current.getRight(); + }else { + current.setRight(new BinarySearchTreeNode(data)); + return current.getRight(); + } + } + } + } + + /** + * 查找元素data + * @param data 要查找的元素 + * @return 返回true找到、false未找到 + */ + public boolean contains(T data){ + if(root == null){ + return false; + } + BinarySearchTreeNode current = root; + while(true){ + if(current.getData().compareTo(data) > 0){ + if(current.getLeft() != null){ + current = current.getLeft(); + }else{ + return false; + } + }else if(current.getData().compareTo(data) < 0){ + if(current.getRight() != null){ + current = current.getRight(); + }else { + return false; + } + }else { + return true; + } + } + } + + /** + * 前序遍历递归实现 + * @param n 根节点 + */ + public void preOrder(BinarySearchTreeNode n){ + System.out.print(n.getData()+" "); + if(n.getLeft() != null){ + preOrder(n.getLeft()); + } + if(n.getRight() != null){ + preOrder(n.getRight()); + } + } + + /** + * 中序遍历递归实现 + * @param n 根节点 + */ + public void midOrder(BinarySearchTreeNode n){ + if(n.getLeft() != null){ + midOrder(n.getLeft()); + } + System.out.print(n.getData()+" "); + if(n.getRight() != null){ + midOrder(n.getRight()); + } + } + + /** + * 后序遍历递归实现 + * @param n + */ + public void postOrder(BinarySearchTreeNode n){ + if(n.getLeft() != null){ + postOrder(n.getLeft()); + } + if(n.getRight() != null){ + postOrder(n.getRight()); + } + System.out.print(n.getData()+" "); + } + + +} diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java new file mode 100644 index 0000000000..68ca1c2d9b --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +/** + * 二叉树结点 + */ +public class BinarySearchTreeNode{ + private T data; + private BinarySearchTreeNode left; + private BinarySearchTreeNode right; + + public BinarySearchTreeNode(T data) { + this.data = data; + this.left = null; + this.right = null; + } + + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinarySearchTreeNode getLeft() { + return left; + } + public void setLeft(BinarySearchTreeNode left) { + this.left = left; + } + public BinarySearchTreeNode getRight() { + return right; + } + public void setRight(BinarySearchTreeNode right) { + this.right = right; + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeTest.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeTest.java new file mode 100644 index 0000000000..2da60ab332 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeTest.java @@ -0,0 +1,83 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class BinarySearchTreeTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + } + + @Test + public void testContains() { + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + System.out.println(bst.contains(8)); + } + + @Test + public void testPreOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.preOrder(bst.getRoot()); + } + + @Test + public void testMidOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.midOrder(bst.getRoot()); + } + + @Test + public void testPostOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.postOrder(bst.getRoot()); + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/Iterator.java b/group15/1511_714512544/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..b8c8e0ce6a --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group15/1511_714512544/src/com/coding/basic/LinkedList.java b/group15/1511_714512544/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3cc3769421 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/LinkedList.java @@ -0,0 +1,150 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; //数据结构,首节点 + private int size; //节点个数 + + public void add(Object o){ //在链表尾部添加node + if(head == null){ + head = new Node(o, null); + }else { + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = new Node(o, null); + } + size ++; + } + + public void add(int index , Object o){ //在指定索引处插入node + if(index > size || index < 0) throw new RuntimeException("索引越界"); + if(head == null){ + head = new Node(o, null); + }else { + if(index == 0){ //插入位置在头部 + head = new Node(o, head); + }else { //后面位置 + int i = 0; + Node temp = head; + while(i != index - 1){ + i ++; + temp = temp.next; + } + Node tempNext = temp.next; + temp.next = new Node(o, tempNext); + } + } + size ++; + } + + public Object get(int index){ //取出指定节点处的元素 + if(index > size -1 || index < 0) throw new RuntimeException("索引越界"); + int i = 0; + Node temp = head; + while(i != index){ + i ++; + temp = temp.next; + } + return temp.data; + } + + public Object remove(int index){ //删除指定索引处的节点 + if(index > size -1 || index < 0) throw new RuntimeException("索引越界"); + if(index == 0) { //第一个元素或只有一个元素 + Object o = head.data; + head = head.next; + size --; + return o; + }else { //其他元素 + int i = 0; + Node temp = head; + while(i != index - 1){ + i ++; + temp = temp.next; + } + Node delete = temp.next; + Object o = delete.data; + temp.next = delete.next; + delete = null; + size --; + return o; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ //在表头添加节点 + head = new Node(o, head); + size ++; + } + + public void addLast(Object o){ //在链表尾部添加节点 + if(head == null){ + head = new Node(o,null); + size ++; + return; + } + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = new Node(o, null); + size ++; + } + + public Object removeFirst(){ //在链表头部删除节点 + if(size() == 0) throw new RuntimeException("堆栈下溢"); + Object o = head.data; + head = head.next; + size --; + return o; + } + + public Object removeLast(){ //在链表尾部删除节点 + if(size() == 0) throw new RuntimeException("堆栈下溢"); + Node last = head; + while(last.next != null){ + last = last.next; + } + Object o = last.data; + size --; + last = null; + return o; + } + + public Iterator iterator(){ //迭代器 + return new ListIterator(); + } + + private class ListIterator implements Iterator{ //实例内部类 + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Object next() { + if(size() == 0) throw new NoSuchElementException("堆栈下溢"); + Object o = current.data; + current = current.next; + return o; + } + } + + //这里内部类必须为static,这里的每个LinkedList对象都对应唯一的class Node,在类级别上一一对应,非实例级别 + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group15/1511_714512544/src/com/coding/basic/List.java b/group15/1511_714512544/src/com/coding/basic/List.java new file mode 100644 index 0000000000..ed7729f25b --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/List.java @@ -0,0 +1,12 @@ +package com.coding.basic; + +/** + * 随机访问 + */ +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); +} diff --git a/group15/1511_714512544/src/com/coding/basic/Queue.java b/group15/1511_714512544/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..d362906bb5 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Queue.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 队列,先进先出 + */ +public class Queue { + private LinkedList list = new LinkedList(); //自己选取数据结构 + + //入队 + public void enQueue(Object o){ + list.addLast(o); + } + + //出队 + public Object deQueue(){ + if(list.size() == 0) throw new NoSuchElementException("队列无元素"); + return list.removeFirst(); + } + + //是否为空 + public boolean isEmpty(){ + return list.size() == 0; + } + + //队列内元素 + public int size(){ + return list.size(); + } + //迭代器 + public Iterator iterator(){ + return list.iterator(); + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/Stack.java b/group15/1511_714512544/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b904cb288c --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; +//后进先出 +public class Stack { + private ArrayList elementData = new ArrayList(); //使用刚实现的ArrayList + + //入栈 + public void push(Object o){ + elementData.add(o); + } + + //出栈 + public Object pop(){ + if(elementData.size() == 0) throw new NoSuchElementException("堆栈下溢"); + return elementData.remove(elementData.size()-1); + } + + //栈顶元素 + public Object peek(){ + if(elementData.size() == 0) throw new NoSuchElementException("堆栈下溢"); + return elementData.get(elementData.size()-1); + } + + //是否为空 + public boolean isEmpty(){ + return elementData.size() == 0; + } + + //栈内元素个数 + public int size(){ + return elementData.size(); + } + + + + +} From 0a5e381474e3edf8ef167a7748988bab7c839ab8 Mon Sep 17 00:00:00 2001 From: JayXu Date: Thu, 23 Feb 2017 13:57:12 +0800 Subject: [PATCH 005/419] xuweijay --- .../1511_714512544/.idea/checkstyle-idea.xml | 10 ++ group15/1511_714512544/.idea/encodings.xml | 6 + group15/1511_714512544/.idea/misc.xml | 15 +++ group15/1511_714512544/.idea/modules.xml | 8 ++ group15/1511_714512544/.idea/vcs.xml | 6 + group15/1511_714512544/1511_714512544.iml | 20 ++++ .../coding/basic/BinarySearchTreeTest.java | 103 ++++++++++++++++++ 7 files changed, 168 insertions(+) create mode 100644 group15/1511_714512544/.idea/checkstyle-idea.xml create mode 100644 group15/1511_714512544/.idea/encodings.xml create mode 100644 group15/1511_714512544/.idea/misc.xml create mode 100644 group15/1511_714512544/.idea/modules.xml create mode 100644 group15/1511_714512544/.idea/vcs.xml create mode 100644 group15/1511_714512544/1511_714512544.iml create mode 100644 group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java diff --git a/group15/1511_714512544/.idea/checkstyle-idea.xml b/group15/1511_714512544/.idea/checkstyle-idea.xml new file mode 100644 index 0000000000..9d5b48d873 --- /dev/null +++ b/group15/1511_714512544/.idea/checkstyle-idea.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/encodings.xml b/group15/1511_714512544/.idea/encodings.xml new file mode 100644 index 0000000000..cfca28230a --- /dev/null +++ b/group15/1511_714512544/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/misc.xml b/group15/1511_714512544/.idea/misc.xml new file mode 100644 index 0000000000..f45ffd5f26 --- /dev/null +++ b/group15/1511_714512544/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/modules.xml b/group15/1511_714512544/.idea/modules.xml new file mode 100644 index 0000000000..0ed960e3bf --- /dev/null +++ b/group15/1511_714512544/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/vcs.xml b/group15/1511_714512544/.idea/vcs.xml new file mode 100644 index 0000000000..b2bdec2d71 --- /dev/null +++ b/group15/1511_714512544/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/1511_714512544.iml b/group15/1511_714512544/1511_714512544.iml new file mode 100644 index 0000000000..3ab15b1867 --- /dev/null +++ b/group15/1511_714512544/1511_714512544.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java b/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java new file mode 100644 index 0000000000..6181413860 --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java @@ -0,0 +1,103 @@ +package test.com.coding.basic; + +import com.coding.basic.BinarySearchTree; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** +* BinarySearchTree Tester. +* +* @author +* @since
 23, 2017
+* @version 1.0 +*/ +public class BinarySearchTreeTest { + +@Before +public void before() throws Exception { +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: getRoot() +* +*/ +@Test +public void testGetRoot() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: setRoot(BinarySearchTreeNode root) +* +*/ +@Test +public void testSetRoot() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: insert(T data) +* +*/ +@Test +public void testInsert() throws Exception { + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); +} + +/** +* +* Method: contains(T data) +* +*/ +@Test +public void testContains() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: preOrder(BinarySearchTreeNode n) +* +*/ +@Test +public void testPreOrder() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: midOrder(BinarySearchTreeNode n) +* +*/ +@Test +public void testMidOrder() throws Exception { +//TODO: Test goes here... +} + +/** +* +* Method: postOrder(BinarySearchTreeNode n) +* +*/ +@Test +public void testPostOrder() throws Exception { +//TODO: Test goes here... +} + + +} From b8d284ea10ac280ec72450e9e28336757c0cef30 Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Thu, 23 Feb 2017 14:04:59 +0800 Subject: [PATCH 006/419] first load.... --- .../src/com/coding/basic/ArrayList.java | 32 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++++++++++ .../src/com/coding/basic/Iterator.java | 7 +++ .../src/com/coding/basic/LinkedList.java | 46 +++++++++++++++++++ .../src/com/coding/basic/List.java | 9 ++++ .../src/com/coding/basic/Queue.java | 19 ++++++++ .../src/com/coding/basic/Stack.java | 22 +++++++++ 7 files changed, 167 insertions(+) create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/Iterator.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/List.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/Queue.java create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/Stack.java diff --git a/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..57412dcf7f --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.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/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java b/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..1fd99bf26b --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/List.java b/group03/345943980/2017Learning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.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/group03/345943980/2017Learning/src/com/coding/basic/Queue.java b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..08d2d86b14 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Stack.java b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4bfe28057f --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From be853d34d2135a838bd8db390eff49ff74f641bb Mon Sep 17 00:00:00 2001 From: chzh88 Date: Thu, 23 Feb 2017 14:24:07 +0800 Subject: [PATCH 007/419] test --- .gitignore | 9 +++++++++ group19/2558178127/.gitignore | 9 +++++++++ group19/2558178127/src/com/cn/kevin/Test.java | 5 +++++ 3 files changed, 23 insertions(+) create mode 100644 group19/2558178127/.gitignore create mode 100644 group19/2558178127/src/com/cn/kevin/Test.java diff --git a/.gitignore b/.gitignore index ec55baf87d..fcfd185b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,12 @@ hs_err_pid* #ide config .metadata .recommenders + +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* + diff --git a/group19/2558178127/.gitignore b/group19/2558178127/.gitignore new file mode 100644 index 0000000000..ee46440140 --- /dev/null +++ b/group19/2558178127/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* diff --git a/group19/2558178127/src/com/cn/kevin/Test.java b/group19/2558178127/src/com/cn/kevin/Test.java new file mode 100644 index 0000000000..f5a6ce104a --- /dev/null +++ b/group19/2558178127/src/com/cn/kevin/Test.java @@ -0,0 +1,5 @@ +package com.cn.kevin; + +public class Test { + +} From 2a62e4b39dc7b1743ee24536c54f5ef53e33e881 Mon Sep 17 00:00:00 2001 From: byhieg Date: Thu, 23 Feb 2017 14:49:08 +0800 Subject: [PATCH 008/419] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/ArrayList.java" | 102 ++++++++++++ .../src/BinaryTree.java" | 66 ++++++++ .../src/BinaryTreeNode.java" | 42 +++++ .../src/Iterator.java" | 7 + .../src/LinkedList.java" | 152 ++++++++++++++++++ .../src/List.java" | 13 ++ .../src/Queue.java" | 23 +++ .../src/Stack.java" | 36 +++++ .../test/ArrayListTest.java" | 81 ++++++++++ .../test/BinaryTreeTest.java" | 24 +++ .../test/LinkedListTest.java" | 122 ++++++++++++++ .../test/QueueTest.java" | 34 ++++ .../test/StackTest.java" | 52 ++++++ group03/1196051822/README | 2 + 14 files changed, 756 insertions(+) create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" create mode 100644 "group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" create mode 100644 group03/1196051822/README diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" new file mode 100644 index 0000000000..4a68cd276b --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" @@ -0,0 +1,102 @@ +package com.byhieg.coding2017; + +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) { + isCapacityEnough(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForAdd(index); + isCapacityEnough(size + 1); + System.arraycopy(elementData,index,elementData,index + 1,size - index); + elementData[index] = o; + size++; + } + + private void checkForAdd(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index不在指定范围内"); + } + + } + private void isCapacityEnough(int size) { + if (size > 100) { + explicitCapacity(size); + } + if (size < 0) { + throw new OutOfMemoryError(); + } + } + + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; + + public void explicitCapacity(int size) { + int newLength = elementData.length * 2; + if (newLength > (MAX_ARRAY_LENGTH)){ + newLength = (size > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + elementData = Arrays.copyOf(elementData, newLength); + + } + + + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + private void checkRange(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index不在范围内"); + } + } + + public Object remove(int index) { + Object o = get(index); + //要保证后面的 index + 1是有效的 + int moveSize = size - index - 1; + if (moveSize > 0) { + System.arraycopy(elementData,index + 1,elementData,index, size - index); + } + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if (cursor >= size) { + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + } + + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" new file mode 100644 index 0000000000..26407d749a --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" @@ -0,0 +1,66 @@ +package com.byhieg.coding2017; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ + +public class BinaryTree { + + private BinaryTreeNode root = new BinaryTreeNode(); + + public BinaryTree(Object rootData){ + root = root.insert(rootData); + } + + + //左边的值小于等于父节点的值,右边的值大于父节点的值 + private void insertNode(BinaryTreeNode root, BinaryTreeNode node) { + int value = (int)node.getData(); + int rootValue = (int)root.getData(); + if (value <= rootValue){ + insertLeft(root,node); + }else { + insertRight(root,node); + } + } + + + public void insert(Object o) { + BinaryTreeNode node = new BinaryTreeNode(); + node = node.insert(o); + insertNode(root,node); + } + + private void insertLeft(BinaryTreeNode father, BinaryTreeNode node) { + if (father.getLeft() == null) { + father.setLeft(node); + }else{ + insertNode(father.getLeft(),node); + } + } + + private void insertRight(BinaryTreeNode father, BinaryTreeNode node) { + if (father.getRight() == null) { + father.setRight(node); + } else { + insertNode(father.getRight(),node); + } + } + + //前序遍历输出书 + private void preOrder(BinaryTreeNode node) { + if (node != null) { + System.out.println(node.getData()); + preOrder(node.getLeft()); + preOrder(node.getRight()); + } + } + + + //打印树 + public void printTree(){ + preOrder(root); + } + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" new file mode 100644 index 0000000000..67f70bb696 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" @@ -0,0 +1,42 @@ +package com.byhieg.coding2017; + +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) { + BinaryTreeNode node = new BinaryTreeNode(); + int value = (int)o; + node.setData(value); + node.setRight(null); + node.setLeft(null); + return node; + } + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" new file mode 100644 index 0000000000..beef5b5554 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" @@ -0,0 +1,7 @@ +package com.byhieg.coding2017; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" new file mode 100644 index 0000000000..04b3f9f027 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" @@ -0,0 +1,152 @@ +package com.byhieg.coding2017; + +import javax.swing.text.html.HTMLDocument; + +public class LinkedList implements List { + + private Node head; + int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkRangeForAdd(index); + if (index == size) { + addLast(o); + } + Node nextNode = node(index); + Node newNode = new Node(o, nextNode); + + Node prevNode; + if (index == 0) { + prevNode = null; + } else { + prevNode = node(index); + } + + + if (prevNode == null) { + head = newNode; + }else{ + prevNode.next = newNode; + } + + size++; + } + + + private Node node(int index) { + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } + + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("指定的index超过界限"); + } + } + + public Object get(int index) { + checkRange(index); + return node(index).data; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("指定index超过界限"); + } + } + + public Object remove(int index) { + checkRange(index); + Node targetNode = node(index); + Object o = targetNode.data; + Node prevNode ; + Node nextNode = targetNode.next; + + if (index == 0) { + prevNode = null; + }else{ + prevNode = node(index - 1); + } + if (prevNode == null) { + head = nextNode; + targetNode.next = null; + }else { + prevNode.next = nextNode; + targetNode.next = null; + } + + targetNode.data = null; + size --; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node nextNode = head; + Node newNode = new Node(o, nextNode); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (size == 0) { + head = newNode; + }else{ + Node lastNode = node(size - 1); + lastNode.next = newNode; + } + size++; + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size() - 1); + } + + public Iterator iterator() { + + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + public Node cursor = head; + @Override + public boolean hasNext() { + return cursor != null; + } + + @Override + public Object next() { + Object o = cursor.data; + cursor = cursor.next; + return o; + } + } + + + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" new file mode 100644 index 0000000000..15c27c8cf2 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" @@ -0,0 +1,13 @@ +package com.byhieg.coding2017; + +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/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" new file mode 100644 index 0000000000..f83ad337e7 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" @@ -0,0 +1,23 @@ +package com.byhieg.coding2017; + +public class Queue { + + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue() { + Object value = list.get(0); + list.removeFirst(); + return value; + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" new file mode 100644 index 0000000000..45b8530a8f --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" @@ -0,0 +1,36 @@ +package com.byhieg.coding2017; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (size() == 0) { + throw new EmptyStackException(); + } + Object value = elementData.get(size() - 1); + elementData.remove(size() - 1); + return value; + } + + public Object peek(){ + if (size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" new file mode 100644 index 0000000000..b0b3b6704d --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" @@ -0,0 +1,81 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.ArrayList; +import com.byhieg.coding2017.Iterator; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class ArrayListTest extends TestCase { + ArrayList arrayList = new ArrayList(); + + public void testAdd() throws Exception { + arrayList.add(1); + arrayList.add(null); + arrayList.add(-1); + arrayList.add("1"); + arrayList.add(true); + arrayList.add(Integer.MAX_VALUE); + arrayList.add(Integer.MIN_VALUE); + + + } + + public void testAdd1() throws Exception { +// arrayList.add(-1,0); +// arrayList.add(100,0); + arrayList.add(0,2); + arrayList.add(1,10); + arrayList.add(2,111); + } + + public void testGet() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + for (int i = 0 ; i < 10 ; i++) { + System.out.println(arrayList.get(i)); + } + } + + public void testRemove() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + for (int i = 0 ; i < 10 ; i++) { + System.out.println(arrayList.get(i)); + } + + for (int i = 0 ; i < 10 ; i++) { + arrayList.remove(9 - i); + } + + for (int i = 0 ; i < arrayList.size() ; i++) { + System.out.println(arrayList.get(i)); + } + } + + public void testSize() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + System.out.println(arrayList.size()); + } + + public void testIterator() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + System.out.println("开始测试Iterator"); + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" new file mode 100644 index 0000000000..1945f2c695 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" @@ -0,0 +1,24 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.BinaryTree; +import com.byhieg.coding2017.BinaryTreeNode; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class BinaryTreeTest extends TestCase { + + public void testPrintTree() throws Exception { + BinaryTree tree = new BinaryTree(5); + tree.insert(2); + tree.insert(7); + tree.insert(1); + tree.insert(6); + tree.insert(4); + tree.insert(8); + tree.printTree(); + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" new file mode 100644 index 0000000000..61a78e150a --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" @@ -0,0 +1,122 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Iterator; +import com.byhieg.coding2017.LinkedList; +import com.sun.org.apache.bcel.internal.generic.INEG; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class LinkedListTest extends TestCase { + private LinkedList list = new LinkedList(); + public void testAdd() throws Exception { + list.add(null); + list.add(-1); + list.add(-2); + list.add(0x5); + list.add(true); + list.add("123"); + list.add(Integer.MAX_VALUE + 100000); + + } + + public void testAdd1() throws Exception { +// list.add(-1,100); +// list.add(20,111); + list.add(0,11); + list.add(1,"sad"); + list.add(2,"fas"); + + } + + public void testGet() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + list.add(i,i + ""); + } + + for (int i = 0 ;i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + public void testRemove() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + list.add(i,i + ""); + } + + for (int i = 0 ; i < list.size() ; i++) { + list.remove(i); + } + + for (int i = 0 ;i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + + public void testAddFirst() throws Exception { + list.addFirst("byhieg"); + list.addFirst("123412"); + list.addFirst("byhaieg"); + list.addFirst("byhfadas12ieg"); + list.addFirst("fas"); + for (int i = 0 ; i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + public void testAddLast() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + + for (int i = 0 ; i < list.size();i++) { + System.out.println(list.get(i)); + } + + } + + public void testRemoveFirst() throws Exception { + list.addFirst("byhieg"); + list.addFirst("123412"); + list.addFirst("byhaieg"); + list.addFirst("byhfadas12ieg"); + list.addFirst("fas"); + for (int i = 0 ; i < list.size();i++) { + list.removeLast(); + } + + System.out.println(list.size()); + } + + public void testRemoveLast() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + for (int i = 0 ; i < list.size();i++) { + list.removeFirst(); + } + + System.out.println(list.size()); + } + + public void testIterator() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" new file mode 100644 index 0000000000..82d0fe2349 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" @@ -0,0 +1,34 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Queue; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class QueueTest extends TestCase { + Queue queue = new Queue(); + + public void testEnQueue() throws Exception { + queue.enQueue(1); + queue.enQueue("true"); + queue.enQueue(true); + queue.enQueue(null); + queue.enQueue(-12341); + queue.enQueue(Integer.MIN_VALUE - 10000); + + } + + public void testDeQueue() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + queue.enQueue(i); + } + + while (!queue.isEmpty()) { + System.out.println(queue.deQueue()); + } + } + + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" new file mode 100644 index 0000000000..a81484251c --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" @@ -0,0 +1,52 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Stack; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class StackTest extends TestCase { + Stack stack = new Stack(); + + public void testPush() throws Exception { + stack.push(1); + stack.push("31231"); + stack.push(null); + stack.push(Integer.MAX_VALUE + 1000); + stack.push(Integer.MIN_VALUE - 1000000); + stack.push(true); + stack.push('a'); + } + + public void testPop() throws Exception { + int a = 1; + for (int i = 0; i < 10; i++) { + stack.push(a + i); + } + int size = stack.size(); + while (!stack.isEmpty()){ + System.out.println(stack.pop()); + } + } + + public void testPeek() throws Exception { + char a = 'a'; + for (int i = 0; i < 10; i++) { + stack.push(a + i); + } + + System.out.println("size的大小是" + stack.size()); + System.out.println(stack.peek()); + } + + public void testIsEmpty() throws Exception { + System.out.println(stack.isEmpty()); + stack.push(1); + System.out.println(stack.isEmpty()); + + } + + +} \ No newline at end of file diff --git a/group03/1196051822/README b/group03/1196051822/README new file mode 100644 index 0000000000..c7b21c2ef0 --- /dev/null +++ b/group03/1196051822/README @@ -0,0 +1,2 @@ +# 作业文件夹说明 +src文件夹存放是的作业源码的文件,test文件夹存放的是源码相应的测试文件 From 5d87cd6dd3a5ba741a51d1070d77bb3cd32ac855 Mon Sep 17 00:00:00 2001 From: jy97799 <977996067@qq.com> Date: Thu, 23 Feb 2017 15:22:11 +0800 Subject: [PATCH 009/419] =?UTF-8?q?3.26=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1507_977996067/src/task1/MyArrayList.java | 95 +++++++++++++ .../src/task1/MyLinkedList.java | 130 ++++++++++++++++++ group15/1507_977996067/src/task1/MyList.java | 16 +++ group15/1507_977996067/src/task1/MyQueue.java | 25 ++++ group15/1507_977996067/src/task1/MyStack.java | 29 ++++ group15/1507_977996067/src/task1/README.md | 1 + 6 files changed, 296 insertions(+) create mode 100644 group15/1507_977996067/src/task1/MyArrayList.java create mode 100644 group15/1507_977996067/src/task1/MyLinkedList.java create mode 100644 group15/1507_977996067/src/task1/MyList.java create mode 100644 group15/1507_977996067/src/task1/MyQueue.java create mode 100644 group15/1507_977996067/src/task1/MyStack.java create mode 100644 group15/1507_977996067/src/task1/README.md diff --git a/group15/1507_977996067/src/task1/MyArrayList.java b/group15/1507_977996067/src/task1/MyArrayList.java new file mode 100644 index 0000000000..e4d01a2c06 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyArrayList.java @@ -0,0 +1,95 @@ +package task1; + +import java.util.Arrays; +import java.util.Iterator; + +/** + * ArrayList实现 + */ +public class MyArrayList implements MyList { + + //列表元素的个数 + private int size; + //列表存放的元素 + private Object[] elements; + //初始容量10 + private static final int DEFAULT_CAPACITY = 10; + + public MyArrayList() { + elements = new Object[DEFAULT_CAPACITY]; + } + + public MyArrayList(int capacity) { + elements = new Object[capacity <= DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity]; + } + + @Override + public void add(T o) { + add(size, o); + } + + @Override + public void add(int index, T o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index " + index + " 不合法"); + if (size >= elements.length) + ensureCapacity((size >> 1) + size); + elements[index] = o; + size++; + } + + @Override + @SuppressWarnings("unchecked") + public T get(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + return (T) elements[index]; + } + + @Override + @SuppressWarnings("unchecked") + public T remove(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + T removeElement = (T) elements[index]; + System.arraycopy(elements, index + 1, elements, index, elements.length - index - 1); + size--; + return removeElement; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new MyItr(); + } + + private void ensureCapacity(int newCapacity) { + elements = Arrays.copyOf(elements, newCapacity); + } + + private class MyItr implements Iterator { + + private int current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) elements[current++]; + } + + @Override + public void remove() { + if (current == 0) + throw new IllegalStateException("先调用next后才能再调用remove"); + MyArrayList.this.remove(--current); + } + } +} diff --git a/group15/1507_977996067/src/task1/MyLinkedList.java b/group15/1507_977996067/src/task1/MyLinkedList.java new file mode 100644 index 0000000000..fa5d651078 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyLinkedList.java @@ -0,0 +1,130 @@ +package task1; + +import java.util.Iterator; + +/** + * LinkedList 实现 + */ +public class MyLinkedList implements MyList { + + //存放的元素数量 + private int size; + + private Node head; + + public MyLinkedList() { + head = new Node<>(null, null); + } + + @Override + public void add(T o) { + add(size, o); + } + + @Override + public void add(int index, T o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index " + index + " 不合法"); + Node targetNode = new Node<>(null, o); + Node targetPrevNode = getPrevNode(index); + targetNode.next = targetPrevNode.next; + targetPrevNode.next = targetNode; + size++; + } + + @Override + public T get(int index) { + checkIndexRange(index); + return getPrevNode(index).next.data; + } + + + @Override + public T remove(int index) { + checkIndexRange(index); + Node prevNode = getPrevNode(index); + Node nodeToRemove = prevNode.next; + prevNode.next = nodeToRemove.next; + size--; + return nodeToRemove.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(T o) { + add(0, o); + + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + + public Iterator iterator() { + return new MyLinkedItr(); + } + + /** + * 找到位置为index的前一个node + * + * @param index 索引值 + */ + + private Node getPrevNode(int index) { + Node targetPrevNode = head; + for (int i = 0; i < index; i++) { + targetPrevNode = targetPrevNode.next; + } + return targetPrevNode; + } + + /** + * 检查索引是否越界 + * + * @param index 索引值 + */ + private void checkIndexRange(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + } + + private static class Node { + private Node next; + private T data; + + private Node(Node next, T data) { + this.next = next; + this.data = data; + } + } + + private class MyLinkedItr implements Iterator { + + private Node currentNode = head; + + @Override + public boolean hasNext() { + return currentNode.next != null; + } + + @Override + public T next() { + Node nextNode = currentNode.next; + T data = nextNode.data; + currentNode = nextNode; + return data; + } + } +} diff --git a/group15/1507_977996067/src/task1/MyList.java b/group15/1507_977996067/src/task1/MyList.java new file mode 100644 index 0000000000..e68f445174 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyList.java @@ -0,0 +1,16 @@ +package task1; + +/** + * List 接口 + */ +public interface MyList { + public void add(T o); + + public void add(int index, T o); + + public T get(int index); + + public T remove(int index); + + public int size(); +} diff --git a/group15/1507_977996067/src/task1/MyQueue.java b/group15/1507_977996067/src/task1/MyQueue.java new file mode 100644 index 0000000000..2ae3d8529f --- /dev/null +++ b/group15/1507_977996067/src/task1/MyQueue.java @@ -0,0 +1,25 @@ +package task1; + +/** + * Queue 实现 + */ +public class MyQueue { + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(T o) { + elementData.addFirst(o); + } + + public T deQueue() { + return elementData.removeLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group15/1507_977996067/src/task1/MyStack.java b/group15/1507_977996067/src/task1/MyStack.java new file mode 100644 index 0000000000..476caf67e2 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyStack.java @@ -0,0 +1,29 @@ +package task1; + +/** + * Stack 实现 + */ +public class MyStack { + private MyLinkedList elementData = new MyLinkedList(); + + public void push(T o) { + elementData.addFirst(o); + } + + public T pop() { + return elementData.removeFirst(); + } + + public T peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group15/1507_977996067/src/task1/README.md b/group15/1507_977996067/src/task1/README.md new file mode 100644 index 0000000000..3f8eb94089 --- /dev/null +++ b/group15/1507_977996067/src/task1/README.md @@ -0,0 +1 @@ +###3.26作业:实现简单的数据结构 \ No newline at end of file From c6f9472d0cb37b5d37c1872835e13988244f5b4b Mon Sep 17 00:00:00 2001 From: jy97799 <977996067@qq.com> Date: Thu, 23 Feb 2017 15:24:21 +0800 Subject: [PATCH 010/419] =?UTF-8?q?3.26=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group15/1507_977996067/src/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 group15/1507_977996067/src/README.md diff --git a/group15/1507_977996067/src/README.md b/group15/1507_977996067/src/README.md new file mode 100644 index 0000000000..78a3f19258 --- /dev/null +++ b/group15/1507_977996067/src/README.md @@ -0,0 +1 @@ +### qq 977996067 的作业文件夹 \ No newline at end of file From ba075eb3e93e4445e9e29bd293c29df4d349c65c Mon Sep 17 00:00:00 2001 From: jy97799 <977996067@qq.com> Date: Thu, 23 Feb 2017 15:25:20 +0800 Subject: [PATCH 011/419] =?UTF-8?q?3.26=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group15/1507_977996067/{src => }/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename group15/1507_977996067/{src => }/README.md (100%) diff --git a/group15/1507_977996067/src/README.md b/group15/1507_977996067/README.md similarity index 100% rename from group15/1507_977996067/src/README.md rename to group15/1507_977996067/README.md From 3888ae8ece8fd7a47cfc8d91329286350e739be3 Mon Sep 17 00:00:00 2001 From: jy97799 <977996067@qq.com> Date: Thu, 23 Feb 2017 15:27:05 +0800 Subject: [PATCH 012/419] =?UTF-8?q?2.26=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group15/1507_977996067/src/task1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group15/1507_977996067/src/task1/README.md b/group15/1507_977996067/src/task1/README.md index 3f8eb94089..d012aef953 100644 --- a/group15/1507_977996067/src/task1/README.md +++ b/group15/1507_977996067/src/task1/README.md @@ -1 +1 @@ -###3.26作业:实现简单的数据结构 \ No newline at end of file +###2.26作业:实现简单的数据结构 \ No newline at end of file From 4550485e298ac7f53b6ce81e606d092569f043c6 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 23 Feb 2017 16:09:38 +0800 Subject: [PATCH 013/419] 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 11f922265f2532e7aad2b651894871ccb7617f03 Mon Sep 17 00:00:00 2001 From: JayXu Date: Thu, 23 Feb 2017 17:03:23 +0800 Subject: [PATCH 014/419] commit --- group15/1511_714512544/.idea/workspace.xml | 800 +++++++++++++++++++++ 1 file changed, 800 insertions(+) create mode 100644 group15/1511_714512544/.idea/workspace.xml diff --git a/group15/1511_714512544/.idea/workspace.xml b/group15/1511_714512544/.idea/workspace.xml new file mode 100644 index 0000000000..8df02d2252 --- /dev/null +++ b/group15/1511_714512544/.idea/workspace.xml @@ -0,0 +1,800 @@ + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + + + + + + + + + + + + + + + + + + 1487829138626 + + + 1487829266287 + + + 1487829432867 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + 1511_714512544 + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file From 9ad7f682401e165e8dd542db3943b041444e7037 Mon Sep 17 00:00:00 2001 From: yangdd Date: Thu, 23 Feb 2017 17:55:55 +0800 Subject: [PATCH 015/419] basic data structure --- group18/1049843090/.classpath | 10 + group18/1049843090/.gitignore | 7 + group18/1049843090/.project | 15 ++ .../src/com/coding/basic/ArrayList.java | 148 +++++++++++++ .../src/com/coding/basic/BinaryTree.java | 95 ++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 67 ++++++ .../src/com/coding/basic/Iterator.java | 26 +++ .../src/com/coding/basic/LinkedList.java | 205 ++++++++++++++++++ .../1049843090/src/com/coding/basic/List.java | 46 ++++ .../src/com/coding/basic/Queue.java | 58 +++++ .../src/com/coding/basic/Stack.java | 62 ++++++ .../test/com/coding/basic/ArrayListTest.java | 89 ++++++++ .../test/com/coding/basic/LinkedListTest.java | 118 ++++++++++ .../test/com/coding/basic/QueueTest.java | 61 ++++++ .../test/com/coding/basic/StackTest.java | 67 ++++++ 15 files changed, 1074 insertions(+) create mode 100644 group18/1049843090/.classpath create mode 100644 group18/1049843090/.gitignore create mode 100644 group18/1049843090/.project create mode 100644 group18/1049843090/src/com/coding/basic/ArrayList.java create mode 100644 group18/1049843090/src/com/coding/basic/BinaryTree.java create mode 100644 group18/1049843090/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group18/1049843090/src/com/coding/basic/Iterator.java create mode 100644 group18/1049843090/src/com/coding/basic/LinkedList.java create mode 100644 group18/1049843090/src/com/coding/basic/List.java create mode 100644 group18/1049843090/src/com/coding/basic/Queue.java create mode 100644 group18/1049843090/src/com/coding/basic/Stack.java create mode 100644 group18/1049843090/test/com/coding/basic/ArrayListTest.java create mode 100644 group18/1049843090/test/com/coding/basic/LinkedListTest.java create mode 100644 group18/1049843090/test/com/coding/basic/QueueTest.java create mode 100644 group18/1049843090/test/com/coding/basic/StackTest.java diff --git a/group18/1049843090/.classpath b/group18/1049843090/.classpath new file mode 100644 index 0000000000..68a547a96e --- /dev/null +++ b/group18/1049843090/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/group18/1049843090/.gitignore b/group18/1049843090/.gitignore new file mode 100644 index 0000000000..117a0b15d5 --- /dev/null +++ b/group18/1049843090/.gitignore @@ -0,0 +1,7 @@ +*.idea +*.iml +*.eml +.settings/ +target/ +build/ +out/ \ No newline at end of file diff --git a/group18/1049843090/.project b/group18/1049843090/.project new file mode 100644 index 0000000000..978233664c --- /dev/null +++ b/group18/1049843090/.project @@ -0,0 +1,15 @@ + + + 1049843090 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1049843090/src/com/coding/basic/ArrayList.java b/group18/1049843090/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f6b3007a7d --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/ArrayList.java @@ -0,0 +1,148 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * A Simple ArrayList + */ +public class ArrayList implements List { + + /** + * 当前list的元素个数 + */ + private int size; + + /** + * 默认数组大小 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 存储元素的数组 + */ + private Object[] elementData; + + /** + * 追加一个元素 + * + * @param e + */ + public void add(E e) { + grow(); + elementData[size++] = e; + } + + private void grow() { + if (elementData.length == size) { + elementData = Arrays.copyOf(elementData, size + 10); + } + } + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + grow(); + int movedNum = size - index; + if (movedNum > 0) { + System.arraycopy(elementData, index, elementData, index + 1, movedNum); + } + elementData[index] = e; + size++; + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return getElement(index); + } + + + /** + * 删除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + E delEle = getElement(index); + int movedNum = size - index - 1;//是不是最后一个元素 + if (movedNum > 0) { + System.arraycopy(elementData, index + 1, elementData, index, movedNum); + } + elementData[--size] = null; + return delEle; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + private E getElement(int index) { + return (E) elementData[index]; + } + + /** + * list中元素的个数 + * + * @return + */ + public int size() { + return this.size; + } + + + public ArrayList() { + this.elementData = new Object[DEFAULT_CAPACITY]; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor;//游标 + + private int lastRet = -1;//可被删除元素下标 + + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public E next() { + int i = cursor; + cursor++; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + cursor = lastRet;//游标等于当前删除元素的下标 或者 cursor--; + ArrayList.this.remove(lastRet); + lastRet = -1;//重置可删元素下标 + + } + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/BinaryTree.java b/group18/1049843090/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..7f476b1d74 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,95 @@ +package com.coding.basic; + + +/** + * + */ +public class BinaryTree { + + private Node root; + + public boolean insert(int data) { + Node newNode = new Node(data); + if (root == null) { + root = newNode; + return true; + } + + return add(data); + //return add(root,new Node(data)); + } + + private boolean add(Node currentNode,Node newNode) { + if (currentNode.data == newNode.data) { + return false; + } + if (currentNode.data < newNode.data) { + if (currentNode.right == null) { + currentNode.right = newNode; + return true; + } else { + return add(currentNode.right, newNode); + } + } + if (currentNode.left == null) { + currentNode.left = newNode; + return true; + } else { + return add(currentNode.left, newNode); + } + } + + private boolean add(int data) { + Node newNode = new Node(data); + boolean result = false; + Node cursorNode = root; + Node parentNode = null; + while (true) { + parentNode = cursorNode; + if (cursorNode.data == data) { + break; + } + if (cursorNode.data < data) { + cursorNode = cursorNode.right; + if (cursorNode == null) { + parentNode.right = newNode; + result = true; + break; + } + } else { + cursorNode = cursorNode.left; + if (cursorNode == null) { + parentNode.left = newNode; + result = true; + break; + } + } + } + return result; + } + + + private static class Node { + int data; + Node left, right; + + public Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + public static void main(String[] args) { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(6); + binaryTree.insert(4); + binaryTree.insert(8); + binaryTree.insert(7); + binaryTree.insert(3); + + System.out.println("finsh"); + } + +} diff --git a/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..96ec46d661 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +/** + * Binary Tree + */ +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) { + BinaryTreeNode newNode = new BinaryTreeNode(o); + if ((int)data<(int)o) { + this.right = newNode; + } else { + this.left = newNode; + } + return newNode; + } + + public BinaryTreeNode(Object o){ + this.setData(o); + } + + + + public static void main(String[] args) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(1); + binaryTreeNode.insert(2); + System.out.println(binaryTreeNode.getRight().getData()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Iterator.java b/group18/1049843090/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f4938a46c7 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Iterator.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Iterator + */ +public interface Iterator { + + /** + * 可迭代对象是否还有值 + * + * @return + */ + boolean hasNext(); + + /** + * 返回迭代对象的下一个元素 + * + * @return + */ + E next(); + + /** + * 移除当前元素 + */ + void remove(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/LinkedList.java b/group18/1049843090/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5bebe8caa6 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/LinkedList.java @@ -0,0 +1,205 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * A Simple LinkedList + * + * @param element + */ +public class LinkedList implements List { + /** + * 链表head + */ + private Node head; + + + /** + * 链表中元素的个数 + */ + private int size; + + + /** + * 追加一个元素到链表尾 + * + * @param e + */ + public void add(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 插入一个元素到链表指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + if (index == 0) { + addFirst(e); + } else if (index == size) { + addLast(e); + } else { + Node indexNode = index(index); + Node next = indexNode.next; + Node newNode = new Node(e, next); + index(index - 1).next = newNode; + indexNode = null; + size++; + } + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return index(index).data; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + /** + * 移除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == (size - 1)) { + return removeLast(); + } else { + Node delNode = index(index); + E e = delNode.data; + Node prev = index(index - 1); + prev.next = index(index + 1); + delNode = null; + size--; + return e; + } + } + + /** + * 当前链表的元素个数 + * + * @return + */ + public int size() { + return this.size; + } + + /** + * 添加到链表的头 + * + * @param e + */ + public void addFirst(E e) { + Node newNode = new Node(e, null); + if (this.head != null) { + newNode.next = this.head; + } + this.head = newNode; + size++; + } + + /** + * 添加到链表的尾 + * + * @param e + */ + public void addLast(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 获取指定位置的节点 + * + * @param index + * @return + */ + private Node index(int index) { + Node node = this.head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + /** + * 删除链表第一个元素 + * + * @return + */ + public E removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + E e = head.data; + head = head.next; + size--; + return e; + } + + /** + * 删除链表最后一个元素 + * + * @return + */ + public E removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node end = index(size - 1); + E e = end.data; + end = null; + end = index(size - 2); + end.next = null; + size--; + return e; + } + + /** + * 节点数据 + * + * @param + */ + private static class Node { + //当前节点存储的数据 + E data; + //下一个节点 + Node next; + + public Node(E data, Node next) { + this.data = data; + this.next = next; + } + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/List.java b/group18/1049843090/src/com/coding/basic/List.java new file mode 100644 index 0000000000..962fcf4b77 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/List.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +/** + * A Simple List Interface + */ +public interface List { + + /** + * 追加一个元素 + * + * @param e + */ + void add(E e); + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + void add(int index, E e); + + /** + * 获取指定位置元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 移除指定位置元素 + * + * @param index + * @return + */ + E remove(int index); + + + /** + * 当前List中元素的个数 + * + * @return + */ + int size(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Queue.java b/group18/1049843090/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..94759f0b5b --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Queue.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +/** + * A Simple Queue + */ +public class Queue { + + private LinkedList elementData; + + /** + * 入列 + * + * @param e + */ + public void enQueue(E e) { + elementData.addLast(e); + } + + /** + * 出列 + * + * @return + */ + public E deQueue() { + return elementData.removeFirst(); + } + + /** + * 查看第一个元素 + * + * @return + */ + public E peek() { + return elementData.size()==0?null:elementData.get(0); + } + + /** + * 队列是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 队列中元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Queue() { + elementData = new LinkedList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Stack.java b/group18/1049843090/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..61561f1450 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Stack.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * A Simple Stack + */ +public class Stack { + private ArrayList elementData; + + /** + * 压入栈顶 + * + * @param e + */ + public void push(E e) { + elementData.add(e); + } + + /** + * 取出栈顶元素 + * + * @return + */ + public E pop() { + return elementData.remove(elementData.size() - 1); + } + + /** + * 查看栈顶元素 + * + * @return + */ + public E peek() { + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + /** + * 栈内是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 栈顶内元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Stack() { + elementData = new ArrayList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/ArrayListTest.java b/group18/1049843090/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..25b2a018df --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * ArrayList Test + */ +public class ArrayListTest { + + ArrayList list; + + @Before + public void setUp() throws Exception { + list = new ArrayList<>(); + + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void add() throws Exception { + list.add("first"); + assertEquals("first", list.get(0)); + } + + @Test + public void add1() throws Exception { + list.add(0, "first"); + assertEquals("插入第一条", "first", list.get(0)); + list.add(0, "insert"); + assertEquals("插入第二条", "insert", list.get(0)); + list.add(2, "position_2"); + assertEquals("position_2", list.get(2)); + assertEquals(3, list.size()); + } + + @Test + public void get() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + assertEquals("first", list.get(0)); + assertEquals("second", list.get(1)); + assertEquals("third", list.get(2)); + + } + + @Test + public void remove() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + list.add("fourth"); + assertEquals("first", list.remove(0)); + assertEquals(3, list.size()); + assertEquals("third", list.remove(1)); + assertEquals("fourth", list.remove(1)); + assertEquals(1, list.size()); + + } + + @Test + public void size() throws Exception { + list.add("first"); + assertEquals(1,list.size()); + list.add("second"); + assertEquals( 2,list.size()); + } + + + @Test + public void iterator() throws Exception { + Iterator iterator = list.iterator(); + assertEquals(false,iterator.hasNext()); + list.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,list.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/LinkedListTest.java b/group18/1049843090/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..8b4ef44068 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,118 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * LinkedList Test + */ +public class LinkedListTest { + + LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList<>(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void add() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + assertEquals(2, linkedList.size()); + + } + + @Test + public void add1() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals(2, linkedList.size()); + } + + @Test + public void get() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals("third", linkedList.get(2)); + } + + @Test + public void remove() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals("first", linkedList.remove(0)); + assertEquals("third", linkedList.remove(1)); + assertEquals("fourth", linkedList.remove(1)); + assertEquals(1, linkedList.size()); + + } + + @Test + public void size() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals(4, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addFirst("first first"); + assertEquals("first first", linkedList.get(0)); + + } + + @Test + public void addLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addLast("last"); + assertEquals("last", linkedList.get(2)); + } + + @Test + public void removeFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("first", linkedList.removeFirst()); + assertEquals("second", linkedList.removeFirst()); + assertEquals(1, linkedList.size()); + assertEquals("third", linkedList.get(0)); + assertEquals("third", linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("third", linkedList.removeLast()); + assertEquals("second", linkedList.removeLast()); + assertEquals("first", linkedList.removeLast()); + assertEquals(0, linkedList.size()); + + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/QueueTest.java b/group18/1049843090/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..2652d1e214 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/QueueTest.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Queue Test + */ +public class QueueTest { + + Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue<>(); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("A"); + assertEquals("A",queue.deQueue()); + } + + @Test + public void peek() throws Exception { + assertEquals(null,queue.peek()); + queue.enQueue("A"); + assertEquals("A",queue.peek()); + } + + @Test + public void deQueue() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals("A",queue.deQueue()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,queue.isEmpty()); + queue.enQueue("A"); + assertEquals(false,queue.isEmpty()); + } + + @Test + public void size() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals(2,queue.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/StackTest.java b/group18/1049843090/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..e2587ba7f2 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/StackTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Stack Test + */ +public class StackTest { + Stack stack; + @Before + public void setUp() throws Exception { + stack = new Stack<>(); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("A"); + assertEquals("A",stack.pop()); + } + + @Test + public void pop() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.pop()); + assertEquals("B",stack.pop()); + assertEquals("A",stack.pop()); + assertEquals(0,stack.size()); + + + } + + @Test + public void peek() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.peek()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,stack.isEmpty()); + stack.push("A"); + assertEquals(false,stack.isEmpty()); + } + + @Test + public void size() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals(3,stack.size()); + } + +} \ No newline at end of file From bcce0dde4bc9b0c78126d35cb292e44ce3cb0cde Mon Sep 17 00:00:00 2001 From: yangdd Date: Thu, 23 Feb 2017 17:57:20 +0800 Subject: [PATCH 016/419] modify --- .../src/com/coding/basic/BinaryTreeNode.java | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java index 96ec46d661..20f90e9239 100644 --- a/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java +++ b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java @@ -43,25 +43,11 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Object o) { - BinaryTreeNode newNode = new BinaryTreeNode(o); - if ((int)data<(int)o) { - this.right = newNode; - } else { - this.left = newNode; - } - return newNode; + return null; } public BinaryTreeNode(Object o){ this.setData(o); } - - - public static void main(String[] args) { - BinaryTreeNode binaryTreeNode = new BinaryTreeNode(1); - binaryTreeNode.insert(2); - System.out.println(binaryTreeNode.getRight().getData()); - } - } \ No newline at end of file From 957bb914e3a8a59a9a04bad16ce86f7018b5bc29 Mon Sep 17 00:00:00 2001 From: HuiZhou-Xmu <1814014897@qq.com> Date: Thu, 23 Feb 2017 17:59:12 +0800 Subject: [PATCH 017/419] Basic Data Structure Test---Version 1.0 --- group01/1814014897/zhouhui/.classpath | 7 ++ group01/1814014897/zhouhui/.gitignore | 1 + group01/1814014897/zhouhui/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/BasicDataStructure/ArrayList.java | 74 ++++++++++++ .../BasicDataStructure/BinaryTreeNode.java | 56 +++++++++ .../src/BasicDataStructure/Iterator.java | 7 ++ .../src/BasicDataStructure/LinkedList.java | 113 ++++++++++++++++++ .../zhouhui/src/BasicDataStructure/List.java | 9 ++ .../zhouhui/src/BasicDataStructure/Queue.java | 25 ++++ .../zhouhui/src/BasicDataStructure/Stack.java | 25 ++++ .../src/BasicDataStructureTest/AllTest.java | 18 +++ .../BasicDataStructureTest/ArrayListTest.java | 72 +++++++++++ .../BinaryTreeNodeTest.java | 77 ++++++++++++ .../LinkedListTest.java | 103 ++++++++++++++++ .../src/BasicDataStructureTest/QueueTest.java | 53 ++++++++ .../src/BasicDataStructureTest/StackTest.java | 67 +++++++++++ 17 files changed, 735 insertions(+) create mode 100644 group01/1814014897/zhouhui/.classpath create mode 100644 group01/1814014897/zhouhui/.gitignore create mode 100644 group01/1814014897/zhouhui/.project create mode 100644 group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/List.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java create mode 100644 group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java diff --git a/group01/1814014897/zhouhui/.classpath b/group01/1814014897/zhouhui/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group01/1814014897/zhouhui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group01/1814014897/zhouhui/.gitignore b/group01/1814014897/zhouhui/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/1814014897/zhouhui/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/1814014897/zhouhui/.project b/group01/1814014897/zhouhui/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group01/1814014897/zhouhui/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs b/group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java b/group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java new file mode 100644 index 0000000000..3fdea3d732 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java @@ -0,0 +1,74 @@ +package BasicDataStructure; + +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); //size increase,in order to have enough capacity. + elementData[size++] = o; //similar to: elementData[size]=o; size++; + } + + private void ensureCapacity(int minCapacity){ + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + private void grow(int minCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + ( oldCapacity >> 1 ); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + public void add(int index, Object o){ + if(index < 0 || index > size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[size - 1] = null; + size--; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos < size; + } + + public Object next() { + return elementData[pos++]; + } + } + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java b/group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..b11ca68417 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java @@ -0,0 +1,56 @@ +package BasicDataStructure; + +public class BinaryTreeNode{ + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.data = data; + 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){ + if((Integer)o < (Integer)this.data) + { + if(this.left == null){ + BinaryTreeNode node = new BinaryTreeNode(o); + this.setLeft(node); + return node; + }else{ + return this.left.insert(o); + } + }else{ + if(this.right == null){ + BinaryTreeNode node = new BinaryTreeNode(o); + this.setRight(node); + return node; + }else{ + return this.right.insert(o); + } + } + } + } + + diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java b/group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java new file mode 100644 index 0000000000..c70ebb77dd --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java @@ -0,0 +1,7 @@ +package BasicDataStructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java b/group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java new file mode 100644 index 0000000000..b99e6f15f9 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java @@ -0,0 +1,113 @@ +package BasicDataStructure; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(head == null){ + head = new Node(o); + }else{ + Node pos = head; + while(pos.next != null){ + pos = pos.next; + } + pos.next = new Node(o); + } + size++; + } + + public void add(int index , Object o){ + if(index < 0 || index >size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + if(index == 0) { + Node node = new Node(o); + node.next = head; + head = node; + } + else{ + Node pos = head; + for(int i = 0;i < index-1;i++){ + pos = pos.next; + } + Node node = new Node(o); + node.next = pos.next; + pos.next = node; + } + size++; + } + + public Object get(int index){ + if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + Node pos = head; + for(int i = 0;i < index;i++){ + pos = pos.next; + } + return pos.data; + } + + public Object remove(int index){ + if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + Node element = head; + if(index == 0){ + head = head.next; + }else{ + Node pos = head; + for(int i = 0;i < index - 1;i++){ + pos = pos.next; + } + element = pos.next; + pos.next = pos.next.next; + } + size--; + return element.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); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator{ + + private Node node = head; + private int pos = 0; + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public Object next() { + pos++; + if(pos != 1){ + node = node.next; + } + return node.data; + } + } + + private static class Node{ + Object data; + Node next; + public Node(Object data){ + this.data = data; + next = null; + } + } +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/List.java b/group01/1814014897/zhouhui/src/BasicDataStructure/List.java new file mode 100644 index 0000000000..746612c77e --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/List.java @@ -0,0 +1,9 @@ +package BasicDataStructure; + +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/1814014897/zhouhui/src/BasicDataStructure/Queue.java b/group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java new file mode 100644 index 0000000000..c4fe4c578e --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java @@ -0,0 +1,25 @@ +package BasicDataStructure; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + linkedList.add(o); + size++; + } + + public Object deQueue(){ + size--; + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return size; + } +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java b/group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java new file mode 100644 index 0000000000..c524ee618d --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java @@ -0,0 +1,25 @@ +package BasicDataStructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + return elementData.remove(--size); + } + + 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/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java new file mode 100644 index 0000000000..ff09c3e4bd --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java @@ -0,0 +1,18 @@ +package BasicDataStructureTest; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ + ArrayListTest.class, + BinaryTreeNodeTest.class, + LinkedListTest.class, + QueueTest.class, + StackTest.class +}) + +public class AllTest { + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java new file mode 100644 index 0000000000..61cf94a49c --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java @@ -0,0 +1,72 @@ +package BasicDataStructureTest; + +import org.junit.*; +import BasicDataStructure.ArrayList; +import BasicDataStructure.Iterator; + +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + for(int i = 0;i < 100 ; i++){ + arrayList.add(i); + } + } + + @Test + public void testAddObject() { + for(int i = 0;i < 100;i++){ + Assert.assertEquals(arrayList.get(i), i); + } + } + + @Test + public void testAddIntObject() { + arrayList.add(0,10); + arrayList.add(22, 44); + arrayList.add(40, 5); + arrayList.add(100,88); + Assert.assertEquals(arrayList.get(0), 10); + Assert.assertEquals(arrayList.get(22),44); + Assert.assertEquals(arrayList.get(40), 5); + Assert.assertEquals(arrayList.get(100), 88); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(0), 0); + Assert.assertEquals(arrayList.get(33), 33); + Assert.assertEquals(arrayList.get(77), 77); + Assert.assertEquals(arrayList.get(99), 99); + } + + @Test + public void testRemove() { + arrayList.remove(0); + Assert.assertEquals(arrayList.get(0), 1); + arrayList.remove(50); + Assert.assertEquals(arrayList.get(50), 52); + arrayList.remove(97); + Assert.assertEquals(arrayList.size(), 97); + Assert.assertEquals(arrayList.get(96), 98); + } + + @Test + public void testSize() { + Assert.assertEquals(arrayList.size(), 100); + arrayList.add(5,5); + Assert.assertEquals(arrayList.size(),101); + arrayList.remove(5); + Assert.assertEquals(arrayList.size(), 100); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + for(int i=0;iterator.hasNext();i++){ + Assert.assertEquals(iterator.next(),i); + } + } +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..cf7e899816 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java @@ -0,0 +1,77 @@ +package BasicDataStructureTest; + +import org.junit.*; +import BasicDataStructure.BinaryTreeNode; + + +public class BinaryTreeNodeTest { + + private BinaryTreeNode root = new BinaryTreeNode(5); + + @Before + public void setUp() throws Exception { + root.insert(2); + root.insert(7); + root.insert(1); + root.insert(6); + } + + @Test + public void testGetData() { + Assert.assertEquals(root.getData(), 5); + Assert.assertEquals(root.getLeft().getData(), 2); + Assert.assertEquals(root.getRight().getData(), 7); + Assert.assertEquals(root.getLeft().getLeft().getData(), 1); + Assert.assertEquals(root.getRight().getLeft().getData(), 6); + } + + @Test + public void testSetData() { + root.setData(8); + Assert.assertEquals(root.getData(),8); + root.getLeft().setData(88); + Assert.assertEquals(root.getLeft().getData(),88); + root.getRight().setData(888); + Assert.assertEquals(root.getRight().getData(),888); + } + + @Test + public void testGetLeft() { + BinaryTreeNode node_left = root.getLeft(); + Assert.assertEquals(node_left.getData(), 2); + BinaryTreeNode node_left_left = root.getLeft().getLeft(); + Assert.assertEquals(node_left_left.getData(), 1); + } + + @Test + public void testSetLeft() { + BinaryTreeNode node = new BinaryTreeNode(100); + root.setLeft(node); + Assert.assertEquals(root.getLeft().getData(), 100); + } + + @Test + public void testGetRight() { + BinaryTreeNode node_right = root.getRight(); + Assert.assertEquals(node_right.getData(), 7); + root.insert(8); + BinaryTreeNode node_right_right = root.getRight().getRight(); + Assert.assertEquals(node_right_right.getData(), 8); + } + + @Test + public void testSetRight() { + BinaryTreeNode node = new BinaryTreeNode(100); + root.setRight(node); + Assert.assertEquals(root.getRight().getData(), 100); + } + + @Test + public void testInsert() { + root.insert(4); + root.insert(8); + Assert.assertEquals(root.getLeft().getRight().getData(), 4); + Assert.assertEquals(root.getRight().getRight().getData(), 8); + } + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java new file mode 100644 index 0000000000..cb0b2e3191 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java @@ -0,0 +1,103 @@ +package BasicDataStructureTest; + +import org.junit.*; +import BasicDataStructure.Iterator; +import BasicDataStructure.LinkedList; + +public class LinkedListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<100;i++){ + linkedList.add(i); + } + } + + @Test + public void testAddObject() { + for(int i=0;i<200;i++){ + linkedList.add(i); + } + for(int i=0;i<100;i++){ + Assert.assertEquals(linkedList.get(i), i); + } + for(int i=100;i<300;i++){ + Assert.assertEquals(linkedList.get(i), i-100); + } + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 10); + Assert.assertEquals(linkedList.get(0), 10); + linkedList.add(5,60); + Assert.assertEquals(linkedList.get(5), 60); + Assert.assertEquals(linkedList.get(101), 99); + } + + @Test + public void testGet() { + for(int i =0;i<100;i++){ + Assert.assertEquals(linkedList.get(i), i); + } + } + + @Test + public void testRemove() { + Assert.assertEquals(linkedList.remove(0), 0); + Assert.assertEquals(linkedList.remove(0), 1); + Assert.assertEquals(linkedList.size(), 98); + linkedList.remove(97); + Assert.assertEquals(linkedList.get(96), 98); + } + + @Test + public void testSize() { + linkedList.add(0); + Assert.assertEquals(linkedList.size(), 101); + linkedList.add(0, 10); + Assert.assertEquals(linkedList.size(), 102); + linkedList.remove(0); + Assert.assertEquals(linkedList.size(), 101); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(22); + Assert.assertEquals(linkedList.get(0), 22); + linkedList.addFirst(44); + Assert.assertEquals(linkedList.get(0), 44); + Assert.assertEquals(linkedList.size(), 102); + } + + @Test + public void testAddLast() { + linkedList.addLast(22); + Assert.assertEquals(linkedList.get(100), 22); + linkedList.addLast(44); + Assert.assertEquals(linkedList.get(101), 44); + } + + @Test + public void testRemoveFirst() { + Assert.assertEquals(linkedList.removeFirst(), 0); + Assert.assertEquals(linkedList.removeFirst(), 1); + } + + @Test + public void testRemoveLast() { + Assert.assertEquals(linkedList.removeLast(),99 ); + Assert.assertEquals(linkedList.removeLast(), 98); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + for(int i = 0;iterator.hasNext();i++){ + Assert.assertEquals(iterator.next(), i); + } + } + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java new file mode 100644 index 0000000000..9af9ca2288 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java @@ -0,0 +1,53 @@ +package BasicDataStructureTest; + +import org.junit.*; +import BasicDataStructure.Queue; + + +public class QueueTest { + + Queue queue = new Queue(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<100;i++){ + queue.enQueue(i); + } + } + + @Test + public void testEnQueue() { + Assert.assertEquals(queue.size(), 100); + for(int i =0;i<100;i++){ + queue.enQueue(i); + } + Assert.assertEquals(queue.size(), 200); + } + + @Test + public void testDeQueue() { + for(int i =0;i<100;i++){ + Assert.assertEquals(queue.deQueue(), i); + } + + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(queue.isEmpty(), false); + for(int i=0;i<100;i++){ + queue.deQueue(); + } + Assert.assertEquals(queue.isEmpty(), true); + } + + @Test + public void testSize() { + Assert.assertEquals(queue.size(), 100); + queue.enQueue(100); + Assert.assertEquals(queue.size(), 101); + queue.deQueue(); + Assert.assertEquals(queue.size(), 100); + } + +} diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java b/group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java new file mode 100644 index 0000000000..9f9c930600 --- /dev/null +++ b/group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java @@ -0,0 +1,67 @@ +package BasicDataStructureTest; + +import org.junit.*; +import BasicDataStructure.Stack; + + +public class StackTest { + + Stack stack = new Stack(); + + @Before + public void setUp() throws Exception { + for(int i =0 ;i <100;i++){ + stack.push(i); + } + } + + @Test + public void testPush() { + Assert.assertEquals(stack.peek(), 99); + for(int i =0;i <200;i++){ + stack.push(i); + } + Assert.assertEquals(stack.peek(), 199); + Assert.assertEquals(stack.size(), 300); + } + + @Test + public void testPop() { + stack.pop(); + Assert.assertEquals(stack.peek(), 98); + for(int i=0;i<99;i++){ + stack.pop(); + } + Assert.assertEquals(stack.size(), 0); + } + + @Test + public void testPeek() { + for(int i=0;i<100;i++){ + Assert.assertEquals(stack.peek(), 99); + Assert.assertEquals(stack.size(), 100); + } + stack.pop(); + Assert.assertEquals(stack.peek(), 98); + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(stack.isEmpty(), false); + for(int i =0 ;i <100;i++){ + stack.pop(); + } + Assert.assertEquals(stack.isEmpty(), true); + } + + @Test + public void testSize() { + stack.push(100); + Assert.assertEquals(stack.size(), 101); + stack.pop(); + Assert.assertEquals(stack.size(), 100); + stack.peek(); + Assert.assertEquals(stack.size(), 100); + } + +} From 335ea91d7276b0634c4a780555eb6f12ecc1a2f0 Mon Sep 17 00:00:00 2001 From: wa122as Date: Thu, 23 Feb 2017 18:04:47 +0800 Subject: [PATCH 018/419] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=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 --- group15/1521_653895972/basic/ArrayList.java | 81 +++++++++ group15/1521_653895972/basic/BasicTest.java | 150 ++++++++++++++++ .../1521_653895972/basic/BinaryTreeNode.java | 85 +++++++++ group15/1521_653895972/basic/Iterator.java | 7 + group15/1521_653895972/basic/LinkedList.java | 170 ++++++++++++++++++ group15/1521_653895972/basic/List.java | 9 + group15/1521_653895972/basic/Queue.java | 28 +++ group15/1521_653895972/basic/Stack.java | 33 ++++ 8 files changed, 563 insertions(+) create mode 100644 group15/1521_653895972/basic/ArrayList.java create mode 100644 group15/1521_653895972/basic/BasicTest.java create mode 100644 group15/1521_653895972/basic/BinaryTreeNode.java create mode 100644 group15/1521_653895972/basic/Iterator.java create mode 100644 group15/1521_653895972/basic/LinkedList.java create mode 100644 group15/1521_653895972/basic/List.java create mode 100644 group15/1521_653895972/basic/Queue.java create mode 100644 group15/1521_653895972/basic/Stack.java diff --git a/group15/1521_653895972/basic/ArrayList.java b/group15/1521_653895972/basic/ArrayList.java new file mode 100644 index 0000000000..7b2d1cef5e --- /dev/null +++ b/group15/1521_653895972/basic/ArrayList.java @@ -0,0 +1,81 @@ +package com.oneces.tool.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + //实例化空数组 不用每次都new + private static final Object[] Empty_elementData = {}; + private int size = 0; + + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.elementData = Empty_elementData; + } + //检查是否越界 + private void checkLenght(int index){ + if (index - size > 0) + throw new IndexOutOfBoundsException(); + } + //增加数组容量 + private void kuorong(){ + elementData = Arrays.copyOf(elementData, size + 1); + } + public void add(Object o) { + kuorong(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + kuorong(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkLenght(index); + return elementData[index]; + } + + public Object remove(int index) { + checkLenght(index); + Object element =elementData[index]; + int movesize=size-index-1; + System.arraycopy(elementData,index+1,elementData,index,movesize); + elementData[--size]=null; + return element; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayItr(); + } + private class ArrayItr implements Iterator{ + int cursor;//游标 + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + int i=cursor; + if (i>size)throw new NoSuchElementException(); + Object [] newElementData = ArrayList.this.elementData; + if (i>newElementData.length)throw new IndexOutOfBoundsException(); + cursor=i+1; + return newElementData[i]; + } + } + + @Override + public String toString() { + Object[] s = Arrays.copyOf(elementData,size); + return Arrays.toString(s); + } +} diff --git a/group15/1521_653895972/basic/BasicTest.java b/group15/1521_653895972/basic/BasicTest.java new file mode 100644 index 0000000000..3f3567169a --- /dev/null +++ b/group15/1521_653895972/basic/BasicTest.java @@ -0,0 +1,150 @@ +package com.oneces.tool.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by wanc on 2017/2/21. + */ +public class BasicTest { + + @Test + public void test() { + //测试 +// testArrayList(); +// testLinkedList(); +// testBinaryTreeNode(); +// testStack(); + testQueue(); + } + + + public void testQueue(){ + Queue queue = new Queue(); + queue.enQueue("S"); + queue.enQueue("Y"); + queue.enQueue(5); + System.out.println(queue); + System.out.println("queue.size()="+queue.size()); + System.out.println("queue.deQueue()="+queue.deQueue()); + System.out.println(queue); + System.out.println("queue.isEmpty()="+queue.isEmpty()); + System.out.println(queue); + } + public void testStack(){ + Stack stack = new Stack(); + stack.push("S"); + stack.push("Y"); + stack.push(5); + System.out.println("stack.size()="+stack.size()); + System.out.println("stack.peek()="+stack.peek()); + System.out.println(stack); + System.out.println("stack.isEmpty()="+stack.isEmpty()); + stack.pop(); + System.out.println(stack); + } + public void testBinaryTreeNode(){ + System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); + System.out.println("new 一个实例"); + BinaryTreeNode root = new BinaryTreeNode(); + root.insert(5); + root.insert(6); + root.insert(9); + root.insert(3); + root.insert(3); + root.insert(2); + root.insert(10); + System.out.println(root); + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testLinkedList() { + System.out.println("-------------------LinkedList 测试开始-------------------"); + System.out.println("new 一个实例"); + LinkedList list = new LinkedList(); + System.out.println("1.add(\"A\") 添加元素----A"); + list.add("A"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("2.add(\"B\") 添加元素----B"); + list.add("B"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("3.add(3) 添加元素----3"); + list.add(3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("4.add(1, 3) 在下标1插入元素----3"); + list.add(1, 3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("5.add(3, 6) 在下标3插入元素----6"); + list.add(3, 6); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("6.remove(0) 删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("7.size() 获取size"); + System.out.println("结果:"+list.size()); + System.out.println(); + System.out.println("8.addFirst(\"F\") 在首位前插入F"); + list.addFirst("F"); + System.out.println("结果:"+list); + System.out.println("9.addLast(\"K\") 在末位前插入K"); + list.addLast("K"); + System.out.println("结果:"+list); + System.out.println("10.removeFirst() 删除首位"); + list.removeFirst(); + System.out.println("结果:"+list); + System.out.println("11.removeLast() 删除末尾"); + list.removeLast(); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("12.迭代器"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.println(i.next()); + } + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testArrayList() { + System.out.println("-------------------ArrayList 测试开始-------------------"); + System.out.println("new 一个实例"); + ArrayList list = new ArrayList(); + System.out.println("1.添加元素----A"); + list.add("A"); + Assert.assertEquals(list.get(0),"A"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("2.添加元素----B"); + list.add("B"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("3.添加元素----3"); + list.add(3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("4.在下标1插入元素----3"); + list.add(1, 3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("5.在下标3插入元素----6"); + list.add(3, 6); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("6.删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("7.获取size"); + System.out.println("结果:"+list.size()); + System.out.println(); + System.out.println("8.迭代器"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.println(i.next()); + } + System.out.println("-------------------ArrayList 测试结束-------------------"); + } +} diff --git a/group15/1521_653895972/basic/BinaryTreeNode.java b/group15/1521_653895972/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2e4fcae8dc --- /dev/null +++ b/group15/1521_653895972/basic/BinaryTreeNode.java @@ -0,0 +1,85 @@ +package com.oneces.tool.basic; + +/** + * 实现二叉树 + * left总比父节点小 + * right总比父节点大 + */ +public class BinaryTreeNode { + private Node root; + private int size = 0; + + public void insert(int data) { + final Node newNode = new Node(data); + if (root == null) { + root = newNode; + } else { + Node current = root; + while (true) { + Node parent = current; + if (data < current.data) {//left + current = current.left; + if (current == null) { + parent.left = newNode; + return; + } + } else {//right + current = current.right; + if (current == null) { + parent.right = newNode; + return; + } + } + } + } + size++; + } + + + //先序遍历 + private String preTraverse(Node node) { + if (node == null) + return ""; + else + return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); + } + //中序遍历 + private String midTraverse(Node node) { + if (node == null) + return ""; + else + return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); + } + //后序遍历 + private String posTraverse(Node node) { + if (node == null) + return ""; + else + return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; + } + + private String preJointComma(String str) { + return str == "" ? "" : "," + str; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return "["+midTraverse(root)+"]"; + } + + private static class Node { + int data; + Node left; + Node right; + + Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } +} diff --git a/group15/1521_653895972/basic/Iterator.java b/group15/1521_653895972/basic/Iterator.java new file mode 100644 index 0000000000..5096f85909 --- /dev/null +++ b/group15/1521_653895972/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.oneces.tool.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group15/1521_653895972/basic/LinkedList.java b/group15/1521_653895972/basic/LinkedList.java new file mode 100644 index 0000000000..877b267943 --- /dev/null +++ b/group15/1521_653895972/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.oneces.tool.basic; + +import java.util.NoSuchElementException; + +/** + * 实现单向链表集合 + */ +public class LinkedList implements List { + + private Node head; + private int size = 0; + + //检查是否越界 利用jdk源码的检测方法 + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + //获取对应下标的节点 + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + public void add(Object o) { + + if (head==null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + public Object get(int index) { + return node(index); + } + + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index-1<0){ + prevNode.next=null; + head=x; + }else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + final Node h = head; + final Node newNode = new Node(o, h); + head = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + final Node h = head; + if (h == null) + throw new NoSuchElementException(); + final Node newFirst = h.next; + h.next = null; + head = newFirst; + size--; + return h.data; + } + + public Object removeLast() { + final Node prev = node(size - 1-1); + final Node l = prev.next; + prev.next = null; + l.next = null; + size--; + return l.data; + } + + public Iterator iterator() { + return new LinkedItr(); + } + private class LinkedItr implements Iterator{ + int cursor;//游标 + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + int i=cursor; + if (i>size-1)throw new NoSuchElementException(); + Node current = node(i); + if (current==null)throw new IndexOutOfBoundsException(); + cursor=i+1; + return current.data; + } + } + + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + @Override + public String toString() { + String result = "["; + for (int i = 0; i < size; i++) { + Node n = node(i); + if (i == 0) + result += n.data; + else + result += "," + n.data; + + } + + return result + "]"; + } +} diff --git a/group15/1521_653895972/basic/List.java b/group15/1521_653895972/basic/List.java new file mode 100644 index 0000000000..0d13889fa2 --- /dev/null +++ b/group15/1521_653895972/basic/List.java @@ -0,0 +1,9 @@ +package com.oneces.tool.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/group15/1521_653895972/basic/Queue.java b/group15/1521_653895972/basic/Queue.java new file mode 100644 index 0000000000..657ddb30f4 --- /dev/null +++ b/group15/1521_653895972/basic/Queue.java @@ -0,0 +1,28 @@ +package com.oneces.tool.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return "Queue{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/basic/Stack.java b/group15/1521_653895972/basic/Stack.java new file mode 100644 index 0000000000..6e41577235 --- /dev/null +++ b/group15/1521_653895972/basic/Stack.java @@ -0,0 +1,33 @@ +package com.oneces.tool.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + elementData.remove(elementData.size()-1); + return null; + } + + public Object peek() { + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return "Stack{" + + "elementData=" + elementData + + '}'; + } +} From dfe7e9719d4db86e963223880c69ddecbed5f08a Mon Sep 17 00:00:00 2001 From: HuiZhou-Xmu <1814014897@qq.com> Date: Thu, 23 Feb 2017 18:10:45 +0800 Subject: [PATCH 019/419] Basic Data Structure Test---Version 1.0 --- .../{ => week01}/BasicDataStructure/ArrayList.java | 2 +- .../BasicDataStructure/BinaryTreeNode.java | 2 +- .../src/{ => week01}/BasicDataStructure/Iterator.java | 2 +- .../{ => week01}/BasicDataStructure/LinkedList.java | 2 +- .../src/{ => week01}/BasicDataStructure/List.java | 2 +- .../src/{ => week01}/BasicDataStructure/Queue.java | 2 +- .../src/{ => week01}/BasicDataStructure/Stack.java | 2 +- .../{ => week01}/BasicDataStructureTest/AllTest.java | 2 +- .../BasicDataStructureTest/ArrayListTest.java | 11 +++++++---- .../BasicDataStructureTest/BinaryTreeNodeTest.java | 9 ++++++--- .../BasicDataStructureTest/LinkedListTest.java | 11 +++++++---- .../BasicDataStructureTest/QueueTest.java | 9 ++++++--- .../BasicDataStructureTest/StackTest.java | 9 ++++++--- 13 files changed, 40 insertions(+), 25 deletions(-) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/ArrayList.java (98%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/BinaryTreeNode.java (96%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/Iterator.java (70%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/LinkedList.java (98%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/List.java (83%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/Queue.java (90%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructure/Stack.java (92%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/AllTest.java (88%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/ArrayListTest.java (88%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/BinaryTreeNodeTest.java (91%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/LinkedListTest.java (91%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/QueueTest.java (84%) rename group01/1814014897/zhouhui/src/{ => week01}/BasicDataStructureTest/StackTest.java (88%) diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java similarity index 98% rename from group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java index 3fdea3d732..e33d14ae20 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/ArrayList.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; import java.util.Arrays; diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java similarity index 96% rename from group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java index b11ca68417..a4fb2cf8b9 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/BinaryTreeNode.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public class BinaryTreeNode{ diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java similarity index 70% rename from group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java index c70ebb77dd..0ad3fff8f3 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/Iterator.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public interface Iterator { public boolean hasNext(); diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java similarity index 98% rename from group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java index b99e6f15f9..35b1158cd1 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/LinkedList.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public class LinkedList implements List { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/List.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java similarity index 83% rename from group01/1814014897/zhouhui/src/BasicDataStructure/List.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java index 746612c77e..7806b75ed3 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/List.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public interface List { public void add(Object o); diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java similarity index 90% rename from group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java index c4fe4c578e..e0ab6bbb9c 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/Queue.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public class Queue { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java similarity index 92% rename from group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java index c524ee618d..53f99b37c7 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructure/Stack.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java @@ -1,4 +1,4 @@ -package BasicDataStructure; +package week01.BasicDataStructure; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java similarity index 88% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java index ff09c3e4bd..5d5f07d815 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/AllTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java @@ -1,4 +1,4 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java similarity index 88% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java index 61cf94a49c..072d53f833 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/ArrayListTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java @@ -1,8 +1,11 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; -import org.junit.*; -import BasicDataStructure.ArrayList; -import BasicDataStructure.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.ArrayList; +import week01.BasicDataStructure.Iterator; public class ArrayListTest { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java similarity index 91% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java index cf7e899816..724e6c0e03 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/BinaryTreeNodeTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java @@ -1,7 +1,10 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; -import org.junit.*; -import BasicDataStructure.BinaryTreeNode; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.BinaryTreeNode; public class BinaryTreeNodeTest { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java similarity index 91% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java index cb0b2e3191..2fb20d12f4 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/LinkedListTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java @@ -1,8 +1,11 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; -import org.junit.*; -import BasicDataStructure.Iterator; -import BasicDataStructure.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Iterator; +import week01.BasicDataStructure.LinkedList; public class LinkedListTest { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java similarity index 84% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java index 9af9ca2288..7302b5ec38 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/QueueTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java @@ -1,7 +1,10 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; -import org.junit.*; -import BasicDataStructure.Queue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Queue; public class QueueTest { diff --git a/group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java similarity index 88% rename from group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java rename to group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java index 9f9c930600..b8fb964955 100644 --- a/group01/1814014897/zhouhui/src/BasicDataStructureTest/StackTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java @@ -1,7 +1,10 @@ -package BasicDataStructureTest; +package week01.BasicDataStructureTest; -import org.junit.*; -import BasicDataStructure.Stack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Stack; public class StackTest { From 0847c96068b57e30f2e62c8462c789ca17d6a24b Mon Sep 17 00:00:00 2001 From: wa122as Date: Thu, 23 Feb 2017 18:18:11 +0800 Subject: [PATCH 020/419] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=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 --- group15/1521_653895972/basic/ArrayList.java | 81 --------- group15/1521_653895972/basic/BasicTest.java | 150 ---------------- .../1521_653895972/basic/BinaryTreeNode.java | 85 --------- group15/1521_653895972/basic/Iterator.java | 7 - group15/1521_653895972/basic/LinkedList.java | 170 ------------------ group15/1521_653895972/basic/List.java | 9 - group15/1521_653895972/basic/Queue.java | 28 --- group15/1521_653895972/basic/Stack.java | 33 ---- 8 files changed, 563 deletions(-) delete mode 100644 group15/1521_653895972/basic/ArrayList.java delete mode 100644 group15/1521_653895972/basic/BasicTest.java delete mode 100644 group15/1521_653895972/basic/BinaryTreeNode.java delete mode 100644 group15/1521_653895972/basic/Iterator.java delete mode 100644 group15/1521_653895972/basic/LinkedList.java delete mode 100644 group15/1521_653895972/basic/List.java delete mode 100644 group15/1521_653895972/basic/Queue.java delete mode 100644 group15/1521_653895972/basic/Stack.java diff --git a/group15/1521_653895972/basic/ArrayList.java b/group15/1521_653895972/basic/ArrayList.java deleted file mode 100644 index 7b2d1cef5e..0000000000 --- a/group15/1521_653895972/basic/ArrayList.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.oneces.tool.basic; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -public class ArrayList implements List { - //实例化空数组 不用每次都new - private static final Object[] Empty_elementData = {}; - private int size = 0; - - private Object[] elementData = new Object[100]; - - public ArrayList() { - this.elementData = Empty_elementData; - } - //检查是否越界 - private void checkLenght(int index){ - if (index - size > 0) - throw new IndexOutOfBoundsException(); - } - //增加数组容量 - private void kuorong(){ - elementData = Arrays.copyOf(elementData, size + 1); - } - public void add(Object o) { - kuorong(); - elementData[size++] = o; - } - - public void add(int index, Object o) { - kuorong(); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkLenght(index); - return elementData[index]; - } - - public Object remove(int index) { - checkLenght(index); - Object element =elementData[index]; - int movesize=size-index-1; - System.arraycopy(elementData,index+1,elementData,index,movesize); - elementData[--size]=null; - return element; - } - - public int size() { - return size; - } - - public Iterator iterator() { - return new ArrayItr(); - } - private class ArrayItr implements Iterator{ - int cursor;//游标 - @Override - public boolean hasNext() { - return cursor!=size; - } - - @Override - public Object next() { - int i=cursor; - if (i>size)throw new NoSuchElementException(); - Object [] newElementData = ArrayList.this.elementData; - if (i>newElementData.length)throw new IndexOutOfBoundsException(); - cursor=i+1; - return newElementData[i]; - } - } - - @Override - public String toString() { - Object[] s = Arrays.copyOf(elementData,size); - return Arrays.toString(s); - } -} diff --git a/group15/1521_653895972/basic/BasicTest.java b/group15/1521_653895972/basic/BasicTest.java deleted file mode 100644 index 3f3567169a..0000000000 --- a/group15/1521_653895972/basic/BasicTest.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.oneces.tool.basic; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Created by wanc on 2017/2/21. - */ -public class BasicTest { - - @Test - public void test() { - //测试 -// testArrayList(); -// testLinkedList(); -// testBinaryTreeNode(); -// testStack(); - testQueue(); - } - - - public void testQueue(){ - Queue queue = new Queue(); - queue.enQueue("S"); - queue.enQueue("Y"); - queue.enQueue(5); - System.out.println(queue); - System.out.println("queue.size()="+queue.size()); - System.out.println("queue.deQueue()="+queue.deQueue()); - System.out.println(queue); - System.out.println("queue.isEmpty()="+queue.isEmpty()); - System.out.println(queue); - } - public void testStack(){ - Stack stack = new Stack(); - stack.push("S"); - stack.push("Y"); - stack.push(5); - System.out.println("stack.size()="+stack.size()); - System.out.println("stack.peek()="+stack.peek()); - System.out.println(stack); - System.out.println("stack.isEmpty()="+stack.isEmpty()); - stack.pop(); - System.out.println(stack); - } - public void testBinaryTreeNode(){ - System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); - System.out.println("new 一个实例"); - BinaryTreeNode root = new BinaryTreeNode(); - root.insert(5); - root.insert(6); - root.insert(9); - root.insert(3); - root.insert(3); - root.insert(2); - root.insert(10); - System.out.println(root); - System.out.println("-------------------LinkedList 测试结束-------------------"); - } - public void testLinkedList() { - System.out.println("-------------------LinkedList 测试开始-------------------"); - System.out.println("new 一个实例"); - LinkedList list = new LinkedList(); - System.out.println("1.add(\"A\") 添加元素----A"); - list.add("A"); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("2.add(\"B\") 添加元素----B"); - list.add("B"); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("3.add(3) 添加元素----3"); - list.add(3); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("4.add(1, 3) 在下标1插入元素----3"); - list.add(1, 3); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("5.add(3, 6) 在下标3插入元素----6"); - list.add(3, 6); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("6.remove(0) 删除下标0元素"); - list.remove(0); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("7.size() 获取size"); - System.out.println("结果:"+list.size()); - System.out.println(); - System.out.println("8.addFirst(\"F\") 在首位前插入F"); - list.addFirst("F"); - System.out.println("结果:"+list); - System.out.println("9.addLast(\"K\") 在末位前插入K"); - list.addLast("K"); - System.out.println("结果:"+list); - System.out.println("10.removeFirst() 删除首位"); - list.removeFirst(); - System.out.println("结果:"+list); - System.out.println("11.removeLast() 删除末尾"); - list.removeLast(); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("12.迭代器"); - Iterator i = list.iterator(); - while (i.hasNext()){ - System.out.println(i.next()); - } - System.out.println("-------------------LinkedList 测试结束-------------------"); - } - public void testArrayList() { - System.out.println("-------------------ArrayList 测试开始-------------------"); - System.out.println("new 一个实例"); - ArrayList list = new ArrayList(); - System.out.println("1.添加元素----A"); - list.add("A"); - Assert.assertEquals(list.get(0),"A"); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("2.添加元素----B"); - list.add("B"); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("3.添加元素----3"); - list.add(3); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("4.在下标1插入元素----3"); - list.add(1, 3); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("5.在下标3插入元素----6"); - list.add(3, 6); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("6.删除下标0元素"); - list.remove(0); - System.out.println("结果:"+list); - System.out.println(); - System.out.println("7.获取size"); - System.out.println("结果:"+list.size()); - System.out.println(); - System.out.println("8.迭代器"); - Iterator i = list.iterator(); - while (i.hasNext()){ - System.out.println(i.next()); - } - System.out.println("-------------------ArrayList 测试结束-------------------"); - } -} diff --git a/group15/1521_653895972/basic/BinaryTreeNode.java b/group15/1521_653895972/basic/BinaryTreeNode.java deleted file mode 100644 index 2e4fcae8dc..0000000000 --- a/group15/1521_653895972/basic/BinaryTreeNode.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.oneces.tool.basic; - -/** - * 实现二叉树 - * left总比父节点小 - * right总比父节点大 - */ -public class BinaryTreeNode { - private Node root; - private int size = 0; - - public void insert(int data) { - final Node newNode = new Node(data); - if (root == null) { - root = newNode; - } else { - Node current = root; - while (true) { - Node parent = current; - if (data < current.data) {//left - current = current.left; - if (current == null) { - parent.left = newNode; - return; - } - } else {//right - current = current.right; - if (current == null) { - parent.right = newNode; - return; - } - } - } - } - size++; - } - - - //先序遍历 - private String preTraverse(Node node) { - if (node == null) - return ""; - else - return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); - } - //中序遍历 - private String midTraverse(Node node) { - if (node == null) - return ""; - else - return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); - } - //后序遍历 - private String posTraverse(Node node) { - if (node == null) - return ""; - else - return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; - } - - private String preJointComma(String str) { - return str == "" ? "" : "," + str; - } - - public int size() { - return size; - } - - @Override - public String toString() { - return "["+midTraverse(root)+"]"; - } - - private static class Node { - int data; - Node left; - Node right; - - Node(int data) { - this.data = data; - this.left = null; - this.right = null; - } - } -} diff --git a/group15/1521_653895972/basic/Iterator.java b/group15/1521_653895972/basic/Iterator.java deleted file mode 100644 index 5096f85909..0000000000 --- a/group15/1521_653895972/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.oneces.tool.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group15/1521_653895972/basic/LinkedList.java b/group15/1521_653895972/basic/LinkedList.java deleted file mode 100644 index 877b267943..0000000000 --- a/group15/1521_653895972/basic/LinkedList.java +++ /dev/null @@ -1,170 +0,0 @@ -package com.oneces.tool.basic; - -import java.util.NoSuchElementException; - -/** - * 实现单向链表集合 - */ -public class LinkedList implements List { - - private Node head; - private int size = 0; - - //检查是否越界 利用jdk源码的检测方法 - private boolean isElementIndex(int index) { - return index >= 0 && index < size; - } - - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - - private String outOfBoundsMsg(int index) { - return "Index: " + index + ", Size: " + size; - } - - private void checkElementIndex(int index) { - if (!isElementIndex(index)) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - //获取对应下标的节点 - Node node(int index) { - Node x = head; - for (int i = 0; i < index; i++) - x = x.next; - return x; - } - - public void add(Object o) { - - if (head==null) - head = new Node(o, null); - else { - final Node lastNode = node(size - 1); - final Node newNode = new Node(o, null); - lastNode.next = newNode; - } - size++; - } - - public void add(int index, Object o) { - checkPositionIndex(index); - if (size == index) - add(o); - else { - final Node prevNode = node(index - 1); - final Node nextNode = prevNode.next; - final Node newNode = new Node(o, nextNode); - prevNode.next = newNode; - size++; - } - } - - public Object get(int index) { - return node(index); - } - - public Object remove(int index) { - checkElementIndex(index); - final Node prevNode = node(index - 1); - final Node x = prevNode.next; - if (index-1<0){ - prevNode.next=null; - head=x; - }else { - final Node nextNode = x.next; - prevNode.next = nextNode; - x.next = null; - } - size--; - return x.data; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - final Node h = head; - final Node newNode = new Node(o, h); - head = newNode; - size++; - } - - public void addLast(Object o) { - add(o); - } - - public Object removeFirst() { - final Node h = head; - if (h == null) - throw new NoSuchElementException(); - final Node newFirst = h.next; - h.next = null; - head = newFirst; - size--; - return h.data; - } - - public Object removeLast() { - final Node prev = node(size - 1-1); - final Node l = prev.next; - prev.next = null; - l.next = null; - size--; - return l.data; - } - - public Iterator iterator() { - return new LinkedItr(); - } - private class LinkedItr implements Iterator{ - int cursor;//游标 - @Override - public boolean hasNext() { - return cursor!=size; - } - - @Override - public Object next() { - int i=cursor; - if (i>size-1)throw new NoSuchElementException(); - Node current = node(i); - if (current==null)throw new IndexOutOfBoundsException(); - cursor=i+1; - return current.data; - } - } - - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - @Override - public String toString() { - String result = "["; - for (int i = 0; i < size; i++) { - Node n = node(i); - if (i == 0) - result += n.data; - else - result += "," + n.data; - - } - - return result + "]"; - } -} diff --git a/group15/1521_653895972/basic/List.java b/group15/1521_653895972/basic/List.java deleted file mode 100644 index 0d13889fa2..0000000000 --- a/group15/1521_653895972/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.oneces.tool.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/group15/1521_653895972/basic/Queue.java b/group15/1521_653895972/basic/Queue.java deleted file mode 100644 index 657ddb30f4..0000000000 --- a/group15/1521_653895972/basic/Queue.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.oneces.tool.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o) { - elementData.add(o); - } - - public Object deQueue() { - return elementData.removeFirst(); - } - - public boolean isEmpty() { - return elementData.size()==0?true:false; - } - - public int size() { - return elementData.size(); - } - - @Override - public String toString() { - return "Queue{" + - "elementData=" + elementData + - '}'; - } -} diff --git a/group15/1521_653895972/basic/Stack.java b/group15/1521_653895972/basic/Stack.java deleted file mode 100644 index 6e41577235..0000000000 --- a/group15/1521_653895972/basic/Stack.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.oneces.tool.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - elementData.add(o); - } - - public Object pop() { - elementData.remove(elementData.size()-1); - return null; - } - - public Object peek() { - return elementData.get(elementData.size()-1); - } - - public boolean isEmpty() { - return elementData.size()==0?true:false; - } - - public int size() { - return elementData.size(); - } - - @Override - public String toString() { - return "Stack{" + - "elementData=" + elementData + - '}'; - } -} From f3f4984007781bd25d41e53a1fdf1329434aabfd Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 23 Feb 2017 18:22:54 +0800 Subject: [PATCH 021/419] =?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 6edb4f1a32d42ec54847ba28c2f40cc7312c05c7 Mon Sep 17 00:00:00 2001 From: wa122as Date: Thu, 23 Feb 2017 18:27:20 +0800 Subject: [PATCH 022/419] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=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 --- .../src/com/coding/basic/ArrayList.java | 81 +++++++++ .../src/com/coding/basic/BasicTest.java | 150 ++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 85 +++++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 170 ++++++++++++++++++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 28 +++ .../src/com/coding/basic/Stack.java | 33 ++++ 8 files changed, 563 insertions(+) create mode 100644 group15/1521_653895972/src/com/coding/basic/ArrayList.java create mode 100644 group15/1521_653895972/src/com/coding/basic/BasicTest.java create mode 100644 group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group15/1521_653895972/src/com/coding/basic/Iterator.java create mode 100644 group15/1521_653895972/src/com/coding/basic/LinkedList.java create mode 100644 group15/1521_653895972/src/com/coding/basic/List.java create mode 100644 group15/1521_653895972/src/com/coding/basic/Queue.java create mode 100644 group15/1521_653895972/src/com/coding/basic/Stack.java diff --git a/group15/1521_653895972/src/com/coding/basic/ArrayList.java b/group15/1521_653895972/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..61813344dc --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/ArrayList.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + //实例化空数组 不用每次都new + private static final Object[] Empty_elementData = {}; + private int size = 0; + + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.elementData = Empty_elementData; + } + //检查是否越界 + private void checkLenght(int index){ + if (index - size > 0) + throw new IndexOutOfBoundsException(); + } + //增加数组容量 + private void kuorong(){ + elementData = Arrays.copyOf(elementData, size + 1); + } + public void add(Object o) { + kuorong(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + kuorong(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkLenght(index); + return elementData[index]; + } + + public Object remove(int index) { + checkLenght(index); + Object element =elementData[index]; + int movesize=size-index-1; + System.arraycopy(elementData,index+1,elementData,index,movesize); + elementData[--size]=null; + return element; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayItr(); + } + private class ArrayItr implements Iterator{ + int cursor;//游标 + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + int i=cursor; + if (i>size)throw new NoSuchElementException(); + Object [] newElementData = ArrayList.this.elementData; + if (i>newElementData.length)throw new IndexOutOfBoundsException(); + cursor=i+1; + return newElementData[i]; + } + } + + @Override + public String toString() { + Object[] s = Arrays.copyOf(elementData,size); + return Arrays.toString(s); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BasicTest.java b/group15/1521_653895972/src/com/coding/basic/BasicTest.java new file mode 100644 index 0000000000..3add39d66c --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BasicTest.java @@ -0,0 +1,150 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by wanc on 2017/2/21. + */ +public class BasicTest { + + @Test + public void test() { + //测试 +// testArrayList(); +// testLinkedList(); +// testBinaryTreeNode(); +// testStack(); + testQueue(); + } + + + public void testQueue(){ + Queue queue = new Queue(); + queue.enQueue("S"); + queue.enQueue("Y"); + queue.enQueue(5); + System.out.println(queue); + System.out.println("queue.size()="+queue.size()); + System.out.println("queue.deQueue()="+queue.deQueue()); + System.out.println(queue); + System.out.println("queue.isEmpty()="+queue.isEmpty()); + System.out.println(queue); + } + public void testStack(){ + Stack stack = new Stack(); + stack.push("S"); + stack.push("Y"); + stack.push(5); + System.out.println("stack.size()="+stack.size()); + System.out.println("stack.peek()="+stack.peek()); + System.out.println(stack); + System.out.println("stack.isEmpty()="+stack.isEmpty()); + stack.pop(); + System.out.println(stack); + } + public void testBinaryTreeNode(){ + System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); + System.out.println("new 一个实例"); + BinaryTreeNode root = new BinaryTreeNode(); + root.insert(5); + root.insert(6); + root.insert(9); + root.insert(3); + root.insert(3); + root.insert(2); + root.insert(10); + System.out.println(root); + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testLinkedList() { + System.out.println("-------------------LinkedList 测试开始-------------------"); + System.out.println("new 一个实例"); + LinkedList list = new LinkedList(); + System.out.println("1.add(\"A\") 添加元素----A"); + list.add("A"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("2.add(\"B\") 添加元素----B"); + list.add("B"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("3.add(3) 添加元素----3"); + list.add(3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("4.add(1, 3) 在下标1插入元素----3"); + list.add(1, 3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("5.add(3, 6) 在下标3插入元素----6"); + list.add(3, 6); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("6.remove(0) 删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("7.size() 获取size"); + System.out.println("结果:"+list.size()); + System.out.println(); + System.out.println("8.addFirst(\"F\") 在首位前插入F"); + list.addFirst("F"); + System.out.println("结果:"+list); + System.out.println("9.addLast(\"K\") 在末位前插入K"); + list.addLast("K"); + System.out.println("结果:"+list); + System.out.println("10.removeFirst() 删除首位"); + list.removeFirst(); + System.out.println("结果:"+list); + System.out.println("11.removeLast() 删除末尾"); + list.removeLast(); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("12.迭代器"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.println(i.next()); + } + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testArrayList() { + System.out.println("-------------------ArrayList 测试开始-------------------"); + System.out.println("new 一个实例"); + ArrayList list = new ArrayList(); + System.out.println("1.添加元素----A"); + list.add("A"); + Assert.assertEquals(list.get(0),"A"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("2.添加元素----B"); + list.add("B"); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("3.添加元素----3"); + list.add(3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("4.在下标1插入元素----3"); + list.add(1, 3); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("5.在下标3插入元素----6"); + list.add(3, 6); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("6.删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + System.out.println(); + System.out.println("7.获取size"); + System.out.println("结果:"+list.size()); + System.out.println(); + System.out.println("8.迭代器"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.println(i.next()); + } + System.out.println("-------------------ArrayList 测试结束-------------------"); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..82ff8302e7 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,85 @@ +package com.coding.basic; + +/** + * 实现二叉树 + * left总比父节点小 + * right总比父节点大 + */ +public class BinaryTreeNode { + private Node root; + private int size = 0; + + public void insert(int data) { + final Node newNode = new Node(data); + if (root == null) { + root = newNode; + } else { + Node current = root; + while (true) { + Node parent = current; + if (data < current.data) {//left + current = current.left; + if (current == null) { + parent.left = newNode; + return; + } + } else {//right + current = current.right; + if (current == null) { + parent.right = newNode; + return; + } + } + } + } + size++; + } + + + //先序遍历 + private String preTraverse(Node node) { + if (node == null) + return ""; + else + return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); + } + //中序遍历 + private String midTraverse(Node node) { + if (node == null) + return ""; + else + return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); + } + //后序遍历 + private String posTraverse(Node node) { + if (node == null) + return ""; + else + return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; + } + + private String preJointComma(String str) { + return str == "" ? "" : "," + str; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return "["+midTraverse(root)+"]"; + } + + private static class Node { + int data; + Node left; + Node right; + + Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Iterator.java b/group15/1521_653895972/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group15/1521_653895972/src/com/coding/basic/LinkedList.java b/group15/1521_653895972/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..0c2222d969 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 实现单向链表集合 + */ +public class LinkedList implements List { + + private Node head; + private int size = 0; + + //检查是否越界 利用jdk源码的检测方法 + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + //获取对应下标的节点 + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + public void add(Object o) { + + if (head==null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + public Object get(int index) { + return node(index); + } + + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index-1<0){ + prevNode.next=null; + head=x; + }else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + final Node h = head; + final Node newNode = new Node(o, h); + head = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + final Node h = head; + if (h == null) + throw new NoSuchElementException(); + final Node newFirst = h.next; + h.next = null; + head = newFirst; + size--; + return h.data; + } + + public Object removeLast() { + final Node prev = node(size - 1-1); + final Node l = prev.next; + prev.next = null; + l.next = null; + size--; + return l.data; + } + + public Iterator iterator() { + return new LinkedItr(); + } + private class LinkedItr implements Iterator{ + int cursor;//游标 + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + int i=cursor; + if (i>size-1)throw new NoSuchElementException(); + Node current = node(i); + if (current==null)throw new IndexOutOfBoundsException(); + cursor=i+1; + return current.data; + } + } + + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + @Override + public String toString() { + String result = "["; + for (int i = 0; i < size; i++) { + Node n = node(i); + if (i == 0) + result += n.data; + else + result += "," + n.data; + + } + + return result + "]"; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/List.java b/group15/1521_653895972/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.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/group15/1521_653895972/src/com/coding/basic/Queue.java b/group15/1521_653895972/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..8084624da1 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return "Queue{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Stack.java b/group15/1521_653895972/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..5125abd268 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Stack.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + elementData.remove(elementData.size()-1); + return null; + } + + public Object peek() { + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return "Stack{" + + "elementData=" + elementData + + '}'; + } +} From 266c6ce7600dad6955cfd0523a8820ee853ea9fb Mon Sep 17 00:00:00 2001 From: core2for Date: Thu, 23 Feb 2017 20:09:52 +0800 Subject: [PATCH 023/419] add my work --- group18/1787597051/.classpath | 6 + group18/1787597051/.gitignore | 1 + group18/1787597051/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ ...4\351\202\243\344\272\233\344\272\213.txt" | 20 +++ .../src/com/coding/basic/MyArrayList.java | 75 +++++++++++ .../src/com/coding/basic/MyIterator.java | 6 + .../src/com/coding/basic/MyLinkedList.java | 125 ++++++++++++++++++ .../src/com/coding/basic/MyList.java | 9 ++ .../src/com/coding/basic/MyQueue.java | 25 ++++ .../src/com/coding/basic/MyStack.java | 33 +++++ 11 files changed, 328 insertions(+) create mode 100644 group18/1787597051/.classpath create mode 100644 group18/1787597051/.gitignore create mode 100644 group18/1787597051/.project create mode 100644 group18/1787597051/.settings/org.eclipse.jdt.core.prefs create mode 100644 "group18/1787597051/article/cpu\345\222\214\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\347\232\204\351\202\243\344\272\233\344\272\213.txt" create mode 100644 group18/1787597051/src/com/coding/basic/MyArrayList.java create mode 100644 group18/1787597051/src/com/coding/basic/MyIterator.java create mode 100644 group18/1787597051/src/com/coding/basic/MyLinkedList.java create mode 100644 group18/1787597051/src/com/coding/basic/MyList.java create mode 100644 group18/1787597051/src/com/coding/basic/MyQueue.java create mode 100644 group18/1787597051/src/com/coding/basic/MyStack.java diff --git a/group18/1787597051/.classpath b/group18/1787597051/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group18/1787597051/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/1787597051/.gitignore b/group18/1787597051/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/1787597051/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/1787597051/.project b/group18/1787597051/.project new file mode 100644 index 0000000000..2abc06efd7 --- /dev/null +++ b/group18/1787597051/.project @@ -0,0 +1,17 @@ + + + 1787597051 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1787597051/.settings/org.eclipse.jdt.core.prefs b/group18/1787597051/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group18/1787597051/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git "a/group18/1787597051/article/cpu\345\222\214\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\347\232\204\351\202\243\344\272\233\344\272\213.txt" "b/group18/1787597051/article/cpu\345\222\214\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\347\232\204\351\202\243\344\272\233\344\272\213.txt" new file mode 100644 index 0000000000..ae17c6f15e --- /dev/null +++ "b/group18/1787597051/article/cpu\345\222\214\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\347\232\204\351\202\243\344\272\233\344\272\213.txt" @@ -0,0 +1,20 @@ + cpuڴ桢Ӳָ̡Щ +cpuǴģļɵ·пﲿһЭиС +ڴ棺ʱijʱݡcpuӲ֮ݵĽ +Ӳ̣дݣԴ룬Ƶļȡϵ󲻻ᶪʧڴһϵ磬ڴĶͻ㡣 +ָָ΢ָָ֮ + ÿָһ߼ + ΢ָ΢򼶵Ӳ + ָָɵָ +ʾ +public class Test{ + public static void main(String[] args){ + System.out.println("hello world"); + } +} +notepad±дjavaԴ룬 + һᱣӲ̵ijλ(E:javacode/Test.java) + cmdjavacTest.javaԴļΪֽļijλ(E:javaclasses/Test.class), + ִֽļ + 1ִгʱcpuӲ̵ĶдٶȲ̫ṩһмСcpuӲ֮ĶдٶȲ࣬ЧcpuȻֽļӲﱻװڴ棬ڴ棬ֽΪһָУ + 2cpuڴеָУһһִУԶȡִָָIJһЩҪʱֻͨڴʱݣȻŴŵӲ̡ʹcpuӲ֮ĶдٶȲСЧʡ diff --git a/group18/1787597051/src/com/coding/basic/MyArrayList.java b/group18/1787597051/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..77b14150c1 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + private final int GROW = 4; + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o) { + if (size > elementData.length - 1) { + elementData = Arrays.copyOf(elementData, elementData.length + GROW); + elementData[size] = o; + } else { + elementData[size] = o; + } + size++; + } + + public void add(int index, Object o) { + Object[] target = new Object[elementData.length - index]; + for (int x = index, y = 0; x < elementData.length; x++, y++) { + target[y] = elementData[x]; + } + elementData = Arrays.copyOf(elementData, elementData.length + 1); + size = index; + // elementData[index] = o; + elementData[size] = o; + size++; + for (int y = 0; y < target.length; y++) { + // add(target[y]); + elementData[size] = target[y]; + size++; + } + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + Object removeData = elementData[index]; + elementData[index] = null; + Object[] target = Arrays.copyOfRange(elementData, index + 1, elementData.length); + for (int x = index, y = 0; y < target.length; y++, x++) { + elementData[x] = target[y]; + } + size--; + return removeData; + } + + public int size() { + return size; + } + + public MyIteratorImpl iterator() { + return new MyIteratorImpl(); + } + + private class MyIteratorImpl implements MyIterator { + int index; + + public boolean hasNext() { + return index != size; + } + + public Object next() { + int i = index; + index = i + 1; + return elementData[i]; + } + + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyIterator.java b/group18/1787597051/src/com/coding/basic/MyIterator.java new file mode 100644 index 0000000000..59af236aab --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyIterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface MyIterator { + public abstract boolean hasNext(); + public abstract Object next(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyLinkedList.java b/group18/1787597051/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..862a0b9f38 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +public class MyLinkedList implements MyList { + private int size; + private Node head; + + public MyLinkedList() { + head = new Node(); + head.data = "ͷ"; + head.next = null; + } + + public void add(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p3 = new Node(); + p3.data = o; + p.next = p3; + size++; + } + + public void add(int index, Object o) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = new Node(); + p2.data = o; + p2.next = p.next; + p.next = p2; + size++; + } + p = p.next; + num++; + } + } + + public Object get(int index) { + int num = 0; + Node p = head.next; + while (p != null) { + if (num == index) { + return p.data; + } + p = p.next; + num++; + } + return null; + } + + public Object remove(int index) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = p.next; + p.next = p.next.next; + size--; + return p2.data; + } + p = p.next; + num++; + } + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node p = new Node(); + p.data = o; + p.next = head.next; + head.next = p; + size++; + } + + public void addLast(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2.data = o; + p.next = p2; + size++; + } + + public Object removeFirst() { + Node p = head; + if (p.next != null) { + Node p2 = head.next; + p.next = p.next.next; + size--; + return p2.data; + } + return null; + } + + public Object removeLast() { + Node p = head; + if (p.next != null) { + while (p.next.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2 = p.next; + p.next = null; + size--; + return p2.data; + } + return null; + } + /* + * public Iterator iterator(){ return null; } + */ + + private static class Node { + Object data; + Node next; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyList.java b/group18/1787597051/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..afb20940ea --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyList.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface MyList { + public abstract void add(Object o); + public abstract void add(int index, Object o); + public abstract Object get(int index); + public abstract Object remove(int index); + public abstract int size(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyQueue.java b/group18/1787597051/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..3161f6b4e9 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyQueue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class MyQueue { + private int size; + MyLinkedList mll = new MyLinkedList(); + public MyQueue() { + } + public void enQueue(Object o){ + mll.add(o); + size++; + } + + public Object deQueue(){ + size--; + return mll.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyStack.java b/group18/1787597051/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..36c9aaffa5 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyStack.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() > 0) { + Object data = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return data; + } + return null; + } + + public Object peek() { + if (elementData.size() > 0) { + return elementData.get(elementData.size() - 1); + } + return null; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} From 7a3dcc0221ee17984d26f704406260e95e28d04d Mon Sep 17 00:00:00 2001 From: core2for Date: Thu, 23 Feb 2017 20:15:13 +0800 Subject: [PATCH 024/419] add my work --- .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 group18/1787597051/src/com/coding/basic/BinaryTreeNode.java diff --git a/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java b/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.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; + } + +} From 2ebcff4546888d40f0495f5cc1c176e724fe4d11 Mon Sep 17 00:00:00 2001 From: JayXu Date: Thu, 23 Feb 2017 21:36:27 +0800 Subject: [PATCH 025/419] xuweijay --- group15/1511_714512544/.idea/misc.xml | 9 - group15/1511_714512544/.idea/uiDesigner.xml | 124 ++++++ group15/1511_714512544/.idea/workspace.xml | 415 ++++++++++++++++-- .../com/coding/basic/BinarySearchTree.java | 106 ++++- .../coding/basic/BinarySearchTreeNode.java | 8 +- .../coding/basic/BinarySearchTreeTest.java | 83 ---- .../coding/basic/BinarySearchTreeTest.java | 202 +++++---- .../src/test/com/coding/basic/StackTest.java | 59 +++ 8 files changed, 777 insertions(+), 229 deletions(-) create mode 100644 group15/1511_714512544/.idea/uiDesigner.xml delete mode 100644 group15/1511_714512544/src/com/coding/basic/BinarySearchTreeTest.java create mode 100644 group15/1511_714512544/src/test/com/coding/basic/StackTest.java diff --git a/group15/1511_714512544/.idea/misc.xml b/group15/1511_714512544/.idea/misc.xml index f45ffd5f26..05483570e0 100644 --- a/group15/1511_714512544/.idea/misc.xml +++ b/group15/1511_714512544/.idea/misc.xml @@ -1,14 +1,5 @@ - - - diff --git a/group15/1511_714512544/.idea/uiDesigner.xml b/group15/1511_714512544/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group15/1511_714512544/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/workspace.xml b/group15/1511_714512544/.idea/workspace.xml index 8df02d2252..23a7acce91 100644 --- a/group15/1511_714512544/.idea/workspace.xml +++ b/group15/1511_714512544/.idea/workspace.xml @@ -1,7 +1,15 @@ - + + + + + + + + +