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/432] =?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/432] 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/432] =?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 b8d284ea10ac280ec72450e9e28336757c0cef30 Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Thu, 23 Feb 2017 14:04:59 +0800 Subject: [PATCH 004/432] 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 90a6fb539cb8d325e8cbf306a33a120fdd96639f Mon Sep 17 00:00:00 2001 From: hejj Date: Thu, 23 Feb 2017 14:12:33 +0800 Subject: [PATCH 005/432] wrote a 844028312 file --- group04/844028312/one/.classpath | 6 ++++++ group04/844028312/one/.gitignore | 1 + group04/844028312/one/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group04/844028312/one/.classpath create mode 100644 group04/844028312/one/.gitignore create mode 100644 group04/844028312/one/.project diff --git a/group04/844028312/one/.classpath b/group04/844028312/one/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group04/844028312/one/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/844028312/one/.gitignore b/group04/844028312/one/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/844028312/one/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/844028312/one/.project b/group04/844028312/one/.project new file mode 100644 index 0000000000..d674d0dad5 --- /dev/null +++ b/group04/844028312/one/.project @@ -0,0 +1,17 @@ + + + one + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From be853d34d2135a838bd8db390eff49ff74f641bb Mon Sep 17 00:00:00 2001 From: chzh88 Date: Thu, 23 Feb 2017 14:24:07 +0800 Subject: [PATCH 006/432] 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 007/432] =?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 d8e2d779891b7731d9ff5be6c1ecf2c74f97a3bf Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 23 Feb 2017 15:45:52 +0800 Subject: [PATCH 008/432] ArrayList;LinkedList;Stack;Queue; --- .../src/com/coding/basic/ArrayList.java | 97 ++++++++++ .../src/com/coding/basic/LinkedList.java | 183 ++++++++++++++++++ .../src/com/coding/basic/Queue.java | 29 +++ .../src/com/coding/basic/Stack.java | 34 ++++ 4 files changed, 343 insertions(+) create mode 100644 group04/1299310140/src/com/coding/basic/ArrayList.java create mode 100644 group04/1299310140/src/com/coding/basic/LinkedList.java create mode 100644 group04/1299310140/src/com/coding/basic/Queue.java create mode 100644 group04/1299310140/src/com/coding/basic/Stack.java diff --git a/group04/1299310140/src/com/coding/basic/ArrayList.java b/group04/1299310140/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..fb484db727 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size < this.elementData.length){//加至末尾,size+1 + this.elementData[size] = o; + size++; + }else{//扩张数组,然后加至末尾,size+1 + this.elementData = grow(this.elementData,10); + this.elementData[size] = o; + size++; + } + } + + public void add(int index, Object o){//-1 this.size){//index小于0or大于size,参数错误 + return; + } + if(size >= this.elementData.length){//当前数组容量已满,需扩张 + this.elementData = grow(this.elementData, 10); + } + + //此时只需考虑将o加至index处(0= index;i--){ + this.elementData[i] = this.elementData[i-1]; + } + this.elementData[index] = o; + this.size++; + }else{//直接插入o,size+1 + this.elementData[index] = o; + this.size++; + } + + } + + public Object get(int index){//-1= this.size){//index小于0or大于等于size,参数错误 + return null; + } + return this.elementData[index]; + } + + public Object remove(int index){//-1= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Object o = this.elementData[index];//o保存被移除的值 + //此时只需考虑将index处的o移除 + for(int i = index;i < this.size-1;i++){ + this.elementData[i] = this.elementData[i+1]; + } + this.size--; + return o; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + + /* + * 说明:扩张数组 + * 参数:被扩张的原数组,扩张的增加数(扩张后数组的大小为原数组的长度+增加数) + * 返回值:扩张后的数组 + */ + private static Object[] grow(Object[] src,int size){ +// return Arrays.copyOf(src, src.length + size); + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public String toString(){ + String result = "["; + if(this.size == 0){ + result = result + "]"; + return result; + }else{ + for(int i = 0;i < size;i++){ + result = result + this.elementData[i] + ","; + } + result = result.substring(0,result.length()-1); + result = result + "]"; + return result; + } + } + +} diff --git a/group04/1299310140/src/com/coding/basic/LinkedList.java b/group04/1299310140/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ab330879b7 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/LinkedList.java @@ -0,0 +1,183 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(this.size == 0){//size为0,给head赋值 + this.head = new Node(o); + size++; + }else{//将o加至链表末尾 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + } + } + + public void add(int index , Object o){//index:0~size + if(index < 0 || index > this.size){//index小于0or大于size,参数错误 + return; + } + if(index == 0){//将o加至链表头部 + //size为0时,index必为0,执行该分支 + Node first = new Node(o); + first.next = this.head; + this.head = first; + size++; + }else if(index == size){//将o加至链表末尾 + //index == size != 0时,执行该分支 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + }else{ + //0= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Node pres = this.head;//pres指向0 + for(int i = 0;i < index;i++){ + pres = pres.next; + } + //此时pres指向index + return pres.data; + } + + public Object remove(int index){//index:0~size-1 + if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Object o = null; + if(index == 0){//删除头节点 + o = this.head.data; + this.head = this.head.next; + size--; + }else{//删除头节点以外的其他节点 + Node pres = this.head;//pres指向0 + for(int i = 0;i < index-1;i++){ + pres = pres.next; + } + + //此时pres指向index-1 + o = pres.next.data; + pres.next = pres.next.next; + size--; + } + return o; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){//同add(int 0 , Object o) + Node first = new Node(o); + first.next = this.head; + this.head = first; + size++; + } + + public void addLast(Object o){//同add(int size , Object o) + if(this.size == 0){ + this.head = new Node(o); + size++; + }else{//size>=1 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + } + } + + public Object removeFirst(){//同remove(int 0) + if(this.size == 0){ + return null; + } + + Object o = this.head.data; + this.head = this.head.next; + size--; + return o; + } + + public Object removeLast(){ + if(this.size == 0){ + return null; + } + + Object o = null; + if(this.size == 1){//size==1 + o = this.head.data; + this.head = null; + this.size--; + }else{//size>=2 + Node pres = this.head; + while(pres.next.next != null){ + pres = pres.next; + } + o = pres.next.data; + pres.next = null; + size--; + } + return o; + } + + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + super(); + this.data = data; + } + } + + public String toString(){ + String result = "["; + if(this.size == 0){ + result = result + "]"; + return result; + }else{ + Node pres = this.head; + while(pres != null){ + result = result + pres.data + ","; + pres = pres.next; + } + result = result.substring(0,result.length()-1); + result = result + "]"; + return result; + } + } +} diff --git a/group04/1299310140/src/com/coding/basic/Queue.java b/group04/1299310140/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..181f0cfcb0 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + this.elementData.addLast(o); + } + + public Object deQueue(){ + return this.elementData.removeFirst(); + } + + public boolean isEmpty(){ + if(this.elementData.size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return this.elementData.size(); + } + + public String toString(){ + return this.elementData.toString(); + } +} diff --git a/group04/1299310140/src/com/coding/basic/Stack.java b/group04/1299310140/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..c0a3adac8e --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + this.elementData.add(o); + } + + public Object pop(){ + return this.elementData.remove(this.elementData.size()-1); + } + + public Object peek(){ + return this.elementData.get(this.elementData.size()-1); + } + + public boolean isEmpty(){ + if(this.elementData.size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return this.elementData.size(); + } + + public String toString(){ + return this.elementData.toString(); + } + +} From 0034894b47718db9b8461f1dfbf9e0ee566d553b Mon Sep 17 00:00:00 2001 From: popking008 Date: Thu, 23 Feb 2017 15:51:15 +0800 Subject: [PATCH 009/432] ggg --- group04/274407594/226/.classpath | 6 ++++++ group04/274407594/226/.gitignore | 1 + group04/274407594/226/.project | 17 +++++++++++++++++ .../226/.settings/org.eclipse.jdt.core.prefs | 11 +++++++++++ 4 files changed, 35 insertions(+) create mode 100644 group04/274407594/226/.classpath create mode 100644 group04/274407594/226/.gitignore create mode 100644 group04/274407594/226/.project create mode 100644 group04/274407594/226/.settings/org.eclipse.jdt.core.prefs diff --git a/group04/274407594/226/.classpath b/group04/274407594/226/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group04/274407594/226/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/274407594/226/.gitignore b/group04/274407594/226/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group04/274407594/226/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group04/274407594/226/.project b/group04/274407594/226/.project new file mode 100644 index 0000000000..1f70b883ee --- /dev/null +++ b/group04/274407594/226/.project @@ -0,0 +1,17 @@ + + + 226 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs b/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group04/274407594/226/.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 From bce5fabccbadaaa6893f4585fdc4704d5839459b Mon Sep 17 00:00:00 2001 From: popking008 Date: Thu, 23 Feb 2017 15:57:20 +0800 Subject: [PATCH 010/432] Revert "ggg" This reverts commit 0034894b47718db9b8461f1dfbf9e0ee566d553b. --- group04/274407594/226/.classpath | 6 ------ group04/274407594/226/.gitignore | 1 - group04/274407594/226/.project | 17 ----------------- .../226/.settings/org.eclipse.jdt.core.prefs | 11 ----------- 4 files changed, 35 deletions(-) delete mode 100644 group04/274407594/226/.classpath delete mode 100644 group04/274407594/226/.gitignore delete mode 100644 group04/274407594/226/.project delete mode 100644 group04/274407594/226/.settings/org.eclipse.jdt.core.prefs diff --git a/group04/274407594/226/.classpath b/group04/274407594/226/.classpath deleted file mode 100644 index fb565a588d..0000000000 --- a/group04/274407594/226/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/group04/274407594/226/.gitignore b/group04/274407594/226/.gitignore deleted file mode 100644 index 5e56e040ec..0000000000 --- a/group04/274407594/226/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin diff --git a/group04/274407594/226/.project b/group04/274407594/226/.project deleted file mode 100644 index 1f70b883ee..0000000000 --- a/group04/274407594/226/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - 226 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs b/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 7341ab1683..0000000000 --- a/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -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 From feb49da9c53b562cd59644b77aaabddf0e0dd187 Mon Sep 17 00:00:00 2001 From: popking008 Date: Thu, 23 Feb 2017 15:58:14 +0800 Subject: [PATCH 011/432] Revert "Revert "ggg"" This reverts commit bce5fabccbadaaa6893f4585fdc4704d5839459b. --- group04/274407594/226/.classpath | 6 ++++++ group04/274407594/226/.gitignore | 1 + group04/274407594/226/.project | 17 +++++++++++++++++ .../226/.settings/org.eclipse.jdt.core.prefs | 11 +++++++++++ 4 files changed, 35 insertions(+) create mode 100644 group04/274407594/226/.classpath create mode 100644 group04/274407594/226/.gitignore create mode 100644 group04/274407594/226/.project create mode 100644 group04/274407594/226/.settings/org.eclipse.jdt.core.prefs diff --git a/group04/274407594/226/.classpath b/group04/274407594/226/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group04/274407594/226/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/274407594/226/.gitignore b/group04/274407594/226/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group04/274407594/226/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group04/274407594/226/.project b/group04/274407594/226/.project new file mode 100644 index 0000000000..1f70b883ee --- /dev/null +++ b/group04/274407594/226/.project @@ -0,0 +1,17 @@ + + + 226 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs b/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group04/274407594/226/.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 From 4550485e298ac7f53b6ce81e606d092569f043c6 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 23 Feb 2017 16:09:38 +0800 Subject: [PATCH 012/432] 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 f5f52858da37ec1829516543911238d9b286d194 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 23 Feb 2017 17:31:38 +0800 Subject: [PATCH 013/432] BinaryTreeNode --- .../src/com/coding/basic/BinaryTreeNode.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 group04/1299310140/src/com/coding/basic/BinaryTreeNode.java diff --git a/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java b/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..dd539df626 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +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){ + if(this.data == null){//根节点为空 + this.data = o; + }else{//根节点非空 + BinaryTreeNode pres = this; + BinaryTreeNode insertNode = new BinaryTreeNode(); + insertNode.setData(o); + while(pres != null){ + if((int)o < (int)pres.data){//插入值<当前值,pres向左移动,或者将插入节点挂在当前节点左边 + if(pres.left == null){ + pres.left = insertNode; + break; + } + pres = pres.left; + }else{//插入值>=当前值,pres向右移动,或者将插入节点挂在当前节点右边 + if(pres.right == null){ + pres.right = insertNode; + break; + } + pres = pres.right; + } + } + } + return null; + } + + public void print(){ + if(this.data == null){ + return; + }else{ + if(this.left !=null){ + this.left.print(); + } + System.out.print(this.data); + System.out.print(" "); + if(this.right != null){ + this.right.print(); + } + } + } +} From 9ad7f682401e165e8dd542db3943b041444e7037 Mon Sep 17 00:00:00 2001 From: yangdd Date: Thu, 23 Feb 2017 17:55:55 +0800 Subject: [PATCH 014/432] 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 015/432] 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 016/432] 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 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 017/432] 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 f3f4984007781bd25d41e53a1fdf1329434aabfd Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 23 Feb 2017 18:22:54 +0800 Subject: [PATCH 018/432] =?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 266c6ce7600dad6955cfd0523a8820ee853ea9fb Mon Sep 17 00:00:00 2001 From: core2for Date: Thu, 23 Feb 2017 20:09:52 +0800 Subject: [PATCH 019/432] 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)�� + ������cmd����javac�������Test.javaԴ�ļ�Ϊ�ֽ����ļ�����������ij��λ��(E:javaclasses/Test.class), + ��������ִ���ֽ����ļ��� + 1����ִ�г���ʱ������cpu��Ӳ�̵Ķ�д�ٶȲ��̫�������ṩ��һ���м���������Сcpu��Ӳ��֮��Ķ�д�ٶȲ�࣬����Ч������cpu�����Ȼ���ֽ����ļ���Ӳ���ﱻװ���ڴ棬���ڴ����棬�ֽ����Ϊ��һ��ָ�����У� + 2��cpu������ڴ��е�ָ�����У�һ��һ������ִ�У��Զ����ȡָ���ִ��ָ��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 020/432] 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 a598a5dc72c20769d22152424f25ddc2dba5ce96 Mon Sep 17 00:00:00 2001 From: "kaitao.li" Date: Thu, 23 Feb 2017 22:42:20 +0800 Subject: [PATCH 021/432] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/280646174/.gitignore | 27 +++ group01/280646174/280646174.md | 1 + group01/280646174/basic/pom.xml | 27 +++ .../java/com/coding2017/basic/ArrayList.java | 100 ++++++++++ .../com/coding2017/basic/BinaryTreeNode.java | 37 ++++ .../java/com/coding2017/basic/Iterator.java | 7 + .../java/com/coding2017/basic/LinkedList.java | 179 ++++++++++++++++++ .../main/java/com/coding2017/basic/List.java | 13 ++ .../main/java/com/coding2017/basic/Queue.java | 22 +++ .../main/java/com/coding2017/basic/Stack.java | 25 +++ .../com/coding2017/basic/ArrayListTest.java | 79 ++++++++ .../com/coding2017/basic/LinkedListTest.java | 83 ++++++++ .../java/com/coding2017/basic/QueueTest.java | 23 +++ .../java/com/coding2017/basic/StackTest.java | 24 +++ group01/280646174/pom.xml | 38 ++++ 15 files changed, 685 insertions(+) create mode 100644 group01/280646174/.gitignore create mode 100644 group01/280646174/280646174.md create mode 100644 group01/280646174/basic/pom.xml create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/List.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java create mode 100644 group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java create mode 100644 group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java create mode 100644 group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java create mode 100644 group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java create mode 100644 group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java create mode 100644 group01/280646174/pom.xml diff --git a/group01/280646174/.gitignore b/group01/280646174/.gitignore new file mode 100644 index 0000000000..6b8abd62d4 --- /dev/null +++ b/group01/280646174/.gitignore @@ -0,0 +1,27 @@ +# 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/280646174/280646174.md b/group01/280646174/280646174.md new file mode 100644 index 0000000000..f2c1a2defa --- /dev/null +++ b/group01/280646174/280646174.md @@ -0,0 +1 @@ +# dutekt's home \ No newline at end of file diff --git a/group01/280646174/basic/pom.xml b/group01/280646174/basic/pom.xml new file mode 100644 index 0000000000..421a346d70 --- /dev/null +++ b/group01/280646174/basic/pom.xml @@ -0,0 +1,27 @@ + + + + root + com.coding2017 + 1.0-SNAPSHOT + + 4.0.0 + + basic + + + + + junit + junit + + + junit + junit-dep + + + + + \ No newline at end of file diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..a4199bdbdb --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o) { + if (noSpace()) { + extendSpace(); + } + + elementData[size++] = o; + } + + private void extendSpace() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + private boolean noSpace() { + return size == elementData.length; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (noSpace()) { + extendSpace(); + } + + if (index == size) { + add(o); + return; + } + + 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(); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + if (index == size - 1) { + return elementData[--size]; + } + + Object removed = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return removed; + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("["); + if (size > 0) { + builder.append(get(0)); + } + for (int i = 1; i < size; i++) { + builder.append(", ").append(get(i)); + } + builder.append("]"); + return builder.toString(); + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + private int pos; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public Object next() { + return ArrayList.this.get(pos++); + } + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..29910f06a3 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o) { + return 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; + } + +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..19e214cfbb --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..5aeeb7c7f8 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java @@ -0,0 +1,179 @@ +package com.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == size) { + addLast(o); + } else if (index == 0) { + addFirst(o); + } else { + Node node = new Node(o); + Node prevNode = getNode(index - 1); + Node nextNode = prevNode.next; + prevNode.next = node; + node.prev = prevNode; + nextNode.prev = node; + node.next = nextNode; + size++; + } + } + + private Node getNode(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int j = 0; j < index; j++) { + node = node.next; + } + return node; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + return getNode(index).data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node node = getNode(index); + node.prev.next = node.next; + node.next.prev = node.prev; + size--; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + tail = node; + } else { + head.prev = node; + node.next = head; + head = node; + } + size++; + } + + public void addLast(Object o) { + if (size == 0) { + addFirst(o); + } else { + Node node = new Node(o); + tail.next = node; + node.prev = tail; + tail = node; + size++; + } + } + + public Object removeFirst() { + if (size == 0) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + if (size == 1) { + head = null; + tail = null; + size--; + } else { + head.next.prev = null; + head = head.next; + size--; + } + return node.data; + } + + public Object removeLast() { + if (size == 0) { + throw new IndexOutOfBoundsException(); + } + if (size == 1) { + return removeFirst(); + } + Node node = tail; + tail.prev.next = null; + tail = tail.prev; + size--; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("["); + if (size > 0) { + builder.append(get(0)); + } + for(Node node = head.next; node != null; node = node.next) { + builder.append(", ").append(node.data); + } + builder.append("]"); + return builder.toString(); + } + + private static class Node { + private Object data; + private Node next; + private Node prev; + + public Node() {} + + private Node(Object data) { + this.data = data; + } + } + + private class LinkedListIterator implements Iterator { + private Node node; + + public LinkedListIterator() { + this.node = LinkedList.this.head; + } + + @Override + public boolean hasNext() { + return node != null; + } + + @Override + public Object next() { + Node temp = node; + node = node.next; + return temp.data; + } + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/List.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/List.java new file mode 100644 index 0000000000..0fee4d7a42 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/List.java @@ -0,0 +1,13 @@ +package com.coding2017.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/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java new file mode 100644 index 0000000000..f611b874e0 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding2017.basic; + +public class Queue { + + LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.addLast(o); + } + + public Object deQueue() { + return list.removeFirst(); + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3ec7b5788b --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding2017.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(); + } +} diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..21e9d59694 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java @@ -0,0 +1,79 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class ArrayListTest { + @Test + public void testAdd() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + Assert.assertTrue(arrayList.get(0).equals("0")); + } + + @Test + public void testAddWithIndex() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add(1, "2"); + Assert.assertTrue(arrayList.get(1).equals("2")); + Assert.assertTrue(arrayList.get(2).equals("1")); + Assert.assertTrue(arrayList.size() == 3); + } + + @Test + public void get() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Assert.assertTrue(arrayList.get(1).equals("1")); + } + + @Test + public void remove() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + Object remove = arrayList.remove(1); + Assert.assertTrue(remove.equals("1")); + Assert.assertTrue(arrayList.size() == 2); + } + + @Test + public void size() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Assert.assertEquals(arrayList.size(), 2); + } + + @Test + public void testExtend() { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + arrayList.add("4"); + Assert.assertTrue(arrayList.get(4).equals("4")); + } + + @Test + public void iterator() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Iterator iterator = arrayList.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("0")); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("1")); + Assert.assertTrue(!iterator.hasNext()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..f6855d3583 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java @@ -0,0 +1,83 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class LinkedListTest { + @Test + public void testAdd() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + Assert.assertTrue(list.get(0).equals("0")); + } + + @Test + public void testAddWithIndex() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add(1, "2"); + Assert.assertTrue(list.get(1).equals("2")); + Assert.assertTrue(list.get(2).equals("1")); + Assert.assertTrue(list.size() == 3); + } + + @Test + public void get() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + Assert.assertTrue(list.get(1).equals("1")); + } + + @Test + public void remove() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add("2"); + Object remove = list.remove(1); + Assert.assertTrue(remove.equals("1")); + Assert.assertTrue(list.size() == 2); + } + + @Test + public void size() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + Assert.assertEquals(list.size(), 2); + } + + @Test + public void testAddFirst() { + LinkedList list = new LinkedList(); + list.addFirst("0"); + Assert.assertTrue(list.get(0).equals("0")); + list.addFirst("1"); + Assert.assertTrue(list.get(0).equals("1")); + list.removeFirst(); + Assert.assertTrue(list.get(0).equals("0")); + list.removeLast(); + Assert.assertTrue(list.size() == 0); + } + + @Test + public void iterator() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Iterator iterator = arrayList.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("0")); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("1")); + Assert.assertTrue(!iterator.hasNext()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..5fde883433 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java @@ -0,0 +1,23 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + Assert.assertTrue(queue.size() == 2); + Assert.assertTrue(queue.deQueue().equals(1)); + Assert.assertTrue(queue.deQueue().equals(2)); + Assert.assertTrue(queue.isEmpty()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..145d22d371 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java @@ -0,0 +1,24 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + Assert.assertTrue(stack.size() == 2); + Assert.assertTrue(stack.peek().equals(2)); + Assert.assertTrue(stack.pop().equals(2)); + Assert.assertTrue(stack.pop().equals(1)); + Assert.assertTrue(stack.isEmpty()); + } + +} \ No newline at end of file diff --git a/group01/280646174/pom.xml b/group01/280646174/pom.xml new file mode 100644 index 0000000000..c99644072b --- /dev/null +++ b/group01/280646174/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.coding2017 + root + pom + 1.0-SNAPSHOT + + basic + + + + 4.12 + 4.11 + + + + + + + junit + junit + ${junit.junit.version} + test + + + junit + junit-dep + ${junit.junit-dep.version} + test + + + + + \ No newline at end of file From 478e4c475e9b9284d795dfc96b8e28ba57b2ef49 Mon Sep 17 00:00:00 2001 From: thomas_young Date: Fri, 24 Feb 2017 07:34:35 +0800 Subject: [PATCH 022/432] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=EF=BC=8C=E5=B0=9A=E6=9C=AA=E5=AE=8C=E6=88=90=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group02/812350401/.gitignore | 2 + .../coding2017/basic/ArrayList.java | 77 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++++ .../miniyk2012/coding2017/basic/Iterator.java | 6 + .../coding2017/basic/IteratorImp.java | 24 ++++ .../coding2017/basic/LinkedList.java | 103 ++++++++++++++++ .../miniyk2012/coding2017/basic/List.java | 10 ++ .../miniyk2012/coding2017/basic/Queue.java | 23 ++++ .../miniyk2012/coding2017/basic/Stack.java | 25 ++++ .../coding2017/basic/test/ArrayListTest.java | 14 +++ .../basic/test/BinaryTreeNodeTest.java | 33 +++++ .../coding2017/basic/test/LinkedListTest.java | 93 ++++++++++++++ .../coding2017/basic/test/ListTest.java | 115 ++++++++++++++++++ .../coding2017/basic/test/QueueTest.java | 33 +++++ .../coding2017/basic/test/StackTest.java | 40 ++++++ 15 files changed, 630 insertions(+) create mode 100644 group02/812350401/.gitignore create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java create mode 100644 group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore new file mode 100644 index 0000000000..e6416c5be6 --- /dev/null +++ b/group02/812350401/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/lib/ diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e6bd69b5d8 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.github.miniyk2012.coding2017.basic; + +import java.util.Arrays; + + +public class ArrayList implements List { + + private int size = 0; + private int DEFAULT_LENGTH = 10; + + private Object[] elementData = new Object[DEFAULT_LENGTH]; + + public void add(Object o){ + ensureCapcacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + ensurnPosition(index); + ensureCapcacity(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + ensureIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + ensureIndex(index); + Object temp = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index); + size--; + return temp; + } + + public void clear() { + elementData = new Object[DEFAULT_LENGTH]; + size = 0; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + Iterator iterator = new IteratorImp(this); + return iterator; + } + + private void ensureCapcacity() { + int oldLength = elementData.length; + if (size+1 > oldLength) { + elementData = Arrays.copyOf(elementData, 2*oldLength); + } + } + + private void ensureIndex(int index) { + if (index >= size || index < 0) + throw new ArrayIndexOutOfBoundsException(String.format("index %d out of bounds [0-%d)", index, size)); + } + + private void ensurnPosition(int index) { + if (index <0 || index>size) + throw new ArrayIndexOutOfBoundsException(String.format("position %d out of position [0-%d)", index, size)); + } + + @Override + public String toString() { + Object[] tempArray = Arrays.copyOf(elementData, size); + return Arrays.toString(tempArray); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2b47a098ff --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.miniyk2012.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..229ba41b05 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java new file mode 100644 index 0000000000..cfca8eaac7 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java @@ -0,0 +1,24 @@ +package com.github.miniyk2012.coding2017.basic; + +public class IteratorImp implements Iterator { + + private List aList; + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != aList.size(); + } + + @Override + public Object next() { + int i = cursor; + Object next = aList.get(i); + cursor = i+1; + return next; + } + + public IteratorImp(List aList) { + this.aList = aList; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..e34d76001b --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java @@ -0,0 +1,103 @@ +package com.github.miniyk2012.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + /** + * node接收的index一定是范围内的值,不可能越界 + * @param index + * @return a Node + */ + Node node(int index) { + Node x = head; + for (int i=0; i= size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + + private void checkPositionIndex(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java new file mode 100644 index 0000000000..ff6718f462 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java @@ -0,0 +1,10 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java new file mode 100644 index 0000000000..bcb9d0f9e4 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java @@ -0,0 +1,23 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Queue { + + // 队列入口 -》1,2,3,4 -》队列出口 + private LinkedList aList = new LinkedList(); + + public void enQueue(Object o){ + aList.addFirst(o); + } + + public Object deQueue(){ + return aList.removeLast(); + } + + public boolean isEmpty(){ + return aList.size() == 0; + } + + public int size(){ + return aList.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ddfe76f774 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Stack { + + // 栈顶 《-》 1,2,3,4 栈底 + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0, o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..b308c6bfef --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import org.junit.Before; +import com.github.miniyk2012.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..99798b3222 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + assertEquals(4, binaryTreeNode.getData()); + assertEquals(1, binaryTreeNode.getLeft().getData()); + assertEquals(5, binaryTreeNode.getRight().getData()); + assertEquals(3, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(2, binaryTreeNode.getLeft().getRight().getLeft().getData()); + // binaryTreeNode.print(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..487fd2ee00 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..3d871f832d --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java @@ -0,0 +1,115 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.basic.Iterator; +import com.github.miniyk2012.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..67035c1dcf --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..a9b4e14afd --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.miniyk2012.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From ea616bc89e2e488a980aaca0dbe880a16fcfb956 Mon Sep 17 00:00:00 2001 From: ddyblack <1796244932@qq.com> Date: Fri, 24 Feb 2017 09:30:34 +0800 Subject: [PATCH 023/432] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group04/1796244932/.gitignore | 138 ++++++++++++++++++ group04/1796244932/learn01/.gitignore | 6 + group04/1796244932/learn01/pom.xml | 42 ++++++ .../com/dudy/learn01/base/MyArrayList.java | 124 ++++++++++++++++ .../com/dudy/learn01/base/MyBinaryTree.java | 71 +++++++++ .../com/dudy/learn01/base/MyIterator.java | 6 + .../com/dudy/learn01/base/MyLinkedList.java | 132 +++++++++++++++++ .../java/com/dudy/learn01/base/MyList.java | 8 + .../java/com/dudy/learn01/base/MyQueue.java | 22 +++ .../java/com/dudy/learn01/base/MyStack.java | 26 ++++ .../dudy/learn01/base/MyArrayListTest.java | 59 ++++++++ .../dudy/learn01/base/MyBinaryTreeTest.java | 42 ++++++ .../dudy/learn01/base/MyLinkedListTest.java | 88 +++++++++++ .../com/dudy/learn01/base/MyQueueTest.java | 27 ++++ .../com/dudy/learn01/base/MyStackTest.java | 58 ++++++++ 15 files changed, 849 insertions(+) create mode 100644 group04/1796244932/.gitignore create mode 100644 group04/1796244932/learn01/.gitignore create mode 100644 group04/1796244932/learn01/pom.xml create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java create mode 100644 group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java create mode 100644 group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java create mode 100644 group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java create mode 100644 group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java create mode 100644 group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java diff --git a/group04/1796244932/.gitignore b/group04/1796244932/.gitignore new file mode 100644 index 0000000000..b241c62e19 --- /dev/null +++ b/group04/1796244932/.gitignore @@ -0,0 +1,138 @@ +.metadata/ +RemoteSystemsTempFiles/ +.recommenders/ + +*.iml + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +*/.idea/ +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +### Java ### +# Compiled class file +*.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* + +# End of https://www.gitignore.io/api/eclipse,intellij,java diff --git a/group04/1796244932/learn01/.gitignore b/group04/1796244932/learn01/.gitignore new file mode 100644 index 0000000000..83ef0e8099 --- /dev/null +++ b/group04/1796244932/learn01/.gitignore @@ -0,0 +1,6 @@ +.settings/ +/target/ +.classpath +.project +/bin/ + diff --git a/group04/1796244932/learn01/pom.xml b/group04/1796244932/learn01/pom.xml new file mode 100644 index 0000000000..5a00c4d139 --- /dev/null +++ b/group04/1796244932/learn01/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + com.dudy + learn01 + 0.0.1-SNAPSHOT + jar + + learn01 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.11 + test + + + + + + + + jdk-1.8 + + true + 1.8 + + + 1.8 + 1.8 + 1.8 + + + + diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java new file mode 100644 index 0000000000..5a8e82aaaf --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java @@ -0,0 +1,124 @@ +package com.dudy.learn01.base; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[16]; + + /** + * 增加元素: ①数组没满之前,直接添加到最后,满了扩容添加 + */ + public void add(Object o) { + // 检查是否需要扩容 + this.checkGrow(size + 1); + elementData[size++] = o; + } + + /** + * 检查是否需要扩容 + * + * @param newSize + */ + private void checkGrow(int newSize) { + if (newSize > elementData.length) { + this.grow(elementData); + } + } + + /** + * 扩容 + * + * @param oldElementData + */ + private void grow(Object[] oldElementData) { + int lenth = (int) (oldElementData.length * 1.5); + elementData = Arrays.copyOf(oldElementData, lenth); + } + + /** + * 根据索引添加:①同 add ② 可能会出现 index 超出当前位置的情况 ③往 中间插入时需要移位 + */ + public void add(int index, Object o) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + this.checkGrow(size + 1); + // 循环移位 + int tmp = size; + for (int i = 0; i < size - index; i++) { + elementData[tmp] = elementData[tmp - 1]; + tmp--; + } + // 索引位置赋值 + elementData[index] = o; + size++; + } + + /** + * 直接返回相应数组下标就好 + */ + public Object get(int index) { + return elementData[index]; + } + + /** + * 删除元素:①注意移位 + */ + public Object remove(int index) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + Object desc = elementData[index]; + + for (int i = 0; i < size - index; i++) { + elementData[index] = elementData[index+1]; + index++; + } + size--; + return desc; + } + + public int size() { + return this.size; + } + + public MyIterator iterator() { + + + return new MyItr(); + } + + public class MyItr implements MyIterator{ + + int cursor; + + + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + return elementData[cursor++]; + } + } + + + + @Override + public String toString() { + StringBuffer str = new StringBuffer(); + str.append("["); + for (int i = 0; i < size; i++) { + str.append(elementData[i]+","); + } + str.append("]"); + return str.toString(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java new file mode 100644 index 0000000000..cfc06bc0d2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java @@ -0,0 +1,71 @@ +package com.dudy.learn01.base; + +public class MyBinaryTree { + + private Node root; + + public Node getRoot() { + return root; + } + + static class Node { + private int data; + private Node left; + private Node right; + + public Node(int data) { + this.data = data; + } + + } + + public Node insert(int o) { + Node newNode = new Node(o); + + if (root == null) { + root = newNode; + return newNode; + } + Node currentNode = root; + + while (true) { + // System.out.println("currentNode: " + currentNode.data ); + if (o <= currentNode.data) { // left + + if (currentNode.left != null) { + currentNode = currentNode.left; + } else { + currentNode.left = newNode; + // System.out.println("left return ..."); + // System.out.println(""); + return newNode; + } + } else { // right + if (currentNode.right != null) { + currentNode = currentNode.right; + } else { + currentNode.right = newNode; + // System.out.println("right return ..."); + // System.out.println(""); + return newNode; + } + } + + } + + } + + public void display(Node root) { + + System.out.print(root.data + " "); + if (root.left != null) { + display(root.left); + } + if (root.right != null) { + display(root.right); + } + } + + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java new file mode 100644 index 0000000000..e53306fa03 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java @@ -0,0 +1,6 @@ +package com.dudy.learn01.base; +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java new file mode 100644 index 0000000000..60254997aa --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java @@ -0,0 +1,132 @@ +package com.dudy.learn01.base; + + +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node head; + + public void add(Object o) { + Node newNode = new Node(o); + newNode.next = head;// next --> head + head = newNode; // head --> newNode + size++; + } + + public void add(int index, Object o) { + checkRange(index); + Node current = getCurrentNode(index); + Node newNode = new Node(o); + newNode.next = current.next;//new next --> next + current.next = newNode; // next -->new + size++; + } + + private Node getCurrentNode(int index) {// 获取当前节点 + Node current = head; + for(int i = 0; i< size-index -1; i++){ + current = current.next; + } + return current; + } + + private void checkRange(int index) { + if(index > size || index < 0){ + throw new RuntimeException("indexOutOfException:" + "Index: " + index + ", Size: " + size); + } + } + + public Object get(int index) { + checkRange(index); + Node node = getCurrentNode(index); + return node.data; + } + + public Object remove(int index) { + if(size < 1){// ①没有元素的情况 + throw new RuntimeException("NoSuchElementException: size= " + size); + } + if(index == 0) return removeFirst(); + if(index == size - 1) return removeLast(); + + Node node = getCurrentNode(index+1); + node.next = node.next.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addLast(Object o) { + add(o); + } + + public void addFirst(Object o) { + Node current = head; + while(current.next != null){// 找到最后一个节点 + current = current.next; + } + Node newNode = new Node(o); + current.next = newNode; + size++; + } + + public Object removeFirst() { + Node currentNode = getCurrentNode(1); + Node tmp = currentNode.next; + currentNode.next = null; + size--; + return tmp ==null? currentNode.data:tmp.data;// 可能只有一个数据 + } + + public Object removeLast() { + Node tmp = head; + head = head.next; + size--; + return tmp.data; + } + + public MyIterator iterator() { + return new MyLinkedListItr(); + } + + + public class MyLinkedListItr implements MyIterator { + + int cursor; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + Node currentNode = getCurrentNode(cursor++); + return currentNode.data; + } + } + + private static class Node { + Object data; + Node next; + public Node() { + } + public Node(Object data) { + this.data = data; + } + } + + + private void displayLink() {// 自己调试使用 + Node current = head; + while(current != null){ + System.out.print(current.data); + current = current.next; + } + System.out.println(""); + } + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java new file mode 100644 index 0000000000..455727381d --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java @@ -0,0 +1,8 @@ +package com.dudy.learn01.base; +public interface MyList { + 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(); +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java new file mode 100644 index 0000000000..25b9da93f2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java @@ -0,0 +1,22 @@ +package com.dudy.learn01.base; +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0 ; + } + + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java new file mode 100644 index 0000000000..e59c366e0a --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java @@ -0,0 +1,26 @@ +package com.dudy.learn01.base; +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()); + return o; + } + + public Object peek(){ + + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java new file mode 100644 index 0000000000..5c6edd8fec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java @@ -0,0 +1,59 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Iterator; + +public class MyArrayListTest { + + + + @Test + public void iteraterTest(){ + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void myArrayListTest() { + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + list.add(1, "s"); + list.add(21, 21); + System.out.println("--" + list.size()); + System.out.println(list); + + Object remove = list.remove(3); + System.out.println("remove:" + remove); + System.out.println("--" + list.size()); + System.out.println(list); + } + + @Test + public void arrayListTest(){ + + ArrayList list = new ArrayList(); + list.add("1"); + list.add("2"); + + for (Iterator it = list.iterator(); it.hasNext();){ + System.out.println(it.next()); + } + + } + + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java new file mode 100644 index 0000000000..65bee44580 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java @@ -0,0 +1,42 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/21. + */ +public class MyBinaryTreeTest { + + + /** + * 5 + * 2 7 + * 1 4 6 8 + * + */ + @Test + public void insert() throws Exception { + + + MyBinaryTree tree = new MyBinaryTree(); + tree.insert(5); + tree.insert(2); + tree.insert(1); + tree.insert(7); + tree.insert(8); + tree.insert(4); + tree.insert(6); + + + tree.display(tree.getRoot()); + + } + + @Test + public void display() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java new file mode 100644 index 0000000000..c6ebb096ec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java @@ -0,0 +1,88 @@ +package com.dudy.learn01.base; + +import java.util.LinkedList; + +import org.junit.Test; + +public class MyLinkedListTest { + + + + @Test + public void iteraterTest(){ + MyLinkedList list = new MyLinkedList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void removeTest(){ + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + //list.remove(0); + //list.remove(1); + list.remove(2); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + + + @Test + public void removeLastTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + //list.add(2); + list.removeLast(); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void removeFirstTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + Object first = list.removeFirst(); + System.out.println("--" + list.size() + ",first = " + first); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void baseTest() { + + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.addFirst(0); + list.addLast("last"); + list.add(3, "s");// 0 1 2 s 3 last + System.out.println(list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + Integer first = list.removeFirst(); + } + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java new file mode 100644 index 0000000000..de8d683abe --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java @@ -0,0 +1,27 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyQueueTest { + @Test + public void enQueue() throws Exception { + + + MyQueue queue = new MyQueue(); + queue.enQueue("1"); +// queue.enQueue("2"); +// +// queue.enQueue("3"); + + while (queue.size() > 0){ + System.out.println(queue.deQueue()); + } + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java new file mode 100644 index 0000000000..51175953b0 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java @@ -0,0 +1,58 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.Stack; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyStackTest { + @Test + public void push() throws Exception { + + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + + Object pop = stack.pop(); + System.out.println( "size :" + stack.size() + "pop:" + pop); + Object peek = stack.peek(); + System.out.println( "size :" + stack.size() + "peek: " + peek); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void stackTest(){ + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.peek(); + stack.pop(); + System.out.println(stack.size()); + + } + +} \ No newline at end of file From 371b7ce554dd1231a28ee04dd11e779dcaed8229 Mon Sep 17 00:00:00 2001 From: xukai Date: Fri, 24 Feb 2017 09:54:38 +0800 Subject: [PATCH 024/432] =?UTF-8?q?=E5=AE=9E=E7=8E=B0arraylist=E3=80=81lin?= =?UTF-8?q?kedlist=E3=80=81stack=E3=80=81queue=E5=92=8Cbinarytreenode?= =?UTF-8?q?=E6=8F=92=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group19/527220084/xukai_coding/.gitignore | 8 + .../xukai_coding/coding-common/pom.xml | 107 +++++ .../main/java/org/xukai/common/ArrayList.java | 102 +++++ .../java/org/xukai/common/BinaryTreeNode.java | 53 +++ .../main/java/org/xukai/common/Iterator.java | 7 + .../java/org/xukai/common/LinkedList.java | 163 +++++++ .../src/main/java/org/xukai/common/List.java | 9 + .../src/main/java/org/xukai/common/Queue.java | 27 ++ .../src/main/java/org/xukai/common/Stack.java | 34 ++ .../java/org/xukai/common/ArrayListTest.java | 70 +++ .../org/xukai/common/BinaryTreeNodeTest.java | 28 ++ .../java/org/xukai/common/LinkedListTest.java | 91 ++++ .../test/java/org/xukai/common/QueueTest.java | 46 ++ .../test/java/org/xukai/common/StackTest.java | 52 +++ group19/527220084/xukai_coding/pom.xml | 422 ++++++++++++++++++ 15 files changed, 1219 insertions(+) create mode 100644 group19/527220084/xukai_coding/.gitignore create mode 100644 group19/527220084/xukai_coding/coding-common/pom.xml create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java create mode 100644 group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java create mode 100644 group19/527220084/xukai_coding/pom.xml diff --git a/group19/527220084/xukai_coding/.gitignore b/group19/527220084/xukai_coding/.gitignore new file mode 100644 index 0000000000..ba13ec60db --- /dev/null +++ b/group19/527220084/xukai_coding/.gitignore @@ -0,0 +1,8 @@ +*target +*.classpath +*.project +*.settings +*iml +.idea +*gen-* +rebel.xml \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/pom.xml b/group19/527220084/xukai_coding/coding-common/pom.xml new file mode 100644 index 0000000000..ab70ddd636 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/pom.xml @@ -0,0 +1,107 @@ + + + + xukai.coding + org.xukai.coding + 1.0-SNAPSHOT + + 4.0.0 + jar + coding.common + + + + junit + junit + + + org.freemarker + freemarker + + + commons-beanutils + commons-beanutils + 1.8.0 + + + org.apache.commons + commons-compress + 1.8.1 + + + commons-lang + commons-lang + + + commons-logging + commons-logging + 1.2 + + + org.apache.commons + commons-lang3 + + + commons-io + commons-io + + + com.google.guava + guava + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + jcl-over-slf4j + + + + + + + + + com.google.code.findbugs + jsr305 + 3.0.1 + + + cglib + cglib + + + joda-time + joda-time + + + + + + + + + + + javax.servlet + javax.servlet-api + + + org.springframework + spring-web + + + com.alibaba + fastjson + + + + \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java new file mode 100644 index 0000000000..0a8601c8ee --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java @@ -0,0 +1,102 @@ +package org.xukai.common; + + + +public class ArrayList implements List { + + private int size = 0; + + private static final int DEFAULT_CAPICITY = 10; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPICITY]; + } + + public ArrayList(int size) { + elementData = new Object[size]; + } + + private Object[] elementData ; + + public void add(Object o){ + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (isNeedResize(index)) { + elementData = grown(elementData, Math.max(elementData.length << 1, index + 1)); + } + if(index < size){ + 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 ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index > size-1 || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + Object result = elementData[index]; + size--; + System.arraycopy(elementData,index+1,elementData,index,size-index); + return result; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + class ArrayListIterator implements Iterator { + + private int pos = -1; + + @Override + public boolean hasNext() { + return pos < size - 1; + } + + @Override + public Object next() { + pos++; + return elementData[pos]; + } + } + + public void display(){ + System.out.print("["); + Iterator iterator = iterator(); + while (iterator.hasNext()){ + System.out.print(" " + iterator.next()); + } + System.out.print(" ]"); + } + + private boolean isNeedResize(int index){ + return index > elementData.length-1 || size > elementData.length-1; + } + + private Object[] grown(Object[] src,int capacity){ + Object[] des = new Object[capacity]; + System.arraycopy(elementData,0,des,0,elementData.length); + return des; + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java new file mode 100644 index 0000000000..2ff876cac5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package org.xukai.common; + +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(Comparable o){ + if (data == null) { + data = o; + return this; + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + if (o.compareTo(data) < 0) { + if (left == null) { + left = node; + return node; + } else { + return left.insert(o); + } + } else { + if (right == null) { + right = node; + return node; + } else { + return right.insert(o); + } + } + } + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java new file mode 100644 index 0000000000..d81ade6ba5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java @@ -0,0 +1,7 @@ +package org.xukai.common; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java new file mode 100644 index 0000000000..cb1585cecf --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java @@ -0,0 +1,163 @@ +package org.xukai.common; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if (head == null) { + head = node; + } else { + Node next = head.next; + if (next == null) { + head.next = node; + } else { + while (next.next != null){ + next = next.next; + } + next.next = node; + } + } + size++; + } + + public void add(int index , Object o){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + size++; + Node node = new Node(); + node.data = o; + if (index == 0) { + node.next = head; + head = node; + return; + } + int pos = 1; + Node next = head; + //index 2 + while(index > pos){ + next = next.next; + pos++; + } + node.next = next.next; + next.next = node; + } + } + public Object get(int index){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (index == 0) { + return head.data; + } + int pos = 1; + Node next = head; + //index 2 + while(index > pos){ + next = next.next; + pos++; + } + return next.data; + } + } + + public Object remove(int index){ + if (index < 0 || index > size - 1 ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (index == 0) { + Node result = head; + head = head.next; + return result.data; + } + int pos = 1; + Node next = head; + //index 1 + while(index > pos){ + next = next.next; + pos++; + } + Node result = next.next; + next.next = next.next.next; + size--; + return result.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + Node result = head; + head = head.next; + size--; + return result.data; + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator { + + private Node currentNode ; + + @Override + public boolean hasNext() { + if (currentNode == null) { + if (head != null) { + return true; + } else { + return false; + } + } + return currentNode.next != null; + } + + @Override + public Object next() { + if (currentNode == null) { + currentNode = head; + return currentNode.data; + } + currentNode = currentNode.next; + return currentNode.data; + } + } + + public void display(){ + System.out.print("["); + Iterator iterator = iterator(); + while (iterator.hasNext()){ + System.out.print(" " + iterator.next()); + } + System.out.print(" ]"); + } + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java new file mode 100644 index 0000000000..ab5dcb4178 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java @@ -0,0 +1,9 @@ +package org.xukai.common; + +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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java new file mode 100644 index 0000000000..ad77cee9ae --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java @@ -0,0 +1,27 @@ +package org.xukai.common; + +import java.util.NoSuchElementException; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (isEmpty()) { + throw new NoSuchElementException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java new file mode 100644 index 0000000000..de705e1fec --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java @@ -0,0 +1,34 @@ +package org.xukai.common; + +import java.util.EmptyStackException; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + if (isEmpty()) { + 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/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java new file mode 100644 index 0000000000..b14feb5ab4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java @@ -0,0 +1,70 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 2:02 + */ +public class ArrayListTest { + + @Test + public void testAdd() throws Exception { + ArrayList list = new ArrayList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Assert.assertTrue(list.size() == 5); + list.add(0,"3"); + Assert.assertTrue(list.get(0).equals("3")); + Assert.assertTrue(list.size() == 6); + list.remove(5); + Assert.assertTrue(list.size() == 5); + list.display(); + } + + @Test + public void testAdd1() throws Exception { + + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testIterator() throws Exception { + ArrayList list = new ArrayList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + @Test + public void testDisplay() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..cc85baaa97 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package org.xukai.common; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 5:02 + */ +public class BinaryTreeNodeTest { + + + @Test + public void testInsert() throws Exception { + BinaryTreeNode node = new BinaryTreeNode(); + node.insert(5); + node.insert(9); + node.insert(3); + node.insert(7); + node.insert(2); + node.insert(8); + node.insert(4); + node.insert(6); + node.insert(1); + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java new file mode 100644 index 0000000000..d31e3a70ec --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java @@ -0,0 +1,91 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 3:54 + */ +public class LinkedListTest { + + @Test + public void testAdd() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Assert.assertTrue(list.size() == 5); + list.add(0,"000"); + Assert.assertTrue( (list.get(0)).equals("000")); + Assert.assertTrue(list.size() == 6); + list.addFirst("111"); + Assert.assertTrue(list.get(0).equals("111")); + list.remove(5); + Assert.assertTrue(list.size() == 6); + list.addLast("111"); + Assert.assertTrue(list.size() == 7); + list.removeFirst(); + Assert.assertTrue(list.size() == 6); + list.removeLast(); + Assert.assertTrue(list.size() == 5); + list.remove(4); + Assert.assertTrue(list.size() == 4); + list.display(); + } + + @Test + public void testAdd1() throws Exception { + + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + + } + + @Test + public void testAddLast() throws Exception { + + } + + @Test + public void testRemoveFirst() throws Exception { + + } + + @Test + public void testRemoveLast() throws Exception { + + } + + @Test + public void testIterator() throws Exception { + + } + + @Test + public void testDisplay() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java new file mode 100644 index 0000000000..fa1a6e9f2c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java @@ -0,0 +1,46 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 4:36 + */ +public class QueueTest { + + @Test + public void testEnQueue() throws Exception { + Queue queue = new Queue(); + Assert.assertTrue(queue.isEmpty()); + queue.enQueue("0"); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + Assert.assertTrue(!queue.isEmpty()); + Assert.assertTrue(queue.deQueue().equals("0")); + Assert.assertTrue(queue.deQueue().equals("1")); + Assert.assertTrue(queue.size() == 2); + queue.deQueue(); + queue.deQueue(); + Assert.assertTrue(queue.isEmpty()); + } + + @Test + public void testDeQueue() throws Exception { + + } + + @Test + public void testIsEmpty() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java new file mode 100644 index 0000000000..69fc9dee6e --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java @@ -0,0 +1,52 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 4:20 + */ +public class StackTest { + + @Test + public void testPush() throws Exception { + Stack stack = new Stack(); + Assert.assertTrue(stack.isEmpty()); + stack.push("0"); + stack.push("1"); + stack.push("2"); + stack.push("3"); + Assert.assertTrue(!stack.isEmpty()); + Assert.assertTrue(stack.peek().equals("3")); + Assert.assertTrue(stack.pop().equals("3")); + Assert.assertTrue(stack.size() == 3); + stack.pop(); + stack.pop(); + stack.pop(); + Assert.assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() throws Exception { + + } + + @Test + public void testPeek() throws Exception { + + } + + @Test + public void testIsEmpty() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/pom.xml b/group19/527220084/xukai_coding/pom.xml new file mode 100644 index 0000000000..f8e396ae46 --- /dev/null +++ b/group19/527220084/xukai_coding/pom.xml @@ -0,0 +1,422 @@ + + + 4.0.0 + + org.xukai.coding + xukai.coding + pom + 1.0-SNAPSHOT + + coding-common + + + + UTF-8 + 3.15.0-GA + 3.7.0.Final + 4.1.6.RELEASE + 2.6 + 3.4 + 1.7 + 2.3.21 + 3.2.8 + 1.2.2 + 5.1.29 + + 1.3 + 17.0 + 1.0.11 + 4.11 + 2.1.1 + 2.1.2 + 3.4.5 + 1.1.38 + 2.2.2 + 1.2-GUAHAO + 1.1.1 + 2.3.2 + 2.4.4 + 1.9 + 2.9.6 + 1.8 + 10.2.0.4.0 + 2.2.1 + 1.7.2 + 1.3 + 1.6.6 + 1.6.6 + 3.1 + 4.0 + 2.8.0 + + + + + + + org.slf4j + jcl-over-slf4j + ${jcl-over-slf4j.version} + + + + cglib + cglib + ${cglib.version} + + + org.quartz-scheduler + quartz + ${quartz.version} + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} + + + com.github.sgroschupf + zkclient + 0.1 + + + org.springframework + spring-org.xukai.core.org.xukai.core.aop + ${spring.version} + + + org.springframework + spring-aspects + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + org.springframework + spring-websocket + ${spring.version} + + + org.springframework + spring-messaging + ${spring.version} + + + org.springframework + spring-aop + ${spring.version} + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-context-support + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + org.springframework + spring-beans + ${spring.version} + + + org.springframework + spring-expression + ${spring.version} + + + + + + + + org.springframework + spring-oxm + ${spring.version} + + + org.springframework + spring-test + ${spring.version} + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + + com.google.guava + guava + ${guava.version} + + + org.mybatis + mybatis-spring + ${mybatis.spring.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j-log4j12.version} + + + org.mybatis + mybatis + ${mybatis.version} + + + + mysql + mysql-connector-java + ${mysql.java.version} + + + com.oracle + ojdbc14 + ${oracle.version} + + + org.apache.velocity + velocity + ${velocity.version} + + + org.freemarker + freemarker + ${freemarker.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + commons-lang + commons-lang + ${commons.lang.version} + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + commons-dbcp + commons-dbcp + ${commons.dbcp.version} + + + com.google.code.kaptcha + kaptcha + ${kaptcha.version} + + + com.alibaba + druid + ${druid.version} + + + org.slf4j + slf4j-api + 1.7.7 + + + org.apache.velocity + velocity-tools + 2.0 + + + log4j + log4j + 1.2.16 + + + dom4j + dom4j + 1.6.1 + + + javax.servlet + servlet-api + 2.5 + + + commons-codec + commons-codec + ${commons.codec} + + + commons-io + commons-io + 1.4 + + + javax.servlet + javax.servlet-api + 3.0.1 + + + junit + junit + ${junit.version} + + + org.apache.maven.plugins + maven-resources-plugin + 2.4.3 + + + com.alibaba + fastjson + ${fastjson.version} + + + com.greenline.common + greenline-common-util + ${greenline.common.util} + + + joda-time + joda-time + ${v.joda.time.version} + + + + javax.servlet + jsp-api + 2.0 + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + redis.clients + jedis + ${jedis.version}} + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${v.p.mvn.compiler} + + ${project.ud.jdk} + ${project.ud.jdk} + UTF-8 + + ${project.build.sourceDirectory} + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8 + + true + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8 + + utf-8 + utf-8 + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.3 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + true + + + + + + + \ No newline at end of file From c707d7dae24aceb5b2a3bd0b33fb7794138cafe5 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 24 Feb 2017 10:22:11 +0800 Subject: [PATCH 025/432] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FelixCJF/coding2017/basic/ArrayList.java | 96 ++++++++++--------- .../FelixCJF/coding2017/basic/Stack.java | 8 +- .../coding2017/basic/test/ArrayListTest.java | 4 +- .../coding2017/basic/test/LinkedListTest.java | 3 + .../coding2017/basic/test/ListTest.java | 10 +- 5 files changed, 68 insertions(+), 53 deletions(-) diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java index e287d1779a..133db97491 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java @@ -1,6 +1,5 @@ package com.github.FelixCJF.coding2017.basic; -import java.util.Arrays; public class ArrayList implements List { @@ -12,50 +11,44 @@ public void add(Object o){ //容量增加 ensureCapacity(size + 1); //添加 - elementData[size++] = o; + elementData[size ++] = o; } public void add(int index, Object o){ - //容量增加 - ensureCapacity(size + 1); - //临时变量 - Object[] elementData3 = new Object[size + 1]; - //将index前数据复制 - for (int i = 0; i < index + 1 ; i++) { - elementData3[i] = elementData[i]; - } - //插入的数据 - elementData3 [index + 1] = o; - //插入数据之后的后半段复制 - for (int i = index + 2 ; i < elementData3.length; i++) { - elementData3[i] = elementData[i-1]; - } - elementData = elementData3; - size++; + + //检查是否越界 + rangeCheck(index); + // 进行扩容检查 + ensureCapacity(size + 1); + // 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置 + System. arraycopy(elementData, index, elementData, index + 1, + size - index); + // 将指定的index位置赋值为Object o + elementData[index] = o; + // 自增一位长度 + size++; } public Object get(int index){ - if (index < 0 || index >= this.size) { - throw new IndexOutOfBoundsException(); - } + rangeCheck(index); return elementData[index]; } public Object remove(int index){ - if (index < 0 || index >= this.size) { - throw new IndexOutOfBoundsException(); - } - Object oldValue = elementData[index]; - Object[] elementData4 = new Object[size - 1]; - for (int i = 0; i < index; i++) { - elementData4[i] = elementData[i]; - } - for (int i = index; i < elementData4.length; i++) { - elementData4[i] = elementData[i + 1]; - } - elementData = elementData4; - size--; - return oldValue; + // 数组越界检查 + rangeCheck(index); + // 取出要删除位置的元素,供返回使用 + Object oldValue = elementData[index]; + // 计算数组要复制的数量 + int numMoved = size - index - 1; + // 数组复制,就是将index之后的元素往前移动一个位置 + if (numMoved > 0) + System. arraycopy(elementData, index+1, elementData, index, + numMoved); + // 将数组最后一个元素置空(因为删除了一个元素,然后index后面的元素都向前移动了,所以最后一个就没用了),好让gc尽快回收 + // 不要忘了size减一 + elementData[--size] = null; + return oldValue; } public int size(){ @@ -84,14 +77,29 @@ public Object next() { return object; } } - public void ensureCapacity(int minCapacity) { - int oldCapacity = elementData.length; - if (minCapacity > oldCapacity) { - int newCapacity = (oldCapacity * 3) / 2 + 1; - if (newCapacity < minCapacity) - newCapacity = minCapacity; - elementData = Arrays.copyOf(elementData, newCapacity); - } + //扩容 + public void ensureCapacity( int minCapacity) { + // 当前数组的长度 + int oldCapacity = elementData .length; + // 最小需要的容量大于当前数组的长度则进行扩容 + if (minCapacity > oldCapacity) { + // 扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1); + // 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + //数组复制 + Object[] elementData2 = new Object[newCapacity]; + for (int i = 0; i < oldCapacity; i++) { + elementData2[i] = elementData[i]; + } + elementData = elementData2; + } + } + //检查是否越界 + private void rangeCheck(int index){ + if (index < 0 || index >= this.size) { + throw new IndexOutOfBoundsException("index :" + index + "size :" + size); + } } - } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java index cbb7e0683c..16684f7d92 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java @@ -7,30 +7,28 @@ public class Stack { //存放栈内元素的容器 private ArrayList elementData = new ArrayList(); //记录栈内元素个数 - private int size = 0; public void push(Object o){ elementData.add(o); - size ++; } public Object pop(){ if (isEmpty()) { throw new EmptyStackException(); } - return elementData.remove(size - 1); + return elementData.remove(elementData.size() - 1); } public Object peek(){ if (isEmpty()) { throw new EmptyStackException(); } - return elementData.get(size - 1); + return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ return elementData.size() == 0; } public int size(){ - return size; + return elementData.size(); } } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java index 51f2c1115c..9484ce1527 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java @@ -6,9 +6,9 @@ public class ArrayListTest extends ListTest { - @Before +/* @Before public void setUpArrayList() { aList = new ArrayList(); - } + }*/ } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java index b990f0327e..ce0c0d1c0d 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java @@ -7,7 +7,9 @@ import org.junit.Before; import org.junit.Test; +import com.github.FelixCJF.coding2017.basic.ArrayList; import com.github.FelixCJF.coding2017.basic.LinkedList; +import com.github.FelixCJF.coding2017.basic.List; public class LinkedListTest extends ListTest{ @@ -15,6 +17,7 @@ public class LinkedListTest extends ListTest{ @Before public void setUpLinkedList() { + List aList = new ArrayList(); aList = new LinkedList(); aLinkedList = new LinkedList(); } diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java index d9c52595d1..b970372bbe 100644 --- a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java @@ -6,17 +6,18 @@ import org.junit.Test; import org.junit.rules.ExpectedException; +import com.github.FelixCJF.coding2017.basic.ArrayList; import com.github.FelixCJF.coding2017.basic.Iterator; import com.github.FelixCJF.coding2017.basic.List; public class ListTest { - protected static List aList; - + //protected static List aList = new ArrayList(); @Test public void testFunctional() { + List aList = new ArrayList(); aList.add(1); aList.add(2); assertEquals(1, aList.get(0)); @@ -41,6 +42,7 @@ public void testFunctional() { @Test public void testAdd() { + List aList = new ArrayList(); for (int i=0; i<100; i++) aList.add(i); assertEquals(0, aList.get(0)); @@ -50,6 +52,7 @@ public void testAdd() { @Test public void testRemove() { + List aList = new ArrayList(); aList.add(1); aList.add(2); aList.add(3); @@ -73,6 +76,7 @@ public void testRemove() { @Test public void testSize() { + List aList = new ArrayList(); for (int i=0; i<10; i++) aList.add(i*2); assertEquals(10, aList.size()); @@ -83,6 +87,7 @@ public void testSize() { @Test public void testException() { + List aList = new ArrayList(); expectedEx.expect(Exception.class); aList.remove(1); @@ -93,6 +98,7 @@ public void testException() { @Test public void testIterator() { + List aList = new ArrayList(); Iterator it = aList.iterator(); assertEquals(false, it.hasNext()); From a3b368b6a06ef8cd7a61631c7f64a3f7617605e9 Mon Sep 17 00:00:00 2001 From: earliest Date: Thu, 23 Feb 2017 18:35:44 -0800 Subject: [PATCH 026/432] add QQ directory --- group17/1282579502/.classpath | 6 ++++++ group17/1282579502/.gitignore | 1 + group17/1282579502/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group17/1282579502/.classpath create mode 100644 group17/1282579502/.gitignore create mode 100644 group17/1282579502/.project diff --git a/group17/1282579502/.classpath b/group17/1282579502/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group17/1282579502/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/1282579502/.gitignore b/group17/1282579502/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/1282579502/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/1282579502/.project b/group17/1282579502/.project new file mode 100644 index 0000000000..0b4748644a --- /dev/null +++ b/group17/1282579502/.project @@ -0,0 +1,17 @@ + + + 1282579502Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From 5f8f4473bd01bccc1a95a918033d87d60292d60d Mon Sep 17 00:00:00 2001 From: kai Date: Fri, 24 Feb 2017 10:39:26 +0800 Subject: [PATCH 027/432] first commit --- group06/1378560653/.classpath | 6 ++++++ group06/1378560653/.gitignore | 1 + group06/1378560653/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group06/1378560653/.classpath create mode 100644 group06/1378560653/.gitignore create mode 100644 group06/1378560653/.project diff --git a/group06/1378560653/.classpath b/group06/1378560653/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/1378560653/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1378560653/.gitignore b/group06/1378560653/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1378560653/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1378560653/.project b/group06/1378560653/.project new file mode 100644 index 0000000000..0feed82399 --- /dev/null +++ b/group06/1378560653/.project @@ -0,0 +1,17 @@ + + + 1378560653Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From ffba0ce27c81eef67ad3c6fdac9c9f72c0d1fb0f Mon Sep 17 00:00:00 2001 From: earliest Date: Thu, 23 Feb 2017 18:59:53 -0800 Subject: [PATCH 028/432] add first test class --- .../src/com/wusa/assignment1/TestDriver.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 group17/1282579502/src/com/wusa/assignment1/TestDriver.java diff --git a/group17/1282579502/src/com/wusa/assignment1/TestDriver.java b/group17/1282579502/src/com/wusa/assignment1/TestDriver.java new file mode 100644 index 0000000000..9d150fc01a --- /dev/null +++ b/group17/1282579502/src/com/wusa/assignment1/TestDriver.java @@ -0,0 +1,10 @@ +package com.wusa.assignment1; + +public class TestDriver { + + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println("Hello World"); + } + +} From 19eb0d2d588ff255988c1f01ed81ada2975a007a Mon Sep 17 00:00:00 2001 From: huaerhly <15736873360@163.com> Date: Fri, 24 Feb 2017 11:02:35 +0800 Subject: [PATCH 029/432] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/932573198/20170220/.classpath | 7 + group01/932573198/20170220/.gitignore | 1 + group01/932573198/20170220/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 92 +++++++++++ .../src/com/coding/basic/BinaryTree.java | 77 ++++++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/LinkedList.java | 144 ++++++++++++++++++ .../20170220/src/com/coding/basic/List.java | 9 ++ .../20170220/src/com/coding/basic/Queue.java | 28 ++++ .../20170220/src/com/coding/basic/Stack.java | 40 +++++ .../src/com/coding/test/ArrayListTest.java | 79 ++++++++++ .../src/com/coding/test/BinaryTreeTest.java | 29 ++++ .../src/com/coding/test/IteratorTest.java | 40 +++++ .../src/com/coding/test/LinkedListTest.java | 108 +++++++++++++ .../src/com/coding/test/QueueTest.java | 52 +++++++ .../src/com/coding/test/StackTest.java | 76 +++++++++ 17 files changed, 816 insertions(+) create mode 100644 group01/932573198/20170220/.classpath create mode 100644 group01/932573198/20170220/.gitignore create mode 100644 group01/932573198/20170220/.project create mode 100644 group01/932573198/20170220/.settings/org.eclipse.jdt.core.prefs create mode 100644 group01/932573198/20170220/src/com/coding/basic/ArrayList.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/BinaryTree.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/Iterator.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/LinkedList.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/List.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/Queue.java create mode 100644 group01/932573198/20170220/src/com/coding/basic/Stack.java create mode 100644 group01/932573198/20170220/src/com/coding/test/ArrayListTest.java create mode 100644 group01/932573198/20170220/src/com/coding/test/BinaryTreeTest.java create mode 100644 group01/932573198/20170220/src/com/coding/test/IteratorTest.java create mode 100644 group01/932573198/20170220/src/com/coding/test/LinkedListTest.java create mode 100644 group01/932573198/20170220/src/com/coding/test/QueueTest.java create mode 100644 group01/932573198/20170220/src/com/coding/test/StackTest.java diff --git a/group01/932573198/20170220/.classpath b/group01/932573198/20170220/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group01/932573198/20170220/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group01/932573198/20170220/.gitignore b/group01/932573198/20170220/.gitignore new file mode 100644 index 0000000000..65776c32fc --- /dev/null +++ b/group01/932573198/20170220/.gitignore @@ -0,0 +1 @@ +/bin/ \ No newline at end of file diff --git a/group01/932573198/20170220/.project b/group01/932573198/20170220/.project new file mode 100644 index 0000000000..82b0a5ccfd --- /dev/null +++ b/group01/932573198/20170220/.project @@ -0,0 +1,17 @@ + + + 20170220 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/932573198/20170220/.settings/org.eclipse.jdt.core.prefs b/group01/932573198/20170220/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group01/932573198/20170220/.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/932573198/20170220/src/com/coding/basic/ArrayList.java b/group01/932573198/20170220/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..039e83f095 --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + /** + * 扩容 + */ + private void expansion() { + if (elementData.length <= size) + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + + /** + * 越界 + */ + private void outOfBoundsForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private void outOfBoundsForOther(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + public void add(Object o) { + expansion(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + outOfBoundsForAdd(index); + expansion(); + for (int i = size - 1; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + outOfBoundsForOther(index); + return elementData[index]; + } + + public Object remove(int index) { + outOfBoundsForOther(index); + Object re = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return re; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return Arrays.toString(elementData); + } + + public Iterator iterator() { + return new ArrayIterator(); + } + + private class ArrayIterator implements Iterator { + + int pos = -1; + + @Override + public boolean hasNext() { + return size > ++pos ? true : false; + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + +} diff --git a/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java b/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..730d7e7b3a --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +public class BinaryTree { + + private BinaryTreeNode tNode; + + @Override + public String toString() { + return tNode + ""; + } + + public void insert(Object o) { + tNode = insert(o, tNode); + } + + public BinaryTreeNode insert(Object o, BinaryTreeNode node) { + if (node == null) { + node = new BinaryTreeNode(o); + } else { + int result = o.toString().compareTo(node.getData().toString()); + if (result < 0) + node.setLeft(insert(o, node.getLeft())); + if (result > 0) + node.setRight(insert(o, node.getRight())); + } + return node; + } + + private static class BinaryTreeNode { + + private BinaryTreeNode left; + + private Object data; + + private BinaryTreeNode right; + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Object data) { + this.left = null; + this.data = data; + this.right = null; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @Override + public String toString() { + return "[" + left + ", " + data + ", " + right + "]"; + } + + } + +} diff --git a/group01/932573198/20170220/src/com/coding/basic/Iterator.java b/group01/932573198/20170220/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group01/932573198/20170220/src/com/coding/basic/LinkedList.java b/group01/932573198/20170220/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..db61384e4c --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/LinkedList.java @@ -0,0 +1,144 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + + public Node getHead() { + return head; + } + + public LinkedList() { + this.head = new Node(); + } + + @Override + public String toString() { + return "[" + head + "]"; + } + + private void outOfBoundsForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private void outOfBoundsForOther(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + public void add(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + public void add(int index, Object o) { + outOfBoundsForAdd(index); + if(size == index) + add(o); + else{ + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node nextNode = prevNode.next; + Node node = new Node(o); + prevNode.next = node; + node.next = nextNode; + size++; + } + } + + public Object get(int index) { + outOfBoundsForOther(index); + Node node = head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + outOfBoundsForOther(index); + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node node = prevNode.next; + prevNode.next = node.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + Node node = head.next; + head.next = newNode; + newNode.next = node; + size++; + } + + public void addLast(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + private void noSuchEle() { + if (head.next == null) + throw new NoSuchElementException("没有这个元素"); + } + + public Object removeFirst() { + noSuchEle(); + Node node = head.next; + head.next = node.next; + size--; + return node.data; + } + + public Object removeLast() { + noSuchEle(); + Node node = head; + for(int i=0;i Date: Thu, 23 Feb 2017 19:08:48 -0800 Subject: [PATCH 030/432] test commit from company box --- group17/1282579502/src/com/wusa/assignment1/TestDriver.java | 1 + 1 file changed, 1 insertion(+) diff --git a/group17/1282579502/src/com/wusa/assignment1/TestDriver.java b/group17/1282579502/src/com/wusa/assignment1/TestDriver.java index 9d150fc01a..d757ac09cd 100644 --- a/group17/1282579502/src/com/wusa/assignment1/TestDriver.java +++ b/group17/1282579502/src/com/wusa/assignment1/TestDriver.java @@ -5,6 +5,7 @@ public class TestDriver { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World"); + System.out.println("hello back"); } } From f63a0b343179779177664a684635b87d70fa6394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Fri, 24 Feb 2017 11:18:01 +0800 Subject: [PATCH 031/432] init --- group11/252308879/dataStructure/.classpath | 22 ++++ group11/252308879/dataStructure/.gitignore | 11 ++ group11/252308879/dataStructure/pom.xml | 25 ++++ .../org/apn/coding2017/basic/ArrayList.java | 120 ++++++++++++++++++ .../apn/coding2017/basic/BinaryTreeNode.java | 35 +++++ .../org/apn/coding2017/basic/Iterator.java | 12 ++ .../org/apn/coding2017/basic/LinkedList.java | 50 ++++++++ .../java/org/apn/coding2017/basic/List.java | 22 ++++ .../java/org/apn/coding2017/basic/Queue.java | 22 ++++ .../java/org/apn/coding2017/basic/Stack.java | 25 ++++ .../apn/coding2017/TestJavaUtilArrayList.java | 32 +++++ 11 files changed, 376 insertions(+) create mode 100644 group11/252308879/dataStructure/.classpath create mode 100644 group11/252308879/dataStructure/.gitignore create mode 100644 group11/252308879/dataStructure/pom.xml create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java create mode 100644 group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java diff --git a/group11/252308879/dataStructure/.classpath b/group11/252308879/dataStructure/.classpath new file mode 100644 index 0000000000..d0800a3fc4 --- /dev/null +++ b/group11/252308879/dataStructure/.classpath @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/group11/252308879/dataStructure/.gitignore b/group11/252308879/dataStructure/.gitignore new file mode 100644 index 0000000000..26104baec0 --- /dev/null +++ b/group11/252308879/dataStructure/.gitignore @@ -0,0 +1,11 @@ +dataStructure/.classpath +dataStructure/.project +dataStructure/.settings/ +dataStructure/.classpath + +.project +.settings/ +target/ +# 忽略IntelliJ IDEA配置文件 +*.iml +*.idea/ diff --git a/group11/252308879/dataStructure/pom.xml b/group11/252308879/dataStructure/pom.xml new file mode 100644 index 0000000000..4b756e4f0d --- /dev/null +++ b/group11/252308879/dataStructure/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + org.apn.coding2017 + dataStructure + 1.0.0-SNAPSHOT + jar + + dataStructure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..5ad2b6cd97 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java @@ -0,0 +1,120 @@ +package org.apn.coding2017.basic; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/23. + */ +public class ArrayList implements List { + + private int size; + + // 设置默认容量 不可变 + private static final int DEFAULT_CAPACITY = 10; + + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + + private Object[] elementData; + + private int modCount = 0; + + // 定义一个默认的空的数组,引用不可变 + private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; + + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = new Object[]{}; + } else { + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + } + } + + public ArrayList() { // 如果调用默认构造函数,设置容器为空的默认数组 + this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; + } + + + public boolean add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size++] = o; + return true; + } + + public Object set(int index, Object element) { + return null; + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return 0; + } + + public boolean isEmpty() { + return false; + } + + public Iterator iterator() { + return null; + } + + private void ensureCapacityInternal(int minCapacity) { + // 如果是容器是默认的空的数组 + if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { + // 取得为与默认容量相比的较大值 + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + ensureExplicitCapacity(minCapacity); + // 调用确保有能力放下那么多元素 + } + + private void ensureExplicitCapacity(int minCapacity) { + modCount++; + + // 防止容量溢出。所需的最小容器大于数组长度 + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + /** + * 扩充容量 + * @param minCapacity + */ + private void grow(int minCapacity){ + int oldCapacity = elementData.length; + // 得到一个新的容量大小,为 oldCapacity 的1.5倍 + int newCapacity = oldCapacity + (oldCapacity >> 1); + // 如果扩容后的大小比,最小容量(DEFAULT_CAPACITY: 10)小 + if (newCapacity - minCapacity < 0){ + newCapacity = minCapacity; + }else if (newCapacity - MAX_ARRAY_SIZE > 0){ // 扩容后比最大的还大,考虑溢出 + // + newCapacity = hugeCapacity(minCapacity); + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /** + * 这个很少用到这个判断,毕竟基本不会使用那么大容量存储 + * @param minCapacity + * @return + */ + private static int hugeCapacity(int minCapacity){ + if (minCapacity < 0){ + throw new OutOfMemoryError(); + } + return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..f63010c239 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package org.apn.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +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/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..94dc84dfdc --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java @@ -0,0 +1,12 @@ +package org.apn.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public interface Iterator { + boolean hasNext(); + + Object next(); + + void remove(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..bac49b845a --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java @@ -0,0 +1,50 @@ +package org.apn.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public class LinkedList implements List { + + Node last; + + private static class Node { + Object item; + Node next; + } + + public boolean add(Object o) { + + return true; + } + + public Object set(int index, Object element) { + return null; + } + + 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 0; + } + + public boolean isEmpty() { + return false; + } + + public Iterator iterator() { + return null; + } + + + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java new file mode 100644 index 0000000000..b76bb0eec9 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java @@ -0,0 +1,22 @@ +package org.apn.coding2017.basic; + + +/** + * Created by QiPan on 2017/2/23. + */ +public interface List { + + boolean add(Object o); + + Object set(int index, Object element); + + Object get(int index); + + Object remove(int index); + + int size(); + + boolean isEmpty(); + + Iterator iterator(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java new file mode 100644 index 0000000000..8b40827110 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java @@ -0,0 +1,22 @@ +package org.apn.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +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/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java new file mode 100644 index 0000000000..60a4c1c8fd --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package org.apn.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +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; + } +} diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java b/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java new file mode 100644 index 0000000000..3fab1e57fd --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java @@ -0,0 +1,32 @@ +package org.apn.coding2017; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by QiPan on 2017/2/23. + */ +public class TestJavaUtilArrayList { + + + @Test + public void testAdd() { + List arrayList = new ArrayList(5); + arrayList.add(new Object()); + System.out.println("sssssssssssss"); + } + + @Test + public void testRightShift() { + int x = 5; + + x = x << 1; + x = x >> 1; + + System.out.println(x); + } + +} From 70b1782212e6706a6e5468f4e2a6423fec9ed539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Fri, 24 Feb 2017 11:33:06 +0800 Subject: [PATCH 032/432] =?UTF-8?q?=E4=BF=AE=E6=94=B9gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/252308879/dataStructure/.classpath | 22 ---------------------- group11/252308879/dataStructure/.gitignore | 1 - 2 files changed, 23 deletions(-) delete mode 100644 group11/252308879/dataStructure/.classpath diff --git a/group11/252308879/dataStructure/.classpath b/group11/252308879/dataStructure/.classpath deleted file mode 100644 index d0800a3fc4..0000000000 --- a/group11/252308879/dataStructure/.classpath +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/group11/252308879/dataStructure/.gitignore b/group11/252308879/dataStructure/.gitignore index 26104baec0..c7175f9b40 100644 --- a/group11/252308879/dataStructure/.gitignore +++ b/group11/252308879/dataStructure/.gitignore @@ -1,7 +1,6 @@ dataStructure/.classpath dataStructure/.project dataStructure/.settings/ -dataStructure/.classpath .project .settings/ From 300feeacf433b3ae5ee8cb7eb694800d7bff5ba8 Mon Sep 17 00:00:00 2001 From: "yanght1454385822@qq.com" Date: Fri, 24 Feb 2017 13:56:33 +0800 Subject: [PATCH 033/432] homework --- group06/1454385822/.classpath | 6 + group06/1454385822/.gitignore | 1 + group06/1454385822/.project | 17 ++ .../src/com/coding/basic/ArrayList.java | 124 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 65 +++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 241 ++++++++++++++++++ .../1454385822/src/com/coding/basic/List.java | 11 + .../src/com/coding/basic/Queue.java | 42 +++ .../src/com/coding/basic/Stack.java | 42 +++ 10 files changed, 557 insertions(+) create mode 100644 group06/1454385822/.classpath create mode 100644 group06/1454385822/.gitignore create mode 100644 group06/1454385822/.project create mode 100644 group06/1454385822/src/com/coding/basic/ArrayList.java create mode 100644 group06/1454385822/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group06/1454385822/src/com/coding/basic/Iterator.java create mode 100644 group06/1454385822/src/com/coding/basic/LinkedList.java create mode 100644 group06/1454385822/src/com/coding/basic/List.java create mode 100644 group06/1454385822/src/com/coding/basic/Queue.java create mode 100644 group06/1454385822/src/com/coding/basic/Stack.java diff --git a/group06/1454385822/.classpath b/group06/1454385822/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/1454385822/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1454385822/.gitignore b/group06/1454385822/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1454385822/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1454385822/.project b/group06/1454385822/.project new file mode 100644 index 0000000000..35750341bb --- /dev/null +++ b/group06/1454385822/.project @@ -0,0 +1,17 @@ + + + 1454385822Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1454385822/src/com/coding/basic/ArrayList.java b/group06/1454385822/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..571ca71f21 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/ArrayList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int pos = 0; // 当前数组的末尾元素的下一位置 + + private Object[] elementData = new Object[3]; + + public void add(Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + elementData[pos++] = o; + + } + public void add(int index, Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + /* + * 情况1.index < pos && pos < elementData.length - 1 + * index 只能在pos前面 + */ + if(index <= pos && pos <= elementData.length - 1){ + Object[] tem = new Object[pos - index]; + System.arraycopy(elementData, index, tem, 0, tem.length); + elementData[index] = o; + System.arraycopy(tem, 0, elementData, index + 1, tem.length); + pos++; + }else{ + throw new IndexOutOfBoundsException(); + } + + + + } + + public Object get(int index){ + if(index < pos){ + return elementData[index]; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + //将数字删除并将该数字后面的元素向前移动一位 + if(index < pos ){ //只有index在pos之前才进行删除操作 + result = elementData[index]; + if(pos - index > 1){ //删除的不是最后一个元素 + Object[] tem = new Object[pos - index - 1]; + System.arraycopy(elementData, index + 1, tem, 0, tem.length); + for(int i=0; i i2 ? 1 : (i1 == i2 ? 0 : -1); +// return result; +// } +// +//} diff --git a/group06/1454385822/src/com/coding/basic/Iterator.java b/group06/1454385822/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d2e7a2c23c --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/1454385822/src/com/coding/basic/LinkedList.java b/group06/1454385822/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..6197b9fb35 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/LinkedList.java @@ -0,0 +1,241 @@ +package com.coding.basic; + + +public class LinkedList implements List{ + + //private Node head; + private Node pre; //指向当前结点的前一个元素 + private Node pHead; //头节点指向第一个元素 + private Node cur; //指向链表的最后一个元素 + private int num = 0; //链表中的元素个数 + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if(pHead == null){ //链表为空,从第一个元素添加 + pHead = cur = node; + pHead.pre = null; + }else{ + node.pre = cur; //前一结点向后移动一位 + cur.next = node; // 添加元素 + cur = cur.next; //当前结点向后移动一位 + } + num++; //链表数目增1 + } + + /** + * 根据索引找到对应的结点 + * @param index + * @return + */ + public Node findNode(int index){ + Node node = pHead; + int tem = 0; + while(tem++ != index){ + node = node.next; + } + return node; + } + + public void add(int index , Object o){ + if(num == 0 || index == num){ + add(o); + return; + } + if(index <= num-1 && index > 0){ + Node node = new Node(); + node.data = o; + Node tem = findNode(index); + Node preNode = tem.pre; + Node posNode = tem.next; + preNode.next = node; + node.next = posNode; + posNode.pre = node; + num++; + return; + } + if(index == 0){ + Node node = new Node(); + node.data = o; + pHead.pre = node; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object get(int index){ + if(index <= num - 1 && index >= 0){ + return findNode(index).data; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + if(index >0 && index < num - 1){ //删除链表中间的元素 + Node node = findNode(index); + result = node.data; + Node preNode = node.pre; + Node posNode = node.next; + preNode.next = posNode; + posNode.pre = preNode; + num--; + return result; + } + if(index == 0 && num > 0){ //删除第一个元素 + Node node = pHead.next; + result = pHead.data; + node.pre = null; + pHead = node; + num--; + return result; + } + if(index == num - 1 && num > 0){ //删除最后一个元素 + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return num; + } + + public void addFirst(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = null; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + + } + public void addLast(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = cur; + cur.next = node; + node.next = null; + cur = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object removeFirst(){ + Object result; + if(num > 0){ + result = pHead.data; + if(num == 1){ + pHead = null; + num = 0; + } + if(num > 1){ + pHead = pHead.next; + pHead.pre = null; + num--; + } + return result; + } + throw new IndexOutOfBoundsException(); + } + + public Object removeLast(){ + Object result; + if(num == 1){ + result = pHead.data; + pHead = null; + num = 0; + return result; + } + if(num > 1){ + + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + public Iterator iterator(){ + return new Iterator(){ + int cur = 0; + Node node = pHead; + @Override + public boolean hasNext() { + if(cur++ < num){ + return true; + } + return false; + } + + @Override + public Object next() { + Object result = node.data; + node = node.next; + return result; + } + + }; + } + + + private static class Node{ + Object data; + Node pre; + Node next; + + } + + public static void main(String[]args){ + LinkedList list = new LinkedList(); + list.add(1); +// list.add(2); +// list.add(3); +// list.add(4); +// list.add(0, 0); +// list.addFirst(0); +// list.addLast(5); +// list.removeFirst(); + System.out.println(list.removeLast()); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } + +} + + + + + + + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/List.java b/group06/1454385822/src/com/coding/basic/List.java new file mode 100644 index 0000000000..1fd3aa61b3 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/List.java @@ -0,0 +1,11 @@ +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/group06/1454385822/src/com/coding/basic/Queue.java b/group06/1454385822/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c77317ef21 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + private int num = 0; + + public void enQueue(Object o){ + elementData.add(o); + num++; + } + + public Object deQueue(){ + num--; + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return num <= 0; + } + + public int size(){ + return num; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + System.out.println("当前队列的长度为:"+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQueue()); + } + + } + +} + + + diff --git a/group06/1454385822/src/com/coding/basic/Stack.java b/group06/1454385822/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2900430728 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Stack.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int num = 0; + + public void push(Object o){ + elementData.add(o); + num++; + } + + public Object pop(){ + + return elementData.remove(--num) ; + } + + public Object peek(){ + return elementData.get(num - 1); + } + public boolean isEmpty(){ + return num <= 0 ; + } + public int size(){ + return num; + } + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.peek()); + System.out.println(stack.size()); +// while(!stack.isEmpty()){ +// System.out.println(stack.pop()); +// } + } +} + + + + From 8ee1ef8e1e983b035c7acecf3ae6f221ffd80ac5 Mon Sep 17 00:00:00 2001 From: Haochen Date: Fri, 24 Feb 2017 14:12:50 +0800 Subject: [PATCH 034/432] add unit test for data structures(week 1) --- .../src/datastructure/basic/ArrayList.java | 71 +++- .../datastructure/basic/BinarySortedTree.java | 61 +++ .../src/datastructure/basic/Iterator.java | 5 +- .../src/datastructure/basic/LinkedList.java | 64 ++- .../code/src/datastructure/basic/List.java | 11 +- .../code/src/datastructure/basic/Queue.java | 40 +- .../code/src/datastructure/basic/Stack.java | 33 +- .../exception/EmptyListException.java | 8 + .../exception/EmptyQueueException.java | 7 + .../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 --- .../datastructure/basic/ArrayListTest.java | 152 +++++++ .../basic/BinarySortedTreeTest.java | 60 +++ .../basic/DataStructureTestSuite.java | 13 + .../datastructure/basic/LinkedListTest.java | 88 ++++ .../test/datastructure/basic/QueueTest.java | 108 +++++ .../test/datastructure/basic/StackTest.java | 93 +++++ 21 files changed, 735 insertions(+), 1053 deletions(-) create mode 100644 group01/895457260/code/src/datastructure/basic/BinarySortedTree.java create mode 100644 group01/895457260/code/src/datastructure/exception/EmptyListException.java create mode 100644 group01/895457260/code/src/datastructure/exception/EmptyQueueException.java delete mode 100644 group01/895457260/code/src/datastructure/jdklist/MyArrayList.java delete mode 100644 group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java delete mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java delete mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java delete mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyQueue.java delete mode 100644 group01/895457260/code/src/datastructure/nongeneric/MyStack.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/QueueTest.java create mode 100644 group01/895457260/code/src/test/datastructure/basic/StackTest.java diff --git a/group01/895457260/code/src/datastructure/basic/ArrayList.java b/group01/895457260/code/src/datastructure/basic/ArrayList.java index ed7df1a702..28eb439c1b 100644 --- a/group01/895457260/code/src/datastructure/basic/ArrayList.java +++ b/group01/895457260/code/src/datastructure/basic/ArrayList.java @@ -1,10 +1,10 @@ package datastructure.basic; public class ArrayList implements List { - + private int size = 0; - - Object[] elementData; + + private Object[] elementData; public ArrayList() { elementData = new Object[100]; @@ -14,37 +14,37 @@ public ArrayList(int initCapacity) { elementData = new Object[initCapacity]; } - public void add(Object o){ + public void add(Object o) { autoGrow(); elementData[size()] = o; size++; } - public void add(int index, Object o){ + 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){ + public Object get(int index) { checkIndex(index); return elementData[index]; } - public Object remove(int index){ + public Object remove(int index) { checkIndex(index); Object removed = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size() - index); + System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); size--; return removed; } - public int size(){ + public int size() { return size; } - public Iterator iterator(){ + public Iterator iterator() { return new Iterator() { int index = -1; @Override @@ -82,4 +82,55 @@ private String indexOutOfBoundMessage(int index) { return "index: " + index + ", size: " + size(); } + public static void main(String[] args) { + ArrayList list = new ArrayList(); + 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-----------------iterator"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + 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(); + } + + public void print() { + Iterator iterator = iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + System.out.println("\nsize: " + size()); + } } diff --git a/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java new file mode 100644 index 0000000000..ff159cef59 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java @@ -0,0 +1,61 @@ +package datastructure.basic; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class BinarySortedTree { + + private BinaryTreeNode root = null; + + public void traversal(Visitor visitor) { + traversal(root, visitor); + } + + private void traversal(BinaryTreeNode node, Visitor visitor) { + if (node == null) { + return; + } + traversal(node.getLeft(), visitor); + visitor.visit(node); + traversal(node.getRight(), visitor); + } + + public interface Visitor { + void visit(BinaryTreeNode node); + } + + //不递归的写法 + public void add(T o) { + //根节点空,直接加入 + if (root == null) { + root = new BinaryTreeNode(); + root.setData(o); + } else { + BinaryTreeNode target = root; + //从根结点不断向下比较target和o,o小则往左,o大则往右,相等不加入 + while (true) { + int compare = o.compareTo(target.getData()); + if (compare == 0) {//相等不加入 + return; + } else if (compare < 0) {//o小往左 + if (target.getLeft() == null) {//左空则加入 + target.setLeft(new BinaryTreeNode()); + target.getLeft().setData(o); + return; + } else {//不空继续比较 + target = target.getLeft(); + } + } else {//o大往右 + if (target.getRight() == null) { + target.setRight(new BinaryTreeNode()); + target.getRight().setData(o); + return; + } else { + target = target.getRight(); + } + } + } + } + } +} diff --git a/group01/895457260/code/src/datastructure/basic/Iterator.java b/group01/895457260/code/src/datastructure/basic/Iterator.java index 4787b60086..c1fb7ae8a5 100644 --- a/group01/895457260/code/src/datastructure/basic/Iterator.java +++ b/group01/895457260/code/src/datastructure/basic/Iterator.java @@ -1,7 +1,6 @@ package datastructure.basic; public interface Iterator { - public boolean hasNext(); - public Object next(); - + boolean hasNext(); + Object next(); } diff --git a/group01/895457260/code/src/datastructure/basic/LinkedList.java b/group01/895457260/code/src/datastructure/basic/LinkedList.java index 189280da30..174044c546 100644 --- a/group01/895457260/code/src/datastructure/basic/LinkedList.java +++ b/group01/895457260/code/src/datastructure/basic/LinkedList.java @@ -1,5 +1,7 @@ package datastructure.basic; +import datastructure.exception.EmptyListException; + public class LinkedList implements List { private Node head; @@ -8,58 +10,86 @@ public class LinkedList implements List { public LinkedList() { head = new Node(); } - - public void add(Object o){ + + @Override + public void add(Object o) { addLast(o); } - public void add(int index , Object o){ + @Override + 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){ + @Override + public Object get(int index) { checkIndex(index); - return findNode(index); + return findNode(index).data; } - public Object remove(int index){ + + @Override + 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(){ + + @Override + public int size() { return size; } - public void addFirst(Object o){ + public void addFirst(Object o) { Node node = new Node(); node.data = o; addNode(node, head); } - public void addLast(Object o){ + + public void addLast(Object o) { Node node = new Node(); node.data = o; - Node pre = findNode(size()); + Node pre = findNode(size() - 1); addNode(node, pre); } - public Object removeFirst(){ + + public Object removeFirst() { + if (size() == 0) { + throw new EmptyListException(); + } Node removed = head.next; removeNode(head.next, head); return removed.data; } - public Object removeLast(){ + + public Object removeLast() { + if (size() == 0) { + throw new EmptyListException(); + } return remove(size() - 1); } - public Iterator iterator(){ - return null; + + @Override + public Iterator iterator() { + return new Iterator() { + Node node = head; + @Override + public boolean hasNext() { + return node.next != null; + } + + @Override + public Object next() { + node = node.next; + return node.data; + } + }; } - - + private static class Node{ Object data; Node next; diff --git a/group01/895457260/code/src/datastructure/basic/List.java b/group01/895457260/code/src/datastructure/basic/List.java index 39394c145e..2f085701c5 100644 --- a/group01/895457260/code/src/datastructure/basic/List.java +++ b/group01/895457260/code/src/datastructure/basic/List.java @@ -1,9 +1,10 @@ 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(); + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); + Iterator iterator(); } diff --git a/group01/895457260/code/src/datastructure/basic/Queue.java b/group01/895457260/code/src/datastructure/basic/Queue.java index ee96a1a83e..edd0a6a29e 100644 --- a/group01/895457260/code/src/datastructure/basic/Queue.java +++ b/group01/895457260/code/src/datastructure/basic/Queue.java @@ -1,12 +1,22 @@ package datastructure.basic; +import datastructure.exception.EmptyQueueException; + public class Queue { //数组实现自增长的循环队列 - private Object[] array = new Object[10]; + private Object[] array; private int head = 0; private int rear = 0; - public void enQueue(Object o){ + public Queue() { + this.array = new Object[10]; + } + + public Queue(int initCapacity) { + this.array = new Object[initCapacity]; + } + + public void enQueue(Object o) { int target = mapIndex(rear); autoGrow(); array[target] = o; @@ -14,16 +24,19 @@ public void enQueue(Object o){ } public Object deQueue() { + if (isEmpty()) { + throw new EmptyQueueException(); + } Object obj = array[mapIndex(head)]; head++; return obj; } - - public boolean isEmpty(){ + + public boolean isEmpty() { return head == rear; } - public int size(){ + public int size() { return rear - head; } @@ -52,21 +65,4 @@ private int nextCapacity() { 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 index 772ab16385..ab4fc874ae 100644 --- a/group01/895457260/code/src/datastructure/basic/Stack.java +++ b/group01/895457260/code/src/datastructure/basic/Stack.java @@ -1,43 +1,32 @@ package datastructure.basic; +import java.util.EmptyStackException; + public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o){ + public void push(Object o) { elementData.add(o); } - public Object pop(){ + public Object pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } Object peek = peek(); elementData.remove(elementData.size() - 1); return peek; } - - public Object peek(){ + + public Object peek() { return elementData.get(elementData.size() - 1); } - public boolean isEmpty(){ + public boolean isEmpty() { return size() == 0; } - public int size(){ + 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/exception/EmptyListException.java b/group01/895457260/code/src/datastructure/exception/EmptyListException.java new file mode 100644 index 0000000000..6f38ed6c43 --- /dev/null +++ b/group01/895457260/code/src/datastructure/exception/EmptyListException.java @@ -0,0 +1,8 @@ +package datastructure.exception; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class EmptyListException extends RuntimeException { +} diff --git a/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java b/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java new file mode 100644 index 0000000000..071a366ed8 --- /dev/null +++ b/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java @@ -0,0 +1,7 @@ +package datastructure.exception; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class EmptyQueueException extends RuntimeException {} diff --git a/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java b/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java deleted file mode 100644 index 3eb826c4cc..0000000000 --- a/group01/895457260/code/src/datastructure/jdklist/MyArrayList.java +++ /dev/null @@ -1,304 +0,0 @@ -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 deleted file mode 100644 index 44b2368f11..0000000000 --- a/group01/895457260/code/src/datastructure/jdklist/MyLinkedList.java +++ /dev/null @@ -1,382 +0,0 @@ -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 deleted file mode 100644 index ca04443db2..0000000000 --- a/group01/895457260/code/src/datastructure/nongeneric/MyBaseList.java +++ /dev/null @@ -1,137 +0,0 @@ -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 deleted file mode 100644 index 893c1d88ca..0000000000 --- a/group01/895457260/code/src/datastructure/nongeneric/MyBinaryTree.java +++ /dev/null @@ -1,61 +0,0 @@ -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 deleted file mode 100644 index c7d6e2c13b..0000000000 --- a/group01/895457260/code/src/datastructure/nongeneric/MyQueue.java +++ /dev/null @@ -1,43 +0,0 @@ -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 deleted file mode 100644 index 16e65c304a..0000000000 --- a/group01/895457260/code/src/datastructure/nongeneric/MyStack.java +++ /dev/null @@ -1,47 +0,0 @@ -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() + "] "); - } - } -} diff --git a/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java b/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java new file mode 100644 index 0000000000..42b9144d38 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java @@ -0,0 +1,152 @@ +package test.datastructure.basic; + +import datastructure.basic.ArrayList; +import datastructure.basic.Iterator; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * ArrayList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class ArrayListTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + protected final List getList() { + List list = createList(); + init(list); + return list; + } + + List createList() { + return new ArrayList(5); + } + + private void init(List list) { + for (int i = 1; i <= 5; ++i) { + list.add(i); + } + } + + protected final Object[] toArray(List list) { + Object[] array = new Object[list.size()]; + Iterator iterator = list.iterator(); + int pos = 0; + while (iterator.hasNext()) { + array[pos++] = iterator.next(); + } + return array; + } + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { +//TODO: Test goes here... + List list = getList(); + for (int i = 6; i <= 10; ++i) { + list.add(i); + } + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + Assert.assertEquals(list.size(), 10); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {nowSize + 1, -1, nowSize, nowSize, 0, 1}; + Object[] values = {0, 0, 300, 400, 100, 200}; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + list.add(indexes[i], values[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(toArray(list), new Object[]{100, 200, 1, 2, 3, 4, 5, 400, 300}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize + 4); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, 0, 1, nowSize - 1, nowSize - 2}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.get(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 1, 2, 5, 4}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, nowSize - 2, nowSize - 2, 1, 0}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.remove(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 4, 5, 2, 1}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize - 4); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { +//TODO: Test goes here... + List list = getList(); + Iterator iterator = list.iterator(); + Object[] values = new Object[list.size()]; + int pos = 0; + while (iterator.hasNext()) { + values[pos++] = iterator.next(); + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5}); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java new file mode 100644 index 0000000000..6bdf72450a --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java @@ -0,0 +1,60 @@ +package test.datastructure.basic; + +import datastructure.basic.BinarySortedTree; +import datastructure.basic.BinaryTreeNode; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * BinarySortedTree Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class BinarySortedTreeTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private BinarySortedTree getTree() { + return new BinarySortedTree<>(); + } + + /** + * Method: add(T o) + */ + @Test + public void testAdd() throws Exception { +//TODO: Test goes here... + BinarySortedTree tree = getTree(); + int[] addValues = {5, 3, 1, 7, 6, 4, 8}; + for (int i : addValues) { + tree.add(i); + } + + final Object[] left = new Object[addValues.length]; + final Object[] value = new Object[addValues.length]; + final Object[] right = new Object[addValues.length]; + tree.traversal(new BinarySortedTree.Visitor() { + int pos = 0; + @Override + public void visit(BinaryTreeNode node) { + left[pos] = node.getLeft() == null ? null : (int) node.getLeft().getData(); + value[pos] = node.getData(); + right[pos] = node.getRight() == null ? null : (int) node.getRight().getData(); + pos++; + } + }); + Assert.assertArrayEquals(left, new Object[]{null, 1, null, 3, null, 6, null}); + Assert.assertArrayEquals(value, new Object[]{1, 3, 4, 5, 6, 7, 8}); + Assert.assertArrayEquals(right, new Object[]{null, 4, null, 7, null, 8, null}); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java b/group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java new file mode 100644 index 0000000000..0c50610601 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java @@ -0,0 +1,13 @@ +package test.datastructure.basic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ArrayListTest.class, LinkedListTest.class, + BinarySortedTreeTest.class, QueueTest.class, StackTest.class}) +public class DataStructureTestSuite {} diff --git a/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java b/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java new file mode 100644 index 0000000000..0ab03eb589 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java @@ -0,0 +1,88 @@ +package test.datastructure.basic; + +import datastructure.exception.EmptyListException; +import datastructure.basic.LinkedList; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; + +/** + * LinkedList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class LinkedListTest extends ArrayListTest { + + @Override + List createList() { + return new LinkedList(); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addFirst(100); + Assert.assertArrayEquals(toArray(list), new Object[]{100, 1, 2, 3, 4, 5}); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addLast(100); + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 100}); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeFirst(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeLast(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/QueueTest.java b/group01/895457260/code/src/test/datastructure/basic/QueueTest.java new file mode 100644 index 0000000000..df7587bd3c --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/QueueTest.java @@ -0,0 +1,108 @@ +package test.datastructure.basic; + +import datastructure.exception.EmptyQueueException; +import datastructure.basic.Queue; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Queue Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class QueueTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Queue getQueue() { + Queue queue = new Queue(5); + for (int i = 1; i <= 5; ++i) { + queue.enQueue(i); + } + return queue; + } + + private void assertQueue(Queue queue, Object[] actual) { + Class clazz = Queue.class; + Object[] array = null; + int head = 0; + int rear = 0; + Method mapIndex = null; + try { + Field arrayField = clazz.getDeclaredField("array"); + Field headField = clazz.getDeclaredField("head"); + Field rearField = clazz.getDeclaredField("rear"); + mapIndex = clazz.getDeclaredMethod("mapIndex", int.class); + arrayField.setAccessible(true); + headField.setAccessible(true); + rearField.setAccessible(true); + mapIndex.setAccessible(true); + array = (Object[]) arrayField.get(queue); + head = (int) headField.get(queue); + rear = (int) rearField.get(queue); + } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + } + int size = queue.size(); + Object[] excepted = new Object[size]; + int pos = 0; + try { + while (head < rear) { + excepted[pos++] = array[(int) mapIndex.invoke(queue, head)]; + head++; + } + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + for (int i = 6; i <= 10; ++i) { + queue.enQueue(i); + } + assertQueue(queue, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + int count = queue.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = queue.deQueue(); + } catch (EmptyQueueException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertQueue(queue, new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/StackTest.java b/group01/895457260/code/src/test/datastructure/basic/StackTest.java new file mode 100644 index 0000000000..ac2bd31a22 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/StackTest.java @@ -0,0 +1,93 @@ +package test.datastructure.basic; + +import datastructure.basic.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.util.EmptyStackException; + +/** + * Stack Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class StackTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Stack getStack() { + Stack stack = new Stack(); + for (int i = 1; i <= 5; ++i) { + stack.push(i); + } + return stack; + } + + private void assertStack(Stack stack, Object[] actual) { + Class clazz = Stack.class; + ArrayList elementData = null; + try { + Field field = clazz.getDeclaredField("elementData"); + field.setAccessible(true); + elementData = (ArrayList) field.get(stack); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + Object[] excepted = null; + if (elementData != null) { + int size = stack.size(); + excepted = new Object[size]; + for (int i = 0; i < size; ++i) { + excepted[i] = elementData.get(i); + } + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + for (int i = 6; i <= 10; ++i) { + stack.push(i); + } + assertStack(stack, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + int count = stack.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = stack.pop(); + } catch (EmptyStackException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertStack(stack, new Object[0]); + } +} From 2cff6559f852d0f9514f7f69536a0305c7b7b35d Mon Sep 17 00:00:00 2001 From: Haochen Date: Fri, 24 Feb 2017 14:37:25 +0800 Subject: [PATCH 035/432] add unit test for data structures(week 1) --- .../code/src/datastructure/basic/BinarySortedTree.java | 10 +++++----- .../test/datastructure/basic/BinarySortedTreeTest.java | 2 +- .../{DataStructureTestSuite.java => TestSuite.java} | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) rename group01/895457260/code/src/test/datastructure/basic/{DataStructureTestSuite.java => TestSuite.java} (88%) diff --git a/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java index ff159cef59..b251ff02ee 100644 --- a/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java +++ b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java @@ -8,17 +8,17 @@ public class BinarySortedTree { private BinaryTreeNode root = null; - public void traversal(Visitor visitor) { - traversal(root, visitor); + public void traverse(Visitor visitor) { + traverse(root, visitor); } - private void traversal(BinaryTreeNode node, Visitor visitor) { + private void traverse(BinaryTreeNode node, Visitor visitor) { if (node == null) { return; } - traversal(node.getLeft(), visitor); + traverse(node.getLeft(), visitor); visitor.visit(node); - traversal(node.getRight(), visitor); + traverse(node.getRight(), visitor); } public interface Visitor { diff --git a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java index 6bdf72450a..ce15d5622c 100644 --- a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java +++ b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java @@ -43,7 +43,7 @@ public void testAdd() throws Exception { final Object[] left = new Object[addValues.length]; final Object[] value = new Object[addValues.length]; final Object[] right = new Object[addValues.length]; - tree.traversal(new BinarySortedTree.Visitor() { + tree.traverse(new BinarySortedTree.Visitor() { int pos = 0; @Override public void visit(BinaryTreeNode node) { diff --git a/group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java b/group01/895457260/code/src/test/datastructure/basic/TestSuite.java similarity index 88% rename from group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java rename to group01/895457260/code/src/test/datastructure/basic/TestSuite.java index 0c50610601..f7c5868383 100644 --- a/group01/895457260/code/src/test/datastructure/basic/DataStructureTestSuite.java +++ b/group01/895457260/code/src/test/datastructure/basic/TestSuite.java @@ -10,4 +10,4 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ArrayListTest.class, LinkedListTest.class, BinarySortedTreeTest.class, QueueTest.class, StackTest.class}) -public class DataStructureTestSuite {} +public class TestSuite {} From 5e07f16a4eb16d907ad9f21e7a29f4e158b44e0d Mon Sep 17 00:00:00 2001 From: dream <763878069@qq.coom> Date: Fri, 24 Feb 2017 15:52:48 +0800 Subject: [PATCH 036/432] homework --- group03/763878069/.classpath | 6 + group03/763878069/.gitignore | 1 + group03/763878069/.project | 17 ++ .../src/cmj/datastructure/list/ArrayList.java | 154 +++++++++++++ .../cmj/datastructure/list/LinkedList.java | 208 ++++++++++++++++++ .../src/cmj/datastructure/list/List.java | 13 ++ .../src/cmj/datastructure/list/Queue.java | 128 +++++++++++ .../src/cmj/datastructure/list/Stack.java | 58 +++++ .../src/cmj/datastructure/tree/BSTree.java | 191 ++++++++++++++++ .../cmj/datastructure/tree/BinaryTree.java | 185 ++++++++++++++++ 10 files changed, 961 insertions(+) create mode 100644 group03/763878069/.classpath create mode 100644 group03/763878069/.gitignore create mode 100644 group03/763878069/.project create mode 100644 group03/763878069/src/cmj/datastructure/list/ArrayList.java create mode 100644 group03/763878069/src/cmj/datastructure/list/LinkedList.java create mode 100644 group03/763878069/src/cmj/datastructure/list/List.java create mode 100644 group03/763878069/src/cmj/datastructure/list/Queue.java create mode 100644 group03/763878069/src/cmj/datastructure/list/Stack.java create mode 100644 group03/763878069/src/cmj/datastructure/tree/BSTree.java create mode 100644 group03/763878069/src/cmj/datastructure/tree/BinaryTree.java diff --git a/group03/763878069/.classpath b/group03/763878069/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group03/763878069/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/763878069/.gitignore b/group03/763878069/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/763878069/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/763878069/.project b/group03/763878069/.project new file mode 100644 index 0000000000..14ea6baea5 --- /dev/null +++ b/group03/763878069/.project @@ -0,0 +1,17 @@ + + + SimpleDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/763878069/src/cmj/datastructure/list/ArrayList.java b/group03/763878069/src/cmj/datastructure/list/ArrayList.java new file mode 100644 index 0000000000..21286512d0 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/ArrayList.java @@ -0,0 +1,154 @@ +package cmj.datastructure.list; + +import java.util.Arrays; +import java.util.Collection; + +public class ArrayList implements List { + private transient Object[] elementData; + private int size; + + /** + * ArrayList初始化无参数构造函数 + */ + public ArrayList() { + this(10); + } + + /** + * ArrayList带容量的构造函数 + * + * @param initialCapacity初始化容量 + */ + public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + // 新建一个数组 + this.elementData = new Object[initialCapacity]; + } + + /** + * 检查数组的容量 + * + * @param neededMinCapacity所需最小的容量 + */ + public void ensureCapacity(int neededMinCapacity) { + int currCapacity = elementData.length;// 获取当前数据的全部容量 + // 需要扩容的情况 + if (neededMinCapacity > currCapacity) { + int newCapacity = (currCapacity * 3) / 2 + 1;// 计算新的容量 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 添加数据 + * + * @param o要添加的元素 + * @return 是否添加成功 + */ + public void add(Object o) { + // 确定ArrayList的容量大小 + ensureCapacity(size + 1); // Increments modCount!! + // 添加o到ArrayList中 + elementData[size++] = o; + } + + /** + * 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 + * + * @param index要用于检查的索引 + */ + private void RangeCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); + } + + /** + * 向指定的位置添加元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + RangeCheck(index); + ensureCapacity(size + 1);// 检查容量 + + /* 将原数组从第index个位置复制到原数组第index+1个位置上,一共移动size-index(也就是后面剩下的)个元素 */ + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int growthNum = a.length; + ensureCapacity(size + growthNum); // Increments modCount + System.arraycopy(a, 0, elementData, size, growthNum); + size += growthNum; + return growthNum != 0; + } + + public Object get(int index) { + RangeCheck(index); + return elementData[index]; + + } + + public Object remove(int index) { + RangeCheck(index); + int numMoved = size - index - 1;// 删除后需要移动的对象 + Object RemovedValue = elementData[index]; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null; + return RemovedValue; + } + + public int size() { + return size; + } + + @Override + public String toString() { + String arraylist = "["; + for (int i = 0; i < size; i++) { + if (i == size - 1) { + arraylist += elementData[i].toString() + "]"; + } else { + arraylist += elementData[i].toString() + " ,"; + } + } + return arraylist; + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(5); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + arrayList.add(6); + System.out.println(arrayList); + arrayList.add(1, 1234); + System.out.println(arrayList); + arrayList.remove(1); + System.out.println(arrayList); + System.out.println(arrayList.get(5)); + + ArrayList stringArraylist = new ArrayList(3); + stringArraylist.add("Hello "); + stringArraylist.add("string "); + stringArraylist.add("arraylist"); + System.out.println(stringArraylist); + + ArrayList mixArraylist = new ArrayList(5); + mixArraylist.add("String"); + mixArraylist.add(1); + mixArraylist.add('f'); + mixArraylist.add(3.1f); + mixArraylist.add(4L); + System.out.println(mixArraylist); + } +} diff --git a/group03/763878069/src/cmj/datastructure/list/LinkedList.java b/group03/763878069/src/cmj/datastructure/list/LinkedList.java new file mode 100644 index 0000000000..c36197b360 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/LinkedList.java @@ -0,0 +1,208 @@ +package cmj.datastructure.list; + +public class LinkedList implements List { + + private Node head;// 头结点 + private Node current;// 尾结点 + private int size; + + public LinkedList() { + // 头指针和尾指针都指向头结点 + head = new Node(null, null); + current = head; + } + + /** + * 添加元素 + * + * @param o——用于添加的元素 + */ + public void add(Object o) { + Node node = new Node(o, null);// 新建一个结点 + current.next = node;// 尾指针指向它 + current = current.next;// 尾指针指向最后一个元素 + size++; + } + + /** + * 在第index个位置插入元素 + * + * @param index——要插入的位置 + * @param o——用于插入的对象 + */ + public void add(int index, Object o) { + Node node = new Node(o, null);// 新建一个结点 + if (index == 0) { + addFirst(o); + } else { + Node curr = (Node) this.get(index - 1);// 获得前一个结点 + Node behind = (Node) this.get(index);// 获得后一个结点 + // 在这两个结点之间插入新的元素,修改引用指向 + curr.next = node; + node.next = behind; + size++; + } + + } + + /** + * 随机访问index位置上的元素 + * + * @param index——元素的位置 + * @return——对应的元素 + */ + public Object get(int index) { + RangeCheck(index);// 检查索引是否越界 + Node curr = head;// 得到头结点的引用 + // 从头结点开始遍历到第index个元素 + for (int i = 0; i <= index; i++) + curr = curr.next; + return curr; + } + + /** + * 删除第index个位置上的元素 + * + * @param index + * @return + */ + public Object remove(int index) { + RangeCheck(index);// 检查索引是否越界 + if (0 == index) { + return removeFirst(); + } else { + Node toRemove = (Node) this.get(index);// 获得要删除的结点 + Node preRemove = (Node) this.get(index - 1);// 获得前一个结点 + preRemove.next = toRemove.next;// 将前一个结点指向要删除的结点的下一个结点 + size--; + return toRemove; + } + + } + + /** + * 获取元素的大小 + * + * @return + */ + public int size() { + return size; + } + + /** + * 在链表头部增加元素 + * + * @param o——要增加的元素 + */ + public void addFirst(Object o) { + Node node = new Node(o, null);// 新建一个结点 + node.next = head.next;// 结点指向第一个元素 + head.next = node;// 将头结点指向它 + size++; + } + + /** + * 在链表末尾添加元素 + * + * @param o——要添加的元素 + */ + public void addLast(Object o) { + Node node = new Node(o, null);// 新建一个结点 + current.next.next = node;// 尾结点的next指向新建的结点 + current.next = node;// 尾结点引用指向向新结点 + size++; + } + + /** + * 移除第一个元素 + * + * @return——移除元素 + */ + public Object removeFirst() { + Node curr = head.next;// 新建一个引用记录第一个结点 + head.next = curr.next;// 头指针移动到原第二个元素上 + size--; + return curr; + } + + /** + * 移除最后一个元素 + * + * @return——移除元素 + */ + public Object removeLast() { + Node remove = current.next; + Node pre = (Node) this.get(size - 2);// 获得倒数第二个结点 + current.next = pre; + pre.next = null; + size--; + return remove; + } + + /** + * 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 + * + * @param index要用于检查的索引 + */ + private void RangeCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); + } + + /** + * 重写toString()方法 + */ + @Override + public String toString() { + String linkedlist = "["; + Node visit = head; + while (visit.next != null) { + visit = visit.next; + if (visit.next == null) { + linkedlist += visit.data.toString() + "]"; + } else { + linkedlist += visit.data.toString() + "--->"; + } + } + return linkedlist; + } + + /** + * 结点内部类,主要要声明为static的 + * + * @author think + * + */ + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + System.out.println(list); + System.out.println(((Node) list.get(3)).data); + list.add(4, "4"); + System.out.println(list); + list.add(0, "0"); + System.out.println(list); + list.addLast("last"); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.remove(1)).data); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.removeFirst()).data); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.removeLast()).data); + System.out.println(list); + } +} diff --git a/group03/763878069/src/cmj/datastructure/list/List.java b/group03/763878069/src/cmj/datastructure/list/List.java new file mode 100644 index 0000000000..8e58ed10f4 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/List.java @@ -0,0 +1,13 @@ +package cmj.datastructure.list; + +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(); +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/list/Queue.java b/group03/763878069/src/cmj/datastructure/list/Queue.java new file mode 100644 index 0000000000..88d8a32e38 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/Queue.java @@ -0,0 +1,128 @@ +package cmj.datastructure.list; + +import java.util.Arrays; + +public class Queue { + + private transient Object[] elementData; + private int size; + + /** 数组的头部,即 下次删除数据的 index */ + private int head; + /** 数组的尾部,即 下次插入数据的 index */ + private int tail; + + /** + * Queue 初始化无参数构造函数 + */ + public Queue() { + this(10); + } + + /** + * Queue带容量的构造函数 + * + * @param initialCapacity初始化容量 + */ + public Queue(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + // 新建一个数组 + this.elementData = new Object[initialCapacity]; + this.head = 0; + this.tail = 0; + this.size = 0; + + } + + /** + * 检查数组的容量 + * + * @param neededMinCapacity所需最小的容量 + */ + public void ensureCapacity(int neededMinCapacity) { + int currCapacity = elementData.length;// 获取当前数据的全部容量 + // 需要扩容的情况 + if (neededMinCapacity > currCapacity) { + int newCapacity = (currCapacity * 3) / 2 + 1;// 计算新的容量 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 添加数据到尾部 + * + * @param o——要添加的数据 + */ + public void enQueue(Object o) { + // 确定ArrayList的容量大小 + ensureCapacity(size + 1); // Increments modCount!! + // 添加o到ArrayList中 + elementData[tail] = o; + size++; + tail++; + } + + /** + * 删除数据 从头部 + * + * @return——被删除的数据 + */ + public Object deQueue() { + if (isEmpty()) { + throw new RuntimeException("队列为空"); + } + Object deleted = (Object) elementData[head]; + elementData[head] = null; + size--; + head++; + return deleted; + } + + public boolean isEmpty() { + return size <= 0 ? true : false; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if (isEmpty()) { + return "[空队列]"; + } + String arraylist = "["; + for (int i = head; i < tail; i++) { + if (i == tail - 1) { + arraylist += elementData[i].toString() + "]"; + } else { + arraylist += elementData[i].toString() + "--->"; + } + } + return arraylist; + } + + public static void main(String[] args) { + Queue queue = new Queue(4); + queue.enQueue("one"); + queue.enQueue("two"); + queue.enQueue("three"); + queue.enQueue("four"); + queue.enQueue("five"); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + + } + +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/list/Stack.java b/group03/763878069/src/cmj/datastructure/list/Stack.java new file mode 100644 index 0000000000..bc16af0203 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/Stack.java @@ -0,0 +1,58 @@ +package cmj.datastructure.list; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + private int size; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object pop = elementData.get(elementData.size()); + elementData.remove(size - 1); + size--; + return pop; + } + + public Object peek() { + if (size == 0) { + throw new RuntimeException("栈为空"); + } + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size <= 0 ? true : false; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return "Stack [elementData=" + elementData + ", size=" + size + "]"; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push("a"); + stack.push("b"); + stack.push("c"); + stack.push("d"); + stack.push("e"); + System.out.println(stack); + + stack.pop(); + System.out.println(stack); + stack.pop(); + System.out.println(stack); + stack.pop(); + System.out.println(stack); + + } + +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/tree/BSTree.java b/group03/763878069/src/cmj/datastructure/tree/BSTree.java new file mode 100644 index 0000000000..25a822a1a1 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/tree/BSTree.java @@ -0,0 +1,191 @@ +package cmj.datastructure.tree; + +/** + * 二叉搜索树 + * + * @author think + * + */ +public class BSTree { + private Node root;// 根结点 + + public BSTree() { + root = null; + } + + public void insert(Node node, int data) { + if (null == root) { + root = new Node(data); + } else { + if (data < node.data) { + if (null == node.left) { + + node.left = new Node(data); + } else { + insert(node.left, data); + } + } else { + if (node.right == null) { + node.right = new Node(data); + } else { + insert(node.right, data); + } + } + } + } + + /** + * 前序遍历 + * + * @param node + */ + public void preOrder(Node node) { + if (node != null) { + System.out.println(node.data); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * + * @param node + */ + public void inOrder(Node node) { + if (node != null) { + inOrder(node.left); + System.out.println(node.data); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * + * @param node + */ + public void postOrder(Node node) { + if (node != null) { + postOrder(node.left); + postOrder(node.right); + System.out.println(node.data); + } + } + + // 删除节点分三种方式删除节点 + // 1、删除没有子节点的节点,直接让该节点的父节点的左节点或右节点指向空 + // 2、删除有一个子节点的节点,直接让该节点的父节点指向被删除节点的剩余节点 + // 3、删除有三个节点的子节点,找到要删除节点的后继节点, 用该节点替代删除的节点 + public boolean delete(int data) { + // 首先查找节点,并记录该节点的父节点引用 + Node current = root; + Node parent = root; + boolean isLeftNode = true; + while (current.data != data) { + parent = current; + if (data < current.data) { + isLeftNode = true; + current = current.left; + } else { + isLeftNode = false; + current = current.right; + } + } + if (current == null) { + System.out.println("没有找到要删除的节点!"); + return false; + } + // 下面分三种情况删除节点 + if (current.left == null && current.right == null) { // 要删除的节点没有子节点 + if (current == root) { // 根节点就删除整棵树 + root = null; + } else if (isLeftNode) { // 如果是左节点,做节点指向空 + parent.left = null; + } else { // 如果是右节点,右节点指向空 + parent.right = null; + } + } else if (current.left == null) { // 要删除的节点只有右节点 + if (current == root) { + root = current.right; + } else if (isLeftNode) { + parent.left = current.right; + } else { + parent.right = current.right; + } + } else if (current.right == null) { // 要删除的节点只有左节点 + if (current == root) { + root = current.left; + } else if (isLeftNode) { + parent.left = current.left; + } else { + parent.right = current.left; + } + } else { // 要删除的节点有两个节点 + Node successor = findSuccessor(current); + if (current == root) { + root = successor; + } else if (isLeftNode) { + parent.left = successor; + } else { + parent.right = successor; + } + successor.left = current.left; + } + return true; + } + + /** + * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 + * + * @param delNode——要删除的结点 + * @return + */ + private Node findSuccessor(Node delNode) { + Node parent = delNode; + Node successor = delNode; + Node current = delNode.right; + /* 找到要删除结点的右子树的最左叶子结点,就是比要删除的数据大的最小结点 */ + while (current != null) { + parent = successor; + successor = current; + current = current.left; + } + + if (successor != delNode.right) { + parent.left = successor.right; + successor.right = delNode.right; + } + return successor; + } + + /** + * 内部结点类 + * + * @author think + * + */ + private class Node { + private Node left; + private Node right; + private int data; + + public Node(int data) { + this.left = null; + this.right = null; + this.data = data; + } + } + + public static void main(String[] args) { + int[] a = { 2, 4, 12, 45, 21, 6, 111, 1, 23, 45 }; + BSTree bTree = new BSTree(); + for (int i = 0; i < a.length; i++) { + bTree.insert(bTree.root, a[i]); + } + bTree.preOrder(bTree.root); + bTree.inOrder(bTree.root); + bTree.postOrder(bTree.root); + } + +} diff --git a/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java b/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java new file mode 100644 index 0000000000..421d342f36 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java @@ -0,0 +1,185 @@ +package cmj.datastructure.tree; + +public class BinaryTree { + private Node root;// 根结点 + + public BinaryTree() { + root = null; + } + + public void insert(Node node, int data) { + if (null == root) { + root = new Node(data); + } else { + if (data < node.data) { + if (null == node.left) { + + node.left = new Node(data); + } else { + insert(node.left, data); + } + } else { + if (node.right == null) { + node.right = new Node(data); + } else { + insert(node.right, data); + } + } + } + } + + /** + * 前序遍历 + * + * @param node + */ + public void preOrder(Node node) { + if (node != null) { + System.out.println(node.data); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * + * @param node + */ + public void inOrder(Node node) { + if (node != null) { + inOrder(node.left); + System.out.println(node.data); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * + * @param node + */ + public void postOrder(Node node) { + if (node != null) { + postOrder(node.left); + postOrder(node.right); + System.out.println(node.data); + } + } + + // 删除节点分三种方式删除节点 + // 1、删除没有子节点的节点,直接让该节点的父节点的左节点或右节点指向空 + // 2、删除有一个子节点的节点,直接让该节点的父节点指向被删除节点的剩余节点 + // 3、删除有三个节点的子节点,找到要删除节点的后继节点, 用该节点替代删除的节点 + public boolean delete(int data) { + // 首先查找节点,并记录该节点的父节点引用 + Node current = root; + Node parent = root; + boolean isLeftNode = true; + while (current.data != data) { + parent = current; + if (data < current.data) { + isLeftNode = true; + current = current.left; + } else { + isLeftNode = false; + current = current.right; + } + } + if (current == null) { + System.out.println("没有找到要删除的节点!"); + return false; + } + // 下面分三种情况删除节点 + if (current.left == null && current.right == null) { // 要删除的节点没有子节点 + if (current == root) { // 根节点就删除整棵树 + root = null; + } else if (isLeftNode) { // 如果是左节点,做节点指向空 + parent.left = null; + } else { // 如果是右节点,右节点指向空 + parent.right = null; + } + } else if (current.left == null) { // 要删除的节点只有右节点 + if (current == root) { + root = current.right; + } else if (isLeftNode) { + parent.left = current.right; + } else { + parent.right = current.right; + } + } else if (current.right == null) { // 要删除的节点只有左节点 + if (current == root) { + root = current.left; + } else if (isLeftNode) { + parent.left = current.left; + } else { + parent.right = current.left; + } + } else { // 要删除的节点有两个节点 + Node successor = findSuccessor(current); + if (current == root) { + root = successor; + } else if (isLeftNode) { + parent.left = successor; + } else { + parent.right = successor; + } + successor.left = current.left; + } + return true; + } + + /** + * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 + * + * @param delNode——要删除的结点 + * @return + */ + private Node findSuccessor(Node delNode) { + Node parent = delNode; + Node successor = delNode; + Node current = delNode.right; + /* 找到要删除结点的右子树的最左叶子结点,就是比要删除的数据大的最小结点 */ + while (current != null) { + parent = successor; + successor = current; + current = current.left; + } + + if (successor != delNode.right) { + parent.left = successor.right; + successor.right = delNode.right; + } + return successor; + } + + /** + * 内部结点类 + * + * @author think + * + */ + private class Node { + private Node left; + private Node right; + private int data; + + public Node(int data) { + this.left = null; + this.right = null; + this.data = data; + } + } + + public static void main(String[] args) { + int[] a = { 2, 4, 12, 45, 21, 6, 111 }; + BinaryTree bTree = new BinaryTree(); + for (int i = 0; i < a.length; i++) { + bTree.insert(bTree.root, a[i]); + } + bTree.preOrder(bTree.root); + bTree.inOrder(bTree.root); + bTree.postOrder(bTree.root); + } + +} From c04c587e050c5513497c414a3d9d44b618a26938 Mon Sep 17 00:00:00 2001 From: waking2017 Date: Fri, 24 Feb 2017 15:59:37 +0800 Subject: [PATCH 037/432] work1 --- group05/441517454/work1/.classpath | 6 + group05/441517454/work1/.gitignore | 1 + group05/441517454/work1/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../work1/src/com/coding/basic/ArrayList.java | 106 ++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++ .../work1/src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 156 ++++++++++++++++++ .../work1/src/com/coding/basic/List.java | 9 + .../work1/src/com/coding/basic/Queue.java | 30 ++++ .../work1/src/com/coding/basic/Stack.java | 30 ++++ .../work1/src/com/coding/basic/Test1.java | 48 ++++++ 12 files changed, 453 insertions(+) create mode 100644 group05/441517454/work1/.classpath create mode 100644 group05/441517454/work1/.gitignore create mode 100644 group05/441517454/work1/.project create mode 100644 group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/441517454/work1/src/com/coding/basic/ArrayList.java create mode 100644 group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Iterator.java create mode 100644 group05/441517454/work1/src/com/coding/basic/LinkedList.java create mode 100644 group05/441517454/work1/src/com/coding/basic/List.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Queue.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Stack.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Test1.java diff --git a/group05/441517454/work1/.classpath b/group05/441517454/work1/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/441517454/work1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/441517454/work1/.gitignore b/group05/441517454/work1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/441517454/work1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/441517454/work1/.project b/group05/441517454/work1/.project new file mode 100644 index 0000000000..49a7fabffd --- /dev/null +++ b/group05/441517454/work1/.project @@ -0,0 +1,17 @@ + + + work1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs b/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/441517454/work1/.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/group05/441517454/work1/src/com/coding/basic/ArrayList.java b/group05/441517454/work1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..01671f55d8 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + if(sizeindex;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index] = o; + this.size++; + } + else if(index<(this.size)&&this.size==elementData.length) + { + elementData = grow(elementData,1); + elementData[index] = o; + this.size++; + } + else{ + System.out.println("index/>size����ʧ��"); + + } + + + } + + public Object get(int index){ + + if(this.size>0&&index<(this.size)) + return elementData[index]; + else{ + return null; + } + } + + public Object remove(int index){ + + if(this.size>0&&index<(this.size)) + { Object o= elementData[index]; + for(int i=index;iarrayList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return arrayList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java b/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/Iterator.java b/group05/441517454/work1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/LinkedList.java b/group05/441517454/work1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5eefddce8b --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,156 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + private Node body; + //private Node body1; + private int size = 0; + + + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + size++; + } +// else if(head.next == null){ +// +// head.next =new Node(); +// head.next.data = o; +// head.next.next = null; +// body=head.next; +// size++; +// } + else { + body=head; + while(!(body.next ==null)) + {body =body.next;} + body.next =new Node(); + body.next.data =o; + body.next.next =null; + size++; + } + + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + if (index1) + {body=head; + for (int i=0;ilinkedList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return linkedList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/List.java b/group05/441517454/work1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/Queue.java b/group05/441517454/work1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..cae520dbce --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + if(elementData.size()>0) + + { + elementData.remove(0); + return elementData.remove(0); + } + else return null; + } + + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Stack.java b/group05/441517454/work1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..40e8518b24 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Stack.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()>0) + + { int size =elementData.size(); + elementData.remove(size-1); + return elementData.remove(size-1);} + else return null; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Test1.java b/group05/441517454/work1/src/com/coding/basic/Test1.java new file mode 100644 index 0000000000..b7f644c95d --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Test1.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class Test1 { + public static void main (String[] args){ +// ArrayList arr = new ArrayList(); +// arr.add(1); +// arr.add(2); +// arr.add(3); +// arr.add(4); +// arr.remove(3); +// arr.add(6); +// arr.add(7); +// System.out.println(arr.size()+"_"); +// Iterator it =arr.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } +// LinkedList link = new LinkedList(); +// link.add(0); +// link.add(1); +// link.add(2); +// link.add(3); +// link.add(4); +// link.add(5); +// link.remove(2); +// link.addFirst(100); +// link.addLast(200); +// //link.removeFirst(); +// //link.removeLast(); +//// System.out.println(link.size()+"_"); +//// System.out.println(link.get(0)); +//// System.out.println(link.get(link.size()-1)); +//// +// +// Iterator it =link.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } + Stack st = new Stack(); + st.push(0); + st.push(1); + st.push(2); + st.push(4); + st.push(5); + st.pop(); + System.out.println(st.peek()); + } +} \ No newline at end of file From f0c5ba0fb734e6f984100b78ac852fe22af9457b Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Fri, 24 Feb 2017 16:03:29 +0800 Subject: [PATCH 038/432] data structrue without tree --- group11/1178243325/DataStructure/build.gradle | 10 ++ group11/1178243325/DataStructure/readme.md | 1 + .../src/main/java/com/coding/Main.java | 58 ++++++++ .../main/java/com/coding/basic/ArrayList.java | 97 +++++++++++++ .../java/com/coding/basic/BinaryTreeNode.java | 32 +++++ .../main/java/com/coding/basic/Iterator.java | 6 + .../java/com/coding/basic/LinkedList.java | 135 ++++++++++++++++++ .../src/main/java/com/coding/basic/List.java | 9 ++ .../src/main/java/com/coding/basic/Queue.java | 30 ++++ .../src/main/java/com/coding/basic/Stack.java | 38 +++++ .../ConcurrentModificationException.java | 9 ++ .../basic/exception/EmptyQueueException.java | 8 ++ .../basic/exception/EmptyStackException.java | 8 ++ 13 files changed, 441 insertions(+) create mode 100644 group11/1178243325/DataStructure/build.gradle create mode 100644 group11/1178243325/DataStructure/readme.md create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/Main.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java create mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java diff --git a/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle new file mode 100644 index 0000000000..5c3694d8ec --- /dev/null +++ b/group11/1178243325/DataStructure/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +jar { + manifest { + attributes 'Main-Class' : 'com.coding.Main' + } +} + + diff --git a/group11/1178243325/DataStructure/readme.md b/group11/1178243325/DataStructure/readme.md new file mode 100644 index 0000000000..29ad9d5c06 --- /dev/null +++ b/group11/1178243325/DataStructure/readme.md @@ -0,0 +1 @@ +实现基本的数据结构ArrayList,LinkList,Stack,Queue,Tree,Iterator diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java new file mode 100644 index 0000000000..31d2f2f277 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java @@ -0,0 +1,58 @@ +package com.coding; + +import com.coding.basic.*; +public class Main { + public static void main(String[] args) { + ArrayList list = new ArrayList(); + + list.add(0, "2xxx"); + list.add(1, "we"); + list.add(2, "sss"); + list.add("xing"); + list.remove(2); + System.out.println(list.get(2)); + Iterator iterator = list.iterator(); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + System.out.println(list.size()); + + LinkedList llist = new LinkedList(); + llist.add("hu"); + llist.add("zhao"); + llist.add(2,"xing"); + llist.addFirst("身骑白马"); + llist.addLast("德州小老虎"); + llist.add(5, "sf"); + llist.remove(5); + llist.removeFirst(); + llist.removeLast(); + for (int i = 2; i >=0; i--) + System.out.print(llist.get(i)); + System.out.println(llist.size()); + + Iterator literator = llist.iterator(); + while(literator.hasNext()) { + System.out.println(literator.next()); + } + + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.peek()); + while(!stack.isEmpty()) + System.out.println(stack.pop()); + + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + System.out.println(queue.size()); + while (!queue.isEmpty()) { + System.out.println(queue.deQueue()); + } + + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f6cd4c38fc --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class ArrayList implements List { + + private int size; + private Object[] elementData; + + public ArrayList () { + size = 0; + elementData = new Object[100]; + } + + public void add(Object o){ + add(size(), o); + } + + public void add(int index, Object o){ + if (size() == elementData.length) + ensureCapacity( size() * 2 + 1); + if (index > size() || index < 0) { //index == size时相当于在尾后插入 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + + } + + private void ensureCapacity(int newCapacity) { + if (newCapacity < size()) + return; + Object[] old = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + elementData[i] = old[i]; + } + } + + public Object get(int index){ + if (index >= size() || index < 0) { //获取时,index==size()越界 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + return elementData[index]; + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ", Size:" + size; + } + + public Object remove(int index){ + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Object old = elementData[index]; + for (int i = index; i < size(); i++) { + elementData[i] = elementData[i+1]; + } + size--; + return old; + } + + /*获取表内容量*/ + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public class ArrayListIterator implements Iterator { + private final int ONLY_CAPACITY = size; + private int index; + public ArrayListIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + return index != size; + } + + @Override + public Object next() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + if (index >= ONLY_CAPACITY) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return elementData[index++]; + } + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d82349089b --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,135 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class LinkedList implements List { + + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; + + } + + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; + } + + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.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(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; + } + @Override + public boolean hasNext() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + return index < capacity; + } + + @Override + public Object next() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + if (index >= capacity) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return get(index++); + } + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + void setData(Object data) { + this.data = data; + } + + Object getData() { + return data; + } + + void setNext(Node next) { + this.next = next; + } + + Object getNext() { + return next; + } + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a5c31f5a09 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; +import com.coding.basic.exception.*; +public class Queue { + + private LinkedList elementData; + + public Queue() { + elementData = new LinkedList(); + } + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + throw new EmptyQueueException("队空"); + } + Object result = elementData.removeFirst(); + return result; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e41c662792 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class Stack { + + private ArrayList elementData; + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + Object result = elementData.get(size()-1); + elementData.remove(size()-1); + return result; + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + return elementData.get(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java new file mode 100644 index 0000000000..f1c5c79721 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java @@ -0,0 +1,9 @@ +package com.coding.basic.exception; + +public class ConcurrentModificationException extends RuntimeException { + public ConcurrentModificationException() {} + public ConcurrentModificationException(String msg) { + super(msg); + } +} + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java new file mode 100644 index 0000000000..2ee7aa4ee7 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java @@ -0,0 +1,8 @@ +package com.coding.basic.exception; + +public class EmptyQueueException extends RuntimeException { + public EmptyQueueException() {} + public EmptyQueueException(String msg) { + super(msg); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java new file mode 100644 index 0000000000..2a5ae4055d --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java @@ -0,0 +1,8 @@ +package com.coding.basic.exception; + +public class EmptyStackException extends RuntimeException { + public EmptyStackException() {} + public EmptyStackException(String msg) { + super(msg); + } +} From 2cd5cee628539d351b58b8eaa0cba9d21915ba15 Mon Sep 17 00:00:00 2001 From: sulei <1020483199@qq.com> Date: Fri, 24 Feb 2017 16:50:21 +0800 Subject: [PATCH 039/432] homework homework --- .../1020483199/1020483199Learning/.classpath | 7 + .../1020483199/1020483199Learning/.gitignore | 1 + .../1020483199/1020483199Learning/.project | 17 ++ .../org.eclipse.core.resources.prefs | 3 + .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/coding/basic/ArrayList.java | 128 ++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 82 +++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 175 ++++++++++++++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 40 ++++ .../src/com/coding/basic/Stack.java | 38 +++ .../src/com/coding/test/JavaTest.java | 223 ++++++++++++++++++ .../RemoteSystemsTempFiles/.project | 12 + 14 files changed, 753 insertions(+) create mode 100644 group04/1020483199/1020483199Learning/.classpath create mode 100644 group04/1020483199/1020483199Learning/.gitignore create mode 100644 group04/1020483199/1020483199Learning/.project create mode 100644 group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs create mode 100644 group04/1020483199/1020483199Learning/.settings/org.eclipse.jdt.core.prefs create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/Iterator.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/List.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java create mode 100644 group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java create mode 100644 group04/1020483199/RemoteSystemsTempFiles/.project diff --git a/group04/1020483199/1020483199Learning/.classpath b/group04/1020483199/1020483199Learning/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/1020483199/1020483199Learning/.gitignore b/group04/1020483199/1020483199Learning/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/1020483199/1020483199Learning/.project b/group04/1020483199/1020483199Learning/.project new file mode 100644 index 0000000000..1ee8060dd3 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.project @@ -0,0 +1,17 @@ + + + 1020483199Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs b/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..f7e195b54e --- /dev/null +++ b/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/com/coding/basic=UTF-8 +encoding/=UTF-8 diff --git a/group04/1020483199/1020483199Learning/.settings/org.eclipse.jdt.core.prefs b/group04/1020483199/1020483199Learning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.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/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a61325db63 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,128 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + + private int size = 0; + + private transient Object[] elementData = new Object[100]; + /** + * 向数组中添加某个元素 + */ + public void add(Object o){ + /** + * 数组扩容判断 + */ + ensureSize(size+1); + elementData[size++] = o; + } + /** + * 向指定位置数组中添加某个元素 + */ + public void add(int index, Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("数组越界"); + } + ensureSize(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + } + /** + * 获取数组中某个位置元素 + */ + public Object get(int index){ + if(index<0||index>elementData.length){ + return null; + }else{ + return elementData[index]; + } + + } + /** + * 移除数组中指定位置元素 + */ + public Object remove(int index){ + if(index<0||index>elementData.length){ + return null; + }else{ + int newLength = size-index-1; + if (newLength>0) + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size] = null; + return elementData; + } + } + /** + * 获取当前数组的大小 + */ + public int size(){ + if(size>0){ + return this.size; + }else{ + return 0; + } + } + /** + * 利用arraylist实现迭代器 + * @return + */ + public Iterator iterator(){ + + return new ArrayListIterator(); + } + private class ArrayListIterator implements Iterator{ + int cursor; + int lastReset = -1; + @Override + public boolean hasNext() { + return size!=cursor; + } + + @Override + public Object next() { + //标记索引当前位置 + int i = cursor; + if(i>size){ + throw new NoSuchElementException(); + } + Object[] newData = elementData; + if(i>newData.length){ + throw new ConcurrentModificationException(); + } + cursor = i + 1; + return newData[lastReset = i]; + } + + } + + + /** + * @author sulei + * @param minCapcity + */ + public void ensureSize(int minCapcity){ + if(minCapcity>elementData.length){ + grow(minCapcity); + } + } + + /** + * @author sulei + * @param autoCapcity + */ + public void grow(int autoCapcity){ + int oldLength = elementData.length; + if(autoCapcity>oldLength){ + Object[] oldData = elementData; + int newLength = (oldLength * 3)/2 + 1; + if(autoCapcity>newLength){ + newLength=autoCapcity; + } + elementData = Arrays.copyOf(elementData, newLength); + } + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..48026be26c --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + //存放当前树 + private BinaryTreeNode primary; + + public BinaryTreeNode getPrimary() { + return primary; + } + + public void setPrimary(BinaryTreeNode primary) { + this.primary = primary; + } + + + 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; + } + /** + * 在二叉树中插入 + * @param o + * @return + */ + public BinaryTreeNode insert(Object o){ + if(o==null){ + throw new IllegalArgumentException("二叉树的元素不能为空!"); + } + //新建要插入的节点 + BinaryTreeNode bt = new BinaryTreeNode(); + bt.setData(o); + int value = (int)o; + //当原始二叉树为空时 + if(primary==null){ + primary = bt; + }else{ + BinaryTreeNode bi = primary; + while(true){ + if(value<(int)bi.data){ + if(bi.left==null){ + bi.setLeft(bt); + break; + }else{ + bi=bi.left; + } + }else if(value>(int)bi.data){ + if(bi.right==null){ + bi.setRight(bt); + break; + }else{ + bi=bi.right; + } + + }else{ + System.out.println("当前元素在二叉树已存在"); + break; + } + } + } + + return bt; + } + +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/Iterator.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/1020483199/1020483199Learning/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/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a85c311f72 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,175 @@ +package com.coding.basic; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + /** + * 当前节点的大小 + */ + private int size; + /** + * 头节点 + */ + private Node header; + /** + * 尾节点 + */ + private Node end; + + + + public void add(Object o){ + if(header==null){ + header = new Node(); + header.data = o; + end = header; + }else{ + Node n = new Node(); + n.data = o; + end.next = n; + n.previous=end; + end = n; + } + size++; + } + public void add(int index,Object o){ + judge(index); + Node n = new Node(); + n.data = o; + Node temp = header; + for(int i = 0;isize){ + throw new NoSuchElementException(); + } + Node n = header; + for(int j = 0;j < i;j ++){ + n = n.next; + } + cursor = i + 1; + lastReset = i; + return n.data; + } + + } + + + /** + * 判断数组是否越界方法 + */ + public void judge(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("数组越界"); + } + } + /** + * @author sulei + */ + private static class Node{ + Object data;//当前节点数据 + Node next;//下一个节点 + Node previous;//上一个节点 + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/List.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/1020483199/1020483199Learning/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/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b9a26bb40e --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class Queue { + /** + * 队列长度 + */ + private int size; + + private LinkedList elementData = new LinkedList(); + /** + * 入队操作[向栈尾插入] + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + size++; + } + + public Object deQueue(){ + Object obj = elementData.removeFirst(); + size--; + return obj; + } + + public boolean isEmpty(){ + if(size==0){ + return true; + }else{ + return false; + } + + } + + public int size(){ + return size; + } + +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..535922bb35 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class Stack { + /** + * 当前栈的大小 + */ + private int size; + + private ArrayList elementData = new ArrayList(); + /** + * 压栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + size++; + } + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.remove(--size); + } + /** + * 返回栈顶元素 + * @return + */ + public Object peek(){ + return elementData.get(size-1); + } + public boolean isEmpty(){ + return size==0; + } + public int size(){ + return size; + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java b/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java new file mode 100644 index 0000000000..045e3d7a48 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java @@ -0,0 +1,223 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import java.math.BigDecimal; + +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.List; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + + + +public class JavaTest { + /** + * List集合单元测试内容 + */ + @Test + public void test() { + List list = new ArrayList(); + list.add(0, "aa"); + } + + @Test + public void test1() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.get(0)); + } + + @Test + public void test2() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.get(0)); + list.remove(0); + System.out.println(list.get(0)); + } + + @Test + public void test3() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.size()); + list.add("aa"); + System.out.println(list.size()); + } + + @Test + public void test4() { + ArrayList list = new ArrayList(); + Integer [] str={11,22}; + list.add(str); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + + } + LinkedList ll = new LinkedList(); + } + + /** + * LinkedList测试 + */ + @Test + public void test5() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + System.out.println(list.get(1)); + } + + @Test + public void test6() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.add(2, "ff"); + System.out.println(list.get(3)); + } + + @Test + public void test7() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.remove(2); + System.out.println(list.get(0)+"----"+list.get(1)); + } + + @Test + public void test8() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + System.out.println(list.get(0)); + list.addFirst("haha"); + System.out.println(list.get(0)); + } + + @Test + public void test9() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.addLast("haha"); + System.out.println(list.get(3)); + } + + @Test + public void test10() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.removeFirst(); + System.out.println(list.get(0)); + } + + @Test + public void test11() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.removeLast(); + System.out.println(list.get(2)); + } + + @Test + public void test12() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + Iterator it = list.iterator(); + while(it!=null&&it.hasNext()){ + System.out.println(it.next()); + } + } + + /** + * Queue集合测试 + */ + @Test + public void test13() { + Queue q = new Queue(); + System.out.println(q.size()+"------"+q.isEmpty()); + q.enQueue("aaa"); + q.enQueue("bbb"); + q.enQueue("ccc"); + System.out.println(q.size()+"------"+q.isEmpty()); + q.deQueue(); + System.out.println(q.size()+"------"+q.isEmpty()); + } + + /** + * Stack集合测试 + */ + @Test + public void test14() { + Stack q = new Stack(); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + q.push("aa"); + q.push("bb"); + q.push("cc"); + q.push("dd"); + System.out.println(q.size()); + q.pop(); + System.out.println(q.size()); + System.out.println(q.peek()); + q.pop(); + System.out.println(q.size()); + System.out.println(q.peek()); + System.out.println(q.isEmpty()); + } + + /** + * BinaryTree测试 + */ + @Test + public void test15(){ + BinaryTreeNode bt = new BinaryTreeNode(); + Integer[] data={3,2,5,4,6,8}; + for(Integer i=0;i=1;i--){ + if(y%i==0&&x%i==0){ + maxValue = i; + break; + } + } + System.out.println(x/maxValue+"比"+y/maxValue); + + } + + + +} diff --git a/group04/1020483199/RemoteSystemsTempFiles/.project b/group04/1020483199/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group04/1020483199/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + From 88b4bc07f4e4b2c4a439b8f9be4a59cea5df254a Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Fri, 24 Feb 2017 00:57:53 -0800 Subject: [PATCH 040/432] complete coding except iterator --- group11/395443277/a1/.gitignore | 111 ++++++++++++++ .../a1/src/com/coding/basic/ArrayList.java | 64 ++++++++ .../src/com/coding/basic/ArrayListTest.java | 67 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 75 ++++++++++ .../com/coding/basic/BinaryTreeNodeTest.java | 40 +++++ .../a1/src/com/coding/basic/Iterator.java | 7 + .../a1/src/com/coding/basic/LinkedList.java | 138 ++++++++++++++++++ .../src/com/coding/basic/LinkedListTest.java | 114 +++++++++++++++ .../a1/src/com/coding/basic/List.java | 9 ++ .../a1/src/com/coding/basic/Queue.java | 21 +++ .../a1/src/com/coding/basic/QueueTest.java | 29 ++++ .../a1/src/com/coding/basic/Stack.java | 23 +++ .../a1/src/com/coding/basic/StackTest.java | 45 ++++++ 13 files changed, 743 insertions(+) create mode 100644 group11/395443277/a1/.gitignore create mode 100644 group11/395443277/a1/src/com/coding/basic/ArrayList.java create mode 100644 group11/395443277/a1/src/com/coding/basic/ArrayListTest.java create mode 100644 group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java create mode 100644 group11/395443277/a1/src/com/coding/basic/Iterator.java create mode 100644 group11/395443277/a1/src/com/coding/basic/LinkedList.java create mode 100644 group11/395443277/a1/src/com/coding/basic/LinkedListTest.java create mode 100644 group11/395443277/a1/src/com/coding/basic/List.java create mode 100644 group11/395443277/a1/src/com/coding/basic/Queue.java create mode 100644 group11/395443277/a1/src/com/coding/basic/QueueTest.java create mode 100644 group11/395443277/a1/src/com/coding/basic/Stack.java create mode 100644 group11/395443277/a1/src/com/coding/basic/StackTest.java diff --git a/group11/395443277/a1/.gitignore b/group11/395443277/a1/.gitignore new file mode 100644 index 0000000000..e02fdb6730 --- /dev/null +++ b/group11/395443277/a1/.gitignore @@ -0,0 +1,111 @@ + + +.metadata + +bin/ + +tmp/ + +*.tmp + +*.bak + +*.swp + +*~.nib + +local.properties + +.settings/ + +.loadpath + +.recommenders + + + +# Eclipse Core + +.project + + + +# External tool builders + +.externalToolBuilders/ + + + +# Locally stored "Eclipse launch configurations" + +*.launch + + + +# PyDev specific (Python IDE for Eclipse) + +*.pydevproject + + + +# CDT-specific (C/C++ Development Tooling) + +.cproject + + + +# JDT-specific (Eclipse Java Development Tools) + +.classpath + + + +# Java annotation processor (APT) + +.factorypath + + + +# PDT-specific (PHP Development Tools) + +.buildpath + + + +# sbteclipse plugin + +.target + + + +# Tern plugin + +.tern-project + + + +# TeXlipse plugin + +.texlipse + + + +# STS (Spring Tool Suite) + +.springBeans + + + +# Code Recommenders + +.recommenders/ + + + +# Scala IDE specific (Scala & Java development for Eclipse) + +.cache-main + +.scala_dependencies + +.worksheet \ No newline at end of file diff --git a/group11/395443277/a1/src/com/coding/basic/ArrayList.java b/group11/395443277/a1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4c6c92c42e --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o){ + if (size == elementData.length) { + // double size + doubleSize(); + } + + elementData[size] = o; + size++; + } + + private void doubleSize() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + public void add(int index, Object o){ + // check size + if (size == elementData.length) { + doubleSize(); + } + + //shift and add element + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (size == 0) { + return null; + } + + // remove element and shift + Object target = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size - index - 1); + + // reset last element + elementData[size-1] = null; + size--; + return target; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/ArrayListTest.java b/group11/395443277/a1/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..3025b8e077 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testAddObject() { + ArrayList list = new ArrayList(); + list.add(5); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + list.add(1); + assertEquals(1, list.get(4)); + + // size equals to 5 + assertEquals(5, list.size()); + } + + @Test + public void testAddIntObject() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + // change position 2 element + list.add(2, 10); + + // pos 2 has 10 + assertEquals(10, list.get(2)); + + // last element is 1 + assertEquals(1, list.get(5)); + + // size is 6 + assertEquals(6, list.size()); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + Object removed = list.remove(2); + assertEquals(removed, 3); + + assertEquals(2, list.get(2)); + assertEquals(4, list.size()); + assertEquals(null, list.get(4)); + + list.add(6); + assertEquals(6, list.get(4)); + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..907fc23275 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable{ + + 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){ + if (this.compareTo(o)==0) { + return null; + } else { + // current value less than inserted value + // go right + if (this.compareTo(o)<0) { + if (this.right == null) { + BinaryTreeNode nd = new BinaryTreeNode(); + nd.setData(o); + this.setRight(nd); + } else { + this.getRight().insert(o); + } + } + // greater than + // go left + else if(this.compareTo(o)>0) { + if (this.left == null) { + BinaryTreeNode nd = new BinaryTreeNode(); + nd.setData(o); + this.setLeft(nd); + } else { + this.getLeft().insert(o); + } + } + } + + return null; + } + + /** + * oversimplified implementation: only allows int and string + */ + @Override + public int compareTo(Object nd) throws ClassCastException{ + if (!(nd instanceof Object)) { + throw new ClassCastException("An object expected."); + } + + if (nd instanceof String) { + return ((String)this.data).compareTo((String) nd); + } else { + return ((int) this.data) -((int) nd); + } + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java b/group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..1c5c637ccd --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Test + public void testInsert() { + BinaryTreeNode root = new BinaryTreeNode(); + root.setData(3); + + root.insert(2); + BinaryTreeNode left = root.getLeft(); + assertEquals(2,left.getData()); + + root.insert(5); + BinaryTreeNode right = root.getRight(); + assertEquals(5, right.getData()); + + root.insert(7); + BinaryTreeNode rr = right.getRight(); + assertEquals(7, rr.getData()); + } + + @Test + public void testCompareTo() { + BinaryTreeNode n1 = new BinaryTreeNode(); + n1.setData("abc"); + + assertEquals(true, n1.compareTo("cde")<0); + + BinaryTreeNode n3 = new BinaryTreeNode(); + n3.setData(1); + + assertEquals(true, n3.compareTo(2)<0); + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/Iterator.java b/group11/395443277/a1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group11/395443277/a1/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/group11/395443277/a1/src/com/coding/basic/LinkedList.java b/group11/395443277/a1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e05f035eb2 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,138 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + Node curr = head; + while(curr.next != null) { + curr = curr.next; + } + curr.next = newNode; + } + } + public void add(int index , Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + Node curr = head; + Node prev = curr; + while(index >0 && curr.next != null) { + prev = curr; + curr = curr.next; + index--; + } + + prev.next = newNode; + newNode.next = curr; + } + } + public Object get(int index){ + Node curr = head; + while(index > 0) { + curr = curr.next; + index--; + } + return curr.data; + } + public Object remove(int index){ + if (index ==0) { + return this.removeFirst(); + } + + Node curr = head; + Node prev = curr; + while(index >0 && curr.next != null) { + prev = curr; + curr = curr.next; + index--; + } + + Object target = curr.data; + prev.next = curr.next; + curr.next = null; + + return target; + } + public int size(){ + int size = 0; + Node curr = head; + while(curr != null) { + size++; + curr = curr.next; + } + return size; + } + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + newNode.next = head.next; + head.next = newNode; + } + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + Node curr = head; + if (head == null) { + head = newNode; + } else { + while(curr.next != null) { + curr = curr.next; + } + curr.next = newNode; + } + } + public Object removeFirst(){ + if (head == null) { + return null; + } + + Object target = head.data; + head = head.next; + return target; + } + public Object removeLast(){ + if (head == null) { + return null; + } + Node curr = head; + Node prev = curr; + while(curr.next != null) { + prev = curr; + curr = curr.next; + } + Object target = curr.data; + prev.next = null; + return target; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group11/395443277/a1/src/com/coding/basic/LinkedListTest.java b/group11/395443277/a1/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..7faae38be6 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,114 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void testAddObject() { + LinkedList list = new LinkedList(); + list.add(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(4, list.size()); + assertEquals(4, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(2, list.get(3)); + } + + @Test + public void testAddIntObject() { + LinkedList list = new LinkedList(); + list.add(0, 5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + + list.add(1, 1); + assertEquals(1, list.get(1)); + } + + @Test + public void testRemove() { + LinkedList list = new LinkedList(); + assertEquals(null, list.remove(0)); + list.add(4); + assertEquals(4, list.remove(0)); + + list.add(5); + list.add(-1); + list.add(16); + list.add(2); + list.add(7); + assertEquals(16, list.remove(2)); + assertEquals(4, list.size()); + assertEquals(2, list.get(2)); + } + + @Test + public void testAddFirst() { + LinkedList list = new LinkedList(); + list.addFirst(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.addFirst(4); + list.addFirst(3); + list.addFirst(2); + assertEquals(4, list.size()); + assertEquals(2, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(4, list.get(3)); + } + + @Test + public void testAddLast() { + LinkedList list = new LinkedList(); + list.addLast(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.addLast(4); + list.addLast(3); + list.addLast(2); + assertEquals(4, list.size()); + assertEquals(4, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(2, list.get(3)); + } + + @Test + public void testRemoveFirst() { + LinkedList list = new LinkedList(); + assertEquals(null, list.removeFirst()); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(4, list.removeFirst()); + assertEquals(3, list.removeFirst()); + assertEquals(2, list.removeFirst()); + } + + @Test + public void testRemoveLast() { + LinkedList list = new LinkedList(); + assertEquals(null, list.removeLast()); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(2, list.removeLast()); + assertEquals(3, list.removeLast()); + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/List.java b/group11/395443277/a1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group11/395443277/a1/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/group11/395443277/a1/src/com/coding/basic/Queue.java b/group11/395443277/a1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f12e73d46d --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +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; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/395443277/a1/src/com/coding/basic/QueueTest.java b/group11/395443277/a1/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..bd1ec0487a --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/QueueTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class QueueTest { + + @Test + public void testEnQueue() { + Queue q = new Queue(); + assertEquals(0, q.size()); + + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + } + + @Test + public void testDeQueue() { + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + assertEquals(1, q.deQueue()); + assertEquals(2, q.deQueue()); + } + +} diff --git a/group11/395443277/a1/src/com/coding/basic/Stack.java b/group11/395443277/a1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..28b9e8a203 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/Stack.java @@ -0,0 +1,23 @@ +package com.coding.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(); + } +} diff --git a/group11/395443277/a1/src/com/coding/basic/StackTest.java b/group11/395443277/a1/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..7b3c4d2cf6 --- /dev/null +++ b/group11/395443277/a1/src/com/coding/basic/StackTest.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class StackTest { + + @Test + public void testPush() { + Stack st = new Stack(); + st.push(1); + assertEquals(1, st.peek()); + + st.push(2); + st.push(3); + st.push(4); + assertEquals(4, st.peek()); + } + + @Test + public void testPop() { + Stack st = new Stack(); + assertEquals(null, st.pop()); + + st.push(1); + assertEquals(1, st.pop()); + + st.push(2); + st.push(3); + st.push(4); + assertEquals(4, st.pop()); + } + + @Test + public void testIsEmpty() { + Stack st = new Stack(); + assertEquals(true, st.isEmpty()); + + st.push(1); + assertEquals(false, st.isEmpty()); + } + + +} From dbd3902161a57a1a410bb6506766ce5539b4ce40 Mon Sep 17 00:00:00 2001 From: earliest Date: Fri, 24 Feb 2017 01:02:15 -0800 Subject: [PATCH 041/432] array list implementation and ut --- .../src/com/coding/basic/ArrayList.java | 86 +++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 46 ++++++++ .../1282579502/src/com/coding/basic/List.java | 9 ++ .../src/com/coding/basic/Queue.java | 19 ++++ .../src/com/coding/basic/Stack.java | 22 ++++ .../com/coding/basic/ut/ArrayListTest.java | 100 ++++++++++++++++++ .../src/com/wusa/assignment1/TestDriver.java | 13 +++ 9 files changed, 334 insertions(+) create mode 100644 group17/1282579502/src/com/coding/basic/ArrayList.java create mode 100644 group17/1282579502/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group17/1282579502/src/com/coding/basic/Iterator.java create mode 100644 group17/1282579502/src/com/coding/basic/LinkedList.java create mode 100644 group17/1282579502/src/com/coding/basic/List.java create mode 100644 group17/1282579502/src/com/coding/basic/Queue.java create mode 100644 group17/1282579502/src/com/coding/basic/Stack.java create mode 100644 group17/1282579502/src/com/coding/basic/ut/ArrayListTest.java diff --git a/group17/1282579502/src/com/coding/basic/ArrayList.java b/group17/1282579502/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..db8e918503 --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/ArrayList.java @@ -0,0 +1,86 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[8]; + + public int test = 10; + + public void add(Object o){ + if(size + 1 >elementData.length){ + expand(); + } + elementData[size] = o; + size++; + } + /** + * Parameters: + * index index at which the specified element is to be inserted + * element element to be inserted + * Throws: + * IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) + */ + + public void add(int index, Object o){ + if(index <0 || index > size()) throw new IndexOutOfBoundsException(index + ": Invalid Index"); + if(size()+1>elementData.length){ + expand(); + } + if(index < size()) + 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 ArrayIndexOutOfBoundsException(index + ": Invalid Index."); + return elementData[index]; + } + + public Object remove(int index) { + if(index <0 || index >= size()) throw new ArrayIndexOutOfBoundsException(index + ": Invalid Index."); + Object item = elementData[index]; + if(index Date: Fri, 24 Feb 2017 17:05:46 +0800 Subject: [PATCH 042/432] wrote a readme file --- group04/844028312/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 group04/844028312/.gitignore diff --git a/group04/844028312/.gitignore b/group04/844028312/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/844028312/.gitignore @@ -0,0 +1 @@ +/bin/ From afde9da6a16036a963db4e81e57633d7606795ac Mon Sep 17 00:00:00 2001 From: C-BoBo <183127807@qq.com> Date: Fri, 24 Feb 2017 17:10:17 +0800 Subject: [PATCH 043/432] HomeWork0226 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ArrayList,LinkList,Stack,Queue的实现 --- group05/183127807/HomeWork0226/.idea/misc.xml | 22 + .../183127807/HomeWork0226/.idea/modules.xml | 8 + group05/183127807/HomeWork0226/.idea/vcs.xml | 6 + .../HomeWork0226/.idea/workspace.xml | 1056 +++++++++++++++++ .../183127807/HomeWork0226/HomeWork0226.iml | 11 + .../src/com/coding/basic/ArrayList.java | 85 ++ .../src/com/coding/basic/BinaryTreeNode.java | 32 + .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 142 +++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 22 + .../src/com/coding/basic/Stack.java | 35 + 12 files changed, 1435 insertions(+) create mode 100644 group05/183127807/HomeWork0226/.idea/misc.xml create mode 100644 group05/183127807/HomeWork0226/.idea/modules.xml create mode 100644 group05/183127807/HomeWork0226/.idea/vcs.xml create mode 100644 group05/183127807/HomeWork0226/.idea/workspace.xml create mode 100644 group05/183127807/HomeWork0226/HomeWork0226.iml create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/List.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Queue.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Stack.java diff --git a/group05/183127807/HomeWork0226/.idea/misc.xml b/group05/183127807/HomeWork0226/.idea/misc.xml new file mode 100644 index 0000000000..2e47c90c3c --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/modules.xml b/group05/183127807/HomeWork0226/.idea/modules.xml new file mode 100644 index 0000000000..e8f27e7b12 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/vcs.xml b/group05/183127807/HomeWork0226/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/workspace.xml b/group05/183127807/HomeWork0226/.idea/workspace.xml new file mode 100644 index 0000000000..bea69f9904 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/workspace.xml @@ -0,0 +1,1056 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487584408499 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/HomeWork0226.iml b/group05/183127807/HomeWork0226/HomeWork0226.iml new file mode 100644 index 0000000000..c90834f2d6 --- /dev/null +++ b/group05/183127807/HomeWork0226/HomeWork0226.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..d8609f5ca3 --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,85 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 100; + + private int size = 0; + + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(size+1); + for (int i = size;i>index;i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + + public Object remove(int index){ + Object[] removeData = (Object[]) elementData[index]; + for (int i =index;i elementData.length) { + + elementData = Arrays.copyOf(elementData, minCapacity + DEFAULT_CAPACITY); + } + } + +} diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java b/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/183127807/HomeWork0226/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/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java b/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/183127807/HomeWork0226/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/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1bbcad9db --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,142 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size ; + + private Node current = head; + public LinkedList() { + head = null; + size = 0; + } + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + if (current.next == null) + { + current.next = newNode; + } + + while (current.next != null){ + current = current.next; + } + current.next = newNode; + size++; + + } + public void add(int index , Object o){ + + Node newNode = new Node(); + newNode.data = o; + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + for (int i = 0; i < index - 2; i++) { + + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + size++; + } + public Object get(int index){ + + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else if (index == 0) { + return head; + } + for (int i = 0;i Date: Fri, 24 Feb 2017 17:39:50 +0800 Subject: [PATCH 044/432] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group04/844028312/one/.classpath | 1 + group04/844028312/one/.gitignore | 1 - .../one/src/com/coding/basic/ArrayList.java | 97 ++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 145 ++++++++++++++ .../one/src/com/coding/basic/Iterator.java | 7 + .../one/src/com/coding/basic/JavaTest.java | 89 +++++++++ .../one/src/com/coding/basic/LinkedList.java | 178 ++++++++++++++++++ .../one/src/com/coding/basic/List.java | 9 + .../one/src/com/coding/basic/Queue.java | 24 +++ .../one/src/com/coding/basic/Stack.java | 28 +++ .../one/src/com/coding/basic/Test.java | 27 +++ 11 files changed, 605 insertions(+), 1 deletion(-) delete mode 100644 group04/844028312/one/.gitignore create mode 100644 group04/844028312/one/src/com/coding/basic/ArrayList.java create mode 100644 group04/844028312/one/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group04/844028312/one/src/com/coding/basic/Iterator.java create mode 100644 group04/844028312/one/src/com/coding/basic/JavaTest.java create mode 100644 group04/844028312/one/src/com/coding/basic/LinkedList.java create mode 100644 group04/844028312/one/src/com/coding/basic/List.java create mode 100644 group04/844028312/one/src/com/coding/basic/Queue.java create mode 100644 group04/844028312/one/src/com/coding/basic/Stack.java create mode 100644 group04/844028312/one/src/com/coding/basic/Test.java diff --git a/group04/844028312/one/.classpath b/group04/844028312/one/.classpath index fb5011632c..3e0fb272a8 100644 --- a/group04/844028312/one/.classpath +++ b/group04/844028312/one/.classpath @@ -2,5 +2,6 @@ + diff --git a/group04/844028312/one/.gitignore b/group04/844028312/one/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group04/844028312/one/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/group04/844028312/one/src/com/coding/basic/ArrayList.java b/group04/844028312/one/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..947b34ed7c --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData = new Object[100]; + /** + * ����Ԫ�� + */ + public void add(Object o){ + + if(size=0 && index<=size){ //�ж� index�Ƿ���size��Χ�� 1 2 3 4 + if(size+1=insertData && left==null){ //�жϲ�����С����һ���ڵ�,left==null + left=new BinaryTreeNode(); + left.data=o; + left.left=null; + left.right=null; + } + else if(insertData>nowData && right==null){ //�жϲ�����������һ���ڵ�,==null + right=new BinaryTreeNode(); + right.data=o; + right.left=null; + right.right=null; + } + else{ + BinaryTreeNode treeNode=null; //��¼�ȽϽڵ� + if(nowData>=insertData ){ //�����ǰ���ڵ����ݴ������������� + treeNode=left; //�ȽϽڵ�Ϊ��ڵ� + } + else{ + treeNode=right; //����Ϊ�ҽڵ� + } + BinaryTreeNode tempNode=null; //��ʱ�ڵ㣬���ڼ�¼���ȽϽڵ����ڵ���ҽڵ�Ϊ��ʱ��¼�ȽϽڵ� + while(treeNode!=null){ + nowData=(int) treeNode.data; //���ĵ�ǰ��ֵ + if(insertData<=nowData){ //�����ǰ���ڵ����ݴ������������� + tempNode=treeNode.left; //��ʱ�ڵ�Ϊ��ڵ� + } + else{ + tempNode=treeNode.right; //����Ϊ�ҽڵ� + } + if(tempNode==null){ + tempNode=treeNode; //��¼�ȽϽڵ� + if(insertData<=nowData){ //�����ǰ���ڵ����ݴ������������� + treeNode=treeNode.left; //�ȽϽڵ�Ϊ��ڵ� + } + else{ + treeNode=treeNode.right; //����Ϊ�ҽڵ� + } + } + else{ + treeNode=tempNode; //��ʱ�ڵ㲻Ϊ��ʱ���ȽϽڵ㸳ֵΪ��ʱ�ڵ� + } + } + if(treeNode==null){ //���ȽϽڵ�Ϊ��ʱ + treeNode=new BinaryTreeNode(); //�½�����ڵ� + treeNode.data=o; + treeNode.left=null; + treeNode.right=null; + int upData=(int) tempNode.data; + if(insertData<=upData){ //����һ���ڵ�����ݴ��ڲ���ڵ������ʱ + tempNode.left=treeNode; //��һ���ڵ����ڵ㸳�����ڵ� + } + else{ + tempNode.right=treeNode; + } + } + } + } + else{ //left!=null&&right!=null + BinaryTreeNode treeNode=null; //�������ж�һ�� + if(nowData>=insertData ){ + treeNode=left; + } + else{ + treeNode=right; + } + BinaryTreeNode tempNode=null; + while(treeNode!=null){ + nowData=(int) treeNode.data; + if(insertData<=nowData){ + tempNode=treeNode.left; + } + else{ + tempNode=treeNode.right; + } + if(tempNode==null){ + tempNode=treeNode; + if(insertData<=nowData){ + treeNode=treeNode.left; + } + else{ + treeNode=treeNode.right; + } + } + else{ + treeNode=tempNode; + } + } + if(treeNode==null){ + treeNode=new BinaryTreeNode(); + treeNode.data=o; + treeNode.left=null; + treeNode.right=null; + int upData=(int) tempNode.data; + if(insertData<=upData){ + tempNode.left=treeNode; + } + else{ + tempNode.right=treeNode; + } + } + } + } + return this; + } + + + +} diff --git a/group04/844028312/one/src/com/coding/basic/Iterator.java b/group04/844028312/one/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/844028312/one/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/group04/844028312/one/src/com/coding/basic/JavaTest.java b/group04/844028312/one/src/com/coding/basic/JavaTest.java new file mode 100644 index 0000000000..2d424110a1 --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/JavaTest.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +//import java.util.ArrayList; +//import java.util.Iterator; +public class JavaTest { + public static void main(String[] args){ +// ArrayList a=new ArrayList(); +// for(int i=0;i<5;i++){ +// a.add(i); +// } +// Iterator i=a.iterator(); +// while(i.hasNext()){ +// int c=(int) i.next(); +// System.out.println(c); +// } + + +// a.add(0, "aa"); +// a.add(100, "bb"); +// a.add(200, "cc"); +// long start=System.currentTimeMillis(); +// System.out.println("1start:"+start); +// System.out.println(a.remove(40)); +// long end=System.currentTimeMillis(); +// System.out.println("1end:"+end); +// System.out.println("1:"+(end-start)); +// LinkedList l=new LinkedList(); +// for(int i=0;i<5;i++){ +// l.add(i); +// } +// Iterator i=l.iterator(); +// while(i.hasNext()){ +// Object c=i.next(); +// System.out.println(c); +// } + +// long start2=System.currentTimeMillis(); +// System.out.println("start2:"+start2); +//// System.out.println(l.remove(40)); +//// long end2=System.currentTimeMillis(); +// System.out.println("end2:"+end2); +// System.out.println("2:"+(end2-start2)); +// a.add(1000, "a"); +// Stack s=new Stack(); +// for(int i=0;i<100;i++){ +// s.push(i); +// } +// Object o=s.pop(); +// Object o2=s.peek(); +// System.out.println(o2); +// LinkedList l=new LinkedList(); +// for(int i=0;i<5;i++){ +// l.add(i); +// } +// System.out.println(l.remove(90)); +// l.add(90, "a"); +// System.out.println(l.get(90)); +// System.out.println(l.get(89)); +// System.out.println(l.get(91)); +// System.out.println("a"+l.size()); +// l.addFirst("bb"); +// System.out.println("b"+l.size()); +// //System.out.println(l.get(0)); +// l.addLast("cc"); +// +// System.out.println(l.get(l.size()-1)); +// System.out.println(l.size()); +// l.removeFirst(); +// l.removeLast(); +// System.out.println(l.size()); +// Queue q=new Queue(); +// for(int i=0;i<5;i++){ +// q.enQueue(i); +// } +// q.size(); +// q.isEmpty(); +// q.deQueue(); +// System.out.println(q); +// ArrayList a=new ArrayList(); + BinaryTreeNode btn=new BinaryTreeNode(); + + btn.insert(2); + btn.insert(3); + btn.insert(1); + btn.insert(6); + btn.insert(8); + System.out.println(btn); + } +} diff --git a/group04/844028312/one/src/com/coding/basic/LinkedList.java b/group04/844028312/one/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d27a6c93f6 --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/LinkedList.java @@ -0,0 +1,178 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + if(head==null){ //���headΪ�� + head=new Node(); //�½�һ���ڵ� + head.data=o; //�������� + head.next=null; //��һ���ڵ�Ϊ�� + } + else{ + Node node=head.next; + Node temp = null; + while(node!=null){ + temp=node.next; + if(temp==null){ + temp=node; + node=temp.next; + } + else{ + node=temp; + } + } + if(node==null&&temp==null){ + node=new Node(); + node.data=o; + node.next=null; + head.next=node; + } + else{ + node=new Node(); + node.data=o; + node.next=null; + temp.next=node; + } + } + } + public void add(int index , Object o){ + int size=size(); + if(index0&&size>0){ + Node node=(Node) get(index-1); + Node newNode=new Node(); + newNode.data=o; + newNode.next=(Node) get(index); + node.next=newNode; + } + else if(index==size&&size>0){ + Node node=(Node) get(size-1); + Node newNode=new Node(); + newNode.data=o; + newNode.next=null; + node.next=newNode; + } + else if(index==0&&size!=0){ + Node temp=new Node(); + temp.next=head.next; + temp.data=head.data; + head.data=o; + //head.next=temp; + head.next=temp; + } + else if(index==0&&size==0){ + head=new Node(); + head.data=o; + head.next=null; + } + else{ + System.out.println("�±곬����Χ"); + } + } + public Object get(int index){ + if(index==0){ + return head; + } + else{ + int i=1; + Node node=head.next;//1 2 3 4 5 + while(node!=null){ + if(i==index){ + return node.data; + } + node=node.next; + i++; + } + } + return null; + } + public Object remove(int index){ + if(index==0){ + Node node=head.next; + Node temp=head; + head=node; + return temp; + } + else{ + Node temp=(Node) get(index); + Node node=(Node) get(index-1); + node.next=(Node) get(index+1); + return temp.data; + } + + } + + public int size(){ + if(head==null){ + return 0; + } + else{ + Node node=head.next; + if(node==null){ + return 1; + } + else{ + int i=1; + while(node!=null){ + node=node.next; + i++; + } + return i; + } + } + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size(),o); + } + public Object removeFirst(){ + Node temp = null; + if(size()>0){ + temp=(Node) remove(0); + } + + return temp.data; + } + public Object removeLast(){ + Node temp = null; + if(size()>0){ + temp=(Node) remove(size()-1); + } + return temp.data; + } + public Iterator iterator(){ + return new Ir(); + } + + + private static class Node{ + Object data; + Node next; + + } + private class Ir implements Iterator{ + private int cursor; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return cursor!=size(); + } + + @Override + public Object next() { + // TODO Auto-generated method stub + int i=cursor; + if(cursor0){ + return false; + } + return true; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group04/844028312/one/src/com/coding/basic/Stack.java b/group04/844028312/one/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..79733597ca --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); //��ջ��ѹ��Ԫ�� + } + + public Object pop(){ + Object o=elementData.get(elementData.size()-1); //ջ��Ԫ�أ���Ϊջ�Ƚ���� + elementData.remove(elementData.size()-1); //�Ƴ�ջ��Ԫ�� + return o; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); //��ȡջ��Ԫ�� + } + public boolean isEmpty(){ + if(elementData.size()==0){ //����elementData.size�ж��Ƿ�Ϊ�� + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group04/844028312/one/src/com/coding/basic/Test.java b/group04/844028312/one/src/com/coding/basic/Test.java new file mode 100644 index 0000000000..a06411f912 --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/Test.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.BeforeClass; + +public class Test { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @org.junit.Test + public void test() { + fail("Not yet implemented"); + } + @org.junit.Test + public void arrayListTest(){ + + } + +} From 58f30c7a4e9671735bebe7a7b510eec70329df07 Mon Sep 17 00:00:00 2001 From: khalil2333 Date: Fri, 24 Feb 2017 17:52:51 +0800 Subject: [PATCH 045/432] zuoyetij --- group03/1753176091/bin/.gitignore | 1 + .../src/com/coding/basic/ArrayList.java | 56 ++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++ .../src/com/coding/basic/FileUtil.java | 68 ++++++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 120 ++++++++++++++++++ .../1753176091/src/com/coding/basic/List.java | 9 ++ .../src/com/coding/basic/Queue.java | 25 ++++ .../src/com/coding/basic/Stack.java | 28 ++++ 9 files changed, 346 insertions(+) create mode 100644 group03/1753176091/bin/.gitignore create mode 100644 group03/1753176091/src/com/coding/basic/ArrayList.java create mode 100644 group03/1753176091/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/1753176091/src/com/coding/basic/FileUtil.java create mode 100644 group03/1753176091/src/com/coding/basic/Iterator.java create mode 100644 group03/1753176091/src/com/coding/basic/LinkedList.java create mode 100644 group03/1753176091/src/com/coding/basic/List.java create mode 100644 group03/1753176091/src/com/coding/basic/Queue.java create mode 100644 group03/1753176091/src/com/coding/basic/Stack.java diff --git a/group03/1753176091/bin/.gitignore b/group03/1753176091/bin/.gitignore new file mode 100644 index 0000000000..c2d9872a16 --- /dev/null +++ b/group03/1753176091/bin/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/group03/1753176091/src/com/coding/basic/ArrayList.java b/group03/1753176091/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..88e9ec8ce0 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/ArrayList.java @@ -0,0 +1,56 @@ +package com.coding.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; + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > 100) { + grow(elementData); + } + } + + private void grow(Object[] elementData) { + int oldLength = elementData.length; + int newLength = oldLength * 2; + Arrays.copyOf(elementData, newLength); + } + + public void add(int index, Object o) { + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + public Object get(int index) { + if (index < 0 || index > elementData.length) { + throw new IndexOutOfBoundsException(); + } + return (Object) elementData[index]; + } + + public Object remove(int index) { + int lowLength = size - index - 1; + System.arraycopy(elementData, index + 1, elementData, index, lowLength); + elementData[--size] = null; + return (Object) elementData[index]; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group03/1753176091/src/com/coding/basic/BinaryTreeNode.java b/group03/1753176091/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/1753176091/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/1753176091/src/com/coding/basic/FileUtil.java b/group03/1753176091/src/com/coding/basic/FileUtil.java new file mode 100644 index 0000000000..856abf7249 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/FileUtil.java @@ -0,0 +1,68 @@ +package com.coding.basic; + + + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +public class FileUtil { + + public static String byteToHexString(byte[] codes ){ + + StringBuffer buffer = new StringBuffer(); + + for(int i=0;i= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node newHead = new Node(o, null); + Node f = head; + for (int i = 0; i < index - 1; i++) { + f = f.next; + } + newHead.next = f.next; + f.next = newHead; + size++; + } + + public Object get(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node f = head; + for (int i = 0; i < index; i++) { + f = f.next; + } + return f.data; + } + + public Object remove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node f = head; + for (int i = 0; i < index - 1; i++) { + f = f.next; + } + f.next = f.next.next; + final Node d = f.next; + final Object element = d.data; + d.data = null; + d.next = null; + size--; + return element; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newHead = new Node(o, head); + head = newHead; + size++; + } + + public void addLast(Object o) { + Node newHead = new Node(o, null); + Node f = head; + for (int i = 0; i < size - 1; i++) { + f = f.next; + } + f.next = newHead; + size++; + } + + public Object removeFirst() { + final Node f = head; + if (f == null) + throw new NoSuchElementException(); + final Object element = f.data; + head = f.next; + f.data = null; + f.next = null; + size--; + return element; + } + + public Object removeLast() { + Node f = head; + for (int i = 0; i < size - 2; i++) { + f = f.next; + } + Object element = f.next; + f.next = null; + size--; + return element; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + private Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group03/1753176091/src/com/coding/basic/List.java b/group03/1753176091/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/1753176091/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/1753176091/src/com/coding/basic/Queue.java b/group03/1753176091/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b5440dbade --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + + private int size; + LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.addLast(o); + size++; + } + + public Object deQueue() { + size--; + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/1753176091/src/com/coding/basic/Stack.java b/group03/1753176091/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..0ca338f666 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (size > 0) + return elementData.remove(size); + return null; + } + + public Object peek() { + return elementData.get(size); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} From 0b3d2828f6a48a32b0e038b008d8e05dd1196c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Fri, 24 Feb 2017 17:56:49 +0800 Subject: [PATCH 046/432] =?UTF-8?q?=E6=9A=82=E5=AD=98ArrayList=E3=80=81Sta?= =?UTF-8?q?ck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apn/coding2017/basic/ArrayList.java | 124 +++++++++++++++--- .../org/apn/coding2017/basic/LinkedList.java | 34 ++++- .../java/org/apn/coding2017/basic/List.java | 2 + .../java/org/apn/coding2017/basic/Stack.java | 48 +++++-- 4 files changed, 174 insertions(+), 34 deletions(-) diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java index 5ad2b6cd97..3d2a685f35 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java @@ -1,6 +1,7 @@ package org.apn.coding2017.basic; import java.util.Arrays; +import java.util.NoSuchElementException; /** * Created by QiPan on 2017/2/23. @@ -16,8 +17,6 @@ public class ArrayList implements List { private Object[] elementData; - private int modCount = 0; - // 定义一个默认的空的数组,引用不可变 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; @@ -42,28 +41,80 @@ public boolean add(Object o) { return true; } + @Override + public boolean add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + ensureCapacityInternal(size + 1); + // 从插入位置开发,所有的数据需要往后移动 + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + return true; + } + public Object set(int index, Object element) { - return null; + checkIndexOutOf(index); + Object oldEle = elementData[index]; + elementData[index] = element; + return oldEle; } public Object get(int index) { - return null; + checkIndexOutOf(index); + return elementData[index]; } public Object remove(int index) { - return null; + checkIndexOutOf(index); + Object oldValue = elementData[index]; + + // 计算需要移动的位数 + int needMoveNum = size - index - 1; + + if (needMoveNum > 0) { + /* + * src:源数组; + * srcPos:源数组要复制的起始位置; + * dest:目的数组; + * destPos:目的数组放置的起始位置; + * length:复制的长度。 + */ + System.arraycopy(elementData, index + 1, elementData, index, needMoveNum); + } + // 避免对象游离,是的gcc能工作回收 + elementData[--size] = null; + return oldValue; } public int size() { - return 0; + return this.size; } public boolean isEmpty() { - return false; + return this.size == 0; } public Iterator iterator() { - return null; + return new Itr(); + } + + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * 检查数组越界 + * + * @param index + */ + private void checkIndexOutOf(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } } private void ensureCapacityInternal(int minCapacity) { @@ -72,13 +123,11 @@ private void ensureCapacityInternal(int minCapacity) { // 取得为与默认容量相比的较大值 minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } - ensureExplicitCapacity(minCapacity); // 调用确保有能力放下那么多元素 + ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { - modCount++; - // 防止容量溢出。所需的最小容器大于数组长度 if (minCapacity - elementData.length > 0) { grow(minCapacity); @@ -87,16 +136,17 @@ private void ensureExplicitCapacity(int minCapacity) { /** * 扩充容量 + * * @param minCapacity */ - private void grow(int minCapacity){ + private void grow(int minCapacity) { int oldCapacity = elementData.length; // 得到一个新的容量大小,为 oldCapacity 的1.5倍 int newCapacity = oldCapacity + (oldCapacity >> 1); // 如果扩容后的大小比,最小容量(DEFAULT_CAPACITY: 10)小 - if (newCapacity - minCapacity < 0){ + if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; - }else if (newCapacity - MAX_ARRAY_SIZE > 0){ // 扩容后比最大的还大,考虑溢出 + } else if (newCapacity - MAX_ARRAY_SIZE > 0) { // 扩容后比最大的还大,考虑溢出 // newCapacity = hugeCapacity(minCapacity); } @@ -106,15 +156,57 @@ private void grow(int minCapacity){ /** * 这个很少用到这个判断,毕竟基本不会使用那么大容量存储 + * * @param minCapacity * @return */ - private static int hugeCapacity(int minCapacity){ - if (minCapacity < 0){ + private static int hugeCapacity(int minCapacity) { + if (minCapacity < 0) { throw new OutOfMemoryError(); } return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } + private class Itr implements Iterator { + + /** + * 当前游标 + */ + int cursor; + int lastRet = -1; // 是否是返回最后一个结果 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + // 游标已经超过数组的大小 + if (i >= size) { + throw new NoSuchElementException(); + } + Object[] elementData = ArrayList.this.elementData; + + // 指向下一个元素 + cursor = i + 1; + Object elementDatum = elementData[i]; + lastRet = i; + return elementDatum; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + // 实际上这个时候的 lastRet,为 next方法时候的 i = cursor + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } + } + } diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java index bac49b845a..92d25ae5db 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java @@ -7,22 +7,19 @@ public class LinkedList implements List { Node last; - private static class Node { - Object item; - Node next; - } public boolean add(Object o) { - return true; + return true; } public Object set(int index, Object element) { return null; } - public void add(int index, Object o) { + public boolean add(int index, Object o) { + return true; } public Object get(int index) { @@ -44,7 +41,30 @@ public boolean isEmpty() { public Iterator iterator() { return null; } + + private static class Node { + Object item; + Node next; - + Node(Object item, Node next) { + this.item = item; + this.next = next; + } + public Object getItem() { + return item; + } + + public void setItem(Object item) { + this.item = item; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + } } diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java index b76bb0eec9..15c9d9d3be 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java @@ -8,6 +8,8 @@ public interface List { boolean add(Object o); + boolean add(int index, Object o); + Object set(int index, Object element); Object get(int index); diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java index 60a4c1c8fd..3233954cf8 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java @@ -3,23 +3,49 @@ /** * Created by QiPan on 2017/2/23. */ -public class Stack { - private ArrayList elementData = new ArrayList(); +public class Stack { - public void push(Object o){ + private E[] elements; + // 记录元素当前位置 + private int N; + + public Stack(int cap) { + elements = (E[]) new Object[cap]; + } + + public boolean isEmpty() { + return N == 0; } - public Object pop(){ - return null; + public int size() { + return N; } - public Object peek(){ - return null; + public void push(E e) { + // 如果 N 和数组的长度已经相同,就进行扩容 + if (N == elements.length) { + resize(2 * elements.length); + } + elements[N++] = e; } - public boolean isEmpty(){ - return false; + + public E pop() { + E element = elements[--N]; + elements[N] = null;// 避免对象游离 + // 如果元素值剩下容量的1/4,那么就把数组容量变成现在的一半 + if (N > 0 && N == elements.length / 4) { + resize(elements.length / 2); + } + return element; } - public int size(){ - return -1; + + private void resize(int max) { + E[] elementTmps = (E[]) new Object[max]; + for (int i = 0; i < N; i++) { + elementTmps[i] = elements[i]; + } + // 指向扩容的数组 + elements = elementTmps; } + } From 07940f8b889f1c6ae66b294d5d2324e98c68af2d Mon Sep 17 00:00:00 2001 From: "yanght1454385822@qq.com" Date: Fri, 24 Feb 2017 18:29:57 +0800 Subject: [PATCH 047/432] =?UTF-8?q?cpu=20=EF=BC=8C=20memory=20=20=EF=BC=8C?= =?UTF-8?q?=20hard=20disk,=20=20=20=20order=20=20=20's=20=20common?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\347\232\204\345\205\263\347\263\273.docx" | Bin 0 -> 26679 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" diff --git "a/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000000000000000000000000000000000..4399ea1b1d304b3993c28506ca305ac873f5f609 GIT binary patch literal 26679 zcmeFZWmr|~x-dK@jiex50s_(@APu5)H%KGh-7!%Ck?!u2Mo?*(h;(A z_DefkXA@gzeN_*86DM63cN^=+*$8k?GXXdd{@;K9gEjCawnx5|4O{F?^7`cpv-E7G z2nyFOTsIDb(vNALm~krK&gG`tbO#D0?XPlK6~VESXY4QvX^u%W1Y=qB!*f{Q$waZlv&OeHF+B-52#TcOcE^k4NAT8Og+^^IIx8pP~=v; zO?_I>80nIW`sHUx7r&SkJ{i5-SZj!bO8QA@XfQkP_nKEiR~(cn^f$DH_hQ z%0xN~BwuB*hl!aLe_*z8B7QF-$46&;HkdCb*YVYf`a8PN!flvQPs799+!@YC8F;Bf zhZNj7-)!Hba8g29Gj>CG~KTj06Dg?%)B%zqv7a6mj)2Xq)n&m!g4gtnX-I?ZnD*|NU?G{U7YJ zzlk0iJtGbO0zGgaalx?D=lRS7s^O`5$t*XjZTR;|I}kU_-rVV?#+=7>iZgm^zt(#c z##7#P|l-Jb(!` z3pIG)*xMx){#(Qr4|#ukJr`9cU7$BLSRDs&pyJ9!0?n0JR^tz0Hgp4 z#LdppnDtLbF}8c-VgqKq`*imYr-6Vu4}|`o{Z|sxcOUcL?@KnkT;X?6$rSr!!3rH^ zeB|Jo=3?}4Cd%!iYvzq|Ts|i%spIFBuL8#&7lS4Dx!u^r@US zc+}JwK8&tNdYTpxI|jA>Dj=*aAWHvK=ewu|AtGJ~J>!QW?S3VE1BCW6I1>5TV{ygF z%Tx`~rjMlO&KyY&3*!bo)P3e@dA;RTGWa%I3_N$GyRpMKd#T>f3<_J{Qey`s7!E!X zLYAIt!ad{AzhA*BCVM`3R4h?PjeaUeUdjI(yX=O&3MR{so*kpXE>ve4454kpJvq3EpfxZ`DA@!ea?-4`Ctt41+!BGOhL(ruDu)W`{ zZs+y3-OM=NoshvDd`o!%Q&+xm@CVh&$~Op-&vPt3N5G3I;hRM#Jf^3gXA|-0{A_5} zUoq}6wLQxh9Q7lD#cI9!HX$5qQ{^rotyQKgnuhM%n9(LSjGNVCU+`>p;Udk6qC-Qd zsgOZdibFQZoSvHx+mJ{KS4w_7UwLOeLl=Wav;7MKGu_iyG0Hok#+4KWemBL8CPETF zvKcJ!;i))dShtNnGHb^72esyFMyj@uDw+&ErtQ*(r&4Ntl93=ruR|-&B0v}{ejRO# zt;8Wm$VzQa(_#$oU|G$XEkhg{M;FMTxdyQ3e=jnSB{;#BoIsT6@BV1r2i=iupbyNQ z%+5OW7tM+noeU^=fEZzlm1Bi3*q$fY0f%t?ur)9jiuvjpTBR>XSda8Odi-RDxOwj2 z=pxRUxRa+7TzXdB>z`W^DPvn6(ZSXKI02|7_zNGjL3X{2#zt^*qN?R+D^Na~Z`zaN z@pV{RWS#C0qRSEq9( z{$h|OUPo7aT|Cr>*ws zi#Ay1tFXp*8w1)~{X7)*_{N^kr@X_wFem+8C+a^2g_36;Zv$nu1^BJw?ZNWsB=2?e*J4(onhWR4P zK5vlBV5X9Xa`{|NyQ@aVST$B^-m1FS<%3-ktE>LoXrG3yCcP@RGljmTpCYM-{uw`p z8d{tE6K)C|L+VH#z0Tbmyt+$kCa9L4_PC6&-99zlxY|daLkFlPtgsqNgTlHs`fr`g zw)guVcm|(2Ph6bESKSV4`T1wrH5pL|I2Aed_eWKI(QS4@Fu7V}9rb$K^i|`j&Gw{L zv)R7ww<|g1E*4yxe*19DT8hq@&vG!l^g4bd#)Z0*4vG1>{W-s~TE=6t!8AOl1wZlU zwtjE+`*^=u)gUqbN$w%UQ5 zxxRSGG+%jE)c=#i=HjKv$-8%{KK92-Cj;2R;W<_N1U={EkvcU<|tzor}S zxZamujuY=V&c*pmHhoWqNWd;b?D{t`Zfa8-wLJ(>HYRQi7R$<(6tK1V+~@_rce1XG z%47X(@%MTIk)Lr6Br`vqecy%@WW>HF*c`$?khI2OmdB~{`is6}14&s`IZIGSZPpJ1 z+|Ik*vApA7lPS%}J*YcfhLGJv{Q0hE3)#%`rUxk`yvS!IpK547*(;q~p>i;M82N=% zIEAlMHWsZ{aqC>|wrllHoc2(VmHd=e11r zWOdfd0%pR=uY!q~;^0xTJDJ2j${})?ea%H%;Ov6;t7>e!Qfne$z@BP9pPOTp}yB zE0nG_%RDtCth(0-|2--F9kP2cM1qQ)(^>= zoJciso^+`oqVolRxJ#yq7aNVS6zi@Y!+%0a2l694&?mu|{C_8LSj4cdVEo=+Q~2xgPQ0BMy>lAjKXUb0+mZPqeHE4+V}T->xEi-O<-X zhe67l&X|SL>4 z=heX}==Bzr5dnZXszJh$da-K4>2+MmdNKrnqWv`~PfoAvSPUeDL5{xNfif)VGI0+` z;Ihp@yUkb&5?q{qXLv*Sg7`1q9QSnEjc>-m69x`gfhUIN!cu=uNpM;xrcD^P2N{6n zBDY?$zvWb(`+CoNz-%+LYpkstWTa-brNTG#ahR$bv)xx|7s@kM14oy`dns%^*h(m< zC8@`Bd$dcHbvtj+5_NR1?^)IUJ7;Bc0pCpgT>5%k*JNC2*!fWcz3IdRKf0pK?^@Evl>vOnot#J1)7aswhF+HrWEw( z#N`BSvW49;?XI0GuzY#Eitd=coL$<5m$4QUY;m6tu_9+%AI5T9dJhg#Ee6(RmTAsMR)ZN2prM=m!LMgXW#mqDz^ zAZ(pRci4XIQK(dOZ_DF?P%eBTB*h24!7XaLt#}~D^ zM9al=?H5NOJ-xVhhcuuM3?1S&-?DX-=ii9RN2F{Wxb$AzfJ~P;KQJJPzV|t_m#%!$ zMVDt>w-`%=_41!B`Jid!m+-Fu;G7<^qkQk)da2Q)l)_>DMYklb%lmo-vf~VfOxn%u zVIZOygbJB^+hjb54k3cqyt&vGQ2Xkz6+6~XVYaUXY8&SC7le%0SX?Sip>g4%Tluko z%QI*gs_ZQs0L^&#XMhljo_pr~s$9@UPWj?uJ3mCxl^oVxMeXMg3Mfno)$In|%tt=l z<7?^-UVi0Bm@qx*%L$7{RMU)%mtzs2PaMGi6(i_$>~1%S_&!0H4y}6MP*89sjw!m} z_q4OcAKANOo-z`G4)IsWxqPpk;^&#pi;=XMm?(d^MI|g(P(%z1Y}T2|;mr;y=-C5~_>{0Nj#)2FOb% zKi<=DM>R2NP_N>DQSe)h2onBv#t3fVdE)%3%G+)VEWhD}a$Eq|c=^{Fcb1%}@JRJjrUDO{rqS<4q(6>~l^+h_E7v@k6uvXrkR}rLdMd)2Mfr!?L ze(-B)uRUx9(}6IxJKeS{zY(|yzl_gm*-IorO(gpJr#$O2e0NWkfZf7Tx-U9Sqq<(xxnb zj;aQ}F-;(KWdXy|fqZWZ6E*M7w;gaz$=KH_mAed8DnV|)wKMgcJ_+Kg1jXAnbu__> zZY@%UfY~mzXh+d;@-euWnvi9B{sdCmU154{9Tgy^wAQ^FC~jt8$J?xGqga1b7N0zDIKu3sE9h$X($3v2>Kb zEZYonWCPd+i4956D@oKuJC7YFw^kkfs3hQEULy|;pM_SCC{uOBIQE*Z+6d5f0I>RD zG(#DVR!n6hlcN&>O5`2M`AJ+y0BE4l#s0<+Q=;X{b>t%uJy#!JeJSY*591%YGdSkx z#Z=BN_r7#9*-q(7X=c=hEDCOTS1=M;Ak(uKc#l_`EX?$DIB1_jVDpcmz?_^j=O>$`V`*mKzfR-=yf^eXTp3>^-bvD&K`44Jm*n(GpNJmTFTdqZSf$cd!wUUVBd+VuQM29K~DHyHLthVRcbSq6I+nHl;*_)7BR`JgO^UB%K=bop1n?^h>z9 zEpFt5q$|;Jcle9$0TT=$K8FO-ctRA=Q~ae8Hq%M4a1SW)94?-Hn^Px$R~_H|*~R5O z#sG6M7zYRD02}stLm1ORydy3r7$0N?gP;K>PkrY!Q%ATeVcOktgZBttP{64#sl9PX zs?gS23|mz$9Zs{up)Xf(l4(TJV-cApzD6Muu6(=4$~Wu_P3x4^&3h0w zOL`B*>WNi>oRm+Z1r@gqzeh#Th&=gDc0{eno~Bwdjsf_UeFmf?DAh$F)>1)gi!=$ug)g{zg3ou&#Hv`0=MtL`%!_-YI%ana6?wW*-#v_E9vGVo@ zmJY!})6_wew|nbczzefp#ht%(-60wzD%A}6#-QR9x8=oyr7vLvX0Z=o{v;NPhym%z zUt(0KB_$WDE{I~he&c$1b~1)k@w%M1RDb}+f|1dY#00>sIY|s+I9Q)o9$-f2mc@*i z{NO7`F~ALsSb3rbB!-#7TPv@vGK@Vx^ahHB{ZS37(J8v5iU2l}KBURq-X66gyX^KN zmH5kBYwGdD!v2g2Gv1mc)o=5hisA5&G+T)KQI#{eqP4$1Hpeu0f zmtlNsxIMadD(J-dHlw>4fGD;p5FaGZ!6zfr?~?w$7!ql)2R z(kN`2J~nHrI*v#<+uvTc4;B^peSG|U zIkjI?=%fcHsSbOVpOLv+3g-rcLV$jOL z12WUU-|yiw%38jAbsT9EZnzowUOLmVRjo@f2;I3`s`dM(1-q62!(pNBg%Hmk%N>WN zdUr7y`oKW+R6e|>he|0Z*?&ZqQD%cK@@AAiE_n7?(pTD#ee@)&@0VYDb<(?H^jws? zq}F@SeeE&1H-p|Uq#z&s_k2Y3Yn5e57S@$rzq&}<%oHz+j@m=6*bH@vOdYjuJf|-q zpGx;bH}GJ)Ufcd?HF5h^7p7MPRuIO^U1t*lg$Y}CelxH{I396<_Vr-v z=GB3l{zY!;O;KPd8w_ZNx7J(R5XZ-llVM*dmu-r>&(&yQV6F}wRx2uxuM(!~sNlHp z1as+snZmsyEx;6RzOr^-1$Yj5fmMJAn6c3vweRZ^G7Djtym7v*F$AfW`99f(f1xlC z=1_kQ7CAi_Ltu^aD-Rx0AH7s39ao(|lKodDqTA&S;;>_?76mJ~!8BTg9XatGaeQ}~ zTGV?aG#f8r^Tyq_#{Si@DVg`R*IOZk9Yv&F5ahQqNyudO9wdsj=^iB97GXz)cF##` z8DoV}IDzZW5%s`5a1vOA-PcxP7Lu@ZlV9K@-JS%H0f0$P0nU{L-kln4N406~K5Zd> zp)pwJc$d*bTpz=FeqT!7kKAztf+PIV<)`-}WX`mU%*KQu4OdG2t=bX@%+g#QR^KV< z%VK4%-!ycO2P{Ar!;IPNjQzuUCZ<4AC879;4GE#^VN4ZT7gYD$oDAhbt79l{bjBx> zz5VXX&g~o-h#lR_%Q&X0--_dTCGitREz^^u09~*$FNxDbY$!zE(WvbGz1Fr$njJ6+ zQ|nG}rs)AHZ29mH_BfhBo_{Od0pTKRk*4VTnm=r?4hZMy*YDy8=}vICpFvhfAY%Nf zIr{fr0U8r}s`9s%Vo}-jno%o1#Ct=d5*?Vyje*E^RtVGZC&~$J&`&{tl)NN}Y)|L? z$P1yL_aoZ*8IakSDL-k~1F8$n0%K&rI-?BfKtvRVG4+FDB?oM!00RO}ZfL<1jM+%J zYek`^KiaBSUhwYfMlCi|9k@qPbMZbv1uGvZY~TsSY`o8GWW*-eOhW~S*8yK@t)*`} zIZ+X7_aisMyMQliEjZ%kb;0vT-^MS9wOAfaxz|1ZJm$Hd#y7Cp9J~PZs6Bp`S^L0y zwBB#!HQ?T0EyhJNr$@q{((La>?I1zVa?s!!^c>rr{??8{)}nh4ad{ol9NQFn41t4r z=U31vnzz@An0jG4<5;T(fI%h~oOAT6BW+$836G_PZg8Cgkx(n3#<3cmM0bzrJOff6 z0yQtH5HywF_O!2pJHQbe$$(W9XVt@6b6}CqSrHm1J*X9_xh;q2=+}`XOn?g2&~8A6Um@21Jzm zdHA;4&pOqU+tsU<8P*gnw|aVak9DlCPaUft2NpRpt1znU?F*IYH5Ya%?EOp*85nRp z9gh%x0&u?q;57qUXVWBvRZUOPwCZgv6;O%G(G7xG25N``+l!0nNZjIk>ILlUJbx`( zlIyhm@&vb4_vql&IZ3+dxP)v&%=m#xItaP}9;TBUx!|t7WYqeY%j)!KI{TLDh(o3- z1us8C&}=BrJLHQ`YVty)iu&v0Lwzo(^9yYx^`6V?j$0(#9(HE?XM1!SMFkzZ#lkf> zfUC$q_)@5M>U0Qg_WE8pR=nB#Xs_*qS^5IsKunnefk~F$-T`q3UBS`8C*Fgd8w`a? z_ivH70_tZJSYW~A>pZ&jwQRt<0Xl-XzVyQj(Lfl14XVb2KJW1>HRx*JE>TlXfY7O} zbkR8pr+>NU1c;U2mHXdc{d)G`t9w}=k5$T zHiTpvWed_%!s)fvJc71Gt&F-E095cH3{YQU^H?9kO`r1i8~9;~3Wv<4HTqp_*!<0p z7jkL@7b}iFRAr$-Fh* z?k+T!AF7({o}2UB)@1oCtsR~pT=V@y;W41Xk5>AxW@y!CP@cVqezqq^ohEefRZS77 z)Jr#Bl@+YedYU4$^hkUwr|NiZBl-0nxJ&-+?wrXgNUo3~Hlb9e@3?NgJ@9D9{by^_ z(shTl-1{IolU4^eOzYU~uJLjQYJi^>3Sru3bGsL6*4uC9#PaQr^93inyQ_^iw!C~a z;lyOoLj9piPgoqqyrPxpw7HSJm!()E7;yGTrs5n#tu5O7jn`=H;tRnQyH+=nU z-$a>kc2a(Ot)}nj4WHw%-L|~eDg%Mu;J|=*x5S3yji`j3)Yq2EZIqaye#08@TDVWz zH|&XI!;#6m<-YLz$UVf`?%wBrPIi_+-E-kMdg!3H&XGpibMhXocR7dh6 zWuQO^7x*Ug2Z=q9MEb1y?6f!O5o>(Aq@$;!r?0a*zgUt*!nT=Nx%6SXRI;p!XxAok z*;0*T*>dYCLO~8Di(Xk{(V?gGW{uO+Id6$o=(Y;}iAj}!p4dgg_(#XB)b+&!@1LIS z@9Y|IfcnSa^6#)YtPklb*pkdm`{`2Pi17cSc#uG}h>c+>!YdZ!RN(Es5Sn?ah^ zEa~KZrR*}|zH-8t5#IOOiyrJ+1RMAJyI6&zjcQ7khd0GoxYxeZ6<_qUnU;!%OpY{`|!svOaQ z3ep=w1EOZhSAM8UaD+gKzb|?PIs_e^U2vW0p;%#U_!yJWg*2Ko5C^}xPcqr zK?WH9fO!JK9Cfs2IG`)F?yD~=Xs)(Uiu%Onjy);Msq=pN^$v5=od>WF0g%}0DNC-8 z{OtUE78T;oM{g}peArfsm-LZ(y3b=K4z8J!Pz`{CNC0WDi!ya(XvEi{lU4SP$PiU| z-Gfp7b1|wp!SoFvLIZ_3J4(`kaXe%1E`(B&=2_a^BXwNOtJZNs*pb2cz(fYl1bqes zAOIu2uXKD@4Cp7P#59g%V|UNi*oD&KlTp-h$5(|}wD0*5YSq!LF*GtMscHd}#efRw~Dv`-zsd7M*BK0xA%EP!g4p<{8vB z7|-;xfXHobJgO|)B?@J-i){5;z?)j-hd<$^v>7SDNfF-fI=!A+;vlF@< z)AN_V%j7cMTpnRvzs?l7icJ}E6{x)qZcg3zTD?!Jsf>p>@qhauUsSohm* zsF&DmIp_^)MI-(2;}_+xI}%;KrDrtL!cD5#zjKrUuRH$H0furMs29px!K+Itb#$!1 zVA_6jUDvI(PLGMSsz>zuVy1DM78sYQeBpb9xk{_;;}k1K4DL(*Q)vvp;;zcU4mds&3-YWRrk(e|XZ04m zocJ$kMfGd%{trO$J$Yph;U7|c?_NPdgzj;Ij+W$V2wsv&{w12SwM_1Y>gc}2|R*b};y4+-1^RAWP47cZvz>)3;i2sgcrr&RiSag(L zCoF;%UehO93=$f%+&C!to5^{qpuRr5@L`3f*)%FV@bap~{hPg2$Lg129x`+l3p8`z z!o@#wmDF4vD$Qtjck1xU`rTVj0#`~y0M})LimIE|C6nC>`V;tzkQaMFzZ?7(#=@#F z|D82h2VZ_ttJ${C`0~58&%1`|5J0R&Z~BT?D!tnEJKU{m$^Onx8e&B`c+~H>BO{Stkk}RG6 zu^53CdYHOAH}tz+E>m%e+OgF#Fm3Oy3~_TSsuTF{FPr@z@2cvB|4qzcEs@%j|3?h_ zzx)HdRUsHG&i?)VZw>rg1HfRL-`x#``2;pO->?wq6#yV5CjY8T)Zp#kpC8_|mJj;^ zo-YQUF2)0){&+sk+1$j&g!PYa_WLIzceE7ja3vl!Yizj@?vfeNxqhG~eZ!`u_HpAyH42042KsiNixfL<+GAwny$1E8^CM zpOv-rKCp!eK3Yckp+^?>Nnx-m!_VWe@7;p!!aFcA=I6AED3#>u9VBg7VEmXeuV|es z67LMQe&&D5X62b_+j(}i5`ToR=Vned6k!An<_olK7sk1zi!1@sJ z0u-d$Vaw`qp6aNc=Rm%xLz;ZST;bXgI&dZAZfQf`UE-oqR4QuxYN7&!%m$as``}AAHXyi=)FI_wd^dGDJDGB>?{6?vaphk24EO#4iOeg ziHc7K8QCtIKYlv?;DDhefX=lm;hD^9g6O8qU`tO5Izr+%4Etjv`}%@Dp_qFDyfw!o zEpzqAZoXgMrs}5fV1BIE^SzqV$Fj>2rtrHtx+Pe3*ZOgH+uCT>?5l_Q{xm81__{~J zPr&2XbxQ$jQ@uW%K7qU9`!kDIPwGTZhcF{x?6`OnjvUx@GecPH%zK&<@WF3NzQ%U$ ze;RzDpJWhk+VMF@Y!*e2K!m~s7P6cY-qNTTipMiL?X1H= zu}mj4q=qc?tlPQTVP~cKnLPA)Ri!hjUCe&HRo7oD_|WHjDx2Pq zHvu^uQ9h)fjbN;A@ZyQ4Qc#*dY)c|0VWKR5diF#)ez%qZwjespYG_b0{Kf9G)CyHV zcD)G)>zn?NPK>4*Oo!N5LoZIukjaP@me{A_GdgiC)ipDem(Nxp9Zn-YX*hIq5>+i+ z#5}ZeXs?8cLZWr_p3D|5%)bv~V^@XG-LrRjwd_Y_EAC%g8aP62o7ugtFUa)yN;hxE zg{QIG)ASfyX`q!Xf1R7)<>g-0F%o&l3<9yy=?71R{{610tAUu~245i`>GVoA>@VqJ zJDE1^d**wig4v-bqwk}7hSBG7`qK>w@K1!C4xd~wGodai%ynhLS`gt<1SRcHP}K}s zf1sl8swtqM8Z+=kK3&3L2{Elz{np;PSiYlHRmMsg&-+AO+uJ-L;g_T9L}wdeF`QFY z-y>!VNQc@tXBNB%O^xA|MciAH(?#u^FF%HVn>8Uba-t84TrX=eB9|ky#10k3UNxz? zv>LOYY6(SmnY9V8{F2C&YWmUmL(r4Hs8cU)3ALEq5_MD3A>T=(L4pqIZ1utML8Aqc z8HZ?9RgIJ!mY(?(WBTGni+GXgC0ApI&%9cprzv_lO`2zs5n!}<>&0t+ z)3C=Dnkfvc=Owc_?xb1&viZ^O2YP?moA~!UdD?T?T6Eeql)1A=$At#u=jxW=4FW4= ziSfcT8Xrxrg`LDQ=nhT|Ej~S4Y$Y-BkJ^jP?+AFnTcL=f+{|cT&o&DggWGpoVt<6! zP{7@cI5pC2GoVG3U%G@-=xPo{2soAucpGVxNu%N;fl{1rmVno#{31X5!CLo^WWmQ? z?@zdpS+=X@UQ))^8OFpSq3_w>)Mkue5+ita(x0ZZ8edFVc`e!~2z^Uxp-%|v6NzaP ze$GyTm0wUaXZOv_%x>u6jRBwOSY;Vt5|EP__3g$g+5UUZILCNsN61MpPv_5)j*3LA zSEvmyMK6VU?mit5v73}&jS4M5Y{d>%1Mrbd;MIKEG07!yb9G5!i^c>t>=sJgUp6;# zbK=Ry3j{XTJh2e9wOv!aGuS3C?AMB%JP$eFrHM}lf7MMPvFzMg!Vkz(a*bjSW?zWM zYwr5_E+ujUJznEAJ|8;t+zG3?q%#SaqTy~C>Dz1shL z4BN@X+1bL@%;`SGeNor4o8iIs)i=0%m$mNV{bLVFG?glNnKobqU#;>=Ai5p@d4q_u zda8c&OXZK2DgFL9eciYt`xMb3FeOrp+Dam5^Z#qTcL2qe1->&5Ph`Sr_KP+ zTaax@TeT9(uq8#$Zmt}b|2X;+X~(kh3LdZ63$8j<{n#GMH>%7%RtV9Jg?jX>uetI> zZ8(rIQD-ez1XSHx9zKnwog>CpO$d1+4QCuo@~O7|oq&^c%n;^ATa^WgqD3ZUq(Wev zUzS4&kZ$o*ax_VPjlyV$rYgDh_BGLIZZ3S&j!*fg~EKa!?u{Q2g?1wQm;0$CkM*jL~Fof;|ED0zYv`g-36Pj73fE$;HS* ziL+X7IGs^gMJw7Yu}|!oyz+Op0D8*vutM zHHd#Joej$kJ;v|&xn+srrB-i1VkVAXfGMc>Hgh_7*fn~#2uCpIt08Omao^D?roV2{ zcQ+R0A9zDyl(61iUK+W&P{E~ICgBlA!oK{(CH;uSMnLg$)H^T9vwEUNG?QtV^VW|A zEwz+qq&jrEZ?-d<@2FXi+j3Ykp`8CUZ{9BELZLQ4ar<&i%!T9EkF^J;ji%h``n@dr zBfSM3#~w0^SpyjwRm(BobTPSZ1RrUUL`dE|zBBEYU+#(#!2T@4@?s^hWpO;hlL0Sk zZEfjomwX0wDc`kH%$P?3DQ3?12aDcfax?Uz3n8Ne!8EpTOwEW@yAIlD$3?NUW9w#2 z!q11ivmu4cHenF5uI;jvUQ=fBm{E`Hm>IqSK31vaOJqE`nR5PRh=h-4^5{@JmRv=k z?_L(^yjj!Jhb8>g==fbKTn*Ph8-smqIqn>w)tN4-@fWU@k~Ma>!ER2!%5x*$AJ8qx zYtM0RY-CIX2rHgCxm7Ofi8HcI_jyh=#PoSh1<8w3(96A6IwqQa zjKyhKmy=-kXuYWAs1BDUnjkw7LEB$EdzzJS3y4F491;O+b+;OEglifcC$BbDC*Pv~HaG(0;t z(qjE0KT~b>R2)^MbM<58L#nrzfQ73oNw4vDS<4ayyXm6GdY(N`k|L7)YA;3f8{tE8 z1`AE~iXXhqYY8Y+DQDfD3M+bIw9;roiLRD646lr)2$VkTL59b5GD#p^F=)A^6I zOWSNAVZHRCPa~+=lU1eDIf;E|+LCBOX=}llvU{SYt6yvvIG|)snijxc$^Skw{3f}D zAnL8PZY%|JMvlo(N)9b-`m0P~4|AP!ljJsGlr$3@;*kcX90lA>c(v)2OLx(7hA+FV zk3RIZCm=XgDf$FQ7&&eT8OnM0#3E`^l!cweW{~g8WZfCh{{H0ZzurrRhgfp(8hjW= zRTuz>|DiJ{XAf%=Ck+c{b1_p>lQ+(PhHULV#~E(yCb#^3*wS2-F|X^-;ONhB!UHQE z!8@BoRFr63QWZ~3raYjomvDk7!`fJlC!+*MPR@`>Y`Kp=%eqpHm0-SCSIke|=r`11 zz^@0=6-EyIAQL32!;BEv4B4>!si)U4=e5RX(N2|t$ng{REWBW^I6?UKz{n@n)D5CQ zhK-%qu|1hm)58=Jf&6Y%`zSI`L3D|RBQ9hon6A}dhv|I>vQ^UgyPGBRPj?@2HxD@x zztD%+YHqFl{9eaNbc|Ag&w;8HFBH`^_Sm@Vgkc_jlfCDM4gys@{8VeJXlQCp@e{s& zyy>b3h0Gn|S=V`!QKKc3Z>?OL;^!Gwl=2@Uqhg656XnS~wCi;*;be2r`s7^I^C5-p z>3He#&`I7!I2x*1dAqY*}>-M$&KyQK~C z3)O!#{=Nn=|3Xg2!$3WkAcEKQOYay_8&Ruho_1a8mfNpDf4=*vOxg*%j36ZoR8&Oz zsmS70%u_rT@9$d4X}NFXguP>gRWew|z8?j-79^O`_!oR9*c0*BJAU#u>^W0n&DEOH zWuFisoVIyo!w?_x7B|9xbir;47Qj=Qp|JeGto^mBx0i+3zvF9JX0?p?-D= z)>1npv`1s>>t$m(k6{&(+)uu$LKjM`flXg7txkDR8N+e%IAjP9Q1rV$okv?(|F{?u zFr<8bT|g7TqOxa~?&ndw$(69tr55Y|1~bC>(YsMUr|m<{A<{)LS+R|nX@C^rCi8s` z-s|8n?zI<)YQoof(B8&IJd>^@OR4a2Kf1_oo?}Gj`dX^oB0V&le0wZEGwVMT%!!Hc z?{0`^T~9#%t(A;~NGfy@aToNv}_b2aVke7kQ}q{{xCWZPJYZrgiO~jE1adj{uS)73URAY zswr9SQuT8jRzLjOVII17IuF71_^a58yBKFv_Tyg{4Pv$7hBuGP;*;PD_{JQy1sx_A zf0-%}UQoO$@~Nmwq2Nv&rX|H|Z4GR&#=niaGQz_zdyy)RiIIW(UB`crVb68Q|AA8D z2klZt2rSWH8gUMRnIfy%g2nlQp`GwXj?pvM`~7XcgP<~af92dE&9gqMbFCjQ;wB!} zx)yLmb{XM4;u@63l30204VwqIQ_OlbNAl+foffQ(5#B?A^R>V(6-Op+0cjD+pw`R{ zqt+QwOzD7AafFagNRh!VZbh8~GvtG8o&H6bqVP}U6n|3-uW@VUb%%GznTT2agZkCj$pWBn;uQI*OkWt=S9Bjvk3li%&?)t?q8d}dK1gBVyDG6+iKR^}1xES(Y`z-hn zYq*gYr=s(Vb$y~dKj$-wsH7>lq1Pe`KDZ9ImY?R=bUvNQAw1v}IcW+Op_C{X?y+8Y zWpFb7{Gld>ZVs=wiF_WR*j|dsV=o*L%~325YUm}U9fG3pN*J&uKfn9 zJ<4Qo#S};4z{$&bluX}F63D+uxEmc)HJrONPSIfhk%_#&h!UvPt*XCZ_(9d zm;3VbD32VWTPtDrtc!j+jr<6sLjLsDq>IEqFF`SyvSYYQj=cyZ@ZYjdHS!LTA?7*U zhh7ZrE^UHkH#)m}t+*VCXGZ3zP^$Jgp)ff@88qWThWPaPKuAee4<_ukY>;U~r9adW z1d2V^RZo^Hd?aoVRomid`G+DrD@05{38QKa89la%FZS-4MgH^$=gti8EkRbrFctr$ ziOnB7vhyg98&@8R)O~zvxCLUx|3f=_D|48`6du$SowEkKU+-Z!1T`}`z!mA7&D@*% z?xXbUWK`)Tj=?_BqiW(o=oQ9`?{I5KzN={IFxAI)&1Az5D=H$Plab__xC(g@+Higy?>G7pTHy=&9I z%bmPJYKzZe-!dzAXG>rkN2wPPYsApwGSZc>LzVUsK7dqKkeR21mi z>s8vcqqoe@vKNo(vED|=A~VJ~91lZTIgU5YKV7{MdxXFqV|aY~N}pmP*%}vMXJvdd z&p@3VL-{(uDSahjCA ztzM$uIiGJ;K_0Pi4|NH)mh11|o$uZb_@#!esCZK#y>vI8?9REcX#~i)&%^9rVmca+fAA+xs1K-}I=xAr}#QMh0(d4i9 zr2hMzaiHo#m0Q4f#-TS-A7ZKP=}u!37EQ~;eZh^1B!qK73!Vu0Y7y(ceR-CkRN z*dp0?ic+>@-?w5ywycA~$TG51h(xwg3`MeqzSdA7Tg)I!)=!oQWoPVLzK7~o^Xb0- zg8Te(&JXi?J+5=+nsc7ld0+FomPK1b)D+#+wH5f7&;CQc?R%L=U9%3VS$$5GpF=7P ztaaJZNs4SbIm$|F$3gL)xtMljG)k7Oxw?n(Efl(5hYK<3Uwi%pWm~zWyj|=cIB&F; zb)$M*p+segnU^t^cXikE%$uEUl9&Kr(v$C~#0qr!#L&p1e3>lM(u&R=G;JS4441Ls zJ9qB$uVL#YnW45!ZR6d;o}!8uC6w=tYJ6&WXZfm@6N5?%?{w)oF+fdT?~&A49l}2m zu)SGk8Y@CxpUkcj=vhjBSL${2-Or^Kj_^^_9)8>x$9?0}Xa;?7wd5CM^4?#&s;(J_ zOE2&reA|maNW!1+eE5>XtleNpjix3?XfeH;ZIPV>gO?#5h{?lKuPaR!pb>*)@d$1w zs!gJJ9K}SUwyX3+lA&e9R}KA#62i}WU$A2N2fB2E_IAn{v#;;Ev4(wMihFL*WazzW z_f3Xt{VK zmml~&tSPAgSUjL@eTc3DSF?5E?^Jo9*yG0TsE!8Lx?zFVs$0VGF3TEu0%D!i$)F6TthFdNmz^lf)W-h{Sp*)0i0=JHe4jCXNaOdXj?7O{g#iIvMQ7|7$pZrUiK1l;EJo;4JdG!iBAHpin)7q!*r>73$Rx&7F3H6H?EKc;E^+0Gl zR~1@8sq~QRp>g|q+28J_HFjWJr)*n;foegUwx4d7M#bM3;L-P5tS z*;tD=IYMx(4IASDuil#jcvS*60Pt#p#(1p7A+N3nY)+|R4N(BE;uNd_UbQOjajeu_b~ga6|qv-m>aCbPn@49BrtGhsW>`3dzE*@yFyp0Nw=3CnQTM!6}#+yWPYr6G2#>#6KzA9BC=`Iftz1YwNlgrAWEfG6>MEMmpn(uCW zV`&!`@(^kkK#htV8NtDZu=R4p_NcvBLf)x$?ASejOQ2o{mWC(6T7i8mc z*ZR#aQcZPKS@2Em(7M71gdPQurLKatdQRk9GLy=%1TqE@Z7)bcU<(EWU@Gi@guws_ z>w*ACBm0GZd{)Ij(2q>P`4Ij1Kx2%@C7pSZ1=~oy-(3M-Rn`@IDd+%!efnr9E3Q1Nsf7>&h%8mGbwC!(7++p(D7`UAiw z6PWu3C+)?bB#L57Cr{!Ijk({Ih zzicQ4oDT`6!2pOY+1Db;j|46=z`=a!MF$+LJpvIy9dPgrsmVd}NMWVtfJ@=l${k#E zU+Mr?$$r@X`(j@>#8p`Ez)cuC@e!3Fo*%&&Ihw|AVb-Ef@@1t zRBg=Y=CIYrFuj){PX>p{)lf!F?e3yfYLTnvMwh^$tQ}SyEe(` zcSfic^8?WMeveM>U9*meJ706SC`bL63v>!oD#C5zl5;XS*PHJPklJKFn_3~WWLMXH zMFv79p~czDY1>_6`n$||w%RwcL)nnG%BzTPraI(PNi>IM`&M214>YVyFilf80d|Ix zfwFz9ItcEtP&YKJF6;MLT`+OkD`P|uuCM_Kkv2|aa9nb$l=t^73A*vTo`x?ejm`U{ zTx;pVEMlmuAOz~>UCMtdF~3%iQxV~7QmiHHm>4d4u61v5e|UdqWAA$H$o}%Dy{G$9 ztgZQ$II^rcK4iUTwO-;~s z{Uk;cwn6L*4cRg2(Kl?4I7#nH>6xIKa56Ys5F-!-IquTy@U3Ly8RmVDHYoX=fPZ#CvBKFP0>|-@-)fVFh1A;(Vlqz85fjj!u9)l zyNhgo;d1Qfz+=E&v1F|#IZJS{828NiW{RIHz@=zHz;MT7}-8&UY|DWdzqg=;=` z&w0HWo#**)KbAlxqKqo#GRJg<;~g*Ul!^Bhqm@uDM)5w>r=1+ao#>f#u;_%0PBB|W zJCjf6uF|G_mA?;t7Uvyye1V}44(L%dzYl$^-Q50vu$AwW#xy`$Dvp2rJ`DQgT-qnzd9^2rZ&e)ynn#sY(Ax{6DXy+>F@Ri8O-l_u4x8fVR|$%YnSpcgKI>DyQ)Pk;0LOk5-$%I|_DO`fGV%c#N@ ziVPg0BJDF^Ht$-0OUK7m1S1me_qC=@^*<%ty%Vum3po-uZ^E0;1+iKLW4=4#r~M5&H_`^;GR;nmQ0@ag z@<+ji2rLQ|SN^`N$*=F{*ZwbyoLtua)8Nkq6@Cd1_RT@)4l)eJCeP0wrt#;jFMD3#h{XGDnQi zg0SL#WU-+3)8hB~JE5Ggbao`S)BY(ZD5McO5Ehb-93JcZYQW#hN`yXtzWzCK0fB(| z$Dn_`3?h^h4#|%UdX4{R#=-rRP)ztVKN9y}|0(|C*-ohc^ZtFL27!2Oerx;ByZL2p V;=`GOR3Ivl4$#?qfjCzX=)b*a*L45@ literal 0 HcmV?d00001 From 5810a2be3b94852fe6e4ba6597894f97dbc964f0 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Fri, 24 Feb 2017 18:43:47 +0800 Subject: [PATCH 048/432] Iterator --- .../src/com/coding/basic/ArrayList.java | 30 ++++++++++++++++++- .../src/com/coding/basic/LinkedList.java | 30 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/group04/1299310140/src/com/coding/basic/ArrayList.java b/group04/1299310140/src/com/coding/basic/ArrayList.java index fb484db727..afc073293a 100644 --- a/group04/1299310140/src/com/coding/basic/ArrayList.java +++ b/group04/1299310140/src/com/coding/basic/ArrayList.java @@ -64,7 +64,35 @@ public int size(){ } public Iterator iterator(){ - return null; + return new ArrayListIterator(this); + } + + public static class ArrayListIterator implements Iterator{ + private ArrayList list; + private int pres; + + public ArrayListIterator(ArrayList list) { + super(); + this.list = list; + this.pres = 0; + } + + @Override + public boolean hasNext() { + if(this.pres < this.list.size()){ + return true; + }else{ + return false; + } + } + + @Override + public Object next() { + Object o = this.list.get(this.pres); + this.pres++; + return o; + } + } /* diff --git a/group04/1299310140/src/com/coding/basic/LinkedList.java b/group04/1299310140/src/com/coding/basic/LinkedList.java index ab330879b7..4636bbd279 100644 --- a/group04/1299310140/src/com/coding/basic/LinkedList.java +++ b/group04/1299310140/src/com/coding/basic/LinkedList.java @@ -151,9 +151,37 @@ public Object removeLast(){ } public Iterator iterator(){ - return null; + return new LinkedListIterator(this); } + private static class LinkedListIterator implements Iterator{ +// private LinkedList list; + private Node pres; + + public LinkedListIterator(LinkedList list) { + super(); +// this.list = list; + this.pres = list.head; + } + + @Override + public boolean hasNext() { + if(this.pres != null){ + return true; + }else{ + return false; + } + } + + @Override + public Object next() { + Object o = this.pres.data; + pres = pres.next; + return o; + } + + } + private static class Node{ Object data; Node next; From 3ab6f5eb492793cf168ec96315bded657bf73002 Mon Sep 17 00:00:00 2001 From: yangdd Date: Fri, 24 Feb 2017 18:57:04 +0800 Subject: [PATCH 049/432] LinkedList Iterator --- .../src/com/coding/basic/LinkedList.java | 33 +++++++++++++++++++ .../test/com/coding/basic/LinkedListTest.java | 11 +++++++ 2 files changed, 44 insertions(+) diff --git a/group18/1049843090/src/com/coding/basic/LinkedList.java b/group18/1049843090/src/com/coding/basic/LinkedList.java index 5bebe8caa6..7b4a378301 100644 --- a/group18/1049843090/src/com/coding/basic/LinkedList.java +++ b/group18/1049843090/src/com/coding/basic/LinkedList.java @@ -202,4 +202,37 @@ public Node(E data, Node next) { this.next = next; } } + + public Iterator iterator(){ + return new LinkedListIterator<>(); + } + + private class LinkedListIterator 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) LinkedList.this.get(lastRet=i); + } + + @Override + public void remove() { + if(lastRet<0){ + throw new IllegalStateException(); + } + cursor = lastRet; + LinkedList.this.remove(lastRet); + lastRet = -1; + } + } } \ 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 index 8b4ef44068..fb962287d3 100644 --- a/group18/1049843090/test/com/coding/basic/LinkedListTest.java +++ b/group18/1049843090/test/com/coding/basic/LinkedListTest.java @@ -115,4 +115,15 @@ public void removeLast() throws Exception { } + @Test + public void iterator() throws Exception { + Iterator iterator = linkedList.iterator(); + assertEquals(false,iterator.hasNext()); + linkedList.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,linkedList.size()); + } + } \ No newline at end of file From 4a04135a93dda197799ab31d8afe226f97549e2a Mon Sep 17 00:00:00 2001 From: "kaitao.li" Date: Fri, 24 Feb 2017 20:12:37 +0800 Subject: [PATCH 050/432] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coding2017/basic/BinaryTreeNode.java | 30 +++++++++++++++---- .../coding2017/basic/BinaryTreeNodeTest.java | 24 +++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java index 29910f06a3..acf4798b9e 100644 --- a/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java @@ -2,19 +2,35 @@ public class BinaryTreeNode { - private Object data; + private Integer data; private BinaryTreeNode left; private BinaryTreeNode right; - public BinaryTreeNode insert(Object o) { - return null; + public BinaryTreeNode insert(Integer o) { + if (o <= data) { + if (left == null) { + left = new BinaryTreeNode(o); + return left; + } + return left.insert(o); + } else { + if (right == null) { + right = new BinaryTreeNode(o); + return right; + } + return right.insert(o); + } } - public Object getData() { + public BinaryTreeNode(Integer data) { + this.data = data; + } + + public Integer getData() { return data; } - public void setData(Object data) { + public void setData(Integer data) { this.data = data; } @@ -34,4 +50,8 @@ public void setRight(BinaryTreeNode right) { this.right = right; } + @Override + public String toString() { + return data + " " + left + " " + right; + } } diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..3a9877c596 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,24 @@ +package com.coding2017.basic; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/24. + */ +public class BinaryTreeNodeTest { + + @Test + public void insert() throws Exception { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(5); + binaryTreeNode.insert(4); + binaryTreeNode.insert(6); + binaryTreeNode.insert(5); + assertTrue(binaryTreeNode.getLeft().getData() == 4); + assertTrue(binaryTreeNode.getRight().getData() == 6); + assertTrue(binaryTreeNode.getLeft().getRight().getData() == 5); + System.out.println(binaryTreeNode); + } + +} \ No newline at end of file From 4b19a0be08cb09df0b322fcb6393047d8cf2cdd2 Mon Sep 17 00:00:00 2001 From: tennysons Date: Fri, 24 Feb 2017 21:35:00 +0800 Subject: [PATCH 051/432] Tennyson's task --- .../296933284/DataStructuresTest/.classpath | 7 + .../296933284/DataStructuresTest/.gitignore | 14 ++ group14/296933284/DataStructuresTest/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/coding/basic/ArrayList.java | 147 ++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 77 +++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/JavaTest.java | 185 +++++++++++++++++ .../src/com/coding/basic/LinkedList.java | 188 ++++++++++++++++++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 50 +++++ .../src/com/coding/basic/Stack.java | 61 ++++++ 12 files changed, 772 insertions(+) create mode 100644 group14/296933284/DataStructuresTest/.classpath create mode 100644 group14/296933284/DataStructuresTest/.gitignore create mode 100644 group14/296933284/DataStructuresTest/.project create mode 100644 group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/List.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java diff --git a/group14/296933284/DataStructuresTest/.classpath b/group14/296933284/DataStructuresTest/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group14/296933284/DataStructuresTest/.gitignore b/group14/296933284/DataStructuresTest/.gitignore new file mode 100644 index 0000000000..e32d56a40c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.gitignore @@ -0,0 +1,14 @@ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ diff --git a/group14/296933284/DataStructuresTest/.project b/group14/296933284/DataStructuresTest/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs b/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.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/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..116020466d --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java @@ -0,0 +1,147 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList ʵ�� ��14С�� 296933284 + * + * @author Tonnyson + * + */ +public class ArrayList implements List { + + private int size; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initCapacity) { + elementData = new Object[initCapacity]; + } + + /** + * ������ĩβ����ָ��Ԫ�أ����������������Զ���չΪԭ�����ȵ����� + */ + public void add(Object obj) { + + ensureCapacityInternal(size); + + elementData[size] = obj; + size++; + } + + + /** + * �������ָ��λ�ò���Ԫ�� + */ + public void add(int index, Object obj) { + + rangCheckForAdd(index); + ensureCapacityInternal(size + 1); + + for (int i = size - 1; i >= index; i--) + elementData[i + 1] = elementData[i]; + + elementData[index] = obj; + size++; + } + + /** + * ���������� + */ + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity - elementData.length > 0) { + int newCapacity = elementData.length * 2; + elementData = Arrays.copyOf(elementData, newCapacity); + // elementData = tempElementData; + } + } + + /** + * ������ add() �м�������±��Ƿ�Խ�� + */ + private void rangCheckForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } + + /** + * ����ָ������λ�õ�Ԫ��ֵ + */ + public Object get(int index) { + + rangCheck(index); + + return elementData[index]; + } + + /** + * ɾ��ָ������λ�õ�Ԫ�أ������ظ�ֵ + */ + public Object remove(int index) { + rangCheck(index); + + Object obj = elementData[index]; + + for (int i = index; i < size; i++) + elementData[i] = elementData[i + 1]; + + size--; + + return obj; + } + + /** + * ��������±��Ƿ�Խ�� + * + * @param index + */ + private void rangCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + + /** + * �������鳤�� + */ + public int size() { + return size; + } + + /** + * ������ + * + * @return + */ + public Iterator iterator() { + return new Iter(); + } + + //�������ڲ��� + private class Iter implements Iterator { + int current; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + + int i = current; + rangCheck(i); + current++; + + return elementData[i]; + } + + } + +} + + diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d01667aaa8 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable { + + private Object data; + private BinaryTreeNode leftChild; + private BinaryTreeNode rightChild; + + public BinaryTreeNode() { + super(); + this.data = null; + this.leftChild = null; + this.rightChild = null; + } + + public BinaryTreeNode(Object data) { + super(); + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinaryTreeNode leftChild) { + this.leftChild = leftChild; + } + + public BinaryTreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(BinaryTreeNode rightChild) { + this.rightChild = rightChild; + } + + /** + * �����в���ڵ� + * + * @param i + */ + public void insert(int i) { + + } + + // ... + @Override + public int compareTo(Object obj) { + + return 0; + } + + +} + + + + + + + + + + + + + + + diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java new file mode 100644 index 0000000000..bc0878cb50 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java @@ -0,0 +1,185 @@ +package com.coding.basic; + +import org.junit.Test; + +public class JavaTest { + + @Test + public void binaryTreeNodeTest() { +// BinaryTreeNode tree = new BinaryTreeNode(5); +// +// System.out.println(tree.getData()); + } + + + @Test + public void queueTest() { + Queue queue = new Queue(); + + // enQueue() + for (int i = 0; i < 10; i++) { + queue.enQueue("hello: " + i); + } + + // size() + System.out.println(queue.size()); // 10 + // isEmpty + System.out.println(queue.isEmpty()); + + // deQueue() + for (int i = 0; i < 10; i++) { + System.out.println(queue.deQueue()); + } + + // size() + System.out.println(queue.size()); // 0 + + // isEmpty + System.out.println(queue.isEmpty()); + } + + + @Test + public void stackTest() { + Stack stack = new Stack(); + + // push() + for (int i = 0; i < 10; i++) { + stack.push("hello: " + i); + } + + // size() + System.out.println(stack.size()); + + // pop() + for (int i = 0; i < 10; i++) { + System.out.println(stack.pop()); + } + + // isEmpty() + System.out.println(stack.isEmpty()); + System.out.println(stack.size()); + + stack.push("hello world 1"); + stack.push("hello world 2"); + stack.push("hello world 3"); + stack.push("hello world 4"); + + // peek() + System.out.println(stack.peek()); + + // isEmpty() + System.out.println(stack.isEmpty()); + } + + @Test + public void linkedListTest() { + LinkedList linkedList = new LinkedList(); + + // add(obj) + for (int i = 0; i < 10; i++) { + linkedList.add("hello: " + i); + } + + // size() + System.out.println(linkedList.size()); // 10 + + // get(index) + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + +// System.out.println("---->" + linkedList.get(10)); // java.lang.NullPointerException + + // remove() + System.out.println("---->" + linkedList.remove(9)); // 9 + System.out.println("---->" + linkedList.remove(5)); // 5 + System.out.println(linkedList.size()); // 8 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + + // removeFirst() + System.out.println("---->" + linkedList.removeFirst()); // 0 + // removeLast + System.out.println("---->" + linkedList.removeLast()); // 8 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + + // add(index, obj) + linkedList.add(4, "^_^"); + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("&&&&" + linkedList.get(i)); + } + // addFirst() + linkedList.addFirst("***first***"); + // addLast() + linkedList.addLast("---last---"); + + System.out.println(linkedList.get(0)); // ***first*** + System.out.println(linkedList.get(linkedList.size() - 1)); // ---last--- + System.err.println(linkedList.size()); // 9 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("&&&&" + linkedList.get(i)); + } + + System.out.println("-------------------------------"); + // iterator + Iterator iter = linkedList.iterator(); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + + System.out.println("-------------------------------"); + } + + @Test + public void arrayListTest() { + ArrayList arrayList = new ArrayList(); + + // add(obj) + for (int i = 0; i < 10; i++) { + arrayList.add("hello: " + i); + } + + // get(index) + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("-->" + arrayList.get(i)); + } + + // add(index, obj) + arrayList.add(5, "Tennyson"); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("++>" + arrayList.get(i)); + } + + // size() + System.out.println("size: " + arrayList.size()); + System.out.println("index 5: " + arrayList.get(5)); + + // remove() + Object value = arrayList.remove(5); + System.out.println("index 5: " + value); + System.out.println("size: " + arrayList.size()); + System.out.println("index5: " + arrayList.get(5)); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("index " + i + " : " + arrayList.get(i)); + } + + System.out.println("-------------------------------"); + // iterator + Iterator iter = arrayList.iterator(); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + + System.out.println("-------------------------------"); + + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..72b4ba8a90 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java @@ -0,0 +1,188 @@ +package com.coding.basic; + +/** + * LinkedList ʵ�� ��14С�� 296933284 + * + * @author Tonnyson + * + */ +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + super(); + this.head = new Node(); + this.size = 0; + } + + /** + * β�巨������ĩβ����ڵ� + */ + public void add(Object obj) { + + if (head.data == null) { + head.data = obj; + head.next = null; + } else { + + Node r = head; + while (r.next != null) + r = r.next; + + Node node = new Node(); + node.data = obj; + node.next = null; + + r.next = node; + } + + size++; + + } + + /** + * ��ָ������λ�ò���ڵ� + */ + public void add(int index, Object obj) { + + if (index > size) + throw new IndexOutOfBoundsException(); + + Node r = head; + for (int i = 0; i < index - 1; i++) + r = r.next; + + Node node = new Node(); + node.data = obj; + node.next = null; + node.next = r.next; + r.next = node; + size++; + } + + /** + * ����ָ��λ�õ�ֵ + */ + public Object get(int index) { + if (index > size) + throw new IndexOutOfBoundsException(); + + Node r = head; + for (int i = 0; i < index; i++) + r = r.next; + + return r.data; + } + + /** + * ɾ��ָ��λ�ýڵ㲢������ֵ + */ + public Object remove(int index) { + if (index > size) + throw new IndexOutOfBoundsException(); + + Node node = new Node(); + + Node r = head; + for (int i = 0; i < index - 1; i++) + r = r.next; + + node = r.next; + r.next = node.next; + node.next = null; + size--; + return node.data; + } + + /** + * ���������ij��� + */ + public int size() { + return size; + } + + /** + * ����������ʼλ�ò���ڵ� + * + * @param obj + */ + public void addFirst(Object obj) { + Node node = new Node(); + node.data = obj; + node.next = head; + head = node; + size++; + } + + /** + * ������β������ڵ� + * + * @param obj + */ + public void addLast(Object obj) { + add(obj); + } + + /** + * ɾ�������ĵ�һ���ڵ� + * + * @return ��ɾ���ڵ��ֵ + */ + public Object removeFirst() { + Node node = new Node(); + node = head; + head = head.next; + size--; + + return node.data; + } + + /** + * ɾ�����������һ���ڵ� + * + * @return ��ɾ���ڵ��ֵ + */ + public Object removeLast() { + return remove(size - 1); + } + + /** + * ������ + * + * @return ����һ������������ + */ + public Iterator iterator() { + return new Iter(); + } + + // �������ڲ��� + private class Iter implements Iterator { + int current; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + int i = current; + + if (i >= size) + throw new IndexOutOfBoundsException(); + + current++; + + return get(i); + } + + } + + // �ڵ��ඨ�� + private static class Node { + Object data; + Node next; + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java new file mode 100644 index 0000000000..969e6dd82b --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object obj); + public void add(int index, Object obj); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9257eb04ca --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +/** + * Queue ʵ�� + * First In First Out + * ��14С�� 296933284 + * + * @author Tonnyson + * + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * ������в���Ԫ�� + * + * @param obj + */ + public void enQueue(Object obj){ + elementData.addLast(obj); + } + + /** + * ɾ������Ԫ�� + * + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * �ж϶����Ƿ�Ϊ�� + * + * @return + */ + public boolean isEmpty(){ + return elementData.size() == 0; + } + + /** + * ���ض��е�Ԫ�ظ��� + * + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e28a9e3760 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +/** + * Stack ʵ�� + * Last In First Out + * ��14С�� 296933284 + * + * @author Tonnyson + * + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + private int top = 0; + + /** + * ��ջ�в���Ԫ�� + * + * @param obj + */ + public void push(Object obj) { + elementData.add(obj); + top++; + } + + /** + * ��ջ��ȡ��Ԫ�� + * + * @return + */ + public Object pop() { + return elementData.remove(--top); + } + + /** + * ��ȡջ��Ԫ�� + * + * @return + */ + public Object peek() { + return elementData.get(top - 1); + } + + /** + * �ж�ջ�Ƿ�Ϊ�� + * + * @return + */ + public boolean isEmpty() { + return top == 0; + } + + /** + * ��ȡջ��Ԫ�ظ��� + * + * @return + */ + public int size() { + return top; + } +} From f83d57403ea4c514d8c92676083f89efc22bd8b3 Mon Sep 17 00:00:00 2001 From: diliuzuzhanghao <949319266@qq.com> Date: Fri, 24 Feb 2017 21:57:30 +0800 Subject: [PATCH 052/432] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=92=8C=E6=96=87?= =?UTF-8?q?=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {liuxin => group06/949319266}/.classpath | 13 +- {liuxin => group06/949319266}/.project | 34 ++--- .../.settings/org.eclipse.jdt.core.prefs | 23 +-- .../src/com/coding/basic/ArrayList.java | 93 ++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 100 +++++++++++++ .../src/com/coding/basic/LinkedList.java | 133 ++++++++++++++++++ .../949319266/src/com/coding/basic/List.java | 13 ++ .../949319266/src/com/coding/basic/Queue.java | 52 +++++++ .../949319266/src/com/coding/basic/Stack.java | 36 +++++ .../com/coding/basic/test/ArrayListTest.java | 39 +++++ .../com/coding/basic/test/LinkedListTest.java | 24 ++++ .../src/com/coding/basic/test/QueueTest.java | 28 ++++ .../src/com/coding/basic/test/StackTest.java | 26 ++++ .../949319266/\346\226\207\347\253\240.txt" | 1 + liuxin/.gitignore | 1 - liuxin/src/com/coding/basic/ArrayList.java | 32 ----- .../src/com/coding/basic/BinaryTreeNode.java | 32 ----- liuxin/src/com/coding/basic/Iterator.java | 7 - liuxin/src/com/coding/basic/LinkedList.java | 46 ------ liuxin/src/com/coding/basic/List.java | 9 -- liuxin/src/com/coding/basic/Queue.java | 19 --- liuxin/src/com/coding/basic/Stack.java | 22 --- 22 files changed, 581 insertions(+), 202 deletions(-) rename {liuxin => group06/949319266}/.classpath (73%) rename {liuxin => group06/949319266}/.project (89%) rename {liuxin => group06/949319266}/.settings/org.eclipse.jdt.core.prefs (70%) create mode 100644 group06/949319266/src/com/coding/basic/ArrayList.java create mode 100644 group06/949319266/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group06/949319266/src/com/coding/basic/LinkedList.java create mode 100644 group06/949319266/src/com/coding/basic/List.java create mode 100644 group06/949319266/src/com/coding/basic/Queue.java create mode 100644 group06/949319266/src/com/coding/basic/Stack.java create mode 100644 group06/949319266/src/com/coding/basic/test/ArrayListTest.java create mode 100644 group06/949319266/src/com/coding/basic/test/LinkedListTest.java create mode 100644 group06/949319266/src/com/coding/basic/test/QueueTest.java create mode 100644 group06/949319266/src/com/coding/basic/test/StackTest.java create mode 100644 "group06/949319266/\346\226\207\347\253\240.txt" delete mode 100644 liuxin/.gitignore delete mode 100644 liuxin/src/com/coding/basic/ArrayList.java delete mode 100644 liuxin/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 liuxin/src/com/coding/basic/Iterator.java delete mode 100644 liuxin/src/com/coding/basic/LinkedList.java delete mode 100644 liuxin/src/com/coding/basic/List.java delete mode 100644 liuxin/src/com/coding/basic/Queue.java delete mode 100644 liuxin/src/com/coding/basic/Stack.java diff --git a/liuxin/.classpath b/group06/949319266/.classpath similarity index 73% rename from liuxin/.classpath rename to group06/949319266/.classpath index fceb4801b5..a7a9bf927c 100644 --- a/liuxin/.classpath +++ b/group06/949319266/.classpath @@ -1,6 +1,7 @@ - - - - - - + + + + + + + diff --git a/liuxin/.project b/group06/949319266/.project similarity index 89% rename from liuxin/.project rename to group06/949319266/.project index fab8d7f04c..d3a5c7c636 100644 --- a/liuxin/.project +++ b/group06/949319266/.project @@ -1,17 +1,17 @@ - - - 2017Learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + algorithm + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/.settings/org.eclipse.jdt.core.prefs similarity index 70% rename from liuxin/.settings/org.eclipse.jdt.core.prefs rename to group06/949319266/.settings/org.eclipse.jdt.core.prefs index 3a21537071..500f6b26f4 100644 --- a/liuxin/.settings/org.eclipse.jdt.core.prefs +++ b/group06/949319266/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +1,12 @@ -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 +#Fri Feb 24 09:38:47 CST 2017 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +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.6 diff --git a/group06/949319266/src/com/coding/basic/ArrayList.java b/group06/949319266/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9d2dca03c2 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/ArrayList.java @@ -0,0 +1,93 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class ArrayList implements List { + + private int size; + private int current ; + private int point; + private Object[] elementData = null; + private static final int DEFAULT_SIZE=100; + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public ArrayList(int size) { + if(size < 0) { + System.out.println("��С����С��0"); + } else { + this.elementData = new Object[size]; + this.current = 0; + point = size; + } + } + + public void add(E e){ + judgeSize(); + this.elementData[current] = e; + this.current++; + } + public void add(int index, E e){ + judgeIndex(index); + for(int i=0 ; i= index && i+2 < elementData.length) { + elementData[i] = e; + elementData[i+1] = elementData[i+2]; + } + } + current++; + } + + public E get(int index){ + judgeIndex(index); + return (E)this.elementData[index]; + } + + public void remove(int index) { + judgeIndex(index); + for(int i=0;i0) { + return true; + } + return false; + } + public void clear() { + elementData = new Object[DEFAULT_SIZE]; + } + private void judgeSize() { + Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; + System.arraycopy(elementData, 0, newarr, 0, elementData.length); + this.elementData = newarr; + } + public void judgeIndex(int index) { + if(index < 0 || index > point) { + System.out.println("�±�Խ��"); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/BinaryTreeNode.java b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..941c31bc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode getRoot() { + return root; + } + + public Object getData() { + return data; + } + public void setData(int 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 void insert(int value){ + BinaryTreeNode newNode = new BinaryTreeNode(); + if(root == null) { + root=newNode; + root.left = null; + root.right = null; + } else { + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true) { + parentNode = currentNode; + if(newNode.data > currentNode.data) { + currentNode = currentNode.getRight(); + if(currentNode == null) { + parentNode.setRight(newNode); + return; + } + } else { + currentNode = currentNode.getLeft(); + if(currentNode == null) { + parentNode.setLeft(newNode); + return; + } + } + } + + } + } + public boolean find(int key) { + BinaryTreeNode cNode = root; + if(cNode != null) { + while(cNode.data != key) { + if(cNode.data > key) { + cNode = cNode.getLeft(); + } else { + cNode = cNode.getRight(); + } + return true; + } + return true; + } + return false; + } + //������� + public void inOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + inOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + inOrder(treeNode.getRight()); + } + } + //������� + public void leftOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + leftOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + leftOrder(treeNode.getRight()); + } + } + //������� + public void rightOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + rightOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + rightOrder(treeNode.getRight()); + } + } +} diff --git a/group06/949319266/src/com/coding/basic/LinkedList.java b/group06/949319266/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ee95a118e0 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/LinkedList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class LinkedList implements List { + + private Node last; + private Node head; + private int xsize = 0; + + public LinkedList() { + init(); + } + + private void init() { + head = new Node(null,null,null); + last = new Node(null,head,null); + head.next = last; + xsize = 0; + } + + //���������һ�����������ڲ�ʹ��;��index֮ǰ����һ���ڵ� + private void add(Node node,int index) { + if(index < 0 || index > xsize) { + throw new IndexOutOfBoundsException("�±�Խ��"); + } + node.pre = getNode(index-1); + getNode(index-1).next = node; + node.next = getNode(index); + getNode(index).pre = node; + xsize++; + } + public void add(E e){ + add(new Node(e,null,null),xsize); + } + + public void add(int index,E e){ + add(new Node(e,null,null),index); + } + private Node getNode(int index){ + Node newNode; + int current; + if(index == -1) { + return head; + } else if (index == xsize ){ + return last; + } else { + current = 0; + newNode = head.next; + while (current < index) { + newNode = newNode.next; + current++; + } + } + return newNode; + } + + public E get(int index) { + return getNode(index).e; + } + + public void remove(int index){ + Node node = getNode(index); + node.pre.next = node.next; + node.next.pre=node.pre; + xsize--; + } + + public int size(){ + return xsize; + } + + public void addFirst(E e){ + add(new Node(e,null,null),0); + } + public void addLast(E e){ + add(new Node(e,null,null),xsize); + } + public void removeFirst(){ + remove(0); + } + public void removeLast(){ + remove(xsize); + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + public E e; + Node next; + Node pre; + public Node (E e,Node pre,Node next) { + this.pre = pre; + this.next = next; + this.e = e; + } + + } + + + public boolean contains(Node node) { + Node mnode = head.next; + while(mnode !=null) { + if(mnode.equals(node)) { + return true; + } + mnode = mnode.next; + } + return false; + } + public boolean isEmpty() { + return xsize == 0 ? true:false; + } + public void clear() { + init(); + } + + public boolean contains(E e) { + // TODO Auto-generated method stub + Node node = null; + while((node=(head.next)) != null) { + if(e.equals(node)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + +} diff --git a/group06/949319266/src/com/coding/basic/List.java b/group06/949319266/src/com/coding/basic/List.java new file mode 100644 index 0000000000..970c0cfc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + + +public interface List { + public void add(E e); + public void add(int index, E e); + //public E get(int index); + public void remove(int index); + public boolean contains(E e); + public int size(); + public boolean isEmpty(); + public void clear(); +} diff --git a/group06/949319266/src/com/coding/basic/Queue.java b/group06/949319266/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b090a3626e --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Queue.java @@ -0,0 +1,52 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Queue { + E[] element; + //Ϊ�˲��Է�����ʱ����������Ϊ5 + private static final int DEFAULT_SIZE=5; + int front,rear;//�ֱ�Ϊ���׺Ͷ�β�±� + + public Queue() { + this(DEFAULT_SIZE); + } + public Queue(int size) { + element = (E[])(new Object[size]); + front = 0; + rear = 0; + } + + public void enQueue(E e){ + if(((rear+1)%element.length) == front) { + System.out.println("���������޷����"); + } else { + element[rear] = e; + rear=(rear+1)%element.length; + } + } + + public E deQueue(){ + if(rear == front) { + return null; + } else { + E e = element[front]; + front = (front + 1)%element.length; + return e; + } + + } + + public boolean isEmpty(){ + return rear == front; + } + + public int size(){ + if(rear > front){ + return rear-front; + } else { + return (element.length+1)-(front-rear); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/Stack.java b/group06/949319266/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2a309fde95 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Stack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Stack { + private ArrayList element; + int top; + public Stack() { + element = new ArrayList(); + top = 0; + } + + public void push(Object o){ + element.add(o); + top++; + } + + public void pop(){ + if(isEmpty()) { + System.out.println("ջ�ǿյ�"); + System.exit(0); + } + element.remove(top-1); + top--; + } + + public Object peek(){ + return element.get(top-1); + } + public boolean isEmpty(){ + return top == 0?true:false; + } + public int size(){ + return top; + } +} diff --git a/group06/949319266/src/com/coding/basic/test/ArrayListTest.java b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..5abd16725f --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test() { + ArrayList list = new ArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + System.out.println("�±�Ϊ3��Ԫ��Ϊ��"+list.get(3)); + System.out.println("���鳤��"+list.size()); + list.remove(2); + System.out.println("remove������鳤��"+list.size()); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + list.add(3, "g"); + System.out.println(""); + System.out.println("����������Ϊ��"); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/LinkedListTest.java b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..2b4c390721 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java @@ -0,0 +1,24 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Test; + +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + @Test + public void test() { + LinkedList list = new LinkedList(); + list.add("First"); + list.add("Second"); + list.add("Thrid"); + for(int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+ " "); + } + + } +} diff --git a/group06/949319266/src/com/coding/basic/test/QueueTest.java b/group06/949319266/src/com/coding/basic/test/QueueTest.java new file mode 100644 index 0000000000..6ec8036c35 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/QueueTest.java @@ -0,0 +1,28 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + @Test + public void test() { + Queue qu = new Queue(); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.enQueue("jie"); + System.out.println(qu.size()); + qu.deQueue(); + System.out.println(qu.size()); + System.out.println(qu.isEmpty()); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.deQueue(); + qu.enQueue("jie"); + System.out.println(qu.size()); + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/StackTest.java b/group06/949319266/src/com/coding/basic/test/StackTest.java new file mode 100644 index 0000000000..d76742e8ff --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/StackTest.java @@ -0,0 +1,26 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + @Test + public void test() { + Stack s = new Stack(); + s.push("gong"); + s.push("bo"); + s.push("jie"); + s.push("hao"); + s.push("ren"); + System.out.println(s.size()); + System.out.println(s.peek()); + s.pop(); + System.out.println(s.size()); + System.out.println(s.peek()); + } + +} diff --git "a/group06/949319266/\346\226\207\347\253\240.txt" "b/group06/949319266/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..f00eb98185 --- /dev/null +++ "b/group06/949319266/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x31y.html \ No newline at end of file diff --git a/liuxin/.gitignore b/liuxin/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/liuxin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 1f185736f9..0000000000 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,32 +0,0 @@ -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/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -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/liuxin/src/com/coding/basic/Iterator.java b/liuxin/src/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/liuxin/src/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java deleted file mode 100644 index e2c4e5e795..0000000000 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,46 +0,0 @@ -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/liuxin/src/com/coding/basic/List.java b/liuxin/src/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/liuxin/src/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -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/liuxin/src/com/coding/basic/Queue.java b/liuxin/src/com/coding/basic/Queue.java deleted file mode 100644 index 36e516e266..0000000000 --- a/liuxin/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -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/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java deleted file mode 100644 index a5a04de76d..0000000000 --- a/liuxin/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -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 40bdce249facc1ce3ea221f84068c4134479c5a2 Mon Sep 17 00:00:00 2001 From: diliuzuzhanghao <949319266@qq.com> Date: Fri, 24 Feb 2017 22:09:54 +0800 Subject: [PATCH 053/432] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=92=8C=E6=96=87?= =?UTF-8?q?=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group06/949319266/949319266learn/.classpath | 6 ++++++ group06/949319266/949319266learn/.gitignore | 1 + group06/949319266/949319266learn/.project | 17 +++++++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++++++++++ .../949319266/RemoteSystemsTempFiles/.project | 12 ++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 group06/949319266/949319266learn/.classpath create mode 100644 group06/949319266/949319266learn/.gitignore create mode 100644 group06/949319266/949319266learn/.project create mode 100644 group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs create mode 100644 group06/949319266/RemoteSystemsTempFiles/.project diff --git a/group06/949319266/949319266learn/.classpath b/group06/949319266/949319266learn/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group06/949319266/949319266learn/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/949319266/949319266learn/.gitignore b/group06/949319266/949319266learn/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/949319266/949319266learn/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/949319266/949319266learn/.project b/group06/949319266/949319266learn/.project new file mode 100644 index 0000000000..aa3f29e266 --- /dev/null +++ b/group06/949319266/949319266learn/.project @@ -0,0 +1,17 @@ + + + 949319266learn + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group06/949319266/949319266learn/.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/group06/949319266/RemoteSystemsTempFiles/.project b/group06/949319266/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group06/949319266/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + From 720d25fa2ae20fa0a189082124f77df9bbe648e5 Mon Sep 17 00:00:00 2001 From: myparamita Date: Fri, 24 Feb 2017 22:53:10 +0800 Subject: [PATCH 054/432] first commit --- .gitignore | 5 + group07/43819473/homework/pom.xml | 12 ++ .../main/java/dataStructure/ArrayList.java | 87 +++++++++++++ .../src/main/java/dataStructure/Iterator.java | 9 ++ .../main/java/dataStructure/LinkedList.java | 116 ++++++++++++++++++ .../src/main/java/dataStructure/List.java | 16 +++ .../src/main/java/dataStructure/Queue.java | 25 ++++ .../src/main/java/dataStructure/Stack.java | 29 +++++ 8 files changed, 299 insertions(+) create mode 100644 group07/43819473/homework/pom.xml create mode 100644 group07/43819473/homework/src/main/java/dataStructure/ArrayList.java create mode 100644 group07/43819473/homework/src/main/java/dataStructure/Iterator.java create mode 100644 group07/43819473/homework/src/main/java/dataStructure/LinkedList.java create mode 100644 group07/43819473/homework/src/main/java/dataStructure/List.java create mode 100644 group07/43819473/homework/src/main/java/dataStructure/Queue.java create mode 100644 group07/43819473/homework/src/main/java/dataStructure/Stack.java diff --git a/.gitignore b/.gitignore index ec55baf87d..4de1b4e4c3 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ hs_err_pid* #ide config .metadata .recommenders + +#add rules +*.xml +!pom.xml +*.iml \ No newline at end of file diff --git a/group07/43819473/homework/pom.xml b/group07/43819473/homework/pom.xml new file mode 100644 index 0000000000..bafc987cc6 --- /dev/null +++ b/group07/43819473/homework/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + homework1 + homework + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java new file mode 100644 index 0000000000..40ccb13a06 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java @@ -0,0 +1,87 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + elementData[size] = o; + size++; + } + + private Object[] grow(Object[] datas) { + Object[] elementDataNew = new Object[elementData.length + elementData.length / 4]; + System.arraycopy(datas, 0, elementDataNew, 0, size); + return elementDataNew; + } + + public void add(int index, Object o) { + + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + for (int i = size - 1; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + for (int i = index; i <= size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return null; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListInterator(); + } + + private class ArrayListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return elementData[nowIndex++]; + } + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/Iterator.java b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java new file mode 100644 index 0000000000..06ad99316f --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java @@ -0,0 +1,9 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java new file mode 100644 index 0000000000..f6a960698a --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java @@ -0,0 +1,116 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/21. + */ +public class LinkedList implements List { + + private Node head=new Node(); + private int size = 0; + + public void add(Object o) { + addToNode(head,o); + size++; + } + + public void add(int index, Object o) { + if (index <0 || index > size) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + addToNode(getLastNode(index),o); + size++; + } + + private Node getLastNode(int index){ + + Node nowNode = head; + for (int pos = 1; pos <= index; pos++) { + nowNode = nowNode.next; + } + return nowNode; + } + private void addToNode(Node node,Object o){ + if (node.next == null) { + Node newNode = new Node(); + newNode.data = o; + node.next = newNode; + } else { + Node newNode = new Node(); + newNode.data = o; + newNode.next = node.next; + node.next = newNode; + } + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + return node.next==null?null:node.next.data; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + Node nowNode=node.next; + if(nowNode.next!=null){ + node.next=node.next.next; + }else{ + node.next=null; + } + size--; + return nowNode.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 LinkedListInterator(); + } + + private class LinkedListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return get(nowIndex++); + } + } + + + private static class Node { + Object data; + Node next; + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/List.java b/group07/43819473/homework/src/main/java/dataStructure/List.java new file mode 100644 index 0000000000..0b1b43fc26 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/List.java @@ -0,0 +1,16 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +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(); +} \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/Queue.java b/group07/43819473/homework/src/main/java/dataStructure/Queue.java new file mode 100644 index 0000000000..943e0e64f6 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Queue.java @@ -0,0 +1,25 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Queue { + + private LinkedList list=new LinkedList(); + + public void enQueue(Object o) { + list.addFirst(o); + } + + public Object deQueue() { + return list.removeLast(); + } + + public boolean isEmpty() { + return list.size()==0?true:false; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/Stack.java b/group07/43819473/homework/src/main/java/dataStructure/Stack.java new file mode 100644 index 0000000000..e1a7161317 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Stack.java @@ -0,0 +1,29 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Stack { + + private LinkedList list = new LinkedList(); + + public void push(Object o) { + list.addFirst(o); + } + + public Object pop() { + return list.removeFirst(); + } + + public Object peek() { + return list.get(0); + } + + public boolean isEmpty() { + return list.size() == 0 ? true : false; + } + + public int size() { + return list.size(); + } +} From 094f0bee1c07da3cbfde82cf1741b2a45f70d760 Mon Sep 17 00:00:00 2001 From: GallenZhang <1298552064@qq.com> Date: Fri, 24 Feb 2017 22:59:44 +0800 Subject: [PATCH 055/432] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1298552064/src/week01/basic/Iterator.java | 8 + group01/1298552064/src/week01/basic/List.java | 13 ++ .../src/week01/basic/MyArrayList.java | 131 +++++++++++ .../src/week01/basic/MyBinaryTreeNode.java | 70 ++++++ .../src/week01/basic/MyLinkedList.java | 215 ++++++++++++++++++ .../1298552064/src/week01/basic/MyQueue.java | 22 ++ .../1298552064/src/week01/basic/MyStack.java | 25 ++ .../src/week01/test/MyArrayListTest.java | 53 +++++ .../src/week01/test/MyLinkedListTest.java | 78 +++++++ .../src/week01/test/MyQueueTest.java | 48 ++++ .../src/week01/test/MyStackTest.java | 53 +++++ 11 files changed, 716 insertions(+) create mode 100644 group01/1298552064/src/week01/basic/Iterator.java create mode 100644 group01/1298552064/src/week01/basic/List.java create mode 100644 group01/1298552064/src/week01/basic/MyArrayList.java create mode 100644 group01/1298552064/src/week01/basic/MyBinaryTreeNode.java create mode 100644 group01/1298552064/src/week01/basic/MyLinkedList.java create mode 100644 group01/1298552064/src/week01/basic/MyQueue.java create mode 100644 group01/1298552064/src/week01/basic/MyStack.java create mode 100644 group01/1298552064/src/week01/test/MyArrayListTest.java create mode 100644 group01/1298552064/src/week01/test/MyLinkedListTest.java create mode 100644 group01/1298552064/src/week01/test/MyQueueTest.java create mode 100644 group01/1298552064/src/week01/test/MyStackTest.java diff --git a/group01/1298552064/src/week01/basic/Iterator.java b/group01/1298552064/src/week01/basic/Iterator.java new file mode 100644 index 0000000000..e209875b54 --- /dev/null +++ b/group01/1298552064/src/week01/basic/Iterator.java @@ -0,0 +1,8 @@ +package week01.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group01/1298552064/src/week01/basic/List.java b/group01/1298552064/src/week01/basic/List.java new file mode 100644 index 0000000000..608c1b532b --- /dev/null +++ b/group01/1298552064/src/week01/basic/List.java @@ -0,0 +1,13 @@ +package week01.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group01/1298552064/src/week01/basic/MyArrayList.java b/group01/1298552064/src/week01/basic/MyArrayList.java new file mode 100644 index 0000000000..c4f6572f1c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyArrayList.java @@ -0,0 +1,131 @@ +package week01.basic; + +import java.util.Arrays; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkPositionIndex(index); + ensureCapacity(size + 1); + + if (index >= size) { + elementData[size++] = o; + } else { + System.arraycopy(elementData, index, elementData, index + 1, size + - index); + + elementData[index] = o; + + size++; + } + } + + public Object get(int index) { + checkElementIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkElementIndex(index); + Object removeElement = elementData[index]; + if (index == (size - 1)) { + elementData[index] = null; + size--; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + elementData[size - 1] = null; + size--; + } + return removeElement; + } + + public int size() { + return size; + } + + /** + * 保证数组空间充足 + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + int capacity = elementData.length; + if (minCapacity > capacity) { + capacity += capacity / 2; + grow(capacity); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private void grow(int newCapacity) { + elementData = Arrays.copyOf(elementData, newCapacity); + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private MyArrayList list; + private int position = 0; + + private ArrayListIterator(MyArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + @Override + public String toString() { + String elementStr = ""; + for (int i = 0; i < size; i++) { + elementStr += elementData[i] + ","; + } + return "MyArrayList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } +} diff --git a/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java new file mode 100644 index 0000000000..30e6c810a5 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java @@ -0,0 +1,70 @@ +package week01.basic; + +public class MyBinaryTreeNode { + + private Object data; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public MyBinaryTreeNode getLeft() { + return left; + } + + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + + public MyBinaryTreeNode getRight() { + return right; + } + + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Object o) { + if(this.getData() == null && this.getLeft() == null && this.getRight() == null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + } + + MyBinaryTreeNode node = new MyBinaryTreeNode(); + MyBinaryTreeNode currentNode = this; + while(true){ + if((Integer) o < (Integer) getData()){ + if(currentNode.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setLeft(node); + return this; + }else{ + currentNode = currentNode.getLeft(); + } + + }else{ + if(currentNode.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setRight(node); + return this; + }else{ + currentNode = currentNode.getRight(); + } + } + } + } +} diff --git a/group01/1298552064/src/week01/basic/MyLinkedList.java b/group01/1298552064/src/week01/basic/MyLinkedList.java new file mode 100644 index 0000000000..4894c5ff6c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyLinkedList.java @@ -0,0 +1,215 @@ +package week01.basic; + +public class MyLinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + // 空链表 + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = head; + while (p.next != null) { + p = p.next; + } + + Node target = new Node(); + target.data = o; + target.next = null; + p.next = target; + } + size++; + } + + public void add(int index, Object o) { + // index 是否合法 + checkPositionIndex(index); + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node p = new Node(); + Node p1 = head; + for (int i = 0; i < index - 1; i++) { + p1 = p1.next; + } + p.data = o; + p.next = p1.next; + p1.next = p; + + size++; + } + } + } + + public Object get(int index) { + checkElementIndex(index); + Node p = head; + for (int i = 0; i < index; i++) { + p = p.next; + } + return p.data; + } + + public Object remove(int index) { + checkElementRemove(); + checkElementIndex(index); + + Object removeObject = null; + if (index == 0) { + removeObject = removeFirst(); + } else if (index == (size - 1)) { + removeObject = removeLast(); + } else { + Node p = head; + for (int i = 1; i < index; i++) { + p = p.next; + } + removeObject = p.next.data; + p.next = p.next.next; + size--; + } + return removeObject; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = new Node(); + p.data = o; + p.next = head; + head = p; + } + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + checkElementRemove(); + Object removeObject = head.data; + head = head.next; + size--; + return removeObject; + } + + public Object removeLast() { + checkElementRemove(); + + Object removeObject = null; + + if (size == 1) { + removeObject = head.data; + head = head.next; + } else { + Node p = head; + for (int i = 0; i < size; i++) { + if (p.next.next == null) { + removeObject = p.next; + p.next = null; + break; + } else { + p = p.next; + } + } + } + size--; + return removeObject; + } + + private boolean isEmpty() { + return size == 0; + } + + private void checkElementRemove() { + if (isEmpty()) { + throw new NullPointerException("The list is empty."); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new MyLinkedListIterator(this); + } + + private class MyLinkedListIterator implements Iterator { + private MyLinkedList list = null; + private int position = 0; + + private MyLinkedListIterator(MyLinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + private static class Node { + Object data; + Node next; + } + + @Override + public String toString() { + String elementStr = ""; + Node p = head; + while (p != null) { + elementStr += p.data + ","; + p = p.next; + } + + return "MyLinkedList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } + +} diff --git a/group01/1298552064/src/week01/basic/MyQueue.java b/group01/1298552064/src/week01/basic/MyQueue.java new file mode 100644 index 0000000000..54008652ff --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyQueue.java @@ -0,0 +1,22 @@ +package week01.basic; + +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/basic/MyStack.java b/group01/1298552064/src/week01/basic/MyStack.java new file mode 100644 index 0000000000..aea4d94e24 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyStack.java @@ -0,0 +1,25 @@ +package week01.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(size() - 1); + } + + public Object peek() { + return elementData.get(size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/test/MyArrayListTest.java b/group01/1298552064/src/week01/test/MyArrayListTest.java new file mode 100644 index 0000000000..219035b46f --- /dev/null +++ b/group01/1298552064/src/week01/test/MyArrayListTest.java @@ -0,0 +1,53 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyArrayList; + + + +public class MyArrayListTest { + + private MyArrayList list = null; + + @Before + public void setUp() throws Exception { + list = new MyArrayList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAdd(){ + list.add(4, 10); + Assert.assertEquals("MyArrayList: { size=6, elementData=[1,2,3,4,10,5] }", list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(3), list.get(2)); + } + + @Test + public void testRemove(){ + list.remove(2); + Assert.assertEquals("MyArrayList: { size=4, elementData=[1,2,4,5] }", list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyLinkedListTest.java b/group01/1298552064/src/week01/test/MyLinkedListTest.java new file mode 100644 index 0000000000..b5d6c048d6 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyLinkedListTest.java @@ -0,0 +1,78 @@ +package week01.test; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyLinkedList; + +public class MyLinkedListTest { + + private MyLinkedList list = null; + + @Before + public void setUp() throws Exception { + list = new MyLinkedList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + + @Test + public void testAdd(){ + list.add(3,10); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,10,4,5] }",list.toString()); + } + + @Test + public void testAddFirst(){ + list.addFirst(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[100,1,2,3,4,5] }",list.toString()); + } + + @Test + public void testAddLast(){ + list.addLast(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,4,5,100] }",list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(5), list.get(4)); + } + + @Test + public void testRemove(){ + list.remove(3); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,5] }",list.toString()); + } + + @Test + public void testRemoveFirst(){ + list.removeFirst(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[2,3,4,5] }",list.toString()); + } + + @Test + public void testRemoveLast(){ + list.removeLast(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,4] }",list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } + +} diff --git a/group01/1298552064/src/week01/test/MyQueueTest.java b/group01/1298552064/src/week01/test/MyQueueTest.java new file mode 100644 index 0000000000..f9b7cb63f2 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyQueueTest.java @@ -0,0 +1,48 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyQueue; + +public class MyQueueTest { + + private MyQueue queue = null; + + @Before + public void setUp() throws Exception { + queue = new MyQueue(); + + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void testEnQueue(){ + queue.enQueue(4); + Assert.assertEquals((Object)new Integer(4), queue.size()); + } + + @Test + public void testDeQueue(){ + Assert.assertEquals((Object) new Integer(1), queue.deQueue()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(queue.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(3), queue.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyStackTest.java b/group01/1298552064/src/week01/test/MyStackTest.java new file mode 100644 index 0000000000..4efbc2b204 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyStackTest.java @@ -0,0 +1,53 @@ +package week01.test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyStack; + +public class MyStackTest { + private MyStack stack = null; + + @Before + public void setUp() throws Exception { + stack = new MyStack(); + + stack.push(1); + stack.push(2); + stack.push(3); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void tearPush() throws Exception { + stack.push(10); + Assert.assertEquals((Object) new Integer(4), stack.size()); + Assert.assertEquals((Object) new Integer(10), stack.peek()); + } + + @Test + public void testPop(){ + Assert.assertEquals((Object) new Integer(3), stack.pop()); + Assert.assertEquals((Object) new Integer(2), stack.size()); + } + + @Test + public void testPeek(){ + Assert.assertEquals((Object) new Integer(3), stack.peek()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(stack.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object) new Integer(3), stack.size()); + } +} From 64878674b8fa3ebdc1dc74353121f3d6c8c27b83 Mon Sep 17 00:00:00 2001 From: longcloud Date: Fri, 24 Feb 2017 23:24:00 +0800 Subject: [PATCH 056/432] Commit the ArrayList --- group03/619224754/.classpath | 7 ++ group03/619224754/.gitignore | 1 + group03/619224754/.project | 17 +++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ group03/619224754/src/Main/Main.java | 5 ++ .../src/com/coding/basic/ArrayList.java | 75 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 46 ++++++++++++ .../619224754/src/com/coding/basic/List.java | 9 +++ .../619224754/src/com/coding/basic/Queue.java | 19 +++++ .../619224754/src/com/coding/basic/Stack.java | 22 ++++++ group03/619224754/src/test/ArrayListTest.java | 62 +++++++++++++++ 13 files changed, 313 insertions(+) create mode 100644 group03/619224754/.classpath create mode 100644 group03/619224754/.gitignore create mode 100644 group03/619224754/.project create mode 100644 group03/619224754/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/619224754/src/Main/Main.java create mode 100644 group03/619224754/src/com/coding/basic/ArrayList.java create mode 100644 group03/619224754/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/619224754/src/com/coding/basic/Iterator.java create mode 100644 group03/619224754/src/com/coding/basic/LinkedList.java create mode 100644 group03/619224754/src/com/coding/basic/List.java create mode 100644 group03/619224754/src/com/coding/basic/Queue.java create mode 100644 group03/619224754/src/com/coding/basic/Stack.java create mode 100644 group03/619224754/src/test/ArrayListTest.java diff --git a/group03/619224754/.classpath b/group03/619224754/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group03/619224754/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group03/619224754/.gitignore b/group03/619224754/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/619224754/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/619224754/.project b/group03/619224754/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group03/619224754/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/619224754/.settings/org.eclipse.jdt.core.prefs b/group03/619224754/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/619224754/.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/group03/619224754/src/Main/Main.java b/group03/619224754/src/Main/Main.java new file mode 100644 index 0000000000..2419e8fa45 --- /dev/null +++ b/group03/619224754/src/Main/Main.java @@ -0,0 +1,5 @@ +package Main; + +public class Main { + +} diff --git a/group03/619224754/src/com/coding/basic/ArrayList.java b/group03/619224754/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..05fc412f93 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if(elementData.length == size) { + Object[] arrTaget = new Object[size * 2]; + System.arraycopy(elementData, 0, arrTaget, 0, size); + this.elementData = arrTaget; + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + Object[] arrTarget = new Object[size - index]; + System.arraycopy(elementData, index, arrTarget, 0, size - index); + elementData[index] = o; + System.arraycopy(arrTarget, 0, elementData, index + 1, size - index); + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object retObj = elementData[index]; + + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + else if(index == size) { + elementData[index] = null; + } + else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + + size--; + return retObj; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int cursor = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.cursor != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[this.cursor++]; + } + } + +} diff --git a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/619224754/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/619224754/src/com/coding/basic/Iterator.java b/group03/619224754/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/619224754/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/619224754/src/com/coding/basic/LinkedList.java b/group03/619224754/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/619224754/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/619224754/src/com/coding/basic/List.java b/group03/619224754/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/619224754/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/619224754/src/com/coding/basic/Queue.java b/group03/619224754/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/619224754/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/619224754/src/com/coding/basic/Stack.java b/group03/619224754/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/619224754/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; + } +} diff --git a/group03/619224754/src/test/ArrayListTest.java b/group03/619224754/src/test/ArrayListTest.java new file mode 100644 index 0000000000..9d9e014379 --- /dev/null +++ b/group03/619224754/src/test/ArrayListTest.java @@ -0,0 +1,62 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + + + +public class ArrayListTest { + + @Test + public void testAdd() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 105, array.size()); + } + + @Test + public void testAddIndex() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + array.add(100, 100); + Assert.assertEquals("Shoule be the same", 100, array.get(100)); + Assert.assertEquals("Shoule be the same", 100, array.get(101)); + } + + @Test + public void testRemove() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 100, array.remove(100)); + Assert.assertEquals("Shoule be the same", 104, array.size()); + } + + @Test + public void testIterator() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + Iterator iterator = array.iterator(); + int j = 0; + while(iterator.hasNext()){ + Assert.assertEquals("Shoule be the same", iterator.next(), array.get(j)); + j++; + } + } + +} From dc84c30b8c9a2e636981de7f8dde3d115fb46eaf Mon Sep 17 00:00:00 2001 From: liuqiancheng Date: Sat, 25 Feb 2017 00:27:23 +0800 Subject: [PATCH 057/432] from QQ:976180558 --- group19/976180558/src/test/Test.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 group19/976180558/src/test/Test.java diff --git a/group19/976180558/src/test/Test.java b/group19/976180558/src/test/Test.java new file mode 100644 index 0000000000..054dcf504d --- /dev/null +++ b/group19/976180558/src/test/Test.java @@ -0,0 +1,8 @@ +package test; + +public class Test { + + public static void main(String[] args) { + System.out.println("my QQ is :976180558"); + } +} From 391a7638d3c3da9b33c1da4a24a8fe3f2476145d Mon Sep 17 00:00:00 2001 From: Pxshuo Date: Sat, 25 Feb 2017 02:32:33 +0800 Subject: [PATCH 058/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 初版 --- group06/2415980327/.gitignore | 7 + group06/2415980327/CodeSE01/.gitignore | 1 + .../src/com/pxshuo/basic/Iterator.java | 15 ++ .../CodeSE01/src/com/pxshuo/basic/List.java | 37 ++++ .../src/com/pxshuo/basic/TreeData.java | 20 ++ .../src/com/pxshuo/basic/impl/ArrayList.java | 132 +++++++++++++ .../src/com/pxshuo/basic/impl/BinaryTree.java | 19 ++ .../com/pxshuo/basic/impl/BinaryTreeNode.java | 81 ++++++++ .../src/com/pxshuo/basic/impl/LinkedList.java | 181 ++++++++++++++++++ .../src/com/pxshuo/basic/impl/Queue.java | 44 +++++ .../src/com/pxshuo/basic/impl/Stack.java | 51 +++++ .../com/pxshuo/basic/impl/package-info.java | 14 ++ .../CodeSE01/src/com/pxshuo/test/Test.java | 57 ++++++ .../com/pxshuo/basic/impl/ArrayListTest.java | 15 ++ group06/2415980327/readme.md | 18 ++ 15 files changed, 692 insertions(+) create mode 100644 group06/2415980327/.gitignore create mode 100644 group06/2415980327/CodeSE01/.gitignore create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java create mode 100644 group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java create mode 100644 group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java create mode 100644 group06/2415980327/readme.md diff --git a/group06/2415980327/.gitignore b/group06/2415980327/.gitignore new file mode 100644 index 0000000000..d3b69db2b2 --- /dev/null +++ b/group06/2415980327/.gitignore @@ -0,0 +1,7 @@ +/.metadata/ + +/RemoteSystemsTempFiles/ +/.recommenders/ +.settings/ +.project +.classpath diff --git a/group06/2415980327/CodeSE01/.gitignore b/group06/2415980327/CodeSE01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/2415980327/CodeSE01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java new file mode 100644 index 0000000000..c41df9c249 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java @@ -0,0 +1,15 @@ +package com.pxshuo.basic; + +public interface Iterator { + /** + * 查看是否有下一个元素 + * @return 有的话返回true,否则返回false + */ + public boolean hasNext(); + /** + * 获得下一个元素,也就是当前元素 + * @return 获取当前位置的元素 + */ + public Object next(); + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java new file mode 100644 index 0000000000..615ee9419b --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java @@ -0,0 +1,37 @@ +package com.pxshuo.basic; +/** + * 数组的接口 + * @author Pxshuo + * + */ + +public interface List { + /** + * 向数组中添加一个元素 + * @param o 添加的元素 + */ + public void add(Object o); + /** + * 向数组中某一个位置添加一个元素 + * @param index 添加元素的位置 + * @param o 添加的元素 + */ + public void add(int index,Object o); + /** + * 从数组中根据位置获取一个元素 + * @param index 想要获取的元素的位置 + * @return 想获取的元素 + */ + public Object get(int index); + /** + * 根据位置移除数组中的一个元素 + * @param index 被移除元素的位置 + * @return 被移除的元素 + */ + public Object remove(int index); + /** + * 获取数组的长度 + * @return 返回数组的长度 + */ + public int size(); +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java new file mode 100644 index 0000000000..ef7901606c --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java @@ -0,0 +1,20 @@ +package com.pxshuo.basic; + +public class TreeData implements Comparable { + private int data; + + public TreeData(int data) { + this.data = data; + } + + @Override + public String toString() { + return data + ""; + } + + @Override + public int compareTo(Object o) { + return data - ((TreeData)o).data; + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java new file mode 100644 index 0000000000..7590bca6f9 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java @@ -0,0 +1,132 @@ +package com.pxshuo.basic.impl; + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.List; + +/** + * 实现一个ArrayList + * @author Pxshuo + * + */ + +public class ArrayList implements List{ + + private int size = -1;//数组的长度的下标 + private Object[] elements = new Object[10];//数组内容 + private int addSize = 10;//每次增加的长度 + + @Override + public void add(Object o) { + elements = grow(); + size++; + elements[size] = o;//size与index同一个概念 + } + + @Override + public void add(int index, Object o) { + elements = grow(); + int moveNum = size - index + 1;//本次操作需要移动的元素的个数; + size++; + if (index >= elements.length - 1) {//按照位置来看 + elements = grow(elements, index - (elements.length - 1)); + size = index;//size与index同一个概念 + } + + /** + * 整体向后移一位 + */ + if(moveNum > 0){ + System.arraycopy(elements, index, elements, index + 1, moveNum); + } +// for(int i = size - 1; i >= index; i--) +// { +// elements[i] = elements[i-1]; +// } + + elements[index] = o; + } + + @Override + public Object get(int index) { + return elements[index]; + } + + @Override + public Object remove(int index) { + Object removeEle = elements[index]; + int moveNum = size - index;//本次操作需要移动的元素的个数; + if (moveNum > 0) { + System.arraycopy(elements, index + 1, elements, index, size - index + 1); + } + elements[size] = null; + size--; + return removeEle; + } + + @Override + public int size() { + return size + 1; + } + + /** + * 设置迭代器 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + + ArrayList arrayList = null; + int position = -1; + + public ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + position ++; + if (position >= arrayList.size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return arrayList.elements[position]; + } + + } + + /** + * 自动控制是否增加数组长度 + * @return 如果增加一条数据会造成数组溢出,则增加数组的长度,否则不进行改变。 + */ + private Object[] grow(){ + if (size() >= elements.length) { + return grow(elements, addSize); + } + else { + return elements; + } + + } + + /** + * 动态增加数组长度 + * @param src + * @param addSize + * @return + */ + private Object[] grow(Object[] src,int addSize){ + Object[] target = new Object[src.length + addSize]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + + //return Arrays.copyOf(src, src.length + addSize);同理 + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java new file mode 100644 index 0000000000..8aea75b019 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java @@ -0,0 +1,19 @@ +package com.pxshuo.basic.impl; + +public class BinaryTree { + BinaryTreeNode root = null; + + public void add(Comparable o){ + if (root == null) { + root = new BinaryTreeNode(); + root.setData(o); + } + else { + root.insert(o); + } + } + + public void display(){ + root.display(1); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java new file mode 100644 index 0000000000..2b85095dbe --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java @@ -0,0 +1,81 @@ +package com.pxshuo.basic.impl; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int index = 0; + + public BinaryTreeNode() { + data = null; + 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 void setIndex(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + + public BinaryTreeNode insert(Comparable o){ + if(data == null){ + data = o; + return this; + } + //本节点已经存过数据 + BinaryTreeNode child = new BinaryTreeNode(); + child.setData(o); + if (o.compareTo(data) > 0) { + if (right == null) { + right = child; + } + else { + right.insert(o); + } + } + else {//小于等于的数据放在左子树中 + if (left == null) { + left = child; + } + else { + left.insert(o); + } + } + return child; + } + + public void display(int myIndex){ + + System.out.println(myIndex + ":" + data.toString()); + if (left != null) { + left.display(2 * myIndex); + } + if (right != null) { + right.display(2 * myIndex + 1); + } + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java new file mode 100644 index 0000000000..7711e72a86 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java @@ -0,0 +1,181 @@ +package com.pxshuo.basic.impl; + +import java.time.Period; + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.List; + +public class LinkedList implements List { + + private Node head = new Node(); + private Node tail = null; + private int size = -1;//与index的位置等同 + + //封装空表时候的增加与最后一次的删除-- + + public void add(Object o){ + Node node = new Node(o); + node.next = null; + + if (head.next == null) {//初始化 + head.next = node; + tail = node; + } + else { + tail.next = node; + tail = node; + } + size ++; + } + public void add(int index , Object o){ + if (index > size + 1) { + add(o); + } + else{ + Node prev = head; + Node current = new Node(o); + for(int i=0; i < index; i++) + { + prev = prev.next; + } + current.next = prev.next; + prev.next = current; + size ++; + } + } + public Object get(int index){ + if (index <= size) { + Node node = head; + for(int i = 0; i <= index;i++){ + node = node.next; + } + return node.data; + } + return null; + } + public Object remove(int index){ + Node remove = null; + if (index <= size) { + Node prev = head; + for(int i=0; i < index; i++) + { + prev = prev.next; + } + remove = prev.next; + prev.next = remove.next; + remove.next = null; + + if (index == size) {//设置尾部 + tail = prev; + if (size == 0) { + tail = null; + } + } + size --; + } + return remove != null ? remove.data : null; + } + + public int size(){ + return size + 1; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head.next; + head.next = first; + if(tail == null) + { + tail = first; + } + size ++; + } + public void addLast(Object o){ + if(tail == null){ + add(o); + } + else { + Node last = new Node(o); + last.next = null; + tail.next = last; + tail = last; + size ++; + } + + } + public Object removeFirst(){ + Node first = head.next; + if(first != null) + { + head.next = first.next; + first.next = null; + } + else { + head.next = null; + } + if (head.next == null) {//如果链表为空 + tail = null; + } + size --; + return first!=null ? first.data : null; + } + public Object removeLast(){ + Node last = head; + for(;last.next != tail; last = last.next ){ + + } + tail = last; + last = last.next; + if(tail == head){//最后一个元素 + head.next=null; + tail = null; + }else { + tail.next = null; + } + + size --; + return last != null? last.data : null; + } + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private static class Node{ + Object data; + Node next; + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + private class LinkedListIterator implements Iterator{ + LinkedList linkedList = null; + Node position = new Node(); + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + this.position = head; + } + + @Override + public boolean hasNext() { + position = position.next; + if (position == null) { + return false; + } + return true; + } + + @Override + public Object next() { + return position.data; + } + + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java new file mode 100644 index 0000000000..b042f1d18c --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java @@ -0,0 +1,44 @@ +package com.pxshuo.basic.impl; + +/** + * 基本数据结构-队列 + * @author Pxshuo + * + */ + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + /** + * 入队操作 + * @param o 入队的元素 + */ + public void enQueue(Object o){ + linkedList.addLast(o); + } + + /** + * 出队操作 + * @return 返回出队的元素 + */ + public Object deQueue(){ + return linkedList.removeFirst(); + } + + /** + * 判断队列是否为空 + * @return 为空返回true,否则返回false + */ + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + /** + * 返回队列的长度 + * @return + */ + public int size(){ + return linkedList.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java new file mode 100644 index 0000000000..f3f837e117 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java @@ -0,0 +1,51 @@ +package com.pxshuo.basic.impl; + +/** + * 基本数据结构-栈 + * @author Pxshuo + * + */ + +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 使一个元素入栈 + * @param o 将要入栈的元素 + */ + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + /** + * 使一个元素出栈 + * @return 返回出栈的元素 + */ + public Object pop(){ + return elementData.size() == 0 ? null : elementData.remove(elementData.size() - 1); + } + + /** + * 获得栈顶元素 + * @return 返回栈顶元素 + */ + public Object peek(){ + return elementData.size() == 0 ? null : elementData.get(elementData.size() - 1); + } + + /** + * 查看栈是否为空 + * @return 空的话返回true + */ + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + /** + * 查看栈中元素的个数 + * @return 返回栈中的个数 + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java new file mode 100644 index 0000000000..e87dca3a61 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java @@ -0,0 +1,14 @@ +/** + * 用于实现基本数据类型,包括但不仅限于: + * ArrayList + * Stack + * LinkedList + * Queue + * Tree + * Iterator + */ +/** + * @author Pxshuo + * + */ +package com.pxshuo.basic.impl; \ No newline at end of file diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java new file mode 100644 index 0000000000..39f8dcce2e --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -0,0 +1,57 @@ +package com.pxshuo.test; + + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.TreeData; +import com.pxshuo.basic.impl.ArrayList; +import com.pxshuo.basic.impl.BinaryTree; +import com.pxshuo.basic.impl.LinkedList; +import com.pxshuo.basic.impl.Queue; +import com.pxshuo.basic.impl.Stack; + +public class Test { + public static void main(String[] args) { +// LinkedList arrayList = new LinkedList(); +// arrayList.add("hello1"); +// arrayList.add("hello2"); +// arrayList.add(9,"hello3"); +// //arrayList.add(10,"hello4"); +// arrayList.addLast("hi"); +// arrayList.addLast("hihi"); +// arrayList.addFirst("hi1"); +// arrayList.removeFirst(); +// arrayList.removeLast(); +// arrayList.add(1,"hi1"); +// arrayList.remove(1); +// //arrayList.add(0, "hi"); +// //arrayList.remove(8); +// //arrayList.remove(0); +// for (Iterator iterator = arrayList.iterator(); iterator.hasNext();) { +// System.out.println("hi"+iterator.next()); +// } + //Queue queue = new Queue(); +// Stack stack = new Stack(); +// +// for (int i = 0; i < 10; i++) { +// //queue.enQueue("test-" + i); +// stack.push("test-" + i); +// } +// for(int i =0; i< 11; i++) +// { +// System.out.println(stack.pop()); +// } +// stack.push("test-" + 233); +// System.out.println(stack.pop()); + + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + binaryTree.display(); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java new file mode 100644 index 0000000000..8c49730e47 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java @@ -0,0 +1,15 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.ArrayList; + +public class ArrayListTest { + @Test + public void addTest() { + ArrayList arrayList = new ArrayList(); + arrayList.add("String"); + Assert.assertEquals(arrayList.get(0), "String"); + } +} diff --git a/group06/2415980327/readme.md b/group06/2415980327/readme.md new file mode 100644 index 0000000000..566bee6595 --- /dev/null +++ b/group06/2415980327/readme.md @@ -0,0 +1,18 @@ +# 2415980327 北京-郑朔 + + + +## 一、创建GIt的忽略文档 + + + +windows下在git文件夹中按住shift+鼠标右键,在当前路径下打开命令窗口,输入: + +`type NUL > .gitignore` + +创建忽略配置文件,在其中将 + +> /.metadata/ + +存入即可。 + From b3e643d8bf008a7452078d000c1c50140190dd0f Mon Sep 17 00:00:00 2001 From: sanmubird <751425278@qq.com> Date: Sat, 25 Feb 2017 07:30:33 +0800 Subject: [PATCH 059/432] firstTry --- group01/751425278/.classpath | 6 ++++++ group01/751425278/.gitignore | 1 + group01/751425278/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group01/751425278/.classpath create mode 100644 group01/751425278/.gitignore create mode 100644 group01/751425278/.project diff --git a/group01/751425278/.classpath b/group01/751425278/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group01/751425278/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/751425278/.gitignore b/group01/751425278/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/751425278/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/751425278/.project b/group01/751425278/.project new file mode 100644 index 0000000000..e9e61528e2 --- /dev/null +++ b/group01/751425278/.project @@ -0,0 +1,17 @@ + + + firstTry + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From 29e1c61a6f0b74aeb74f35b33eeaddad21de2a68 Mon Sep 17 00:00:00 2001 From: BigJoyce <1091149131@qq.com> Date: Sat, 25 Feb 2017 09:05:12 +0800 Subject: [PATCH 060/432] =?UTF-8?q?=E4=BD=9C=E4=B8=9A1-=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=8F=8A=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 20170226需要交的作业,作业1-数据结构及文章,文章暂时是个空链接,待完善 --- .../basicstructuredemo/.classpath" | 7 + .../basicstructuredemo/.gitignore" | 1 + .../basicstructuredemo/.project" | 17 ++ .../.settings/org.eclipse.jdt.core.prefs" | 11 ++ .../src/com/maple/basic/ArrayList.java" | 66 ++++++++ .../src/com/maple/basic/BinaryTree.java" | 45 +++++ .../src/com/maple/basic/BinaryTreeNode.java" | 69 ++++++++ .../src/com/maple/basic/Iterator.java" | 7 + .../src/com/maple/basic/LinkedList.java" | 154 ++++++++++++++++++ .../src/com/maple/basic/List.java" | 9 + .../src/com/maple/basic/Queue.java" | 21 +++ .../src/com/maple/basic/Stack.java" | 26 +++ .../src/com/maple/test/TestAPIDemo.java" | 53 ++++++ .../src/com/maple/test/TestAll.java" | 71 ++++++++ .../src/com/maple/test/TestArrayList.java" | 22 +++ .../src/com/maple/test/TestMyDemo.java" | 75 +++++++++ ...\240\351\223\276\346\216\245_20170224.txt" | 5 + 17 files changed, 659 insertions(+) create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" create mode 100644 "group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" @@ -0,0 +1 @@ +/bin/ diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" new file mode 100644 index 0000000000..194f47da7a --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" @@ -0,0 +1,17 @@ + + + basicstructuredemo + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs" new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.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/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" new file mode 100644 index 0000000000..f6d3b4c44a --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" @@ -0,0 +1,66 @@ +package com.maple.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //不够了怎么扩容 + elementData[size++]=o; + } + public void add(int index, Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + for(int i=size;i>index;i--){ + elementData[i-1]=elementData[i]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + Object removeObj=elementData[index]; + for(int i=index;i=size) return false; + return true; + } + }; + } + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" new file mode 100644 index 0000000000..5e63cf4d3c --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" @@ -0,0 +1,45 @@ +package com.maple.basic; + +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; + +public class BinaryTree> { + private BinaryTreeNode root; + + public void traversal(BinaryTreeNode node){ + if(node.getLeft()!=null){ + traversal(node.getLeft()); + } + System.out.println("--"+node.getData()+"--"); + if(node.getRight()!=null){ + traversal(node.getRight()); + } + } + /** + * 如果根节点为null,则作为根节点,否则遍历下去插值 + * @param o + * @return + * 2017年2月23日 下午4:21:51 + * @Author Joy + */ + public BinaryTreeNode insert(T o){ + if(root==null){ + BinaryTreeNode newB=new BinaryTreeNode(); + newB.setData(o); + newB.setLeft(null); + newB.setRight(null); + root=newB; + return root; + } + + return root.insert(o); + } + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" new file mode 100644 index 0000000000..f702d48922 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" @@ -0,0 +1,69 @@ +package com.maple.basic; + +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; + +public class BinaryTreeNode>{ + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + /** + * 如果待插入的值等于节点的值,则抛出异常:duplicate value + * 如果小于节点的值,则往左边遍历 + * 如果大于节点的值,则往右边遍历 + * @param o + * @return + * 2017年2月23日 下午4:22:50 + * @Author Joy + */ + public BinaryTreeNode insert(T o){ + //assume that no duplicate key + + if(o.compareTo(data)==0){ + try { + throw new DuplicateName("duplicate value: "+o); + } catch (DuplicateName e) { + e.printStackTrace(); + } + } + BinaryTreeNode newB=new BinaryTreeNode(); + newB.setData(o); + newB.setLeft(null); + newB.setRight(null); + //o更大,在右边 + if(o.compareTo(data)>0){ + if(this.getRight()!=null){ + this.getRight().insert(o); + }else{ + this.setRight(newB); + } + }else if(o.compareTo(data)<0){ + if(this.getLeft()!=null){ + this.getLeft().insert(o); + }else{ + this.setLeft(newB); + } + } + return newB; + } + + 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/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" new file mode 100644 index 0000000000..ac8ecd6050 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" @@ -0,0 +1,7 @@ +package com.maple.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" new file mode 100644 index 0000000000..d957f74bdc --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" @@ -0,0 +1,154 @@ +package com.maple.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size = 0;//自己加的,觉得需要 + /** + * 与addList()是一样的 + */ + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + Node newNode=new Node(); + newNode.data=o; + + newNode.next=curNode; + prevNode.next=newNode; + size++; + break; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + + + } + public Object get(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + return curNode.data; + } + curNode=curNode.next; + count++; + } + return null; + } + public Object remove(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + prevNode.next=curNode.next; + size--; + return curNode.data; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + objNode.next=head.next; + size++; + head.next=objNode; + } + public void addLast(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + + //也可以用iterator迭代,先不用吧 + Node curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + objNode.next=curNode.next; + curNode.next=objNode; + size++; + + } + public Object removeFirst(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node delNode=head.next; + head.next=delNode.next; + size--; + return delNode.data; + } + public Object removeLast(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node prevNode=head; + Node curNode=head.next; + while(curNode!=null){ + if(curNode.next==null){//说明是尾节点 + prevNode.next=curNode.next; + size--; + return curNode.data; + } + curNode=curNode.next; + prevNode=prevNode.next; + } + return null; + } + public Iterator iterator(){ + return new Iterator() { + private Node cur=head!=null?head.next:head; + @Override + public Object next() { + if(cur==null){ + throw new NoSuchElementException(); + } + Object object=cur.data; + cur=cur.next; + return object; + } + + @Override + public boolean hasNext() { + if(cur==null){ + return false; + }else{ + return true; + } + + } + }; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" new file mode 100644 index 0000000000..99bed9d96b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" @@ -0,0 +1,9 @@ +package com.maple.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/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" new file mode 100644 index 0000000000..278d3dba7f --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" @@ -0,0 +1,21 @@ +package com.maple.basic; + +public class Queue { + private LinkedList elementData=new LinkedList(); + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + Object first=elementData.removeFirst(); + return first; + } + + public boolean isEmpty(){ + return elementData.size()<=0; + } + + public int size(){ + return elementData.size(); + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" new file mode 100644 index 0000000000..cec4599237 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" @@ -0,0 +1,26 @@ +package com.maple.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object object=elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return object; + } + + public Object peek(){ + Object object=elementData.get(elementData.size()-1); + return object; + } + public boolean isEmpty(){ + return elementData.size()<=0; + } + public int size(){ + return elementData.size(); + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" new file mode 100644 index 0000000000..ee592fbb7b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" @@ -0,0 +1,53 @@ +package com.maple.test; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + + +/** + * 测试Java中的API + * @author group 14, QQ:1091149131 + */ +public class TestAPIDemo { + public static void main(String[] args) { + + //Stack + /*Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek());*/ + + //LinkedList + /*LinkedList list2=new LinkedList<>(); + list2.add(0); + list2.add(1); + list2.add(2); + + list2.addLast(3); + list2.remove(0); + //list2.removeFirst(); + Iterator ite2=list2.iterator(); + ite2.next(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + }*/ + + + //ArrayList + /*ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + }*/ + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" new file mode 100644 index 0000000000..9fd59e512e --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" @@ -0,0 +1,71 @@ +package com.maple.test; + +import org.junit.Test; + +import com.maple.basic.ArrayList; +import com.maple.basic.BinaryTree; +import com.maple.basic.Iterator; +import com.maple.basic.LinkedList; +import com.maple.basic.Queue; +import com.maple.basic.Stack; + +public class TestAll { + @Test + public void testArrayList(){ + ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + } + } + @Test + public void testLinkedList(){ + LinkedList list2=new LinkedList(); + list2.add(0); + list2.add(1); + list2.addFirst(-1); + list2.addLast(-2); + + list2.removeFirst(); + list2.removeLast(); + list2.remove(0); + + Iterator ite2=list2.iterator(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + } + } + @Test + public void testStack(){ + Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek()); + } + @Test + public void testQueue(){ + Queue queue=new Queue(); + queue.enQueue(0); + queue.enQueue(1); + + System.out.println(queue.deQueue()); + } + @Test + public void testBinaryTree(){ + BinaryTree tree=new BinaryTree<>(); + tree.insert(3); + tree.insert(2); + tree.insert(5); + //tree.insert(5);//error,duplicate + tree.insert(1); + tree.traversal(tree.getRoot()); + } + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" new file mode 100644 index 0000000000..94ecec52f7 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" @@ -0,0 +1,22 @@ +package com.maple.test; + +import org.junit.Test; + +import com.maple.basic.ArrayList; +import com.maple.basic.Iterator; + +public class TestArrayList{ + + @Test + public void testAdd(){ + ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + } + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" new file mode 100644 index 0000000000..639aaa629b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" @@ -0,0 +1,75 @@ +package com.maple.test; + +import com.maple.basic.ArrayList; +import com.maple.basic.BinaryTree; +import com.maple.basic.BinaryTreeNode; +import com.maple.basic.Iterator; +import com.maple.basic.LinkedList; +import com.maple.basic.Queue; +import com.maple.basic.Stack; + +/** + * 测试自己写的数据结构 + * @author group 14, QQ:1091149131 + */ +public class TestMyDemo { + public static void main(String[] args) { + //BinaryTree + BinaryTree tree=new BinaryTree<>(); + tree.insert(3); + tree.insert(2); + tree.insert(5); + //tree.insert(5);//error,duplicate + tree.insert(1); + tree.traversal(tree.getRoot()); + + + //Queue + /*Queue queue=new Queue(); + queue.enQueue(0); + queue.enQueue(1); + + System.out.println(queue.deQueue());*/ + + //Stack + /*Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek());*/ + + //LinkedList + /*LinkedList list2=new LinkedList(); + list2.add(0); + list2.add(1); + list2.addFirst(-1); + list2.addLast(-2); + + list2.removeFirst(); + list2.removeLast(); + list2.remove(0); + + Iterator ite2=list2.iterator(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + }*/ + + + //ArrayList + /*ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + }*/ + + + + + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" new file mode 100644 index 0000000000..1ecad55f85 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" @@ -0,0 +1,5 @@ +����CPU���ڴ棬 Ӳ�̣�ָ��֮��Ĺ�ϵ�� + +�������ӣ�http://www.cnblogs.com/qingmaple/p/6437070.html + +QQ��1091149131 ����-Ҷ�� \ No newline at end of file From 62657a1b30871895d11f8343745ddbc0509f56ae Mon Sep 17 00:00:00 2001 From: lvxg <819048836@qq.com> Date: Sat, 25 Feb 2017 09:43:20 +0800 Subject: [PATCH 061/432] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 数据结构 --- .../lvxg2017/src/basic/BinaryTree.java | 41 +++++ .../lvxg2017/src/basic/BinaryTreeNode.java | 34 ++++ .../lvxg2017/src/basic/MyArrayList.java | 91 +++++++++++ .../lvxg2017/src/basic/MyLinkedList.java | 146 ++++++++++++++++++ .../819048836/lvxg2017/src/basic/Queue.java | 44 ++++++ .../819048836/lvxg2017/src/basic/Stack.java | 61 ++++++++ 6 files changed, 417 insertions(+) create mode 100644 group01/819048836/lvxg2017/src/basic/BinaryTree.java create mode 100644 group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java create mode 100644 group01/819048836/lvxg2017/src/basic/MyArrayList.java create mode 100644 group01/819048836/lvxg2017/src/basic/MyLinkedList.java create mode 100644 group01/819048836/lvxg2017/src/basic/Queue.java create mode 100644 group01/819048836/lvxg2017/src/basic/Stack.java diff --git a/group01/819048836/lvxg2017/src/basic/BinaryTree.java b/group01/819048836/lvxg2017/src/basic/BinaryTree.java new file mode 100644 index 0000000000..748a8f477e --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/BinaryTree.java @@ -0,0 +1,41 @@ +package basic; + +public class BinaryTree { + private BinaryTreeNode root;//���ڵ� + + //������� + public void insert(int value){ + BinaryTreeNode newNode = new BinaryTreeNode(value); + if(root==null){ + root = newNode; + root.setLeft(null); + root.setRight(null); + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true) + { + parentNode = currentNode; + //���ҷ� + if(newNode.getData()>currentNode.getData()){ + currentNode = currentNode.getRight(); + if(currentNode ==null){ + parentNode.setRight(newNode); + return; + } + }else if(newNode.getData()<=currentNode.getData()){ + currentNode = currentNode.getLeft(); + if(currentNode ==null){ + parentNode.setLeft(newNode); + return; + } + } + + } + + } + + + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java b/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e8e7bb7b63 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package basic; + +//������ +public class BinaryTreeNode { + private int data;// �ڵ�ֵ + private BinaryTreeNode left; //���ӽڵ� + private BinaryTreeNode right;//���ӽڵ� + public BinaryTreeNode(int data){ + this.data =data; + this.left = null; + this.right = null; + } + public void setDate(int data){ + this.data =data; + } + public int getData(){ + return data; + } + public BinaryTreeNode getLeft(){ + return left; + } + public BinaryTreeNode getRight(){ + return right; + } + public void setLeft(BinaryTreeNode left){ + this.left = left; + } + public void setRight(BinaryTreeNode right){ + this.right =right; + } + + + +} diff --git a/group01/819048836/lvxg2017/src/basic/MyArrayList.java b/group01/819048836/lvxg2017/src/basic/MyArrayList.java new file mode 100644 index 0000000000..ff74830b08 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/MyArrayList.java @@ -0,0 +1,91 @@ +package basic; +/** + * arraylist��Linkedlist,ջ�����У���������������ʵ�֣�һƪ���¹��ڼ�������ԭ�� ����CPU���ڴ棬 Ӳ�̣�ָ��֮��Ĺ�ϵ + * + * @author lvxg + * + */ +public class MyArrayList { + private Object[] element; + private int size; + private static final Object[] EMPTY = new Object[10]; + + public MyArrayList() { + this.element = EMPTY; + } + + public boolean add(Object o) { + // �������û�� + if (size < element.length) { + element[size] = o; + size++; + } else { + // �������� + grow(); + element[size] = o; + size++; + } + return true; + } + + // ���������������� + public boolean add(int index, Object o) { + rangeCheckForAdd(index); + if (size < element.length + 1) { + Object[] e = new Object[element.length+1]; + System.arraycopy(element, 0, e, 0, index); + e[index] = o; + System.arraycopy(element, index, e, index + 1, element.length-index); + size++; + } + return true; + } + + public Object get(int index) { + rangeCheck(index); + return element[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object oldValue = element[index]; + int numMoved = size - index-1; + if(numMoved>0){ + System.arraycopy(element, index+1, element, index, numMoved); + } + element[--size] =null; + return oldValue; + } + public int size() { + return size; + } + private void rangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + private void rangeCheckForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + // �������ݷ��� + private void grow() { + Object[] e = new Object[size * 2]; + // ���鿽�� + System.arraycopy(element, 0, e, 0, element.length); + element = e; + + } + + public static void main(String[] args) { + MyArrayList m = new MyArrayList(); + m.add("1"); + for (int i = 0; i < 10; i++) { + m.add(i); + } + m.add(2, "123"); + m.remove(1); + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/MyLinkedList.java b/group01/819048836/lvxg2017/src/basic/MyLinkedList.java new file mode 100644 index 0000000000..884f3b8a0f --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/MyLinkedList.java @@ -0,0 +1,146 @@ +package basic; + +import java.util.LinkedList; +import java.util.Queue; + +/** + * ���� + * + * @author lvxg + * + */ +public class MyLinkedList { + // ͷ�ڵ� + private Node first; + // β�ڵ� + private Node last; + // Ԫ�ظ��� + private int size = 0; + + public boolean add(Object o) { + linklast(o); + return true; + } + /** + * ������������ڵ� + * @param index + * @param o + */ + public void add(int index, Object o) { + // �ж�index�Ƿ�Խ�� + checkPositionIndex(index); + if (index == size) { + linklast(o); + } else { + final Node succ = node(index); + final Node pred = succ.prev; + Node newNode = new Node(o, pred, succ); + // ���ǰ��Ϊ��˵����ͷ�ڵ� + if (pred == null) { + first = newNode; + } else { + pred.next = newNode; + } + size++; + } + } + //��������ɾ���ڵ� + @SuppressWarnings("unused") + private Object remove(int index){ + Object x= unllink(node(index)); + return x; + } + //ɾ���ڵ㣬�޲� + @SuppressWarnings("unused") + private Object remove(){ + Object x = unllink(node(size)); + return x; + } + + // ���ӽڵ㵽����ĩβ + private void linklast(Object e) { + final Node l = last; + final Node newNode = new Node(e, null, l); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + // ɾ���ڵ㷽�� + private Object unllink(Node x) { + final Object element = x.data; + final Node next = x.next; + final Node prev = x.prev; + // ǰ��Ϊ�գ���ʾɾ������ͷ�ڵ� + if (prev == null) { + first = next; + } else { + prev.next = next; + x.next = null; + x.prev = null; + } + // ���Ϊ�գ���ʾɾ������β�ڵ� + if (next == null) { + last = prev; + x.next = null; + } + size--; + x.data = null; + return element; + } + + // ���������õ��ڵ� + private Node node(int index) { + if (index < (size >> 1)) { // �������λ����ǰ��Σ����� + Node x = first; + 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.prev; + } + return x; + } + } + + private static class Node { + Object data; // ������ + Node next;// ��� + Node prev;// ǰ�� + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException(); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public static void main(String[] args) { + MyLinkedList l = new MyLinkedList(); + l.add("1"); + l.add("2"); + l.add("3"); + l.add(2, "4"); + l.remove(); + l.remove(1); + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/Queue.java b/group01/819048836/lvxg2017/src/basic/Queue.java new file mode 100644 index 0000000000..c9331a1992 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/Queue.java @@ -0,0 +1,44 @@ +package basic; + +public class Queue { + // ͷ�ڵ� + private Node first; + // β�ڵ� + private Node last; + // Ԫ�ظ��� + private int size = 0; + + // ���� + private void enQueue(Object o) { + final Node f = first; + final Node newNode = new Node(o, f, null); + f.prev = newNode; + } + public int size(){ + return size; + } +public boolean isEmpty(){ + return size>=0; +} + // ���� + private Object deQueue() { + final Node l = last; + final Node p = l.prev; + last = p; + p.next = null; + return l; + } + + private static class Node { + Object data; // ������ + Node next;// ��� + Node prev;// ǰ�� + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/Stack.java b/group01/819048836/lvxg2017/src/basic/Stack.java new file mode 100644 index 0000000000..2ff11133e9 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/Stack.java @@ -0,0 +1,61 @@ +package basic; + +//ջ +public class Stack { + // ͷ�ڵ� + private Node first; + // β�ڵ� + private Node last; + // Ԫ�ظ��� + private int size = 0; + + // ��ջ + private void pop() { + removeLast(); + } + + // ��ջ + private void push(Object o) { + addLast(o); + } + private boolean isEmpty(){ + if(size==0){ + return true; + }else{ + return false; + } + } + private int size(){ + return size; + } + private void removeLast(){ + final Node f = last.prev; + last = f; + f.next =null; + } + + + private void addLast(Object o){ + final Node f= first; + final Node l = last; + final Node newNode = new Node(o, last, null); + if(f==null){ + first =newNode; + }else{ + l.next = newNode; + } + size++; + } + + private static class Node { + Object data; // ������ + Node next;// ��� + Node prev;// ǰ�� + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } +} From 250c46efb36079a16d68b398194d209ec5ba5dcf Mon Sep 17 00:00:00 2001 From: thomas_young Date: Sat, 25 Feb 2017 10:02:24 +0800 Subject: [PATCH 062/432] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group02/812350401/.gitignore | 1 + .../coding2017/basic/BinaryTreeNode.java | 49 ++++++++++++++----- .../basic/test/BinaryTreeNodeTest.java | 16 +++--- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore index e6416c5be6..c24866af6d 100644 --- a/group02/812350401/.gitignore +++ b/group02/812350401/.gitignore @@ -1,2 +1,3 @@ /bin/ /lib/ +/src/java_training diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java index 2b47a098ff..5b1f17342b 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -1,32 +1,55 @@ package com.github.miniyk2012.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode > { + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { + public BinaryTreeNode(E x) { + data = x; + } + public E getData() { return data; } - public void setData(Object data) { + public void setData(E data) { this.data = data; } - public BinaryTreeNode getLeft() { + public BinaryTreeNode getLeft() { return left; } - public void setLeft(BinaryTreeNode left) { + public void setLeft(BinaryTreeNode left) { this.left = left; } - public BinaryTreeNode getRight() { + public BinaryTreeNode getRight() { return right; } - public void setRight(BinaryTreeNode right) { + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; + public BinaryTreeNode insert(E o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode currentNode = this; + BinaryTreeNode previousNode = null; + + while (currentNode != null) { + previousNode = currentNode; + int compareTo = node.getData().compareTo(currentNode.getData()); + if (compareTo <= 0) { // 小于,往左插入 + currentNode = currentNode.left; + left = true; + } else { + currentNode = currentNode.right; + left = false; + } + } + if (left) { + previousNode.left = node; + } else { + previousNode.right = node; + } + return node; } } diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java index 99798b3222..36f5dff484 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -8,26 +8,24 @@ public class BinaryTreeNodeTest { - private BinaryTreeNode binaryTreeNode; + private BinaryTreeNode binaryTreeNode; @Before public void setUpBinaryTreeNode() { - binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode = new BinaryTreeNode(4); } @Test public void testBinaryTreeNodeFunctional() { - binaryTreeNode.insert(4); binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); - assertEquals(4, binaryTreeNode.getData()); - assertEquals(1, binaryTreeNode.getLeft().getData()); - assertEquals(5, binaryTreeNode.getRight().getData()); - assertEquals(3, binaryTreeNode.getLeft().getRight().getData()); - assertEquals(2, binaryTreeNode.getLeft().getRight().getLeft().getData()); - // binaryTreeNode.print(); + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); } } From 41835f501ba709a88ae1c4bd9343cac3454278b9 Mon Sep 17 00:00:00 2001 From: liuqiancheng Date: Sat, 25 Feb 2017 10:22:39 +0800 Subject: [PATCH 063/432] second test --- group19/976180558/.gitignore | 1 + .../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 +++++++++++++++++++ .../976180558/src/com/coding/basic/List.java | 9 ++++ .../976180558/src/com/coding/basic/Queue.java | 19 ++++++++ group19/976180558/src/test/Test.java | 8 ---- 8 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 group19/976180558/.gitignore create mode 100644 group19/976180558/src/com/coding/basic/ArrayList.java create mode 100644 group19/976180558/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group19/976180558/src/com/coding/basic/Iterator.java create mode 100644 group19/976180558/src/com/coding/basic/LinkedList.java create mode 100644 group19/976180558/src/com/coding/basic/List.java create mode 100644 group19/976180558/src/com/coding/basic/Queue.java delete mode 100644 group19/976180558/src/test/Test.java diff --git a/group19/976180558/.gitignore b/group19/976180558/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group19/976180558/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group19/976180558/src/com/coding/basic/ArrayList.java b/group19/976180558/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..57412dcf7f --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/BinaryTreeNode.java b/group19/976180558/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/Iterator.java b/group19/976180558/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/LinkedList.java b/group19/976180558/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..1fd99bf26b --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/List.java b/group19/976180558/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/Queue.java b/group19/976180558/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..08d2d86b14 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/test/Test.java b/group19/976180558/src/test/Test.java deleted file mode 100644 index 054dcf504d..0000000000 --- a/group19/976180558/src/test/Test.java +++ /dev/null @@ -1,8 +0,0 @@ -package test; - -public class Test { - - public static void main(String[] args) { - System.out.println("my QQ is :976180558"); - } -} From 06b44351b2214420454129f01ef5e3ffa8918b98 Mon Sep 17 00:00:00 2001 From: liuqiancheng Date: Sat, 25 Feb 2017 10:24:35 +0800 Subject: [PATCH 064/432] third test --- .../976180558/src/com/coding/basic/Stack.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 group19/976180558/src/com/coding/basic/Stack.java diff --git a/group19/976180558/src/com/coding/basic/Stack.java b/group19/976180558/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4bfe28057f --- /dev/null +++ b/group19/976180558/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 7f333cb526c178d6120f855b5dc050e2d8ca8bbc Mon Sep 17 00:00:00 2001 From: qilei Date: Sat, 25 Feb 2017 11:14:40 +0800 Subject: [PATCH 065/432] ArrayList completed --- group04/916758663/learn01/.gitignore | 21 +++++++ group04/916758663/learn01/learn01.iml | 17 ++++++ group04/916758663/learn01/pom.xml | 32 ++++++++++ .../main/java/com/coding/basic/ArrayList.java | 56 ++++++++++++++++++ .../java/com/coding/basic/BinaryTreeNode.java | 32 ++++++++++ .../main/java/com/coding/basic/Iterator.java | 7 +++ .../java/com/coding/basic/LinkedList.java | 46 +++++++++++++++ .../src/main/java/com/coding/basic/List.java | 9 +++ .../src/main/java/com/coding/basic/Queue.java | 19 ++++++ .../src/main/java/com/coding/basic/Stack.java | 22 +++++++ .../java/com/coding/basic/ArrayListTest.java | 59 +++++++++++++++++++ .../com/coding/basic/JavaArrayListTest.java | 21 +++++++ 12 files changed, 341 insertions(+) create mode 100644 group04/916758663/learn01/.gitignore create mode 100644 group04/916758663/learn01/learn01.iml create mode 100644 group04/916758663/learn01/pom.xml create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/Iterator.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/List.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java create mode 100644 group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java create mode 100644 group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java create mode 100644 group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java diff --git a/group04/916758663/learn01/.gitignore b/group04/916758663/learn01/.gitignore new file mode 100644 index 0000000000..45026a9caf --- /dev/null +++ b/group04/916758663/learn01/.gitignore @@ -0,0 +1,21 @@ +# Eclipse project files +.classpath +.project +.settings/ + + +# Intellij project files +*.iml +.idea/ +*.iws + +# maven +target/ +logs/ +.DS_Store + +# Mac +.DS_Store + +# +*.log diff --git a/group04/916758663/learn01/learn01.iml b/group04/916758663/learn01/learn01.iml new file mode 100644 index 0000000000..6e0c17c683 --- /dev/null +++ b/group04/916758663/learn01/learn01.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group04/916758663/learn01/pom.xml b/group04/916758663/learn01/pom.xml new file mode 100644 index 0000000000..e40157b3bf --- /dev/null +++ b/group04/916758663/learn01/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coding + basic + 1.0-SNAPSHOT + jar + + basic + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + + org.assertj + assertj-core + 3.6.2 + test + + + diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8fb9efb251 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[3]; + + public void add(Object o){ + add(size,o); + } + public void add(int index, Object o){ + if (index > size){ + throw new IndexOutOfBoundsException(); + } + // 扩容 + if (size == elementData.length || index + 1 > elementData.length) { + int newLength = index + 1 > size * 2 ? index + 1 :size * 2; + elementData = Arrays.copyOf(elementData, newLength); + } + // 移动元素 + 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-1 - index); + size --; + return removed; + } + + private void checkIndex(int index) { + if (index > size-1){ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/BinaryTreeNode.java b/group04/916758663/learn01/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/Iterator.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/List.java b/group04/916758663/learn01/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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; + } +} diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..e82f0178ef --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/24. + */ +public class ArrayListTest { + + @Test + public void add() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + l.add(4); + assertThat(l.size()).isEqualTo(4); + } + + @Test + public void insert() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + l.add(1,4); + assertThat(l.size()).isEqualTo(4); + assertThat(l.get(1)).isEqualTo(4); + } + + @Test + public void remove() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + Object removed = l.remove(1); + assertThat(l.size()).isEqualTo(2); + assertThat(removed).isEqualTo(2); + } + + @Test + public void get() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java new file mode 100644 index 0000000000..348f7e9a66 --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class JavaArrayListTest { + + @Test + public void add() throws Exception { + java.util.ArrayList l = new java.util.ArrayList(); + l.add(1); + l.add(2); + l.add(3,3); + assertThat(l.size()).isEqualTo(3); + } + +} From 7ce083216f67174bcab0eed52964401a57d29a4e Mon Sep 17 00:00:00 2001 From: zg Date: Sat, 25 Feb 2017 11:28:46 +0800 Subject: [PATCH 066/432] first work --- group14/254659936/.gitignore | 10 ++ group14/254659936/src/Main.java | 7 + .../src/com/coding/basic/ArrayList.java | 73 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 69 ++++++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 155 ++++++++++++++++++ .../254659936/src/com/coding/basic/List.java | 13 ++ .../254659936/src/com/coding/basic/Queue.java | 45 +++++ .../254659936/src/com/coding/basic/Stack.java | 50 ++++++ 9 files changed, 430 insertions(+) create mode 100644 group14/254659936/.gitignore create mode 100644 group14/254659936/src/Main.java create mode 100644 group14/254659936/src/com/coding/basic/ArrayList.java create mode 100644 group14/254659936/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/254659936/src/com/coding/basic/Iterator.java create mode 100644 group14/254659936/src/com/coding/basic/LinkedList.java create mode 100644 group14/254659936/src/com/coding/basic/List.java create mode 100644 group14/254659936/src/com/coding/basic/Queue.java create mode 100644 group14/254659936/src/com/coding/basic/Stack.java diff --git a/group14/254659936/.gitignore b/group14/254659936/.gitignore new file mode 100644 index 0000000000..5561991601 --- /dev/null +++ b/group14/254659936/.gitignore @@ -0,0 +1,10 @@ +*.iml +.gradle +.idea +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild \ No newline at end of file diff --git a/group14/254659936/src/Main.java b/group14/254659936/src/Main.java new file mode 100644 index 0000000000..4d4fbb71ef --- /dev/null +++ b/group14/254659936/src/Main.java @@ -0,0 +1,7 @@ + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/group14/254659936/src/com/coding/basic/ArrayList.java b/group14/254659936/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7fb9c076b6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.Objects; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + if (size == elementData.length) { + Object[] newArr = new Object[elementData.length * 2]; + System.arraycopy(newArr, 0, elementData, 0, elementData.length); + elementData = newArr; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + elementData[index] = o; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + Object resultObj = elementData[index]; + size--; + for (int i = index; i < size; i++) { + elementData[index] = elementData[index + 1]; + } + return resultObj; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private class ArrayIterator implements Iterator { + + private int iteratorIndex = 0; + + @Override + public boolean hasNext() { + return iteratorIndex < size; + } + + @Override + public Object next() { + if (iteratorIndex >= size) { + throw new RuntimeException("the index is out of the list"); + } + return elementData[iteratorIndex++]; + } + } + +} diff --git a/group14/254659936/src/com/coding/basic/BinaryTreeNode.java b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..8b89c37114 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + 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; + } + + public BinaryTreeNode insert(T o) { + BinaryTreeNode node = new BinaryTreeNode(); + node.setData(o); + insert(this, node); + return node; + } + + private void insert(BinaryTreeNode parent, BinaryTreeNode child) { + BinaryTreeNode node; + if (child.getData().isLargeThanTarget(parent.getData())) { + // 子节点比父节点大,需要向右插入 + node = getRight(); + if (null == node) { + // 右节点为空则可以直接插入 + parent.setRight(node); + } else { + // 递归检查右边子树的插入位置 + insert(node, child); + } + } else { + // 子节点比父节点小,或者等于父节点,需要向左插入 + node = getLeft(); + if (null == node) { + // 左节点为空,则直接插入 + parent.setLeft(node); + } else { + // 递归检查左子树的插入位置 + insert(node, child); + } + } + } + + public interface Compare { + boolean isLargeThanTarget(Compare target); + } + +} diff --git a/group14/254659936/src/com/coding/basic/Iterator.java b/group14/254659936/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..160f044ad1 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group14/254659936/src/com/coding/basic/LinkedList.java b/group14/254659936/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e0ad67a6d8 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/LinkedList.java @@ -0,0 +1,155 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node node = new Node(); + node.data = 0; + if (index == 0) { + node.next = head; + head = node; + } + Node next = head; + int i = 0; + while (null != next) { + i++; + if (index == i) { + node.next = next.next; + next.next = node; + break; + } + next.next = next.next; + } + size++; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + return next; + } + next = next.next; + i++; + } + return null; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + size--; + Node resultNode = null; + if (index == 0) { + resultNode = head; + head = head.next; + return resultNode.data; + } + + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + resultNode = next.next; + next.next = resultNode.next; + } + } + return resultNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + size++; + Node node = new Node(); + node.data = 0; + node.next = head; + head = node; + } + + public void addLast(Object o) { + size++; + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + return; + } + Node next = head; + while (null != next.next) { + next = next.next; + } + next.next = node; + } + + public Object removeFirst() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + size--; + Object obj = head.data; + head = head.next; + return obj; + } + + public Object removeLast() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + Node next = head; + Object obj = null; + for (int i = 0; i < size - 1; i++) { + next = next.next; + } + next.next = null; + size--; + return obj; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + } + + private class LinkedListIterator implements Iterator { + + private Node next = head; + + @Override + public boolean hasNext() { + return null != next; + } + + @Override + public Object next() { + if (null == next) { + throw new RuntimeException("the LinkedList is out of index"); + } + Object obj = next.data; + next = next.next; + return obj; + } + } +} diff --git a/group14/254659936/src/com/coding/basic/List.java b/group14/254659936/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +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/group14/254659936/src/com/coding/basic/Queue.java b/group14/254659936/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..50f99e6d1c --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Queue.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Queue { + + private Node head; + private Node tail; + private int size; + + public void enQueue(Object o) { + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + tail = node; + } else { + tail.next = node; + tail = tail.next; + } + size++; + } + + public Object deQueue() { + if (size <= 0) { + throw new RuntimeException("the queue is empty"); + } + Object obj = head.data; + head = head.next; + size--; + return obj; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} diff --git a/group14/254659936/src/com/coding/basic/Stack.java b/group14/254659936/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..796a3e2e24 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack { + + private Node mStackNode; + private int size; + + public void push(Object o) { + Node node = new Node(); + node.data = o; + if (null == mStackNode) { + mStackNode = node; + } else { + mStackNode.next = node; + mStackNode = node; + } + size++; + } + + public Object pop() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + Object obj = mStackNode.data; + mStackNode = mStackNode.pre; + size--; + return obj; + } + + public Object peek() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + return mStackNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} From 3ba0f68994bd531f5f189afb26cb917dc23149af Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 11:43:25 +0800 Subject: [PATCH 067/432] A New Branch --- group03/617187912/Learning201702/.classpath | 6 +++ group03/617187912/Learning201702/.gitignore | 1 + group03/617187912/Learning201702/.project | 17 +++++++ .../org.eclipse.core.resources.prefs | 2 + .../src/com/coding/basic/ArrayList.java | 47 +++++++++++++++++++ .../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 +++++++++ 11 files changed, 208 insertions(+) create mode 100644 group03/617187912/Learning201702/.classpath create mode 100644 group03/617187912/Learning201702/.gitignore create mode 100644 group03/617187912/Learning201702/.project create mode 100644 group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Iterator.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/List.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Queue.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Stack.java diff --git a/group03/617187912/Learning201702/.classpath b/group03/617187912/Learning201702/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group03/617187912/Learning201702/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning201702/.gitignore b/group03/617187912/Learning201702/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning201702/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning201702/.project b/group03/617187912/Learning201702/.project new file mode 100644 index 0000000000..b3dfe82232 --- /dev/null +++ b/group03/617187912/Learning201702/.project @@ -0,0 +1,17 @@ + + + 617187912Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..517f614f71 --- /dev/null +++ b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java @@ -0,0 +1,47 @@ +package com.coding.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){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size] = o; + size+=1; + } + public void add(int index, Object o){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + System.arraycopy(elementData, index, + elementData, index+1, size-index); + elementData[index]=o; + size+=1; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + System.arraycopy(elementData, index+1, + elementData, index, size-index); + size-=1; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} \ No newline at end of file diff --git a/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/617187912/Learning201702/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/617187912/Learning201702/src/com/coding/basic/Iterator.java b/group03/617187912/Learning201702/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/617187912/Learning201702/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/617187912/Learning201702/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/617187912/Learning201702/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/617187912/Learning201702/src/com/coding/basic/List.java b/group03/617187912/Learning201702/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/617187912/Learning201702/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/617187912/Learning201702/src/com/coding/basic/Queue.java b/group03/617187912/Learning201702/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/617187912/Learning201702/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/617187912/Learning201702/src/com/coding/basic/Stack.java b/group03/617187912/Learning201702/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/617187912/Learning201702/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 dc287772c5c1c3d9b44ad994172ec33522196b49 Mon Sep 17 00:00:00 2001 From: 875867418 Date: Sat, 25 Feb 2017 12:06:42 +0800 Subject: [PATCH 068/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group10/875867419/.classpath | 6 + group10/875867419/.gitignore | 1 + group10/875867419/.project | 17 ++ .../src/com/work/week01/MyArrayList.java | 203 ++++++++++++++++++ .../src/com/work/week01/MyBinaryTree.java | 82 +++++++ .../src/com/work/week01/MyIterator.java | 6 + .../src/com/work/week01/MyLinkedList.java | 169 +++++++++++++++ .../875867419/src/com/work/week01/MyList.java | 11 + .../src/com/work/week01/MyQueue.java | 38 ++++ .../src/com/work/week01/MyStack.java | 43 ++++ ...4\344\270\232\345\234\260\345\235\200.txt" | 1 + 11 files changed, 577 insertions(+) create mode 100644 group10/875867419/.classpath create mode 100644 group10/875867419/.gitignore create mode 100644 group10/875867419/.project create mode 100644 group10/875867419/src/com/work/week01/MyArrayList.java create mode 100644 group10/875867419/src/com/work/week01/MyBinaryTree.java create mode 100644 group10/875867419/src/com/work/week01/MyIterator.java create mode 100644 group10/875867419/src/com/work/week01/MyLinkedList.java create mode 100644 group10/875867419/src/com/work/week01/MyList.java create mode 100644 group10/875867419/src/com/work/week01/MyQueue.java create mode 100644 group10/875867419/src/com/work/week01/MyStack.java create mode 100644 "group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" diff --git a/group10/875867419/.classpath b/group10/875867419/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group10/875867419/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/875867419/.gitignore b/group10/875867419/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/875867419/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/875867419/.project b/group10/875867419/.project new file mode 100644 index 0000000000..b6d8ce6204 --- /dev/null +++ b/group10/875867419/.project @@ -0,0 +1,17 @@ + + + coding2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/875867419/src/com/work/week01/MyArrayList.java b/group10/875867419/src/com/work/week01/MyArrayList.java new file mode 100644 index 0000000000..d005800d39 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyArrayList.java @@ -0,0 +1,203 @@ +package com.work.week01; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * ʵ��List�ӿڣ�������ķ�ʽʵ���Լ���ArrayList + * @author denghuaijun + * + * @param + */ +public class MyArrayList implements MyList,Serializable { + + private static final long serialVersionUID = 4145346362382387995L; + + /** + * ����Ĭ�ϴ�С + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * ����Ĭ�Ͽ����� + */ + private static final Object[] EMPTY_ELEMENTDATA = {}; + + transient Object[] elementData; + + /** + * ��С + */ + private int size; + + public MyArrayList(){ + this.elementData = EMPTY_ELEMENTDATA; + } + + public MyArrayList(int capacity){ + if(capacity > 0){ + this.elementData = new Object[capacity]; + }else if(capacity == 0){ + this.elementData = EMPTY_ELEMENTDATA; + }else{ + throw new IllegalArgumentException("�Ƿ�����"); + } + } + + private void ensureCapacity(int minCapacity){ + if(this.elementData == EMPTY_ELEMENTDATA){ + minCapacity = Math.max(minCapacity, DEFAULT_CAPACITY); + } + 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); + } + + @Override + public boolean add(E element) { + ensureCapacity(size + 1); + elementData[size++] = element; + return true; + } + + @Override + public void add(int index, E element) { + //ȷ��index�Ƿ�Խ�� + checkAddRange(index); + //ȷ�����鳤���Ƿ��㹻 + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + private void checkAddRange(int index){ + if(index < 0 || index > size){//index == size ������������Ԫ�� + throw new IndexOutOfBoundsException("����Խ��"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + checkRange(index); + return (E) elementData[index]; + } + + private void checkRange(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("����Խ��"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E remove(int index) { + checkRange(index); + E element = (E) elementData[index]; + int numMoved = size - index - 1; + if(numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[size--] = null; + return element; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public int indexOf(Object o) { + if(o == null){ + for(int i=0;i=0;i--){ + if(elementData[i] == null){ + return i; + } + } + }else{ + for(int i=size-1;i>=0;i--){ + if(o.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = -1; + + public MyIter(){ + flag = size; //���鳤�� + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("����ֵ�������鷶Χ"); + } + return (E) elementData[size-(flag--)]; + } + + } + public static void main(String[] args) { + MyArrayList array = new MyArrayList(); + array.add("1"); + array.add("2"); + array.add("3"); + array.add("4"); + array.remove(2); + array.add(2, "1"); + System.out.println("size="+array.size()); + System.out.println("indexOf(3)="+array.indexOf("3")); + System.out.println("lastIndexOf(1)="+array.lastIndexOf("1")); + MyIterator itr = array.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyBinaryTree.java b/group10/875867419/src/com/work/week01/MyBinaryTree.java new file mode 100644 index 0000000000..8c6f057648 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyBinaryTree.java @@ -0,0 +1,82 @@ +package com.work.week01; + +public class MyBinaryTree { + + private MyBinaryTreeNode parent; + + public MyBinaryTree(){ + this.parent = new MyBinaryTreeNode(null, null, null); + } + + public void insertNode(E element){ + MyBinaryTreeNode node = new MyBinaryTreeNode(element, null, null); + if(parent.element == null){ + parent = node; + return; + } + insertNode(parent, node); + } + + private void insertNode(MyBinaryTreeNode parentNode, MyBinaryTreeNode newNode){ + if(parentNode.compareTo(newNode) <= 0){// + if(parentNode.right == null){ + parentNode.right = newNode; + }else{ + insertNode(parentNode.right, newNode); + } + }else{ + if(parentNode.left == null){ + parentNode.left = newNode; + }else{ + insertNode(parentNode.left, newNode); + } + } + } + + private void printNode(MyBinaryTreeNode node, int count){ + if(node.left != null){ + printNode(node.left, count++); + } + if(node.right != null){ + printNode(node.right, count++); + } + for(int i=0;i implements Comparable> { + + private T element; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public MyBinaryTreeNode(T element, MyBinaryTreeNode left, MyBinaryTreeNode right){ + this.element = element; + this.left = left; + this.right = right; + } + + @Override + public int compareTo(MyBinaryTreeNode o) { + Integer src = (Integer) this.element; + Integer dest = (Integer) o.element; + return src.compareTo(dest); + } + } + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree(); + tree.insertNode(5); + tree.insertNode(7); + tree.insertNode(3); + tree.insertNode(9); + tree.insertNode(4); + tree.printTree(); + } +} diff --git a/group10/875867419/src/com/work/week01/MyIterator.java b/group10/875867419/src/com/work/week01/MyIterator.java new file mode 100644 index 0000000000..78abc20f23 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyIterator.java @@ -0,0 +1,6 @@ +package com.work.week01; + +public interface MyIterator { + boolean hasNext(); + E next(); +} diff --git a/group10/875867419/src/com/work/week01/MyLinkedList.java b/group10/875867419/src/com/work/week01/MyLinkedList.java new file mode 100644 index 0000000000..675323a249 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyLinkedList.java @@ -0,0 +1,169 @@ +package com.work.week01; + +import java.io.Serializable; + + +public class MyLinkedList implements MyList, Serializable{ + + private static final long serialVersionUID = 8700137302944494769L; + + transient int size = 0; + + transient MyNode head; + transient MyNode last; + + public MyLinkedList(){ + head = new MyNode(null, null); + last = new MyNode(null, null); + } + + @Override + public boolean add(E element) { + if(head.element == null){ + head = new MyNode(element, null); + last = head; + }else{ + MyNode node = new MyNode(element, null); + last.next = node; + last = node; + } + size++; + return true; + } + + @Override + public void add(int index, E element) { + if(index < 0 || index -size > 0){ + throw new IndexOutOfBoundsException("������������"); + } + if(index == 0){ + MyNode node = new MyNode(element, null); + node.next = head; + head = node; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = new MyNode(element, null); + node.next = leftNode.next; + leftNode.next = node; + } + size++; + } + + private MyNode getIndexNode(int index){ + MyNode node = head; + for(int i=0; i= 0){ + throw new IndexOutOfBoundsException("������������"); + } + MyNode node = getIndexNode(index); + return node.element; + } + + @Override + public E remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("������������"); + } + if(index == 0){//�Ƴ�ͷ��� + MyNode node = head; + head = head.next; + node.next = null; + size--; + return node.element; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = leftNode.next; //���Ƴ��Ľڵ� + leftNode.next = node.next; + node.next = null; + size--; + return node.element; + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E element){ + add(0, element); + } + + public void addLast(E element){ + add(size, element); + } + + public void removeFirst(){ + remove(0); + } + + public void removeLast(){ + remove(size-1); + } + + private static class MyNode{ + E element; + MyNode next; + + MyNode(E element, MyNode next) { + this.element = element; + this.next = next; + } + + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = 0; + + public MyIter(){ + flag = size; + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("����ֵ����������Χ"); + } + return get(size-(flag--)); + } + } + + public static void main(String[] args) { + MyLinkedList link = new MyLinkedList(); + link.add("1"); + link.add("2"); + link.add("3"); + link.add("4"); + link.add(3, "1"); + link.removeFirst(); + System.out.println("size="+link.size()); + MyIterator itr = link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + link.remove(4); + } +} diff --git a/group10/875867419/src/com/work/week01/MyList.java b/group10/875867419/src/com/work/week01/MyList.java new file mode 100644 index 0000000000..f7cc918888 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyList.java @@ -0,0 +1,11 @@ +package com.work.week01; + +public interface MyList{ + boolean add(E element); + void add(int index, E element); + E get(int index); + E remove(int index); + int size(); + boolean isEmpty(); + MyIterator iterator(); +} diff --git a/group10/875867419/src/com/work/week01/MyQueue.java b/group10/875867419/src/com/work/week01/MyQueue.java new file mode 100644 index 0000000000..97bca5399a --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyQueue.java @@ -0,0 +1,38 @@ +package com.work.week01; + +public class MyQueue { + private MyArrayList elementData; + + public MyQueue(){ + elementData = new MyArrayList(); + } + + public void enQueue(E element){//��� + elementData.add(element); + } + + public E deQuene(){//���� �Ƚ��ȳ� + return elementData.remove(0); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + System.out.println("size="+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQuene()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyStack.java b/group10/875867419/src/com/work/week01/MyStack.java new file mode 100644 index 0000000000..f82bbe04c1 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyStack.java @@ -0,0 +1,43 @@ +package com.work.week01; + +public class MyStack { + private MyArrayList elementData; + + public MyStack(){ + elementData = new MyArrayList<>(); + } + + public void push(E element){ + elementData.add(element); + } + + public E pop(){ //�Ƴ�ջ��Ԫ�� ����ȳ� + return elementData.remove(elementData.size() - 1); + } + + public E peek(){ //��ȡջ��Ԫ�� + return elementData.get(elementData.size() - 1); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + stack.push("4"); + stack.push("5"); + System.out.println("size="+stack.size()); + System.out.println("peekջ��Ԫ��="+stack.peek()); + while(!stack.isEmpty()){ + System.out.println("popջ��Ԫ��"+stack.pop()); + } + } +} diff --git "a/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..5c63425fba --- /dev/null +++ "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +������ҵ��ַ��https://my.oschina.net/u/3080511/blog/846172 \ No newline at end of file From beaf0d3bdd182f9e4e5197cfefaac9a628515089 Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Fri, 24 Feb 2017 20:17:39 -0800 Subject: [PATCH 069/432] Complete data structure assignment --- .../{a1 => data_structure}/.gitignore | 0 .../src/com/coding/basic/ArrayList.java | 22 +++++++++++++++-- .../src/com/coding/basic/ArrayListTest.java | 17 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 0 .../com/coding/basic/BinaryTreeNodeTest.java | 0 .../src/com/coding/basic/Iterator.java | 0 .../src/com/coding/basic/LinkedList.java | 24 ++++++++++++++++--- .../src/com/coding/basic/LinkedListTest.java | 13 ++++++++++ .../src/com/coding/basic/List.java | 0 .../src/com/coding/basic/Queue.java | 0 .../src/com/coding/basic/QueueTest.java | 0 .../src/com/coding/basic/Stack.java | 0 .../src/com/coding/basic/StackTest.java | 0 13 files changed, 71 insertions(+), 5 deletions(-) rename group11/395443277/{a1 => data_structure}/.gitignore (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/ArrayList.java (78%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/ArrayListTest.java (80%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/BinaryTreeNode.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/BinaryTreeNodeTest.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/Iterator.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/LinkedList.java (84%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/LinkedListTest.java (90%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/List.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/Queue.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/QueueTest.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/Stack.java (100%) rename group11/395443277/{a1 => data_structure}/src/com/coding/basic/StackTest.java (100%) diff --git a/group11/395443277/a1/.gitignore b/group11/395443277/data_structure/.gitignore similarity index 100% rename from group11/395443277/a1/.gitignore rename to group11/395443277/data_structure/.gitignore diff --git a/group11/395443277/a1/src/com/coding/basic/ArrayList.java b/group11/395443277/data_structure/src/com/coding/basic/ArrayList.java similarity index 78% rename from group11/395443277/a1/src/com/coding/basic/ArrayList.java rename to group11/395443277/data_structure/src/com/coding/basic/ArrayList.java index 4c6c92c42e..93a2781f3b 100644 --- a/group11/395443277/a1/src/com/coding/basic/ArrayList.java +++ b/group11/395443277/data_structure/src/com/coding/basic/ArrayList.java @@ -57,8 +57,26 @@ public int size(){ return size; } - public Iterator iterator(){ - return null; + public Iterator iterator (){ + return new SeqIterator(); + } + + private class SeqIterator implements Iterator { + int i = 0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + if (!hasNext()) { + return null; + } + return elementData[i++]; + } + } } diff --git a/group11/395443277/a1/src/com/coding/basic/ArrayListTest.java b/group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java similarity index 80% rename from group11/395443277/a1/src/com/coding/basic/ArrayListTest.java rename to group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java index 3025b8e077..75bae320f4 100644 --- a/group11/395443277/a1/src/com/coding/basic/ArrayListTest.java +++ b/group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java @@ -64,4 +64,21 @@ public void testRemove() { assertEquals(6, list.get(4)); } + @Test + public void testIterator() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + Iterator it = list.iterator(); + if(it.hasNext()) { + assertEquals(5, it.next()); + assertEquals(4, it.next()); + } + + } + } diff --git a/group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/BinaryTreeNode.java rename to group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java diff --git a/group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java b/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/BinaryTreeNodeTest.java rename to group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java diff --git a/group11/395443277/a1/src/com/coding/basic/Iterator.java b/group11/395443277/data_structure/src/com/coding/basic/Iterator.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/Iterator.java rename to group11/395443277/data_structure/src/com/coding/basic/Iterator.java diff --git a/group11/395443277/a1/src/com/coding/basic/LinkedList.java b/group11/395443277/data_structure/src/com/coding/basic/LinkedList.java similarity index 84% rename from group11/395443277/a1/src/com/coding/basic/LinkedList.java rename to group11/395443277/data_structure/src/com/coding/basic/LinkedList.java index e05f035eb2..fb9fbd81ca 100644 --- a/group11/395443277/a1/src/com/coding/basic/LinkedList.java +++ b/group11/395443277/data_structure/src/com/coding/basic/LinkedList.java @@ -1,7 +1,8 @@ package com.coding.basic; +import java.util.NoSuchElementException; + public class LinkedList implements List { - private Node head; public void add(Object o){ @@ -126,11 +127,28 @@ public Object removeLast(){ return target; } public Iterator iterator(){ - return null; + return new SeqIterator(); } + private class SeqIterator implements Iterator { + Node curr = head; + + @Override + public boolean hasNext() { + return curr != null; + } + + @Override + public Object next() { + if (!hasNext()) throw new NoSuchElementException(); + Object target = curr.data; + curr = curr.next; + return target; + } + + } - private static class Node{ + private static class Node{ Object data; Node next; diff --git a/group11/395443277/a1/src/com/coding/basic/LinkedListTest.java b/group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java similarity index 90% rename from group11/395443277/a1/src/com/coding/basic/LinkedListTest.java rename to group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java index 7faae38be6..af5aa2d3a2 100644 --- a/group11/395443277/a1/src/com/coding/basic/LinkedListTest.java +++ b/group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java @@ -111,4 +111,17 @@ public void testRemoveLast() { assertEquals(3, list.removeLast()); } + @Test + public void testIterator() { + LinkedList list = new LinkedList(); + list.add(4); + list.add(3); + list.add(2); + + Iterator it = list.iterator(); + + assertEquals(4, it.next()); + assertEquals(3, it.next()); + assertEquals(2, it.next()); + } } diff --git a/group11/395443277/a1/src/com/coding/basic/List.java b/group11/395443277/data_structure/src/com/coding/basic/List.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/List.java rename to group11/395443277/data_structure/src/com/coding/basic/List.java diff --git a/group11/395443277/a1/src/com/coding/basic/Queue.java b/group11/395443277/data_structure/src/com/coding/basic/Queue.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/Queue.java rename to group11/395443277/data_structure/src/com/coding/basic/Queue.java diff --git a/group11/395443277/a1/src/com/coding/basic/QueueTest.java b/group11/395443277/data_structure/src/com/coding/basic/QueueTest.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/QueueTest.java rename to group11/395443277/data_structure/src/com/coding/basic/QueueTest.java diff --git a/group11/395443277/a1/src/com/coding/basic/Stack.java b/group11/395443277/data_structure/src/com/coding/basic/Stack.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/Stack.java rename to group11/395443277/data_structure/src/com/coding/basic/Stack.java diff --git a/group11/395443277/a1/src/com/coding/basic/StackTest.java b/group11/395443277/data_structure/src/com/coding/basic/StackTest.java similarity index 100% rename from group11/395443277/a1/src/com/coding/basic/StackTest.java rename to group11/395443277/data_structure/src/com/coding/basic/StackTest.java From c27c036fe16bc949bce4ccee4ac8718bef8e12ef Mon Sep 17 00:00:00 2001 From: fei9009 Date: Fri, 24 Feb 2017 22:48:11 -0600 Subject: [PATCH 070/432] implementation of basic data structures --- .../fei9009/coding2017/basic/ArrayList.java | 71 +++++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 57 ++++++++++++ .../fei9009/coding2017/basic/Iterator.java | 7 ++ .../fei9009/coding2017/basic/LinkedList.java | 91 +++++++++++++++++++ .../github/fei9009/coding2017/basic/List.java | 9 ++ .../fei9009/coding2017/basic/Queue.java | 21 +++++ .../fei9009/coding2017/basic/Stack.java | 26 ++++++ 7 files changed, 282 insertions(+) create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/List.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Queue.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/Stack.java diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..855e25b257 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.github.fei9009.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + private int capacity; + + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.capacity = 20; + } + + private void extend() { + Object[] updatedElementData = new Object[this.elementData.length + this.capacity]; + System.arraycopy(this.elementData, 0, updatedElementData, 0, this.elementData.length); + this.elementData = updatedElementData; + } + + public void add(Object o){ + if (this.size == elementData.length) { + extend(); + } + elementData[size] = o; + this.size++; + } + + public void add(int index, Object o){ + if (this.size == elementData.length) { + extend(); + } + int i; + for (i = this.size - 1; i >= index; i--) { + this.elementData[i + 1] = this.elementData[i]; + } + this.elementData[i + 1] = o; + this.size++; + } + + public Object get(int index){ + if (index >= 0 && index < this.size) { + return this.elementData[index]; + }else { + return null; + } + } + + public Object remove(int index){ + if (index >= 0 && index < this.size) { + int i = 0; + Object deletedElement = this.elementData[index]; + for (i = index + 1; i < this.size; i++) { + this.elementData[i - 1] = this.elementData[i]; + } + this.elementData[i] = null; + this.size--; + return deletedElement; + }else { + return null; + } + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6dbc7ee012 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package com.github.fei9009.coding2017.basic; + +public class BinaryTreeNode > { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode cur = this; + BinaryTreeNode pre = null; + while (cur != null) { + pre = cur; + if (node.getData().compareTo(cur.getData()) <= 0) { + cur = cur.left; + left = true; + } else { + cur = cur.right; + left = false; + } + } + if(left) { + pre.left = node; + } else { + pre.right = node; + } + return node; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..e2f03b8247 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.fei9009.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..1ed5923ee9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java @@ -0,0 +1,91 @@ +package com.github.fei9009.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node dummy = node(index); + Node newNode = new Node(o); + newNode.next = dummy; + if (index == 0) { + head = newNode; + } + else { + Node before = node(index-1); + before.next = newNode; + } + this.size++; + + } + + public Object get(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node node = node(index); + return node.data; + + } + public Object remove(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node delNode = node(index); + if (index == 0) + head = delNode.next; + else { + Node before = node(index-1); + before.next = delNode.next; + } + size--; + return delNode.data; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(this.size -1); + } + public Iterator iterator(){ + return null; + } + + Node node(int index) { + Node x = head; + for (int i=0; i Date: Sat, 25 Feb 2017 12:54:22 +0800 Subject: [PATCH 071/432] a little test --- group18/542330964/.classpath | 6 ++++++ group18/542330964/.gitignore | 1 + group18/542330964/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group18/542330964/.classpath create mode 100644 group18/542330964/.gitignore create mode 100644 group18/542330964/.project diff --git a/group18/542330964/.classpath b/group18/542330964/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group18/542330964/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/542330964/.gitignore b/group18/542330964/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/542330964/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/542330964/.project b/group18/542330964/.project new file mode 100644 index 0000000000..ffec859bfd --- /dev/null +++ b/group18/542330964/.project @@ -0,0 +1,17 @@ + + + 542330964learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From 5d563253ade225dc14ffbf785dd89564736dddc6 Mon Sep 17 00:00:00 2001 From: MysteriousAuntieCook <1925347167@qq.com> Date: Sat, 25 Feb 2017 15:56:24 +1100 Subject: [PATCH 072/432] Create 1925347167.md --- group01/1925347167/1925347167.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 group01/1925347167/1925347167.md diff --git a/group01/1925347167/1925347167.md b/group01/1925347167/1925347167.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group01/1925347167/1925347167.md @@ -0,0 +1 @@ + From 316c91a414342f295b7672ecfe7a755406011431 Mon Sep 17 00:00:00 2001 From: Jie Date: Sat, 25 Feb 2017 13:05:01 +0800 Subject: [PATCH 073/432] Delete .classpath --- group18/542330964/.classpath | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 group18/542330964/.classpath diff --git a/group18/542330964/.classpath b/group18/542330964/.classpath deleted file mode 100644 index d171cd4c12..0000000000 --- a/group18/542330964/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - From a7b47894efe848488a87cdf5b00dffc020fafe8d Mon Sep 17 00:00:00 2001 From: Jie Date: Sat, 25 Feb 2017 13:05:11 +0800 Subject: [PATCH 074/432] Delete .gitignore --- group18/542330964/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group18/542330964/.gitignore diff --git a/group18/542330964/.gitignore b/group18/542330964/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group18/542330964/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ From a9d6edbfdeaf1b38561cd6e22f2c92277e14e34f Mon Sep 17 00:00:00 2001 From: Jie Date: Sat, 25 Feb 2017 13:05:45 +0800 Subject: [PATCH 075/432] Delete .project --- group18/542330964/.project | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 group18/542330964/.project diff --git a/group18/542330964/.project b/group18/542330964/.project deleted file mode 100644 index ffec859bfd..0000000000 --- a/group18/542330964/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - 542330964learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - From be549f3b8b41ba55c118bea5b852444c97c6108b Mon Sep 17 00:00:00 2001 From: thomas_young Date: Sat, 25 Feb 2017 13:09:30 +0800 Subject: [PATCH 076/432] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/test/BinaryTreeNodeTest.java | 49 +++++++++++++++++-- .../coding2017/basic/test/ListTest.java | 4 +- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java index 36f5dff484..57be8bbfef 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -3,29 +3,68 @@ import static org.junit.Assert.assertEquals; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; + import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; public class BinaryTreeNodeTest { private BinaryTreeNode binaryTreeNode; + /** + // 4 + // 1 5 + // 2 3 + */ @Before public void setUpBinaryTreeNode() { binaryTreeNode = new BinaryTreeNode(4); - } - - @Test - public void testBinaryTreeNodeFunctional() { binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); + } + + @Test + public void testBinaryTreeNodeFunctional1() { assertEquals(new Integer(4), binaryTreeNode.getData()); assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); } - + + @Test + public void testBinaryTreeFunctional2() { + BinaryTreeNode node1 = binaryTreeNode.getLeft(); + assertEquals(new Integer(1), node1.getData()); + assertEquals(new Integer(3), node1.getRight().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testBinaryTreeFunctional3() + { + BinaryTreeNode treeNode = new BinaryTreeNode(100); + treeNode.insert(10); + binaryTreeNode.setRight(treeNode); + // 4 + // 1 100 + // 2 3 10 + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); + + expectedEx.expect(Exception.class); + binaryTreeNode.getRight().getRight().getRight(); // null exception + binaryTreeNode.getRight().getRight().getLeft(); // null exception + } } diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java index 3d871f832d..587bf17f4d 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java @@ -85,7 +85,6 @@ public void testException() { aList.remove(1); aList.add(3); aList.add(2, 5); - expectedEx.expect(Exception.class); } @Test @@ -110,6 +109,9 @@ public void testIterator() { assertEquals(1, it.next()); assertEquals(3, it.next()); assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); } } From 4c1bab9f945f6c9894d4fecc4c70b3f2c72e4884 Mon Sep 17 00:00:00 2001 From: Kevin_win10 Date: Sat, 25 Feb 2017 13:54:28 +0800 Subject: [PATCH 077/432] Initialize project --- group04/2082945465/.gitignore | 138 +++++++++++++++++++++++ group04/2082945465/week01/src/Hello.java | 7 ++ 2 files changed, 145 insertions(+) create mode 100644 group04/2082945465/.gitignore create mode 100644 group04/2082945465/week01/src/Hello.java diff --git a/group04/2082945465/.gitignore b/group04/2082945465/.gitignore new file mode 100644 index 0000000000..3f016d5a8c --- /dev/null +++ b/group04/2082945465/.gitignore @@ -0,0 +1,138 @@ +.metadata/ +RemoteSystemsTempFiles/ +.recommenders/ + +*.iml + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +*/.idea/ +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +*.iml +modules.xml +.idea/misc.xml +# *.ipr + +### Java ### +# Compiled class file +*.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* + +# End of https://www.gitignore.io/api/eclipse,intellij,java diff --git a/group04/2082945465/week01/src/Hello.java b/group04/2082945465/week01/src/Hello.java new file mode 100644 index 0000000000..ac6e67463e --- /dev/null +++ b/group04/2082945465/week01/src/Hello.java @@ -0,0 +1,7 @@ +package src; + +public class Hello { + public static void main(String[] args) { + System.out.println("Hello! World!"); + } +} From e85198b25f7c2794e025095864a3f5439dbbbdab Mon Sep 17 00:00:00 2001 From: peter <729245768@qq.com> Date: Sat, 25 Feb 2017 14:13:46 +0800 Subject: [PATCH 078/432] =?UTF-8?q?=E6=B7=BB=E5=8A=A0main=E5=92=8Ctest?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/coding_170225/ArrayList.java | 86 +++++++++++++ .../src/main/coding_170225/BinaryTree.java | 57 +++++++++ .../main/coding_170225/BinaryTreeNode.java | 40 +++++++ .../src/main/coding_170225/Iterator.java | 9 ++ .../src/main/coding_170225/LinkedList.java | 113 ++++++++++++++++++ .../src/main/coding_170225/Queue.java | 23 ++++ .../src/main/coding_170225/Stack.java | 27 +++++ 7 files changed, 355 insertions(+) create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/BinaryTree.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/Iterator.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/Queue.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170225/Stack.java diff --git a/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java b/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java new file mode 100644 index 0000000000..d0a81044c6 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java @@ -0,0 +1,86 @@ +import java.util.Arrays; + +/** + * Created by peter on 2017/2/22. + */ +public class ArrayList { + private Object[] elements = new Object[10]; + private int position = 0; + //添加元素 + public void add(Object o){ + //首先判断当前数组是否已满 + if(position==elements.length){ + ensureCapacity();//如果到达则扩展数组长度 + } + elements[position++] = o; + } + //在index位置添加元素 + public void add(int index,Object o){ + if(index<0||index>position){ + System.out.println("invalid index"); + throw new ArrayIndexOutOfBoundsException(); + } + if(position==elements.length){ + //如果此时数组已满 + ensureCapacity(); + } + for(int i=position-1;i>=index;i--){ + elements[i+1] = elements[i];//元素向后移动一位 + } + elements[index] = o; + position++;//数组元素个数加一 + } + + //获取index位置的元素 + public Object get(int index){ + if(index<0||index>position-1){ + System.out.println("不合法的index"); + throw new ArrayIndexOutOfBoundsException(); + } + return elements[index]; + } + + //删除index位置的元素 + public Object remove(int index){ + if(index<0||index>position-1){ + System.out.println("不合法的index"); + throw new ArrayIndexOutOfBoundsException(); + } + position--; + return elements[position+1]; + } + public int size(){ + return position; + } + //返回一个迭代器 + public Iterator getIterator(){ + return new ArrayListIterator(this); + } + //扩展数组大小,在原来基础上扩展一倍 + public void ensureCapacity(){ + elements = Arrays.copyOf(elements,elements.length); + } + private class ArrayListIterator implements Iterator{ + private ArrayList list; + int position=-1; + public ArrayListIterator(ArrayList list){ + this.list=list; + } + @Override + public boolean hasNext() { + if(++position=binaryTreeNode.getData()){ + //插入到左孩子 + parent.setLeft(binaryTreeNode); + }else{ + //插入到右孩子 + parent.setRight(binaryTreeNode); + } + } + //输出二叉树每个节点 + public void printBinaryTreeNode(BinaryTreeNode root){ + if(root==null){ + return; + } + if(root.getLeft()!=null){ + printBinaryTreeNode(root.getLeft()); + } + System.out.println(root.getData()); + if(root.getRight()!=null){ + printBinaryTreeNode(root.getRight()); + } + } + //获取根节点 + public BinaryTreeNode getRoot(){ + return root; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java b/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java new file mode 100644 index 0000000000..0cea0f59c7 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java @@ -0,0 +1,40 @@ + +/** + * Created by peter on 2017/2/23. + */ +public class BinaryTreeNode{ + private int data; + private BinaryTreeNode left=null; + private BinaryTreeNode right=null; + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(int data){ + this.data=data; + } + + public int getData() { + return data; + } + + public void setData(int 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/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java b/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java new file mode 100644 index 0000000000..a833ba0acd --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java @@ -0,0 +1,9 @@ + +/** + * Created by peter on 2017/2/23. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java b/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java new file mode 100644 index 0000000000..b02e7101b1 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java @@ -0,0 +1,113 @@ + +/** + * Created by peter on 2017/2/22. + */ +public class LinkedList { + private Node head,tail; + private int size=0; + public void add(Object o){ + Node node = new Node(); + node.data = o; + node.next=null; + if(size==0){ + head=node; + tail=node; + }else{ + tail.next = node; + tail = tail.next; + } + size++; + } + public void add(int index,Object o){ + if(index<0||index>size){ + System.out.println("插入下标越界"); + throw new ArrayIndexOutOfBoundsException(); + } + //如果插入的位置是第一个 + if(index==0){ + Node node =new Node(); + node.data = o; + node.next = head; + head = node; + }else { + int i =0;//记录走过的节点 + Node p = head;//移动节点 + while (isize-1){ + System.out.println("访问下标越界"); + throw new ArrayIndexOutOfBoundsException(); + } + Node node =head; + int i=0; + while (isize-1){ + System.out.println("out of array"); + throw new ArrayIndexOutOfBoundsException(); + } + Object data=null;//用来存储返回值 + if(index==0){ + //删除的是第一个节点 + data = head.data; + head=head.next; + size--; + return data; + }else{ + Node p1=head,p2=null;//p1表示移动节点,p2是上一个节点 + int i =0;//表示移动的距离 + while (i Date: Sat, 25 Feb 2017 14:14:24 +0800 Subject: [PATCH 079/432] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/171535320/ArrayList.java | 62 +++++++++ group11/171535320/BinaryTreeNode.java | 43 ++++++ group11/171535320/Iterator.java | 5 + group11/171535320/LinkedList.java | 152 +++++++++++++++++++++ group11/171535320/List.java | 7 + group11/171535320/stackANDqueue/Queue.java | 34 +++++ group11/171535320/stackANDqueue/Stack.java | 52 +++++++ 7 files changed, 355 insertions(+) create mode 100644 group11/171535320/ArrayList.java create mode 100644 group11/171535320/BinaryTreeNode.java create mode 100644 group11/171535320/Iterator.java create mode 100644 group11/171535320/LinkedList.java create mode 100644 group11/171535320/List.java create mode 100644 group11/171535320/stackANDqueue/Queue.java create mode 100644 group11/171535320/stackANDqueue/Stack.java diff --git a/group11/171535320/ArrayList.java b/group11/171535320/ArrayList.java new file mode 100644 index 0000000000..e277814cc9 --- /dev/null +++ b/group11/171535320/ArrayList.java @@ -0,0 +1,62 @@ +import java.util.Arrays; +import java.util.Objects; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private void ensureCapacity(int minCapacity) { + if(minCapacity > elementData.length) { + Object[] temp = elementData; + int newCapacity = elementData.length * 3 / 2 + 1; + Object[] newArray = new Object[newCapacity]; + System.arraycopy(temp, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + + public void add(Object o){ + + + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index >= size || index < 0) { + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index >= size) { + return null; + } + Object obj = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + elementData[--size] = null; + + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group11/171535320/BinaryTreeNode.java b/group11/171535320/BinaryTreeNode.java new file mode 100644 index 0000000000..3c8d543355 --- /dev/null +++ b/group11/171535320/BinaryTreeNode.java @@ -0,0 +1,43 @@ +public class BinaryTreeNode { + + private Node root = null; + + public void insert(int value) { + if(root == null) { + root = new Node(value); + root.leftNode = null; + root.rightNode = null; + } else { + Node current = root; + Node old = root; + while(true) { + if(value < current.value) { + if(current.leftNode == null) { + current.leftNode = new Node(value); + break; + } + old = current; + current = current.leftNode; + } else { + if(current.rightNode == null) { + current.rightNode = new Node(value); + break; + } + old = current; + current = current.rightNode; + } + } + } + } + +} + +class Node { + int value; + Node leftNode; + Node rightNode; + + public Node(int value) { + this.value = value; + } +} diff --git a/group11/171535320/Iterator.java b/group11/171535320/Iterator.java new file mode 100644 index 0000000000..96a43dbe0a --- /dev/null +++ b/group11/171535320/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group11/171535320/LinkedList.java b/group11/171535320/LinkedList.java new file mode 100644 index 0000000000..43bb74e516 --- /dev/null +++ b/group11/171535320/LinkedList.java @@ -0,0 +1,152 @@ +import java.util.Objects; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + if(head == null) { + head = new Node(); + head.data = o; + } else { + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = new Node(); + temp.next.data = o; + } + } + + public void add(int index , Object o){ + if(index > size()) { + return ; + } + if(index == 0) { + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head = newNode; + } else { + int temp = 0; + Node newNode = new Node(); + newNode.data = o; + Node tempNode = head; + while(temp != index - 1) { + tempNode = tempNode.next; + ++temp; + } + Node tempNode2 = tempNode.next; + tempNode.next = newNode; + newNode.next = tempNode2; + } + } + + public Object get(int index){ + if(index > size() || size() == 0) { + return null; + } + Node temp = head; + for(int i = 0; i < index; ++i) { + temp = temp.next; + } + + return temp.data; + } + + + public Object remove(int index){ + if(size() == 0) { + return null; + } + if(size() == 1) { + Object obj = head.data; + head = null; + return obj; + } + if(index == 0) { + Node temp = head; + head = head.next; + temp.next = null; + } else if(index > size()) { + return null; + } else { + int t = 0; + Node temp = head; + while(t != index - 1) { + temp = temp.next; + ++t; + } + Node result = temp.next; + temp.next = temp.next.next; + return result.data; + } + return null; + } + + public int size(){ + int len = 0; + Node temp = head; + while(temp != null) { + temp = temp.next; + ++len; + } + return len; + } + + public void addFirst(Object o){ + Node temp = new Node(); + temp.data = o; + temp.next = head; + head = temp; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + + if(size() == 0) { + head = newNode; + } + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = newNode; + } + public Object removeFirst(){ + if(size() == 0) { + return null; + } + Node temp = head; + head = head.next; + return temp.data; + } + public Object removeLast(){ + if(size() == 0) { + return null; + } + if(size() == 1) { + Node temp = head.next; + head = head.next; + return temp.data; + } + Node temp1 = head; + Node temp2 = head.next; + while(temp2.next != null) { + temp1 = temp1.next; + temp2 = temp2.next; + } + temp1.next = null; + return temp2.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group11/171535320/List.java b/group11/171535320/List.java new file mode 100644 index 0000000000..4f7bcc71a8 --- /dev/null +++ b/group11/171535320/List.java @@ -0,0 +1,7 @@ +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/group11/171535320/stackANDqueue/Queue.java b/group11/171535320/stackANDqueue/Queue.java new file mode 100644 index 0000000000..cbd885e73c --- /dev/null +++ b/group11/171535320/stackANDqueue/Queue.java @@ -0,0 +1,34 @@ +package stackANDqueue; + +import java.util.LinkedList; +import java.util.List; + +/** + * Created by dengdechao on 2017/2/22. + */ +public class Queue { + + private LinkedList queue = new LinkedList(); + + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + if(queue.isEmpty()) { + return null; + } + return queue.removeFirst(); + } + + public boolean isEmpty(){ + if(queue.isEmpty()) { + return true; + } + return false; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group11/171535320/stackANDqueue/Stack.java b/group11/171535320/stackANDqueue/Stack.java new file mode 100644 index 0000000000..17ca9d6298 --- /dev/null +++ b/group11/171535320/stackANDqueue/Stack.java @@ -0,0 +1,52 @@ +package stackANDqueue; + +import java.util.ArrayList; + +/** + * Created by dengdechao on 2017/2/22. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size() == 0) { + return null; + } + Object obj = null; + for(int i = 0; i < elementData.size(); ++i) { + obj = elementData.get(i); + } + int j = 0; + while(j < elementData.size()) { + ++j; + } + elementData.set(j - 1, null); + return obj; + } + + public Object peek(){ + if(elementData.size() == 0) { + return null; + } + Object obj = null; + for(int i = 0; i < elementData.size(); ++i) { + obj = elementData.get(i); + } + + return obj; + } + public boolean isEmpty(){ + if(elementData.size() == 0) { + return true; + } + + return false; + } + public int size(){ + return elementData.size(); + } +} From d11bf6a392eff6760aaa9af4b188fe802adc20c3 Mon Sep 17 00:00:00 2001 From: qilei Date: Sat, 25 Feb 2017 14:34:01 +0800 Subject: [PATCH 080/432] linkedList --- .../java/com/coding/basic/LinkedList.java | 87 +++++++++++++++++-- .../java/com/coding/basic/LinkedListTest.java | 64 ++++++++++++++ 2 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java index e2c4e5e795..cba0879ac4 100644 --- a/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java @@ -5,20 +5,71 @@ public class LinkedList implements List { private Node head; public void add(Object o){ - - } + Node node = new Node(o, null); + if (head == null){ + head = node; + }else { + Node current = head; + while (current.getNext() != null) { + current = current.getNext(); + } + current.setNext(node); + } + } + public void add(int index , Object o){ - - } + Node current = head; + for (int i = 0; i < index-1; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + Node right = current.getNext(); + current.setNext(new Node(o,right)); + } + public Object get(int index){ - return null; + if (head == null){ + return null; + } + Node current = head; + for (int i = 0; i < index; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + return current.getData(); } + public Object remove(int index){ - return null; + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + if (current.getNext() == null){ + throw new IndexOutOfBoundsException(); + } + Object removed = current.getNext().getData(); + current.setNext(current.getNext().getNext()); + return removed; } public int size(){ - return -1; + if (head == null){ + return 0; + } + int n = 1; + Node current = head; + while (current.getNext() != null) { + n ++; + current = current.getNext(); + } + return n; } public void addFirst(Object o){ @@ -41,6 +92,26 @@ public Iterator iterator(){ private static class Node{ Object data; Node next; - + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } } } diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..29a5a3d90e --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class LinkedListTest { + + @Test + public void add() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + assertThat(l.size()).isEqualTo(3); + assertThat(l.get(1)).isEqualTo(2); + } + + @Test + public void insert() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + l.add(1,4); + assertThat(l.size()).isEqualTo(4); + assertThat(l.get(1)).isEqualTo(4); + } + + @Test + public void remove() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + Object removed = l.remove(1); + assertThat(l.size()).isEqualTo(2); + assertThat(removed).isEqualTo(2); + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + +} \ No newline at end of file From 0acb01864b26275684d7fe053651fc6ef3b87605 Mon Sep 17 00:00:00 2001 From: BaymaxGithub <1280157271@qq.com> Date: Sat, 25 Feb 2017 14:53:08 +0800 Subject: [PATCH 081/432] 1280157271_FirstHomeWork MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm not goog at use Github,I'm Members of the 7 group.This is my First Homework commit!!!thank you! --- .../20170224-01-ArrayList/.classpath | 6 + .../20170224-01-ArrayList/.gitignore | 1 + .../1280157271/20170224-01-ArrayList/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/firstHomework/fan/List.java | 9 ++ .../src/firstHomework/fan/myArrayList.java | 60 ++++++++ .../src/firstHomework/fan/myLinkedList.java | 129 ++++++++++++++++++ .../src/firstHomework/fan/myQueue.java | 51 +++++++ .../src/firstHomework/fan/myStack.java | 31 +++++ ...\347\232\204\345\205\263\347\263\273 .txt" | 1 + 10 files changed, 316 insertions(+) create mode 100644 group07/1280157271/20170224-01-ArrayList/.classpath create mode 100644 group07/1280157271/20170224-01-ArrayList/.gitignore create mode 100644 group07/1280157271/20170224-01-ArrayList/.project create mode 100644 group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs create mode 100644 group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java create mode 100644 group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java create mode 100644 group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java create mode 100644 group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java create mode 100644 group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java create mode 100644 "group07/1280157271/\347\254\254\344\270\200\346\211\271\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" diff --git a/group07/1280157271/20170224-01-ArrayList/.classpath b/group07/1280157271/20170224-01-ArrayList/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1280157271/20170224-01-ArrayList/.gitignore b/group07/1280157271/20170224-01-ArrayList/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1280157271/20170224-01-ArrayList/.project b/group07/1280157271/20170224-01-ArrayList/.project new file mode 100644 index 0000000000..4c0107dd15 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.project @@ -0,0 +1,17 @@ + + + 20170224-01-ArrayList + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs b/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.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/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java new file mode 100644 index 0000000000..6eb3636be5 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java @@ -0,0 +1,9 @@ +package firstHomework.fan; + +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/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java new file mode 100644 index 0000000000..bd39bfca4e --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java @@ -0,0 +1,60 @@ +package firstHomework.fan; + +public class myArrayList implements List { + + private int size = 0; + private int initLength=10; + private Object[] elementData = new Object[initLength]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + ensureCapacity(size+1); + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + }else{ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + } + + public Object get(int index){ + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + }else{ + return elementData[index]; + } + return null; + } + + public Object remove(int index){ + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + return null; + } + Object obj = get(index); + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return obj; + } + + public int size(){ + return this.size; + } + + + + public void ensureCapacity(int x){ + int oldCapacity = elementData.length; + if(x>oldCapacity){ + Object newEle[] = new Object[oldCapacity+initLength]; + System.arraycopy(elementData, 0, newEle, 0, size); + elementData = newEle; + } + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java new file mode 100644 index 0000000000..da481e0232 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java @@ -0,0 +1,129 @@ +package firstHomework.fan; + + + +public class myLinkedList implements List { + private static class Node{//�����Ľڵ�ṹ�� + Object data;//������ + Node next; //Node���ã��൱��ָ�룬ָ����һ���ڵ� + + Node(Object e, Node next) { + this.data = e; + this.next = next; + } + } + + private Node head,last=null;//�ֱ�ָ���һ�������һ���ڵ� + private int size; + + + public void add(Object o){//��β�����ӣ����൱������β�ڵ� + creatLastNode(o); + } + public void add(int index , Object o){//��indexǰ����� + if(index == 0){//˵����������λ��Ϊ0����ô������ͷ��� + createFirstNode(o); + }else{//��ȥ�ҵ�ָ��λ�� + Node indexBeforeNode = getNode(index-1);//���ﷵ�ص���index��ǰһ���ڵ� + Node newIndex =new Node(o,indexBeforeNode.next) ;//x�½ڵ㱣��indexBefore��ָ�� + indexBeforeNode.next = newIndex; + size++; + } + } + public Object get(int index){ + return getNode(index).data;//���ص��ǽڵ��е����ݶ��� + } + + public Object remove(int index){ + if(index==0){//�Ƴ�ͷ��� + removeFirst(); + }else{//�ҵ�ָ���ڵ��ǰһ���ڵ� + Node removeNode = getNode(index-1); + removeNode.next = removeNode.next.next;//�Ƴ���index + size--; + return removeNode.next.data;//�����Ƴ��ڵ�����ݶ��� + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + createFirstNode(o); + } + public void addLast(Object o){ + creatLastNode(o); + } + public Object removeFirst(){ + if(size>0){//�б���Ϊ�գ���һ����ͷ + Node removeHead = head; + head = head.next; + size--; + return removeHead.data;//����ͷ�������ݶ��� + }else{ + System.out.println("����Ϊ�գ�"); + } + return null; + } + public Object removeLast(){ + if(size>0){ + Node removeLastBefore = getNode(size-2);//�ҵ�last�ڵ����һ���ڵ� + Object returnObj = removeLastBefore.next.data; + removeLastBefore.next = null; + last = removeLastBefore; + size--; + return returnObj; + }else{ + System.out.println("����Ϊ�գ�"); + } + return null; + } + + /* + * ����ͷ��� + * */ + private void createFirstNode(Object e){ + Node oldHead = head; + Node newHead = new Node(e,oldHead);//�������Ľڵ���Ϊhead�ڵ��ǰһ�ڵ� + head = newHead;//���ܿղ��գ�headҪָ���µ�ͷ�ڵ� + if(head == null){//�������Ϊ�գ�head��last��ָ���½ڵ㣨��Ϊ��last�Ͳ����Ҹ�ֵ����Ϊ��ȷ������������ģ� + last = newHead; + }else{//ͷ����Ѿ�����,�½ڵ���Ϊͷ��㣬ԭhead��ڶ���last����ָ������last + newHead.next = head; + } + size++; + } + /* + * ����β��� + * */ + private void creatLastNode(Object e){ + Node oldLast = last; + Node newLast = new Node(e,null);//�µ�β�ڵ���һ���ڵ�Ϊ�� + last = newLast;//���ܿղ��գ�last��Ҫָ���µ�β�ڵ� + if(head == null){//����Ϊ�� + head = newLast; + }else{ + oldLast.next = newLast; + } + size++; + } + /* + * Ѱ��ָ����� + * */ + private Node getNode(int index){ + if(index<0||index>=size){ + System.out.println("indexԽ�磡����"); + }else{ + Node node=head; + while(index != 0){ + node = node.next; + index--; + } + return node; + } + return null; + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java new file mode 100644 index 0000000000..041da7e29b --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java @@ -0,0 +1,51 @@ +package firstHomework.fan; + +public class myQueue { + private int iniLength = 10; + private Object[] array = new Object[iniLength]; + private int size = 0; + + public void enQueue(Object o){ + if(size>=array.length){//��Ҫ���� + Object[] newArray = new Object[iniLength+array.length];//array����ԭ���ټ���10 + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size++] = o; + } + + public Object deQueue(){//�Ƴ���һ��Ԫ�أ������Ԫ��������ǰλ�� + Object deQueue = array[0]; + System.arraycopy(array, 1, array, 0, size-1); + size--; + return deQueue; + + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return this.size; + } + public static void main(String[] args) { + myQueue queue = new myQueue(); + queue.enQueue("A"); + queue.enQueue("B"); + queue.enQueue("C"); + + queue.enQueue("D"); + queue.enQueue("E"); + queue.enQueue("F"); + queue.enQueue("G"); + + while(!queue.isEmpty()){ + System.out.println(queue.deQueue());//���� + } + + } + + + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java new file mode 100644 index 0000000000..623bbe787f --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java @@ -0,0 +1,31 @@ +package firstHomework.fan; + + + +public class myStack { + private myArrayList array = new myArrayList();//myArrayList�������и���̬���� + + public void push(Object o){ //��ջ + //���Ȳ���ʱmyArrayList���Լ����� + array.add(o);//�¶������������� + } + + public Object pop(){//��ջ��������β�ϵ�Ԫ�أ� ��Ҫɾ���� + Object pop = array.get(array.size()-1);//�±�Ҫ��sizeС1 + array.remove(array.size()-1); + return pop; + } + + public Object peek(){//ֻ�ǵ���ջ����ֵ����ɾ�� + return array.get(array.size()-1); + } + public boolean isEmpty(){ + return array.size()==0; + } + public int size(){ + return array.size(); + } + + +} + diff --git "a/group07/1280157271/\347\254\254\344\270\200\346\211\271\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" "b/group07/1280157271/\347\254\254\344\270\200\346\211\271\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" new file mode 100644 index 0000000000..1e19f6adda --- /dev/null +++ "b/group07/1280157271/\347\254\254\344\270\200\346\211\271\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" @@ -0,0 +1 @@ +http://blog.csdn.net/stromcloud/article/details/56678442 \ No newline at end of file From b0cfea5cf5de095ae7f79cfa116bd8db38ab1a60 Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:05:35 +0800 Subject: [PATCH 082/432] 1st commit --- .../src/com/coding/basic/ArrayList.java | 106 ++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 52 +++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/LinkedList.java | 134 ++++++++++++++++++ .../972815123/src/com/coding/basic/List.java | 9 ++ .../src/com/coding/basic/MainTest.java | 21 +++ .../972815123/src/com/coding/basic/Queue.java | 28 ++++ .../972815123/src/com/coding/basic/Stack.java | 31 ++++ 8 files changed, 387 insertions(+) create mode 100644 group19/972815123/src/com/coding/basic/ArrayList.java create mode 100644 group19/972815123/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group19/972815123/src/com/coding/basic/Iterator.java create mode 100644 group19/972815123/src/com/coding/basic/LinkedList.java create mode 100644 group19/972815123/src/com/coding/basic/List.java create mode 100644 group19/972815123/src/com/coding/basic/MainTest.java create mode 100644 group19/972815123/src/com/coding/basic/Queue.java create mode 100644 group19/972815123/src/com/coding/basic/Stack.java diff --git a/group19/972815123/src/com/coding/basic/ArrayList.java b/group19/972815123/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3e8afb1ffc --- /dev/null +++ b/group19/972815123/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private int size; + private Object[] data; + public ArrayList() { + data = new Object[10]; + } + + @Override + public void add(Object o) { + size = size(); + if(data.length <= size){ + grow(); + } + data[size] = o; + size++; + } + + /* (non-Javadoc) + * @see dataStructure.List#add(java.lang.Object, int) + * 在第index元素前插入元素 + */ + @Override + public void add(int index, Object o){ + if (index >= size()){ + return; + } + size = size(); + if(data.length <= size){ + grow(); + } + for(int i = size , len = size - index; i < len; i -- ){ + data[i] = data[i -1]; + } + data[index] = o; + size++; + } + + @Override + public Object get(int index) { + return data[index]; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + if (index >= size()){ + return null; + }else{ + Object o = data[index]; + for(int i = index; i < size; i ++){ + data[i] = data[i + 1]; + } + data[size] = null; + size--; + return o; + } + + } + + private void grow(){ + size = size(); + int length = 0; + if(size < 10000){ + length = size * 2; + }else{ + length = (int)(size * 1.5); + } + size = length; + + Object[] temData = new Object[length]; + for(int i = 0, j = data.length; i < j; i ++){ + temData[i] = data[i]; + } + data = temData; + } + + private int index = 0; + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + index++; + return data[index - 1]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for(int i = 0; i < size; i++){ + sb.append(data[i].toString() + ","); + } + sb.append("]"); + return sb.toString(); + } + +} diff --git a/group19/972815123/src/com/coding/basic/BinaryTreeNode.java b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d6518342d4 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,52 @@ +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){ + Comparable co = (Comparable)o; + Comparable coData = (Comparable)data; + BinaryTreeNode result = null; + if(co.compareTo(data) > 0){ + if(null == right){ + right = new BinaryTreeNode(); + right.data = o; + result = right; + return right; + }else{ + right.insert(o); + } + }else{ + if(null == left){ + left = new BinaryTreeNode(); + left.data = o; + result = left; + return left; + }else{ + left.insert(o); + } + } + return result; + } +} diff --git a/group19/972815123/src/com/coding/basic/Iterator.java b/group19/972815123/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group19/972815123/src/com/coding/basic/LinkedList.java b/group19/972815123/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..04de763349 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/LinkedList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +public class LinkedList implements List,Iterator { + + private Node head; + private Node last; + private int size = 0; + + public LinkedList() { + head = new Node(); + } + + @Override + public void add(Object o) { + Node newNode = new Node(); + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = newNode; + newNode.prev = last; + last = newNode; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + Node indexNode = head ; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + Node indexNextNode = indexNode.next; + indexNode.next = newNode; + newNode.prev = indexNode; + newNode.next = indexNextNode; + indexNextNode.prev = newNode; + size ++; + } + + @Override + public Object get(int index) { + Node indexNode = head; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + return indexNode; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + Node indexNode = head ; + int i = 0; + while(i == index){ + + indexNode = indexNode.next; + i++; + } + Object o = indexNode.prev; + Node indexNextNode = indexNode.next; + Node indexPrevNode = indexNode.prev; + + indexNextNode.prev = indexPrevNode; + indexPrevNode.next = indexNextNode; + + indexNode.next = null; + indexNode.prev = null; + size--; + return o; + } + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head.prev = newNode; + head = newNode; + size ++; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.prev = last; + last.next = newNode; + last = newNode; + size ++; + } + public Object removeFirst(){ + Node ret = head; + head = head.next; + head.prev = null; + size--; + return ret; + } + public Object removeLast(){ + Node ret = last; + last = last.prev; + last.next = null; + size--; + return ret; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + } + + private Node index = head; + @Override + public boolean hasNext() { + return index != null; + } + + @Override + public Object next() { + Node tem = index; + index = index.next; + return tem; + } +} diff --git a/group19/972815123/src/com/coding/basic/List.java b/group19/972815123/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coding/basic/MainTest.java b/group19/972815123/src/com/coding/basic/MainTest.java new file mode 100644 index 0000000000..504e2759e8 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/MainTest.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class MainTest { + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add("this is the fifth"); + System.out.println(list); + System.out.println(list.size()); + System.out.println(list.get(3)); + + while(list.hasNext()){ + System.out.println(list.next()); + } + } + +} diff --git a/group19/972815123/src/com/coding/basic/Queue.java b/group19/972815123/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6f03f6052b --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList data; + private int size; + + public Queue(){ + data = new LinkedList(); + } + + public void enQueue(Object o){ + data.addLast(o); + size++; + } + + public Object deQueue(){ + size --; + return data.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group19/972815123/src/com/coding/basic/Stack.java b/group19/972815123/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..fcfda97a71 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + +private ArrayList elementData = new ArrayList(); +private int size = 0; + + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if(size > 0){ + size--; + return elementData.remove(size); + }else{ + return null; + } + } + + public Object peek(){ + return elementData.get(size); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} From c20c357134ea5ab22874ef067195d15d9c4216dc Mon Sep 17 00:00:00 2001 From: "devin.yin" Date: Sat, 25 Feb 2017 15:12:44 +0800 Subject: [PATCH 083/432] 1st commit--init with ArrayList --- group06/284999210/.classpath | 6 ++++++ group06/284999210/.gitignore | 3 +++ group06/284999210/.project | 17 +++++++++++++++++ .../com/coding/basic/container/ArrayList.java | 5 +++++ 4 files changed, 31 insertions(+) create mode 100644 group06/284999210/.classpath create mode 100644 group06/284999210/.gitignore create mode 100644 group06/284999210/.project create mode 100644 group06/284999210/src/com/coding/basic/container/ArrayList.java diff --git a/group06/284999210/.classpath b/group06/284999210/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/284999210/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/284999210/.gitignore b/group06/284999210/.gitignore new file mode 100644 index 0000000000..f5ebe83019 --- /dev/null +++ b/group06/284999210/.gitignore @@ -0,0 +1,3 @@ +*.class +/.metadata +/bin \ No newline at end of file diff --git a/group06/284999210/.project b/group06/284999210/.project new file mode 100644 index 0000000000..c3e8c69e46 --- /dev/null +++ b/group06/284999210/.project @@ -0,0 +1,17 @@ + + + 284999210 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/284999210/src/com/coding/basic/container/ArrayList.java b/group06/284999210/src/com/coding/basic/container/ArrayList.java new file mode 100644 index 0000000000..74c79d17bc --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/ArrayList.java @@ -0,0 +1,5 @@ +package com.coding.basic.container; + +public class ArrayList { + +} From 05d029396f5d8dd36af9dc8057ef801568515213 Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:15:08 +0800 Subject: [PATCH 084/432] Revert "test" This reverts commit be853d34d2135a838bd8db390eff49ff74f641bb. --- .gitignore | 9 --------- group19/2558178127/.gitignore | 9 --------- group19/2558178127/src/com/cn/kevin/Test.java | 5 ----- 3 files changed, 23 deletions(-) delete mode 100644 group19/2558178127/.gitignore delete mode 100644 group19/2558178127/src/com/cn/kevin/Test.java diff --git a/.gitignore b/.gitignore index fcfd185b8c..ec55baf87d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,12 +14,3 @@ hs_err_pid* #ide config .metadata .recommenders - -*.settings -*.project -*.classpath -*/.settings -*.iml -/.idea -/**/target/**/* - diff --git a/group19/2558178127/.gitignore b/group19/2558178127/.gitignore deleted file mode 100644 index ee46440140..0000000000 --- a/group19/2558178127/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -/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 deleted file mode 100644 index f5a6ce104a..0000000000 --- a/group19/2558178127/src/com/cn/kevin/Test.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.cn.kevin; - -public class Test { - -} From 1fa7bbae6fe2f63df3090d4d22a98d8ba143a3c7 Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:21:18 +0800 Subject: [PATCH 085/432] revert "Revert "test"" This reverts commit 05d029396f5d8dd36af9dc8057ef801568515213. --- .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 3264b8f26e6b7b7197822368efb4731aa83526e1 Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:24:16 +0800 Subject: [PATCH 086/432] Revert "1st commit" This reverts commit b0cfea5cf5de095ae7f79cfa116bd8db38ab1a60. --- .../src/com/coding/basic/ArrayList.java | 106 -------------- .../src/com/coding/basic/BinaryTreeNode.java | 52 ------- .../src/com/coding/basic/Iterator.java | 6 - .../src/com/coding/basic/LinkedList.java | 134 ------------------ .../972815123/src/com/coding/basic/List.java | 9 -- .../src/com/coding/basic/MainTest.java | 21 --- .../972815123/src/com/coding/basic/Queue.java | 28 ---- .../972815123/src/com/coding/basic/Stack.java | 31 ---- 8 files changed, 387 deletions(-) delete mode 100644 group19/972815123/src/com/coding/basic/ArrayList.java delete mode 100644 group19/972815123/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 group19/972815123/src/com/coding/basic/Iterator.java delete mode 100644 group19/972815123/src/com/coding/basic/LinkedList.java delete mode 100644 group19/972815123/src/com/coding/basic/List.java delete mode 100644 group19/972815123/src/com/coding/basic/MainTest.java delete mode 100644 group19/972815123/src/com/coding/basic/Queue.java delete mode 100644 group19/972815123/src/com/coding/basic/Stack.java diff --git a/group19/972815123/src/com/coding/basic/ArrayList.java b/group19/972815123/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 3e8afb1ffc..0000000000 --- a/group19/972815123/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.coding.basic; - -public class ArrayList implements List, Iterator { - - private int size; - private Object[] data; - public ArrayList() { - data = new Object[10]; - } - - @Override - public void add(Object o) { - size = size(); - if(data.length <= size){ - grow(); - } - data[size] = o; - size++; - } - - /* (non-Javadoc) - * @see dataStructure.List#add(java.lang.Object, int) - * 在第index元素前插入元素 - */ - @Override - public void add(int index, Object o){ - if (index >= size()){ - return; - } - size = size(); - if(data.length <= size){ - grow(); - } - for(int i = size , len = size - index; i < len; i -- ){ - data[i] = data[i -1]; - } - data[index] = o; - size++; - } - - @Override - public Object get(int index) { - return data[index]; - } - - @Override - public int size() { - return size; - } - - @Override - public Object remove(int index) { - if (index >= size()){ - return null; - }else{ - Object o = data[index]; - for(int i = index; i < size; i ++){ - data[i] = data[i + 1]; - } - data[size] = null; - size--; - return o; - } - - } - - private void grow(){ - size = size(); - int length = 0; - if(size < 10000){ - length = size * 2; - }else{ - length = (int)(size * 1.5); - } - size = length; - - Object[] temData = new Object[length]; - for(int i = 0, j = data.length; i < j; i ++){ - temData[i] = data[i]; - } - data = temData; - } - - private int index = 0; - @Override - public boolean hasNext() { - return index < size; - } - - @Override - public Object next() { - index++; - return data[index - 1]; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("["); - for(int i = 0; i < size; i++){ - sb.append(data[i].toString() + ","); - } - sb.append("]"); - return sb.toString(); - } - -} diff --git a/group19/972815123/src/com/coding/basic/BinaryTreeNode.java b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d6518342d4..0000000000 --- a/group19/972815123/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,52 +0,0 @@ -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){ - Comparable co = (Comparable)o; - Comparable coData = (Comparable)data; - BinaryTreeNode result = null; - if(co.compareTo(data) > 0){ - if(null == right){ - right = new BinaryTreeNode(); - right.data = o; - result = right; - return right; - }else{ - right.insert(o); - } - }else{ - if(null == left){ - left = new BinaryTreeNode(); - left.data = o; - result = left; - return left; - }else{ - left.insert(o); - } - } - return result; - } -} diff --git a/group19/972815123/src/com/coding/basic/Iterator.java b/group19/972815123/src/com/coding/basic/Iterator.java deleted file mode 100644 index e7cbd474ec..0000000000 --- a/group19/972815123/src/com/coding/basic/Iterator.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} diff --git a/group19/972815123/src/com/coding/basic/LinkedList.java b/group19/972815123/src/com/coding/basic/LinkedList.java deleted file mode 100644 index 04de763349..0000000000 --- a/group19/972815123/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.coding.basic; - -public class LinkedList implements List,Iterator { - - private Node head; - private Node last; - private int size = 0; - - public LinkedList() { - head = new Node(); - } - - @Override - public void add(Object o) { - Node newNode = new Node(); - Node last = head; - while(last.next != null){ - last = last.next; - } - last.next = newNode; - newNode.prev = last; - last = newNode; - size++; - } - - @Override - public void add(int index, Object o) { - Node newNode = new Node(); - Node indexNode = head ; - int i = 0; - while(i == index){ - indexNode = indexNode.next; - i++; - } - Node indexNextNode = indexNode.next; - indexNode.next = newNode; - newNode.prev = indexNode; - newNode.next = indexNextNode; - indexNextNode.prev = newNode; - size ++; - } - - @Override - public Object get(int index) { - Node indexNode = head; - int i = 0; - while(i == index){ - indexNode = indexNode.next; - i++; - } - return indexNode; - } - - @Override - public int size() { - return size; - } - - @Override - public Object remove(int index) { - Node indexNode = head ; - int i = 0; - while(i == index){ - - indexNode = indexNode.next; - i++; - } - Object o = indexNode.prev; - Node indexNextNode = indexNode.next; - Node indexPrevNode = indexNode.prev; - - indexNextNode.prev = indexPrevNode; - indexPrevNode.next = indexNextNode; - - indexNode.next = null; - indexNode.prev = null; - size--; - return o; - } - - public void addFirst(Object o){ - Node newNode = new Node(); - newNode.data = o; - newNode.next = head; - head.prev = newNode; - head = newNode; - size ++; - } - public void addLast(Object o){ - Node newNode = new Node(); - newNode.data = o; - newNode.prev = last; - last.next = newNode; - last = newNode; - size ++; - } - public Object removeFirst(){ - Node ret = head; - head = head.next; - head.prev = null; - size--; - return ret; - } - public Object removeLast(){ - Node ret = last; - last = last.prev; - last.next = null; - size--; - return ret; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - Node prev; - } - - private Node index = head; - @Override - public boolean hasNext() { - return index != null; - } - - @Override - public Object next() { - Node tem = index; - index = index.next; - return tem; - } -} diff --git a/group19/972815123/src/com/coding/basic/List.java b/group19/972815123/src/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/group19/972815123/src/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -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/group19/972815123/src/com/coding/basic/MainTest.java b/group19/972815123/src/com/coding/basic/MainTest.java deleted file mode 100644 index 504e2759e8..0000000000 --- a/group19/972815123/src/com/coding/basic/MainTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.coding.basic; - -public class MainTest { - - public static void main(String[] args) { - ArrayList list = new ArrayList(); - list.add(1); - list.add(2); - list.add(3); - list.add(4); - list.add("this is the fifth"); - System.out.println(list); - System.out.println(list.size()); - System.out.println(list.get(3)); - - while(list.hasNext()){ - System.out.println(list.next()); - } - } - -} diff --git a/group19/972815123/src/com/coding/basic/Queue.java b/group19/972815123/src/com/coding/basic/Queue.java deleted file mode 100644 index 6f03f6052b..0000000000 --- a/group19/972815123/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.coding.basic; - -public class Queue { - private LinkedList data; - private int size; - - public Queue(){ - data = new LinkedList(); - } - - public void enQueue(Object o){ - data.addLast(o); - size++; - } - - public Object deQueue(){ - size --; - return data.removeFirst(); - } - - public boolean isEmpty(){ - return size == 0; - } - - public int size(){ - return size; - } -} diff --git a/group19/972815123/src/com/coding/basic/Stack.java b/group19/972815123/src/com/coding/basic/Stack.java deleted file mode 100644 index fcfda97a71..0000000000 --- a/group19/972815123/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.coding.basic; - -public class Stack { - -private ArrayList elementData = new ArrayList(); -private int size = 0; - - public void push(Object o){ - elementData.add(o); - size ++; - } - - public Object pop(){ - if(size > 0){ - size--; - return elementData.remove(size); - }else{ - return null; - } - } - - public Object peek(){ - return elementData.get(size); - } - public boolean isEmpty(){ - return size == 0; - } - public int size(){ - return size; - } -} From f0e4040449a0fe99097534d10cb098773fe0bfa4 Mon Sep 17 00:00:00 2001 From: "devin.yin" Date: Sat, 25 Feb 2017 15:33:06 +0800 Subject: [PATCH 087/432] basic test for self implemented ArrayList --- .../com/coding/basic/container/ArrayList.java | 75 ++++++++++++++++++- .../basic/container/test/TestContainer.java | 68 +++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 group06/284999210/src/com/coding/basic/container/test/TestContainer.java diff --git a/group06/284999210/src/com/coding/basic/container/ArrayList.java b/group06/284999210/src/com/coding/basic/container/ArrayList.java index 74c79d17bc..07db744bab 100644 --- a/group06/284999210/src/com/coding/basic/container/ArrayList.java +++ b/group06/284999210/src/com/coding/basic/container/ArrayList.java @@ -1,5 +1,78 @@ package com.coding.basic.container; -public class ArrayList { +public class ArrayList { + private Object[] elements; + private int size; + private int capacity; + + public ArrayList() { + elements = new Object[8]; + capacity = 8; + size = 0; + } + + public void add(T element) { + if (size == capacity) { + Object[] tempArray = new Object[capacity]; + for (int i = 0; i < size; i++) { + tempArray[i] = elements[i]; + } + elements = new Object[capacity * 2]; + for (int i = 0; i < size; i++) { + elements[i] = tempArray[i]; + } + elements[capacity] = element; + capacity = capacity * 2; + } + elements[size] = element; + size = size + 1; + } + + public void remove(int index) { + if (index >= capacity) { + throw new IndexOutOfBoundsException(); + } + + for (int i = index; i < size; i++) { + elements[i] = elements[i + 1]; + } + elements[size] = null; + size = size - 1; + } + + public void set(int index, Object element) { + if (index >= capacity) { + throw new IndexOutOfBoundsException(); + } + + elements[index] = element; + } + + @SuppressWarnings("unchecked") public T get(int index) { + if (index >= elements.length) { + throw new IndexOutOfBoundsException(); + } + + return (T) elements[index]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + if (i != size - 1) { + sb.append(elements[i] + ", "); + } else { + sb.append(elements[i]); + } + } + sb.append("]"); + return sb.toString(); + } + + public int size() { + return size; + } } diff --git a/group06/284999210/src/com/coding/basic/container/test/TestContainer.java b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java new file mode 100644 index 0000000000..058bbd250c --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java @@ -0,0 +1,68 @@ +/** + * + */ +package com.coding.basic.container.test; + +import java.util.List; + +/** + * @author devin.yin + * + */ +public class TestContainer { + + /** + * @param args + */ + public static void main(String[] args) { + List list1 = new java.util.ArrayList(); + System.out.println(list1); + + // 4 basic operation for java.util.ArrayList--add remove change query + list1.add("0"); + list1.add("1"); + list1.add("2"); + list1.add("3"); + list1.add("4"); + list1.add("5"); + list1.add("6"); + list1.add("7"); + list1.add("8"); + list1.add("9"); + System.out.println(list1); + + list1.remove(0); + System.out.println(list1); + + list1.set(0, "set"); + System.out.println(list1); + + System.out.println(list1.get(0)); + + System.out.println("------------------------------------------------"); + + // 4 basic operation for com.coding.basic.container.ArrayList--add remove change query + com.coding.basic.container.ArrayList list2 = new com.coding.basic.container.ArrayList(); + System.out.println(list2); + list2.add("0"); + list2.add("1"); + list2.add("2"); + list2.add("3"); + list2.add("4"); + list2.add("5"); + list2.add("6"); + list2.add("7"); + list2.add("8"); + list2.add("9"); + System.out.println(list2); + + list2.remove(0); + System.out.println(list2); + + list2.set(0, "set"); + System.out.println(list2); + + System.out.println(list2.get(0)); + } + +} From d8c55b7413a39869c8b9fd453a8e84b7623c0cb9 Mon Sep 17 00:00:00 2001 From: Kevin_win10 Date: Sat, 25 Feb 2017 15:38:04 +0800 Subject: [PATCH 088/432] ArrayList implemented --- .../2082945465/week01/src/MyArrayList.java | 72 +++++++++++++++++++ group04/2082945465/week01/src/MyIterator.java | 9 +++ .../2082945465/week01/src/MyLinkedList.java | 31 ++++++++ group04/2082945465/week01/src/MyList.java | 12 ++++ group04/2082945465/week01/src/MyQueue.java | 7 ++ group04/2082945465/week01/src/MyStack.java | 7 ++ 6 files changed, 138 insertions(+) create mode 100644 group04/2082945465/week01/src/MyArrayList.java create mode 100644 group04/2082945465/week01/src/MyIterator.java create mode 100644 group04/2082945465/week01/src/MyLinkedList.java create mode 100644 group04/2082945465/week01/src/MyList.java create mode 100644 group04/2082945465/week01/src/MyQueue.java create mode 100644 group04/2082945465/week01/src/MyStack.java diff --git a/group04/2082945465/week01/src/MyArrayList.java b/group04/2082945465/week01/src/MyArrayList.java new file mode 100644 index 0000000000..1de76354fd --- /dev/null +++ b/group04/2082945465/week01/src/MyArrayList.java @@ -0,0 +1,72 @@ +package src; + +import java.util.Arrays; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + this.checkCapacity(size+1); + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + this.validIndex(index); + this.checkCapacity(size+1); + if(index < size){ + for(int i = size; i > index; i--){ + this.elementData[i] = this.elementData[i-1]; + } + }else{ + this.elementData[index] = o; + } + this.size++; + } + + @Override + public Object get(int index) { + this.validIndex(index); + return this.elementData[index]; + } + + @Override + public Object remove(int index) { + this.validIndex(index); + Object o = this.elementData[index]; + for(int i = index; i < this.size-1; i++){ + this.elementData[i] = this.elementData[i+1]; + } + this.size--; + return o; + } + + @Override + public int size() { + return this.size; + } + + private void checkCapacity(int newSize) { + if(newSize > elementData.length){ + this.extend(elementData); + } + } + + private void extend(Object[] oldElementData) { + int newLength = (int) (oldElementData.length * 1.5); + elementData = Arrays.copyOf(oldElementData, newLength); + } + + private void validIndex(int inputIndex) { + if(inputIndex > size || inputIndex < 0){ + throw new RuntimeException("Index: " + inputIndex + " out of bounds( " + size +" )"); + } + } +} diff --git a/group04/2082945465/week01/src/MyIterator.java b/group04/2082945465/week01/src/MyIterator.java new file mode 100644 index 0000000000..245fa82be8 --- /dev/null +++ b/group04/2082945465/week01/src/MyIterator.java @@ -0,0 +1,9 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public interface MyIterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group04/2082945465/week01/src/MyLinkedList.java b/group04/2082945465/week01/src/MyLinkedList.java new file mode 100644 index 0000000000..8549f1b0c8 --- /dev/null +++ b/group04/2082945465/week01/src/MyLinkedList.java @@ -0,0 +1,31 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyLinkedList implements MyList { + @Override + public void add(Object o) { + + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int index) { + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + return 0; + } +} diff --git a/group04/2082945465/week01/src/MyList.java b/group04/2082945465/week01/src/MyList.java new file mode 100644 index 0000000000..0b67820e73 --- /dev/null +++ b/group04/2082945465/week01/src/MyList.java @@ -0,0 +1,12 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public interface MyList { + 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/group04/2082945465/week01/src/MyQueue.java b/group04/2082945465/week01/src/MyQueue.java new file mode 100644 index 0000000000..497602bd3a --- /dev/null +++ b/group04/2082945465/week01/src/MyQueue.java @@ -0,0 +1,7 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyQueue { +} diff --git a/group04/2082945465/week01/src/MyStack.java b/group04/2082945465/week01/src/MyStack.java new file mode 100644 index 0000000000..831af93ed7 --- /dev/null +++ b/group04/2082945465/week01/src/MyStack.java @@ -0,0 +1,7 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyStack { +} From 8db7091445169500702e0e489f629d269052912a Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:45:35 +0800 Subject: [PATCH 089/432] 1st test --- group19/972815123/test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 group19/972815123/test.txt diff --git a/group19/972815123/test.txt b/group19/972815123/test.txt new file mode 100644 index 0000000000..e69de29bb2 From 1ae8f1893372faaf4756ad0e5e831db89e56e956 Mon Sep 17 00:00:00 2001 From: Samson Shenglu Cao Date: Sat, 25 Feb 2017 15:47:00 +0800 Subject: [PATCH 090/432] coding homework --- .../src/com/coding/basic/ArrayList.java | 106 ++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 52 +++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/LinkedList.java | 134 ++++++++++++++++++ .../972815123/src/com/coding/basic/List.java | 9 ++ .../src/com/coding/basic/MainTest.java | 21 +++ .../972815123/src/com/coding/basic/Queue.java | 28 ++++ .../972815123/src/com/coding/basic/Stack.java | 31 ++++ 8 files changed, 387 insertions(+) create mode 100644 group19/972815123/src/com/coding/basic/ArrayList.java create mode 100644 group19/972815123/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group19/972815123/src/com/coding/basic/Iterator.java create mode 100644 group19/972815123/src/com/coding/basic/LinkedList.java create mode 100644 group19/972815123/src/com/coding/basic/List.java create mode 100644 group19/972815123/src/com/coding/basic/MainTest.java create mode 100644 group19/972815123/src/com/coding/basic/Queue.java create mode 100644 group19/972815123/src/com/coding/basic/Stack.java diff --git a/group19/972815123/src/com/coding/basic/ArrayList.java b/group19/972815123/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3e8afb1ffc --- /dev/null +++ b/group19/972815123/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private int size; + private Object[] data; + public ArrayList() { + data = new Object[10]; + } + + @Override + public void add(Object o) { + size = size(); + if(data.length <= size){ + grow(); + } + data[size] = o; + size++; + } + + /* (non-Javadoc) + * @see dataStructure.List#add(java.lang.Object, int) + * 在第index元素前插入元素 + */ + @Override + public void add(int index, Object o){ + if (index >= size()){ + return; + } + size = size(); + if(data.length <= size){ + grow(); + } + for(int i = size , len = size - index; i < len; i -- ){ + data[i] = data[i -1]; + } + data[index] = o; + size++; + } + + @Override + public Object get(int index) { + return data[index]; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + if (index >= size()){ + return null; + }else{ + Object o = data[index]; + for(int i = index; i < size; i ++){ + data[i] = data[i + 1]; + } + data[size] = null; + size--; + return o; + } + + } + + private void grow(){ + size = size(); + int length = 0; + if(size < 10000){ + length = size * 2; + }else{ + length = (int)(size * 1.5); + } + size = length; + + Object[] temData = new Object[length]; + for(int i = 0, j = data.length; i < j; i ++){ + temData[i] = data[i]; + } + data = temData; + } + + private int index = 0; + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + index++; + return data[index - 1]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for(int i = 0; i < size; i++){ + sb.append(data[i].toString() + ","); + } + sb.append("]"); + return sb.toString(); + } + +} diff --git a/group19/972815123/src/com/coding/basic/BinaryTreeNode.java b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d6518342d4 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,52 @@ +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){ + Comparable co = (Comparable)o; + Comparable coData = (Comparable)data; + BinaryTreeNode result = null; + if(co.compareTo(data) > 0){ + if(null == right){ + right = new BinaryTreeNode(); + right.data = o; + result = right; + return right; + }else{ + right.insert(o); + } + }else{ + if(null == left){ + left = new BinaryTreeNode(); + left.data = o; + result = left; + return left; + }else{ + left.insert(o); + } + } + return result; + } +} diff --git a/group19/972815123/src/com/coding/basic/Iterator.java b/group19/972815123/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group19/972815123/src/com/coding/basic/LinkedList.java b/group19/972815123/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..04de763349 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/LinkedList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +public class LinkedList implements List,Iterator { + + private Node head; + private Node last; + private int size = 0; + + public LinkedList() { + head = new Node(); + } + + @Override + public void add(Object o) { + Node newNode = new Node(); + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = newNode; + newNode.prev = last; + last = newNode; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + Node indexNode = head ; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + Node indexNextNode = indexNode.next; + indexNode.next = newNode; + newNode.prev = indexNode; + newNode.next = indexNextNode; + indexNextNode.prev = newNode; + size ++; + } + + @Override + public Object get(int index) { + Node indexNode = head; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + return indexNode; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + Node indexNode = head ; + int i = 0; + while(i == index){ + + indexNode = indexNode.next; + i++; + } + Object o = indexNode.prev; + Node indexNextNode = indexNode.next; + Node indexPrevNode = indexNode.prev; + + indexNextNode.prev = indexPrevNode; + indexPrevNode.next = indexNextNode; + + indexNode.next = null; + indexNode.prev = null; + size--; + return o; + } + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head.prev = newNode; + head = newNode; + size ++; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.prev = last; + last.next = newNode; + last = newNode; + size ++; + } + public Object removeFirst(){ + Node ret = head; + head = head.next; + head.prev = null; + size--; + return ret; + } + public Object removeLast(){ + Node ret = last; + last = last.prev; + last.next = null; + size--; + return ret; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + } + + private Node index = head; + @Override + public boolean hasNext() { + return index != null; + } + + @Override + public Object next() { + Node tem = index; + index = index.next; + return tem; + } +} diff --git a/group19/972815123/src/com/coding/basic/List.java b/group19/972815123/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coding/basic/MainTest.java b/group19/972815123/src/com/coding/basic/MainTest.java new file mode 100644 index 0000000000..504e2759e8 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/MainTest.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class MainTest { + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add("this is the fifth"); + System.out.println(list); + System.out.println(list.size()); + System.out.println(list.get(3)); + + while(list.hasNext()){ + System.out.println(list.next()); + } + } + +} diff --git a/group19/972815123/src/com/coding/basic/Queue.java b/group19/972815123/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6f03f6052b --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList data; + private int size; + + public Queue(){ + data = new LinkedList(); + } + + public void enQueue(Object o){ + data.addLast(o); + size++; + } + + public Object deQueue(){ + size --; + return data.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group19/972815123/src/com/coding/basic/Stack.java b/group19/972815123/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..fcfda97a71 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + +private ArrayList elementData = new ArrayList(); +private int size = 0; + + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if(size > 0){ + size--; + return elementData.remove(size); + }else{ + return null; + } + } + + public Object peek(){ + return elementData.get(size); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} From 0fbde072b040f899065edf14d9fb69849bbc0a1f Mon Sep 17 00:00:00 2001 From: "devin.yin" Date: Sat, 25 Feb 2017 16:20:12 +0800 Subject: [PATCH 091/432] update ArrayList --- .../com/coding/basic/container/ArrayList.java | 157 ++++++++++++++++-- 1 file changed, 146 insertions(+), 11 deletions(-) diff --git a/group06/284999210/src/com/coding/basic/container/ArrayList.java b/group06/284999210/src/com/coding/basic/container/ArrayList.java index 07db744bab..eca9b07f4c 100644 --- a/group06/284999210/src/com/coding/basic/container/ArrayList.java +++ b/group06/284999210/src/com/coding/basic/container/ArrayList.java @@ -1,18 +1,24 @@ package com.coding.basic.container; -public class ArrayList { +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class ArrayList implements java.util.List{ private Object[] elements; private int size; private int capacity; + private static final int DEFAULT_CAPACITY = 8; public ArrayList() { elements = new Object[8]; - capacity = 8; size = 0; + capacity = DEFAULT_CAPACITY; } - public void add(T element) { + public boolean add(T element) { if (size == capacity) { Object[] tempArray = new Object[capacity]; for (int i = 0; i < size; i++) { @@ -27,32 +33,38 @@ public void add(T element) { } elements[size] = element; size = size + 1; + return true; } - public void remove(int index) { - if (index >= capacity) { - throw new IndexOutOfBoundsException(); - } + @SuppressWarnings("unchecked") + public T remove(int index) { + checkIndex(index); + Object o = elements[index]; for (int i = index; i < size; i++) { elements[i] = elements[i + 1]; } elements[size] = null; size = size - 1; + return (T)o; } - public void set(int index, Object element) { + private void checkIndex(int index) { if (index >= capacity) { throw new IndexOutOfBoundsException(); } + } + public T set(int index, Object element) { + checkIndex(index); + + Object o = elements[index]; elements[index] = element; + return (T)o; } @SuppressWarnings("unchecked") public T get(int index) { - if (index >= elements.length) { - throw new IndexOutOfBoundsException(); - } + checkIndex(index); return (T) elements[index]; } @@ -75,4 +87,127 @@ public String toString() { public int size() { return size; } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + if (o == null) return false; + for (int i = 0; i < size; i++) { + if (elements[i].equals(o)) { + return true; + } + } + return false; + } + + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object[] toArray() { + return elements; + } + + @Override + public T[] toArray(T[] a) { + return (T[])elements; + } + + @Override + public boolean remove(Object o) { + if (o == null) return false; + int findIndex = -1; + for (int i = 0; i < size; i++) { + if (elements[i].equals(o)) { + findIndex = i; + break; + } + } + + for (int i = findIndex; i < size - 1; i++) { + elements[i] = elements[i + 1]; + } + elements[size - 1] = null; + size--; + return false; + } + + @Override + public boolean containsAll(Collection c) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addAll(Collection c) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean removeAll(Collection c) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean retainAll(Collection c) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void clear() { + elements = new Object[8]; + size = 0; + capacity = DEFAULT_CAPACITY; + } + + @Override + public void add(int index, T element) { + // TODO Auto-generated method stub + } + + @Override + public int indexOf(Object o) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int lastIndexOf(Object o) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public ListIterator listIterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ListIterator listIterator(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List subList(int fromIndex, int toIndex) { + // TODO Auto-generated method stub + return null; + } } From 6de3613ba5a80403e905d13b75cbcd8103861b67 Mon Sep 17 00:00:00 2001 From: Pxshuo Date: Sat, 25 Feb 2017 16:20:46 +0800 Subject: [PATCH 092/432] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/pxshuo/basic/TreeData.java | 4 + .../src/com/pxshuo/basic/impl/ArrayList.java | 6 ++ .../src/com/pxshuo/basic/impl/BinaryTree.java | 46 +++++++++++ .../com/pxshuo/basic/impl/BinaryTreeNode.java | 82 +++++++++++++------ .../CodeSE01/src/com/pxshuo/test/Test.java | 4 +- .../com/pxshuo/basic/impl/ArrayListTest.java | 33 +++++++- .../com/pxshuo/basic/impl/BinaryTreeTest.java | 26 ++++++ .../com/pxshuo/basic/impl/LinkedListTest.java | 43 ++++++++++ .../test/com/pxshuo/basic/impl/QueueTest.java | 23 ++++++ .../test/com/pxshuo/basic/impl/StackTest.java | 25 ++++++ 10 files changed, 261 insertions(+), 31 deletions(-) create mode 100644 group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java create mode 100644 group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java create mode 100644 group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java create mode 100644 group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java index ef7901606c..f20a3765f1 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java @@ -7,6 +7,10 @@ public TreeData(int data) { this.data = data; } + public int getData() { + return data; + } + @Override public String toString() { return data + ""; diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java index 7590bca6f9..68108e41a2 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java @@ -24,6 +24,9 @@ public void add(Object o) { @Override public void add(int index, Object o) { + if (index > size + 1) { + return; + } elements = grow(); int moveNum = size - index + 1;//本次操作需要移动的元素的个数; size++; @@ -53,6 +56,9 @@ public Object get(int index) { @Override public Object remove(int index) { + if (index > size) { + return null; + } Object removeEle = elements[index]; int moveNum = size - index;//本次操作需要移动的元素的个数; if (moveNum > 0) { diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java index 8aea75b019..7ea54eae78 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java @@ -1,8 +1,20 @@ package com.pxshuo.basic.impl; +import com.pxshuo.basic.Iterator; + +/** + * 排序二叉树 + * @author Pxshuo + * + */ + public class BinaryTree { BinaryTreeNode root = null; + /** + * 添加一个二叉树的节点 + * @param o + */ public void add(Comparable o){ if (root == null) { root = new BinaryTreeNode(); @@ -13,7 +25,41 @@ public void add(Comparable o){ } } + public Object get(int index){ + Stack findChild = childPath(index); + BinaryTreeNode child = null; + int childNum = 0; + for(;!findChild.isEmpty();){ + childNum = (int)findChild.pop(); + if (childNum != -1) { + child = child.getChild(childNum); + } + else { + child = root; + } + } + return child == null ? null : child.getData(); + } + public void display(){ root.display(1); } + + private Stack childPath(int index) { + Stack findChild = new Stack(); + + while(true){ + if (index == 1 || index <= 0) { + findChild.push(-1); + return findChild; + } + if (index%2 == 1) { + findChild.push(1); + } + else { + findChild.push(0); + } + index = index/2; + } + } } diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java index 2b85095dbe..d028dc5f48 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java @@ -13,33 +13,11 @@ public BinaryTreeNode() { 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 void setIndex(int index) { - this.index = index; - } - - public int getIndex() { - return index; - } - + /** + * 差入一个二叉树节点 + * @param o + * @return + */ public BinaryTreeNode insert(Comparable o){ if(data == null){ data = o; @@ -67,6 +45,27 @@ public BinaryTreeNode insert(Comparable o){ return child; } + /** + * 根据二叉树的位置获取孩子节点 + * @param index 0代表左孩子,1代表右孩子 + * @return + */ + public BinaryTreeNode getChild(int index){ + if(index == 0){ + return getLeft(); + } + else if(index == 1){ + return getRight(); + } + else { + return null; + } + } + + /** + * 用于打印二叉树 + * @param myIndex 在二叉树中的位置--采用完全二叉树 + */ public void display(int myIndex){ System.out.println(myIndex + ":" + data.toString()); @@ -78,4 +77,33 @@ public void display(int myIndex){ } } + /////////////////get和set函数/////////////////////////////////////// + + 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 void setIndex(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + } diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java index 39f8dcce2e..c40ddac87d 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -52,6 +52,8 @@ public static void main(String[] args) { binaryTree.add(new TreeData(4)); binaryTree.add(new TreeData(8)); - binaryTree.display(); + System.out.println(binaryTree.get(5).getClass()); + + //binaryTree.display(); } } diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java index 8c49730e47..4249574817 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java @@ -6,10 +6,37 @@ import com.pxshuo.basic.impl.ArrayList; public class ArrayListTest { + ArrayList object = new ArrayList(); + @Test public void addTest() { - ArrayList arrayList = new ArrayList(); - arrayList.add("String"); - Assert.assertEquals(arrayList.get(0), "String"); + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(3) ); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); } } diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java new file mode 100644 index 0000000000..a9d67f7ba1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java @@ -0,0 +1,26 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.TreeData; +import com.pxshuo.basic.impl.BinaryTree; + +public class BinaryTreeTest { + BinaryTree object = new BinaryTree(); + + @Test + public void binaryTest() { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + Assert.assertEquals("4", binaryTree.get(5).toString()); + Assert.assertEquals("8", binaryTree.get(7).toString()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java new file mode 100644 index 0000000000..72fd2c49f1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java @@ -0,0 +1,43 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.ArrayList; +import com.pxshuo.basic.impl.LinkedList; + +public class LinkedListTest { + LinkedList object = new LinkedList(); + + @Test + public void addTest() { + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(0)); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java new file mode 100644 index 0000000000..4f2b5735e4 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java @@ -0,0 +1,23 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.Queue; + +public class QueueTest { + public Queue object = new Queue(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.enQueue("hello"); + object.enQueue("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("hello", object.deQueue()); + Assert.assertEquals("world", object.deQueue()); + Assert.assertEquals(0, object.size()); + } + +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java new file mode 100644 index 0000000000..df1e254595 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java @@ -0,0 +1,25 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.Queue; +import com.pxshuo.basic.impl.Stack; + +public class StackTest { + public Stack object = new Stack(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.push("hello"); + object.push("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("world", object.peek()); + Assert.assertEquals("world", object.pop()); + Assert.assertEquals("hello", object.pop()); + Assert.assertEquals(0, object.size()); + } + +} From 839bda6a84ffd217f08dfdc3f1a68731cee3f1f4 Mon Sep 17 00:00:00 2001 From: tennysons Date: Sat, 25 Feb 2017 16:34:48 +0800 Subject: [PATCH 093/432] BinarySearchTree --- .../com/coding/basic/BinarySearchTree.java | 154 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 77 ------- .../src/com/coding/basic/JavaTest.java | 147 ++++++------- .../src/com/coding/basic/LinkedList.java | 208 ++++++++++-------- 4 files changed, 340 insertions(+), 246 deletions(-) create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java delete mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java new file mode 100644 index 0000000000..b74dbe85a2 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java @@ -0,0 +1,154 @@ +package com.coding.basic; + +/** + * BST ���������� ʵ�� ��14С�� 296933284 + * + * @author Tonnyson + * + */ +public class BinarySearchTree implements Comparable { + + private Object data; + private BinarySearchTree leftChild; + private BinarySearchTree rightChild; + + public BinarySearchTree() { + super(); + this.data = null; + this.leftChild = null; + this.rightChild = null; + } + + public BinarySearchTree(Object data) { + this(); + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinarySearchTree getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinarySearchTree leftChild) { + this.leftChild = leftChild; + } + + public BinarySearchTree getRightChild() { + return rightChild; + } + + public void setRightChild(BinarySearchTree rightChild) { + this.rightChild = rightChild; + } + + /** + * �����в���ڵ� + * + * @param obj + * �ڵ�ֵ + */ + public void insert(Object obj) { + insert(obj, this); + } + + private boolean insert(Object obj, BinarySearchTree node) { + + BinarySearchTree bstNode = new BinarySearchTree(obj); + + if (node == null) { + node = bstNode; + return true; + } else if (node.compareTo(obj) == 0) { + return true; + } else if (node.compareTo(obj) > 0) { + + if (node.getLeftChild() != null) { + return insert(obj, node.getLeftChild()); + } + + node.leftChild = bstNode; + + } else if (node.compareTo(obj) < 0) { + + if (node.getRightChild() != null) { + return insert(obj, node.getRightChild()); + } + + node.rightChild = bstNode; + } + + return false; + + } + + /** + * ������� BST �Ľڵ㣬ʹ֮������� + */ + public void inOrder(BinarySearchTree node) { + + if (node != null) { + inOrder(node.getLeftChild()); + visit(node); + inOrder(node.getRightChild()); + } + + } + + /** + * ������� BST �Ľڵ�ֵ + */ + public void levelOrder(BinarySearchTree node) { + Queue queue = new Queue(); + BinarySearchTree bstNode = null; + queue.enQueue(node); + + while (!queue.isEmpty()) { + bstNode = (BinarySearchTree) queue.deQueue(); + visit(bstNode); + + if (bstNode.getLeftChild() != null) { + queue.enQueue(bstNode.getLeftChild()); + } + + if (bstNode.getRightChild() != null) { + queue.enQueue(bstNode.getRightChild()); + } + } + } + + /** + * ����ָ���ڵ�ֵ + * + * @param node + */ + public void visit(BinarySearchTree node) { + System.out.println(node.getData()); + } + + /** + * �Ƚ� BST �ڵ�ֵ��С + */ + @Override + public int compareTo(Object obj) { + int result = 0; + + if (obj instanceof Integer) { + Integer value = (Integer) obj; + Integer thisValue = (Integer) this.data; + result = thisValue.compareTo(value); + } else { + String value = obj.toString(); + result = this.data.toString().compareTo(value); + } + + return result; + } + +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d01667aaa8..0000000000 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode implements Comparable { - - private Object data; - private BinaryTreeNode leftChild; - private BinaryTreeNode rightChild; - - public BinaryTreeNode() { - super(); - this.data = null; - this.leftChild = null; - this.rightChild = null; - } - - public BinaryTreeNode(Object data) { - super(); - this.data = data; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeftChild() { - return leftChild; - } - - public void setLeftChild(BinaryTreeNode leftChild) { - this.leftChild = leftChild; - } - - public BinaryTreeNode getRightChild() { - return rightChild; - } - - public void setRightChild(BinaryTreeNode rightChild) { - this.rightChild = rightChild; - } - - /** - * �����в���ڵ� - * - * @param i - */ - public void insert(int i) { - - } - - // ... - @Override - public int compareTo(Object obj) { - - return 0; - } - - -} - - - - - - - - - - - - - - - diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java index bc0878cb50..8c4cab01b6 100644 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java @@ -3,51 +3,64 @@ import org.junit.Test; public class JavaTest { - + @Test - public void binaryTreeNodeTest() { -// BinaryTreeNode tree = new BinaryTreeNode(5); -// -// System.out.println(tree.getData()); + public void binarySearchTreeTest() { + BinarySearchTree bSTree = new BinarySearchTree(5); + + System.out.println(bSTree.getData()); + + // insert + bSTree.insert(1); + bSTree.insert(2); + bSTree.insert(4); + bSTree.insert(6); + bSTree.insert(7); + bSTree.insert(8); + System.out.println("-----------------"); + // inOrder + bSTree.inOrder(bSTree); + + System.out.println("-----------------"); + // levelOrder + bSTree.levelOrder(bSTree); } - - + @Test public void queueTest() { Queue queue = new Queue(); - + // enQueue() for (int i = 0; i < 10; i++) { queue.enQueue("hello: " + i); } - + // size() System.out.println(queue.size()); // 10 // isEmpty System.out.println(queue.isEmpty()); - + // deQueue() for (int i = 0; i < 10; i++) { System.out.println(queue.deQueue()); } - + // size() System.out.println(queue.size()); // 0 - + // isEmpty System.out.println(queue.isEmpty()); } - - + @Test public void stackTest() { Stack stack = new Stack(); - + // push() for (int i = 0; i < 10; i++) { stack.push("hello: " + i); } - + // size() System.out.println(stack.size()); @@ -59,127 +72,105 @@ public void stackTest() { // isEmpty() System.out.println(stack.isEmpty()); System.out.println(stack.size()); - + stack.push("hello world 1"); stack.push("hello world 2"); stack.push("hello world 3"); stack.push("hello world 4"); - + // peek() System.out.println(stack.peek()); - + // isEmpty() System.out.println(stack.isEmpty()); } - + @Test public void linkedListTest() { LinkedList linkedList = new LinkedList(); - // add(obj) - for (int i = 0; i < 10; i++) { + // add() addLast() + for (int i = 0; i < 5; i++) { linkedList.add("hello: " + i); } - // size() - System.out.println(linkedList.size()); // 10 + // iterator() get() getPreNode() + Iterator iter = linkedList.iterator(); - // get(index) - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); + while (iter.hasNext()) { + System.out.println(iter.next()); } - -// System.out.println("---->" + linkedList.get(10)); // java.lang.NullPointerException - - // remove() - System.out.println("---->" + linkedList.remove(9)); // 9 - System.out.println("---->" + linkedList.remove(5)); // 5 - System.out.println(linkedList.size()); // 8 - - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); + System.out.println("-----------------------"); + LinkedList linkedList1 = new LinkedList(); + + // addFirst() + for (int i = 0; i < 5; i++) { + linkedList1.addFirst("hello: " + i); } - // removeFirst() - System.out.println("---->" + linkedList.removeFirst()); // 0 - // removeLast - System.out.println("---->" + linkedList.removeLast()); // 8 + Iterator iter1 = linkedList1.iterator(); - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); - } - - // add(index, obj) - linkedList.add(4, "^_^"); - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("&&&&" + linkedList.get(i)); + while (iter1.hasNext()) { + System.out.println(iter1.next()); } - // addFirst() - linkedList.addFirst("***first***"); - // addLast() - linkedList.addLast("---last---"); - System.out.println(linkedList.get(0)); // ***first*** - System.out.println(linkedList.get(linkedList.size() - 1)); // ---last--- - System.err.println(linkedList.size()); // 9 + System.out.println("-----------------------"); + // remove() + System.out.println(linkedList1.remove(0)); // hello: 4 - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("&&&&" + linkedList.get(i)); - } + System.out.println("-----------------------"); + // removeFirst() removeLast() + System.out.println(linkedList1.removeFirst()); // hello: 3 + System.out.println(linkedList1.removeLast()); // hello: 0 - System.out.println("-------------------------------"); - // iterator - Iterator iter = linkedList.iterator(); - while (iter.hasNext()) { - System.out.println(iter.next()); - } - - System.out.println("-------------------------------"); + System.out.println("-----------------------"); + // size() + System.out.println(linkedList.size()); // 5 } - + @Test public void arrayListTest() { ArrayList arrayList = new ArrayList(); - + // add(obj) for (int i = 0; i < 10; i++) { arrayList.add("hello: " + i); } - + // get(index) for (int i = 0; i < arrayList.size(); i++) { System.out.println("-->" + arrayList.get(i)); } - + // add(index, obj) arrayList.add(5, "Tennyson"); - + for (int i = 0; i < arrayList.size(); i++) { System.out.println("++>" + arrayList.get(i)); } - + // size() System.out.println("size: " + arrayList.size()); System.out.println("index 5: " + arrayList.get(5)); - + // remove() Object value = arrayList.remove(5); System.out.println("index 5: " + value); System.out.println("size: " + arrayList.size()); System.out.println("index5: " + arrayList.get(5)); - + for (int i = 0; i < arrayList.size(); i++) { System.out.println("index " + i + " : " + arrayList.get(i)); } - + System.out.println("-------------------------------"); // iterator Iterator iter = arrayList.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } - + System.out.println("-------------------------------"); - + } } diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java index 72b4ba8a90..2a397e9bec 100644 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java @@ -1,7 +1,11 @@ package com.coding.basic; +import java.util.Collection; + +import org.junit.Test; + /** - * LinkedList ʵ�� ��14С�� 296933284 + * LinkedList (��ͷ���ĵ�����) ʵ�� ��14С�� 296933284 * * @author Tonnyson * @@ -10,144 +14,150 @@ public class LinkedList implements List { private Node head; private int size; - + public LinkedList() { super(); this.head = new Node(); this.size = 0; } - /** - * β�巨������ĩβ����ڵ� - */ public void add(Object obj) { + addLast(obj); + } - if (head.data == null) { - head.data = obj; - head.next = null; + public void add(int index, Object obj) { + + if (index == size + 1) { + addLast(obj); } else { - - Node r = head; - while (r.next != null) - r = r.next; - + Node r = getPreNode(index); Node node = new Node(); node.data = obj; - node.next = null; - + node.next = r.next; r.next = node; + size++; } - - size++; - } + + /** - * ��ָ������λ�ò���ڵ� + * �������ײ�����ڵ� + * + * @param obj ������ڵ�Ľڵ�ֵ + * */ - public void add(int index, Object obj) { - - if (index > size) - throw new IndexOutOfBoundsException(); - - Node r = head; - for (int i = 0; i < index - 1; i++) - r = r.next; - + public void addFirst(Object obj) { Node node = new Node(); node.data = obj; - node.next = null; - node.next = r.next; - r.next = node; + + node.next = head.next; + head.next = node; size++; } /** - * ����ָ��λ�õ�ֵ + * ������β������ڵ� + * + * @param obj ������ڵ�Ľڵ�ֵ + * */ - public Object get(int index) { - if (index > size) - throw new IndexOutOfBoundsException(); - + public void addLast(Object obj) { + + Node node = new Node(); + node.data = obj; + node.next = null; + Node r = head; - for (int i = 0; i < index; i++) - r = r.next; + while (r.next != null) r = r.next; + + r.next = node; + + size++; - return r.data; } /** - * ɾ��ָ��λ�ýڵ㲢������ֵ + * ������������Ԫ�ذ�˳����뵥���� + * + * @param c Ҫ���뵥����������Ԫ�صļ��� + * */ - public Object remove(int index) { - if (index > size) - throw new IndexOutOfBoundsException(); + public void addAll(Collection c) { - Node node = new Node(); + Iterator iter = (Iterator) c.iterator(); - Node r = head; - for (int i = 0; i < index - 1; i++) - r = r.next; - - node = r.next; - r.next = node.next; - node.next = null; - size--; - return node.data; - } - - /** - * ���������ij��� - */ - public int size() { - return size; + while (iter.hasNext()) { + addLast(iter.next()); + } } /** - * ����������ʼλ�ò���ڵ� - * - * @param obj + * ��ȡָ��λ�õĽڵ�ֵ */ - public void addFirst(Object obj) { - Node node = new Node(); - node.data = obj; - node.next = head; - head = node; - size++; + public Object get(int index) { + // rangCheck(index); + + return getPreNode(index).next.data; } /** - * ������β������ڵ� - * - * @param obj + * ɾ��ָ��λ�ýڵ㣬�����ؽڵ�ֵ */ - public void addLast(Object obj) { - add(obj); + public Object remove(int index) { + rangCheck(index); + + Node r = getPreNode(index); + + Object result = r.next.data; + + r.next = r.next.next; + size--; + return result; } - + /** - * ɾ�������ĵ�һ���ڵ� + * ɾ����������һ���ڵ㣬�����ؽڵ�ֵ * - * @return ��ɾ���ڵ��ֵ + * @return ��һ���ڵ��ֵ */ public Object removeFirst() { - Node node = new Node(); - node = head; - head = head.next; - size--; - - return node.data; + return remove(0); } - + /** - * ɾ�����������һ���ڵ� + * ɾ�����������һ���ڵ㣬�����ؽڵ�ֵ * - * @return ��ɾ���ڵ��ֵ + * @return ���һ���ڵ��ֵ */ public Object removeLast() { return remove(size - 1); } + // ��ȡָ��λ�õ�ǰ����㲢���� + private Node getPreNode(int index) { + rangCheck(index); + + if (index == 0) { + return head; + } else { + Node r = head; + + for (int i = 0; i < index; i++) + r = r.next; + + return r; + } + + } + + /** + * ���ص������ij��� + */ + public int size() { + return size; + } + /** * ������ * @@ -159,7 +169,7 @@ public Iterator iterator() { // �������ڲ��� private class Iter implements Iterator { - int current; + int current = 0; @Override public boolean hasNext() { @@ -170,8 +180,7 @@ public boolean hasNext() { public Object next() { int i = current; - if (i >= size) - throw new IndexOutOfBoundsException(); + rangCheck(i); current++; @@ -179,10 +188,27 @@ public Object next() { } } + + /** + * ����Ƿ�Խ�� + * + * @param index + */ + private void rangCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } - // �ڵ��ඨ�� private static class Node { Object data; Node next; + + public Node() { + super(); + this.data = null; + this.next = null; + } + + } -} +} \ No newline at end of file From 0123ea9a424d940ad198eb75dcc445c17ca81886 Mon Sep 17 00:00:00 2001 From: bobi <120549547@qq.com> Date: Sat, 25 Feb 2017 16:39:44 +0800 Subject: [PATCH 094/432] bobi --- group04/120549547/my.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group04/120549547/my.txt diff --git a/group04/120549547/my.txt b/group04/120549547/my.txt new file mode 100644 index 0000000000..3da1ec26e9 --- /dev/null +++ b/group04/120549547/my.txt @@ -0,0 +1 @@ +HelloWorld From 603121f900910219ad5d5391b15b89e8a0ede807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E9=98=B3-=E8=93=9DK?= <399258474@qq.com> Date: Sat, 25 Feb 2017 17:06:40 +0800 Subject: [PATCH 095/432] 2/19-2/16 homework --- group05/399258474/.classpath | 7 ++ group05/399258474/.gitignore | 1 + group05/399258474/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 71 ++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 104 ++++++++++++++++++ .../399258474/src/com/coding/basic/List.java | 9 ++ .../399258474/src/com/coding/basic/Queue.java | 35 ++++++ .../399258474/src/com/coding/basic/Stack.java | 39 +++++++ group05/399258474/src/test/BasicTest.java | 78 +++++++++++++ 12 files changed, 411 insertions(+) create mode 100644 group05/399258474/.classpath create mode 100644 group05/399258474/.gitignore create mode 100644 group05/399258474/.project create mode 100644 group05/399258474/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/399258474/src/com/coding/basic/ArrayList.java create mode 100644 group05/399258474/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/399258474/src/com/coding/basic/Iterator.java create mode 100644 group05/399258474/src/com/coding/basic/LinkedList.java create mode 100644 group05/399258474/src/com/coding/basic/List.java create mode 100644 group05/399258474/src/com/coding/basic/Queue.java create mode 100644 group05/399258474/src/com/coding/basic/Stack.java create mode 100644 group05/399258474/src/test/BasicTest.java diff --git a/group05/399258474/.classpath b/group05/399258474/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group05/399258474/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/399258474/.gitignore b/group05/399258474/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group05/399258474/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/399258474/.project b/group05/399258474/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group05/399258474/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/399258474/.settings/org.eclipse.jdt.core.prefs b/group05/399258474/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group05/399258474/.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/group05/399258474/src/com/coding/basic/ArrayList.java b/group05/399258474/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..5030b8d001 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[2]; + + public void add(Object o){ + int len = elementData.length; + if(size >= len){ + Object[] new_elmentData = new Object[len*2]; + System.arraycopy(elementData, 0, new_elmentData, 0, size); + elementData = new_elmentData; + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object[] new_elementData = new Object[size+1]; + System.arraycopy(elementData, 0, new_elementData, 0, index); + System.arraycopy(elementData, index, new_elementData, index+1, size-index); + new_elementData[index] = o; + elementData = new_elementData; + size++; + } + + public Object get(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object oldElement = elementData[index]; + if((index+1) != size){ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + elementData[size-1] = null; + size --; + return oldElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + @Override + public String toString() { + String s = "{"; + for (int i = 0; i < size; i++) { + if(i == (size -1)){ + s += elementData[i] + "}"; + }else{ + s += elementData[i]+","; + } + } + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/BinaryTreeNode.java b/group05/399258474/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/Iterator.java b/group05/399258474/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/LinkedList.java b/group05/399258474/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ff69d8d341 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/LinkedList.java @@ -0,0 +1,104 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head = new Node(new Object(),null,null); + } + + public void add(Object o){ + Node last = head; + for (int i = 0; i < size; i++) { + last = last.next; + } + Node newNode = new Node(o,null,last); + last.next = newNode; + size++; + } + public void add(int index , Object o){ + Node oldNode = getNode(index); + Node newNode = new Node(o, oldNode, oldNode.prev); + oldNode.prev.next = newNode; + oldNode.prev = newNode; + size ++; + } + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + private Node getNode(int index){ + Node n = head.next; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Object remove(int index){ + Node node = getNode(index); + Object o =node.data; + Node prevNode = node.prev; + Node nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + node.next = node.prev =null; + node.data = null; + size --; + return o; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + Object o = remove(0); + return o; + } + public Object removeLast(){ + Object o = remove(size); + return o; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + + public Node(Object o,Node next,Node prev){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + @Override + public String toString() { + String s = "{"; + Node n = head; + for (int i = 0; i < size; i++) { + n = n.next; + if(i == (size -1)){ + s += n.data; + }else{ + s += n.data + ","; + } + } + s += "}"; + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/List.java b/group05/399258474/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/Queue.java b/group05/399258474/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a0bb03e6f2 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = list.get(0); + list.remove(0); + return o; + } + + public boolean isEmpty(){ + if(size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group05/399258474/src/com/coding/basic/Stack.java b/group05/399258474/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d137996b6 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group05/399258474/src/test/BasicTest.java b/group05/399258474/src/test/BasicTest.java new file mode 100644 index 0000000000..7339881525 --- /dev/null +++ b/group05/399258474/src/test/BasicTest.java @@ -0,0 +1,78 @@ +package test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class BasicTest { + + @Test + public void ArrayListTest() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + System.out.println(list); + list.add(1, 99); + System.out.println(list); + list.remove(1); + System.out.println(list); + } + + @Test + public void LinkedListTest(){ + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + System.out.println(l); + l.add(1, 99); + System.out.println(l); + l.remove(1); + System.out.println(l); + System.out.println(l.size()); + } + + @Test + public void StackTest(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("空"); + }else{ + System.out.println("非空"); + + } + System.out.println(s.peek()); + s.pop(); + System.out.println(s); + s.pop(); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("空"); + }else{ + System.out.println("非空"); + + } + s.pop(); + } + + @Test + public void QueueTest(){ + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + System.out.println(q); + q.deQueue(); + System.out.println(q); + q.enQueue(3); + System.out.println(q); + System.out.println(q.size()); + } + +} From 0b24942964290e05b390b3711b08700fcd39fe1d Mon Sep 17 00:00:00 2001 From: FengYang <447809161@qq.com> Date: Sat, 25 Feb 2017 17:10:31 +0800 Subject: [PATCH 096/432] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwjcc969/coding2017/basic/ArrayList.java | 62 ++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 +++++ .../hwjcc969/coding2017/basic/Iterator.java | 7 ++ .../hwjcc969/coding2017/basic/LinkedList.java | 117 ++++++++++++++++++ .../hwjcc969/coding2017/basic/List.java | 9 ++ .../hwjcc969/coding2017/basic/Queue.java | 27 ++++ .../hwjcc969/coding2017/basic/Stack.java | 31 +++++ 7 files changed, 285 insertions(+) create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java create mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..c2acf20e4d --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java @@ -0,0 +1,62 @@ +package com.github.hwjcc969.coding2017.basic; + +import javax.lang.model.element.Element; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (o == null){ + throw new NullPointerException("空对象"); + } + if (size >= 100){ + throw new RuntimeException("越界"); + } + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (o == null){ + throw new NullPointerException("空对象"); + } + if (index >= 99){ + throw new RuntimeException("越界"); + } + for (int i = size; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index >= 100 || index < 0){ + throw new RuntimeException("越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index >= 100 || index < 0){ + throw new RuntimeException("越界"); + } + Object o = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return null; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..0381f88c69 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.hwjcc969.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..0e8394844b --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.hwjcc969.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..d000967c19 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java @@ -0,0 +1,117 @@ +package com.github.hwjcc969.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + Node newNode = new Node(o); + if (head == null) { + head = newNode; + } + else{ + Node last = head.next; + while (last != null){ + last = last.next; + } + last = newNode; + } + size++; + + } + public void add(int index , Object o){ + Node dummy = new Node(); + dummy.next = head; + Node pre = dummy; + while (index > 0) { + pre = pre.next; + } + Node cur = new Node(o); + cur.next = pre.next; + pre.next = cur; + size++; + } + public Object get(int index){ + Node cur = head; + while (index > 0 && cur != null) { + cur = cur.next; + } + return cur; + } + public Object remove(int index){ + Node dummy = new Node(); + dummy.next = head; + Node pre = dummy; + while (index > 0) { + pre = pre.next; + } + Node cur = pre.next; + Node next = pre.next.next; + pre.next = next; + size--; + return cur; + + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node firstNode = new Node(o); + firstNode.next = head; + head = firstNode; + } + public void addLast(Object o){ + Node pre = new Node(); + while (pre.next != null){ + pre = pre.next; + } + Node lastNode = new Node(o); + pre.next = lastNode; + } + public Object removeFirst(){ + Node dummy = new Node(); + dummy.next = head; + dummy.next = head.next; + return dummy.next; + } + public Object removeLast(){ + Node pre = new Node(); + while (pre.next.next != null) { + pre = pre.next; + } + Node lastNode = pre.next.next; + pre.next = null; + return lastNode; + + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + public Node(Object o) { + // TODO Auto-generated constructor stub + } + public Node() { + // TODO Auto-generated constructor stub + } + Object data; + Node next; + + } + + + public boolean isEmpty() { + // TODO Auto-generated method stub + if (size() == 0){ + return false; + } + return true; + } + + +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java new file mode 100644 index 0000000000..56d67bdf29 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.hwjcc969.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java new file mode 100644 index 0000000000..f7cb7b1ad4 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java @@ -0,0 +1,27 @@ +package com.github.hwjcc969.coding2017.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue(){ + if (!list.isEmpty()){ + return list.removeFirst(); + } + return "空队列"; + + } + + public boolean isEmpty(){ + if (list.size() == 0){ + return false; + } + return true; + } + + public int size(){ + return list.size(); + } +} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java new file mode 100644 index 0000000000..a4229a7d12 --- /dev/null +++ b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.hwjcc969.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + if (elementData.size() <= 99) + elementData.add(o); + size++; + + } + + public Object pop(){ + size--; + return elementData.remove(elementData.size() - 1); + + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + if (size == 0) + return false; + return true; + } + public int size(){ + return size; + } +} From 37595cec0ff849be16dcc6a2136ec680ba94b426 Mon Sep 17 00:00:00 2001 From: Mark Ryan <676615845@qq.com> Date: Sat, 25 Feb 2017 17:14:03 +0800 Subject: [PATCH 097/432] default --- group14/676615845/algo/pom.xml | 18 ++ .../algo/src/main/java/algo/BinarySearch.java | 17 ++ .../src/main/java/com/coding/basic/Array.java | 16 ++ .../main/java/com/coding/basic/ArrayList.java | 73 ++++++++ .../java/com/coding/basic/BinaryTreeNode.java | 41 +++++ .../main/java/com/coding/basic/Iterator.java | 7 + .../java/com/coding/basic/LinkedList.java | 164 ++++++++++++++++++ .../src/main/java/com/coding/basic/List.java | 9 + .../src/main/java/com/coding/basic/Queue.java | 22 +++ .../src/main/java/com/coding/basic/Stack.java | 26 +++ .../src/test/java/algo/BinarySearchTest.java | 26 +++ .../java/com/coding/basic/ArrayListTest.java | 146 ++++++++++++++++ .../test/java/com/coding/basic/ArrayTest.java | 30 ++++ .../com/coding/basic/BinaryTreeNodeTest.java | 64 +++++++ .../java/com/coding/basic/LinkedListTest.java | 115 ++++++++++++ .../test/java/com/coding/basic/QueueTest.java | 58 +++++++ .../test/java/com/coding/basic/StackTest.java | 70 ++++++++ 17 files changed, 902 insertions(+) create mode 100644 group14/676615845/algo/pom.xml create mode 100644 group14/676615845/algo/src/main/java/algo/BinarySearch.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Array.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/List.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Queue.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Stack.java create mode 100644 group14/676615845/algo/src/test/java/algo/BinarySearchTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java diff --git a/group14/676615845/algo/pom.xml b/group14/676615845/algo/pom.xml new file mode 100644 index 0000000000..d2e1f258a0 --- /dev/null +++ b/group14/676615845/algo/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.mark + algo + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/group14/676615845/algo/src/main/java/algo/BinarySearch.java b/group14/676615845/algo/src/main/java/algo/BinarySearch.java new file mode 100644 index 0000000000..3144a37181 --- /dev/null +++ b/group14/676615845/algo/src/main/java/algo/BinarySearch.java @@ -0,0 +1,17 @@ +package algo; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by mark on 17/2/23. + */ +public class BinarySearch { + + public static int rank(int key, int[] a) { + List list = new ArrayList(); + list = new LinkedList(); + return -1; + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Array.java b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java new file mode 100644 index 0000000000..44afce6c25 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * Created by mark on 17/2/24. + */ +public class Array { + + public static Object[] grow(Object[] src, int size) { + return Arrays.copyOf(src, src.length + size); +// Object[] target = new Object[src.length + size]; +// System.arraycopy(src, 0, target, 0, src.length); + + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..148bd6da96 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size; // ArrayList 中的实际元素个数 + private Object[] elementData; + + public ArrayList() { + size = 0; + elementData = new Object[100]; + } + + public void add(Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + + if (index > size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size) throw new ArrayIndexOutOfBoundsException(); + return elementData[index]; + } + + public Object remove(int index){ + + if (index >= size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + Object result = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return result; + } + + public int size() { + return size; + } + + public Iterator iterator(){ + + return new Iterator() { + + private int next = 0; // 下一个返回元素所在的位置 + + public boolean hasNext() { + return next < size; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + return elementData[next++]; + } + + public Object remove() { + if (next <= 0) throw new IllegalStateException(); + return ArrayList.this.remove(--next); + } + }; + } + +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..5ddd6f5f8a --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable { + + 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; + } + + public int compareTo(Object obj) { + if (obj == null || obj.getClass() != Integer.class) throw new IllegalArgumentException(); + return Integer.compare(((Integer) data).intValue(), ((Integer) obj).intValue()); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..a0b91b1a82 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); + Object remove(); +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1548a0c23 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node first = null; + private Node last = null; + private int size = 0; + + public void add(Object o){ + Node node = new Node(o); + if (first == null) { + first = node; + } else { + last.next = node; + node.prev = last; + } + last = node; + size++; + } + + public void add(int index , Object o) { + if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException(); + + Node node = new Node(o); + + if (first == null) { + first = node; + last = node; + } else { + if (index == 0) { + node.next = first; + first.prev = node; + first = node; + } else if (index == size) { + last.next = node; + node.prev = last; + last = node; + } else { + Node temp = first; + while (--index > 0) { + temp = temp.next; + } + node.next = temp.next; + temp.next.prev = node; + temp.next = node; + node.prev = temp; + } + } + size++; + } + public Object get(int index){ + if (index < 0 || index > size - 1) throw new ArrayIndexOutOfBoundsException(); + Node node = first; + while (index-- > 0) { + node = node.next; + } + return node.data; + } + + public Object remove(int index){ + if (index < 0 || index >= size) throw new ArrayIndexOutOfBoundsException(); + + Node node = null; + if (index == 0) { + node = first; + if (size == 1) { + first = null; + last = null; + } else { + first = first.next; + first.prev = null; + } + } else if (index == size - 1) { + node = last; + last = last.prev; + last.next = null; + } else { + node = first; + Node temp = null; + while (index-- > 0) { + node = node.next; + } + temp = node.prev; + temp.next = node.next; + node.next.prev = temp; + } + size--; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object obj){ + add(0, obj); + } + + public void addLast(Object obj){ + add(size, obj); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + } + + public Iterator iterator(){ + + if (first == null || last == null) throw new IllegalStateException(); + + return new InnerIterator(); + } + + private class InnerIterator implements Iterator { + + private Node nextNode = first; + + public boolean hasNext() { + return nextNode != null; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + Node node = nextNode; + nextNode = nextNode.next; + return node.data; + } + + public Object remove() { + if (nextNode == first) throw new IllegalStateException(); + + Node node = nextNode.prev; + if (nextNode == first.next) { + first = nextNode; + first.prev = null; + } else if (nextNode == null) { + node = last; + last = last.prev; + last.next = null; + } else { + node.prev = node.next; + node.next.prev = node.prev; + } + return node.data; + } + } + + private static class Node{ + + Object data; + Node next; + Node prev; + + public Node(Object data) { + this.data = data; + next = null; + prev = null; + } + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/List.java b/group14/676615845/algo/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..6d380288e5 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +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/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..60345ca4f6 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.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(); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a9f0d009f3 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,26 @@ +package com.coding.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(); + } +} diff --git a/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java new file mode 100644 index 0000000000..6308e23251 --- /dev/null +++ b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java @@ -0,0 +1,26 @@ +package algo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class BinarySearchTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void rank() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..64fd31eb33 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayListTest { + + private static ArrayList list; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @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("hello"); + Assert.assertEquals(1, list.size()); + + // 可以自动扩容 + for (int i=0; i<150; i++) { + list.add(i); + } + Assert.assertEquals(151, list.size()); + Assert.assertTrue(149 == ((Integer) list.get(150)).intValue()); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(0, "zero"); + list.add(50, "fifty"); + list.add(102, "102"); + Assert.assertEquals("zero", list.get(0)); + Assert.assertEquals("fifty", list.get(50)); + Assert.assertEquals("102", list.get(102)); + + list = new ArrayList(); + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(100, "100"); + Assert.assertEquals("100", list.get(100)); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.add(102, "102"); + } + + @Test + public void get() throws Exception { + list.add("hello"); + Object obj = list.get(0); + Assert.assertTrue("hello".equals(obj)); + } + + @Test + public void remove() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(99, ((Integer) list.remove(99)).intValue()); + Assert.assertEquals(99, list.size()); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.remove(100); + list.remove(-1); + } + + @Test + public void size() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(100, list.size()); + list.add("hello"); + Assert.assertEquals(101, list.size()); + } + + @Test + public void iterator() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + Assert.assertNotNull(iterator.next()); + } +// Assert.assertNotNull(iterator.next()); + + Object[] target = new Object[list.size()]; + int i = 0; + iterator = list.iterator(); + while (iterator.hasNext()) { + target[i++] = iterator.next(); + } + Assert.assertEquals(100, target.length); + + for (int j = 0; j < 100; j++) { + Assert.assertEquals(j, ((Integer) target[j]).intValue()); + } + + // 测试迭代器的 remove() 方法 + list = new ArrayList(); + for (int k=0; k<100; k++) { + list.add(k); + } + iterator = list.iterator(); +// thrown.expect(IllegalStateException.class); +// iterator.remove(); + + iterator.next(); + Object i0 = iterator.remove(); + Assert.assertEquals(0, ((Integer) i0).intValue()); + + for (int j=0; j<50; j++) { + iterator.next(); + } + Object i50 = iterator.remove(); + Assert.assertEquals(50, ((Integer)i50).intValue()); + + for (int j = 0; j < 48; j++) { + iterator.next(); + } + Object i99 = iterator.remove(); + Assert.assertEquals(98, ((Integer)i99).intValue()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java new file mode 100644 index 0000000000..579b98c585 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayTest { + private Object[] src; + + @Before + public void setUp() throws Exception { + src = new Object[10]; + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void grow() throws Exception { + src = Array.grow(src, 20); + Assert.assertEquals(30, src.length); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..b3d3c7d557 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode tree; + + @Before + public void setUp() throws Exception { + tree = new BinaryTreeNode(); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void getData() throws Exception { + + } + + @Test + public void setData() throws Exception { + + } + + @Test + public void getLeft() throws Exception { + + } + + @Test + public void setLeft() throws Exception { + + } + + @Test + public void getRight() throws Exception { + + } + + @Test + public void setRight() throws Exception { + + } + + @Test + public void insert() throws Exception { + tree.insert("8"); + tree.insert("1"); + tree.insert("2"); + tree.insert("10"); + tree.insert("4"); + tree.insert("34"); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..24c2a84367 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,115 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class LinkedListTest { + + private LinkedList linkedList; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @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"); + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals("first", linkedList.get(0)); + + linkedList.add("second"); + linkedList.add("third"); + Assert.assertEquals("third", linkedList.get(2)); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<10; i++) { + linkedList.add(i); + } + linkedList.add(5, "Five"); + Assert.assertEquals("Five", linkedList.get(5)); + Assert.assertEquals(11, linkedList.size()); + + linkedList.add(0, "Zero"); + Assert.assertEquals("Zero", linkedList.get(0)); + + linkedList.add(12, "Last"); + Assert.assertEquals("Last", linkedList.get(12)); + } + + @Test + public void get() throws Exception { + + linkedList.add("hello"); + Assert.assertEquals("hello", linkedList.get(0)); + + linkedList.add("two"); + Assert.assertEquals("two", linkedList.get(1)); + + linkedList = new LinkedList(); + thrown.expect(ArrayIndexOutOfBoundsException.class); + linkedList.get(0); + } + + @Test + public void remove() throws Exception { + Object data = null; + + for (int i=0; i<10; i++) { + linkedList.add("" + i); + } + + data = linkedList.remove(0); + Assert.assertEquals("0", data); + + data = linkedList.remove(8); + Assert.assertEquals("9", data); + + data = linkedList.remove(4); + Assert.assertEquals("5", data); + } + + @Test + public void size() throws Exception { + linkedList.add(0); + Assert.assertEquals(1, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..76ecb28a48 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class QueueTest { + + private 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("first"); + queue.enQueue("second"); + queue.enQueue("third"); + Assert.assertEquals("first", queue.deQueue()); + Assert.assertEquals("second", queue.deQueue()); + Assert.assertEquals("third", queue.deQueue()); + } + + @Test + public void deQueue() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, queue.isEmpty()); + queue.enQueue("first"); + Assert.assertEquals(false, queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, queue.size()); + queue.enQueue("first"); + Assert.assertEquals(1, queue.size()); + queue.deQueue(); + Assert.assertEquals(0, queue.size()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..21192c9036 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,70 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class StackTest { + + private 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("first"); + stack.push("second"); + Assert.assertEquals("second", stack.pop()); + Assert.assertEquals("first", stack.pop()); + Assert.assertEquals(0, stack.size()); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + stack.push("first"); + stack.push("second"); + Assert.assertEquals("second", stack.peek()); + stack.pop(); + Assert.assertEquals("first", stack.peek()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, stack.isEmpty()); + stack.push("first"); + Assert.assertEquals(false, stack.isEmpty()); + stack.pop(); + Assert.assertEquals(true, stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, stack.size()); + stack.push("first"); + Assert.assertEquals(1, stack.size()); + stack.push("second"); + Assert.assertEquals(2, stack.size()); + stack.pop(); + stack.pop(); + Assert.assertEquals(0, stack.size()); + } + +} \ No newline at end of file From 988fb244e160233e8fff14cee59df022f876903e Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sat, 25 Feb 2017 17:47:08 +0800 Subject: [PATCH 098/432] =?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=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group08/769638826/.gitignore | 5 +++++ group08/769638826/.idea/vcs.xml | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 group08/769638826/.gitignore create mode 100644 group08/769638826/.idea/vcs.xml diff --git a/group08/769638826/.gitignore b/group08/769638826/.gitignore new file mode 100644 index 0000000000..c012ac9a02 --- /dev/null +++ b/group08/769638826/.gitignore @@ -0,0 +1,5 @@ +# Created by .ignore support plugin (hsz.mobi) +.gitignore +.idea/ +769638826.iml +target/ \ No newline at end of file diff --git a/group08/769638826/.idea/vcs.xml b/group08/769638826/.idea/vcs.xml new file mode 100644 index 0000000000..b2bdec2d71 --- /dev/null +++ b/group08/769638826/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From ed2ddc1eaeed7e05e44da3e04827be550480cb99 Mon Sep 17 00:00:00 2001 From: Kevin_win10 Date: Sat, 25 Feb 2017 17:57:04 +0800 Subject: [PATCH 099/432] Simpe collection implemented --- .../2082945465/week01/src/MyArrayList.java | 15 ++- .../2082945465/week01/src/MyLinkedList.java | 94 +++++++++++++++++-- group04/2082945465/week01/src/MyList.java | 4 +- group04/2082945465/week01/src/MyQueue.java | 13 +++ group04/2082945465/week01/src/MyStack.java | 19 ++++ 5 files changed, 132 insertions(+), 13 deletions(-) diff --git a/group04/2082945465/week01/src/MyArrayList.java b/group04/2082945465/week01/src/MyArrayList.java index 1de76354fd..798475771c 100644 --- a/group04/2082945465/week01/src/MyArrayList.java +++ b/group04/2082945465/week01/src/MyArrayList.java @@ -12,13 +12,13 @@ public class MyArrayList implements MyList { private Object[] elementData = new Object[100]; @Override - public void add(Object o) { + public void add(Object obj) { this.checkCapacity(size+1); - elementData[size++] = o; + elementData[size++] = obj; } @Override - public void add(int index, Object o) { + public void add(int index, Object obj) { this.validIndex(index); this.checkCapacity(size+1); if(index < size){ @@ -26,7 +26,7 @@ public void add(int index, Object o) { this.elementData[i] = this.elementData[i-1]; } }else{ - this.elementData[index] = o; + this.elementData[index] = obj; } this.size++; } @@ -69,4 +69,11 @@ private void validIndex(int inputIndex) { throw new RuntimeException("Index: " + inputIndex + " out of bounds( " + size +" )"); } } + + public boolean isEmpty() { + if (size() == 0){ + return false; + } + return true; + } } diff --git a/group04/2082945465/week01/src/MyLinkedList.java b/group04/2082945465/week01/src/MyLinkedList.java index 8549f1b0c8..69f518ba0f 100644 --- a/group04/2082945465/week01/src/MyLinkedList.java +++ b/group04/2082945465/week01/src/MyLinkedList.java @@ -4,28 +4,108 @@ * Created by Yang on 2/25/2017. */ public class MyLinkedList implements MyList { - @Override - public void add(Object o) { - } + private int size = 0; + + private Node header; @Override - public void add(int index, Object o) { + public void add(Object obj) { + Node newNode = new Node(obj); + newNode.next = header; + header = newNode; + size++; + } + @Override + public void add(int index, Object obj) { + this.validIndex(index); + Node current = this.getCurrentNode(index); + Node newNode = new Node(obj); + newNode.next = current.next; + current.next = newNode; + size++; } @Override public Object get(int index) { - return null; + this.validIndex(index); + Node node = getCurrentNode(index); + return node.data; } @Override public Object remove(int index) { - return null; + this.validIndex(index); + + if(index == 0) return removeFirst(); + if(index == size -1) return removeLast(); + + Node node = getCurrentNode(index+1); + node.next = node.next.next; + size--; + return node.data; } @Override public int size() { - return 0; + return size; + } + + private static class Node { + Object data; + Node next; + public Node(){ + } + public Node(Object data) { + this.data = data; + } + } + + private void validIndex(int inputIndex) { + if(inputIndex > size || inputIndex < 0){ + throw new RuntimeException("Index: " + inputIndex + " out of bounds( " + size +" )"); + } + } + + private Node getCurrentNode(int index) { + Node current = header; + for(int i = 0; i < size-index -1; i++){ + current = current.next; + } + return current; + } + + public Object removeFirst() { + Node temp = new Node(); + temp.next = header; + temp.next = header.next; + return temp.next; + } + + private Object removeLast() { + Node preNode = new Node(); + while (preNode.next.next != null) { + preNode = preNode.next; + } + Node lastNode = preNode.next.next; + preNode.next = null; + return lastNode; + } + + public void addLast(Object o){ + Node preNode = new Node(); + while (preNode.next != null){ + preNode = preNode.next; + } + Node lastNode = new Node(o); + preNode.next = lastNode; + } + + public boolean isEmpty() { + if (size() == 0){ + return false; + } + return true; } } diff --git a/group04/2082945465/week01/src/MyList.java b/group04/2082945465/week01/src/MyList.java index 0b67820e73..578c86b803 100644 --- a/group04/2082945465/week01/src/MyList.java +++ b/group04/2082945465/week01/src/MyList.java @@ -4,8 +4,8 @@ * Created by Yang on 2/25/2017. */ public interface MyList { - public void add(Object o); - public void add(int index, Object o); + public void add(Object obj); + public void add(int index, Object obj); public Object get(int index); public Object remove(int index); public int size(); diff --git a/group04/2082945465/week01/src/MyQueue.java b/group04/2082945465/week01/src/MyQueue.java index 497602bd3a..87dc900c79 100644 --- a/group04/2082945465/week01/src/MyQueue.java +++ b/group04/2082945465/week01/src/MyQueue.java @@ -4,4 +4,17 @@ * Created by Yang on 2/25/2017. */ public class MyQueue { + private MyLinkedList queue = new MyLinkedList(); + + public void enQuee(Object obj){ + queue.addLast(obj); + } + + public Object deQuee(Object obj){ + return queue.removeFirst(); + } + + public boolean isEmpty() { + return queue.isEmpty(); + } } diff --git a/group04/2082945465/week01/src/MyStack.java b/group04/2082945465/week01/src/MyStack.java index 831af93ed7..3427d86e93 100644 --- a/group04/2082945465/week01/src/MyStack.java +++ b/group04/2082945465/week01/src/MyStack.java @@ -1,7 +1,26 @@ package src; +import java.util.EmptyStackException; + /** * Created by Yang on 2/25/2017. */ public class MyStack { + + private MyArrayList stack = new MyArrayList(); + + public void push(Object obj){ + stack.add(obj); + } + + public Object pop(){ + if (stack.size()==0){ + throw new EmptyStackException(); + } + return stack.remove(stack.size()-1); + } + + public boolean isEmpty(){ + return stack.isEmpty(); + } } From f999553fef95ce9c6f5d270bafc2cc27c15e8080 Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sat, 25 Feb 2017 18:22:27 +0800 Subject: [PATCH 100/432] =?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 --- group08/769638826/pom.xml | 37 ++++ .../main/java/com/coding/basic/ArrayList.java | 102 ++++++++++ .../main/java/com/coding/basic/Iterator.java | 13 ++ .../java/com/coding/basic/LinkedList.java | 190 ++++++++++++++++++ .../src/main/java/com/coding/basic/List.java | 19 ++ .../src/main/java/com/coding/basic/Queue.java | 26 +++ .../src/main/java/com/coding/basic/Stack.java | 32 +++ .../java/com/coding/basic/ArrayListTest.java | 74 +++++++ .../java/com/coding/basic/LinkedListTest.java | 10 + .../test/java/com/coding/basic/QueueTest.java | 10 + .../test/java/com/coding/basic/StackTest.java | 9 + 11 files changed, 522 insertions(+) create mode 100644 group08/769638826/pom.xml create mode 100644 group08/769638826/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group08/769638826/src/main/java/com/coding/basic/Iterator.java create mode 100644 group08/769638826/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group08/769638826/src/main/java/com/coding/basic/List.java create mode 100644 group08/769638826/src/main/java/com/coding/basic/Queue.java create mode 100644 group08/769638826/src/main/java/com/coding/basic/Stack.java create mode 100644 group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java create mode 100644 group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java create mode 100644 group08/769638826/src/test/java/com/coding/basic/QueueTest.java create mode 100644 group08/769638826/src/test/java/com/coding/basic/StackTest.java diff --git a/group08/769638826/pom.xml b/group08/769638826/pom.xml new file mode 100644 index 0000000000..68c1b3095d --- /dev/null +++ b/group08/769638826/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.coding2017 + coding + 1.0-SNAPSHOT + + + UTF-8 + 1.7 + + + + + junit + junit + 4.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.buildEncoding} + + + + + \ No newline at end of file diff --git a/group08/769638826/src/main/java/com/coding/basic/ArrayList.java b/group08/769638826/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8ec793ba3f --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,102 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:23:30 + */ +public class ArrayList implements List { + private int size = 0; + private static final int DEFAULT_SIZE = 16; + private Object[] elementData = null; + private int index; + + public ArrayList() { + elementData = new Object[DEFAULT_SIZE]; + } + + public ArrayList(final int size) { + elementData = new Object[size]; + } + + public void add(Object o) { + //如果当前元素个数大于数组长度的2/3 + if (size() > elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index++] = o; + size++; + } + + public void add(int index, Object o) { + checkParam(index); + + //如果当前元素个数大于数组长度的2/3 + if (size() > elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkParam(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkParam(index); + + Object o = elementData[index]; + elementData[index] = null; + size--; + return o; + } + + private void raiseArray() { + Object[] newElementData = Arrays.copyOf(elementData, size() * 2); + elementData = newElementData; + } + + private void checkParam(int index) { + if (index < 0 || index > elementData.length - 1) { + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private class ListIterator implements Iterator{ + @Override + public boolean hasNext() { + return size != 0; + } + + public void remove(){ + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + + //TODO + return null; + } + } +} diff --git a/group08/769638826/src/main/java/com/coding/basic/Iterator.java b/group08/769638826/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ce3da0d118 --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日16:25:42 + */ +public interface Iterator { + public boolean hasNext(); + + public Object next(); +} diff --git a/group08/769638826/src/main/java/com/coding/basic/LinkedList.java b/group08/769638826/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..514cef0dc7 --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,190 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:57:58 + */ +public class LinkedList implements List { + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + @Override + public void add(Object o) { + if (head == null) { + Node newNode = new Node(); + newNode.data = o; + head = newNode; + } + + Node oldhead = head; + head = new Node(); + head.data = o; + head.next = oldhead; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + newNode.data = o; + + if (head == null) { + + } + + if (index < 1 || index > size + 1) { + throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1); + } + + if (index == 1) { + newNode.next = head; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + newNode.next = currentNode.next; + currentNode.next = newNode; + } + } + + @Override + public Object get(int index) { + if (head == null) { + return null; + } + + if (index == 1) { + return head.next.data; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + + return currentNode.next.data; + } + } + + @Override + public Object remove(int index) { + Object result = null; + + if (index < 1 || index > size) { + throw new IllegalArgumentException("invalid index, it's should be 1 and " + size); + } + + if (index == 1) { + Node currentNode = head.next; + head = null; + return currentNode; + } else { + Node prevNode = head; + + int count = 1; + while (count < index - 1) { + prevNode = prevNode.next; + count++; + } + Node currentNode = prevNode.next; + prevNode.next = currentNode.next; + result = currentNode.data; + currentNode = null; + } + + return result; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + add(1, o); + } + + public void addLast(Object o) { + add(size + 1, o); + } + + public Object removeFirst() { + return remove(1); + } + + public Object removeLast() { + return remove(size); + } + + public Iterator iterator() { + return new ListIterator(); + } + + public void print() { + if (head == null) { + System.out.println("No elements in the list!"); + } + + Node currentNode = head; + while (currentNode != null) { + System.out.println(currentNode.data + "->"); + currentNode = currentNode.next; + } + + System.out.println(); + } + + public int length() { + int count = 0; + + Node currentNode = head; + while (currentNode != null) { + count++; + currentNode = currentNode.next; + } + + return count; + } + + private static class Node { + Object data; + Node next; + } + + private class ListIterator implements Iterator { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + Object o = current.data; + current = current.next; + return o; + } + } +} diff --git a/group08/769638826/src/main/java/com/coding/basic/List.java b/group08/769638826/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..e9eba47da6 --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/List.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:54:33 + */ +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/group08/769638826/src/main/java/com/coding/basic/Queue.java b/group08/769638826/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..abb7e57112 --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:40:02 + */ +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.get(size()); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/src/main/java/com/coding/basic/Stack.java b/group08/769638826/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..46f80bc25d --- /dev/null +++ b/group08/769638826/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:34:22 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.get(1); + elementData.remove(1); + return o; + } + + public Object peek(){ + return elementData.get(1); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java b/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..199e3d14cb --- /dev/null +++ b/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by huitailang on 17/2/25. + * test arraylist + */ +public class ArrayListTest { + ArrayList arrayList = null; + + @Before + public void setUp() { + arrayList = new ArrayList(); + } + + @Test + public void testArrayLength() { + int[] array = new int[10]; + Assert.assertEquals(10, array.length); + } + + @Test + public void testAddElement() { + arrayList.add(11); + Assert.assertEquals(11, arrayList.get(0)); + printElementSize(arrayList); + + for (int i = 0; i < 18; i++) { + + } + } + + @Test + public void testAriseArray() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test + public void testRemoveElement() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + arrayList.remove(17); + + Assert.assertEquals(17, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testInValidGet() { + arrayList.get(19); + } + + private void printElementSize(ArrayList arrayList) { + System.out.println("array size => " + arrayList.size()); + } +} diff --git a/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java b/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..786e5d93a7 --- /dev/null +++ b/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:33:40 + */ +public class LinkedListTest { + +} diff --git a/group08/769638826/src/test/java/com/coding/basic/QueueTest.java b/group08/769638826/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..83ee1ff30a --- /dev/null +++ b/group08/769638826/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:48 + */ +public class QueueTest { + +} diff --git a/group08/769638826/src/test/java/com/coding/basic/StackTest.java b/group08/769638826/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..5380bbaa0c --- /dev/null +++ b/group08/769638826/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:32 + */ +public class StackTest { +} From cc242d85b974fa5c6362dcc18ea27a777c62c29e Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sat, 25 Feb 2017 18:38:30 +0800 Subject: [PATCH 101/432] delete unuseful file --- group08/769638826/.idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 group08/769638826/.idea/vcs.xml diff --git a/group08/769638826/.idea/vcs.xml b/group08/769638826/.idea/vcs.xml deleted file mode 100644 index b2bdec2d71..0000000000 --- a/group08/769638826/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 25f00239efa01d3654386b4ca3baee73fe7db90c Mon Sep 17 00:00:00 2001 From: qilei Date: Sat, 25 Feb 2017 19:05:10 +0800 Subject: [PATCH 102/432] queue and stack --- .../src/main/java/com/coding/basic/Queue.java | 11 ++++--- .../src/main/java/com/coding/basic/Stack.java | 11 +++---- .../test/java/com/coding/basic/QueueTest.java | 29 +++++++++++++++++++ .../test/java/com/coding/basic/StackTest.java | 29 +++++++++++++++++++ 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java create mode 100644 group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java index 36e516e266..4e23a01d17 100644 --- a/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java @@ -1,19 +1,22 @@ package com.coding.basic; public class Queue { + + private ArrayList elementData = new ArrayList(); - public void enQueue(Object o){ + public void enQueue(Object o){ + elementData.add(o); } public Object deQueue(){ - return null; + return elementData.remove(0); } public boolean isEmpty(){ - return false; + return elementData.size() == 0; } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java index a5a04de76d..83896e5dc5 100644 --- a/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java @@ -3,20 +3,21 @@ public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o){ + public void push(Object o){ + elementData.add(o); } public Object pop(){ - return null; + return elementData.remove(elementData.size() - 1); } public Object peek(){ - return null; + return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ - return false; + return elementData.size() == 0; } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..1e451c045c --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class QueueTest { + + @Test + public void enQueue() throws Exception { + Queue q = new Queue(); + q.enQueue("a"); + assertThat(q.size()).isEqualTo(1); + } + + @Test + public void deQueue() throws Exception { + Queue q = new Queue(); + q.enQueue("a"); + q.enQueue("b"); + Object o = q.deQueue(); + assertThat(q.size()).isEqualTo(1); + assertThat(o).isEqualTo("a"); + } + +} \ No newline at end of file diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..1c25b17007 --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class StackTest { + + @Test + public void push() throws Exception { + Stack s = new Stack(); + s.push("a"); + assertThat(s.size()).isEqualTo(1); + assertThat(s.peek()).isEqualTo("a"); + } + + @Test + public void pop() throws Exception { + Stack s = new Stack(); + s.push("a"); + s.push("b"); + Object pop = s.pop(); + assertThat(pop).isEqualTo("b"); + } + +} \ No newline at end of file From 07c1290a0d5024f49898120c69117fcc3f988953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=9D=BF?= Date: Sat, 25 Feb 2017 19:07:28 +0800 Subject: [PATCH 103/432] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group03/1360464792/.gitignore | 34 ++++ group03/1360464792/pom.xml | 42 +++++ .../java/rui/study/coding2017/ArrayList.java | 142 ++++++++++++++++ .../java/rui/study/coding2017/BinaryTree.java | 93 +++++++++++ .../rui/study/coding2017/BinaryTreeNode.java | 40 +++++ .../java/rui/study/coding2017/Iterator.java | 8 + .../java/rui/study/coding2017/LinkedList.java | 156 ++++++++++++++++++ .../main/java/rui/study/coding2017/List.java | 9 + .../main/java/rui/study/coding2017/Queue.java | 21 +++ .../main/java/rui/study/coding2017/Stack.java | 24 +++ .../rui/study/coding2017/ArrayListTest.java | 82 +++++++++ .../rui/study/coding2017/BinaryTreeTest.java | 34 ++++ .../rui/study/coding2017/LinkedListTest.java | 138 ++++++++++++++++ .../java/rui/study/coding2017/QueueTest.java | 39 +++++ .../java/rui/study/coding2017/StackTest.java | 54 ++++++ 15 files changed, 916 insertions(+) create mode 100644 group03/1360464792/.gitignore create mode 100644 group03/1360464792/pom.xml create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/List.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Queue.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Stack.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/ArrayListTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/BinaryTreeTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/LinkedListTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java diff --git a/group03/1360464792/.gitignore b/group03/1360464792/.gitignore new file mode 100644 index 0000000000..b3c9df97d1 --- /dev/null +++ b/group03/1360464792/.gitignore @@ -0,0 +1,34 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.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* + + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### maven +target \ No newline at end of file diff --git a/group03/1360464792/pom.xml b/group03/1360464792/pom.xml new file mode 100644 index 0000000000..5fed7b15ab --- /dev/null +++ b/group03/1360464792/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + rui.study + coding2017 + 0.0.1-SNAPSHOT + + 学习数据结构 + + + 1.6 + UTF-8 + + + + + junit + junit + 4.12 + + + + + + coding2017 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${java.version} + ${java.version} + ${encoding} + + + + + \ No newline at end of file diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java new file mode 100644 index 0000000000..4040049ed8 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java @@ -0,0 +1,142 @@ +package rui.study.coding2017; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size; + + private Object[] elementData; + + private static Object[] emptyObjects={}; + + private static int defaultCapacity=10; + + public void add(Object o){ + ensureCapacity(this.size+1); + elementData[size++]=o; + } + + public void add(int index, Object o){ + rangeCheckForAdd(index); + if(elementData[index]!=null){ + ensureCapacity(this.size+1); + //执行数组拷贝 + System.arraycopy(elementData,index,elementData,index+1,size-index); + size++; + } + elementData[index]=o; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object object=elementData[index]; + + int numMoved=size-index-1; + //如果是最后一位remove ,无需进行数组拷贝 + if(numMoved>0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + elementData[--size]=null; + return object; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public ArrayList(){ + this.size=0; + this.elementData=emptyObjects; + } + + public ArrayList(int size){ + this.size=size; + if(size>0){ + this.elementData=new Object[size]; + }else if(size==0){ + this.elementData=emptyObjects; + }else{ + throw new IllegalArgumentException("非法容器大小 "+size); + } + + } + + /** + * 判断索引是否合法 + * @param index 索引 + */ + private void rangeCheckForAdd(int index) { + if(index>size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + /** + * 判断索引是否合法 , + * @param index 索引 + */ + private void rangeCheck(int index) { + if(index>=size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + + /** + * 确保当前数组能够长度能够容纳新的对象,如果不够,就自行增长 + * @param needLength 需要的数组长度 + */ + private void ensureCapacity(int needLength) { + if(elementData==emptyObjects){ + needLength = Math.max(defaultCapacity, needLength); + } + + if(needLength-elementData.length>0){ + this.grow(needLength); + } + } + + /** + * 数组扩容 + * @param needLength 需要的长度 + */ + private void grow(int needLength) { + int elementLength=elementData.length; + //扩容1.5倍 + int newLength=elementLength+(elementLength>>1); + + if(needLength-newLength>0){ + newLength=needLength; + } + this.elementData= Arrays.copyOf(this.elementData,newLength); + } + + private class ArrayListIterator implements Iterator{ + //游标,当前迭代器执行到何处了 + private int cursor=0; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + if (cursor >= size)throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + return elementData[cursor++]; + } + } + + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java new file mode 100644 index 0000000000..7d63153ffb --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java @@ -0,0 +1,93 @@ +package rui.study.coding2017; + +/** + * 二叉树 + * Created by 赵睿 on 2017/2/25. + */ +public class BinaryTree { + private BinaryTreeNode root; + + private int size; + + public void insert(Comparable comparable){ + BinaryTreeNode binaryTreeNode=new BinaryTreeNode(comparable); + + if(this.root==null){ + this.root=binaryTreeNode; + }else { + boolean flag=false; + BinaryTreeNode cursorNode=root; + while(!flag){ + if(comparable.compareTo(cursorNode.getData())<0){ + if(cursorNode.getLeft()==null){ + cursorNode.setLeft(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getLeft(); + } + }else { + if(cursorNode.getRight()==null){ + cursorNode.setRight(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getRight(); + } + } + + } + } + size++; + } + + public LinkedList inorder(){ + LinkedList linkedList=new LinkedList(); + sortLeft(linkedList,root); + sortRight(linkedList,root); + return linkedList; + } + + private void sortRight(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Queue queue=getRightList(binaryTreeNode); + while(!queue.isEmpty()){ + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + + } + + private void sortLeft(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Stack stack=getLeftList(binaryTreeNode); + while(!stack.isEmpty()) { + BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop(); + linkedList.add(stackNode.getData()); + Queue queue = getRightList(stackNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + } + linkedList.add(binaryTreeNode.getData()); + } + + + private Stack getLeftList(BinaryTreeNode binaryTreeNode){ + Stack stack=new Stack(); + while(binaryTreeNode.getLeft()!=null){ + binaryTreeNode=binaryTreeNode.getLeft(); + stack.push(binaryTreeNode); + } + return stack; + } + + private Queue getRightList(BinaryTreeNode binaryTreeNode){ + Queue queue=new Queue(); + while(binaryTreeNode.getRight()!=null){ + binaryTreeNode=binaryTreeNode.getRight(); + queue.enQueue(binaryTreeNode); + } + return queue; + } + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java new file mode 100644 index 0000000000..3895133f17 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java @@ -0,0 +1,40 @@ +package rui.study.coding2017; + +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Comparable getData() { + return data; + } + public void setData(Comparable 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() { + } + + public BinaryTreeNode(Comparable data) { + this.data = data; + } + + public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java new file mode 100644 index 0000000000..dddde983c6 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java @@ -0,0 +1,8 @@ +package rui.study.coding2017; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java new file mode 100644 index 0000000000..f0a04cd32f --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java @@ -0,0 +1,156 @@ +package rui.study.coding2017; + +/** + * 单向链表 + */ +public class LinkedList { + private Node head; + + private Node current; + + private int size; + + public LinkedList(){ + } + + public int size(){ + return size; + } + + public void add(Object o){ + Node newNode=new Node(o,null); + if(size==0){ + head=current=newNode; + } + current.next=newNode; + current=newNode; + size++; + } + + public void add(int index , Object o){ + checkIndexForAdd(index); + if(index==size){ + add(o); + }else{ + Node newNode=new Node(o,null); + if(index==0){ + newNode.next=head; + head=newNode; + }else{ + Node after=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=newNode; + newNode.next=after; + } + size++; + } + } + + + public Object get(int index){ + return getIndexNode(index).data; + } + + public void addFirst(Object obj){ + add(0,obj); + } + public void addLast(Object obj){ + if(size==0){ + add(obj); + }else { + add(size,obj); + } + } + + public Object remove(int index){ + checkIndex(index); + Node needRemove; + if(index==0){ + needRemove=head; + if(size==1){ + head=null; + }else{ + head=head.next; + } + }else{ + needRemove=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=needRemove.next; + if(index==size-1){ + current=before; + } + } + size--; + return needRemove.data; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + + public class LinkedListIterator implements Iterator{ + + private int cursor=0; + + private Node cursorNode=head; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + Object object=cursorNode.data; + cursorNode=cursorNode.next; + cursor++; + return object; + } + } + + private void checkIndexForAdd(int index){ + if(!(index>=0&&index<=size)){ + throw new IndexOutOfBoundsException("索引"+index+"越界!"); + } + } + private void checkIndex(int index){ + if(!(index>=0&&index>>>>>>>>>>>>>>>>>>>>>>>>>"); + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + + @Test + public void addFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addFirst(-1); + + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + @Test + public void addLast() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addLast(2); + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + + + } + + @Test + public void remove() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + System.out.println(linkedList.size()); + try { + linkedList.remove(2); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.remove(1); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + try { + linkedList.remove(0); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void removeFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeFirst(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeFirst(); + System.out.println(linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeLast(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeLast(); + System.out.println(linkedList.size()); + + } +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java new file mode 100644 index 0000000000..baff49411c --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java @@ -0,0 +1,39 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试队列 + * Created by 赵睿 on 2017/2/25. + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue=new Queue(); + queue.enQueue(1); + queue.enQueue(2); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + } + + + @Test + public void isEmpty() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.isEmpty()); + queue.enQueue(1); + System.out.println(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.size()); + queue.enQueue(1); + System.out.println(queue.size()); + } + +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java new file mode 100644 index 0000000000..115a16f1fc --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java @@ -0,0 +1,54 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试栈 + * Created by 赵睿 on 2017/2/25. + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + } + + @Test + public void peek() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + + + } + + @Test + public void isEmpty() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.isEmpty()); + stack.push(1); + System.out.println(stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.size()); + stack.push(1); + System.out.println(stack.size()); + } + +} \ No newline at end of file From 7c83996309bb4c2c6a1cc05a001e79a9c5388ec4 Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sat, 25 Feb 2017 19:20:37 +0800 Subject: [PATCH 104/432] fix some problem --- group08/769638826/.gitignore | 2 +- .../769638826/src/main/java/com/coding/basic/LinkedList.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/group08/769638826/.gitignore b/group08/769638826/.gitignore index c012ac9a02..5132e3c66b 100644 --- a/group08/769638826/.gitignore +++ b/group08/769638826/.gitignore @@ -1,5 +1,5 @@ # Created by .ignore support plugin (hsz.mobi) .gitignore .idea/ -769638826.iml +coding.iml target/ \ No newline at end of file diff --git a/group08/769638826/src/main/java/com/coding/basic/LinkedList.java b/group08/769638826/src/main/java/com/coding/basic/LinkedList.java index 514cef0dc7..981822c43e 100644 --- a/group08/769638826/src/main/java/com/coding/basic/LinkedList.java +++ b/group08/769638826/src/main/java/com/coding/basic/LinkedList.java @@ -38,7 +38,7 @@ public void add(int index, Object o) { newNode.data = o; if (head == null) { - + head = newNode; } if (index < 1 || index > size + 1) { From ea664afb3f3ae64eead08ab0014ab700258b94d0 Mon Sep 17 00:00:00 2001 From: Mori <1363044717@qq.com> Date: Sat, 25 Feb 2017 19:27:24 +0800 Subject: [PATCH 105/432] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArrayList.java" | 119 +++++++++++++ .../BinaryTreeNode.java" | 68 ++++++++ .../Iterator.java" | 6 + .../JavaTest.java" | 62 +++++++ .../LinkedList.java" | 165 ++++++++++++++++++ .../List.java" | 10 ++ .../Queue.java" | 30 ++++ .../Stack.java" | 30 ++++ 8 files changed, 490 insertions(+) create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" create mode 100644 "group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" new file mode 100644 index 0000000000..620e8ead95 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" @@ -0,0 +1,119 @@ +package com.coding.basic; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + //默认容量 + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList(int capacity){ + if(capacity >= 0){ + elementData = new Object[capacity]; + }else { + throw new IllegalArgumentException("Illegal Capacity: " + + capacity); + } + + } + public ArrayList(){ + this(DEFAULT_CAPACITY); + } + + /** + * 保证集合容量 + * @param minCapacity + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + //扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; + if(minCapacity - newCapacity > 0){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + private void checkIndexRange(int index) + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException("Index out of bounds, index : " + index); + } + } + public void add(E o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + public void add(int index, E o){ + checkIndexRange(index);//检查下标 + ensureCapacity(size+1);//保证数组容量 + System.arraycopy(elementData,index,elementData,index + 1,size-index);//数组复制,把index后的元素全部向后移一位 + elementData[index] = o;//插入元素值 + size++;//元素size加一 + } + + @Override + public E get(int index) { + checkIndexRange(index);//检查下标 + return (E)elementData[index]; + } + + @Override + public E remove(int index) { + E e = this.get(index); + int numMoved = size - index - 1; + if(numMoved > 0) + { + System.arraycopy(elementData, index+1, elementData, index, numMoved);//数组复制,把index后的元素全部向前移一位 + } + elementData[--size] = null;//最后一位赋值为null,size-1 + return e; + } + + + public int size(){ + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + + private Object [] array; + private int endIndex = 0; + private int index = 0; + + public ArrayListIterator(ArrayList list){ + this.array=list.elementData; + this.endIndex = list.size(); + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + return (E)Array.get(this.array, this.index++); + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" new file mode 100644 index 0000000000..a1653b6e9f --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" @@ -0,0 +1,68 @@ +package com.coding.basic; + + +public class BinaryTreeNode implements Comparable { + public BinaryTreeNode(Object data) { + this.data = data; + } + + 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(o); + insertNode(node); + return node; + } + private void insertNode(BinaryTreeNode node){ + insertNode(this,node); + } + private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { + if (parentNode.compareTo(node) <= 0) {//数字大于父节点 + if (parentNode.right == null) { + parentNode.right = node; + return; + } + insertNode(parentNode.right, node); + } else { + if (parentNode.left == null) { + parentNode.left = node; + return; + } + insertNode(parentNode.left, node); + } + } + + @Override + public int compareTo(BinaryTreeNode o) { + Integer i = (Integer) this.data; + return i.compareTo((Integer) o.data); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" new file mode 100644 index 0000000000..09e5b73661 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" new file mode 100644 index 0000000000..208fa8390d --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by Mori on 2017/2/21. + */ +public class JavaTest { + @Test + public void testBinaryTreeNode(){ + BinaryTreeNode node = new BinaryTreeNode(5); + node.insert(4);//左 + node.insert(7);//右 + node.insert(2);//左左 + node.insert(6);//右左 + node.insert(5);//右左左 + node.insert(6);//右左右 + System.out.println(node.getData()); + System.out.println(node.getLeft().getData()); + System.out.println(node.getRight().getData()); + System.out.println(node.getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getData()); + System.out.println(node.getRight().getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getRight().getData()); + } + @Test + public void testArrayList(){ + ArrayList list =new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(5); + Assert.assertEquals((Object) list.get(2),3); + Assert.assertEquals((Object) list.remove(2),3); + Assert.assertEquals((Object) list.get(2),5); + Iterator listIterator = list.iterator(); + while (listIterator.hasNext()){ + System.out.println(listIterator.next()); + } + } + @Test + public void testLinkedList(){ + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(5); + linkedList.addFirst(10); + linkedList.add(1,6); + // linkedList.removeLast(); + //linkedList.removeFirst(); + Iterator linkedListIterator = linkedList.iterator(); + while (linkedListIterator.hasNext()){ + System.out.println(linkedListIterator.next()); + } + System.out.println("----"); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList.remove(2)); + //System.out.println(linkedList.get(3)); + //System.out.println(linkedList.get(4)); + } +} \ No newline at end of file diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" new file mode 100644 index 0000000000..760fec91e9 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" @@ -0,0 +1,165 @@ +package com.coding.basic; + +import jdk.nashorn.internal.ir.ReturnNode; + +import java.lang.reflect.Array; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private int size;//记录当前元素的size + + public void add(E e) { + Node node = new Node(e); + if (head == null) { + head = node; + } else { + tail.next = node; + } + tail = node; + tail.next = null; + size++; + } + + private void checkIndexRange(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(int index, E e) { + checkIndexRange(index); + Node node = new Node(e); + Node temp = head; + Node temp2 = null; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + temp2 = temp.next; + temp.next = node; + node.next = temp2; + size++; + } + + @Override + public E get(int index) { + checkIndexRange(index); + Node temp = head; + + for (int i = 0; i < index; i++) { + temp = temp.next; + } + return (E) temp.data; + } + + @Override + public E remove(int index) { + checkIndexRange(index); + if (index == 0) { + E e = removeFirst(); + return e; + } + if (index == size) { + E e = removeLast(); + return e; + } + Node temp = head; + Node temp2 = null; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + E e = (E) temp.next.data; + temp2 = temp.next.next; + temp.next = null; + temp.next = temp2; + size--; + return e; + } + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + Node node = new Node(e); + node.next = head; + head = node; + size++; + } + + public void addLast(E e) { + this.add(e); + } + + public E removeFirst() { + E e = (E) head.data; + head = head.next; + size--; + return e; + } + + public E removeLast() { + Node temp = head; + for (int i = 0; i < size - 1; i++) { + temp = temp.next; + } + temp.next = null; + E e = (E) tail.data; + tail = temp; + size--; + return e; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + E data; + Node next; + public Node(E e) { + this.data = e; + } + } + private class LinkedListIterator implements Iterator{ + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private Node node;//当前遍历的node + private int index; + private int endIndex; + + public LinkedListIterator(LinkedList list){ + this.head=list.head; + this.tail=list.tail; + this.endIndex = list.size(); + node=head; + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + if(index == 0){ + node = head; + }else { + node = node.next; + } + index++; + return (E)node.data; + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" new file mode 100644 index 0000000000..899ba2bd3e --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void add(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); + boolean isEmpty(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" new file mode 100644 index 0000000000..a8d5741846 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * 进队列 + * @param o + */ + public void enQueue(E o){ + elementData.addLast(o);//添加到队尾 + } + + /** + * 出队列 + * @return + */ + public E deQueue(){ + return elementData.removeFirst();//移除队首 + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" new file mode 100644 index 0000000000..de407c8548 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + public void push(E o){ + elementData.add(o); + } + + /** + * 移除栈顶并返回他 + * @return + */ + public E pop(){ + return elementData.remove(elementData.size()-1); + } + /** + * 得到栈顶元素 + * @return + */ + public E peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.isEmpty(); + } + public int size(){ + return elementData.size(); + } +} From 6b5a9987bbf22843288871cc2bd8d8b592e43472 Mon Sep 17 00:00:00 2001 From: wugu Date: Sat, 25 Feb 2017 19:31:06 +0800 Subject: [PATCH 106/432] ok --- .../src/com/coding/basic/ArrayList.java | 73 +++++++++++++++++ .../src/com/coding/basic/Iterator.java | 9 +++ .../src/com/coding/basic/LinkedList.java | 79 +++++++++++++++++++ .../351121278/src/com/coding/basic/List.java | 9 +++ .../351121278/src/com/coding/basic/Queue.java | 23 ++++++ .../351121278/src/com/coding/basic/Stack.java | 24 ++++++ 6 files changed, 217 insertions(+) create mode 100644 group04/351121278/351121278/src/com/coding/basic/ArrayList.java create mode 100644 group04/351121278/351121278/src/com/coding/basic/Iterator.java create mode 100644 group04/351121278/351121278/src/com/coding/basic/LinkedList.java create mode 100644 group04/351121278/351121278/src/com/coding/basic/List.java create mode 100644 group04/351121278/351121278/src/com/coding/basic/Queue.java create mode 100644 group04/351121278/351121278/src/com/coding/basic/Stack.java diff --git a/group04/351121278/351121278/src/com/coding/basic/ArrayList.java b/group04/351121278/351121278/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..fd80c89662 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.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){ + //容量检查 + checkCapacity(size + 1); + elementData[size++] = o; + + } + + //对elementData数组进行容量检查 + private void checkCapacity(int minSize) { + //取得当前数组的长度 + int elementDataLength = elementData.length; + //如果最小长度大于当前数组的长度,则进行扩容 + if (minSize > elementDataLength) { + //ArrayList类扩容的长度是原来数组长度的1.5倍+1,此处参考ArrayList的长度进行扩容 + int newSize = (elementDataLength * 3) / 2 + 1; + //如果扩张后的长度还是比最小需要的长度小,则取需要的长度 + if (newSize < minSize) { + newSize = minSize; + } + //进行数据的拷贝 + elementData = Arrays.copyOf(elementData, newSize); + } + } + + public void add(int index, Object o){ + //对容量进行检查 + checkCapacity(size + 1); + //对数组进行复制,将指定索引位置空出 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (index > size) { + throw new IllegalArgumentException("参数不对"); + } + //size - index + Object o = elementData[index]; + int moverNum = size - index - 1; + if (moverNum > 0) { + System.arraycopy(elementData, index+1, elementData, index, moverNum); + } + //将数组最后一个元素置为null + elementData[--size] = null; + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + +} + diff --git a/group04/351121278/351121278/src/com/coding/basic/Iterator.java b/group04/351121278/351121278/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..095e38e85b --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + + +public interface Iterator { + boolean hasNext(); + + Object next(); + +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/LinkedList.java b/group04/351121278/351121278/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5428802926 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/LinkedList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + + for (int i = 0; i <= index; i++) { + head = head.next; + } + Node head = this.head; + Node node = new Node(); + this.head.next = node; + node.data = o; + node.next = head.next; + size++; + } + public Object get(int index){ + for (int i = 0; i <= index; i++) { + head = head.next; + } + return head.data; + } + public Object remove(int index){ + for (int i = 0; i < index; i++) { + head = head.next; + } + Node head = this.head; + Object data = head.next.data; + Node next = this.head.next.next; + head.next = next; + return data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.next = head; + node.data = o; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + head.next = node; + node.data = o; + node.next = null; + size++; + } + public Object removeFirst(){ + Object data = head.data; + head.next = null; + return data; + } + public Object removeLast(){ + for (int i = 0; i < size; i++) { + head = head.next; + } + Object data = head.next.data; + head.next = null; + return data; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + } +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/List.java b/group04/351121278/351121278/src/com/coding/basic/List.java new file mode 100644 index 0000000000..ee8ab69781 --- /dev/null +++ b/group04/351121278/351121278/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(); +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/Queue.java b/group04/351121278/351121278/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..25737e5206 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + Object remove = elementData.remove(0); + return remove; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/Stack.java b/group04/351121278/351121278/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..fd3548a241 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object remove = elementData.remove(elementData.size() - 1); + return remove; + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file From 64a047beab1ef3b4ebaf2cdcdf889f03cd7a34b4 Mon Sep 17 00:00:00 2001 From: MysteriousAuntieCook <1925347167@qq.com> Date: Sat, 25 Feb 2017 22:55:34 +1100 Subject: [PATCH 107/432] Create readme.md --- group01/1925347167/Week1 Basic Data Structure/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 group01/1925347167/Week1 Basic Data Structure/readme.md diff --git a/group01/1925347167/Week1 Basic Data Structure/readme.md b/group01/1925347167/Week1 Basic Data Structure/readme.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/readme.md @@ -0,0 +1 @@ + From 8d9cdb460d5ec76c2d81f6c2bb3914f7451fddfa Mon Sep 17 00:00:00 2001 From: xiaobo Date: Sat, 25 Feb 2017 20:00:09 +0800 Subject: [PATCH 108/432] up --- .../src/Collection/Onehomework/ArrayList.java | 101 +++++++++++ .../Onehomework/BinaryTreeNode.java | 76 ++++++++ .../src/Collection/Onehomework/Iterator.java | 7 + .../Collection/Onehomework/LinkedList.java | 170 ++++++++++++++++++ .../src/Collection/Onehomework/List.java | 9 + .../src/Collection/Onehomework/Queue.java | 29 +++ .../src/Collection/Onehomework/Stack.java | 34 ++++ 7 files changed, 426 insertions(+) create mode 100644 group04/349184132/src/Collection/Onehomework/ArrayList.java create mode 100644 group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java create mode 100644 group04/349184132/src/Collection/Onehomework/Iterator.java create mode 100644 group04/349184132/src/Collection/Onehomework/LinkedList.java create mode 100644 group04/349184132/src/Collection/Onehomework/List.java create mode 100644 group04/349184132/src/Collection/Onehomework/Queue.java create mode 100644 group04/349184132/src/Collection/Onehomework/Stack.java diff --git a/group04/349184132/src/Collection/Onehomework/ArrayList.java b/group04/349184132/src/Collection/Onehomework/ArrayList.java new file mode 100644 index 0000000000..cf1aa4f58e --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/ArrayList.java @@ -0,0 +1,101 @@ +package Collection.Onehomework; + +import java.util.Date; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData ; + + public ArrayList(){ this(10); } + public ArrayList(int capacity){ + if(capacity<0) + throw new IllegalArgumentException(); + elementData = new Object[capacity]; + } + public void add(Object o){ + if(size==elementData.length) + ResizeCapacity(); + elementData[size++] = o; + } + private void ResizeCapacity(){ + Object[] newDatas = new Object[size*2+1]; + System.arraycopy(elementData,0,newDatas,0,size); + elementData = newDatas; + } + private void rangeCheck(int index){ + if(index<0 || index > size-1) + throw new IllegalArgumentException(); + } + public void add(int index, Object o){ + rangeCheck(index); + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index] = o; + size++; + } + + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldElement = elementData[index]; + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + size--; + return oldElement; + } + + public int size(){ + return size; + } + public boolean isEmpty(){ return size==0; } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int pos = 0; + @Override + public boolean hasNext() { + return possize) + throw new IllegalArgumentException(); + return elementData[pos++]; + } + + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add("str"); + list.add(new Date()); + + + + Iterator iter = list.iterator(); + while(iter.hasNext()){ + System.out.println(iter.next()); + } + list.add(2,123); + list.add(0,15); + Iterator iter2 = list.iterator(); + while(iter2.hasNext()){ + System.out.println(iter2.next()); + } + + + + } + + +} diff --git a/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java new file mode 100644 index 0000000000..e15b4ae5e3 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java @@ -0,0 +1,76 @@ +package Collection.Onehomework; + + + + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + private BinaryTreeNode root; + + 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 =null; + if(o==null) + throw new NullPointerException("数据不能为空"); + + if(root==null){ + root = new BinaryTreeNode(); + root.setData(o); + }else { + newNode = new BinaryTreeNode(); + BinaryTreeNode nowNode = root; + int val = (int)root.getData(); + nowNode.setData(o); + while(true) { + + if ((int)newNode.getData()< val) { + if(nowNode.left==null){ + nowNode.setLeft(newNode); + } else { + nowNode = nowNode.left; + } + } else if((int)newNode.getData()> val){ + if (nowNode.right==null ) { + nowNode.setRight(newNode); + } else{ + nowNode = newNode.right; + } + }else { + System.out.println("已存在元素结点"); + throw new IllegalArgumentException(); + } + } + } + return newNode; + } + + public static void main(String[] args) { + + } + + + +} diff --git a/group04/349184132/src/Collection/Onehomework/Iterator.java b/group04/349184132/src/Collection/Onehomework/Iterator.java new file mode 100644 index 0000000000..e7c8a4f6d2 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Iterator.java @@ -0,0 +1,7 @@ +package Collection.Onehomework; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/349184132/src/Collection/Onehomework/LinkedList.java b/group04/349184132/src/Collection/Onehomework/LinkedList.java new file mode 100644 index 0000000000..e2cb4ca4b1 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/LinkedList.java @@ -0,0 +1,170 @@ +package Collection.Onehomework; + +public class LinkedList implements List { + + private Node head; + private Node now ; + private int size = 0; + public void add(Object o){ + if(head == null) { + head = new Node(o, null); + now = head; + } + else{ + + Node node = new Node(o,null); + now.next = node; + now = node; + } + size++; + + } + private void rangeCheck(int index) { + if (index < 0 || index > size - 1) + throw new IllegalArgumentException(); + } + public void add(int index , Object o){ + rangeCheck(index); + + if(index==0){ + addFirst(o); + }else { + Node node = new Node(o,null); + Node now = head; + Node next = head; + for (int i = 1; i <= index; i++) { + next = next.next; + if (i == index) { + node.next = next; + now.next = node; + } + now = now.next; + } + size++; + } + } + + public Object get(int index){ + Node indexNode = head; + if(index==0) + return indexNode.data; + else { + + for (int i = 1; i <= index; i++) { + indexNode = indexNode.next; + if (i == index) + return indexNode.data; + } + } + return null; + } + public Object remove(int index){ + rangeCheck(index); + + if(index == 0){ + return removeFirst(); + }else { + Node pre = head; + Node now = head; + for (int i = 1; i <= index; i++) { + now = now.next; + if (i == index) { + pre.next = now.next; + } + pre = pre.next; + } + size--; + return now.data; + } + } + + public int size(){ + return size ; + } + + public boolean isEmpty(){ return size==0; } + + public void addFirst(Object o){ + Node oldhead = head; + Node newhead = new Node(o,oldhead); + head = newhead; + size++; + } + public void addLast(Object o){ + Node node = head; + while(node !=null){ + node = node.next; + if(node==null){ + Node lastnode = new Node(o,null); + node.next = lastnode; + } + } + size++; + } + public Object removeFirst(){ + Node oldhead = head; + Node newhead = head.next; + oldhead.next = null; + head = newhead; + size--; + return oldhead.data; + } + public Object removeLast(){ + Node node = head; + Node prev = head; + while(node !=null){ + node = node.next; + if(node==null){ + prev.next = null; + } + prev = prev.next; + } + size--; + return node.data; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + int pos = 0; + @Override + public boolean hasNext() { + return possize) + throw new IllegalArgumentException(); + return get(pos++); + } + } + private static class Node{ + Object data; + Node next; + private Node(Object data,Node next){ + this.data = data; + this.next = next; + + } + + + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(0,15); + list.add(1,14); + list.removeLast(); + list.addFirst(1); + list.removeFirst(); + Iterator iter = list.iterator(); + while(iter.hasNext()){ + System.out.println(iter.next()); + } + } +} diff --git a/group04/349184132/src/Collection/Onehomework/List.java b/group04/349184132/src/Collection/Onehomework/List.java new file mode 100644 index 0000000000..015dd06102 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/List.java @@ -0,0 +1,9 @@ +package Collection.Onehomework; + +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/group04/349184132/src/Collection/Onehomework/Queue.java b/group04/349184132/src/Collection/Onehomework/Queue.java new file mode 100644 index 0000000000..341adeeec6 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Queue.java @@ -0,0 +1,29 @@ +package Collection.Onehomework; + +public class Queue { + private LinkedList elementData = new LinkedList(); + private int size = 0; + public void enQueue(Object o){ + elementData.addLast(o); + size++; + } + + public Object deQueue(){ + if(isEmpty()) + try { + throw new IllegalAccessException(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + size--; + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return size; + } +} diff --git a/group04/349184132/src/Collection/Onehomework/Stack.java b/group04/349184132/src/Collection/Onehomework/Stack.java new file mode 100644 index 0000000000..a6a488cb05 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Stack.java @@ -0,0 +1,34 @@ +package Collection.Onehomework; + +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()){ + try { + throw new IllegalAccessException(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return elementData.get(--size); + } + + public Object peek(){ + return elementData.get(size-1); + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return size; + } +} From d17daa54e9c5665d9da0a7a9bed8288aa43ba889 Mon Sep 17 00:00:00 2001 From: MysteriousAuntieCook <1925347167@qq.com> Date: Sat, 25 Feb 2017 20:02:51 +0800 Subject: [PATCH 109/432] Add files via upload --- .../Week1 Basic Data Structure/ArrayList.java | 64 +++++++++ .../BinaryTreeNode.java | 38 ++++++ .../Week1 Basic Data Structure/Iterator.java | 7 + .../LinkedList.java | 127 ++++++++++++++++++ .../Week1 Basic Data Structure/List.java | 9 ++ .../Week1 Basic Data Structure/Queue.java | 24 ++++ .../Week1 Basic Data Structure/Stack.java | 28 ++++ 7 files changed, 297 insertions(+) create mode 100644 group01/1925347167/Week1 Basic Data Structure/ArrayList.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/Iterator.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/LinkedList.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/List.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/Queue.java create mode 100644 group01/1925347167/Week1 Basic Data Structure/Stack.java diff --git a/group01/1925347167/Week1 Basic Data Structure/ArrayList.java b/group01/1925347167/Week1 Basic Data Structure/ArrayList.java new file mode 100644 index 0000000000..2797e0b129 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/ArrayList.java @@ -0,0 +1,64 @@ +package com.coding.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){ + if (fullCheck()) + elementData = Arrays.copyOf(elementData, size*2); + elementData[size++] = o; + } + public void add(int index, Object o){ + + if (fullCheck()) + elementData = Arrays.copyOf(elementData, size*2); + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + for (int i = size; i > index; --i) + elementData[i] = elementData[i-1]; + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (rangeCheck(index)) + return elementData[index]; + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + Object rmo = elementData[index]; + for (int i = index; i < size-1; ++i) + elementData[i] = elementData[i-1]; + size--; + return rmo; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private boolean rangeCheck(int index) { + if (index < 0 || index >= size) + return false; + return true; + } + + private boolean fullCheck() { + if (size >= elementData.length) + return true; + return false; + } + +} diff --git a/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java b/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java new file mode 100644 index 0000000000..45827be3a5 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group01/1925347167/Week1 Basic Data Structure/Iterator.java b/group01/1925347167/Week1 Basic Data Structure/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/1925347167/Week1 Basic Data Structure/LinkedList.java b/group01/1925347167/Week1 Basic Data Structure/LinkedList.java new file mode 100644 index 0000000000..3097f69edc --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/LinkedList.java @@ -0,0 +1,127 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + Node tmp = head; + while (tmp.next != null) + tmp = tmp.next; + + Node n = new Node(o); + tmp.next = n; + size++; + } + public void add(int index , Object o){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + + if (index == 0) { + Node newhead = new Node(o); + newhead.next = head; + head = newhead; + } else { + Node tmp = head; + for (int i = 0; i < index - 1; ++i) + tmp = tmp.next; + Node node = new Node(o); + node.next = tmp.next; + tmp.next = node; + } + + size++; + } + public Object get(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + Node tmp = head; + for (int i = 0; i < index - 1; ++i) + tmp = tmp.next; + return tmp.data; + + } + + public Object remove(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + + if (index == 0) { + Node oldHead= head; + head = head.next; + size--; + return oldHead.data; + }else { + Node tmp = head; + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + Node node = tmp.next; + tmp.next = node.next; + size--; + return node.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newHead = new Node(o); + newHead.next = head; + head = newHead; + size++; + } + public void addLast(Object o){ + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + Node node = new Node(o); + tmp.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) + throw new IndexOutOfBoundsException(); + Node oldHead = head; + head = head.next; + size--; + return oldHead.data; + } + public Object removeLast(){ + if (head == null) + throw new IndexOutOfBoundsException(); + Node tmp = head; + while (tmp.next.next != null) { + tmp = tmp.next; + } + Node node = tmp.next; + tmp.next = null; + size--; + return node.data; + } + public Iterator iterator(){ + return null; + } + + private boolean rangeCheck(int index) { + if (index < 0 || index >= size) + return false; + return true; + } + + private static class Node{ + Object data; + Node next; + + Node(Object data) { + this.data = data; + next = null; + } + + } +} diff --git a/group01/1925347167/Week1 Basic Data Structure/List.java b/group01/1925347167/Week1 Basic Data Structure/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/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/group01/1925347167/Week1 Basic Data Structure/Queue.java b/group01/1925347167/Week1 Basic Data Structure/Queue.java new file mode 100644 index 0000000000..b8c394b833 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList llist = new LinkedList(); + + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + if (isEmpty()) + return null; + return llist.removeFirst(); + } + + public boolean isEmpty(){ + return (llist.size()==0); + } + + public int size(){ + return -llist.size(); + } +} diff --git a/group01/1925347167/Week1 Basic Data Structure/Stack.java b/group01/1925347167/Week1 Basic Data Structure/Stack.java new file mode 100644 index 0000000000..4458cb61d7 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) + return null; + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) + return null; + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } +} From 77e6746063116a210ce418b93d4726d4aa4f5ec0 Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sat, 25 Feb 2017 20:03:08 +0800 Subject: [PATCH 110/432] finish ArrayList\'s iterator method --- .../main/java/com/coding/basic/ArrayList.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/group08/769638826/src/main/java/com/coding/basic/ArrayList.java b/group08/769638826/src/main/java/com/coding/basic/ArrayList.java index 8ec793ba3f..1e6ccfcfcb 100644 --- a/group08/769638826/src/main/java/com/coding/basic/ArrayList.java +++ b/group08/769638826/src/main/java/com/coding/basic/ArrayList.java @@ -76,13 +76,15 @@ public int size() { } public Iterator iterator() { - return null; + return new ListIterator(); } private class ListIterator implements Iterator{ + int cursor; + @Override public boolean hasNext() { - return size != 0; + return cursor != size; } public void remove(){ @@ -91,12 +93,19 @@ public void remove(){ @Override public Object next() { - if(!hasNext()){ + if(!hasNext()) { throw new NoSuchElementException(); } - //TODO - return null; + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + + Object[] elementData = ArrayList.this.elementData; + + cursor = i + 1; + + return elementData[i]; } } } From d9ce72e1e6fc08ef0c9e55babf67ec31e870a9d2 Mon Sep 17 00:00:00 2001 From: xiaobo Date: Sat, 25 Feb 2017 20:13:02 +0800 Subject: [PATCH 111/432] up --- ...54\347\233\230\346\214\207\344\273\244.docx" | Bin 0 -> 11099 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" diff --git "a/group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" "b/group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" new file mode 100644 index 0000000000000000000000000000000000000000..72f97352ac6772fc4dfa42578b3bdf2bcab88e86 GIT binary patch literal 11099 zcmb7q1yo$i677WGI=BZ9?ry<@2Y0vN4DJ%#A-D&ZkObEt!QB!Zf@^Sh`9t#V%?jcJE%?>4;KT1<4Gw%^3^J0p2iZC>%7eu)z&Buj ziWPREsVhDK0MsD>0N_8x3~X%~T&=A#Vg_yenScTN30KG=yi;!lM|*(zdE6@2(E5VH`8RZwp6cs17zARHh z`JM$_!D;OTFlL{uB5g)>tWDowYr=D^j%IeM%(PHbXC)D`zimdb#B!p?o;CsgD2SP^Q>+T`3E~FEvXw46^uM&Q5wEqUew<*w=~mFM~e^; z)wXWadAj#DGICT_I~;& z|38B1HO0XEn}YdA{U`rMAbXRC2jCT}W7$rO5ws6;jnaB88n>4lM;>T>ViDNN!{JQn zp4yj#>Lh!;Uud3+$pJmT>E=1KEnYwMfn(a&sbUnfkJS%tq!C?_6VqLXagh}=DOXhyBcqfJ)jl2H?9iK2G;K7Q83h}fs?&M-2*L$FLk5HZjsnrjuK-GcE=<| zILiZ5&d1X&Y=`N1WWXLTs!hxtZq0bX)0UkAH&PkRCwVn~74hB#@m{$Ix7o45yW|ey zfv$h|Bv4?wTtN25kGx0#Ph)H(*vr_M001a|i8wgAS(!LIu-T;3Z$Hb0H77TI^ECWJ zN+8*+-7KM6d`U;NUGZ*wp|MLT30lPn2^^AyrfRb{LrbuEsAvGfyzX>Pj|0Qr3J~yQ zYX!t;j>hMZfRG~Fp6e29&{8SUMe30n6pbLaKXjULt>epsJ?ZS0v-6iqjRk3^zqw z=}E2X=AUTzH7m3qdUbjtWP{CoNNZoDCXq&$8uySdI5|(!#IsLyqKzP92T&d50TY#` zJeX-lRd=&Fax+=JvN`hFZr9n%NhF1mH-i6bNscS+df0oKfwzE}JvN^Et9Z%Ib-28- zt4;D7$V-g=%~(eD6pZ@2qCJ({@;l=@?f#EF{-@miWF*gszXFIc-#8s}_lv>SduVSURMR z$4KwTzHj14U_wfJ8a=JpaWhOY{LJB*$muxckSmRiD1n1c%xQUd>A92y)F}&+fM*fP zeBUN-v?&zOQh7IvJi1huwE0^KQq{dB7t*VGF*8K!PTC6Yw=SE5z7gh(KBE(qGi32} z(nD2ekmA=sYb{;e89{<;k;e02uEXGqPNL;8qU8aiWi_7=lqR5VD1Alp7ew;!ur0Ud zToATC_ECWEnH6M!bIK#|tsng0Ti>Dq-}T_h0O%MJBxGSnBV~}C*#>Y@BJF4xK|vH$ z2uRZM7$s+ynyNnE-5{Ux-3}zV5_~ z)T6TfFzYjCzQgYE0ex;^J0kgq!#RX&uY0z*q^~w4!l5z^j`#U#OB0Uk*#J7u7bvnYl{b0d%1~u5S~5q?1mn?Svwyo0fm27v5mQVEZz&tWDBhc7awxbbaS#=e%mNXL2lR7Ry$=8Rhn9PV>Qm)crAzNU^)QiB_AOm$7A6mrn@g9jy}pcVoTS)! z0mXzzqU$B{yyKag@di4Sy0UyWj2zelwT3HT&SlCeJ5~Dyhp(eShuF9vKbTlMRRmcM zc{Up7C*G(UKUR)>FYh=HjHO1VxR%kw$}d@~%x5X9#zZ!Ss=IN}Y3XMi`Y@gpeQd^Za^IT(Dden=tGrgs928$O32`hpfRFo9!B zd9Les(3x3GQ}M;WOzH^!*}Bx^r74=_z~e7!eD~4f-($+gE9roqU3e3kKeC5x7>jT4x!8 zI!7yDUrR@KzbbsapNgrsE%5Eas3~a(KFa0#hX=w1VYFBZM@g&d=#SnzjGk1U+qDgh zb?As4w0CWL3$4I0U6u>=>tXLGP!O8>wL$pF@D3;V#-Q`CFUF@^ql1_V=hw5RZg9Lc zPd(i-rY&kS77t^CS+$^QF`mhmVcLc7lb~^&e~FQEJ;P{CM6XhwGE_iKVL3J~56m@W z?%nD&MChxjxVjn?GxVxIo6ywDX%d@EG6S8SQp{)td4--$7|G?_223Y)6u^IK9EacS zHyI5|w1hV(Zy7{8il5J;!CbRpI1jqqp9MlrI=S|NaLu|);a7#n_kVP*b3u}HM?Ge(%J;)SggiET^jh?1phBc4B3a9Cq0obl~@|{X%!4fsIJ)`H!yT_UIrwxR;IP z+8owq?A5g30n+%YM0Fkp3Fow*4$uAnv@`;&xYf*vg@qcJ3=TK5(9g?})=ch$`?HSXz!jV2uJ%L3By~;ps2qeySJSBimR`jssSFx+;5o49rQW)zJu=Q|BzO zReG}llrdTQn)6E^U^@nWs+t=~Z4kln1dYNKrpdd(`&QA-VOi~X#?32ZUI=c3kJYu> z=Uip~^qp%Z``M|=;b2PecyYsc(>EMT)c$JSCm)R~oVxTB9P}BMlF$IiR;7&ooVVK_ z#ux5w?oTgWN=`hEWGlD3VK1Z41e-|d?(MRD9I|-c;~p`&N_%-ln8HM}?k=rkLl-PX zueEk^dYK^$kTD^p@FaVcXj!KseByjEg``mxkSyk2ORvojtS~w*xwq-D0?D}R@VpLR zE)xr!mgBV=ye72V^166EZIM87JqpG6-R%DD&4cX z>le5s`ZE^RcRSuOehL7H!v5NS{0#0KOdK7}ZA^d0Zq+LLpiw^HT~x=t{F+Bn!FE9t zD(=iwUl@;|vHx(Cm-PSvZ$(DXPN~nZb0aA{I>c8-r@d~6jfRb(Qe7X*4r7=ZTZ(XU z-c$z_i+1~YS(Ft=eVJoeBUUWzunNaHu< z)~!`b<#)Sz9_Gh9mGv@UnP);KO<4HQA^u09>?oyT%sXg?2UOIb@73D?YhCrhIkZvt6 zkm&Nnv67(NA$%|OVFt&BIod+j{o}RP;I!#Tq7rFeqCAG!=*}wJYFC{(6s}GDdbBGm zX~|jhBv+!yNytd+i?+Ky_h%#KvTWI|Uzu<45wjCQl5Jqw?N^N`-|yZ~MRl4`3L zO?)shxuB`dY%<7shOVCO1CpMa9GYTJvVH2QHJi{87LMB8a^x-xJ+1-5$CAQTMbnj! zg$~8cTS=`~K}1t_N+q&APx|hH>?;$#Z+UD)9N5(6>@QbV<=>JcsdXe?_jZSH-fv|Y z9`o)P@trgvk9H77yUs-fL7G@yOndzPdGh|W;=x(s%J&}$#$1Fx_;kC+BMjZVp3%vn z!E#(5%aJM%u`QMca@HMVG6?To+?LovP6PB0>5t~r<8A?d>GMe?Jc1ETOz9ZCM*LpK z-gM<7`5tYHiB;A(vleNXp|*4b>1myeL2@bgMK#9?oaa0@_B{7OMOr3i{Lj*bxN^o_ zr~0FEcU&3U#Rzw>Jfc06@ui=<7TMi@S5)CM zNg5t#6SXCKHd+l$&mIxqm1Cbff&aeWLilSlb)wui`V<-f;K2m|F#i<% zDbD<`_uAEv1>ti7o3J;1)OVW7jkn2INMUMTEKfM2iAseLP}5iUhcsxvjVGK7TUE~z z{94tM{Ul7+ooWVJ;tiJBm1TN?gc7k&w#c4^n3VD^&I=r|o$wAXR?l?YW)d~ElKJTO zE+O^AbmmuU(i98ry>Fq&;nMDQz0Y&|n<>eqAVf9%((W)2?zbj_iL`~%SlXD+T3(>0 z75xxbEJ(AoG=y!J&^DxgwKyGeh$DH?*IxZ5m+Sz4wE9%>eGL{_?1iqMp{<7z8>z@n z)6MSr@lql_w@l>~E zLT4=jRa5Ply;NQBcy}$L*afG>gZG`AniOgdg6<j+1a8W!kwb_dU-j zKMSg2X<25&P;C`U=Fi=(*Z_Em81hicLh5JPB=L3#3M`8qvy=wp<7{T=WGikQY#7oa z7BA%xxg||T@5#l=nAdd|nlFCrhMUeG`fqfjc=LN-99*gfe~P2dg00khS~ioekD4Eb6EsFC3KA+tkjbFJxp0US45T_KVJCgsr>;q8(6;9QCHC zdt#oXq3tWW7+Nh)yEc(mhNyPl*GDj&Q%Y{WOP7=RP9K1*<4`MaxK_(5Da0SzZ$jIK zHJFA$ffmTs`b}wg3dg8>YY)w^^YjfOH@X#hQ$xqBPzP@83taVaHqIFe=2ep|QrOEH zLkHXLS%b0aEshseF=ezsr?>k?7J-L&sS+3s+Gh$fgKzf9aO)+`3Ovo0d4z=vL}XGX z;mUvvY4`*amgHV2kR2IkPkAD($&-wCj_RXvSYxa?Y{aNu=}bM7Bt^e2#}Nr7-G3g( zaPg$lCFBa(O|ZAmae-t?>b%#&7QbpVkBt#{YTQOx-x!pl@}-HIkmO@Nbl#lePBJ-V zp9@F(B^;;3+6Pc3m#dy~@BDg4y{R*HfIFs*!aJ=9nAPPE4*0UMX*e7m_Tlb~L;Ic6 z_Y2|2#kZb!v3zOIou~E*9_zly;f-W$Y`h2S@5yV+9D$apL^Qmn6uDes4k z?*=u%Iw!BK#vu2Jfwo%7c!tyx#YsJoI>H4?zSw2 zFE@I0L#dW!T4T_u$x=e-`$az{If~_3MB+JaQMKomjM9a7VV#P^*aEQiMS3fde6UO; z>)%?F=g=)`I-P%V8IFpe^+$)>gMidYcgb5;@j}5;yv#E7f2X zJ{iroX*Kx^8QOHY>dWcYzF1k}xh5*EBWTAq5D8VeMbk$J9N?3Emp%=)rx2y&2qbBf z3&KWyCX=3&MV3_f;%t%f^9nM|!;6}I2s~F>s83h4nqR{fp~g0%A`6e}4`yl3Rc+$o z1}Es88h6{`6(h9PeucKHnMh)PU@(>Xg~QI!yjHZbHO&?~ncG z49D9>%$!TcVkQK1lLZ*dTKR#|9F090^3;wrOWjYS76r#fO=E>Qs(ws-`7`1xt0nR^ z1xI{W;1j-w6X%~1A2>VUWNl*O_^@KksW#cnG6CliP6!bp@T@hWc6)?ZTkO(_MZy8E z*5AaMg_en;kz2E;d|pH1aom~*=k#;p@(3BMqjdz2kg4eoGM_u^nEfv&VS2BIMFs61* zWEYgdqGgv!P_=~}?ksNNaiAH*=ay7xw)`u!ve{d_pldUd6g5HA;>9 zNJ$WiTNH5ibeF;(H({?>s?a&~3{nQXAu64Hq*L9|T2%Ms(CK=@qU2Wszwz;Q;Fa1Q z^MTfctA$lDV4|Z24W8LeuRB48j8@ZgtC-~f@9LEFw>`qET!{|yGbbiZn9K_5fuNLf}&t|Mt z@TqG3<6%ZVQ_1Fek<@+bI}vFZd{sGW86`LTNpjf!I&qe@YjXszZo+sHk9)$*Dxy`0 z`6+%^VuPjZ=ZRKd_N|(V%~uTLj4xy4lS=hEl$0{<%}&yNf5c?<_|Cbwd}qlPy+M{v z9@lR1tkoiamuCkNIAS;3Y1+abzkX^j^&0;JTq~+CR^Ki+w09SG$jMjz5$4Q8VeCv5 zYeY8-i(E9WfU!Mi_tz*|I0vyPRmt74sjCx75H-noaGzB6a985DfSdbVP>#v^-1NTi zj`Dg4FqI$A^~Yql4^aMl(D!ikZuB!1p>A#UDCYa_^I|vw{6EBhau>hl%pFave@1*6 zQLXT8OhD0nzkA@BP3?GiJUM@rE7lkata)*uv(o4HU%%AK13S|uQ{39)I&=3s<|4nl zE54il{ezDiO0HXe^Hj>-h7!5CREEj$1&OLHfRNtfP^L zhu9j}3URO%IDc6CV8zcoCgx_swhUHY00e1$B4?LJn86Sg2;S83=#L9ZB?C zgKPlh2>SNG3~wl!^H9Ihku`LzhsGXzDhUM%s@H$++PbxvkEZ-NAXdr*$2Xl<6A?9P z(J9K~0#dTfWh#r1Q^dPWxnHgtoK zD5!jB=@H)`FJWVAFLimrkF3Oz+n+PjT{dq83MY{o9RP`Mdr@UnHA)eMmzhEQQF_sAl0y4ScUsQ`1)ln+*XD&x&u_^JH zRsJQz@F4jqc24RkGca%;YJp_Hi*uHS^qUn(bTdQ3>kUcD9Mas)TbXJv4}YJ!vXZ6) zdlu6nPlq0N2SbZdj@t0q^$1#A_sYkqPQG`Yn@l2(5o!* z9{8113@>OmQ&uwKHO^O=)JS3(GdReDwfn|;L)@bYdsTE6dQL{4rp6Rb;X#>OmI&>< zD!00u<*oWO?nrdf8nY3KY5$RGD4E9)s+0uR`~}PhKScY~*G?}6_miZ=BcO5fkwdYV z6VlOUV|O^^4czF4FLjt2dWIgtEaF2we*nEHrNXXezYwA(xf;_G@i zqN$)8^<8UIi@zg$wmj|jnM3&R^X>0rmwzLO8@~AD222bGIEW?y|NivQzXbmoM*n9P z%2~EEA$IQjd%@&hFXvsiQ#rZg`s<2AH@HW|CU-iQjg@;|rwCSJNjdj+62pBt7jbX6 zjo$9$d|A;-OI1Mt>5gu(tp&9|zCFhZYq)DU2NMndl6)$uma~C#hrLj4Y}!{;@h~sT z=vj?o?2i%izP2|nWK)3|>&jiK$TOZlu-2XrXBzWOzBv+BBCvoulo#+T>LTI3FC|Rv z!t;b%-yAu{M3{ygPPp4)4&9m}rA^k-Acb-xTsDw}bs$IHBdZ?-YCPX4sfs93&#Bjo z3mSbtG{Kx;?~aYjVaWnyDQx+=Ap9yLpB>`^>A_T?*)VqY`xn#w;Z;eGwjE;aCnqe#Pm0ftZ<9Q z#;ZXs#d;Nt7PsC`iZkww(fac4(Ma}bJi~AEm{##98qlxY^XGE(_m7iW z`TNn}3j+?3;cEgr3gWgu9D3pto8&pa*O%@%XmN{`E+bug@g_Inn>g)T-4#C=k}n+) z7rVBu-T5xC$S~?|WxW@0Qspek{GiR_sDM>EkMFx5a^Rl}Gikz;%jPqTSK8vr*o{siMRa+AR(YpFGIOk=YeCHTbg2?Pe|>3|;);0b^j{QZ{vU!T>l z4*uQ8Jb3G0?ZMl$6946w9}oK2dp}@)4Z8L>=I2}VzXtoI!UObSuu}Ygy0`y@dyxLs z91cSMa@4-2LEK@anaxd>=zT1;H$r3|0yAStoq}Ux8LYOuz&a){Z|3p zWBB9ht>18butWY0|GSp!e;NHy&-AN37%c_)cXiW)!T&zHAJ&Fn?E%6}_QwM7SlP#G z#RKpcQ<{GQ!FvCF0P~MI|N9Mnd~EZ(CtUCj>i;^#d929eZN+bR9pQiAe{3=y;~(b< zf8!~U|AYT$*6=a>ae>%xxb!;~|rAPjpr;igPzeip96Zkt_ XA} Date: Sat, 25 Feb 2017 20:21:34 +0800 Subject: [PATCH 112/432] up --- .../src/Collection/Onehomework/BinaryTreeNode.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java index e15b4ae5e3..d57fee396f 100644 --- a/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java +++ b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java @@ -49,18 +49,21 @@ public BinaryTreeNode insert(Object o){ if ((int)newNode.getData()< val) { if(nowNode.left==null){ nowNode.setLeft(newNode); + break; } else { nowNode = nowNode.left; } } else if((int)newNode.getData()> val){ if (nowNode.right==null ) { nowNode.setRight(newNode); + break; } else{ nowNode = newNode.right; + } }else { - System.out.println("已存在元素结点"); - throw new IllegalArgumentException(); + + throw new IllegalArgumentException("已存在元素结点"); } } } From 8af8abb2c9b8e57aa5e22ba9b8a03ba7c6d31882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B5=AE=E6=B8=85=E9=A3=8E?= Date: Sat, 25 Feb 2017 20:21:37 +0800 Subject: [PATCH 113/432] dataStructure --- group01/360176196/.classpath | 6 + group01/360176196/.gitignore | 1 + group01/360176196/.project | 17 +++ .../src/xqfGit/dataStructure/ArrayList.java | 73 +++++++++++ .../xqfGit/dataStructure/BinaryTreeNode.java | 49 ++++++++ .../src/xqfGit/dataStructure/Iterator.java | 7 ++ .../src/xqfGit/dataStructure/LinkedList.java | 116 ++++++++++++++++++ .../src/xqfGit/dataStructure/List.java | 9 ++ .../src/xqfGit/dataStructure/Queue.java | 27 ++++ .../src/xqfGit/dataStructure/Stack.java | 28 +++++ 10 files changed, 333 insertions(+) create mode 100644 group01/360176196/.classpath create mode 100644 group01/360176196/.gitignore create mode 100644 group01/360176196/.project create mode 100644 group01/360176196/src/xqfGit/dataStructure/ArrayList.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/Iterator.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/LinkedList.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/List.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/Queue.java create mode 100644 group01/360176196/src/xqfGit/dataStructure/Stack.java diff --git a/group01/360176196/.classpath b/group01/360176196/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group01/360176196/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/360176196/.gitignore b/group01/360176196/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group01/360176196/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group01/360176196/.project b/group01/360176196/.project new file mode 100644 index 0000000000..a895f05a76 --- /dev/null +++ b/group01/360176196/.project @@ -0,0 +1,17 @@ + + + xqfGit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/360176196/src/xqfGit/dataStructure/ArrayList.java b/group01/360176196/src/xqfGit/dataStructure/ArrayList.java new file mode 100644 index 0000000000..5996182fbe --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/ArrayList.java @@ -0,0 +1,73 @@ +package xqfGit.dataStructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + public void add(Object o){ + if(this.size >= elementData.length){ + elementData = Arrays.copyOf(elementData, size+1); + elementData[size] = o; + size++; + } + else{ + elementData[size-1] = o; + size++; + } + + } + + public void add(int index, Object o){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + System.arraycopy(elementData, index, elementData, index+1, elementData.length+1); + elementData[index] = o; + size++; + } + } + + + public Object get(int index){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + return elementData[index]; + } + } + + + public Object remove(int index){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + Object reObject = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, elementData.length-1); + size--; + return reObject; + } + } + + public int size(){ + if(this.size < elementData.length){ + return size; + }else{ + elementData = Arrays.copyOf(elementData, size); + return size; + } + } + + + public Iterator iterator(){ + return null; + } + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java b/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..7662484884 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package xqfGit.dataStructure; + +import com.sun.swing.internal.plaf.basic.resources.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + + + + public Integer getData() { + return data; + } + public void setData(Integer data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Integer i){ + + return null; + } + + public BinaryTreeNode (){ + + } + + + + public BinaryTreeNode(BinaryTreeNode b1,BinaryTreeNode b2){ + this.left = b1; + this.right = b2; + } + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/Iterator.java b/group01/360176196/src/xqfGit/dataStructure/Iterator.java new file mode 100644 index 0000000000..ee4842739f --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/Iterator.java @@ -0,0 +1,7 @@ +package xqfGit.dataStructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/LinkedList.java b/group01/360176196/src/xqfGit/dataStructure/LinkedList.java new file mode 100644 index 0000000000..a663f85b15 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/LinkedList.java @@ -0,0 +1,116 @@ +package xqfGit.dataStructure; + +public class LinkedList implements List { + + private Node first; + private Node last; + private int size; + + public void add(Object o){ + Node l = new Node(o); + l = last.next; + size++; + } + + public void add(int index , Object o){ + Node l = new Node(o); + Node n = first; + if(size == index){ + l = last.next; + l = last; + size++; + }else{ + Node m = first; + for(int i =0;i Date: Sat, 25 Feb 2017 20:53:53 +0800 Subject: [PATCH 114/432] add JUnit --- group07/476770768/MyDataStructure/.classpath | 1 + .../src/com/coding/basic/MyArrayList.java | 2 +- .../src/com/coding/basic/MyArrayListTest.java | 69 +++++++++++++++++++ .../src/com/coding/basic/MyLinkedList.java | 25 ++++--- .../com/coding/basic/MyLinkedListTest.java | 67 ++++++++++++++++++ .../src/com/coding/basic/MyQueueTest.java | 45 ++++++++++++ .../src/com/coding/basic/MyStack.java | 2 +- .../src/com/coding/basic/MyStackTest.java | 56 +++++++++++++++ .../src/com/coding/basic/testFile.java | 15 ---- 9 files changed, 256 insertions(+), 26 deletions(-) create mode 100644 group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java create mode 100644 group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java create mode 100644 group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java create mode 100644 group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java diff --git a/group07/476770768/MyDataStructure/.classpath b/group07/476770768/MyDataStructure/.classpath index 63b7e892d1..b387714202 100644 --- a/group07/476770768/MyDataStructure/.classpath +++ b/group07/476770768/MyDataStructure/.classpath @@ -2,5 +2,6 @@ + diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java index f0c1b3608c..5c8c3bb858 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java @@ -89,7 +89,7 @@ public boolean isFull(){ } public void checkBounds(int index){ - if(index >= size || index < 0){ + if(index > size || index < 0){ //System.out.println("From MyArrayList: Index out of bounds"); throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java new file mode 100644 index 0000000000..c7d9299934 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyArrayListTest { + + + @Test + public void testAddObject() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + mal.add(new Integer(1)); + assertEquals(1, mal.size()); + } + + @Test + public void testAddIntObject() { + MyArrayList mal = new MyArrayList(); + mal.add(0, new Integer(1)); + assertEquals(1, mal.size()); + int tmp = 0; + try { + mal.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + + } + + @Test + public void testGet() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + int tmp = 0; + try { + mal.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + assertEquals(mal.size(),1); + } + + @Test + public void testSize() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + } + + @Test + public void testIsEmpty() { + MyArrayList mal = new MyArrayList(); + assertTrue(mal.isEmpty()); + mal.add(new Integer(1)); + assertFalse(mal.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java index 297c97a3ed..3fe3693b19 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java @@ -38,9 +38,13 @@ public void add(int index, Object o) { */ public void addFirst(Object o) { Node tmp = new Node(o); - tmp.next = head; - head.prov = tmp; - head = tmp; + if(head == null){ + head = tmp; + }else{ + tmp.next = head; + head.prov = tmp; + head = tmp; + } } /** @@ -97,7 +101,10 @@ public Object remove(int index) { public Node removeFirst() { Node tmp = head; head = head.next; - head.prov = null; + if(head != null){ + head.prov = null; + } + return tmp; } @@ -199,22 +206,22 @@ public MyIterator iterator() { private class LinkedListIterator implements MyIterator{ private MyLinkedList eleIterator; - private Node pos; + private int pos; private LinkedListIterator(MyLinkedList mll){ this.eleIterator = mll; - this.pos = eleIterator.get(0); + this.pos = 0; } @Override public boolean hasNext() { - return pos != null; + return pos <= size; } @Override public Object next() { - Node res = pos; - pos = pos.next; + Node res = eleIterator.get(pos); + pos++; return res; } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..1da080a16a --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void testAddObject() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + mll.add(new Integer(1)); + assertEquals(1, mll.size()); + } + + @Test + public void testAddIntObject() { + MyLinkedList mll = new MyLinkedList(); + mll.add(0, new Integer(1)); + assertEquals(1, mll.size()); + int tmp = 0; + try { + mll.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testGet() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + assertNotNull(mll.get(0)); + int tmp = 0; + try { + mll.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + mll.remove(0); + assertEquals(mll.size(),0); + } + + @Test + public void testSize() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + } + + @Test + public void testIsEmpty() { + MyLinkedList mll = new MyLinkedList(); + assertTrue(mll.isEmpty()); + mll.add(new Object()); + assertFalse(mll.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java new file mode 100644 index 0000000000..3d80a95ee4 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyQueueTest { + + @Test + public void testEnQueue() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + mq.enQueue(new Object()); + assertEquals(mq.size(), 1); + } + + @Test + public void testDeQueue() { + MyQueue mq = new MyQueue(); + int tmp = 0; + try { + mq.deQueue(); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + mq.enQueue(new Object()); + assertNotNull(mq.deQueue()); + } + + @Test + public void testIsEmpty() { + MyQueue mq = new MyQueue(); + assertTrue(mq.isEmpty()); + mq.enQueue(new Object()); + assertFalse(mq.isEmpty()); + } + + @Test + public void testSize() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java index 3d9e1ef9a0..3c5b5a6b67 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java @@ -31,7 +31,7 @@ public Object peek(){ } public boolean isEmpty(){ - return top >= 0; + return top < 0; } public int size(){ diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java new file mode 100644 index 0000000000..0722e2d4fa --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import java.util.EmptyStackException; + +import org.junit.Test; + +public class MyStackTest { + + @Test + public void testPush() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + ms.push(new Object()); + assertEquals(1, ms.size()); + } + + @Test + public void testPop() { + MyStack ms = new MyStack(); + ms.push(new Object()); + assertNotNull(ms.pop()); + assertEquals(0, ms.size()); + } + + @Test + public void testPeek() { + MyStack ms = new MyStack(); + int tmp = 0; + try { + ms.peek(); + } catch (EmptyStackException e) { + tmp = 1; + assertEquals(1, tmp); + } + ms.push(new Object()); + assertNotNull(ms.peek()); + assertEquals(1, ms.size()); + } + + @Test + public void testIsEmpty() { + MyStack ms = new MyStack(); + assertTrue(ms.isEmpty()); + ms.push(new Object()); + assertFalse(ms.isEmpty()); + } + + @Test + public void testSize() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java index 1ccabfc977..e75137744b 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java @@ -3,21 +3,6 @@ public class testFile { public static void main(String[] args) { - MyLinkedList mll = new MyLinkedList(); - mll.add(new Integer(5)); - mll.add(new Integer(2)); - mll.add(new Integer(3)); - mll.add(new Integer(4)); - System.out.println(mll); - MyIterator mIt = mll.iterator(); - while(mIt.hasNext()){ - System.out.println(mIt.next()); - } - mll.remove(3); - System.out.println(mll); - - - } } From c843b6aeb83693c6a91c8c9a678f43e0756ed8b2 Mon Sep 17 00:00:00 2001 From: GZ-RXP <283091182@qq.com> Date: Sat, 25 Feb 2017 21:09:16 +0800 Subject: [PATCH 115/432] basic data structure --- .../src/com/coding/basic/ArrayList.java | 113 +++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 89 +++++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 178 ++++++++++++++++++ .../283091182/src/com/coding/basic/List.java | 9 + .../283091182/src/com/coding/basic/Queue.java | 42 +++++ .../283091182/src/com/coding/basic/Stack.java | 48 +++++ 7 files changed, 486 insertions(+) create mode 100644 group11/283091182/src/com/coding/basic/ArrayList.java create mode 100644 group11/283091182/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group11/283091182/src/com/coding/basic/Iterator.java create mode 100644 group11/283091182/src/com/coding/basic/LinkedList.java create mode 100644 group11/283091182/src/com/coding/basic/List.java create mode 100644 group11/283091182/src/com/coding/basic/Queue.java create mode 100644 group11/283091182/src/com/coding/basic/Stack.java diff --git a/group11/283091182/src/com/coding/basic/ArrayList.java b/group11/283091182/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..402d05c019 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private static final int GROW_BY_SIZE = 10; + + private Object[] elementData = new Object[GROW_BY_SIZE]; + + public void add(Object o){ + if(size == elementData.length){ + grow(); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + validate(index); + if(size == elementData.length){ + grow(); + } + for(int i=size;i>index+1;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + validate(index); + return elementData[index]; + } + + public Object remove(int index){ + validate(index); + Object result = elementData[index]; + for(int i =index;i=size)throw new IndexOutOfBoundsException("Invalid Index:"+pos); + Object result = elementData[pos]; + pos++; + return result; + } + + + } + + private void grow(){ + elementData = Arrays.copyOf(elementData, elementData.length+GROW_BY_SIZE); + } + private void validate(int index){ + if(index<0||index>=size)throw new IndexOutOfBoundsException("Invalid Index:"+index); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder("["); + for(int i=0;i1)sb.append(","); + sb.append(elementData[i]); + } + sb.append("]size=").append(this.size()); + return sb.toString(); + } + + public static void main(String[] args){ + ArrayList l = new ArrayList(); + for(int i=0;i<12;i++){ + l.add(i+""); + } + System.out.println(l); + l.add("aaa"); + System.out.println("After adding aaa:"+l); + l.add(2,"bbb"); + System.out.println("After adding bbb:"+l); + System.out.println(l.get(2)); + System.out.println("After getting:"+l); + System.out.println(l.remove(2)); + System.out.println("After removing:"+l); + Iterator it = l.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } + +} diff --git a/group11/283091182/src/com/coding/basic/BinaryTreeNode.java b/group11/283091182/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7f2a030983 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,89 @@ +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(Object o,BinaryTreeNode leftChild,BinaryTreeNode rightChild){ + this.data = o; + this.left = leftChild; + this.right = rightChild; + } + public BinaryTreeNode insert(Object o){ + if(!(o instanceof Comparable)){ + throw new RuntimeException("Incompareable Oject:"+o); + } + System.out.println("CurrentNode Value="+data); + if(((Comparable)o).compareTo(data)>0){ + System.out.print(o+" is greater than "+data+",insert to right;"); + if(this.right==null){ + System.out.println("Creating new rightChild;"); + this.right = new BinaryTreeNode(o,null,null); + }else{ + System.out.println("rightChild exists,Conitnue to insert to rightChild"); + this.right.insert(o); + } + } + if(((Comparable)o).compareTo(data)<0){ + System.out.print(o+" is less than "+data+",insert to left;"); + if(this.left==null){ + System.out.println("Creating new leftChild;"); + this.left = new BinaryTreeNode(o,null,null); + }else{ + System.out.println("leftChild exists,Conitnue to insert to leftChild"); + this.left.insert(o); + } + } + return this; + } + + public static void main(String[] args){ + Integer one = new Integer(1); + Integer two = new Integer(2); + System.out.println(one.compareTo(two)); + System.out.println(two.compareTo(one)); + System.out.println(one.compareTo(one)); + BinaryTreeNode btn = new BinaryTreeNode(new Integer(5),null,null); + btn.insert(new Integer(2)); + btn.insert(new Integer(7)); + btn.insert(new Integer(1)); + btn.insert(new Integer(6)); + inOrderTraversal(btn); + btn.insert(new Integer(4)); + btn.insert(new Integer(8)); + inOrderTraversal(btn); + } + //in-order traversal to print all nodes in sorted order + private static void inOrderTraversal(BinaryTreeNode btn){ + if(btn!=null){ + if(btn.left!=null){ + inOrderTraversal(btn.left); + } + System.out.println(btn.data); + if(btn.right!=null){ + inOrderTraversal(btn.right); + } + } + } + +} diff --git a/group11/283091182/src/com/coding/basic/Iterator.java b/group11/283091182/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coding/basic/LinkedList.java b/group11/283091182/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..233c243130 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/LinkedList.java @@ -0,0 +1,178 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = null; + private int size = 0; + + public void add(Object o){ + if(head==null){ + head = new Node(o); + }else{ + Node temp = head; + while(temp.hasNext()){ + temp = temp.next; + } + temp.next = new Node(o); + } + size++; + + } + public void add(int index , Object o){ + validate(index); + if(index==0){ + Node newNode = new Node(o,head); + head = newNode; + }else{ + Node temp = head; + for(int i=0;i=size)throw new IndexOutOfBoundsException("Invalid Index:"+index); + } + + private static class Node{ + Object data; + Node next; + public boolean hasNext(){ + return (this.next!=null); + } + public Node(Object data,Node next){ + this.data = data; + this.next = next; + } + public Node(Object data){ + this.data = data; + this.next = null; + } + } + @Override + public String toString(){ + StringBuilder sb = new StringBuilder("["); + Node temp = head; + while(temp!=null){ + if(sb.length()>1)sb.append(","); + sb.append(temp.data); + temp = temp.next; + } + sb.append("]size=").append(this.size()); + return sb.toString(); + } + public static void main(String[] args){ + LinkedList l = new LinkedList(); + for(int i=0;i<12;i++){ + l.add(i+""); + } + System.out.println(l); + l.add("aaa"); + System.out.println("After adding aaa:"+l); + l.add(2,"bbb"); + System.out.println("After adding bbb:"+l); + System.out.println(l.get(2)); + System.out.println("After getting:"+l); + System.out.println(l.remove(2)); + System.out.println("After removing:"+l); + l.addFirst("first"); + System.out.println("After add First:"+l); + l.addLast("last"); + System.out.println("After add Last:"+l); + System.out.println(l.removeFirst()); + System.out.println("After remove First:"+l); + System.out.println(l.removeLast()); + System.out.println("After remove Last:"+l); + Iterator it = l.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + //it.next(); + } +} diff --git a/group11/283091182/src/com/coding/basic/List.java b/group11/283091182/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coding/basic/Queue.java b/group11/283091182/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..45fea2a118 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(list.size()==0)throw new RuntimeException("Queue is empty."); + return list.removeFirst(); + } + + public boolean isEmpty(){ + return (list.size()==0); + } + + public int size(){ + return list.size(); + } + + @Override + public String toString(){ + return this.list.toString(); + } + + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("aaa"); + q.enQueue("bbb"); + System.out.println(q); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + q.deQueue(); + q.deQueue(); + System.out.println(q); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + //q.deQueue(); + } +} diff --git a/group11/283091182/src/com/coding/basic/Stack.java b/group11/283091182/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..915d173b1b --- /dev/null +++ b/group11/283091182/src/com/coding/basic/Stack.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +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 RuntimeException("Stack is empty."); + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + if(elementData.size()==0)throw new RuntimeException("Stack is empty."); + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size()==0); + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString(){ + return elementData.toString(); + } + + public static void main(String[] args){ + Stack s = new Stack(); + s.push("aaa"); + s.push("bbb"); + s.push("ccc"); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + System.out.println(s.peek()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + //System.out.println(s.pop()); + } +} From 5f56c2ac21321f31fe20611878b26268359593f3 Mon Sep 17 00:00:00 2001 From: Pxshuo Date: Sat, 25 Feb 2017 21:10:03 +0800 Subject: [PATCH 116/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\227\264\347\232\204\345\205\263\347\263\273.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 "group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" diff --git "a/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..ee1bdbd433 --- /dev/null +++ "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,13 @@ +# 计算机CPU、硬盘、内存以及指令之间的关系 + +by 2415980327 + +​ 但凡常见的事物,总会让人习以为常。伴随着计算机在在现代社会中的普及,人们可以日常生活中随处见到计算机在辅助我们进行各种各样的工作。在未经深究的情况下,我们就自然而然的认为,计算机本身就是这个样子。计算机实际上是什么样,我也不清楚。从小学知道这种事物开始,也是慢慢地在了解这种机器。 + +​ 就我所知,我们目前使用的常见的个人计算机在概念上是属于图灵机的。图灵机这个概念是研究将人们数学的运算过程使用机器来进行代替。它是由一个纸带作为输入与输出,同时使用一个处理器来处理这个纸带,达到运算的目的。类似是程序中函数的概念。或许来说程序中函数的概念要比图灵机的概念晚得多,但是作为一个程序员来说,就没有必要非要把自己置于一无所知的情形下再去学习这个概念。既然我已经知道了函数这个概念,那我就用函数来类比图灵机的概念吧。类似于函数的输入-处理-输出这种结构,想必大家也是一目了然的。 + +​ 图灵机只是作为一个理论上模拟出来的机器,只是为了证明这样是可行的。而实际上实现的是冯诺依曼体系结构的计算机。我们日常所见的计算机也是基于这种体系结构建立起来的。冯诺依曼体系结构是分为五部分的,包括存储器,运算器、控制器、输入设备和输出设备。同样也可以类比为函数,输入设备输出设备同图灵机一样只是作为输入与输出,剩下的存储器、控制器与运算器是做为处理器存在的,类似于函数中的变量,逻辑结构与逻辑运算的存在。存储器就是存储一些信息,运算器是做一些实际上的运算,控制器是进行程序指令的跳转,来实现各种不同的结构。 + +​ 而本文提及的CPU就是运算器与控制器的集合,内存就相当于函数中的变量,也就是相当于存储器。那硬盘是用来做什么的呢?硬盘是用作数据的持久化,一方面由于成本较低,所以是当做大量的数据存储单元。另一方面由于内存断电会清空,所以使用硬盘来存储信息更为方便。所以我认为硬盘应该是游离于这个体系之外的辅助设备。类似于程序的数据库或者程序的文本存档。 + +​ 指令与数据平时存储于硬盘之中,在需要运行程序时,将其读入到内存中来,通过CPU来处理内存中的数据。那么CPU为什么不直接处理硬盘中的数据呢?因为硬盘相比于内存的访问速度太慢了,相比于CPU的处理速度更是慢到离谱,所以才需要内存的当做存储器为CPU的处理工作提供支持。 \ No newline at end of file From 0b3b7ef330d5ab00397e5c2bea0cc5f8ca417f34 Mon Sep 17 00:00:00 2001 From: tennysons Date: Sat, 25 Feb 2017 21:25:14 +0800 Subject: [PATCH 117/432] README --- group14/296933284/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 group14/296933284/README.md diff --git a/group14/296933284/README.md b/group14/296933284/README.md new file mode 100644 index 0000000000..b2040bf4d1 --- /dev/null +++ b/group14/296933284/README.md @@ -0,0 +1,9 @@ + + +# 2017年编程提高(Java) 作业、练习、总结的记录 + +DataStructuresTest // 基本数据结构Java实现 + + + + From 685a153894f2cb3b1fca3ba96dd09ab40fe62c7b Mon Sep 17 00:00:00 2001 From: peter <729245768@qq.com> Date: Sat, 25 Feb 2017 21:27:18 +0800 Subject: [PATCH 118/432] =?UTF-8?q?=E4=B8=BAArrayList=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/coding_170225/ArrayListTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java diff --git a/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java b/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java new file mode 100644 index 0000000000..bde9c7a637 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java @@ -0,0 +1,39 @@ +package main.coding_170225; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by peter on 2017/2/25. + */ +public class ArrayListTest extends TestCase { + @Test + public void add() throws Exception { + } + + @Test + public void add1() throws Exception { + + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add(12); + arrayList.add(15); + arrayList.add(17); + Assert.assertEquals(3,arrayList.size()); + } + +} \ No newline at end of file From 43fc71637bfac0b55d6afe08a8f1eca5131bee79 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 21:27:23 +0800 Subject: [PATCH 119/432] Add Linked List --- .../src/com/coding/basic/LinkedList.java | 158 ++++++++++++++++-- 1 file changed, 147 insertions(+), 11 deletions(-) diff --git a/group03/619224754/src/com/coding/basic/LinkedList.java b/group03/619224754/src/com/coding/basic/LinkedList.java index e2c4e5e795..8ca8159baf 100644 --- a/group03/619224754/src/com/coding/basic/LinkedList.java +++ b/group03/619224754/src/com/coding/basic/LinkedList.java @@ -4,39 +4,175 @@ public class LinkedList implements List { private Node head; - public void add(Object o){ - + public void add(Object o) { + if(head == null) { + head = new Node(); + head.data = o; + } + else { + Node newNode = new Node(); + newNode.data = o; + Node lastNode = head; + while(head.next != null) { + lastNode = head.next; + } + lastNode.next = newNode; + } } - public void add(int index , Object o){ + + public void add(int index , Object o) { + if(index >= this.size()) + throw new IndexOutOfBoundsException("Index out of bound"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0) { + newNode.next = this.head; + this.head = newNode; + } + else if(index == this.size()) { + Node curNode = this.head; + while(curNode.next != null){ + curNode = curNode.next; + } + curNode.next = newNode; + } + else { + Node beforeNode = this.head; + Node afterNode = null; + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + afterNode = beforeNode.next; + newNode.next = afterNode; + beforeNode.next = newNode; + } } + public Object get(int index){ - return null; + Node retNode = this.head; + + if(index < 0){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + + if(index != 0) { + for(int i = 0; i < index; i++) { + retNode = retNode.next; + } + } + + return retNode.data; } + public Object remove(int index){ - return null; + Node beforeNode = null; + Node afterNode = null; + Node removedNode = null; + if(index == 0) { + removedNode = this.head; + this.head = this.head.next; + } + else { + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + removedNode = beforeNode.next; + afterNode = removedNode.next; + beforeNode.next = afterNode; + } + + + return removedNode.data; } public int size(){ - return -1; + int i = 0; + if(this.head == null) + return 0; + + Node curNode = this.head; + while(curNode != null){ + curNode = curNode.next; + i++; + } + return i; } public void addFirst(Object o){ - + Node firstNode = new Node(); + firstNode.data = o; + firstNode.next = this.head; + this.head = firstNode; } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + if(this.size() == 0){ + this.head = newNode; + } + Node curNode = this.head; + while(curNode.next != null) { + curNode = curNode.next; + } + curNode.next = newNode; } - public Object removeFirst(){ - return null; + + public Object removeFirst() { + Node retNode = this.head; + this.head = this.head.next; + + return retNode; } - public Object removeLast(){ - return null; + + public Object removeLast() { + Node curNode = null; + if(this.size() == 0) { + curNode = null; + } + else if(this.size() == 1) { + curNode = this.head; + this.head = null; + return curNode; + } + else { + Node beforeNode = this.head; + for (int i = 1; i < this.size() - 1; i++) { + beforeNode = beforeNode.next; + } + curNode = beforeNode.next; + beforeNode.next = null; + } + + return curNode; } + public Iterator iterator(){ return null; } + private class LinkedListIterator implements Iterator { + + private Node curNode = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.curNode.next != null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return this.curNode.next; + } + + + + } + private static class Node{ Object data; From aa1dafb2ac2dbac4d3ffb0d5fb07ab7ab3cf9eeb Mon Sep 17 00:00:00 2001 From: peter <729245768@qq.com> Date: Sat, 25 Feb 2017 21:28:21 +0800 Subject: [PATCH 120/432] =?UTF-8?q?=E4=B8=BABinaryTree=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/coding_170225/BinaryTreeTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java diff --git a/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java b/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java new file mode 100644 index 0000000000..1492323570 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java @@ -0,0 +1,37 @@ +package main.coding_170225; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by peter on 2017/2/25. + */ +public class BinaryTreeTest extends TestCase { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testInsert() throws Exception { + + } + + @Test + public void testPrintBinaryTreeNode() throws Exception { + + } + + @Test + public void testGetRoot() throws Exception { + + } + +} \ No newline at end of file From c39f7f93c2d4a957bc7d87efa3ae76edc275fdd2 Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 21:32:59 +0800 Subject: [PATCH 121/432] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayList,LinkedList,?= =?UTF-8?q?Stack,Queue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group03/617187912/Learning02/.classpath | 6 + group03/617187912/Learning02/.gitignore | 1 + group03/617187912/Learning02/.project | 17 +++ .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 117 +++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 33 +++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 137 ++++++++++++++++++ .../Learning02/src/com/coding/basic/List.java | 10 ++ .../src/com/coding/basic/Queue.java | 33 +++++ .../src/com/coding/basic/Stack.java | 48 ++++++ 12 files changed, 423 insertions(+) create mode 100644 group03/617187912/Learning02/.classpath create mode 100644 group03/617187912/Learning02/.gitignore create mode 100644 group03/617187912/Learning02/.project create mode 100644 group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs create mode 100644 group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/617187912/Learning02/src/com/coding/basic/ArrayList.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Iterator.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/LinkedList.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/List.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Queue.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Stack.java diff --git a/group03/617187912/Learning02/.classpath b/group03/617187912/Learning02/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group03/617187912/Learning02/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning02/.gitignore b/group03/617187912/Learning02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning02/.project b/group03/617187912/Learning02/.project new file mode 100644 index 0000000000..12dfa1118c --- /dev/null +++ b/group03/617187912/Learning02/.project @@ -0,0 +1,17 @@ + + + 617187912Learning02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs b/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/617187912/Learning02/.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/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9e22bd0c0b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList(){ + this(64); + } + + public ArrayList(int intSize) { + elementData = new Object[intSize]; + } + + public void add(Object o) { + checkMaxSize(); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkMaxSize(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void checkIndexRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限�?"); + } + } + + private void checkMaxSize() { + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + public Object get(int index) { + checkIndexRange(index); + return elementData[index]; + } + + public Object remove(int index) { + Object o = get(index); + if (index == size - 1) { + elementData[index] = null; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + private int current = 0; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + if (current >= size) { + throw new NoSuchElementException(); + } + return elementData[current++]; + } + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(5); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + arrayList.add(6); + System.out.println(arrayList.get(1)); + arrayList.add(1, 100); + System.out.println(arrayList.get(1)); + System.out.println(arrayList.size); + System.out.println(arrayList.remove(2)); + System.out.println(arrayList.get(2)); + + + ArrayList mixArraylist = new ArrayList(5); + mixArraylist.add("String"); + mixArraylist.add(100); + mixArraylist.add('f'); + mixArraylist.add(3.1f); + mixArraylist.add(4L); + System.out.println(mixArraylist.get(1)); + mixArraylist.add(1, 101); + System.out.println(mixArraylist.get(1)); + System.out.println(mixArraylist.size); + System.out.println(mixArraylist.remove(2)); + System.out.println(mixArraylist.get(2)); + } +} \ No newline at end of file diff --git a/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7b6479d535 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +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/617187912/Learning02/src/com/coding/basic/Iterator.java b/group03/617187912/Learning02/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..017cbc4240 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..88a4c6c31b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkIndexRange(index); + if (index == 0) { + head = new Node(o, head); + size++; + } else { + Node nd = getNode(index-1); + nd.next = new Node(o, nd.next); + size++; + } + } + + public Object get(int index) { + return getNode(index).data; + } + + private Node getNode(int index) { + Node nd = head; + for (int i = 0; i < index; i++) { + nd = nd.next; + } + return nd; + } + + public Object remove(int index) { + if (size == 0) { + throw new NoSuchElementException(); + } + checkIndexRange(index); + if (index == 0) { + Object o = head.data; + head = head.next; + size--; + return o; + } else { + Node nd = getNode(index - 1); + Object o = nd.next.data; + nd.next = nd.next.next; + size--; + return o; + } + } + + private void checkIndexRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限?"); + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0, o); + } + + public void addLast(Object o) { + if (size == 0) { + addFirst(o); + } else { + add(size, o); + } + } + + 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 current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Object next() { + Object o = current.data; + current = current.next; + return o; + } + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + for (int i = 0; i < list.size; i++) { + System.out.println(list.get(i)); + } + System.out.println(list.get(2)); + list.add(2, 100); + System.out.println(list.get(2)); + list.addFirst(10); + System.out.println(list.get(2)); + list.addLast(100); + System.out.println(list.remove(1)); + System.out.println(list.removeFirst()); + System.out.println(list.removeLast()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/List.java b/group03/617187912/Learning02/src/com/coding/basic/List.java new file mode 100644 index 0000000000..cd5130be3b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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/617187912/Learning02/src/com/coding/basic/Queue.java b/group03/617187912/Learning02/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..39e1a8b60b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Queue.java @@ -0,0 +1,33 @@ +package com.coding.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 static void main(String[] args) { + Queue que = new Queue(); + que.enQueue(10); + que.enQueue(11); + que.enQueue(12); + System.out.println(que.deQueue()); + System.out.println(que.isEmpty()); + que.deQueue(); + que.deQueue(); + System.out.println(que.isEmpty()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/Stack.java b/group03/617187912/Learning02/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1ee047ba4a --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Stack.java @@ -0,0 +1,48 @@ +package com.coding.basic; + + +import java.util.EmptyStackException; + +import javax.lang.model.element.QualifiedNameable; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkIsEmpty(); + return elementData.remove(size()-1); + } + + private void checkIsEmpty() { + if (isEmpty()){ + throw new EmptyStackException(); + } + } + + public Object peek(){ + checkIsEmpty(); + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } + public static void main(String[] args) { + Stack que = new Stack(); + que.push(10); + que.push(11); + que.push(12); + System.out.println(que.peek()); + System.out.println(que.isEmpty()); + System.out.println(que.pop()); + System.out.println(que.pop()); + que.pop(); + System.out.println(que.isEmpty()); + } +} From ad1d702c1f485b462bed971e4f21082ccc7405fa Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 21:43:07 +0800 Subject: [PATCH 122/432] add queue and stack --- group03/619224754/src/com/coding/basic/Queue.java | 12 ++++++++---- group03/619224754/src/com/coding/basic/Stack.java | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/group03/619224754/src/com/coding/basic/Queue.java b/group03/619224754/src/com/coding/basic/Queue.java index 36e516e266..de53482312 100644 --- a/group03/619224754/src/com/coding/basic/Queue.java +++ b/group03/619224754/src/com/coding/basic/Queue.java @@ -2,18 +2,22 @@ public class Queue { - public void enQueue(Object o){ + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o); } public Object deQueue(){ - return null; + Object ret = list.removeFirst(); + return ret; } public boolean isEmpty(){ - return false; + return list.size() == 0; } public int size(){ - return -1; + return list.size(); } } diff --git a/group03/619224754/src/com/coding/basic/Stack.java b/group03/619224754/src/com/coding/basic/Stack.java index a5a04de76d..cf10ecc1b3 100644 --- a/group03/619224754/src/com/coding/basic/Stack.java +++ b/group03/619224754/src/com/coding/basic/Stack.java @@ -4,19 +4,22 @@ public class Stack { private ArrayList elementData = new ArrayList(); public void push(Object o){ + this.elementData.add(o); } public Object pop(){ - return null; + Object ret = this.elementData.remove(this.elementData.size() - 1); + return ret; } public Object peek(){ + Object ret = this.elementData.get(this.elementData.size() - 1); return null; } public boolean isEmpty(){ - return false; + return this.elementData.size() == 0; } public int size(){ - return -1; + return this.elementData.size(); } } From e6aa67f347ee227a153841de4165e406d87ce995 Mon Sep 17 00:00:00 2001 From: Kimisme Date: Sat, 25 Feb 2017 22:05:01 +0800 Subject: [PATCH 123/432] 001DataStructure --- group07/178007127/001DataStructure/.classpath | 8 + group07/178007127/001DataStructure/.gitignore | 2 + group07/178007127/001DataStructure/.project | 18 ++ ...ource.ide.eclipse.gradle.core.import.prefs | 9 + ...springsource.ide.eclipse.gradle.core.prefs | 5 + ...ingsource.ide.eclipse.gradle.refresh.prefs | 8 + .../.settings/org.eclipse.jdt.core.prefs | 11 + group07/178007127/001DataStructure/README.md | 2 + .../178007127/001DataStructure/build.gradle | 9 + .../com/easy/util/myarraylist/ArrayList.java | 91 ++++++++ .../easy/util/mylinkedlist/LinkedList.java | 194 ++++++++++++++++++ .../java/com/easy/util/myqueue/Queue.java | 27 +++ .../java/com/easy/util/mystack/Stack.java | 29 +++ .../easy/util/myarraylist/ArrayListTest.java | 57 +++++ .../util/mylinkedlist/LinkedListTest.java | 77 +++++++ 15 files changed, 547 insertions(+) create mode 100644 group07/178007127/001DataStructure/.classpath create mode 100644 group07/178007127/001DataStructure/.gitignore create mode 100644 group07/178007127/001DataStructure/.project create mode 100644 group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs create mode 100644 group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs create mode 100644 group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs create mode 100644 group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs create mode 100644 group07/178007127/001DataStructure/README.md create mode 100644 group07/178007127/001DataStructure/build.gradle create mode 100644 group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java create mode 100644 group07/178007127/001DataStructure/src/main/java/com/easy/util/mylinkedlist/LinkedList.java create mode 100644 group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java create mode 100644 group07/178007127/001DataStructure/src/main/java/com/easy/util/mystack/Stack.java create mode 100644 group07/178007127/001DataStructure/src/test/java/com/easy/util/myarraylist/ArrayListTest.java create mode 100644 group07/178007127/001DataStructure/src/test/java/com/easy/util/mylinkedlist/LinkedListTest.java diff --git a/group07/178007127/001DataStructure/.classpath b/group07/178007127/001DataStructure/.classpath new file mode 100644 index 0000000000..84b630a879 --- /dev/null +++ b/group07/178007127/001DataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/178007127/001DataStructure/.gitignore b/group07/178007127/001DataStructure/.gitignore new file mode 100644 index 0000000000..4a95481e61 --- /dev/null +++ b/group07/178007127/001DataStructure/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/build/ diff --git a/group07/178007127/001DataStructure/.project b/group07/178007127/001DataStructure/.project new file mode 100644 index 0000000000..430da90497 --- /dev/null +++ b/group07/178007127/001DataStructure/.project @@ -0,0 +1,18 @@ + + + 001DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.springsource.ide.eclipse.gradle.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs new file mode 100644 index 0000000000..67ed2b404e --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs @@ -0,0 +1,9 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleImportPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +enableDependendencyManagement=true +projects=; diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs new file mode 100644 index 0000000000..f846d71532 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs @@ -0,0 +1,5 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences +#Thu Feb 23 15:58:56 CST 2017 +build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=; +org.springsource.ide.eclipse.gradle.linkedresources= +org.springsource.ide.eclipse.gradle.rootprojectloc= diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs new file mode 100644 index 0000000000..13198da612 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs @@ -0,0 +1,8 @@ +#org.springsource.ide.eclipse.gradle.core.actions.GradleRefreshPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +useHierarchicalNames=false diff --git a/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/178007127/001DataStructure/.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/group07/178007127/001DataStructure/README.md b/group07/178007127/001DataStructure/README.md new file mode 100644 index 0000000000..b071403fae --- /dev/null +++ b/group07/178007127/001DataStructure/README.md @@ -0,0 +1,2 @@ +# 001DataStructure +001DataStructure diff --git a/group07/178007127/001DataStructure/build.gradle b/group07/178007127/001DataStructure/build.gradle new file mode 100644 index 0000000000..670cff0568 --- /dev/null +++ b/group07/178007127/001DataStructure/build.gradle @@ -0,0 +1,9 @@ +apply plugin:'java' + +repositories{ + mavenCentral() +} + +dependencies{ + compile group: 'junit', name: 'junit', version: '4.12' +} \ No newline at end of file diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java new file mode 100644 index 0000000000..112b6262d2 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java @@ -0,0 +1,91 @@ +package com.easy.util.myarraylist; + +public class ArrayList { + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[] {}; + } + + public ArrayList(int initialCapacity) { + this.elementData = new Object[initialCapacity]; + } + + public void add(Object o) { + if (elementData.length <= size) { + grow(1); + elementData[size] = o; + size++; + } else { + elementData[size] = o; + size++; + } + } + + public void add(int index, Object o) { + rangeCheckForAdd(index); + grow(1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + if (index < size) { + Object removeValue = elementData[index]; + Object[] dest = new Object[size - 1]; + System.arraycopy(elementData, 0, dest, 0, index); + System.arraycopy(elementData, index + 1, dest, index, size - index - 1); + elementData = dest; + size--; + return removeValue; + } else { + return null; + } + } + + public int size() { + return this.size; + } + + private void grow(int minCapacity) { + Object[] dest = new Object[elementData.length + minCapacity]; + System.arraycopy(elementData, 0, dest, 0, elementData.length); + elementData = dest; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Object object : elementData) { + sb.append(object + ","); + } + String temp = sb.toString(); + temp = temp.substring(0, temp.length() - 1); + return "[" + temp + "]"; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ",Size:" + size; + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/mylinkedlist/LinkedList.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/mylinkedlist/LinkedList.java new file mode 100644 index 0000000000..697b0b8476 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/mylinkedlist/LinkedList.java @@ -0,0 +1,194 @@ +package com.easy.util.mylinkedlist; + +import java.util.NoSuchElementException; + +public class LinkedList { + + private Node first; + private Node last; + int size=0; + + public LinkedList(){ + + } + + public void add(Object o){ + linkLast(o); + } + public void add(int index , Object o){ + checkPositionIndex(index); + + if(index==size){ + linkLast(o); + }else if(index==0){ + linkFirst(o); + }else { + linkBefore(index, o); + } + } + + + private void linkBefore(int index,Object o){ + Node pred=node(index-1); + Node newNode=new Node(o, node(index)); + pred.next=newNode; + size++; + } + + public Object get(int index){ + return node(index).item; + } + public boolean remove(Object o){ + Node temp=first; + if(o==null){ + for(int i=0;i0&&index<=size; + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java new file mode 100644 index 0000000000..17238af545 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java @@ -0,0 +1,27 @@ +package com.easy.util.myqueue; + +import com.easy.util.mylinkedlist.LinkedList; + +public class Queue { + + LinkedList linkedList; + public Queue(){ + linkedList=new LinkedList(); + } + + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return size()==0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/mystack/Stack.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/mystack/Stack.java new file mode 100644 index 0000000000..83bfe03b99 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/mystack/Stack.java @@ -0,0 +1,29 @@ +package com.easy.util.mystack; + +import com.easy.util.mylinkedlist.LinkedList; + +public class Stack { + private LinkedList elementData; + + public Stack(){ + elementData=new LinkedList(); + } + + public void push(Object o){ + elementData.addLast(o); + } + + public Object pop(){ + return elementData.removeLast(); + } + + public Object peek(){ + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group07/178007127/001DataStructure/src/test/java/com/easy/util/myarraylist/ArrayListTest.java b/group07/178007127/001DataStructure/src/test/java/com/easy/util/myarraylist/ArrayListTest.java new file mode 100644 index 0000000000..f454374090 --- /dev/null +++ b/group07/178007127/001DataStructure/src/test/java/com/easy/util/myarraylist/ArrayListTest.java @@ -0,0 +1,57 @@ +package com.easy.util.myarraylist; + +import org.junit.Assert; +import org.junit.Test; + +import com.easy.util.myarraylist.ArrayList; + +public class ArrayListTest { + + @Test + public void test_add(){ + ArrayList list=new ArrayList(); + list.add("aa"); + list.add("bb"); + Assert.assertEquals("[aa,bb]", list.toString()); + } + + @Test + public void test_add_object(){ + ArrayList list=new ArrayList(); + list.add("aa"); + list.add("bb"); + list.add(1, "aabb"); + Assert.assertEquals("[aa,aabb,bb]", list.toString()); + } + + @Test + public void test_get(){ + ArrayList list=new ArrayList(); + list.add("aa"); + list.add("bb"); + Object element =list.get(1); + Assert.assertEquals("bb", element); + } + + @Test + public void test_remove_int(){ + ArrayList list=new ArrayList(); + list.add("aa"); + list.add("bb"); + list.add(1, "aabb"); + list.remove(1); + Assert.assertEquals("[aa,bb]", list.toString()); + } + + @Test + public void test_size(){ + ArrayList list=new ArrayList(); + list.add("aa"); + list.add("bb"); + list.add(1, "aabb"); + list.remove(1); + Assert.assertEquals(2, list.size()); + } + + +} diff --git a/group07/178007127/001DataStructure/src/test/java/com/easy/util/mylinkedlist/LinkedListTest.java b/group07/178007127/001DataStructure/src/test/java/com/easy/util/mylinkedlist/LinkedListTest.java new file mode 100644 index 0000000000..816ba6d1e7 --- /dev/null +++ b/group07/178007127/001DataStructure/src/test/java/com/easy/util/mylinkedlist/LinkedListTest.java @@ -0,0 +1,77 @@ +package com.easy.util.mylinkedlist; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.Test; + +import com.easy.util.mylinkedlist.LinkedList; + +public class LinkedListTest { + + @Test + public void test_add_object() { + LinkedList list=new LinkedList(); + list.add("aa"); + list.add("bb"); + assertEquals("[aa,bb]", list.toString()); + } + + @Test + public void test_add_int_object() { + LinkedList list=new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add(1,"aabb"); + assertEquals("[aa,aabb,bb]", list.toString()); + } + + @Test + public void testGet() { + LinkedList list=new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add(1,"aabb"); + assertEquals("aa", list.get(0)); + assertEquals("aabb", list.get(1)); + assertEquals("bb", list.get(2)); + } + + @Test + public void testRemove() { + LinkedList list=new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + boolean b = list.remove("bb"); + assertEquals(true, b); + assertEquals("[aa,cc]", list.toString()); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + + @Test + public void testAddFirst() { + fail("Not yet implemented"); + } + + @Test + public void testAddLast() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveFirst() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveLast() { + fail("Not yet implemented"); + } + +} From fb20d12f3a5947935688f84503143457fcae4e75 Mon Sep 17 00:00:00 2001 From: fei028 Date: Sat, 25 Feb 2017 22:11:17 +0800 Subject: [PATCH 124/432] chushidaoru --- group01/2137642225/work01/.classpath | 7 + group01/2137642225/work01/.gitignore | 1 + group01/2137642225/work01/.project | 17 ++ .../src/com/coding/mybasic/ArrayList.java | 139 +++++++++++ .../com/coding/mybasic/BinaryTreeNode.java | 73 ++++++ .../src/com/coding/mybasic/Iterator.java | 7 + .../src/com/coding/mybasic/LinkedList.java | 223 ++++++++++++++++++ .../work01/src/com/coding/mybasic/List.java | 10 + .../work01/src/com/coding/mybasic/Queue.java | 30 +++ .../work01/src/com/coding/mybasic/Stack.java | 35 +++ .../src/com/coding/test/TestArrayList.java | 81 +++++++ .../com/coding/test/TestBinaryTreeNode.java | 32 +++ .../src/com/coding/test/TestLinkedList.java | 73 ++++++ .../work01/src/com/coding/test/TestQueue.java | 36 +++ .../work01/src/com/coding/test/TestStack.java | 49 ++++ 15 files changed, 813 insertions(+) create mode 100644 group01/2137642225/work01/.classpath create mode 100644 group01/2137642225/work01/.gitignore create mode 100644 group01/2137642225/work01/.project create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/Iterator.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/List.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/Queue.java create mode 100644 group01/2137642225/work01/src/com/coding/mybasic/Stack.java create mode 100644 group01/2137642225/work01/src/com/coding/test/TestArrayList.java create mode 100644 group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java create mode 100644 group01/2137642225/work01/src/com/coding/test/TestLinkedList.java create mode 100644 group01/2137642225/work01/src/com/coding/test/TestQueue.java create mode 100644 group01/2137642225/work01/src/com/coding/test/TestStack.java diff --git a/group01/2137642225/work01/.classpath b/group01/2137642225/work01/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group01/2137642225/work01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group01/2137642225/work01/.gitignore b/group01/2137642225/work01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/2137642225/work01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/2137642225/work01/.project b/group01/2137642225/work01/.project new file mode 100644 index 0000000000..f8dde642e5 --- /dev/null +++ b/group01/2137642225/work01/.project @@ -0,0 +1,17 @@ + + + work01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java b/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java new file mode 100644 index 0000000000..1826ee7c50 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java @@ -0,0 +1,139 @@ +package com.coding.mybasic; + +public class ArrayList implements List{ + + private static final int DEF_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList(){ + elementData = new Object[DEF_CAPACITY]; + } + + public ArrayList(int initCapacity) { + if(initCapacity <= 0){ + throw new RuntimeException("初始化长度必须大于0"); + } + elementData = new Object[initCapacity]; + } + + @Override + public void add(Object element) { + checkArrayOutOfRange(); + elementData[size++] = element; + } + + + @Override + public void add(int index, Object element) { + // 末尾插入 + if(index == size){ + add(element); + return; + } + // index 在 0到size 之间,index之后元素要后移 + checkIndex(index); + checkArrayOutOfRange(); + moveBackwardElement(index); + elementData[index] = element; + size++; + } + + + @Override + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Object temp = elementData[index]; + moveForwardElement(index); + elementData[size--] = null; + return temp; + } + + + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int i = 0; + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + checkIndex(i); + return elementData[i++]; + } + + } + + /** + * 数组增长 + * @param newCapacity 新数组容量 + */ + private void grow(int newCapacity) { + Object[] dest = new Object[newCapacity]; + System.arraycopy(elementData, 0, dest , 0, elementData.length); + elementData = dest; + } + + /** + * 检查index index >=0 且 < size + * @param index + * @throws Exception + */ + private void checkIndex(int index) { + if(index < 0){ + throw new RuntimeException("index 必须大于0"); + } + // 越界 + if(index >= size){ + throw new RuntimeException("index 必须小于size:" + size); + } + } + + /** + * 检查数组容量是否已满,已满则扩容 + */ + private void checkArrayOutOfRange() { + if(size >= elementData.length){ + // 扩容 默认新容量是原来容量的2倍 + grow(elementData.length * 2); + } + } + + /** + * 后移元素,从index开始 + * @param index + */ + private void moveBackwardElement(int index) { + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + } + /** + * 前移元素,从index开始 + * @param index + */ + private void moveForwardElement(int index) { + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + } + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java b/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java new file mode 100644 index 0000000000..21bd8f696f --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +package com.coding.mybasic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object 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 == null){ + throw new RuntimeException("不能插入空值"); + } + BinaryTreeNode searchNode = search(this,o); + if(isExistData(searchNode,o)){ + throw new RuntimeException("该值已存在 无法插入"); + } + if(searchNode != null){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.setData(o); + if(searchNode.data.intValue() > o.intValue()){ + searchNode.setLeft(binaryTreeNode); + }else{ + searchNode.setRight(binaryTreeNode); + } + } else { + throw new RuntimeException("根节点未赋值,无法插入"); + } + return this; + } + + private boolean isExistData(BinaryTreeNode searchNode,Integer data) { + return searchNode != null && searchNode.data.intValue() == data.intValue(); + + } + + private BinaryTreeNode search(BinaryTreeNode binaryTreeNode, Integer data) { + if(binaryTreeNode == null || binaryTreeNode.data == null){ + return null; + } + Integer curNodeData = binaryTreeNode.data; + if(curNodeData.intValue() > data.intValue()){// 左 curNodeData > data + if(binaryTreeNode.left != null){ + return search(binaryTreeNode.left,data); + } + }else if(curNodeData.intValue() < data.intValue()){ + if(binaryTreeNode.right != null){ + return search(binaryTreeNode.right,data); + } + + } + return binaryTreeNode; + } + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java b/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java new file mode 100644 index 0000000000..622cc5b902 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.mybasic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java new file mode 100644 index 0000000000..43398abc03 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java @@ -0,0 +1,223 @@ +package com.coding.mybasic; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size; + + public LinkedList() { + } + + @Override + public void add(Object element) { + if(head == null){ + addHead(element); + }else{ + addLast(element); + } + } + + @Override + public void add(int index, Object element) { + if(index == size){ + add(element); + return; + } + + if(index == 0){ + addFirst(element); + return; + } + checkIndex(index); + insertElement(index - 1,element); + } + + + @Override + public Object get(int index) { + checkIndex(index); + Node node = getNodeByIndex(index); + return node != null ? node.data : null; + } + + @Override + public Object remove(int index) { + + checkIndex(index); + Object element = null; + if(index == 0){ + element = removeFirst(); + } + else if(index == (size - 1)){ + element = removeLast(); + } + else { + element = removeMiddle(index); + } + size--; + return element; + } + + + @Override + public int size() { + return size; + } + + + @Override + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + private Node node = head; + int i = 0; + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + checkIndex(i); + Object element = node.data; + node = node.next; + i++; + return element; + } + + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head.next; + head = node; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + return removeFirstNode(); + } + public Object removeLast(){ + return removeLastNode(); + } + private Object removeMiddle(int index) { + Node temp = getNodeByIndex(index - 1); + Node removeNode = temp.next; + Object element = removeNode.data; + temp.next = removeNode.next; + removeNode = null; + return element; + } + + /** + * 检查index index >=0 且 < size + * @param index + * @throws Exception + */ + private void checkIndex(int index) { + if(index < 0){ + throw new RuntimeException("index 必须大于0"); + } + // 越界 + if(index >= size){ + throw new RuntimeException("index 必须小于size:" + size); + } + } + + /** + * 添加head + * @param element + */ + private void addHead(Object element) { + head = new Node(); + head.data = element; + head.next = null; + last = head; + size++; + } + /** + * 插入序号在0-size之间的元素,不包含0和size位置 + * @param index + * @param element + */ + private void insertElement(int index, Object element) { + + Node temp = getNodeByIndex(index); + if(temp != null){ + Node node = new Node(); + node.data = element; + node.next = temp.next; + temp.next = node; + } + size++; + } + /** + * 获取下标为index的元素 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + Node temp = head; + int i = 0; + + while(i < size){ + if(i == index){ + return temp; + } + temp = temp.next; + i++; + } + + return null; + } + /** + * 移除最后一个元素 + * @return + */ + private Object removeLastNode() { + Node node = getNodeByIndex(size - 2); + Node lastNode = node.next; + Object element = lastNode.data; + lastNode = null; + last = node; + return element; + } + /** + * 移除第一个元素 + * @return + */ + private Object removeFirstNode() { + Node node = head.next; + Object element = head.data; + head = null; + head = node; + return element; + } + + + + private static class Node{ + Object data; + Node next; + public Node() { + } + @SuppressWarnings("unused") + public Node(Object data, Node next) { + super(); + this.data = data; + this.next = next; + } + + + } +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/List.java b/group01/2137642225/work01/src/com/coding/mybasic/List.java new file mode 100644 index 0000000000..87a58a6c4c --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/List.java @@ -0,0 +1,10 @@ +package com.coding.mybasic; + +public interface List { + public void add(Object element); + public void add(int index, Object element); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Queue.java b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java new file mode 100644 index 0000000000..7a3e8f94d5 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.mybasic; + +public class Queue { + private LinkedList linkedList = new LinkedList(); + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + checkEmptyQueue(); + return linkedList.remove(size() - 1); + } + + public boolean isEmpty(){ + return size() <= 0; + } + + public int size(){ + return linkedList.size(); + } + + /** + * 检查队列是否为空 + */ + private void checkEmptyQueue() { + if(isEmpty()){ + throw new RuntimeException("size:" + size() + " 空队列"); + } + } +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Stack.java b/group01/2137642225/work01/src/com/coding/mybasic/Stack.java new file mode 100644 index 0000000000..f50e686317 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.mybasic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkEmptyStack(); + return elementData.remove(size() - 1); + } + + public Object peek(){ + checkEmptyStack(); + Object element = elementData.get(size() - 1); + return element; + } + + public boolean isEmpty(){ + return size() <= 0; + } + public int size(){ + return elementData.size(); + } + /** + * 检查栈是否为空 + */ + private void checkEmptyStack() { + if(isEmpty()){ + throw new RuntimeException("size:" + size() + " 空栈"); + } + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestArrayList.java b/group01/2137642225/work01/src/com/coding/test/TestArrayList.java new file mode 100644 index 0000000000..8bd8952195 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestArrayList.java @@ -0,0 +1,81 @@ +package com.coding.test; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.ArrayList; +import com.coding.mybasic.Iterator; +import com.coding.mybasic.List; + +public class TestArrayList { + + private List list; + @Before + public void before() { + list = new ArrayList(); + } + + @Test + public void testAddObject() { + list.add("ele"); + Assert.assertEquals("ele", list.get(0)); + } + + @Test + public void testAddIntObject() { + + for (int i = 0; i < 5; i++) { + list.add(i,i); + Assert.assertEquals(i, list.get(i)); + } + + } + + @Test + public void testGet() { + list.add("ss"); + Assert.assertEquals("ss", list.get(0)); + } + + @Test + public void testRemove() { + list.add("we"); + list.add(1, "gga"); + list.add(0, "start"); + list.add(3, "end"); + + Assert.assertEquals("end", list.remove(3)); + + } + + @Test + public void testSize() { + + for (int i = 0; i < 10; i++) { + list.add(i); + } + + Assert.assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + + for (int i = 0; i < 10; i++) { + list.add(i); + } + Iterator iterator = list.iterator(); + int i = 0; + while(iterator.hasNext()){ + Assert.assertEquals(i++, iterator.next()); + } + } + + @After + public void after(){ + + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java b/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java new file mode 100644 index 0000000000..662bb55570 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.BinaryTreeNode; + +public class TestBinaryTreeNode { + + private BinaryTreeNode node; + @Before + public void before(){ + node = new BinaryTreeNode(); + } + + @Test + public void testInsert() { + node.insert(1); + node.insert(0); + node.insert(3); + node.insert(-2); + node.insert(-1); + assertEquals(1, node.getData()); + assertEquals(0, node.getLeft().getData()); + assertEquals(3, node.getRight().getData()); + assertEquals(-2, node.getLeft().getLeft().getData()); + assertEquals(-1, node.getLeft().getLeft().getRight().getData()); + } + +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java b/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java new file mode 100644 index 0000000000..57a8b13bb8 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java @@ -0,0 +1,73 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Iterator; +import com.coding.mybasic.LinkedList; +import com.coding.mybasic.List; + +public class TestLinkedList { + + private List list; + + @Before + public void before(){ + list = new LinkedList(); + } + + @Test + public void testAddObject() { + list.add(1); + + System.out.println(list.get(0)); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(0,1); + System.out.println(list.get(0)); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.add(0,1); + System.out.println(list.remove(0)); + assertEquals(0, list.size()); + } + + @Test + public void testSize() { + + for(int i = 0; i < 10; i++){ + list.add(i, i); + } + + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + + for(int i = 0; i < 10; i++){ + list.add(i, i); + } + Iterator iterator = list.iterator(); + int i = 0; + while(iterator.hasNext()){ + assertEquals(i++, iterator.next()); + } + //iterator.next(); + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestQueue.java b/group01/2137642225/work01/src/com/coding/test/TestQueue.java new file mode 100644 index 0000000000..367a44d151 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestQueue.java @@ -0,0 +1,36 @@ +package com.coding.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Queue; + +public class TestQueue { + + private Queue queue; + @Before + public void before(){ + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + } + @Test + public void testEnQueue() { + queue.enQueue(3); + assertEquals(3, queue.size()); + } + + @Test + public void testDeQueue() { + assertEquals(2, queue.deQueue()); + assertEquals(1, queue.deQueue()); + } + + @Test + public void testSize() { + assertEquals(2, queue.size()); + } + +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestStack.java b/group01/2137642225/work01/src/com/coding/test/TestStack.java new file mode 100644 index 0000000000..0e278f2992 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestStack.java @@ -0,0 +1,49 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Stack; + +public class TestStack { + + private Stack stack; + @Before + public void before() { + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + } + + @Test + public void testPush() { + assertEquals(3, stack.peek()); + } + + @Test + public void testPop() { + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + assertEquals(1, stack.pop()); + //stack.pop(); + //System.out.println(stack.size()); + } + + @Test + public void testPeek() { + assertEquals(3, stack.peek()); + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + //assertEquals(1, stack.pop()); + assertEquals(1, stack.peek()); + } + + @Test + public void testSize() { + assertEquals(3, stack.size()); + } + +} From 5918d329620437a1138bf5de5fdabdc5034b8aa1 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 22:17:02 +0800 Subject: [PATCH 125/432] Finished binary tree --- .../src/com/coding/basic/BinaryTreeNode.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java index d7ac820192..58005fb1b4 100644 --- a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java +++ b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.Comparator; + public class BinaryTreeNode { private Object data; @@ -9,24 +11,70 @@ public class BinaryTreeNode { 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; + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.data = o; + int intO = Integer.parseInt(o.toString()); + int intData = Integer.parseInt(this.data.toString()); + if(intO > intData){ + if(this.right == null){ + this.right = treeNode; + } + else { + this.right.insert(o); + } + } + else { + if(this.left == null) { + this.left = treeNode; + } + else { + this.left.insert(o); + } + } + return treeNode; + } + + private class MyComparator implements Comparator { + + @Override + public int compare(BinaryTreeNode arg0, BinaryTreeNode arg1) { + // TODO Auto-generated method stub + int int0 = Integer.parseInt(arg0.data.toString()); + int int1 = Integer.parseInt(arg1.data.toString()); + if(int0 > int1) { + return 1; + } + else if(int0 < int1){ + return -1; + } + + return 0; + + } + + } } From 5d323074adc75b9462e73472796ec3a1da85fb2a Mon Sep 17 00:00:00 2001 From: wangzhengliang <1026626960@qq.com> Date: Sat, 25 Feb 2017 22:25:57 +0800 Subject: [PATCH 126/432] one --- group05/1026626960/.classpath | 6 +++ group05/1026626960/.gitignore | 1 + group05/1026626960/.project | 17 +++++++++ .../1026626960/src/cn/study1/myIterator.java | 5 +++ group05/1026626960/src/cn/study1/myQueue.java | 38 +++++++++++++++++++ group05/1026626960/src/cn/study1/myStack.java | 29 ++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 group05/1026626960/.classpath create mode 100644 group05/1026626960/.gitignore create mode 100644 group05/1026626960/.project create mode 100644 group05/1026626960/src/cn/study1/myIterator.java create mode 100644 group05/1026626960/src/cn/study1/myQueue.java create mode 100644 group05/1026626960/src/cn/study1/myStack.java diff --git a/group05/1026626960/.classpath b/group05/1026626960/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group05/1026626960/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/1026626960/.gitignore b/group05/1026626960/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1026626960/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1026626960/.project b/group05/1026626960/.project new file mode 100644 index 0000000000..a8522c9339 --- /dev/null +++ b/group05/1026626960/.project @@ -0,0 +1,17 @@ + + + 1026626960Coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1026626960/src/cn/study1/myIterator.java b/group05/1026626960/src/cn/study1/myIterator.java new file mode 100644 index 0000000000..a5e5528559 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myIterator.java @@ -0,0 +1,5 @@ +package cn.study1; + +public class myIterator { + // +} diff --git a/group05/1026626960/src/cn/study1/myQueue.java b/group05/1026626960/src/cn/study1/myQueue.java new file mode 100644 index 0000000000..cfb5e061d7 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myQueue.java @@ -0,0 +1,38 @@ +package cn.study1; + +public class myQueue { + private class Node{ + T t; + Node next; + } + private Node first; + private Node last; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void enqueue(T t){ + Node oldlast = last; + last = new Node(); + last.t = t; + last.next = null; + if(isEmpty()){ + first = last; + }else{ + oldlast.next = last; + } + N++; + } + public T dequeue(){ + T t = first.t; + first = first.next; + if(isEmpty()){ + last = null; + } + N--; + return t; + } +} diff --git a/group05/1026626960/src/cn/study1/myStack.java b/group05/1026626960/src/cn/study1/myStack.java new file mode 100644 index 0000000000..8364401c45 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myStack.java @@ -0,0 +1,29 @@ +package cn.study1; + +public class myStack { + private class Node{ + T t; + Node next; + } + private Node first; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void push(T t){ + Node oldfirst = first; + first = new Node(); + first.t = t; + first.next = oldfirst; + N++; + } + public T pop(){ + T t = first.t; + first = first.next; + N--; + return t; + } +} From 0820189c23d6078fe1f4e8c2708127c98b52e490 Mon Sep 17 00:00:00 2001 From: LiANG XUHUI <172487938@qq.com> Date: Sat, 25 Feb 2017 22:29:27 +0800 Subject: [PATCH 127/432] 172487938 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基本数据结构ArrayList,LinkedList,stack,Queue的实现 --- group03/172487938/Iterator.java | 7 + group03/172487938/MyArrayList.java | 186 ++++++++++++++++ group03/172487938/MyLinkedList.java | 274 ++++++++++++++++++++++++ group03/172487938/MyList.java | 27 +++ group03/172487938/Queue.java | 32 +++ group03/172487938/Stack.java | 37 ++++ group03/172487938/TestMyArrayList.java | 36 ++++ group03/172487938/TestMyLinkedList.java | 19 ++ 8 files changed, 618 insertions(+) create mode 100644 group03/172487938/Iterator.java create mode 100644 group03/172487938/MyArrayList.java create mode 100644 group03/172487938/MyLinkedList.java create mode 100644 group03/172487938/MyList.java create mode 100644 group03/172487938/Queue.java create mode 100644 group03/172487938/Stack.java create mode 100644 group03/172487938/TestMyArrayList.java create mode 100644 group03/172487938/TestMyLinkedList.java diff --git a/group03/172487938/Iterator.java b/group03/172487938/Iterator.java new file mode 100644 index 0000000000..e6187ad2fd --- /dev/null +++ b/group03/172487938/Iterator.java @@ -0,0 +1,7 @@ +package 基本数据结构; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public void remove(); +} diff --git a/group03/172487938/MyArrayList.java b/group03/172487938/MyArrayList.java new file mode 100644 index 0000000000..0c00a277ff --- /dev/null +++ b/group03/172487938/MyArrayList.java @@ -0,0 +1,186 @@ +package 基本数据结构; + + +/** + * Created by LIANG on 2017/2/24. + */ +public class MyArrayList implements MyList +{ + public static final int INITIAL_CAPACITTY = 16; + private E[] data = (E[]) new Object[INITIAL_CAPACITTY]; + private static int size = 0; + + public MyArrayList(E[] objects) + { + + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); + size++; + } + } + + public MyArrayList() {} + + ; + + public void ensureCapicity() + { + if (size >= data.length) + { + E[] newData = (E[]) new Object[size * 2]; + System.arraycopy(data, 0, newData, 0, data.length); + data = newData; + } + } + +// public boolean add(E e) +// { +// ensureCapicity(); +// if(e != null) +// data[size++] = e; +// return true; +// } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + ensureCapicity(); + for (int i = size - 1; i >= index; i--) + { + data[i + 1] = data[i]; + } + data[index] = e; + size++; + } + + @Override + public E get(int index) + { + return data[index]; + } + + @Override + public E remove(int index) + { + checkIndex(index); + E e = data[index]; + for (int i = index; i < data.length - 1; i++) + data[i] = data[i + 1]; + + data[size - 1] = null; + size--; + return e; + + } + + private void checkIndex(int index) + { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("输入下标有误"); + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + data = (E[]) new Object[INITIAL_CAPACITTY]; + size = 0; + } + + @Override + public int indexOf(E e) + { + for (int i = 0; i < size; i++) + { + if (e.equals(data[i])) + return i; + } + return -1; + } + + + @Override + public boolean isEmpty() + { + return size == 0; + } + + @Override + public E set(int index, E e) + { + checkIndex(index); + E temp = data[index]; + data[index] = e; + return temp; + } + + @Override + public boolean contains(E e) + { + for (int i = 0; i < size; i++) + { + if(data[i].equals(e)) + return true; + } + return false; + } + + public void trimToSize() + { + if (size != data.length) + { + E[] newData = (E[]) new Object[size]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + for (int i = 0; i < size; i++) + { + result.append(data[i]); + if (i < size - 1) + result.append(","); + } + return result.toString() + "]"; + } + + private class ArrayListIterator implements Iterator + { + private int current = 0; + + @Override + public boolean hasNext() + { + return (current < size); + } + + @Override + public E next() + { + return data[current++]; + } + + @Override + public void remove() + { + MyArrayList.this.remove(current); + } + } + +} diff --git a/group03/172487938/MyLinkedList.java b/group03/172487938/MyLinkedList.java new file mode 100644 index 0000000000..609ab8c81d --- /dev/null +++ b/group03/172487938/MyLinkedList.java @@ -0,0 +1,274 @@ +package 基本数据结构; + +public class MyLinkedList implements MyList +{ + private Node head, tail; + private int size; + + public MyLinkedList() {} + + public MyLinkedList(E[] objects) + { + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); +// size++; + } + } + + public E getFirst() + { + if (size == 0) + return null; + else + return head.element; + } + + public E getLast() + { + if (size == 0) + return null; + else + return tail.element; + } + + public void addFirst(E e) + { + Node newNode = new Node<>(e); + newNode.next = head; + head = newNode; + size++; + + if (tail == null) + tail = head; + } + + public void addLast(E e) + { + Node newNode = new Node<>(e); + + if (tail == null) + head = tail = newNode; + else + { + tail.next = newNode; + tail = tail.next; + } + size++; + } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + if (index == 0) + { + addFirst(e); + } else if (index >= size) + { + addLast(e); + } else + { + Node current = head; + for (int i = 1; i < index; i++) + { + current = current.next; + } + Node temp = current.next; + current.next = new Node(e); + (current.next).next = temp; + size++; + } + } + + @Override + public E get(int index) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + return current.element; + } + + @Override + public E remove(int index) + { + if (index < 0 || index >= size) + return null; + else if (index == 0) + { + E e = removeFirst(); + return e; + } else if (index == size - 1) + return removeLast(); + else + { + Node previous = head; + + for (int i = 1; i < index; i++) + { + previous = previous.next; + } + + Node current = previous.next; + previous.next = current.next; + size--; + return current.element; + } + } + + public E removeFirst() + { + if (size == 0) + return null; + else + { + Node temp = head; + head = head.next; + size--; + if (head == null) + tail = null; + return temp.element; + } + } + + public E removeLast() + { + if (size == 0) + return null; + else if (size == 1) + { + Node temp = head; + head = tail = null; + size = 0; + return temp.element; + } else + { + Node current = head; + + for (int i = 0; i < size - 2; i++) + current = current.next; + + Node temp = tail; + tail = current; + tail.next = null; + size--; + return temp.element; + } + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + size = 0; + head = tail = null; + } + + @Override + public int indexOf(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return i; + current = current.next; + } + return -1; + } + + @Override + public boolean isEmpty() + { + if (size != 0) + return false; + else + return true; + } + + @Override + public E set(int index, E e) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + E temp = current.element; + current.element = e; + + return temp; + } + + @Override + public boolean contains(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return true; + current = current.next; + } + return false; + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + if (size == 0) + return null; + else + { + Node current = head; + for (int i = 0; i < size; i++) + { + try + { + result.append(current.element); + } catch (Exception e) + { + e.printStackTrace(); + } + current = current.next; + if (current != null) + { + result.append(", "); + } else + { + result.append("]"); + } + } + return result.toString(); + } + } + + public static class Node + { + E element; + Node next; + + public Node(E e) + { + this.element = e; + } + } +} \ No newline at end of file diff --git a/group03/172487938/MyList.java b/group03/172487938/MyList.java new file mode 100644 index 0000000000..5d16147855 --- /dev/null +++ b/group03/172487938/MyList.java @@ -0,0 +1,27 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public interface MyList +{ + + public void add(int index, E e); + + public E get(int index); + + public E remove(int index); + + public int size(); + + public void clear(); + + + public int indexOf(E e); + + public boolean isEmpty(); + + public E set(int index, E e); + + public boolean contains(E e); +} diff --git a/group03/172487938/Queue.java b/group03/172487938/Queue.java new file mode 100644 index 0000000000..af335f5274 --- /dev/null +++ b/group03/172487938/Queue.java @@ -0,0 +1,32 @@ +package 基本数据结构; + +import java.util.LinkedList; + +public class Queue +{ + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.addLast(o); + } + + public Object deQueue() + { + return list.removeFirst(); + } + + public boolean isEmpty() + { + if(list.size() != 0) + return true; + else + return false; + } + + public int size() + { + return list.size(); + } +} diff --git a/group03/172487938/Stack.java b/group03/172487938/Stack.java new file mode 100644 index 0000000000..f1b67822b1 --- /dev/null +++ b/group03/172487938/Stack.java @@ -0,0 +1,37 @@ +package 基本数据结构; + +import java.util.ArrayList; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(o); + } + + public Object pop() + { + Object popElement = elementData.remove(elementData.size() - 1); + return popElement; + } + + public Object peek() + { + Object peekElement = elementData.get(size() - 1); + return peekElement; + } + + public boolean isEmpty() + { + if(elementData.size() != 0) + return false; + return true; + } + + public int size() + { + return elementData.size(); + } +} diff --git a/group03/172487938/TestMyArrayList.java b/group03/172487938/TestMyArrayList.java new file mode 100644 index 0000000000..dbf8b610e1 --- /dev/null +++ b/group03/172487938/TestMyArrayList.java @@ -0,0 +1,36 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public class TestMyArrayList +{ + public static void main(String[] args) + { + MyArrayList list = new MyArrayList<>(); + list.add("America"); + System.out.println(list); + + list.add("Canada"); + System.out.println(list); + + list.add("Tokoy"); + System.out.println(list); + + list.add("Shanghai"); + + + int size = list.size(); + System.out.println(size); + list.add(0,"China"); + System.out.println(list); + String[] str = {"Japan","England","France","HonKong","BeiJing"}; + for(String s:str) + list.add(s); + System.out.println(list); + + list.remove(3); + System.out.println(list); + + } +} diff --git a/group03/172487938/TestMyLinkedList.java b/group03/172487938/TestMyLinkedList.java new file mode 100644 index 0000000000..cd412ef646 --- /dev/null +++ b/group03/172487938/TestMyLinkedList.java @@ -0,0 +1,19 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/25. + */ +public class TestMyLinkedList +{ + public static void main(String[] args) + { + String[] name = {"Tom", "George", "Peter", "Jean", "George", "Jane"}; + MyList list = new MyLinkedList(name); + + System.out.println(list.contains("George")); + System.out.println(list.get(3)); + System.out.println(list.indexOf("George")); + list.set(4, "Michael"); + System.out.println(list); + } +} From 1b53610bf0205d183870d908450a7452e8782a01 Mon Sep 17 00:00:00 2001 From: ByrsH Date: Sat, 25 Feb 2017 22:29:29 +0800 Subject: [PATCH 128/432] commit task --- group07/752262774/.gitignore | 23 ++ .../2.26/coding/src/main/java/ArrayList.java | 114 ++++++++++ .../coding/src/main/java/BinaryTreeNode.java | 49 ++++ .../2.26/coding/src/main/java/Iterator.java | 12 + .../2.26/coding/src/main/java/LinkedList.java | 210 ++++++++++++++++++ .../2.26/coding/src/main/java/List.java | 18 ++ .../2.26/coding/src/main/java/Queue.java | 27 +++ .../2.26/coding/src/main/java/Stack.java | 39 ++++ .../2.26/coding/src/main/test/test.java | 60 +++++ .../git\345\221\275\344\273\244.txt" | 11 - 10 files changed, 552 insertions(+), 11 deletions(-) create mode 100644 group07/752262774/.gitignore create mode 100644 group07/752262774/2.26/coding/src/main/java/ArrayList.java create mode 100644 group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java create mode 100644 group07/752262774/2.26/coding/src/main/java/Iterator.java create mode 100644 group07/752262774/2.26/coding/src/main/java/LinkedList.java create mode 100644 group07/752262774/2.26/coding/src/main/java/List.java create mode 100644 group07/752262774/2.26/coding/src/main/java/Queue.java create mode 100644 group07/752262774/2.26/coding/src/main/java/Stack.java create mode 100644 group07/752262774/2.26/coding/src/main/test/test.java delete mode 100644 "group07/752262774/git\345\221\275\344\273\244.txt" diff --git a/group07/752262774/.gitignore b/group07/752262774/.gitignore new file mode 100644 index 0000000000..8d9372e204 --- /dev/null +++ b/group07/752262774/.gitignore @@ -0,0 +1,23 @@ +# Compiled class file +*.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* +/bin/ diff --git a/group07/752262774/2.26/coding/src/main/java/ArrayList.java b/group07/752262774/2.26/coding/src/main/java/ArrayList.java new file mode 100644 index 0000000000..a7b0f43aed --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/ArrayList.java @@ -0,0 +1,114 @@ +package main.java; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/21. + */ +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[10]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = new Object[0]; + } else { + throw new IllegalArgumentException("Illegal Capacity: "+ + initialCapacity); + } + } + + public void add(Object o) { + judegGrow(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == size) { + add(o); + }else { + judegGrow(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object o = elementData[index]; + + int move = size - 1 -index; + if(move > 0) { + System.arraycopy(elementData, index+1, elementData, index, move); + } + elementData[--size] = null; + + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrrayListIterator(this); + } + + private class ArrrayListIterator implements Iterator { + + ArrayList arrayList; + + int pos; + + private ArrrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + return pos != arrayList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return elementData[i]; + }else { + throw new NoSuchElementException(); + } + } + } + + private void judegGrow() { + if(size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + 1); + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java b/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java new file mode 100644 index 0000000000..cf8ade9193 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode(BinaryTreeNode left, Object o, BinaryTreeNode right) { + setData(o); + setLeft(left); + setRight(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/group07/752262774/2.26/coding/src/main/java/Iterator.java b/group07/752262774/2.26/coding/src/main/java/Iterator.java new file mode 100644 index 0000000000..74a0b35573 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Iterator.java @@ -0,0 +1,12 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public interface Iterator { + + public boolean hasNext(); + + public Object next(); +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/LinkedList.java b/group07/752262774/2.26/coding/src/main/java/LinkedList.java new file mode 100644 index 0000000000..4f37c1a31c --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/LinkedList.java @@ -0,0 +1,210 @@ +package main.java; + + +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/23. + */ +public class LinkedList implements List{ + + private int size; + + private Node first; + + private Node last; + + public LinkedList() { + this.first = null; + this.last =null; + } + + public void add(Object o) { + Node l = this.last; + Node newNode = new Node(l, o, null); + this.last = newNode; + if(null == l) { + this.first = newNode; + }else { + l.next = newNode; + } + this.size++; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == this.size) { + this.add(o); + }else { + Node target = targetNode(index); + Node before = target.prev; + Node newNode = new Node(before, o, target); + target.prev = newNode; + if(null == before) { + this.first = newNode; + }else { + before.next = newNode; + } + this.size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return targetNode(index).data; + } + + public Object remove(int index) { + rangeCheck(index); + Node target = targetNode(index); + Node before = target.prev; + Node after = target.next; + Object o = target.data; + + if(null == before) { + this.first = null; + }else { + before.next = after; + target.prev = null; + } + + if(null == after) { + this.last = before; + }else { + after.prev = before; + target.next = null; + } + target.data = null; + this.size--; + + return o; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = first; + Node newNode = new Node(null, o, node); + this.first = newNode; + if(null == node) { + this.last = newNode; + }else { + node.prev = newNode; + } + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = first; + if (node == null) + throw new NoSuchElementException(); + + first = node.next; + Object o = node.data; + if(null == node.next) { + this.last = null; + }else { + first.prev = null; + } + node.data = null; + node.next = null; + this.size--; + return o; + } + + public Object removeLast() { + Node node = last; + if (node == null) + throw new NoSuchElementException(); + + last = node.prev; + Object o = node.data; + if(null == node.prev) { + this.first = null; + }else { + last.next = null; + } + node.data = null; + node.prev = null; + this.size--; + return o; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + + LinkedList linkedList; + + int pos; + + private LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return pos != linkedList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return linkedList.get(i); + }else { + throw new NoSuchElementException(); + } + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } + + private Node targetNode(int index) { + //由index值在链表的前半部分还是后半部分,决定是从前向后,还是从后向前查找。 + Node target = new Node(); + if(index < (this.size >> 1)) { + target = this.first; + for(int i=0; iindex; i--) { + target = target.prev; + } + } + return target; + } + + private static class Node{ + Object data; + Node next; + Node prev; + + Node() { + } + + Node(Node prev, Object o, Node next) { + this.data = o; + this.next = next; + this.prev = prev; + } + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/List.java b/group07/752262774/2.26/coding/src/main/java/List.java new file mode 100644 index 0000000000..0f1d979332 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/List.java @@ -0,0 +1,18 @@ +package main.java; + +/** + * Created by yrs on 2017/2/23. + */ +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/group07/752262774/2.26/coding/src/main/java/Queue.java b/group07/752262774/2.26/coding/src/main/java/Queue.java new file mode 100644 index 0000000000..59a120f51c --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Queue.java @@ -0,0 +1,27 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +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; + } + + public int size() { + return elementData.size(); + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/Stack.java b/group07/752262774/2.26/coding/src/main/java/Stack.java new file mode 100644 index 0000000000..efaa2498b9 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Stack.java @@ -0,0 +1,39 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object o = elementData.remove(elementData.size()-1); + return o; + } + + public Object peek() { + Object o = elementData.get(elementData.size() - 1); + return o; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public static void main(String [] args) { + Stack stack = new Stack(); + stack.push(1); + System.out.println(stack.size() + " " + stack.peek() + " " + stack.pop() + " " + stack.isEmpty()); + + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/test/test.java b/group07/752262774/2.26/coding/src/main/test/test.java new file mode 100644 index 0000000000..b32a0b6406 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/test/test.java @@ -0,0 +1,60 @@ +package main.test; + + +import javax.swing.tree.TreeNode; +import java.util.*; + +/** + * Created by yrs on 2017/2/21. + */ +public class test { + public static void main(String [] args) { + ArrayList list = new ArrayList(4); + list.add(9); + System.out.println(list); + list.add(1,3); +// list.add(2,3); //error IndexOutOfBoundsException + list.remove(1); + System.out.println(list.size()); + + Object[] target = new Object[0]; + System.out.println(target); + Object[] EMPTY_ELEMENTDATA = {}; + System.out.println(EMPTY_ELEMENTDATA); + + + //LinkedList + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + System.out.println(linkedList.get(0)); + linkedList.add(1,3); + System.out.println(linkedList.size()); + System.out.println(3 >> 1); + + for(int i=0; i<1; i++) { + System.out.println("dd"); + } + + Stack stack = new Stack(); + + Queue queue; + TreeNode treeNode; + + List lstint = new ArrayList(); + lstint.add(1); + lstint.add(2); + lstint.add(3); + + // Iterator遍历一 + Iterator iterator = lstint.iterator(); + iterator.hasNext(); + while (iterator.hasNext()) + { + int i = (Integer) iterator.next(); + System.out.println(i); + } + + + } +} + \ No newline at end of file diff --git "a/group07/752262774/git\345\221\275\344\273\244.txt" "b/group07/752262774/git\345\221\275\344\273\244.txt" deleted file mode 100644 index 8bcf2ffa0f..0000000000 --- "a/group07/752262774/git\345\221\275\344\273\244.txt" +++ /dev/null @@ -1,11 +0,0 @@ -��װgit gui��������git bash���� -1.��¡ -git clone git@github.com:leijing1992/coding2017.git -2.�����޸ĵ��ļ� -git add -A -3.�ύ���ݴ��� -git commit -m "�ύ***����" -4.����master -git pull origin master -5.�ύ��master -git push origin master \ No newline at end of file From 5f7fdc539ad010e35ccc3224b868518b90282669 Mon Sep 17 00:00:00 2001 From: "--ztc.dev" <--2418936572@qq.com> Date: Sat, 25 Feb 2017 22:40:44 +0800 Subject: [PATCH 129/432] init --- group03/569045298/JavaLevelUp | 1 + 1 file changed, 1 insertion(+) create mode 160000 group03/569045298/JavaLevelUp diff --git a/group03/569045298/JavaLevelUp b/group03/569045298/JavaLevelUp new file mode 160000 index 0000000000..2633ba619f --- /dev/null +++ b/group03/569045298/JavaLevelUp @@ -0,0 +1 @@ +Subproject commit 2633ba619f19e19c260eee33b8557fbf22acd9a3 From eddddc3d0bd4af6e8da5df110a73208ce24345cb Mon Sep 17 00:00:00 2001 From: lqt0223 Date: Sat, 25 Feb 2017 22:45:24 +0800 Subject: [PATCH 130/432] Collections in Java by lqt0223 564673292 --- .../564673292/com/coding/basic/ArrayList.java | 92 +++++++++++++ .../com/coding/basic/BinaryTreeNode.java | 72 ++++++++++ .../564673292/com/coding/basic/Iterable.java | 5 + .../564673292/com/coding/basic/Iterator.java | 7 + .../com/coding/basic/LinkedList.java | 123 ++++++++++++++++++ group18/564673292/com/coding/basic/List.java | 9 ++ group18/564673292/com/coding/basic/Queue.java | 30 +++++ group18/564673292/com/coding/basic/Stack.java | 50 +++++++ 8 files changed, 388 insertions(+) create mode 100644 group18/564673292/com/coding/basic/ArrayList.java create mode 100644 group18/564673292/com/coding/basic/BinaryTreeNode.java create mode 100644 group18/564673292/com/coding/basic/Iterable.java create mode 100644 group18/564673292/com/coding/basic/Iterator.java create mode 100644 group18/564673292/com/coding/basic/LinkedList.java create mode 100644 group18/564673292/com/coding/basic/List.java create mode 100644 group18/564673292/com/coding/basic/Queue.java create mode 100644 group18/564673292/com/coding/basic/Stack.java diff --git a/group18/564673292/com/coding/basic/ArrayList.java b/group18/564673292/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4bb16148b7 --- /dev/null +++ b/group18/564673292/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterable{ + private E[] array; + private int lastIndex; + private int length; + //Constructor + @SuppressWarnings("unchecked") + public ArrayList(){ + length = 10; + array = (E[])new Object[length]; + lastIndex = 0; + } + + public void add(E object){ + if(lastIndex == length){ + this.grow(); + } + array[lastIndex] = object; + lastIndex++; + } + + @SuppressWarnings("unchecked") + private void grow(){ + E[] tempArray = (E[])new Object[length + 10]; + System.arraycopy(array, 0, tempArray, 0, length); + array = tempArray; + length = length + 10; + } + + public void insert(int index, E o) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + for (int i = lastIndex; i > index; i--) { + array[i] = array[i - 1]; + } + array[index] = o; + length++; + } + + public E get(int index) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + return array[index]; + } + + public E remove(int index){ + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + E removed = array[index]; + for (int i = index; i < lastIndex - 1; i++) { + array[i] = array[i + 1]; + } + lastIndex--; + array[lastIndex] = null; + return removed; + } + + public int size(){ + return lastIndex; + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private ArrayList arrayList; + // constructor + public Itr(ArrayList arrayList){ + this.arrayList = arrayList; + itrCurIndex = -1; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > lastIndex - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.arrayList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.arrayList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/BinaryTreeNode.java b/group18/564673292/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..713e8e4409 --- /dev/null +++ b/group18/564673292/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +// This is a node in a customized binaryTree. The tree have 2 extra features comparing to general binary trees. +// 1. The data of each node are in number class. +// 2. The left child node has a smaller number data than root node, and the right child node has a larger number data that root node. + +package com.coding.basic; + +public class BinaryTreeNode { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + // constructor + public BinaryTreeNode(E data){ + this.data = data; + } + + public E getData() { + return this.data; + } + + public void setData(E data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return this.left; + } + + public boolean setLeft(BinaryTreeNode left) { + if(this.compareWithRoot(left.data) >= 0 || this.left != null){ + System.err.println("The left node data should be smaller than root node."); + return false; + }else{ + this.left = left; + return true; + } + } + + public BinaryTreeNode getRight() { + return this.right; + } + + public boolean setRight(BinaryTreeNode right) { + if(this.compareWithRoot(right.data) <= 0 || this.right != null) { + System.err.println("The right node data should be larger than root node."); + + return false; + }else{ + this.right = right; + return true; + } + } + + private int compareWithRoot(E o){ + return (Integer)o - (Integer)this.getData(); + } + + @SuppressWarnings("unchecked") + public void insert(E o){ + BinaryTreeNode newNode = new BinaryTreeNode(o); + if(!this.setLeft(newNode)){ + if(!this.setRight(newNode)){ + if(this.left.getData() == o){ + this.right.insert(o); + }else{ + this.left.insert(o); + } + } + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterable.java b/group18/564673292/com/coding/basic/Iterable.java new file mode 100644 index 0000000000..e80308012a --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterable.java @@ -0,0 +1,5 @@ +package com.coding.basic; + +public interface Iterable{ + public Iterator iterator(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterator.java b/group18/564673292/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..27d475265e --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public E remove(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/LinkedList.java b/group18/564673292/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..19b12d12da --- /dev/null +++ b/group18/564673292/com/coding/basic/LinkedList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterable { + + private Node head; + private Node last; + private int length; + + private class Node{ + public E data; + public Node next; + + // constructor + private Node(E o, Node n){ + data = o; + next = n; + } + } + + // constructor + public LinkedList(){ + head = new Node(null, null); + last = head; + length = 0; + } + + public void add(E o){ + Node newNode = new Node(o, null); + last.next = newNode; + last = newNode; + length++; + } + + public void insert(int index , E o){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index); + Node nodeToInsert = new Node(o,nextNode); + prevNode.next = nodeToInsert; + length++; + } + + private Node getNode(int index){ + int count = 0; + Node currentNode = head; + while(currentNode.next != null && count <= index){ + currentNode = currentNode.next; + count++; + } + return currentNode; + } + + public E get(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeAtIndex = this.getNode(index); + return nodeAtIndex.data; + } + + public E remove(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeToRemove = this.getNode(index); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index + 1); + prevNode.next = nextNode; + E removedData = nodeToRemove.data; + nodeToRemove = null; + length--; + return removedData; + } + + public int size(){ + return length; + } + + public void addFirst(E o){ + this.insert(0, o); + } + public void addLast(E o){ + this.add(o); + + } + public E removeFirst(){ + return this.remove(0); + } + public E removeLast(){ + return this.remove(length - 1); + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private Node currentNode; + private LinkedList linkedList; + + public Itr(LinkedList linkedList){ + itrCurIndex = -1; + currentNode = head; + this.linkedList = linkedList; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > length - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.linkedList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.linkedList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/List.java b/group18/564673292/com/coding/basic/List.java new file mode 100644 index 0000000000..04a7ac992e --- /dev/null +++ b/group18/564673292/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void insert(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Queue.java b/group18/564673292/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b40f06afc8 --- /dev/null +++ b/group18/564673292/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedList; + + // constructor + public Queue(){ + linkedList = new LinkedList(); + } + + public void enQueue(E o){ + linkedList.addLast(o); + } + + public E deQueue(){ + return linkedList.removeFirst(); + } + + public E peek(){ + return linkedList.get(0); + } + + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + public int size(){ + return linkedList.size(); + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Stack.java b/group18/564673292/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b1b129fc40 --- /dev/null +++ b/group18/564673292/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack{ + private ArrayList arrayList; + + // constructor + public Stack(){ + arrayList = new ArrayList(); + } + + public void push(E o){ + arrayList.add(o); + } + + public E pop(){ + return arrayList.remove(arrayList.size() - 1); + } + + public E peek(){ + return arrayList.get(arrayList.size() - 1); + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true: false; + } + + public int size(){ + return arrayList.size(); + } + + // public Iterator iterator(){ + // return new Itr(); + // } + + // private class Itr implements Iterator{ + // Iterator arrayListItr = arrayList.iterator(); + // public boolean hasNext(){ + // return arrayListItr.hasNext(); + // } + + // public E next(){ + // return arrayListItr.next(); + // } + + // @Override // Stack iterator can only remove the last element + // public E remove(){ + // return arrayList.pop(); + // } + // } +} \ No newline at end of file From a334fa8d1402835ae518845daeaabc1d02a637e9 Mon Sep 17 00:00:00 2001 From: yinwenbing <312816708@qq.com> Date: Sat, 25 Feb 2017 22:55:04 +0800 Subject: [PATCH 131/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\347\232\204\345\205\263\347\263\273.md" | 29 ++++ group04/312816708/coding/coding01/.gitignore | 7 + group04/312816708/coding/coding01/pom.xml | 12 ++ .../com/kevin/coding01/basic/MyArrayList.java | 117 +++++++++++++++ .../kevin/coding01/basic/MyLinkedList.java | 135 ++++++++++++++++++ .../java/com/kevin/coding01/basic/MyList.java | 17 +++ .../com/kevin/coding01/basic/MyQueue.java | 35 +++++ .../com/kevin/coding01/basic/MyStack.java | 67 +++++++++ 8 files changed, 419 insertions(+) create mode 100644 "group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" create mode 100644 group04/312816708/coding/coding01/.gitignore create mode 100644 group04/312816708/coding/coding01/pom.xml create mode 100644 group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java create mode 100644 group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java create mode 100644 group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java create mode 100644 group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java create mode 100644 group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java diff --git "a/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..68542a9e7b --- /dev/null +++ "b/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,29 @@ +# CPU、内存、硬盘、指令之间的关系 + +## 1、简介 + +### 1.1 CPU +> CPU,中央处理器,是一个超大规模的集成电路,是一台计算机的运算核心和控制核心。它的主要功能是解释计算机指令以及处理计算机软件中的数据。 + +### 1.2 内存 +> 内存是计算机中重要的部件之一,它是与CPU沟通的桥梁。计算机中所有的程序都是在内存中运行的。其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换数据。 + +### 1.3 硬盘 +> 硬盘是计算机主要的存储媒介之一。硬盘分为固态硬盘(SSD 盘,新式硬盘)、机械硬盘(HDD 硬盘)、混合硬盘(HHD 一块基于传统机械硬盘诞生出来的新硬盘)。 + +### 1.4 指令 +> 指挥计算机工作的指示命令。程序是一系列按一定顺序排列的,执行程序的过程就是计算机的工作过程。 + +## 2、关系 +计算机在运行时,**CPU** 从**内存** 中获取第一条 **指令** ,然后内存再从硬盘中读取所需的数据。然后CPU再取出第二条指令,一直到指令结束。 + +## 3、参考资料 +[中央处理器 —— 百度百科](http://baike.baidu.com/item/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8/284033?sefr=cr&fromtitle=CPU&fromid=120556) + +[内存 —— 百度百科](http://baike.baidu.com/item/%E5%86%85%E5%AD%98?sefr=enterbtn) + +[硬盘 —— 百度百科](http://baike.baidu.com/item/%E7%A1%AC%E7%9B%98?sefr=enterbtn) + +[计算机指令 —— 百度百科](http://baike.baidu.com/item/%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%8C%87%E4%BB%A4) + + diff --git a/group04/312816708/coding/coding01/.gitignore b/group04/312816708/coding/coding01/.gitignore new file mode 100644 index 0000000000..22cb2e1fb7 --- /dev/null +++ b/group04/312816708/coding/coding01/.gitignore @@ -0,0 +1,7 @@ +# IntelliJ IDEA + +target/ +*.iml +*.ipr +*.iws +.idea/ \ No newline at end of file diff --git a/group04/312816708/coding/coding01/pom.xml b/group04/312816708/coding/coding01/pom.xml new file mode 100644 index 0000000000..9f29c935ae --- /dev/null +++ b/group04/312816708/coding/coding01/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.kevin + coding01 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java new file mode 100644 index 0000000000..5e4c1bdd2e --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java @@ -0,0 +1,117 @@ +package com.kevin.coding01.basic; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public class MyArrayList implements MyList { + private int size = 0; + private Object[] elementData = new Object[10]; + + /** + * 添加 + * 判断数组空间是否足够,不够则扩容,将元素放在数组末尾 + * + * @param e + */ + public void add(E e) { + isGrow(size + 1);//判断是否需要扩容 + elementData[size++] = e; + } + + /** + * 是否需要扩容 + * + * @param size + */ + private void isGrow(int size) { + if (size > elementData.length) { + grow(size); + } + } + + /** + * 扩容 + * + * @param minCapacity + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//>>将oldCapacity向右移一位,右移一位代表除2,右移n位代表除以2的n次方。左移则是乘 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } else if (newCapacity - (Integer.MAX_VALUE - 8) > 0) { + newCapacity = hugeCapacity(minCapacity); + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + private int hugeCapacity(int minCapacity) { + if (minCapacity < 0) { + throw new OutOfMemoryError(); + } + return (minCapacity > (Integer.MAX_VALUE)) ? Integer.MAX_VALUE : Integer.MAX_VALUE - 8; + } + + /** + * 添加指定索引的元素 + * 判断索引是否小于0或大于size + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + isGrow(index); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = e; + size++; + } + + /** + * 根据索引获取数组中的元素 + * + * @param index + * @return + */ + public E get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + return (E) elementData[index]; + } + + /** + * 根据索引移除数组中的元素,如果移除中间的元素,后面的元素要往前移 + * + * @param index + * @return + */ + public E remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + E oldValue = (E) elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null; + size--; + } + return oldValue; + } + + /** + * 获取数组中存放值得数量 + * + * @return + */ + public int size() { + return size; + } +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java new file mode 100644 index 0000000000..4aa55cb9b9 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java @@ -0,0 +1,135 @@ +package com.kevin.coding01.basic; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public class MyLinkedList implements MyList { + private Node first;//头节点 + private int size = 0;//默认大小为0 + + public void add(E e) { + if (size == 0) { + first = new Node(); + first.element = e; + size++; + } else { + Node head = first; + for (int i = 0; i < size - 1; i++) { + head = head.next; + } + + Node add = new Node(); + add.element = e; + head.next = add; + size++; + } + } + + + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + Node prev = getNode(index - 1);//当前索引指向的节点的上一节点 + Node next = getNode(index);//当前索引指向的节点成为添加节点的next + Node add = new Node(); + add.element = e; + prev.next = add; + add.next = next; + size++; + } + + private Node getNode(int index) { + Node node = first; + + for (int i = 0; i < index; i++) { + node = first.next; + } + + return node; + } + + public E get(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + + if (size == 0) { + return null; + } + + return getNode(index).element; + } + + public E remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + + Node prev = getNode(index - 1); + Node next = getNode(index + 1); + + prev.next = next; + return getNode(index).element; + } + + public int size() { + return size; + } + + public void addFirst(E e) { + if (size == 0) { + first = new Node(); + first.element = e; + size++; + } else { + Node add = new Node(); + add.element = e; + add.next = first; + first = add; + size++; + } + } + + public void addLast(E e) { + Node oldLast = getNode(size - 1); + Node add = new Node(); + add.element = e; + oldLast.next = add; + size++; + } + + public E removeFirst() { + Node oldFirst = first; + if (first.next != null) { + first = first.next; + size--; + return (E) oldFirst.element; + } else {//只有一个节点或者一个节点也没有 + first = null; + return null; + } + } + + public E removeLast() { + Node last = getNode(size - 1); + if (last != null) { + E element = (E) last.element; + Node newLast = getNode(size - 2); + newLast.next = null; + size--; + return element; + } else { //一个节点都不存在 + return null; + } + } + + /** + * 静态内部类 + */ + private static class Node { + E element;//节点数据 + Node next;//下一节点 + } + +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java new file mode 100644 index 0000000000..15758bd837 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java @@ -0,0 +1,17 @@ +package com.kevin.coding01.basic; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public interface MyList { + + void add(E e); + + void add(int index, E e); + + E get(int index); + + E remove(int index); + + int size(); +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java new file mode 100644 index 0000000000..feff484d76 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java @@ -0,0 +1,35 @@ +package com.kevin.coding01.basic; + +/** + * 队列:先进先出 + * Created by YinWenBing on 2017/2/25. + */ +public class MyQueue { + private MyLinkedList elementDate = new MyLinkedList(); + + //入队列 + public void enQueue(E e) { + elementDate.addLast(e); + } + + //出队列 + public E deQueue() { + return elementDate.removeFirst(); + } + + public boolean isEmpty() { + return elementDate.size() == 0 ? true : false; + } + + public int size() { + return elementDate.size(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue(1); + queue.enQueue(2); + + System.out.println(queue.deQueue());//1 + } +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java new file mode 100644 index 0000000000..10d115d052 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java @@ -0,0 +1,67 @@ +package com.kevin.coding01.basic; + +/** + * 栈 先进后出 + * Created by YinWenBing on 2017/2/25. + */ +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + /** + * 往栈中添加元素 + * + * @param e + */ + public void push(E e) { + elementData.add(e); + } + + /** + * 删除栈顶元素 + * + * @return + */ + public E pop() { + return elementData.remove(elementData.size() - 1); + } + + /** + * 返回栈顶元素 + * + * @return + */ + public E peek() { + return elementData.get(elementData.size() - 1); + } + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + /** + * 返回栈中元素的数量 + * + * @return + */ + public int size() { + return elementData.size(); + } + + + public static void main(String[] args) { + MyStack myStack = new MyStack(); + myStack.push("A"); + myStack.push("B"); + myStack.push("C"); + + System.out.println(myStack.peek()); + System.out.println(myStack.pop()); + System.out.println(myStack.peek()); + System.out.println(myStack.size()); + } +} From 3c2800ed73bc7c8fcc2c259c5b28e7f4372757c2 Mon Sep 17 00:00:00 2001 From: --ztc-dev <--2418936572@qq.com> Date: Sat, 25 Feb 2017 22:58:15 +0800 Subject: [PATCH 132/432] delete --- group03/569045298/JavaLevelUp | 1 - 1 file changed, 1 deletion(-) delete mode 160000 group03/569045298/JavaLevelUp diff --git a/group03/569045298/JavaLevelUp b/group03/569045298/JavaLevelUp deleted file mode 160000 index 2633ba619f..0000000000 --- a/group03/569045298/JavaLevelUp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2633ba619f19e19c260eee33b8557fbf22acd9a3 From 81c40c9bd4638581ef7fe2f37d323a134739b516 Mon Sep 17 00:00:00 2001 From: --ztc-dev <--2418936572@qq.com> Date: Sat, 25 Feb 2017 23:03:00 +0800 Subject: [PATCH 133/432] datastructure --- group03/569045298/pom.xml | 35 ++++ .../coding/basic/datastructure/ArrayList.java | 108 +++++++++++ .../basic/datastructure/BinaryTreeNode.java | 41 +++++ .../coding/basic/datastructure/Iterator.java | 13 ++ .../basic/datastructure/LinkedList.java | 167 ++++++++++++++++++ .../com/coding/basic/datastructure/List.java | 17 ++ .../com/coding/basic/datastructure/Queue.java | 43 +++++ .../com/coding/basic/datastructure/Stack.java | 47 +++++ .../datastructure/TestDataStructure.java | 66 +++++++ 9 files changed, 537 insertions(+) create mode 100644 group03/569045298/pom.xml create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/List.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/Queue.java create mode 100644 group03/569045298/src/main/com/coding/basic/datastructure/Stack.java create mode 100644 group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java diff --git a/group03/569045298/pom.xml b/group03/569045298/pom.xml new file mode 100644 index 0000000000..338f3870aa --- /dev/null +++ b/group03/569045298/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.ztc + JavaLevelUp + war + 1.0-SNAPSHOT + JavaLevelUp Maven Webapp + http://maven.apache.org + + + + junit + junit + 3.8.1 + test + + + org.junit.jupiter + junit-jupiter-api + RELEASE + + + junit + junit + 4.12 + + + + + JavaLevelUp + + + diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java new file mode 100644 index 0000000000..dee00342d2 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 10; + private int size = 0; + private Object[] elementData = null; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) { + throw new RuntimeException("initialCapacity is smaller than zero"); + } + elementData = new Object[initialCapacity]; + } + + @Override + public void add(Object o) { + checkCapacity(size + 1); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + checkCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObject = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, elementData.length - index - 1); + elementData[--size] = null; + return removedObject; + } + + @Override + public int size() { + return size; + } + + private void checkRange(int index) { + if (index < 0 || index > elementData.length) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkCapacity(int size) { + if (size > elementData.length) { + int newLength = elementData.length * 2; + Object[] newObject = new Object[newLength]; + System.arraycopy(elementData, 0, newObject, 0, elementData.length); + elementData = newObject; + } + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + ArrayList arrayList = null; + int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + // TODO + pos++; + if (pos > size) { + return false; + } + return true; + } + + @Override + public Object next() { + // TODO + return elementData[pos]; + } + + @Override + public Object remove() { + // TODO + return null; + } + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java new file mode 100644 index 0000000000..b69f8932ed --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +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 object) { + return null; + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java new file mode 100644 index 0000000000..8bf2ae7ed5 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public interface Iterator { + + boolean hasNext(); + + Object next(); + + Object remove(); +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java new file mode 100644 index 0000000000..8814ea82e8 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java @@ -0,0 +1,167 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size = 0; + + public LinkedList() { + + } + + @Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + tail = head; + size++; + } else { + // 尾插法 + Node newNode = new Node(object); + tail.next = newNode; + tail = newNode; + tail.next = null; + size++; + } + } + + @Override + public void add(int index, Object object) { + checkRange(index); + if (null == head) { + add(object); + return; + } + if (index == 0) { + addFirst(object); + return; + } + Node pre = node(index - 1); + Node newNode = new Node(object); + newNode.next = pre.next; + pre.next = newNode; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + checkNodeNotNull(); + Node node = node(index); + return node.data; + } + + @Override + public Object remove(int index) { + checkRange(index); + checkNodeNotNull(); + if (index == 0) { + removeFirst(); + return head; + } + Node pre = node(index - 1); + pre.next = pre.next.next; + size--; + return head; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + size++; + } else { + Node firstNode = new Node(object); + firstNode.next = head; + head = firstNode; + size++; + } + } + + public Object removeFirst() { + checkNodeNotNull(); + head = head.next; + size--; + return head; + } + + public Object removeLast() { + checkNodeNotNull(); + if (size == 1) { + head = null; + } + Node pre = node(size() - 2); + pre.next = null; + size--; + return head; + } + + private void checkRange(int index) { + if (index > size - 1 || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkNodeNotNull() { + if (null == head) { + throw new NullPointerException(); + } + } + + private Node node(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node { + Node next; + private Object data; + + public Node() { + + } + + public Node(Object data) { + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Node(Object data, Node next, Node prev) { + this.data = data; + this.next = next; + } + } + + /*@Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + } else { + // 头插法 + Node nextNode = new Node(object); + nextNode.next = head.next; + head.next = nextNode; + } + }*/ + +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/List.java b/group03/569045298/src/main/com/coding/basic/datastructure/List.java new file mode 100644 index 0000000000..4d9292f156 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/List.java @@ -0,0 +1,17 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +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/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java new file mode 100644 index 0000000000..d3d4ebfbab --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class Queue { + + private ArrayList elementData; + + private int size; + + public Queue() { + elementData = new ArrayList(); + } + + public void enQueue(Object object) { + elementData.add(object); + size++; + } + + public Object deQueue() { + checkIsEmpty(); + Object object = elementData.get(0); + elementData.remove(0); + size--; + return object; + } + + private void checkIsEmpty() { + if (elementData.size() == 0) { + throw new RuntimeException("queue is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java new file mode 100644 index 0000000000..c8dbc6b3af --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java @@ -0,0 +1,47 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class Stack { + + private ArrayList elementData = null; + + private int size = 0; + + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object object) { + elementData.add(object); + size++; + } + + public Object pop() { + checkIsEmpty(); + Object peekObject = peek(); + elementData.remove(size - 1); + size--; + return peekObject; + } + + public Object peek() { + checkIsEmpty(); + return elementData.get(size - 1); + } + + private void checkIsEmpty() { + if (isEmpty()) { + throw new RuntimeException("stack is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java new file mode 100644 index 0000000000..9f90242595 --- /dev/null +++ b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java @@ -0,0 +1,66 @@ +package com.coding.basic.datastructure; + +import org.junit.Test; + +/** + * Created by zt on 2017/2/19. + */ +public class TestDataStructure { + + @Test + public void testLinedList() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 5; i++) { + list.add(i); + } + list.add(0, -1); + list.remove(1); + list.removeLast(); + list.addFirst(999); + list.removeFirst(); + System.out.println("list size : " + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + java.util.LinkedList list1 = new java.util.LinkedList(); + list1.add(0, 2); + System.out.print(list1.get(0)); + } + + @Test + public void testStack() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.pop(); + System.out.println(stack.size()); + Object obj = stack.peek(); + } + + @Test + public void testQueue() { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + Object object = queue.deQueue(); + System.out.println("dqueue object : " + object); + System.out.println(queue.isEmpty()); + System.out.println(queue.size()); + } + + @Test + public void testArrayList() { + List arrayList = new ArrayList(); + for (int i = 0; i < 30; i++) { + arrayList.add(i); + } + arrayList.add(0, -2); + arrayList.add(1, -1); + System.out.println(arrayList.remove(1)); + System.out.println("ArrayList size : " + arrayList.size()); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println(arrayList.get(i)); + } + } +} From 4baf9c1add0b51d5ec268a12821a5f6c61e51e10 Mon Sep 17 00:00:00 2001 From: AllenLink <3168592570@qq.com> Date: Sat, 25 Feb 2017 23:03:19 +0800 Subject: [PATCH 134/432] FirstHomework --- group10/3314793852/.classpath | 6 + group10/3314793852/.gitignore | 1 + group10/3314793852/.project | 17 +++ .../3314793852/src/myList/MyArrayList.java | 103 +++++++++++++++ .../src/myList/MyBinarySearchTree.java | 113 ++++++++++++++++ group10/3314793852/src/myList/MyIterator.java | 40 ++++++ .../3314793852/src/myList/MyLinkedList.java | 122 ++++++++++++++++++ group10/3314793852/src/myList/MyQueue.java | 60 +++++++++ group10/3314793852/src/myList/MyStack.java | 48 +++++++ group10/3314793852/src/myList/Test.java | 26 ++++ 10 files changed, 536 insertions(+) create mode 100644 group10/3314793852/.classpath create mode 100644 group10/3314793852/.gitignore create mode 100644 group10/3314793852/.project create mode 100644 group10/3314793852/src/myList/MyArrayList.java create mode 100644 group10/3314793852/src/myList/MyBinarySearchTree.java create mode 100644 group10/3314793852/src/myList/MyIterator.java create mode 100644 group10/3314793852/src/myList/MyLinkedList.java create mode 100644 group10/3314793852/src/myList/MyQueue.java create mode 100644 group10/3314793852/src/myList/MyStack.java create mode 100644 group10/3314793852/src/myList/Test.java diff --git a/group10/3314793852/.classpath b/group10/3314793852/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group10/3314793852/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/3314793852/.gitignore b/group10/3314793852/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/3314793852/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/3314793852/.project b/group10/3314793852/.project new file mode 100644 index 0000000000..e09255853a --- /dev/null +++ b/group10/3314793852/.project @@ -0,0 +1,17 @@ + + + FirstWeek + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/3314793852/src/myList/MyArrayList.java b/group10/3314793852/src/myList/MyArrayList.java new file mode 100644 index 0000000000..7b5a883190 --- /dev/null +++ b/group10/3314793852/src/myList/MyArrayList.java @@ -0,0 +1,103 @@ +package myList; + +/* + * ArrayList�ĵײ���һ�����飬ͨ�����´���������ķ�������̬��������������� + * ��ArrayList���������ղ������ݡ� + */ + +public class MyArrayList { + private int theSize; //��ǰ��С + private static final int DEFAULT_CAPACITY=10; //Ĭ������ + private Object[] theArr=new Object[10]; //�ײ����� + + //��ʼ�� + public MyArrayList(){ + clear(); + } + + //��� + public void clear(){ + theSize=0; + capacityBigger(DEFAULT_CAPACITY); + } + + //��ȡ��С + public int size(){ + return theSize; + } + + //��ȡ�ײ����� + public Object[] getArr(){ + return theArr; + + } + //���룬ֱ�Ӳ��뵽����β���� + public void add(Object a){ + add(theSize, a); + } + + //�����±��ȡ���� + public Object get(int i){ + if(i<0||i>=theSize){ + throw new ArrayIndexOutOfBoundsException(); + } + return theArr[i]; + } + + //���룬����ָ���±���롣 + public void add(int i,Object a){ + + if(theSize==theArr.length){ //��ʼ����Ϊ10��ÿ���ɹ�������һ������ʱ����size+1,��size������Ĵ�С��ͬʱ��������������󷽷�����̬��������Ĵ�С�� + capacityBigger(size()); + } + for(int j=theSize-1;j>=i;j--){ + theArr[j+1]=theArr[j]; + } + theArr[i]=a; + + theSize++; + } + + //ɾ��,�����±�ɾ�����ݡ� + public void remove(int i){ + + for(int j=i;j0){ + return contains(x,aNode.left); + } + else if(comparaResult<0){//�����ݴ��ڵ�ǰ�ڵ������ʱ���������Ӧ���ڵ�ǰ�ڵ���Һ��ӽڵ��С� + return contains(x,aNode.right); + } + else{ //�����ݵ��ڵ�ǰ�ڵ������ʱ���������Ӧ���ڵ�ǰ�ڵ��С� + return true; + } + } + + //�������ݡ� + public void insert(Object x){ + root=insert(x,root); + } + + public BinaryNode insert(Object x,BinaryNode aNode){ + + if(aNode==null){//��ǰΪ�µ����ݽڵ㣬��Ϊ��Ҷ�ӽڵ㣬�������ҽڵ�Ϊnull. + return new BinaryNode(x,null,null); + } + + //�͵�ǰ�Ľڵ���бȽϡ� + Integer comparaResult=(Integer)aNode.element-(Integer)x; + + //������С�ڵ�ǰ�ڵ������ʱ���������Ӧ���ڵ�ǰ�ڵ�����ӽڵ��С� + if(comparaResult>0){ + aNode.left= insert(x,aNode.left); + } + else if(comparaResult<0){//�����ݴ��ڵ�ǰ�ڵ������ʱ���������Ӧ���ڵ�ǰ�ڵ���Һ��ӽڵ��С� + aNode.right=insert(x,aNode.right); + } + else{ //�����ݵ��ڵ�ǰ�ڵ������ʱ���������Ӧ���ڵ�ǰ�ڵ���,�����κβ����� + ; + } + return aNode; + } + + //��ӡ���������� + public void getData(){ + getData(root); + } + public void getData(BinaryNode root){ + if (root != null) { + //���� + this.getData(root.left); + + //�Һ��� + this.getData(root.right); + //���ڵ� + this.print(root); + } + + } + + //��ӡ�ڵ㡣 + public void print(BinaryNode root){ + System.out.println( + (Integer)(root.element) + ); + } + } diff --git a/group10/3314793852/src/myList/MyIterator.java b/group10/3314793852/src/myList/MyIterator.java new file mode 100644 index 0000000000..8c97809af8 --- /dev/null +++ b/group10/3314793852/src/myList/MyIterator.java @@ -0,0 +1,40 @@ + + package myList; + + public class MyIterator { + + private Object aData; + private int i=0; + private int l=0; + MyLinkedList.Node node; + public MyIterator(Object aDate){ + this.aData=aDate; + } + + public boolean hasNext(){ + if(aData instanceof MyArrayList){//MyArrayList��Iterator + + Object[] arr=((MyArrayList) aData).getArr(); + int a=((MyArrayList)aData).size(); + return a>i; + } + else{//MyLinkedList��Iterator + node=((MyLinkedList)aData).getHeadNode();//���ͷ�ڵ� + int a=((MyLinkedList)aData).size(); + return a>l; + } + + + } + public Object next(){ + if(aData instanceof MyArrayList){//MyArrayList��Iterator + + Object[] arr=((MyArrayList) aData).getArr(); + return arr[++i]; + } + else{//MyLinkedList��Iterator + l++; + return node.getDate(); + } + } + } diff --git a/group10/3314793852/src/myList/MyLinkedList.java b/group10/3314793852/src/myList/MyLinkedList.java new file mode 100644 index 0000000000..0bad8c8953 --- /dev/null +++ b/group10/3314793852/src/myList/MyLinkedList.java @@ -0,0 +1,122 @@ + package myList; + + /* + * �õ���������ͷ�ڵ�ġ� + */ + + public class MyLinkedList { + + private int theSize; //�����Ĵ�С�� + private Node headNode; //ͷ�ڵ� + + //�ڵ���ࡣ + public static class Node{ + + private Object data; + private Node node; + + public Node(){ + + } + + public Node(Object data, Node node) { + this.data = data; + this.node = node; + } + public Object getDate(){ + return data; + } + + } + + //���췽������ʼ��ʱ������һ����ͷ�ڵ�Ŀյ������� + public MyLinkedList(){ + clear(); + } + + //���ͷ�ڵ� + public Node getHeadNode(){ + return headNode; + } + + //������� + public void clear(){ + headNode=new Node(null,null); //ͷ����ʼ��������date��ָ��nodeȫ������Ϊnull. + theSize=0; //���Ĵ�С���� + } + + //��ȡ���Ĵ�С + public int size(){ + return theSize; + } + + //���ӽڵ㵽����β���� + public void add(Object aData){ + add(theSize+1,aData); + } + + //���ӽڵ㵽ָ��λ�á� + public void add(int idx,Object aDate){ + + //����һ���µĽڵ� + Node newNode=new Node(); + newNode.data=aDate; + + //�����������ҵ�ָ��λ�õĽڵ㣬�����½ڵ�嵽ָ��λ�ýڵ��ǰ��һ��λ�á� + Node p,q; + p=headNode; + + for(int i=1;i(arr.length-1)){//��tail�Ѿ���������β��ʱ���������ͷ����Ϊ��ʱ�����µ�������뵽�����ͷ���� + tail=0; + } + theSize++; + } + } + + //pop,�����С� + public Object pop(){ + Object a=null; + if(theSize!=0){ //�������Ϊ�գ����ܽ��г����еIJ����� + a=arr[head]; + arr[head]=null; + head++; + if(head>(arr.length-1)){ + head=0; + } + theSize--; + } + return a; + } + + //��ӡ�����С� + public void print(){ + for(int i=0;i Date: Sat, 25 Feb 2017 23:08:32 +0800 Subject: [PATCH 135/432] link queue bug xg --- .../2137642225/work01/src/com/coding/mybasic/LinkedList.java | 5 ++++- group01/2137642225/work01/src/com/coding/mybasic/Queue.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java index 43398abc03..ab37360e78 100644 --- a/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java +++ b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java @@ -55,7 +55,6 @@ else if(index == (size - 1)){ else { element = removeMiddle(index); } - size--; return element; } @@ -95,6 +94,7 @@ public void addFirst(Object o){ node.data = o; node.next = head.next; head = node; + size++; } public void addLast(Object o){ Node node = new Node(); @@ -116,6 +116,7 @@ private Object removeMiddle(int index) { Object element = removeNode.data; temp.next = removeNode.next; removeNode = null; + size--; return element; } @@ -190,6 +191,7 @@ private Object removeLastNode() { Object element = lastNode.data; lastNode = null; last = node; + size--; return element; } /** @@ -201,6 +203,7 @@ private Object removeFirstNode() { Object element = head.data; head = null; head = node; + size--; return element; } diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Queue.java b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java index 7a3e8f94d5..36d10fd668 100644 --- a/group01/2137642225/work01/src/com/coding/mybasic/Queue.java +++ b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java @@ -8,7 +8,7 @@ public void enQueue(Object o){ public Object deQueue(){ checkEmptyQueue(); - return linkedList.remove(size() - 1); + return linkedList.remove(0); } public boolean isEmpty(){ From 1dbd5aee827accb108dc6fbe2d482a1da19b2875 Mon Sep 17 00:00:00 2001 From: Leon1900 Date: Sat, 25 Feb 2017 23:08:40 +0800 Subject: [PATCH 136/432] Task01 @Leon1900 --- .../515505513/RemoteSystemsTempFiles/.project | 12 +++ group05/515505513/Task01/.classpath | 6 ++ group05/515505513/Task01/.gitignore | 1 + group05/515505513/Task01/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ .../src/com/coding/basic/ArrayList.java | 78 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++++ .../Task01/src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 48 ++++++++++++ .../Task01/src/com/coding/basic/List.java | 9 +++ .../Task01/src/com/coding/basic/Queue.java | 49 ++++++++++++ .../Task01/src/com/coding/basic/Stack.java | 49 ++++++++++++ .../src/com/coding/basic/TestExample.java | 27 +++++++ 13 files changed, 346 insertions(+) create mode 100644 group05/515505513/RemoteSystemsTempFiles/.project create mode 100644 group05/515505513/Task01/.classpath create mode 100644 group05/515505513/Task01/.gitignore create mode 100644 group05/515505513/Task01/.project create mode 100644 group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/515505513/Task01/src/com/coding/basic/ArrayList.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Iterator.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/LinkedList.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/List.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Queue.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Stack.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/TestExample.java diff --git a/group05/515505513/RemoteSystemsTempFiles/.project b/group05/515505513/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group05/515505513/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group05/515505513/Task01/.classpath b/group05/515505513/Task01/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/515505513/Task01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/515505513/Task01/.gitignore b/group05/515505513/Task01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/515505513/Task01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/515505513/Task01/.project b/group05/515505513/Task01/.project new file mode 100644 index 0000000000..cf494f7a91 --- /dev/null +++ b/group05/515505513/Task01/.project @@ -0,0 +1,17 @@ + + + Task01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs b/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/515505513/Task01/.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/group05/515505513/Task01/src/com/coding/basic/ArrayList.java b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..29fc9b98cb --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + + private int size = 0; + private static final int DEFAULT_SIZE = 100; + private Object[] elementData = new Object[DEFAULT_SIZE]; + + //添加元素 + public void add(Object o){ + add(size(),o); + } + + + public void add(int index, Object o){ + if(elementData.length==size()){ + ensureCapacity(size()*2 + 1); + } + for (int i = size; i > index; i--) + elementData[i]=elementData[i-1]; + elementData[index] = o; + size++; + } + //扩容 + public void ensureCapacity(int newCapacity){ + if(newCapacity < size){ + return; + } + Object[] oldElements = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size; i++) { + elementData[i] = oldElements[i]; + } + } + //返回固定下标的元素 + public Object get(int index){ + if(index<0 || index >=size){ + throw new ArrayIndexOutOfBoundsException("指定的index超过界限"); + } + return elementData[index]; + } + //删除指定位置的元素 + public Object remove(int index){ + Object removeElement = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i+1]; + } + size--; + return removeElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int current = 0; + @Override + public boolean hasNext() { + return current < size; + } + @Override + public Object next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + return elementData[current+1]; + } + } + +} diff --git a/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java b/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/Iterator.java b/group05/515505513/Task01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/LinkedList.java b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ea3d6a07f3 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head;//指针 + private Object element;//元素 + //添加一个元素 + public void add(Object o){ + + } + //在index处添加一个元素 + 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/group05/515505513/Task01/src/com/coding/basic/List.java b/group05/515505513/Task01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/Queue.java b/group05/515505513/Task01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fd316ac7bf --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Queue.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Queue { + private int maxSize;//队列容量 + private Object[] queue;//队列 + private int head;//队列头,可以删除 + private int tail;//队列尾,可以插入 + + + public Queue() { + this(10); + } + + public Queue(int maxSize) { + if(maxSize >=0){ + this.maxSize = maxSize; + this.queue = new Object[maxSize]; + head = tail = 0; + }else { + throw new RuntimeException("初始化大小不能小于0"); + } + } + + public void enQueue(Object o){ + if(tail == maxSize){ + throw new RuntimeException("队列已满,无法插入新的元素!"); + }else { + queue[tail++] = o; + } + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("空队列异常!"); + }else { + Object value = queue[head]; + queue[head++] = null; + return value; + } + } + //队列是否为空 + public boolean isEmpty(){ + return head==tail?true:false; + } + + public int size(){ + return tail-head; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/Stack.java b/group05/515505513/Task01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a85cfed9d2 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Stack.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Stack { + private Object[] data; + private int capacity; + private int size; + public Stack(){ + capacity = 16; + size = 0; + data = new Object[capacity]; + } + public void push(Object o){ + if(size < capacity){ + data[size++] = o; + }else { + ensureCapacity(); + data[size++] = o; + } + } + + private void ensureCapacity() { + capacity = capacity*2; + } + public Object pop(){ + if(size > 0){ + System.out.println(data[size-1]); + //data[size--] = null; + }else { + System.out.println("Empty stack"); + } + size--; + return data[size]; + } + + public Object peek(){ + if(size>0){ + return data[size-1]; + }else { + return null; + } + } + public boolean isEmpty(){ + + return size==0; + } + public int size(){ + return size; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/TestExample.java b/group05/515505513/Task01/src/com/coding/basic/TestExample.java new file mode 100644 index 0000000000..59bfa47a3c --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/TestExample.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class TestExample { + + public static void main(String[] args) { + /*Stack mystack = new Stack(); + for (int i = 0; i < 10; i++) { + mystack.push(i); + } + for (int i = 0; i < 10; i++) { + mystack.pop(); + System.out.println("==="+mystack.peek()); + }*/ + + /*Test Queue + Queue myQqueue = new Queue(10); + for (int i = 0; i < 10; i++) { + myQqueue.enQueue(i); + } + for (int i = 0; i < 10; i++) { + System.out.println(myQqueue.deQueue()); + }*/ + + + } + +} From f6c77f439ae116addb0982434bb30928a1945852 Mon Sep 17 00:00:00 2001 From: unknown <龚启盼> Date: Sat, 25 Feb 2017 23:15:21 +0800 Subject: [PATCH 137/432] =?UTF-8?q?=E6=9A=82=E5=AD=98=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apn/coding2017/basic/LinkedList.java | 115 +++++++++++++----- .../java/org/apn/coding2017/basic/Queue.java | 44 +++++-- .../java/org/apn/coding2017/basic/Stack2.java | 47 +++++++ 3 files changed, 166 insertions(+), 40 deletions(-) create mode 100644 group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java index 92d25ae5db..e83de27c11 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java @@ -3,68 +3,119 @@ /** * Created by QiPan on 2017/2/23. */ -public class LinkedList implements List { +public class LinkedList { - Node last; + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + public void add(Object o){ + add(size, o); + } - public boolean add(Object o) { + public void add(int index , Object o){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; - return true; } - public Object set(int index, Object element) { - return null; + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; } - public boolean add(int index, Object o) { + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } - return true; + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.data; } - public Object get(int index) { - return null; + public int size(){ + return size; } - public Object remove(int index) { - return null; + public void addFirst(Object o){ + add(0, o); } - public int size() { - return 0; + public void addLast(Object o){ + add(size, o); } - public boolean isEmpty() { - return false; + public Object removeFirst(){ + return remove(0); } - public Iterator iterator() { - return null; + public Object removeLast(){ + return remove(size-1); } - - private static class Node { - Object item; - Node next; - Node(Object item, Node next) { - this.item = item; - this.next = next; + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; } - public Object getItem() { - return item; + @Override + public boolean hasNext() { + return index < capacity; } - public void setItem(Object item) { - this.item = item; + @Override + public Object next() { + return get(index++); } - public Node getNext() { - return next; + @Override + public void remove() { + } + } - public void setNext(Node next) { + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; this.next = next; } } + } diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java index 8b40827110..d51695b148 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java @@ -2,21 +2,49 @@ /** * Created by QiPan on 2017/2/23. + * 队列,链表实现版本 */ -public class Queue { +public class Queue { - public void enQueue(Object o){ + private Node first; + private Node last; + private int N; + + private class Node { + E item; + Node next; + } + + public void enQueue(E e) { + // 向表尾添加元素 + Node oldLast = last; + last = new Node(); + last.item = e; + last.next = null; + if (isEmpty()){// 如果是往空的队列里面添加东西。那么首尾链表都是指向第一个元素 + first = last; + }else { + + oldLast.next = last; + } + N++; } - public Object deQueue(){ - return null; + public E deQueue() { + E item = first.item; + first = first.next; + if (isEmpty()){ + last = null; + } + N--; + return item; } - public boolean isEmpty(){ - return false; + public boolean isEmpty() { + return first == null; } - public int size(){ - return -1; + public int size() { + return N; } } diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java new file mode 100644 index 0000000000..2769c72485 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java @@ -0,0 +1,47 @@ +package org.apn.coding2017.basic; + +/** + * Created by Pan on 2017/2/25. + * 栈(链表实现): 下压栈。操作栈顶元素 + */ +public class Stack2 { + + private Node first; + private int N; + + private class Node { + E item; + Node next; + } + + public boolean isEmpty(){ + return first == null; + } + + public int size(){ + return N; + } + + /** + * 向栈顶添加元素 + * @param element + */ + public void push (E element){ + Node oldFirst = first; + first = new Node(); + first.item = element; + first.next = oldFirst; + N++; + } + + /** + * 弹出栈顶元素 + * @return + */ + public E pop(){ + E item = first.item; + first = first.next; + N--; + return item; + } +} From 21c7401013920ba2ba0caf55f3faf5b0a0371dcc Mon Sep 17 00:00:00 2001 From: core2for Date: Sat, 25 Feb 2017 23:21:19 +0800 Subject: [PATCH 138/432] delete article --- ...4\351\202\243\344\272\233\344\272\213.txt" | 20 ------------------- 1 file changed, 20 deletions(-) delete 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" 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" deleted file mode 100644 index ae17c6f15e..0000000000 --- "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" +++ /dev/null @@ -1,20 +0,0 @@ - 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)�� - ������cmd����javac�������Test.javaԴ�ļ�Ϊ�ֽ����ļ�����������ij��λ��(E:javaclasses/Test.class), - ��������ִ���ֽ����ļ��� - 1����ִ�г���ʱ������cpu��Ӳ�̵Ķ�д�ٶȲ��̫�������ṩ��һ���м���������Сcpu��Ӳ��֮��Ķ�д�ٶȲ�࣬����Ч������cpu�����Ȼ���ֽ����ļ���Ӳ���ﱻװ���ڴ棬���ڴ����棬�ֽ����Ϊ��һ��ָ�����У� - 2��cpu������ڴ��е�ָ�����У�һ��һ������ִ�У��Զ����ȡָ���ִ��ָ��IJ��������������һЩ������Ҫ�����ʱ���ֻ�ͨ���ڴ��������ʱ���ݣ�Ȼ��Ŵ�ŵ�Ӳ�̡�����ʹ��cpu��Ӳ��֮��Ķ�д�ٶȲ���С�������Ч�ʡ� From 1599f09d4dc18d2f7b4e2ef62bfdeaf1cb9b5c32 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sat, 25 Feb 2017 23:28:26 +0800 Subject: [PATCH 139/432] Update README.md --- .../src/com/ikook/basic_data_structure/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md b/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md index a565fb7148..6737563535 100644 --- a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md @@ -2,7 +2,7 @@ 1. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) - 1.1 ArrayList实现了一下方法: + 1.1 ArrayList实现了以下方法: ``` (1)add(Object): 添加元素到集合; @@ -16,7 +16,7 @@ (9)iterator(): 返回一个迭代器的实现类。 ``` - 1.2 LinkedList实现了一下方法: + 1.2 LinkedList实现以下方法: ``` (1)addFirst(Object): 在链表头部插入新的元素; @@ -36,7 +36,7 @@ (15)iterator(): 返回一个迭代器的实现类。 ``` - 1.3 Queue实现了一下方法: + 1.3 Queue实现了以下方法: ``` (1)enQueue(Object): 入队操作; @@ -45,7 +45,7 @@ (4)isEmpty(): 判断队列是否为空。 ``` - 1.4 Stack实现了一下方法: + 1.4 Stack实现了以下方法: ``` (1)push(Object):入栈操作; @@ -58,7 +58,7 @@ 2. 实现了BinarySearchTree、Iterator接口 - 2.1 BinarySearchTree实现了一下方法: + 2.1 BinarySearchTree实现了以下方法: ``` (1)insert(int):插入操作; @@ -67,7 +67,7 @@ (4)inorderTraverse(Node):遍历操作,采用中序遍历。 ``` - 2.2 Iterator定义了一下方法: + 2.2 Iterator定义了以下方法: ``` (1)hasNext():判断是否有元素没有被遍历; From 15d3661abc3ebbe2256beb32fe1eeca027ec2cab Mon Sep 17 00:00:00 2001 From: HuiZhou-Xmu <1814014897@qq.com> Date: Sat, 25 Feb 2017 23:28:32 +0800 Subject: [PATCH 140/432] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouhui/src/week01/BasicDataStructure/ArrayList.java | 3 ++- .../src/week01/BasicDataStructureTest/ArrayListTest.java | 9 +++------ .../src/week01/BasicDataStructureTest/StackTest.java | 7 ++++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java index e33d14ae20..23ed3f6bc2 100644 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java @@ -44,10 +44,11 @@ public Object get(int index){ public Object remove(int index){ if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + Object data_index = elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); elementData[size - 1] = null; size--; - return elementData; + return data_index; } public int size(){ diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java index 072d53f833..c5513acfda 100644 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java @@ -47,13 +47,10 @@ public void testGet() { @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.remove(0), 0); + Assert.assertEquals(arrayList.remove(0), 1); + Assert.assertEquals(arrayList.remove(97), 99); Assert.assertEquals(arrayList.size(), 97); - Assert.assertEquals(arrayList.get(96), 98); } @Test diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java index b8fb964955..ae6d3a39d4 100644 --- a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java @@ -30,9 +30,9 @@ public void testPush() { @Test public void testPop() { - stack.pop(); - Assert.assertEquals(stack.peek(), 98); - for(int i=0;i<99;i++){ + Assert.assertEquals(stack.pop(), 99); + Assert.assertEquals(stack.pop(), 98); + for(int i=0;i<98;i++){ stack.pop(); } Assert.assertEquals(stack.size(), 0); @@ -46,6 +46,7 @@ public void testPeek() { } stack.pop(); Assert.assertEquals(stack.peek(), 98); + Assert.assertEquals(stack.peek(), 98); } @Test From 5671ee360ad71f9481350757d9f95dbb9e45c17a Mon Sep 17 00:00:00 2001 From: xiaozhupig Date: Sat, 25 Feb 2017 23:42:39 +0800 Subject: [PATCH 141/432] =?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 文章地址:http://blog.csdn.net/codingxiaozhupig/article/details/57150919 --- .../src/com/firsthomework/MyArrayList.java | 107 +++++++++++++++++ .../src/com/firsthomework/MyLinkedList.java | 109 ++++++++++++++++++ .../src/com/firsthomework/MyQueue.java | 52 +++++++++ .../src/com/firsthomework/MyStack.java | 62 ++++++++++ 4 files changed, 330 insertions(+) create mode 100644 group06/799237637/src/com/firsthomework/MyArrayList.java create mode 100644 group06/799237637/src/com/firsthomework/MyLinkedList.java create mode 100644 group06/799237637/src/com/firsthomework/MyQueue.java create mode 100644 group06/799237637/src/com/firsthomework/MyStack.java diff --git a/group06/799237637/src/com/firsthomework/MyArrayList.java b/group06/799237637/src/com/firsthomework/MyArrayList.java new file mode 100644 index 0000000000..cf1a3e04a2 --- /dev/null +++ b/group06/799237637/src/com/firsthomework/MyArrayList.java @@ -0,0 +1,107 @@ +/* + * �ó���Ϊ�Լ�ʵ��ArrayList + * ArrayList�ײ�Ϊ����ʵ�֣��Ƕ�̬���� + */ +package com.firsthomework; + + +public class MyArrayList { + private int size=0; + private int initialcapcity=10; + private Object[] elements= null; + public MyArrayList(){ + this.elements=new Object[initialcapcity]; + } + + public MyArrayList(int capcity){ + + this.elements = new Object[capcity]; + + } + public void enlarge(){ + initialcapcity*=2; + Object[] tmpelements= new Object[initialcapcity]; + System.arraycopy(elements, 0, tmpelements, 0,size); + elements=tmpelements; + } + public void add(Object o){ + if(o==null){ + throw new RuntimeException("����Ϊ�գ�"); + } + if(size>initialcapcity){ + enlarge(); + } + elements[size]=o; + size++; + + } + public Object get(int index){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("����Խ�磺"); + } + return elements[index]; + + } +//ʵ��remove()������ɾ��λ�ã������Ԫ��Ҫǰ�ƣ�����ɾ��λ�õ�Ԫ�� + public Object remove(int index){ + Object oldValue=elements[index]; + int eleMoved=size-index-1; //�ƶ���Ԫ�صĸ��� + if(eleMoved>0){ + System.arraycopy(elements, //ԭ���� + index+1, //ԭ�������ʼλ�ã�ɾ��Ԫ�صĺ���һ��λ�ã� + elements, //Ŀ������ + index, //Ŀ���������ʼλ�� + eleMoved);//�����ij��� + } + elements[size-1]=null; + size--; + + return oldValue; + } + + public boolean set(int index,Object o){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("����Խ��"); + } + elements[index]=o; + return true; + } + + //��дtoString()���� + public String toString(){ + StringBuffer sb=new StringBuffer(); + sb.append("["); + for(int i=0;i Date: Sat, 25 Feb 2017 23:51:58 +0800 Subject: [PATCH 142/432] Add my first code v1.0 BinaryTreeNode has a bug --- .../coding2017/basic/ArrayListTest.java | 3 +- .../coding2017/basic/BinaryTreeNode.java | 121 ++++++++++++++++++ .../coding2017/basic/BinaryTreeNodeTest.java | 40 ++++++ .../coding2017/basic/LinkedListTest.java | 2 +- .../HarryHook/coding2017/basic/ListTest.java | 8 +- .../coding2017/basic/MyArrayList.java | 32 +++-- .../src/com/coding/basic/BinaryTreeNode.java | 93 +++++++++++++- liuxin/src/com/coding/basic/insert.java | 37 ++++++ 8 files changed, 317 insertions(+), 19 deletions(-) create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java create mode 100644 liuxin/src/com/coding/basic/insert.java diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java index bd15f2a404..0f22f6ba80 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ArrayListTest.java @@ -7,7 +7,8 @@ public class ArrayListTest extends ListTest { @Before - public void setUpArrayList() { + public void setUpArrayList() + { aList = new MyArrayList(); } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..09ad8c0fd5 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,121 @@ +/* + * Created by Harry 2017-2-23 10:50:39 + * 实现二叉树,并按二叉查找树插入节点 + * 能实现基本的功能,但测试时存在问题,传进去的数据为空 2017-2-2523:49:43 + */ +package com.github.HarryHook.coding2017.basic; + +public class BinaryTreeNode +{ + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + //中序遍历二叉树 + public void inOrder(BinaryTreeNode node) + { + if(node != null) + { + inOrder(node.left); + System.out.print(" " + node.data); + inOrder(node.right); + } + } + + //获取给节点的值 + public Integer getData() + { + return this.data; + } + //给一个节点赋值 + public void setData(Integer data) + { + this.data = data; + } + //获取左节点 + public BinaryTreeNode getLeft() + { + return left; + } + //指定左节点 + public void setLeft(BinaryTreeNode left) + { + this.left = left; + } + + //获取右节点 + public BinaryTreeNode getRight() + { + return right; + } + //指定右节点 + public void setRight(BinaryTreeNode right) + { + this.right = right; + } + + //在二叉树中插入一个节点,需要判断 + public BinaryTreeNode insert(Integer obj) + { + // 新增节点 + BinaryTreeNode newNode = new BinaryTreeNode(); + // 当前节点,保留根的值 + BinaryTreeNode current = this; + // 上个节点 + BinaryTreeNode parent = null; + // 如果根节点为空 + if (this.data == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + return newNode; + }else + { + while (true) + { + parent = current; + if (obj < current.data) + { + current = current.left; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + newNode.setData(obj); + newNode.setLeft(null); + newNode.setRight(null); + parent.right = newNode; + return newNode; + } + } + } + } + } + + public static void main(String[] args) + { + BinaryTreeNode BTN = new BinaryTreeNode(); + + BTN = BTN.insert(5); + System.out.print(BTN.getData() + " "); + System.out.print(BTN.insert(2).getData() + " "); + System.out.print(BTN.insert(1).getData() + " "); + System.out.print(BTN.insert(4).getData() + " "); + System.out.print(BTN.insert(6).getData() + " "); + System.out.print(BTN.insert(8).getData() + " "); + System.out.println(""); + System.out.println("中序遍历二叉树: "); + BTN.inOrder(BTN); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..7f706c24b5 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest +{ + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + + @Before + public void setUpBinaryTreeNode() + { + //binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() + { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + + //节点为空 说明值没有插进去 + binaryTreeNode.inOrder(binaryTreeNode); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java index 314c774dea..7c754e37af 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/LinkedListTest.java @@ -77,7 +77,7 @@ public void testLinkedListFunctional() { assertEquals(5, aLinkedList.size()); assertEquals(5, aLinkedList.get(0)); assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); + assertEquals(0, aLinkedList.get(3)); aLinkedList.remove(3); // [5, 4, 1, 3] assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java index b5680990ea..92f84b687c 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java @@ -88,9 +88,9 @@ public void testException() { } @Test - //protected static List aList; - public void testIterator() { + public void testIterator() + { Iterator it = aList.iterator(); assertEquals(false, it.hasNext()); @@ -111,6 +111,10 @@ public void testIterator() { assertEquals(1, it.next()); assertEquals(3, it.next()); assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java index 3ceb922c53..dbfd8aae19 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java @@ -5,6 +5,7 @@ package com.github.HarryHook.coding2017.basic; import java.util.Arrays; +import java.util.NoSuchElementException; public class MyArrayList implements List { @@ -40,6 +41,7 @@ public void add(int index, Object o) elementData[index] = o; } size++; + /* //判断索引位置是否正确 if (index > size || index < 0) @@ -62,8 +64,12 @@ public void add(int index, Object o) } public Object get(int index) - { + { + //若index超出size应该抛出异常 + if(index >= size) + throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); return elementData[index]; + } public Object remove(int index) @@ -83,7 +89,7 @@ public void ensureCapacity(int minCapacity) int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { - Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 if (newCapacity < minCapacity) newCapacity = minCapacity; @@ -112,25 +118,33 @@ public boolean hasNext() } public Object next() { - Object next = get(cursor); - cursor ++; - return next; + try { + Object next = get(cursor); + cursor++; + return next; + + } catch (IndexOutOfBoundsException e) + { + throw new NoSuchElementException(); + } + } } public static void main(String[] args) { MyArrayList myArrays = new MyArrayList(); - - for(int i = 0; i < 19; i++) - myArrays.add(i, 55); - myArrays.add(3); myArrays.add(0, 11); myArrays.add(1, 2); myArrays.add(3, 5); myArrays.add(2, 1); myArrays.add(7); + Print(myArrays); + + for(int i = 0; i < 19; i++) + myArrays.add(i, 55); + System.out.println("获取指定位置元素: " + myArrays.get(2)); System.out.println("删除指定位置元素: " + myArrays.remove(1)); System.out.println("当前元素个数:" + myArrays.size()); diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index d7ac820192..736ae328c7 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,11 +1,51 @@ package com.coding.basic; -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - +public class BinaryTreeNode +{ + public static TreeNode root; //根节点 + public BinaryTreeNode() + { + this.root = null; + } + public TreeNode insert (int key) + { + // 新增节点 + TreeNode newNode = new TreeNode(key); + // 当前节点 + TreeNode current = root; + // 上个节点 + TreeNode parent = null; + // 如果根节点为空 + if (current == null) + { + root = newNode; + return newNode; + } + while (true) + { + parent = current; + if (key < current.value) + { + current = current.left; + if (current == null) + { + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + parent.right = newNode; + return newNode; + } + } + } + } + +} + public Object getData() { return data; } @@ -30,3 +70,44 @@ public BinaryTreeNode insert(Object o){ } } + +class TreeNode //树的节点 +{ + int value; + TreeNode left; + TreeNode right; + + public TreeNode(int value) + { + this.value = value; + left = null; + right = null; + } +} + +public class BinaryTreeNodeTest { + + public static void main(String[] args) { + BinaryTreeNode b = new BinaryTreeNode(); + b.insert(3);b.insert(8);b.insert(1);b.insert(4);b.insert(6); + b.insert(2);b.insert(10);b.insert(9);b.insert(20);b.insert(25); + + // 打印二叉树 + b.toString(b.root); + System.out.println(); + + // 是否存在节点值10 + TreeNode node01 = b.search(10); + System.out.println("是否存在节点值为10 => " + node01.value); + // 是否存在节点值11 + TreeNode node02 = b.search(11); + System.out.println("是否存在节点值为11 => " + node02); + + // 删除节点8 + TreeNode node03 = b.delete(8); + System.out.println("删除节点8 => " + node03.value); + b.toString(b.root); + + + } +} diff --git a/liuxin/src/com/coding/basic/insert.java b/liuxin/src/com/coding/basic/insert.java new file mode 100644 index 0000000000..9ded2fcfac --- /dev/null +++ b/liuxin/src/com/coding/basic/insert.java @@ -0,0 +1,37 @@ + + //2017年2月24日17:02:55 + public BinaryTreeNode insert (Integer key) + { + // 新增节点 + BinaryTreeNode newNode = new BinaryTreeNode(key); + // 当前节点 + BinaryTreeNode current = root; + // 上个节点 + BinaryTreeNode parent = null; + // 如果根节点为空 + if (current == null) { + root = newNode; + return newNode; + } + while (true) + { + parent = current; + if (key < current.value) + { + current = current.left; + if (current == null) + { + parent.left = newNode; + return newNode; + } + } else + { + current = current.right; + if (current == null) + { + parent.right = newNode; + return newNode; + } + } + } + } From 0c2543d4f9da7929058e620e72b526fa1298f2f1 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sat, 25 Feb 2017 23:52:20 +0800 Subject: [PATCH 143/432] Add files via upload --- group08/1509102580/ArrayList.java | 103 +++++++++++++++ group08/1509102580/Iterator.java | 7 ++ group08/1509102580/LinkedList.java | 196 +++++++++++++++++++++++++++++ group08/1509102580/List.java | 9 ++ group08/1509102580/Queue.java | 33 +++++ group08/1509102580/Stack.java | 38 ++++++ 6 files changed, 386 insertions(+) create mode 100644 group08/1509102580/ArrayList.java create mode 100644 group08/1509102580/Iterator.java create mode 100644 group08/1509102580/LinkedList.java create mode 100644 group08/1509102580/List.java create mode 100644 group08/1509102580/Queue.java create mode 100644 group08/1509102580/Stack.java diff --git a/group08/1509102580/ArrayList.java b/group08/1509102580/ArrayList.java new file mode 100644 index 0000000000..02af2fc04e --- /dev/null +++ b/group08/1509102580/ArrayList.java @@ -0,0 +1,103 @@ +package com.zzk.coding2017.zuoye_1; + + +public class ArrayList implements List { + + private int size = 0; + private int length = 100; + private Object[] elementData = new Object[length]; + + public void add(Object o){ + if(sizesize-1){ + return ; + }else{ + if(sizesize-1){ + return null; + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + if(index<0||index>size-1){ + return null; + }else{ + Object result = elementData[index]; + if(index+1==size){//即index是最后一个元素 + elementData[index] = null; + size--; + return result; + }else{ + System.arraycopy(elementData, index+1, elementData, index, size-1-index); + size--; + return result; + } + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + int current = 0; + @Override + public Object next() { + // TODO Auto-generated method stub + if(current Date: Sat, 25 Feb 2017 23:57:51 +0800 Subject: [PATCH 144/432] finish basic data structures, pass all unit tests --- group02/851113375/.gitignore | 3 + .../coding2017/basic/ArrayList.java | 91 ++++++++++ .../coding2017/basic/BinaryTreeNode.java | 72 ++++++++ .../eloiseSJTU/coding2017/basic/Iterator.java | 6 + .../coding2017/basic/LinkedList.java | 170 ++++++++++++++++++ .../eloiseSJTU/coding2017/basic/List.java | 15 ++ .../eloiseSJTU/coding2017/basic/Queue.java | 28 +++ .../eloiseSJTU/coding2017/basic/Stack.java | 31 ++++ .../coding2017/basic/test/ArrayListTest.java | 15 ++ .../basic/test/BinaryTreeNodeTest.java | 34 ++++ .../coding2017/basic/test/LinkedListTest.java | 93 ++++++++++ .../coding2017/basic/test/ListTest.java | 120 +++++++++++++ .../coding2017/basic/test/QueueTest.java | 33 ++++ .../coding2017/basic/test/StackTest.java | 40 +++++ 14 files changed, 751 insertions(+) create mode 100644 group02/851113375/.gitignore create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java create mode 100644 group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java diff --git a/group02/851113375/.gitignore b/group02/851113375/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/851113375/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1aa396ce37 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java @@ -0,0 +1,91 @@ + +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + ensureCapacity(size + 1); + + if (index < size) { + System.arraycopy(elementData, index, elementData, index + 1, size - index); + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkBounds(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkBounds(index); + + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private int cur; + + @Override + public boolean hasNext() { + return cur != size; + } + + @Override + public Object next() { + if (cur >= size) { + throw new NoSuchElementException(); + } + return elementData[cur++]; + } + + } + + private void ensureCapacity(int capacity) { + if (capacity > elementData.length) { + capacity = elementData.length << 1; + elementData = Arrays.copyOf(elementData, capacity); + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..9aa88f2155 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode() {} + + public BinaryTreeNode(Integer data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Integer getData() { + return data; + } + + public void setData(Integer data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Integer o) { + if (data == null) { + data = o; + } else { + if (o < data) { + if (left == null) { + left = new BinaryTreeNode(o, null, null); + } else { + left = left.insert(o); + } + } else { + if (right == null) { + right = new BinaryTreeNode(o, null, null); + } else { + right = right.insert(o); + } + } + } + return this; + } + + public void print() { + if (left != null) { + left.print(); + } + System.out.println(data); + if (right != null) { + right.print(); + } + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..c37495db66 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..19833b1853 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node cur = head; + while (--index > 0) { + cur = cur.next; + } + Node newNode = new Node(o, cur.next); + cur.next = newNode; + size++; + } + } + + public Object get(int index) { + checkBounds(index); + + Node cur = head; + while (index-- > 0) { + cur = cur.next; + } + return cur.data; + } + + public Object remove(int index) { + checkBounds(index); + + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node cur = head; + int i = 0; + while (++i < index) { + cur = cur.next; + } + Node node = cur.next; + Object o = node.data; + cur.next = node.next; + node.data = null; + node.next = null; + size--; + return o; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + Node cur = head; + while (cur.next != null) { + cur = cur.next; + } + cur.next = newNode; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + + Object o = head.data; + Node node = head.next; + head.data = null; + head.next = null; + head = node; + size--; + return o; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + + if (head.next == null) { + return removeFirst(); + } + + Node cur = head; + int index = 0; + while (++index < size - 1) { + cur = cur.next; + } + Object o = cur.next.data; + cur.next.data = null; + cur.next = null; + size--; + return o; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private Node cur = head; + + @Override + public boolean hasNext() { + return cur != null; + } + + @Override + public Object next() { + if (cur == null) { + throw new NoSuchElementException(); + } + Object o = cur.data; + cur = cur.next; + return o; + } + + } + + private static class Node { + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java new file mode 100644 index 0000000000..2f6bdcc950 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); + + public Iterator iterator(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java new file mode 100644 index 0000000000..96e6547548 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java @@ -0,0 +1,28 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Queue { + + private int size = 0; + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.addLast(o); + size++; + } + + public Object deQueue() { + Object o = elementData.get(0); + elementData.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java new file mode 100644 index 0000000000..22b5117842 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Stack { + + private int size = 0; + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(--size); + elementData.remove(size); + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..a2bd0cad69 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import org.junit.Before; + +import com.github.eloiseSJTU.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..3da3cf3fa0 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,34 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + binaryTreeNode.print(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..0ad905694c --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..956ecb9167 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java @@ -0,0 +1,120 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.eloiseSJTU.coding2017.basic.Iterator; +import com.github.eloiseSJTU.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i = 0; i < 1000; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(100, aList.get(100)); + assertEquals(999, aList.get(999)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i = 0; i < 10; i++) + aList.add(i * 2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + + aList.add(3); + + expectedEx.expect(Exception.class); + aList.add(2, 5); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..39023e8aaa --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..98d2bd9de9 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} From 9cf0c3af1583337fa6e8ddc43cbe3d893b9b4721 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sat, 25 Feb 2017 23:57:57 +0800 Subject: [PATCH 145/432] =?UTF-8?q?Create=20=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "group08/1509102580/\346\226\207\347\253\240" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "group08/1509102580/\346\226\207\347\253\240" diff --git "a/group08/1509102580/\346\226\207\347\253\240" "b/group08/1509102580/\346\226\207\347\253\240" new file mode 100644 index 0000000000..d1dd75f51f --- /dev/null +++ "b/group08/1509102580/\346\226\207\347\253\240" @@ -0,0 +1,25 @@ +CPU,内存,磁盘,指令以及他们之间的关系 + +一、Cpu: +cpu是central process unit的简称,是计算机的核心处理器。Cpu的作用是运行机器语言指令。高级语言和汇编语言程序被编译成cpu能理解的机器语言后,存入到内存中,cpu就会从内存中读取并执行这些指令序列,完成程序功能。 +包括控制器、寄存器、运算器、时钟四个部分组成。 +其中运算器是用来计算的, +寄存器是临时存放中间数据以及指令的,包括程序计数器、指令寄存器、数据寄存器、地址寄存器等。 寄存器是汇编语言所研究的对象,汇编语言通过对寄存器的控制(写入,读出,加),实现功能。eg: + +第一句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 +第二句——对象eax寄存器;动作:add;参数:内存中的数;具体地址:ebp寄存器中存储的地址。 +第三句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 +程序计数器的作用是指定当前执行指令的地址。通过改变程序计数器,可以实现程序的逻辑控制。但是,虽然通过改变程序计数器的值可以实现程序跳转,但是单纯通过改变程序计数器的值却无法实现函数调用。因为跳转不需要回来,而函数调用结束之后还要回到原指令的下一条指令。为了实现函数调用,需要将程序计数器与call/return指令结合使用。也就是说,在函数调用时,在改变程序计数器值的同时,还要调用call指令,将返回位置写入栈内存中还需要在被调用函数的末尾添加return指令,这样在被调用函数执行结束后会继续执行return指令,return指令会将程序计数器的值设置为栈的最上边的值,也就是函数调用的返回地址。 +程序计数器和内存的关系:程序计数器记录了当前执行的指令在内存中年存储的位置。 +控制器是用来控制何时将内存的数据及指令读入寄存器,何时将寄存器中的值输出到内存。 +时钟是同步各个部件之间的步调。 +二、内存: +内存又叫主存,与cpu的寄存器链接,用于将数据以及指令输入到寄存器中;同时也可以接受寄存器的输出。本质是作为磁盘与寄存器的中间件,起到缓冲作用。断电后会丢失所有数据。 +内存和磁盘的关系:由于磁盘的读写速度与cpu相差太大,如果cpu直接从磁盘中读取数据,会造成cpu计算能力极大的浪费。为了解决这个问题,在磁盘与cpu之间加一个内存,将磁盘存储的程序以及数据线读入内存中,cpu直接与内存打交道。此外,由于内存的空间比磁盘小很多,会出现内存不够用的情况,此时,会将一部分磁盘当做内存来使用,也就是虚拟内存。 +三、磁盘: +存放数据的,容量比内存大,速度慢。 +断电不会丢失数据。 +四、指令: +指令是控制cpu按照一定功能运算的机器码。每一种cpu有自己的一套指令集。高级语言编写的程序最终也要编译成可以让cpu理解的一条条机器指令,才能被cpu执行。Cpu除了可以计算数据以外,还可以控制硬件,方法是通过cpu寄存器与I/O端口进行数据交换。计算机中主机与外围硬件的链接方式是通过I/O端口进行的(端口是用于主机寄存器与外围设备交换数据时临时存储数据的寄存器)。所以,指令通过控制cpu的输出和输入位置,可以让cpu寄存器向I/O端口寄存器发送数据,外围设备通过与I/O寄存器交换数据,从而达到控制外围设备的效果。而操作系统的作用是封装一套指令序列,cpu通过执行这套指令序列,可以让硬件完成一系列动作。而应用程序如果需要对硬件进行操作,只需要将操作系统的这套指令序列复制到自己的程序中即可。复制这套指令的方式就是API。从这个角度来看,操作系统就是封装了控制硬件完成一系列动作的指令序列的集合,这套集合提供给应用程序以及计算机的使用者。从而达到了将人、应用程序与硬件隔离的效果。 +五、它们之间的关系 +可执行程序(也就是机器码序列)首先从磁盘中被读入到内存中,然后内存中的程序会被一条一条的读到指令寄存器中被执行。 From dca48221d3371cf1f237c76959468758e8ef3c43 Mon Sep 17 00:00:00 2001 From: fei9009 Date: Sat, 25 Feb 2017 10:03:12 -0600 Subject: [PATCH 146/432] update junit test --- group02/527705641/.gitignore | 1 + .../coding2017/basic/ArrayListTest.java | 55 +++++++++++ .../coding2017/basic/LinkedListTest.java | 92 +++++++++++++++++++ .../fei9009/coding2017/basic/QueueTest.java | 35 +++++++ .../fei9009/coding2017/basic/StackTest.java | 37 ++++++++ 5 files changed, 220 insertions(+) create mode 100644 group02/527705641/.gitignore create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java create mode 100644 group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java diff --git a/group02/527705641/.gitignore b/group02/527705641/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group02/527705641/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..d623dd05e0 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java @@ -0,0 +1,55 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + @Before + public void setUp() throws Exception { + //testArray.clear(); + } + + @Test + public void testArrayList() { + //fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + testArray.add(10); + testArray.add(0, 3); + testArray.add(0, 2); + assertEquals(3, testArray.get(1)); + assertEquals(2, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testGet() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..1eb379a020 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedListTest.java @@ -0,0 +1,92 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + +private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..3e2889edc0 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/QueueTest.java @@ -0,0 +1,35 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + +private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..2872efb3e3 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/StackTest.java @@ -0,0 +1,37 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + +private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } +} From 4f7b4808b96c1803f8e2f6cf52724f1ab3693b39 Mon Sep 17 00:00:00 2001 From: ZhaoHongxin Date: Sun, 26 Feb 2017 00:19:49 +0800 Subject: [PATCH 147/432] test01 --- group05/1094051862/test01/.classpath | 7 + group05/1094051862/test01/.gitignore | 1 + group05/1094051862/test01/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 12 ++ .../src/com/coding1094051862/basic/.gitignore | 1 + .../coding1094051862/basic/ArrayListTest.java | 23 +++ .../basic/BinaryTreeNode.java | 32 ++++ .../com/coding1094051862/basic/Iterator.java | 7 + .../coding1094051862/basic/LinkedList.java | 142 ++++++++++++++++++ .../basic/LinkedListTest.java | 32 ++++ .../src/com/coding1094051862/basic/List.java | 9 ++ .../src/com/coding1094051862/basic/Queue.java | 19 +++ .../src/com/coding1094051862/basic/Stack.java | 22 +++ 13 files changed, 324 insertions(+) create mode 100644 group05/1094051862/test01/.classpath create mode 100644 group05/1094051862/test01/.gitignore create mode 100644 group05/1094051862/test01/.project create mode 100644 group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/List.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java diff --git a/group05/1094051862/test01/.classpath b/group05/1094051862/test01/.classpath new file mode 100644 index 0000000000..04cc82dc42 --- /dev/null +++ b/group05/1094051862/test01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/1094051862/test01/.gitignore b/group05/1094051862/test01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1094051862/test01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1094051862/test01/.project b/group05/1094051862/test01/.project new file mode 100644 index 0000000000..1dfd9165c6 --- /dev/null +++ b/group05/1094051862/test01/.project @@ -0,0 +1,17 @@ + + + test01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..d17b6724d1 --- /dev/null +++ b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +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/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore b/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore new file mode 100644 index 0000000000..709f74c2f7 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore @@ -0,0 +1 @@ +/ArrayList.java diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java b/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java new file mode 100644 index 0000000000..6345b9c57e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java @@ -0,0 +1,23 @@ +package com.coding1094051862.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void test() { + ArrayList list = new ArrayList(); + for(int i = 0; i < 10; i++) { + list.add(i); + } + Assert.assertEquals(10, list.size()); + list.add(11); + list.add(3,99); + Assert.assertEquals(99, list.get(3)); + Assert.assertEquals(12, list.size()); + Assert.assertEquals(99, list.remove(3)); + Assert.assertEquals(11, list.size()); + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java b/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..09d35349b8 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding1094051862.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/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java new file mode 100644 index 0000000000..33dcc89a6a --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding1094051862.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java new file mode 100644 index 0000000000..ea3b54241b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java @@ -0,0 +1,142 @@ +package com.coding1094051862.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size = 0; + + public void add(Object o){ + if (head == null) { + head = new Node(o, null); + size ++; + return; + } + Node n = new Node(o, null); + if (last == null) { + last = n; + head.next = last; + } + last.next = n; + last = n; + size ++; + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + System.out.println("linkedList.add: index < 0 || index > size"); + return; + } + if (index == size) { + add(o); + return; + } + if (index == 0) { + addFirst(o); + return; + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node post = pre.next; + Node n = new Node(o,post); + pre.next = n; + size ++; + } + public Object get(int index){ + if (index == 0) { + return head.data; + } + Node n = head; + for (int i = 1; i <= index; i++) { + n = n.next; + } + return n.data; + } + public Object remove(int index){ + if (index < 0 || index >= size) { + System.out.println("remove :index < 0 || index >= size"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node n = pre.next; + Node post = n.next; + n.next = null; + pre.next = post; + size --; + return n.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node n = new Node(o,head); + head = n; + size ++; + return; + } + public void addLast(Object o){ + Node n = new Node(o,null); + last.next = n; + last = n; + size ++; + return; + } + public Object removeFirst(){ + Object o = head.data; + Node n = head.next; + head.next = null; + head = n; + size --; + return o; + } + public Object removeLast(){ + Node preLast = head; + for (int i = 1; i < size; i++) { + preLast = preLast.next; + } + preLast.next = null; + Object o = last.data; + last = preLast; + size --; + return o; + } + public Iterator iterator(){ + return new Iterator() { + @Override + public Object next() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return false; + } + }; + } + + + private static class Node { + + Object data; + Node next; + + public Node (Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java new file mode 100644 index 0000000000..dccc8e1e86 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java @@ -0,0 +1,32 @@ +package com.coding1094051862.basic; + +import junit.framework.Assert; + +import org.junit.Test; + +public class LinkedListTest extends LinkedList { + + @Test + public void test() { + List list = new LinkedList(); + list.add(0); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(3, 33); + list.add(0, 100); + list.add(8,800); + Assert.assertEquals(9, list.size()); + Assert.assertEquals(100, list.get(0)); + Assert.assertEquals(0, list.get(1)); + Assert.assertEquals(1, list.get(2)); + Assert.assertEquals(2, list.get(3)); + Assert.assertEquals(33, list.get(4)); + Assert.assertEquals(3, list.get(5)); + Assert.assertEquals(4, list.get(6)); + Assert.assertEquals(800, list.get(8)); + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/List.java b/group05/1094051862/test01/src/com/coding1094051862/basic/List.java new file mode 100644 index 0000000000..7f1430b67b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/List.java @@ -0,0 +1,9 @@ +package com.coding1094051862.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/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java new file mode 100644 index 0000000000..fb9f6107fd --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding1094051862.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/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java new file mode 100644 index 0000000000..a9fbb4635b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding1094051862.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 015da347042a51b2de3c338458ddaff562a728ef Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:29:09 +0800 Subject: [PATCH 148/432] =?UTF-8?q?2017=E7=BC=96=E7=A8=8B=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E7=A4=BE=E7=BE=A4=E5=8D=9A=E5=AE=A2=EF=BC=9ACPU?= =?UTF-8?q?=EF=BC=8C=E5=86=85=E5=AD=98=EF=BC=8C=E7=A1=AC=E7=9B=98=EF=BC=8C?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E4=BB=A5=E5=8F=8A=E5=AE=83=E4=BB=AC=E4=B9=8B?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E5=85=B3=E7=B3=BB(2017.2.19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\345\205\263\347\263\273(2017.2.19).md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" diff --git "a/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" "b/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" new file mode 100644 index 0000000000..8fdc8b40b3 --- /dev/null +++ "b/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" @@ -0,0 +1,39 @@ +## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.19) + +> 本人对于CPU、内存、硬盘以及指令等一系列计算机核心组件理解甚浅。并且对其也不是很来感,不过身为一名软件专业学生以及未来的程序猿,还是硬着头皮研究研究。以下是对其学习的一个总结吧算是,有什么不正确的地方还请指出,不喜勿喷。 + +相信大家都知道计算机是由控制器、运算器、存储器、输入设备、输出设备五大部分组成。控制器 + 运算器组成了CPU,存储器即内存,当然硬盘也属于存储器,不过硬盘和内存存储形式有所不同,详细见下。 + +说明:点击以下标题查看维基百科详解 + +### [CPU](https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8) + +CPU即中央处理器(Central Processing Unit)。 + +它是计算机的主要设备之一,可以是说是计算机最重要的组成部分。常听到有人说CPU是"计算机的大脑",但我觉得这句话是不完全正确的。为什么这么说呢,CPU没有存储能力,只有为数不多的寄存器能临时存储一点东西,人可是有存储能力的,像最强大脑上有些人更拥有超强的记忆力。所以在存储能力这方面来讲,我觉得把CPU比作”大脑“的说法不太正确。就像刘欣前辈在其公号上讲解[CPU](http://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665513017&idx=1&sn=5550ee714abd36d0b580713f673e670b&scene=21#wechat_redirect)说的那样,"上帝为你关闭了一扇门,就一定会为你打开一扇窗",CPU虽然“脑容量”很小,但是它拥有超强的运算力。拿内存和硬盘来说,CPU比内存要快100倍,比硬盘快1000多万倍。这种运算速度可是人望尘莫及的了。所以在运算力这方面讲,又可以把CPU比作“超强的大脑”。 + +它负责处理、运算计算机内部的所有数据。 + +### [内存](https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E5%AD%98%E5%8F%96%E5%AD%98%E5%82%A8%E5%99%A8) + +内存即RAM,随机存取存储器(Random Access Memory)。 + +内存是计算机的主存,主存(Main memory)即电脑内部最重要的存储器,是与CPU直接交换数据的内部存储器。它用来加载各种各样的数据和程序以供CPU直接运行与运用。它是可以随时读写的,并且速度也很快,仅仅比CPU慢100倍。它通常作为操作系统或其他正在运行中的程序的临时数据存储媒介。通俗点说,内存就是用来临时存储数据,用来给CPU提供CPU要处理的东西。但是内存不会长期保存数据,只是临时存储,程序和数据处理完后就释放空间。 + +### [硬盘](https://zh.wikipedia.org/wiki/%E7%A1%AC%E7%9B%98) + +硬盘即HDD(Hard Disk Drive)。 + +硬盘是计算机上使用坚硬的旋转盘片为基础的非挥发性存储设备,它在平整的磁性表面存储和检索数字数据,信息通过离磁性表面很近的磁头,由电磁流来改变极性方式被电磁流写到磁盘上,信息可以通过相反的方式读取,例如读头经过纪录数据的上方时磁场导致线圈中电气信号的改变。硬盘的读写是采用随机存取的方式,因此可以以任意顺序读取硬盘中的数据。以上来自维基百科。各种专业名词,我相信你已经看厌倦了快。说白了,硬盘就是存东西的,长期存,也就是具有记忆力。不像CPU和内存,“一觉醒来就忘了以前的事情”,所有的数据全都清空。硬盘会长期存储数据,只要是不人为删除,不出现硬件故障,东西就不会丢。 + +### [指令](https://zh.wikipedia.org/wiki/%E6%8C%87%E4%BB%A4) + +指令,怎么说呢,指令就是任何可执行程序的元素的表述。指令一般会包含一个操作码和零到多个操作数。操作码指定了要进行什么样的操作。操作数可能指定了参与操作的寄存器、内存地址或立即数,它可能还会包含寻址方式,寻址方式确定操作数的含义。说白了,指令就是CPU的命令,CPU通过寄存器中的指令来进行数据的操作。 + +另外,指令一般有四种:加载、存储、操作和跳转。 + +### 它们之间的关系 + +CPU从内存或缓存中取出指令,放入指令寄存器,并对指令译码进行分解,进而对数据进行处理。这么说吧,计算机中所有的程序运行都是在内存中进行的,因此内存对计算机性能的影响非常大。数据由传输速度较慢的硬盘通过内存传送到CPU进行处理。不过内存是带电存储的(断电数据就会消失),而且容量十分有限,所以要长时间储存程序或数据就需要使用硬盘。 + +所以计算机处理数据大概就通过以上几个部分:数据(硬盘)——>内存——>CPU——>CPU通过指令处理数据 \ No newline at end of file From 64d11659f5db89abb494416bf7a1511b1d9011d4 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:36:00 +0800 Subject: [PATCH 149/432] The datum add README.md --- ...\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" | 2 ++ group18/935542673/Datum/README.md | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 group18/935542673/Datum/README.md diff --git "a/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" "b/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" index 8fdc8b40b3..df7de4d68c 100644 --- "a/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" +++ "b/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" @@ -1,5 +1,7 @@ ## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.19) +原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-hdd-instruct-detailed-annotation/) + > 本人对于CPU、内存、硬盘以及指令等一系列计算机核心组件理解甚浅。并且对其也不是很来感,不过身为一名软件专业学生以及未来的程序猿,还是硬着头皮研究研究。以下是对其学习的一个总结吧算是,有什么不正确的地方还请指出,不喜勿喷。 相信大家都知道计算机是由控制器、运算器、存储器、输入设备、输出设备五大部分组成。控制器 + 运算器组成了CPU,存储器即内存,当然硬盘也属于存储器,不过硬盘和内存存储形式有所不同,详细见下。 diff --git a/group18/935542673/Datum/README.md b/group18/935542673/Datum/README.md new file mode 100644 index 0000000000..12d97da663 --- /dev/null +++ b/group18/935542673/Datum/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群博客 + +2017.2.19 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Datum/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) \ No newline at end of file From ff0446ccd27a8147629124fc3730dd5c9851d6b3 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:38:37 +0800 Subject: [PATCH 150/432] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=20Blog=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=BB=A5=E5=8F=8A=E6=B7=BB=E5=8A=A0=E8=BF=9B?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8D=9A=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" | 0 group18/935542673/{Datum => Blog}/README.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename "group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" => "group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" (100%) rename group18/935542673/{Datum => Blog}/README.md (100%) diff --git "a/group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" "b/group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" similarity index 100% rename from "group18/935542673/Datum/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" rename to "group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" diff --git a/group18/935542673/Datum/README.md b/group18/935542673/Blog/README.md similarity index 100% rename from group18/935542673/Datum/README.md rename to group18/935542673/Blog/README.md From fc23b6e513794800b390b9470f61251c6b42edc5 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:40:16 +0800 Subject: [PATCH 151/432] Update README.md --- group18/935542673/Blog/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group18/935542673/Blog/README.md b/group18/935542673/Blog/README.md index 12d97da663..3e2a12cc5c 100644 --- a/group18/935542673/Blog/README.md +++ b/group18/935542673/Blog/README.md @@ -1,3 +1,3 @@ ## 2017编程提高社群博客 -2017.2.19 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Datum/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) \ No newline at end of file +2017.2.19 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) \ No newline at end of file From 1191581ba2f554406e5aea46c479f7d88115aa08 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:43:27 +0800 Subject: [PATCH 152/432] Update README.md --- group18/935542673/Blog/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/group18/935542673/Blog/README.md b/group18/935542673/Blog/README.md index 3e2a12cc5c..7f74cf8b4a 100644 --- a/group18/935542673/Blog/README.md +++ b/group18/935542673/Blog/README.md @@ -1,3 +1,4 @@ ## 2017编程提高社群博客 -2017.2.19 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) \ No newline at end of file +2017.2.25 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) + From 7b161f47d4d3a62bee05e0394d629622f194ebff Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:44:00 +0800 Subject: [PATCH 153/432] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E7=AF=87=E5=8D=9A=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename "group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" => "group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" (99%) diff --git "a/group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" similarity index 99% rename from "group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" rename to "group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" index df7de4d68c..8de7f5c88c 100644 --- "a/group18/935542673/Blog/2017\347\274\226\347\250\213\346\217\220\351\253\230\357\274\232CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273(2017.2.19).md" +++ "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -1,4 +1,4 @@ -## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.19) +## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.25) 原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-hdd-instruct-detailed-annotation/) From d08afafffe3aab6d1d0ff264827d807befcb3248 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:45:13 +0800 Subject: [PATCH 154/432] Update README.md --- group18/935542673/Blog/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group18/935542673/Blog/README.md b/group18/935542673/Blog/README.md index 7f74cf8b4a..0533a45f85 100644 --- a/group18/935542673/Blog/README.md +++ b/group18/935542673/Blog/README.md @@ -1,4 +1,4 @@ ## 2017编程提高社群博客 -2017.2.25 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/2017%E7%BC%96%E7%A8%8B%E6%8F%90%E9%AB%98%EF%BC%9ACPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB(2017.2.19).md) +2017.2.25 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/CPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB.md) From e626bb802a9cdb888c978a8183ef0c6af8a00c2f Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:51:50 +0800 Subject: [PATCH 155/432] Add README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用来汇总2017编程提高社群 Coding --- group18/935542673/Coding/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 group18/935542673/Coding/README.md diff --git a/group18/935542673/Coding/README.md b/group18/935542673/Coding/README.md new file mode 100644 index 0000000000..50eeea3928 --- /dev/null +++ b/group18/935542673/Coding/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群Coding + +2017.2.24 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/src/com/ikook/basic_data_structure) \ No newline at end of file From c26d74c578eb5dabe5d2c8a5e34d92445777085f Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 00:54:09 +0800 Subject: [PATCH 156/432] Update Datum --- ...24\250\347\232\204\351\227\256\351\242\230.docx" | Bin 1 file changed, 0 insertions(+), 0 deletions(-) rename "group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" => "group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" (100%) diff --git "a/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" "b/group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" similarity index 100% rename from "group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" rename to "group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" From b8d2634e89a27e4d19b46a4200cf56de34a056f9 Mon Sep 17 00:00:00 2001 From: =gogoloda <=gogoloda@gmail.com> Date: Sat, 25 Feb 2017 10:54:12 -0600 Subject: [PATCH 157/432] Commit my new homework files except the Iterator to my master --- group04/564451732/.classpath | 7 ++ group04/564451732/.gitignore | 1 + group04/564451732/.project | 17 +++ group04/564451732/src/DataStructureTest.java | 114 +++++++++++++++++ group04/564451732/src/Test.java | 8 ++ group04/564451732/src/hw1/ArrayListImpl.java | 88 +++++++++++++ group04/564451732/src/hw1/BinaryTreeNode.java | 71 +++++++++++ group04/564451732/src/hw1/Iterator.java | 7 ++ group04/564451732/src/hw1/LinkedList.java | 117 ++++++++++++++++++ group04/564451732/src/hw1/List.java | 9 ++ group04/564451732/src/hw1/QueueImpl.java | 21 ++++ group04/564451732/src/hw1/StackImpl.java | 23 ++++ 12 files changed, 483 insertions(+) create mode 100644 group04/564451732/.classpath create mode 100644 group04/564451732/.gitignore create mode 100644 group04/564451732/.project create mode 100644 group04/564451732/src/DataStructureTest.java create mode 100644 group04/564451732/src/Test.java create mode 100644 group04/564451732/src/hw1/ArrayListImpl.java create mode 100644 group04/564451732/src/hw1/BinaryTreeNode.java create mode 100644 group04/564451732/src/hw1/Iterator.java create mode 100644 group04/564451732/src/hw1/LinkedList.java create mode 100644 group04/564451732/src/hw1/List.java create mode 100644 group04/564451732/src/hw1/QueueImpl.java create mode 100644 group04/564451732/src/hw1/StackImpl.java diff --git a/group04/564451732/.classpath b/group04/564451732/.classpath new file mode 100644 index 0000000000..2bdfdc1bf0 --- /dev/null +++ b/group04/564451732/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/564451732/.gitignore b/group04/564451732/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/564451732/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/564451732/.project b/group04/564451732/.project new file mode 100644 index 0000000000..23af4c7791 --- /dev/null +++ b/group04/564451732/.project @@ -0,0 +1,17 @@ + + + 564451732 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/564451732/src/DataStructureTest.java b/group04/564451732/src/DataStructureTest.java new file mode 100644 index 0000000000..2f5cc27a94 --- /dev/null +++ b/group04/564451732/src/DataStructureTest.java @@ -0,0 +1,114 @@ +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import hw1.ArrayListImpl; +import hw1.BinaryTreeNode; +import hw1.LinkedList; +import hw1.QueueImpl; +import hw1.StackImpl; + +public class DataStructureTest { + + @Test + public void ArrayListTest() { + ArrayListImpl al = new ArrayListImpl(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + al.add(new Object()); + al.add(new Object()); + assertTrue(al.size() == 2); + al.add(5); + assertFalse(al.size() == 2); + assertTrue(al.size() == 3); + al.add(2, 3); + //System.out.println((int)al.get(2)); + assertTrue((int)al.get(2) == 3); + assertTrue((int)al.get(3) == 5); + } + + @Test + public void LinkedListTest() { + LinkedList ll = new LinkedList(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + Object o4 = new Object(); + Object o5 = new Object(); + Object o6 = new Object(); + ll.add(o1); + ll.add(o2); + //System.out.println(ll.size()); + assertTrue(ll.size() == 2); + ll.add(o3); + ll.add(1,1); + assertTrue((int)ll.get(1) == 1); + assertTrue(ll.size() == 4); + ll.addFirst(2); + ll.addLast(4); + assertTrue((int)ll.get(0) == 2); + assertTrue((int)ll.get(ll.size() - 1) == 4); + assertTrue((int)ll.get(2) == 1); + ll.remove(2); + assertTrue(ll.get(2) == o2); + //System.out.print((int)ll.remove(2)); + //System.out.println((int)ll.get(2)); + //assertFalse((int)ll.get(2) == 1); + assertTrue(ll.size() == 5); + assertTrue((int)ll.removeFirst() == 2); + assertTrue(ll.size() == 4); + assertTrue((int)ll.removeLast() == 4); + assertTrue(ll.size() == 3); + } + + @Test + public void QueueTest(){ + QueueImpl qi = new QueueImpl(); + assertTrue(qi.isEmpty()); + qi.enQueue(1); + qi.enQueue(2); + qi.enQueue(3); + assertTrue(qi.size() == 3); + assertTrue((int)qi.deQueue() == 1); + assertTrue(qi.size() == 2); + assertFalse(qi.isEmpty()); + } + + @Test + public void StackTest() { + StackImpl stack = new StackImpl(); + assertTrue(stack.isEmpty()); + stack.push(1); + stack.push(2); + stack.push(3); + assertTrue(stack.size() == 3); + assertTrue((int)stack.pop() == 3); + assertTrue(stack.size() == 2); + assertTrue((int)stack.peek() == 2); + assertFalse(stack.isEmpty()); + } + + @Test + public void BinaryTreeTest() { + BinaryTreeNode bt = new BinaryTreeNode(); + Integer i1 = 1; + Integer i2 = 3; + Integer i3 = 4; + Integer i4 = 6; + Integer i5 = -1; + bt.insert(i3); + bt.insert(i1); + bt.insert(i4); + bt.insert(i2); + bt.insert(i5); + assertTrue((int)bt.getRoot().getData() == 4); + assertTrue((int)bt.getRoot().getLeft().getData() == 1); + assertTrue((int)bt.getRoot().getRight().getData() == 6); + assertTrue((int)bt.getRoot().getLeft().getLeft().getData() == -1); + assertTrue((int)bt.getRoot().getLeft().getRight().getData() == 3); + + } + +} diff --git a/group04/564451732/src/Test.java b/group04/564451732/src/Test.java new file mode 100644 index 0000000000..3335242a80 --- /dev/null +++ b/group04/564451732/src/Test.java @@ -0,0 +1,8 @@ + +public class Test { + + public static void main(String[] args) { + + } + +} diff --git a/group04/564451732/src/hw1/ArrayListImpl.java b/group04/564451732/src/hw1/ArrayListImpl.java new file mode 100644 index 0000000000..fe42bfc4f7 --- /dev/null +++ b/group04/564451732/src/hw1/ArrayListImpl.java @@ -0,0 +1,88 @@ +package hw1; + +//import java.util.ArrayList; +import java.util.Iterator; +//import java.util.List; + +import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; + +public class ArrayListImpl implements List { + +private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //size++; + try{ + if (size < elementData.length) { + elementData[size] = o; + } else { + elementData = grow(elementData, elementData.length); + elementData[size] = o; + } + size++; + } catch (IndexOutOfBoundsException e) { + System.out.println("The added index is out of bound"); + } + } + public void add(int index, Object o){ + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be less than 0"); + } + while (index >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + if (index >= size) { + elementData[index] = o; + size++; + } else { + if (size + 1 >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + for (int i = size-1; i >= index;i--) { + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + + } + } + + public Object get(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + return elementData[index]; + } + + } + + public Object remove(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + Object result = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i+1]; + } + elementData[size - 1] = 0; + size--; + return result; + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private Object[] grow (Object[] src, int increase) { + Object[] target = new Object[src.length + increase]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group04/564451732/src/hw1/BinaryTreeNode.java b/group04/564451732/src/hw1/BinaryTreeNode.java new file mode 100644 index 0000000000..4b0367f13c --- /dev/null +++ b/group04/564451732/src/hw1/BinaryTreeNode.java @@ -0,0 +1,71 @@ +package hw1; + +public class BinaryTreeNode { + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode (Comparable data) { + this.data = data; + } + public BinaryTreeNode () { + + } + public Object getData() { + return data; + } + public void setData(Comparable 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; + return; + } + + + + public BinaryTreeNode getRoot() { + return root; + } + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public BinaryTreeNode insert(Comparable o){ + BinaryTreeNode result = new BinaryTreeNode(o); + if (root == null) { + root = result; + } else { + BinaryTreeNode dummy = root; + insertHelper(o,dummy); + + } + return result; + } + + private void insertHelper (Comparable o, BinaryTreeNode root) { + if (o.compareTo(root.data) < 0) { + if (root.left == null) { + root.left = new BinaryTreeNode(o); + } else { + insertHelper (o, root.left); + } + } else { + if (root.right == null) { + root.right = new BinaryTreeNode(o); + + } else { + insertHelper (o, root.right); + } + } + } +} diff --git a/group04/564451732/src/hw1/Iterator.java b/group04/564451732/src/hw1/Iterator.java new file mode 100644 index 0000000000..93e22e9377 --- /dev/null +++ b/group04/564451732/src/hw1/Iterator.java @@ -0,0 +1,7 @@ +package hw1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/564451732/src/hw1/LinkedList.java b/group04/564451732/src/hw1/LinkedList.java new file mode 100644 index 0000000000..793f7c66d6 --- /dev/null +++ b/group04/564451732/src/hw1/LinkedList.java @@ -0,0 +1,117 @@ +package hw1; + +import java.util.Iterator; + +public class LinkedList implements List { +private Node head; +private Node tail; + + public void add(Object o){ + if (head != null) { + Node dummy = new Node(new Object()); + dummy.next = head; + Node nextNode = head; + + while (nextNode != null) { + dummy = dummy.next; + nextNode = nextNode.next; + } + dummy.next = new Node(o); + tail = dummy.next; + } else { + head = new Node(o); + tail = head; + } + + } + public void add(int index , Object o){ + if (index < 0 || index > size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index; i++) { + dummy = dummy.next; + } + Node temp = dummy.next; + dummy.next = new Node(o); + dummy.next.next = temp; + if (index == size()) { + tail = dummy.next; + } + } + public Object get(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 0; i < index;i++) { + dummy = dummy.next; + } + return dummy.data; + } + public Object remove(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index;i++) { + dummy = dummy.next; + } + Node result = dummy.next; + dummy.next = dummy.next.next; + if (dummy.next == null) { + tail = dummy; + } + return result.data; + } + + public int size(){ + Node dummy = head; + int size = 0; + while (dummy != null) { + dummy = dummy.next; + size++; + } + + return size; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head; + head = first; + } + public void addLast(Object o){ + tail.next = new Node(o); + tail = tail.next; + } + public Object removeFirst(){ + Node temp = head; + head = head.next; + return temp.data; + } + public Object removeLast(){ + Node dummy = head; + for (int i = 1; i < size()-1; i++) { + dummy = dummy.next; + } + Node result = dummy.next; + tail = dummy; + dummy.next = null; + return result.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } +} diff --git a/group04/564451732/src/hw1/List.java b/group04/564451732/src/hw1/List.java new file mode 100644 index 0000000000..a976c82204 --- /dev/null +++ b/group04/564451732/src/hw1/List.java @@ -0,0 +1,9 @@ +package hw1; + +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/group04/564451732/src/hw1/QueueImpl.java b/group04/564451732/src/hw1/QueueImpl.java new file mode 100644 index 0000000000..30c73585ff --- /dev/null +++ b/group04/564451732/src/hw1/QueueImpl.java @@ -0,0 +1,21 @@ +package hw1; + +public class QueueImpl { + LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.add(o); + } + + public Object deQueue(){ + return queueList.removeFirst(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group04/564451732/src/hw1/StackImpl.java b/group04/564451732/src/hw1/StackImpl.java new file mode 100644 index 0000000000..3ae23593fa --- /dev/null +++ b/group04/564451732/src/hw1/StackImpl.java @@ -0,0 +1,23 @@ +package hw1; + +public class StackImpl { +private ArrayListImpl elementData = new ArrayListImpl(); + + 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(); + } +} From c28bf65fc1edaed10078ef81a6c39597c3a08160 Mon Sep 17 00:00:00 2001 From: tennysons Date: Sun, 26 Feb 2017 01:05:29 +0800 Subject: [PATCH 158/432] note link --- ...0\227\347\254\224\350\256\260\351\223\276\346\216\245.md" | 5 +++++ group14/296933284/README.md | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 "group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" diff --git "a/group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" "b/group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" new file mode 100644 index 0000000000..e5c29512d4 --- /dev/null +++ "b/group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" @@ -0,0 +1,5 @@ +# 2017编程提高(Java)学习系列笔记链接 +--- + +1. [漫谈计算机组成 -- 微型计算机的硬件组成](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) 2017-02-26 + \ No newline at end of file diff --git a/group14/296933284/README.md b/group14/296933284/README.md index b2040bf4d1..a87e055517 100644 --- a/group14/296933284/README.md +++ b/group14/296933284/README.md @@ -1,6 +1,6 @@  -# 2017年编程提高(Java) 作业、练习、总结的记录 +# 2017年编程提高(Java) 作业、练习、总结 DataStructuresTest // 基本数据结构Java实现 From ac49709af4883474505e93d65fee0af2b95979b2 Mon Sep 17 00:00:00 2001 From: tennysons Date: Sun, 26 Feb 2017 01:06:42 +0800 Subject: [PATCH 159/432] note --- .../296933284/Note/README.md | 1 - 1 file changed, 1 deletion(-) rename "group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" => group14/296933284/Note/README.md (98%) diff --git "a/group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" b/group14/296933284/Note/README.md similarity index 98% rename from "group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" rename to group14/296933284/Note/README.md index e5c29512d4..f4b8f276e5 100644 --- "a/group14/296933284/Note/2017\347\274\226\347\250\213\346\217\220\351\253\230(Java)\345\255\246\344\271\240\347\263\273\345\210\227\347\254\224\350\256\260\351\223\276\346\216\245.md" +++ b/group14/296933284/Note/README.md @@ -1,5 +1,4 @@ # 2017编程提高(Java)学习系列笔记链接 ---- 1. [漫谈计算机组成 -- 微型计算机的硬件组成](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) 2017-02-26 \ No newline at end of file From 3081df977d3ca0ddc59fb865a0486f397cacbcaf Mon Sep 17 00:00:00 2001 From: akinaru-lu Date: Sun, 26 Feb 2017 02:07:29 +0900 Subject: [PATCH 160/432] 2-26 assignment --- .../2-26/src/com/coding/basic/ArrayList.java | 2 +- .../2-26/src/com/coding/basic/BinaryTree.java | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 group08/729770920/2-26/src/com/coding/basic/BinaryTree.java diff --git a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java index 8fe149f4ad..1a7cc5a361 100644 --- a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java +++ b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java @@ -24,7 +24,7 @@ public void add(int index, E e){ throw new IndexOutOfBoundsException(Integer.toString(index)); } if (data.length == size) { - ensureCapacity(size * 2 + 1); + ensureCapacity(size + size >> 1 + 1); } for (int i = size++; i > index; --i) { data[i] = data[i - 1]; diff --git a/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java b/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..0b41529787 --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,105 @@ +package com.coding.basic; + +public class BinaryTree> { + BinaryTreeNode root = null; + + public BinaryTree() { + } + + public boolean isEmpty() { + return root == null; + } + + public void insert(E e) { + root = insert(root, e); + } + + private BinaryTreeNode insert(BinaryTreeNode node, E e) { + if (node == null) { + node = new BinaryTreeNode(e, null, null); + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + node.left = insert(node.left, e); + } else if (compareResult > 0) { + node.right = insert(node.right, e); + } + return node; + } + + public void clear() { + root = null; + } + + public boolean contains(E e) { + return contains(root, e); + } + + private boolean contains(BinaryTreeNode node, E e) { + if (node == null) { + return false; + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + return contains(node.left, e); + } else if (compareResult > 0) { + return contains(node.right, e); + } + // matching + return true; + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node != null) { + while (node.left != null) { + node = node.right; + } + } + return node; + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node != null) { + while (node.right != null) { + node = node.right; + } + } + return node; + } + + public void remove(E e) { + root = remove(root, e); + } + + private BinaryTreeNode remove(BinaryTreeNode node, E e) { + if (node == null) { + return node; + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + node.left = remove(node.left, e); + } else if (compareResult > 0) { + node.right = remove(node.right, e); + } + // matching + if (node.left != null && node.right != null) { // two children + node.data = (E) findMax(node.right).data; + node.right = remove(node.right, node.data); + } else { // one child + node = (node.left != null) ? node.left : node.right; + } + return node; + } + + private class BinaryTreeNode { + E data; + BinaryTreeNode left; + BinaryTreeNode right; + + public BinaryTreeNode(E e, BinaryTreeNode l, BinaryTreeNode r) { + data = e; + left = l; + right = r; + } + } +} From aecbf3d1f05718c567c2e1ef48138206fe5b393a Mon Sep 17 00:00:00 2001 From: tennysons Date: Sun, 26 Feb 2017 01:09:44 +0800 Subject: [PATCH 161/432] note --- group14/296933284/Note/README.md | 4 ++-- group14/296933284/README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/group14/296933284/Note/README.md b/group14/296933284/Note/README.md index f4b8f276e5..3b92cb5667 100644 --- a/group14/296933284/Note/README.md +++ b/group14/296933284/Note/README.md @@ -1,4 +1,4 @@ # 2017编程提高(Java)学习系列笔记链接 +--- -1. [漫谈计算机组成 -- 微型计算机的硬件组成](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) 2017-02-26 - \ No newline at end of file +1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) \ No newline at end of file diff --git a/group14/296933284/README.md b/group14/296933284/README.md index a87e055517..bee5bbfe2e 100644 --- a/group14/296933284/README.md +++ b/group14/296933284/README.md @@ -2,8 +2,8 @@ # 2017年编程提高(Java) 作业、练习、总结 -DataStructuresTest // 基本数据结构Java实现 - +DataStructuresTest // 基本数据结构Java实现 +Note // 2017编程提高(Java)学习系列笔记链接 From 9706d6d27ea2fe27a13c7b1c2085db2a4f20b772 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:10:58 +0800 Subject: [PATCH 162/432] Add README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 README.md 文件说明代码仓库结构 --- group18/935542673/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 group18/935542673/README.md diff --git a/group18/935542673/README.md b/group18/935542673/README.md new file mode 100644 index 0000000000..b8dbe9769f --- /dev/null +++ b/group18/935542673/README.md @@ -0,0 +1,8 @@ +## 2017编程提高社群 + +群昵称:青岛-ikook + +QQ号码:935542673 + +--- + From a8adc5cc02b2483cd635b30bd8e5501254373206 Mon Sep 17 00:00:00 2001 From: tennysons Date: Sun, 26 Feb 2017 01:13:23 +0800 Subject: [PATCH 163/432] note --- group14/296933284/README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/group14/296933284/README.md b/group14/296933284/README.md index bee5bbfe2e..023abf1e46 100644 --- a/group14/296933284/README.md +++ b/group14/296933284/README.md @@ -1,9 +1,4 @@ - - -# 2017年编程提高(Java) 作业、练习、总结 - -DataStructuresTest // 基本数据结构Java实现 -Note // 2017编程提高(Java)学习系列笔记链接 - - +# 2017年编程提高(Java) 作业、练习、总结 +[DataStructuresTest --- 基本数据结构Java实现](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) +[Note --- 2017编程提高(Java)学习系列笔记链接](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note) From f9f1579079e05b33cb38b73b02a6d0f55d29f887 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:15:03 +0800 Subject: [PATCH 164/432] Update README.md --- group18/935542673/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/group18/935542673/README.md b/group18/935542673/README.md index b8dbe9769f..58e32f7595 100644 --- a/group18/935542673/README.md +++ b/group18/935542673/README.md @@ -4,5 +4,12 @@ QQ号码:935542673 ---- +------------------------------------------------------------------------------------------------------------------------------ +**代码库结构说明:** + +Blog 目录为本人在社群中所编写的所有博客,可在 README.md 中直接点击某篇查看 + +Coding 目录为本人在社群中所编写的所有代码,可在 README.md 中直接点击查看 + +Datum 目录为本人在社群中产出的所有资料 \ No newline at end of file From d47df58e274e83e025a33dd0250fc9afc18ac689 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:17:26 +0800 Subject: [PATCH 165/432] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=93=E5=BA=93?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group18/935542673/Coding/{ => 20170219}/.classpath | 0 group18/935542673/Coding/{ => 20170219}/.gitignore | 0 group18/935542673/Coding/{ => 20170219}/.project | 0 .../{src/com/ikook/basic_data_structure => 20170219}/README.md | 0 .../junit/com/ikook/basic_data_structure/MyArrayListTest.java | 0 .../com/ikook/basic_data_structure/MyBinarySearchTreeTest.java | 0 .../junit/com/ikook/basic_data_structure/MyLinkedListTest.java | 0 .../junit/com/ikook/basic_data_structure/MyQueueTest.java | 0 .../junit/com/ikook/basic_data_structure/MyStackTest.java | 0 .../src/com/ikook/basic_data_structure/MyArrayList.java | 0 .../src/com/ikook/basic_data_structure/MyBinarySearchTree.java | 0 .../src/com/ikook/basic_data_structure/MyIterator.java | 0 .../src/com/ikook/basic_data_structure/MyLinkedList.java | 0 .../{ => 20170219}/src/com/ikook/basic_data_structure/MyList.java | 0 .../src/com/ikook/basic_data_structure/MyQueue.java | 0 .../src/com/ikook/basic_data_structure/MyStack.java | 0 16 files changed, 0 insertions(+), 0 deletions(-) rename group18/935542673/Coding/{ => 20170219}/.classpath (100%) rename group18/935542673/Coding/{ => 20170219}/.gitignore (100%) rename group18/935542673/Coding/{ => 20170219}/.project (100%) rename group18/935542673/Coding/{src/com/ikook/basic_data_structure => 20170219}/README.md (100%) rename group18/935542673/Coding/{ => 20170219}/junit/com/ikook/basic_data_structure/MyArrayListTest.java (100%) rename group18/935542673/Coding/{ => 20170219}/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java (100%) rename group18/935542673/Coding/{ => 20170219}/junit/com/ikook/basic_data_structure/MyLinkedListTest.java (100%) rename group18/935542673/Coding/{ => 20170219}/junit/com/ikook/basic_data_structure/MyQueueTest.java (100%) rename group18/935542673/Coding/{ => 20170219}/junit/com/ikook/basic_data_structure/MyStackTest.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyArrayList.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyBinarySearchTree.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyIterator.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyLinkedList.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyList.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyQueue.java (100%) rename group18/935542673/Coding/{ => 20170219}/src/com/ikook/basic_data_structure/MyStack.java (100%) diff --git a/group18/935542673/Coding/.classpath b/group18/935542673/Coding/20170219/.classpath similarity index 100% rename from group18/935542673/Coding/.classpath rename to group18/935542673/Coding/20170219/.classpath diff --git a/group18/935542673/Coding/.gitignore b/group18/935542673/Coding/20170219/.gitignore similarity index 100% rename from group18/935542673/Coding/.gitignore rename to group18/935542673/Coding/20170219/.gitignore diff --git a/group18/935542673/Coding/.project b/group18/935542673/Coding/20170219/.project similarity index 100% rename from group18/935542673/Coding/.project rename to group18/935542673/Coding/20170219/.project diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md b/group18/935542673/Coding/20170219/README.md similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md rename to group18/935542673/Coding/20170219/README.md diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java From bb8ef5199f37e2acfb0708c434845ef543ab739e Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:24:03 +0800 Subject: [PATCH 166/432] Update README.md --- group18/935542673/Coding/20170219/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/group18/935542673/Coding/20170219/README.md b/group18/935542673/Coding/20170219/README.md index 6737563535..638aeb0395 100644 --- a/group18/935542673/Coding/20170219/README.md +++ b/group18/935542673/Coding/20170219/README.md @@ -1,5 +1,7 @@ ## 2017编程提高社群作业:实现基本的数据结构(2017.2.19) +#### [所有基本数据结构实现类及接口](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure) + 1. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) 1.1 ArrayList实现了以下方法: @@ -76,8 +78,6 @@ ``` ​ -3. 对应以上类做了单元测试 + #### [所有基本数据结构测试类](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure) 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 - - 点击查看: [unit test](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/junit/com/ikook/basic_data_structure) \ No newline at end of file From 2a0d8e06dca5a150d3bc1d7df61936faad3f2878 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:25:15 +0800 Subject: [PATCH 167/432] Update README.md --- group18/935542673/Coding/20170219/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/group18/935542673/Coding/20170219/README.md b/group18/935542673/Coding/20170219/README.md index 638aeb0395..a60bf43b0e 100644 --- a/group18/935542673/Coding/20170219/README.md +++ b/group18/935542673/Coding/20170219/README.md @@ -76,8 +76,6 @@ (2)next():返回游标当前位置的元素并将游标移动到下一个位置; (3)remove():删除游标左边的元素,在执行完 next 之后该操作只能执行一次。 ``` - ​ - #### [所有基本数据结构测试类](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure) 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 From 5e6d88647373a546659c528045286c77b1760d5a Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:29:18 +0800 Subject: [PATCH 168/432] Update README.md --- group18/935542673/Coding/20170219/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/group18/935542673/Coding/20170219/README.md b/group18/935542673/Coding/20170219/README.md index a60bf43b0e..a001a83d24 100644 --- a/group18/935542673/Coding/20170219/README.md +++ b/group18/935542673/Coding/20170219/README.md @@ -1,6 +1,6 @@ ## 2017编程提高社群作业:实现基本的数据结构(2017.2.19) -#### [所有基本数据结构实现类及接口](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure) +#### [所有基本数据结构实现类及接口](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure) 1. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) @@ -76,6 +76,8 @@ (2)next():返回游标当前位置的元素并将游标移动到下一个位置; (3)remove():删除游标左边的元素,在执行完 next 之后该操作只能执行一次。 ``` + ​ + #### [所有基本数据结构测试类](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure) 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 From 45c996bcf7bcf7eadb5d664c886ec95cba815899 Mon Sep 17 00:00:00 2001 From: xdx54321 <20409287@qq.com> Date: Sun, 26 Feb 2017 01:29:57 +0800 Subject: [PATCH 169/432] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=98=9F=E5=88=97=20?= =?UTF-8?q?=E6=A0=88=20=E5=92=8C=20ArrayList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/coding2017.iml | 25 + .idea/compiler.xml | 22 + .idea/copyright/profiles_settings.xml | 3 + .idea/encodings.xml | 8 + .idea/inspectionProfiles/Project_Default.xml | 36 + .../inspectionProfiles/profiles_settings.xml | 7 + .idea/misc.xml | 17 + .idea/modules.xml | 8 + .idea/uiDesigner.xml | 124 ++ .idea/vcs.xml | 6 + .idea/workspace.xml | 1617 +++++++++++++++++ .../20409287/git\345\221\275\344\273\244.txt" | 14 +- .../java/xdx/homework/first/ArrayList.java | 214 +++ .../main/java/xdx/homework/first/Hello.java | 15 + .../java/xdx/homework/first/Iterator.java | 11 + .../java/xdx/homework/first/LinkedList.java | 9 + .../main/java/xdx/homework/first/List.java | 86 + .../main/java/xdx/homework/first/Queue.java | 97 + .../xdx/homework/first/SortBinaryTree.java | 7 + .../main/java/xdx/homework/first/Stack.java | 81 + .../xdx/homework/first/ArrayListTest.java | 177 ++ .../test/xdx/homework/first/QueueTest.java | 141 ++ .../test/xdx/homework/first/StackTest.java | 126 ++ 23 files changed, 2844 insertions(+), 7 deletions(-) create mode 100644 .idea/coding2017.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 group07/20409287/src/main/java/xdx/homework/first/ArrayList.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/Hello.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/Iterator.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/LinkedList.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/List.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/Queue.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/SortBinaryTree.java create mode 100644 group07/20409287/src/main/java/xdx/homework/first/Stack.java create mode 100644 group07/20409287/src/test/xdx/homework/first/ArrayListTest.java create mode 100644 group07/20409287/src/test/xdx/homework/first/QueueTest.java create mode 100644 group07/20409287/src/test/xdx/homework/first/StackTest.java diff --git a/.idea/coding2017.iml b/.idea/coding2017.iml new file mode 100644 index 0000000000..459d465d9a --- /dev/null +++ b/.idea/coding2017.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000..f4b0ad32b0 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000..f4be559210 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..3b312839bf --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..3eb9c7edaa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..4bb52afa53 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000..2be72671b0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1617 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488025326268 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + coding2017 + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/group07/20409287/git\345\221\275\344\273\244.txt" "b/group07/20409287/git\345\221\275\344\273\244.txt" index 8bcf2ffa0f..bd0bffaaef 100644 --- "a/group07/20409287/git\345\221\275\344\273\244.txt" +++ "b/group07/20409287/git\345\221\275\344\273\244.txt" @@ -1,11 +1,11 @@ -��װgit gui��������git bash���� -1.��¡ +安装git gui软件,打开git bash操作 +1.克隆 git clone git@github.com:leijing1992/coding2017.git -2.�����޸ĵ��ļ� +2.添加修改的文件 git add -A -3.�ύ���ݴ��� -git commit -m "�ύ***����" -4.����master +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master git pull origin master -5.�ύ��master +5.提交到master git push origin master \ No newline at end of file diff --git a/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java b/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java new file mode 100644 index 0000000000..b7ffc73d07 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java @@ -0,0 +1,214 @@ +package xdx.homework.first; + + +public class ArrayList implements List { + + private Object[] elements; + + // 列表长度,默认10个元素 + private int size = 10; + + // 最后一个元素的位置 + private int position = -1; + + public ArrayList() { + elements = new Object[size]; + } + + public ArrayList(final int capacity) { + if (capacity <= 0) + throw new RuntimeException("列表长度不可小于等于0!"); + elements = new Object[capacity]; + } + + /** + * 添加元素 + * + * @param e 要添加的元素 + * @return + */ + @Override + public boolean add(E e) { + + if (++position > size - 1) { + grow(); + } else { + elements[position] = e; + } + return true; + } + + /** + * 删除指定索引的元素 + * + * @param index 索引下标 + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + + if (index < 0 || index > position || this.isEmpty()) + throw new IndexOutOfBoundsException("要删除的索引不正确!"); + + E returnElement = (E) elements[index]; + elements[index] = null; + System.arraycopy(elements, index + 1, elements, index, position + 1 - index); + position--; + return returnElement; + } + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + @Override + public boolean remove(E e) { + + if (!this.contains(e)) return false; + int removeIndex = 0; + for(int i = 0; i < this.position; i++) { + if(elements[i].equals(e)) { + removeIndex = i; + break; + } + } + remove(removeIndex); + return true; + } + + /** + * 返回列表长度 + * + * @return + */ + @Override + public int size() { + return position + 1; + } + + /** + * 判断列表是否为空 + * + * @return + */ + @Override + public boolean isEmpty() { + return position == -1; + } + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + if (index > position) return null; + return (E)elements[index]; + } + + /** + * 重置某个索引的元素 + * + * @param index + * @param o + * @return + */ + @Override + public boolean set(int index, Object o) { + if (index > position) return false; + elements[index] = null; + elements[index] = o; + return true; + } + + /** + * 判断是否包含某个元素 + * + * @param o + * @return + */ + @Override + public boolean contains(Object o) { + + if(this.isEmpty()) return false; + for(int i = 0; i <= position; i++) { + if (elements[i].equals(o)) + return true; + } + return false; + } + + /** + * 清空列表 + */ + @Override + public void clear() { + for(int i = 0; i <= this.size(); i++) { + elements[i] = null; + } + position = -1; + } + + /** + * 取得迭代器 + * + * @return + */ + @Override + public Iterator iterator() { + return new Itr(); + } + + /** + * 数组增长 + */ + private void grow() { + Object[] newElements = new Object[size << 2]; + System.arraycopy(elements, 0, elements, 0, this.size); + elements = null; + elements = newElements; + } + + private class Itr implements Iterator { + + private int itrIndex = 0; + + @Override + public boolean hasNext() { + return get(itrIndex) != null; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + return (E) elements[itrIndex++]; + } + + @Override + public void remove() { + ArrayList.this.remove(itrIndex); + } + } + + @Override + public String toString() { + + if(this.isEmpty()) { + return "[]"; + } + StringBuilder strList = new StringBuilder("["); + for(int i = 0; i < this.size(); i++) { + strList.append(elements[i].toString()).append(","); + } + strList.deleteCharAt(strList.length() - 1); + strList.append("]"); + return strList.toString(); + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Hello.java b/group07/20409287/src/main/java/xdx/homework/first/Hello.java new file mode 100644 index 0000000000..1abb23045e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Hello.java @@ -0,0 +1,15 @@ +package xdx.homework.first; + +import java.util.List; + +/** + * @Description: Hello World! + * @author: xdx + * @date: 2017年2月25日 上午11:37:58 + */ +public class Hello { + + public static void main(String[] args) { + System.out.println("Hello WOrl"); + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Iterator.java b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java new file mode 100644 index 0000000000..b90db66d5d --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java @@ -0,0 +1,11 @@ +package xdx.homework.first; + + +public interface Iterator { + + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java new file mode 100644 index 0000000000..b541b2069a --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java @@ -0,0 +1,9 @@ +package xdx.homework.first; + +/** + * @Description: + * @author: {User} + * @date: {Date} + */ +public class LinkedList { +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/List.java b/group07/20409287/src/main/java/xdx/homework/first/List.java new file mode 100644 index 0000000000..7c6f1c4e52 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/List.java @@ -0,0 +1,86 @@ +package xdx.homework.first; + +/** + * @param + * @Description: 定义一组操作有序列表的接口 + * @author: xdx + * @date: 2017年2月21日 下午8:53:52 + */ +public interface List { + + /** + * 添加元素 + * + * @param e + * @return + */ + boolean add(E e); + + /** + * 删除指定索引的元素 + * + * @param int 索引下标 + * @return + */ + E remove(int index); + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + boolean remove(E e); + + /** + * 返回列表长度 + * + * @return + */ + int size(); + + /** + * 判断列表是否为空 + * + * @return + */ + boolean isEmpty(); + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 重置某个索引的元素 + * + * @param index + * @param e + * @return + */ + boolean set(int index, E e); + + /** + * 判断是否包含某个元素 + * + * @param e + * @return + */ + boolean contains(E e); + + /** + * 清空列表 + */ + void clear(); + + /** + * 取得迭代器 + * + * @return + */ + Iterator iterator(); + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Queue.java b/group07/20409287/src/main/java/xdx/homework/first/Queue.java new file mode 100644 index 0000000000..5d98cff312 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Queue.java @@ -0,0 +1,97 @@ +package xdx.homework.first; + +/** + * @Description: 链式队列 + */ +public class Queue { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + + } + + private Node front; // 队头 + + private Node rear; // 队尾 + + private int length; // 队长 + + public void enQueue(E data) { + + if(this.front == null) { + this.front = new Node(data); + this.rear = this.front; + } else { + Node newNode = new Node(data); + this.rear.next = newNode; + this.rear = newNode; + } + length++; + } + + public E deQueue() { + + if (isEmpty()) { + return null; + } + Node oldFront = this.front; + this.front = this.front.next; + length--; + return oldFront.data; + } + + public int length() { + return length; + } + + public boolean isEmpty() { + return this.front == this.rear; + } + + public void clear() { + this.length = 0; + this.front = null; + this.rear = null; + } + + public E getFront() { + + if (this.isEmpty()) { + return null; + } + return this.front.data; + } + + public E getRear() { + + if(this.isEmpty()) return null; + return this.rear.data; + } + + public String toString() { + + if (this.length == 0) return "[]"; + + Node node = this.front; + StringBuilder queueToString = new StringBuilder("["); + while (node.next != null) { + queueToString.append(node.data.toString()).append(","); + node = node.next; + } + queueToString.append(node.data.toString()).append("]"); + return queueToString.toString(); + } + + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/SortBinaryTree.java b/group07/20409287/src/main/java/xdx/homework/first/SortBinaryTree.java new file mode 100644 index 0000000000..b48d0cf6a5 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/SortBinaryTree.java @@ -0,0 +1,7 @@ +package xdx.homework.first; + +/** + * @Description: 顺序存储的二叉树实现 + */ +public class SortBinaryTree { +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Stack.java b/group07/20409287/src/main/java/xdx/homework/first/Stack.java new file mode 100644 index 0000000000..d898ffeb6e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Stack.java @@ -0,0 +1,81 @@ +package xdx.homework.first; + +/** + * @Description: + * @author: {User} + * @date: {Date} + */ +public class Stack { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + } + + private int size; + + private Node top; + + public void push(E e) { + if(!isEmpty()) { + Node newNode = new Node(e); + newNode.next = top; + top = newNode; + } else { + top = new Node(e); + } + size++; + } + + public E pop() { + if(isEmpty()) throw new RuntimeException("空栈!"); + Node popNode = top; + top = top.next; + return popNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public E peek() { + if(isEmpty()) throw new RuntimeException("空栈!"); + return top.data; + } + + public int size() { + return size; + } + + public void clear() { + top = null; + size = 0; + } + + @Override + public String toString() { + + if(isEmpty()) return "[]"; + StringBuilder stackToString = new StringBuilder("["); + Node itr = this.top; + while(itr != null) { + stackToString.append(itr.data.toString()).append(","); + itr = itr.next; + } + stackToString.deleteCharAt(stackToString.length() - 1).append("]"); + + return stackToString.toString(); + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java new file mode 100644 index 0000000000..2c71401873 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java @@ -0,0 +1,177 @@ +package xdx.homework.first; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ArrayList Tester. + * + * @author xdx + * @version 1.0 + * @since
2月25日, 2017
+ */ +public class ArrayListTest { + + private List defaultTestList; + + @Before + public void before() throws Exception { + + defaultTestList = new ArrayList<>(); + Assert.assertTrue(defaultTestList.add("《三国演义》")); + Assert.assertTrue(defaultTestList.add("《红楼梦》")); + Assert.assertTrue(defaultTestList.add("《西游记》")); + Assert.assertTrue(defaultTestList.add("《水浒传》")); + } + + @After + public void after() throws Exception { + } + + /** + * Method: add(E e) + */ + @Test + public void testAdd() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(1)); + Assert.assertTrue(testList.add(2)); + Assert.assertTrue(testList.add(3)); + System.out.println(testList.toString()); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemoveIndex() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(4)); + Assert.assertTrue(testReomoveList.add(5)); + Assert.assertTrue(testReomoveList.add(6)); + System.out.println("删除前: " + testReomoveList); + Integer delElement = testReomoveList.get(0); + Assert.assertTrue(testReomoveList.remove(0).equals(delElement)); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: remove(E e) + */ + @Test + public void testRemoveE() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(7)); + Assert.assertTrue(testReomoveList.add(8)); + Assert.assertTrue(testReomoveList.add(9)); + System.out.println("删除前: " + testReomoveList); + Assert.assertTrue(testReomoveList.remove(new Integer(8))); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + int size = defaultTestList.size(); + Assert.assertEquals(4, size); + System.out.println("defaultTest内容:" + defaultTestList); + System.out.println("defaultTest长度:" + size); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertFalse(defaultTestList.isEmpty()); + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.isEmpty()); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + Assert.assertTrue("《三国演义》".equals(defaultTestList.get(0))); + Assert.assertFalse("《西游记》".equals(defaultTestList.get(0))); + } + + /** + * Method: set(int index, Object o) + */ + @Test + public void testSet() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("设置前: " + testList); + Assert.assertTrue(testList.set(0, 10)); + System.out.println("设置后: " + testList); + } + + /** + * Method: contains(Object o) + */ + @Test + public void testContains() throws Exception { + Assert.assertTrue(defaultTestList.contains("《红楼梦》")); + Assert.assertFalse(defaultTestList.contains("《聊斋》")); + } + + /** + * Method: clear() + */ + @Test + public void testClear() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("清除前: " + testList); + testList.clear(); + Assert.assertTrue(testList.isEmpty()); + System.out.println("清除后: " + testList); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + Iterator iterator = testList.iterator(); + Assert.assertNotNull(iterator); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + List testListByDel = new ArrayList<>(); + Assert.assertTrue(testListByDel.add("张三")); + Assert.assertTrue(testListByDel.add("李四")); + Assert.assertTrue(testListByDel.add("王五")); + Iterator iteratorByDel = testListByDel.iterator(); + while(iteratorByDel.hasNext()) { + iteratorByDel.remove(); + } + Assert.assertTrue(testListByDel.isEmpty()); + + } + + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/QueueTest.java b/group07/20409287/src/test/xdx/homework/first/QueueTest.java new file mode 100644 index 0000000000..6e56e3b8b1 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/QueueTest.java @@ -0,0 +1,141 @@ +package xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Queue; + +/** + * Queue Tester. + * + * @author + * @since
二月 25, 2017
+ * @version 1.0 + */ +public class QueueTest { + + private Queue defaultQueue; + + @Before + public void before() throws Exception { + + defaultQueue = new Queue<>(); + defaultQueue.enQueue("赵"); + defaultQueue.enQueue("钱"); + defaultQueue.enQueue("孙"); + defaultQueue.enQueue("李"); + defaultQueue.enQueue("周"); + defaultQueue.enQueue("吴"); + defaultQueue.enQueue("郑"); + defaultQueue.enQueue("王"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: enQueue(E data) + * + */ + @Test + public void testEnQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println(testQueue); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println("删除前:" + testQueue); + System.out.println("删除:" + testQueue.deQueue()); + System.out.println("删除后:" + testQueue); + } + + /** + * + * Method: length() + * + */ + @Test + public void testLength() throws Exception { + Assert.assertEquals(8, defaultQueue.length()); + System.out.println("defaultQueue长度:" + defaultQueue.length()); + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertTrue(!defaultQueue.isEmpty()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + + testQueue.clear(); + Assert.assertTrue(testQueue.isEmpty()); + } + + /** + * + * Method: getFront() + * + */ + @Test + public void testGetFront() throws Exception { + Assert.assertEquals("赵", defaultQueue.getFront()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: getRear() + * + */ + @Test + public void testGetRear() throws Exception { + Assert.assertEquals("王", defaultQueue.getRear()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: toString() + * + */ + @Test + public void testToString() throws Exception { + System.out.println("defaultQueue内容:" + defaultQueue.toString()); + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/StackTest.java b/group07/20409287/src/test/xdx/homework/first/StackTest.java new file mode 100644 index 0000000000..bfc6b2c8f7 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/StackTest.java @@ -0,0 +1,126 @@ +package test.xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Stack; + +/** + * Stack Tester. + * + * @version 1.0 + */ +public class StackTest { + + private Stack defaultStack; + + @Before + public void before() throws Exception { + + defaultStack = new Stack<>(); + defaultStack.push("孙悟空"); + defaultStack.push("唐僧"); + defaultStack.push("猪八戒"); + defaultStack.push("沙僧"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(E e) + * + */ + @Test + public void testPush() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + Assert.assertTrue(testStack.isEmpty()); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + Assert.assertEquals("沙僧", defaultStack.peek()); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + Assert.assertEquals(4, defaultStack.size()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println("清空前:" + testStack); + testStack.clear(); + System.out.println("清空后:" + testStack); + Assert.assertTrue(testStack.isEmpty()); + + + } + + +} From f2cd05ad2f10b2c71de5c4a3420a51d86fac0b74 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:31:49 +0800 Subject: [PATCH 170/432] Update README.md --- group18/935542673/Coding/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group18/935542673/Coding/README.md b/group18/935542673/Coding/README.md index 50eeea3928..06225f9065 100644 --- a/group18/935542673/Coding/README.md +++ b/group18/935542673/Coding/README.md @@ -1,3 +1,3 @@ ## 2017编程提高社群Coding -2017.2.24 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/src/com/ikook/basic_data_structure) \ No newline at end of file +2017.2.24 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219) \ No newline at end of file From d003ef57d55e50ce7aeff68e0592ce10f12a52de Mon Sep 17 00:00:00 2001 From: xdx54321 <20409287@qq.com> Date: Sun, 26 Feb 2017 01:33:30 +0800 Subject: [PATCH 171/432] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 168 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 149 insertions(+), 19 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2be72671b0..1a4c6f97dc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,21 +2,7 @@ - - - - - - - - - - - - - - - + @@ -263,6 +249,42 @@ @@ -1079,11 +1209,11 @@ - - + @@ -1096,7 +1226,7 @@ - + From 20367d354fc9fcc5c670894288ef7e26bc77799a Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:33:31 +0800 Subject: [PATCH 172/432] Update README.md --- group18/935542673/Coding/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group18/935542673/Coding/README.md b/group18/935542673/Coding/README.md index 06225f9065..77e4706a07 100644 --- a/group18/935542673/Coding/README.md +++ b/group18/935542673/Coding/README.md @@ -1,3 +1,3 @@ ## 2017编程提高社群Coding -2017.2.24 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219) \ No newline at end of file +2017.2.19 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219) \ No newline at end of file From d78073434dde21bf995f953528746d2fa5556d26 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:43:49 +0800 Subject: [PATCH 173/432] Update --- ...\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" index 8de7f5c88c..d2a21619bf 100644 --- "a/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" +++ "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -1,6 +1,6 @@ ## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.25) -原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-hdd-instruct-detailed-annotation/) +原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-detailed/) > 本人对于CPU、内存、硬盘以及指令等一系列计算机核心组件理解甚浅。并且对其也不是很来感,不过身为一名软件专业学生以及未来的程序猿,还是硬着头皮研究研究。以下是对其学习的一个总结吧算是,有什么不正确的地方还请指出,不喜勿喷。 From de5bb1b050944e92592708fb8767b916ddc29c73 Mon Sep 17 00:00:00 2001 From: xiaoqin0000 Date: Sun, 26 Feb 2017 01:45:30 +0800 Subject: [PATCH 174/432] add ArrayList and LinkedList --- .../.idea/compiler.xml | 23 + .../.idea/description.html | 1 + .../.idea/encodings.xml | 5 + .../.idea/misc.xml | 12 + .../.idea/modules.xml | 8 + .../.idea/uiDesigner.xml | 124 +++ .../.idea/vcs.xml | 7 + .../.idea/workspace.xml | 786 ++++++++++++++++++ .../job_2017_02_19_data_structure.iml | 12 + .../com/java/xiaoqin/impl/ArrayListImpl.java | 112 +++ .../com/java/xiaoqin/impl/LinkedListImpl.java | 146 ++++ .../java/xiaoqin/interfaces/IIterator.java | 19 + .../com/java/xiaoqin/interfaces/IList.java | 47 ++ 13 files changed, 1302 insertions(+) create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml new file mode 100644 index 0000000000..217af471a9 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html new file mode 100644 index 0000000000..db5f129556 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml new file mode 100644 index 0000000000..e206d70d85 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml new file mode 100644 index 0000000000..91362b9dce --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml new file mode 100644 index 0000000000..46ad2c6b17 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml new file mode 100644 index 0000000000..def6a6a184 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml new file mode 100644 index 0000000000..1dc6e0b616 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml @@ -0,0 +1,786 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + package com.java.xiaoqin.impl; import com.java.xiaoqin.interfaces.IList; /** * Created by xiaoqin on 17-2-25. */ public class ArrayListImpl<T> implements IList<T> { private static final int DEFAULT_INIT_SIZE = 20; private T[] data; private int size = 0; public ArrayListImpl(){ this(DEFAULT_INIT_SIZE); } public ArrayListImpl(int capacity){ if (capacity < 0){ throw new IllegalArgumentException("无效的capacity:" + capacity); } if (0 == capacity){ capacity = DEFAULT_INIT_SIZE; } data = (T[]) new Object[capacity]; } @Override public void add(T t) { } @Override public void add(int index, T t) { } @Override public T get(int index) { return null; } @Override public T remove(int index) { return null; } @Override public int size() { return 0; } } + gro + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488008946620 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 36 + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 53 + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 84 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml new file mode 100644 index 0000000000..d5c0743275 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java new file mode 100644 index 0000000000..c96d0b4f30 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java @@ -0,0 +1,112 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; + +/** + * Created by xiaoqin on 17-2-25. + */ +public class ArrayListImpl implements IList { + + private static final int DEFAULT_INIT_SIZE = 20; + + private T[] data; + + private int size = 0; + + public ArrayListImpl() { + this(DEFAULT_INIT_SIZE); + } + + public ArrayListImpl(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException("无效的capacity:" + capacity); + } + if (0 == capacity) { + capacity = DEFAULT_INIT_SIZE; + } + data = (T[]) new Object[capacity]; + } + + @Override + public void add(T t) { + group(size); + data[size++] = t; + } + + @Override + public void add(int index, T t) { + if (index < 0) { + throw new IllegalArgumentException("index < 0,index:" + index); + } + if (index > size) { + throw new IndexOutOfBoundsException("index >= size。index:" + index + "\tsize:" + size); + } + group(size); + T[] temp = (T[]) new Object[size - index]; + System.arraycopy(data, index, temp, 0, temp.length); + data[index] = t; + size++; + System.arraycopy(temp, 0, data, index + 1, temp.length); + } + + @Override + public T get(int index) { + if (index < data.length) { + return data[index]; + } else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + @Override + public T remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException("index invalid!!!index:" + index); + } + T[] temp = (T[]) new Object[size - index - 1]; + System.arraycopy(data, index + 1, temp, 0, temp.length); + T result = data[index]; + System.arraycopy(temp, 0, data, index, temp.length); + data[--size] = null; + return result; + } + + @Override + public int size() { + return size; + } + + @Override + public IIterator iterator() { + return new ArrayIteratorImpl<>(); + } + + private void group(int minSize) { + if (minSize >= data.length) { + T[] temp = (T[]) new Object[size]; + System.arraycopy(data, 0, temp, 0, size); + int groupSize = (size >> 1) + size; + data = (T[]) new Object[Math.max(groupSize, minSize)]; + System.arraycopy(temp, 0, data, 0, size); + } + } + + private class ArrayIteratorImpl implements IIterator { + + private int index = 0; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index out"); + } + return (T) data[index++]; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java new file mode 100644 index 0000000000..90f8c326af --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java @@ -0,0 +1,146 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; + +/** + * Created by xiaoqin on 17-2-26. + */ +public class LinkedListImpl implements IList { + + private Node head; + private Node last; + private int size = 0; + + public LinkedListImpl() { + } + + @Override + public void add(T t) { + if (size == 0) { + head = new Node<>(); + head.data = t; + head.index = size++; + last = head; + } else { + last.right = new Node<>(); + last.right.left = last; + last = last.right; + last.data = t; + last.index = size++; + } + } + + @Override + public void add(int index, T t) { + Node node = findNodeByIndex(index); + if (node.index == 0) { + node.left = new Node<>(); + node.left.right = node; + node.left.index = 0; + node.left.data = t; + head = node.left; + } else { + node.left.right = new Node<>(); + node.left.right.index = node.index; + node.left.right.right = node; + node.left.right.data = t; + node.left = node.left.right; + } + while (node.right != null) { + node.index++; + node = node.right; + } + size++; + } + + private Node findNodeByIndex(int index) { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index:" + index); + } + Node resultNode = null; + Node origin = null; + int half = size >> 1; + if (index > half) { + origin = last; + while (origin.index != index) { + origin = origin.left; + } + } else { + origin = head; + while (origin.index != index) { + origin = origin.right; + } + } + return origin; + } + + @Override + public T get(int index) { + return findNodeByIndex(index).data; + } + + @Override + public T remove(int index) { + Node node = findNodeByIndex(index); + if (null != node.left) { + node.left.right = node.right; + } else { + node.right.left = null; + head = node.right; + } + if (null != node.right) { + node.right.left = node.left; + while (node.right == null) { + node.right.index--; + } + } else { + node.left.right = null; + last = node.left; + } + size--; + return node.data; + } + + @Override + public int size() { + return size; + } + + @Override + public IIterator iterator() { + return new LinkedIteratorImpl<>(); + } + + private static class Node { + private T data = null; + private Node left = null; + private Node right = null; + private int index = 0; + } + + private class LinkedIteratorImpl implements IIterator { + + private int index = 0; + private Node next; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (0 == index) { + next = (Node) head; + } + if (null == next) { + throw new ArrayIndexOutOfBoundsException("next is null"); + } + Node result = next; + next = next.right; + index++; + return result.data; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java new file mode 100644 index 0000000000..bd83985af4 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java @@ -0,0 +1,19 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-25. + */ +public interface IIterator { + + /** + * 是否有下一个 + * @return + */ + boolean hasNext(); + + /** + * 取出下一个的元素 + * @return + */ + T next(); +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java new file mode 100644 index 0000000000..1a7a01cbca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java @@ -0,0 +1,47 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-19. + */ +public interface IList { + + /** + * 添加元素 + * @param t + */ + void add(T t); + + /** + * 添加元素在第几个 + * @param index + * @param t + */ + void add(int index,T t); + + /** + * 获取第index个的元素 + * @param index + * @return + */ + T get(int index); + + /** + * 移除第index个的元素 + * @param index + * @return + */ + T remove(int index); + + /** + * 返回List的大小 + * @return + */ + int size(); + + /** + * 返回迭代器 + * @return + */ + IIterator iterator(); + +} From 202a678c4bf58838819d8a386451227c5ded19d5 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:46:38 +0800 Subject: [PATCH 175/432] Update README.md --- group18/935542673/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/group18/935542673/README.md b/group18/935542673/README.md index 58e32f7595..d98a632c03 100644 --- a/group18/935542673/README.md +++ b/group18/935542673/README.md @@ -8,8 +8,8 @@ QQ号码:935542673 **代码库结构说明:** -Blog 目录为本人在社群中所编写的所有博客,可在 README.md 中直接点击某篇查看 +[Blog](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Blog) 目录为本人在社群中所编写的所有博客,可在 README.md 中直接点击某篇查看 -Coding 目录为本人在社群中所编写的所有代码,可在 README.md 中直接点击查看 +[Coding](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding) 目录为本人在社群中所编写的所有代码,可在 README.md 中直接点击查看 -Datum 目录为本人在社群中产出的所有资料 \ No newline at end of file +[Datum](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Datum) 目录为本人在社群中产出的所有资料 \ No newline at end of file From 649038500e4120196fdf8be8dcfb3b9dd1a09059 Mon Sep 17 00:00:00 2001 From: xiaoqin0000 Date: Sun, 26 Feb 2017 01:48:03 +0800 Subject: [PATCH 176/432] add .gitignore --- .../job_2017_02_19_data_structure/.gitignore | 6 ++ .../.idea/workspace.xml | 65 ++++++++----------- 2 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore new file mode 100644 index 0000000000..a2b48b352f --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore @@ -0,0 +1,6 @@ +/vendor +/node_modules +Homestead.yaml +Homestead.json +.env +.idea \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml index 1dc6e0b616..2b612eeea0 100644 --- a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml @@ -16,18 +16,6 @@ - - - - - - - - - - - - @@ -48,6 +36,16 @@ + + + + + + + + + + @@ -88,8 +86,9 @@ @@ -131,20 +130,6 @@ - @@ -609,10 +594,10 @@ - + - + @@ -754,6 +739,14 @@ + + + + + + + + @@ -772,13 +765,11 @@ - + - - - - - + + + From c4b1c2a19403bac5542976ef83f59ef3dc916e96 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:52:24 +0800 Subject: [PATCH 177/432] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=9D=9E=E6=9C=AC?= =?UTF-8?q?=E7=BB=84=E6=97=A0=E7=94=A8=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/group01.md | 1 - group02/group02.md | 1 - group03/group03.md | 1 - group04/group04.md | 1 - group05/group05.md | 1 - group06/group06.md | 1 - group07/group07.md | 1 - group08/group08.md | 1 - group09/group09.md | 1 - group10/group10.md | 1 - group11/group11.md | 1 - group12/group12.md | 1 - group13/group13.md | 1 - group14/group14.md | 1 - group15/group15.md | 1 - group16/group16.md | 1 - group17/group17.md | 1 - group19/group19.md | 1 - group20/group20.md | 1 - group21/group21.md | 1 - 20 files changed, 20 deletions(-) delete mode 100644 group01/group01.md delete mode 100644 group02/group02.md delete mode 100644 group03/group03.md delete mode 100644 group04/group04.md delete mode 100644 group05/group05.md delete mode 100644 group06/group06.md delete mode 100644 group07/group07.md delete mode 100644 group08/group08.md delete mode 100644 group09/group09.md delete mode 100644 group10/group10.md delete mode 100644 group11/group11.md delete mode 100644 group12/group12.md delete mode 100644 group13/group13.md delete mode 100644 group14/group14.md delete mode 100644 group15/group15.md delete mode 100644 group16/group16.md delete mode 100644 group17/group17.md delete mode 100644 group19/group19.md delete mode 100644 group20/group20.md delete mode 100644 group21/group21.md diff --git a/group01/group01.md b/group01/group01.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group01/group01.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group02/group02.md b/group02/group02.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group02/group02.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group03/group03.md b/group03/group03.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group03/group03.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group04/group04.md b/group04/group04.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group04/group04.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group05/group05.md b/group05/group05.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group05/group05.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group06/group06.md b/group06/group06.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group06/group06.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group07/group07.md b/group07/group07.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group07/group07.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group08/group08.md b/group08/group08.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group08/group08.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group09/group09.md b/group09/group09.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group09/group09.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group10/group10.md b/group10/group10.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group10/group10.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group11/group11.md b/group11/group11.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group11/group11.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group12/group12.md b/group12/group12.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group12/group12.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group13/group13.md b/group13/group13.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group13/group13.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group14/group14.md b/group14/group14.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group14/group14.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group15/group15.md b/group15/group15.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group15/group15.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group16/group16.md b/group16/group16.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group16/group16.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group17/group17.md b/group17/group17.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group17/group17.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group19/group19.md b/group19/group19.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group19/group19.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group20/group20.md b/group20/group20.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group20/group20.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/group21/group21.md b/group21/group21.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group21/group21.md +++ /dev/null @@ -1 +0,0 @@ - From 7d53f328d41801a8ebd8c1798a4e4582993e327b Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 01:54:22 +0800 Subject: [PATCH 178/432] Add README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000000..a7d23c04cf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群 + +2017编程提高社群代码仓库所在地 \ No newline at end of file From c0fc22161850866430c7266a66a67b67d7786f4e Mon Sep 17 00:00:00 2001 From: lulijie Date: Sun, 26 Feb 2017 02:00:29 +0800 Subject: [PATCH 179/432] Merge remote-tracking branch 'origin/master' Conflicts: group18/542330964/.classpath --- group18/542330964/.classpath | 7 + group18/542330964/.gitignore | 1 + group18/542330964/.project | 17 ++ .../542330964/src/basicstruct/ArrayList.java | 80 ++++++++ .../src/basicstruct/BinaryTreeNode.java | 31 ++++ .../542330964/src/basicstruct/Iterator.java | 8 + .../542330964/src/basicstruct/LinkedList.java | 175 ++++++++++++++++++ group18/542330964/src/basicstruct/List.java | 13 ++ group18/542330964/src/basicstruct/Queue.java | 22 +++ group18/542330964/src/basicstruct/Stack.java | 24 +++ .../src/junittest/ArraylistTest.java | 52 ++++++ .../src/junittest/LinkedListTest.java | 65 +++++++ .../542330964/src/junittest/QueueTest.java | 33 ++++ .../542330964/src/junittest/StackTest.java | 39 ++++ 14 files changed, 567 insertions(+) create mode 100644 group18/542330964/.classpath create mode 100644 group18/542330964/.gitignore create mode 100644 group18/542330964/.project create mode 100644 group18/542330964/src/basicstruct/ArrayList.java create mode 100644 group18/542330964/src/basicstruct/BinaryTreeNode.java create mode 100644 group18/542330964/src/basicstruct/Iterator.java create mode 100644 group18/542330964/src/basicstruct/LinkedList.java create mode 100644 group18/542330964/src/basicstruct/List.java create mode 100644 group18/542330964/src/basicstruct/Queue.java create mode 100644 group18/542330964/src/basicstruct/Stack.java create mode 100644 group18/542330964/src/junittest/ArraylistTest.java create mode 100644 group18/542330964/src/junittest/LinkedListTest.java create mode 100644 group18/542330964/src/junittest/QueueTest.java create mode 100644 group18/542330964/src/junittest/StackTest.java diff --git a/group18/542330964/.classpath b/group18/542330964/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group18/542330964/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group18/542330964/.gitignore b/group18/542330964/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/542330964/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/542330964/.project b/group18/542330964/.project new file mode 100644 index 0000000000..3a99e1d0ad --- /dev/null +++ b/group18/542330964/.project @@ -0,0 +1,17 @@ + + + 542330964learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/542330964/src/basicstruct/ArrayList.java b/group18/542330964/src/basicstruct/ArrayList.java new file mode 100644 index 0000000000..b56c61c4d1 --- /dev/null +++ b/group18/542330964/src/basicstruct/ArrayList.java @@ -0,0 +1,80 @@ +package basicstruct; + + + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData ; + + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList() { + elementData=new Object [DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if(initialCapacity>=0){ + elementData=new Object[initialCapacity]; + }else { + throw new IllegalArgumentException("initialCapacity"+ + initialCapacity+"不能为负数"); + } + } + + public void add(Object o){ + ensureCapacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + if(index<0||index>size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index + 1,size - index); + elementData[index] = o; + size++; + } + + private void rangeCheck(int index) { + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + } + private void ensureCapacity() { + if(size == elementData.length) { + Object[] newArray = new Object[size * 2 + 1]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object movedValue = elementData[index]; + //被删除元素后的元素数目 + int numMoved = size - index - 1; + //后面有元素 + if (numMoved > 0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + //恰为最后一个元素 + size--; + elementData[size] = null; //垃圾回收 + return movedValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group18/542330964/src/basicstruct/BinaryTreeNode.java b/group18/542330964/src/basicstruct/BinaryTreeNode.java new file mode 100644 index 0000000000..5fba822d2f --- /dev/null +++ b/group18/542330964/src/basicstruct/BinaryTreeNode.java @@ -0,0 +1,31 @@ +package basicstruct; +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/group18/542330964/src/basicstruct/Iterator.java b/group18/542330964/src/basicstruct/Iterator.java new file mode 100644 index 0000000000..f7a094dd14 --- /dev/null +++ b/group18/542330964/src/basicstruct/Iterator.java @@ -0,0 +1,8 @@ +package basicstruct; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/LinkedList.java b/group18/542330964/src/basicstruct/LinkedList.java new file mode 100644 index 0000000000..b124e9f8b9 --- /dev/null +++ b/group18/542330964/src/basicstruct/LinkedList.java @@ -0,0 +1,175 @@ +package basicstruct; + +import java.util.NoSuchElementException; + + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size=0; + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index: "+index); + } + if (index == size) { + addLast(o); + } else { + Node temp = node(index); + final Node pred = temp.previous; + final Node newNode = new Node(o, temp, pred); + temp.previous = newNode; + if (pred == null){ + head = newNode; + } + else{ + pred.next = newNode; + } + size++; + } + } + + public Node node(int index) { + //二分法查找 + if (index < (size >> 1)) { + Node temp = head; + for (int i = 0; i < index; i++){ + temp = temp.next; + } + return temp; + } else { + Node temp = tail; + for (int i = size - 1; i > index; i--){ + temp = temp.previous; + } + return temp; + } + } + + public Object get(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return node(index).data; + } + + public Object remove(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return deleteElement(node(index)); + } + + private Object deleteElement(Node node) { + Object element = node.data; + Node next = node.next; + Node prev = node.previous; + if (prev == null) { + head = next; + }else{ + prev.next = next; + node.previous = null; + } + if(next == null) { + tail = prev; + }else { + next.previous = prev; + node.next = null; + } + node.data = null; + size--; + return element; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node h = head; + Node newNode = new Node(o, h, null); + head = newNode; + if (h == null){ + tail = newNode; + }else{ + h.previous = newNode; + } + size++; + } + + public void addLast(Object o){ + Node t = tail; + Node node = new Node(o, null, t); + tail = node; + if (t == null) { + head = node; + } else { + t.next = node; + } + size++; + } + + public Object removeFirst(){ + final Node h = head; + if (h == null) { + throw new NoSuchElementException("No such element"); + } + final Object element = h.data; + final Node next = h.next; + h.data = null; + h.next = null; + head = next; + if (next == null) { + tail = null; + } else { + next.previous = null; + } + size--; + return element; + } + + public Object removeLast(){ + Node t = tail; + if (t == null){ + throw new NoSuchElementException("No such element"); + } + final Object element = t.data; + final Node prev = t.previous; + t.data = null; + t.previous = null; + tail = prev; + if (prev == null) { + head = null; + } else { + prev.next = null; + } + size--; + return element; + } + public boolean isEmpty(){ + return size==0; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + Node previous; + public Node() { + super(); + } + public Node(Object data, Node next, Node previous) { + super(); + this.data = data; + this.next = next; + this.previous = previous; + } + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/List.java b/group18/542330964/src/basicstruct/List.java new file mode 100644 index 0000000000..abe8ec3613 --- /dev/null +++ b/group18/542330964/src/basicstruct/List.java @@ -0,0 +1,13 @@ +package basicstruct; + +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(); +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Queue.java b/group18/542330964/src/basicstruct/Queue.java new file mode 100644 index 0000000000..5cb8ff6f12 --- /dev/null +++ b/group18/542330964/src/basicstruct/Queue.java @@ -0,0 +1,22 @@ +package basicstruct; + +public class Queue { + + private LinkedList queue = new LinkedList(); + //进队列 + public void enQueue(Object o) { + queue.addLast(o); + } + //出队列 + public Object deQueue() { + return queue.removeFirst(); + } + + public int size() { + return queue.size(); + } + + public boolean isEmpty() { + return queue.isEmpty(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Stack.java b/group18/542330964/src/basicstruct/Stack.java new file mode 100644 index 0000000000..41f32af82f --- /dev/null +++ b/group18/542330964/src/basicstruct/Stack.java @@ -0,0 +1,24 @@ +package basicstruct; + +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(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/junittest/ArraylistTest.java b/group18/542330964/src/junittest/ArraylistTest.java new file mode 100644 index 0000000000..bfaa41a4b0 --- /dev/null +++ b/group18/542330964/src/junittest/ArraylistTest.java @@ -0,0 +1,52 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.ArrayList; + +public class ArraylistTest { + + + ArrayList list = new ArrayList(); + @Before + public void setUp() throws Exception { + list.add(1); + list.add(1,"boy next door"); + list.add(222); + list.add("333"); + list.add(4,"444"); + list.add(1232); + list.add("555"); + } + + + @Test + public void testAdd() { + System.out.println(list.get(0)); + System.out.println(list.get(3)); + System.out.println(list.get(5)); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.remove(0); + list.remove(5); + System.out.println(list.get(0)); + System.out.println(list.size()); +// System.out.println(list.get(5)); + } + + @Test + public void testSize() { + System.out.println(list.size()); + } + +} diff --git a/group18/542330964/src/junittest/LinkedListTest.java b/group18/542330964/src/junittest/LinkedListTest.java new file mode 100644 index 0000000000..8572b7a524 --- /dev/null +++ b/group18/542330964/src/junittest/LinkedListTest.java @@ -0,0 +1,65 @@ +package junittest; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.LinkedList; + +public class LinkedListTest { + + LinkedList l = new LinkedList(); + @Before + public void setUp() throws Exception { + l.add(1); + l.add("2"); + l.add(new Date()); + l.add(22); + l.add(33); + l.add(1, 3); + } + + @Test + public void testAddObject() { + System.out.println(l.get(1)); + System.out.println(l.get(3)); + System.out.println(l.get(200)); + } + + @Test + public void testRemove() { + l.remove(1); + System.out.println(l.get(1)); + } + + @Test + public void testSize() { + System.out.println(l.size()); + } + + @Test + public void testAddFirst() { + l.addFirst(0); + System.out.println(l.get(0)); + } + + @Test + public void testAddLast() { + l.addLast(999); + System.out.println(l.get(l.size()-1)); + } + + @Test + public void testRemoveFirst() { + l.removeFirst(); + System.out.println(l.get(0)); + } + + @Test + public void testRemoveLast() { + l.removeLast(); + System.out.println(l.get(l.size()-1)); + } + +} diff --git a/group18/542330964/src/junittest/QueueTest.java b/group18/542330964/src/junittest/QueueTest.java new file mode 100644 index 0000000000..a3488563e1 --- /dev/null +++ b/group18/542330964/src/junittest/QueueTest.java @@ -0,0 +1,33 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Queue; + +public class QueueTest { + + Queue q = new Queue(); + @Before + public void setUp() throws Exception { + q.enQueue(11); + q.enQueue(22); + q.enQueue(33); + q.enQueue(44); + q.enQueue(55); + } + + @Test + public void testDeQueue() { + q.deQueue(); + System.out.println(q.size()); + } + + @Test + public void testIsEmpty() { + System.out.println(q.isEmpty()); + } + +} diff --git a/group18/542330964/src/junittest/StackTest.java b/group18/542330964/src/junittest/StackTest.java new file mode 100644 index 0000000000..96372bb957 --- /dev/null +++ b/group18/542330964/src/junittest/StackTest.java @@ -0,0 +1,39 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Stack; + +public class StackTest { + + Stack s= new Stack(); + @Before + public void setUp() throws Exception { + s.push(11); + s.push(22); + s.push(33); + s.push("44"); + s.push(55); + } + + @Test + public void testPop() { + System.out.println(s.peek()); + s.pop(); + System.out.println(s.peek()); + } + + @Test + public void testIsEmpty() { + System.out.println(s.isEmpty()); + } + + @Test + public void testSize() { + System.out.println(s.size()); + } + +} From 288a420ecd1389488177c316fe42078ee3ed9327 Mon Sep 17 00:00:00 2001 From: wangmeng Date: Sun, 26 Feb 2017 04:48:03 +0800 Subject: [PATCH 180/432] =?UTF-8?q?1,=20=E4=B8=BA=E5=88=98=E8=80=81?= =?UTF-8?q?=E5=B8=88=E4=B8=B2=E8=AE=B2=E7=9A=84.gitignore=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0.idea=E7=9A=84=E8=BF=87=E6=BB=A4=E8=A7=84=E5=88=99?= =?UTF-8?q?=E3=80=82=202=EF=BC=8C=20=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E4=B8=AD=E9=9B=86=E4=B8=AD=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E7=8E=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + group03/510782645/.gitignore | 46 ++++ .../src/com/coding/basic/ArrayList.java | 152 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 83 +++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 202 ++++++++++++++++++ .../510782645/src/com/coding/basic/List.java | 9 + .../510782645/src/com/coding/basic/Queue.java | 100 +++++++++ .../510782645/src/com/coding/basic/Stack.java | 39 ++++ 9 files changed, 642 insertions(+) create mode 100644 group03/510782645/.gitignore create mode 100644 group03/510782645/src/com/coding/basic/ArrayList.java create mode 100644 group03/510782645/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/510782645/src/com/coding/basic/Iterator.java create mode 100644 group03/510782645/src/com/coding/basic/LinkedList.java create mode 100644 group03/510782645/src/com/coding/basic/List.java create mode 100644 group03/510782645/src/com/coding/basic/Queue.java create mode 100644 group03/510782645/src/com/coding/basic/Stack.java diff --git a/.gitignore b/.gitignore index ec55baf87d..aefc6cd3ea 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ hs_err_pid* #ide config .metadata .recommenders + +#idea config +.idea +.idea/* diff --git a/group03/510782645/.gitignore b/group03/510782645/.gitignore new file mode 100644 index 0000000000..497b26f4df --- /dev/null +++ b/group03/510782645/.gitignore @@ -0,0 +1,46 @@ +# Class files +*.class + +# Package Files +*.jar +*.war +*.ear + +# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Ignore web-site project +*web-site/ + +# Temporary files +.DS_STORE +*.log + +# Maven related +/*/target/ +target + +# Netbeans related +nb-configuration.xml +nbactions.xml +nbproject + +# Eclipse related +*.classpath +*.project +.settings + +# IntelliJ related +.idea +*.iml +*.ipr +*.iws + +# Jrebel related +rebel.xml +rebel-remote.xml + +# design model +*.eab + +.idea/workspace.xml diff --git a/group03/510782645/src/com/coding/basic/ArrayList.java b/group03/510782645/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a8a9f990d4 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/ArrayList.java @@ -0,0 +1,152 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; + +public class ArrayList implements List { + /** + * 当数组进行add/remove时, 对modCount进行++ + */ + protected transient int modCount = 0; + /** + * 数组的大小 + */ + private int size = 0; + + /** + * 数组,用来存放ArrayList的内容。 + */ + private Object[] elementData; + + public ArrayList() { + this(10); + } + + public ArrayList(int intialSize) { + elementData = new Object[intialSize]; + } + + public void add(Object o) { + modCount++; + // 检测是否要扩容,当添加的元素大于数组的长度后, 扩容 + increment(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + modCount++; + increment(size + 1); + /** + * @param src + * 源数组 + * @param srcPos + * 源数组要复制的起始位置 + * @param dest + * 目的数组 + * @param destPos + * 目的数组放置的起始位置 + * @param length + * 复制的长度 从index位置开始copy, + */ + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + /** + * 验证是否要扩容。 + * + * @param capacity + */ + private void increment(int capacity) { + if (capacity - elementData.length > 0) { + grow(capacity); + } + } + + /** + * 扩容,扩容规则为:oldCapacity + oldCapacity/2 + * + * @param capacity + */ + private void grow(int capacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + oldCapacity / 2; + elementData = Arrays.copyOf(elementData, newCapacity); + } + + public Object get(int index) throws Exception { + checkSize(index); + return elementData[index]; + } + + public Object remove(int index) throws Exception { + modCount++; + checkSize(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + //回收多出来的内存。 + elementData[size--] = null; + return oldValue; + } + + /** + * 验证给定的数组下标是否小于数组的长度。 + * + * @param index + * @return + */ + private void checkSize(int index) throws Exception { + if (index > size) { + // 数组下标越界异常。 + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//记录下一个元素的索引 + int lastReturn = -1;//记录最后一个元素的索引 + int expectCount = modCount; + + @Override + public boolean hasNext() { + return (cursor != size); + } + + @Override + public Object next() { + checkForComodification(); + int i = cursor; + Object[] elementData = ArrayList.this.elementData; + cursor = i+ 1; + return elementData[lastReturn = i]; + } + + /** + * 核心方法, 这里remove可以避免fail-fast快速失败原则。 + * @throws Exception + */ + public void remove() throws Exception { + checkForComodification(); + ArrayList.this.remove(lastReturn); + cursor = lastReturn; + lastReturn = -1; + expectCount = modCount; + } + + /** + * 验证fail-fast规则。 + */ + final void checkForComodification() { + if (modCount != expectCount) + throw new ConcurrentModificationException(); + } + } +} diff --git a/group03/510782645/src/com/coding/basic/BinaryTreeNode.java b/group03/510782645/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..1e279dd56a --- /dev/null +++ b/group03/510782645/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,83 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + static class Node { + Integer data; + Node parent; + Node left; + Node right; + + public Node(Integer data, Node parent, Node left, Node right) { + this.data = data; + this.parent = parent; + this.left = left; + this.right = right; + } + + public String toString(){ + return "[data=" + data + "]"; + } + + public boolean equals(Object obj){ + if(this == obj){ + return true; + } + + if(obj.getClass() == Node.class){ + Node target = (Node) obj; + return data.equals(target.data) && left == target.left + && right == target.right && parent == target.parent; + } + + return false; + } + } + private Node root; + + BinaryTreeNode() { + root = null; + } + + BinaryTreeNode(Integer data) { + root = new Node(data, null, null, null); + } + + /** + * 暂且使用Intenger作为节点数据。 + * @param o + */ + public void insert(Integer o) { + if (root == null) { + root = new Node(o, null, null, null); + } else { + Node current = root; + Node parent = null; + int cmp; + + //搜索合适的叶子节点,以该叶子节点为父节点添加新节点 + do { + parent = current; + cmp = o.compareTo(current.data); + + //如果新节点的值大于当前节点的值 + if (cmp > 0) { + //以当前节点的右子节点作为当前节点 + current = current.right; + } else { + current = current.left; + } + } while (current != null); + + //创建新节点 + Node newNode = new Node(o, parent, null, null); + + //如果新节点的值大于父节点的值 + if (cmp > 0) { + parent.right = newNode; + } else { + parent.left = newNode; + } + } + } +} diff --git a/group03/510782645/src/com/coding/basic/Iterator.java b/group03/510782645/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/510782645/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/510782645/src/com/coding/basic/LinkedList.java b/group03/510782645/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2019f5c703 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/LinkedList.java @@ -0,0 +1,202 @@ +package com.coding.basic; + +public class LinkedList implements List { + //链表的长度 + int size = 0; + private Node first; + private Node last; + + public void add(Object o){ + linkLast(o); + size++; + } + + /** + * 按照索引添加 + * @param index + * @param o + */ + public void add(int index , Object o){ + if (index == size) + linkLast(o); + else + linkBefore(o, node(index)); + } + + /** + * 向链表的最后添加元素 + * @param o + */ + private void linkLast(Object o) { + final Node l = last; + final Node newNode = new Node(o, l, null); + last = newNode; + if (l == null) + //如果只有一个元素, 那么设置链表的first为newNode + first = newNode; + else + l.next = newNode; + size++; + } + + /** + * 向链表指定位置添加元素 + * @param o + * @param node + */ + private void linkBefore(Object o, Node node) { + final Node pred = node.prev; + final Node newNode = new Node(o, pred, node); + node.prev = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + + /** + * 将元素添加到起始位置。 + * @param o + */ + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(o, null, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + /** + * 这里查找index节点时, 通过index与size/2的距离来判断是从前往后找还是从后往前找。 + * @param index + * @return + */ + Node node(int index) { + if (index < (size >> 1)) { + Node x = first; + 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.prev; + return x; + } + } + + /** + * 直接调用node方法即可。 + * @param index + * @return + */ + public Object get(int index){ + return node(index); + } + + /** + * 根据下标删除 + * @param index + * @return + */ + public Object remove(int index){ + Node node = node(index); + return remove(node); + } + + /** + * 根据节点的data值来remove + * @param o + * @return + */ + public Object remove(Object o) { + if (o == null) { + for (Node x = first; x != null; x = x.next) { + if (x.data == null) { + return remove(x); + } + } + } else { + for (Node x = first; x != null; x = x.next) { + if (o.equals(x.data)) { + return remove(x); + } + } + } + return null; + } + + private Object remove(Node node){ + final Object obj = node.data; + final Node next = node.next; + final Node prev = node.prev; + //判断临界的地方,index为第一个元素, index为第二个元素 + if (node == first) { + first = next; + } else if (node == last) { + last = prev; + } else { + prev.next = next; + next.prev = prev; + + node.next = null; + node.prev = null; + } + + node.data = null; + size--; + return obj; + } + + public int size(){ + return -size; + } + + public void addFirst(Object o){ + linkFirst(o); + } + public void addLast(Object o){ + linkLast(o); + } + public Object removeFirst(){ + return remove(first); + } + + /** + * 获取但不删除栈顶元素,失败则抛出异常 + * @return + */ + public Object peekFirst() { + final Node f = first; + return (f == null) ? null : f.data; + } + + public Object removeLast(){ + return remove(last); + } + public Iterator iterator(){ + return null; + } + + /** + * Node内部实现类 + */ + private static class Node{ + Object data; + Node prev; + Node next; + + /** + * 使用内部类来实现链表的每一个节点,每个节点有一个指向下一个元素的next,指向上一个元素的prev,以及自身的data + */ + public Node(Object data, Node prev, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group03/510782645/src/com/coding/basic/List.java b/group03/510782645/src/com/coding/basic/List.java new file mode 100644 index 0000000000..6aa2551499 --- /dev/null +++ b/group03/510782645/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) throws Exception; + public Object remove(int index) throws Exception; + public int size(); +} diff --git a/group03/510782645/src/com/coding/basic/Queue.java b/group03/510782645/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4ea6bf2fb3 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/Queue.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Queue的常用方法: + * add(Object o):向队尾插入元素,失败则抛出异常 + * offer(Object o):向队尾插入元素,失败则返回false + * remove():获取并删除队首元素,失败则抛出异常 + * poll():获取并删除队首元素,失败则返回null + * element():获取但不删除队首元素,失败则抛出异常 + * peek():获取但不删除队首元素,失败则返回null + */ +public class Queue { + /** + * Queue中存储的元素 + */ + private Object[] data; + /** + * head指向首端第一个有效元素 + */ + private int head; + /** + * tail指向尾端第一个可以插入元素的空位。 + */ + private int tail; + + /** + * 进队列 + */ + public void enQueue(Object o) { + addLast(o); + } + + /** + * 向队列的尾部添加元素 + */ + public void addLast(Object o) { + if (o == null) + throw new NullPointerException(); + data[tail] = o; + //这里可以避免数组是否越界。 + if ((tail = (tail + 1) & (data.length - 1)) == head) + doubleCapacity(); + } + + /** + * 检查是否要扩容。 + */ + private void doubleCapacity() { + assert head == tail; + int p = head; + int n = data.length; + int r = n - p; // head右边元素的个数 + int newCapacity = n << 1;//原空间的2倍 + if (newCapacity < 0) + throw new IllegalStateException("Sorry, deque too big"); + Object[] a = new Object[newCapacity]; + System.arraycopy(data, p, a, 0, r);//复制右半部分 + System.arraycopy(data, 0, a, r, p);//复制左半部分 + data = (Object[]) a; + head = 0; + tail = n; + } + + /** + * 出队列 + */ + public Object deQueue() { + return removeFirst(); + } + + /** + * 移除第一个元素 + */ + public Object removeFirst() { + Object x = pollFirst(); + if (x == null) + throw new NoSuchElementException(); + return x; + } + + public Object pollFirst() { + int h = head; + Object result = data[h]; // Element is null if deque empty + if (result == null) + return null; + data[h] = null; // Must null out slot + head = (h + 1) & (data.length - 1); + return result; + } + + public boolean isEmpty() { + return head == tail; + } + + public int size() { + return (tail - head) & (data.length - 1); + } +} diff --git a/group03/510782645/src/com/coding/basic/Stack.java b/group03/510782645/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6c9384ab67 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 堆栈是先进后出的结构。 + */ +public class Stack { + protected int elementCount; + private LinkedList elementData = new LinkedList(); + + /** + * 向栈顶插入元素,失败则抛出异常。同LikedList中的addFirst(); + * @param o + */ + public void push(Object o){ + elementData.addFirst(o); + } + + /** + * 获取并删除栈顶元素,失败则抛出异常。同LikedList中的removeFirst(); + * @return + */ + public Object pop(){ + return elementData.removeFirst(); + } + + /** + * 获取但不删除栈顶元素,失败则抛出异常. 同LinkedList中的peekFirst(); + * @return + */ + public Object peek(){ + return elementData.peekFirst(); + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} From 8d011fa3948e4aa6e4a2f1b3003a83e53aec719a Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 11:51:09 +1100 Subject: [PATCH 181/432] Finish Except Tree --- group02/609990377/DataStructure/.gitignore | 3 + .../coding2017/basic/ArrayList.java | 113 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++++ .../coding2017/basic/Iterator.java | 8 + .../coding2017/basic/LinkedList.java | 163 ++++++++++++++++++ .../coding2017/basic/List.java | 9 + .../coding2017/basic/Queue.java | 30 ++++ .../coding2017/basic/Stack.java | 28 +++ 8 files changed, 386 insertions(+) create mode 100644 group02/609990377/DataStructure/.gitignore create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java diff --git a/group02/609990377/DataStructure/.gitignore b/group02/609990377/DataStructure/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/609990377/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cfdc5fefe1 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + + //check size limit + if(size + 1 > elementData.length){ + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + //Check Object class + if(size >= 1){ + if(o.getClass() != elementData[0].getClass()) + throw new InputMismatchException("Index:"+index+" Size:"+size); + } + + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + //check size limit + if(size + 1 > elementData.length){ + Object oldData[] = elementData; + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + for(int i = size-1; i >= index; i-- ){ + elementData[i] = elementData[i-1]; + } + + elementData[index] = o; + + } + + public Object get(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + return elementData[index]; + } + + public Object remove(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Object old = elementData[index]; + for(int i = index; i < size-1 ; i++ ){ + elementData[i] = elementData[i+1]; + } + elementData[--size] = null; + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + //index for next element to visit + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if(cursor >= size){ + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + @Override + public void remove() { + //Check bound + if(cursor == 0){ + throw new NoSuchElementException(); + } + + ArrayList.this.remove(--cursor); + + } + + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..74d7ae6f16 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.congcongcong250.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..9033ad33be --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..840bb14f7c --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java @@ -0,0 +1,163 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head.next = head; + head.previous = head; + size = 0; + } + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Node nx = this.find(index); + Node pr = nx.previous; + Node in = new Node(o,pr,nx); + nx.previous = in; + pr.next = in; + size++; + } + + public Object get(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + return this.find(index); + } + + public Object remove(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + Node rem = this.find(index); + + Node pr = rem.previous; + Node nx = rem.next; + pr.next = nx; + nx.previous = pr; + + Object ret = rem.data; + rem.previous = null; + rem.next = null; + rem.data = null; + size--; + return ret; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node nx = head.next; + Node in = new Node(o,head, nx); + head.next = in; + nx.previous = in; + size++; + } + + public void addLast(Object o){ + Node last = head.previous; + Node in = new Node(o,last,head); + last.next = in; + head.previous = in; + + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new ListItr(); + } + + private Node find(int index){ + Node tra = head; + + //If index < size/2 + if( index < (size >> 1)){ + for(int i = 0; i <= index; i++){ + tra = tra.next; + } + }else{ + for(int i = size; i >= index; i--){ + tra = tra.previous; + } + } + return tra; + } + + private static class Node{ + Object data; + Node next; + Node previous; + + public Node(Object obj,Node pre, Node nx){ + data = obj; + next = nx; + previous = pre; + } + + + } + + private class ListItr implements Iterator{ + //Point to next node + Node cursor; + int nextIndex; + + public ListItr(){ + cursor = head.next; + nextIndex = 0; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + Node re = cursor; + cursor = cursor.next; + nextIndex++; + return re; + } + + public Object previous() { + Node re = cursor.previous.previous; + cursor = cursor.previous; + nextIndex--; + return re; + } + + @Override + public void remove() { + //Check bound + if(nextIndex > size){ + throw new NoSuchElementException("Iterates to the end"); + } + LinkedList.this.remove(--nextIndex); + + } + + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java new file mode 100644 index 0000000000..fe05e60f37 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java new file mode 100644 index 0000000000..2cdd329b0b --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Queue { + private LinkedList elementData; + + public Queue(){ + elementData = new LinkedList(); + } + + public void enQueue(Object o){ + elementData.addFirst(o); + } + + public Object deQueue(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return (elementData.size() == 0); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java new file mode 100644 index 0000000000..e3024202da --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public Stack(){ + elementData = new LinkedList(); + } + + public void push(Object o){ + elementData.addLast(o); + } + + public Object pop(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } +} From 97248d3ee35c82038ae8c164a24a7f706f854341 Mon Sep 17 00:00:00 2001 From: lqingchenl <953840070@qq.com> Date: Sun, 26 Feb 2017 08:53:43 +0800 Subject: [PATCH 182/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/ArrayList.java | 104 ++++++++++++++ .../lqingchenl/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 127 ++++++++++++++++++ .../lqingchenl/coding2017/basic/List.java | 9 ++ .../lqingchenl/coding2017/basic/Queue.java | 39 ++++++ .../lqingchenl/coding2017/basic/Stack.java | 46 +++++++ 6 files changed, 332 insertions(+) create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java create mode 100644 group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1019222a9e --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/ArrayList.java @@ -0,0 +1,104 @@ +package com.github.lqingchenl.coding2017.basic; + +import com.github.lqingchenl.coding2017.basic.List; +import org.junit.Test; + +import java.util.Arrays; + + +public class ArrayList implements List { + private int size = 0; + + private Object[] elementData = new Object[3]; + + /** + * 添加一个元素 + * + * @param o + */ + public void add(Object o) { + elementData[size] = o; + size = size + 1; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + /** + * 往固定位置添加一个元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + size = size + 1; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + // 将指定索引出后面的元素集体向后移动一格 +// for (int i = size; i >= index; i--) { +// elementData[i + 1] = elementData[i]; +// } + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + /** + * 获取元素 + * @param index + * @return + */ + public Object get(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + return elementData[index]; + } + + /** + * 移除元素 + * @param index + * @return + */ + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("索引越界"); + } + Object deleteData = elementData[index]; + if (index == size - 1){ + elementData[index] = null; + }else{ + int movedCount = size - index - 1; + System.arraycopy(elementData, index + 1, elementData, index, movedCount); + } + return deleteData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + /** + * 测试添加、移除、当前大小 + */ +// @Test + public void testArrayList() { + for (int j = 0; j < 3; j++) { + add(j); + } + add(2, 66); + + for (Object z : elementData) { + System.out.println(z); + } + + System.out.println(size()); + System.out.println(remove(3)); + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..086e1cd342 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.lqingchenl.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..9583ef43de --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/LinkedList.java @@ -0,0 +1,127 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node nextNode = head; + while (nextNode.next != null) { + nextNode = nextNode.next; + } + nextNode.next = new Node(o); + } + size++; + } + + public void add(int index, Object o) { + Node oldNode = getNode(index); + Node newNode = new Node(o); + newNode.next = oldNode.next; + oldNode.next = newNode; + size++; + } + + public Object get(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Node getNode(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + public Object remove(int index) { + if (index == 1){ + removeFirst(); + } + Node fatherNode = getNode(index -2); + Node oldNode = getNode(index -1); + fatherNode.next = oldNode.next; + size--; + + return oldNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + if (head == null){ + addFirst(o); + } + Node newNode = new Node(o); + Node lastNode = getNode(size -1); + lastNode.next = newNode; + size++; + } + + public Object removeFirst() { + Node oldHead = head; + Node secondNode = head.next; + head = secondNode; + size--; + return oldHead.data; + } + + public Object removeLast() { + if (size == 1){ + removeFirst(); + } + Object data = get(size - 1); + Node oldNode = getNode(size - 2); + oldNode.next = null; + size--; + return data; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } + + @Test + public void testLinkedList() { + add(1); + add(2); + add(3); + add(4); + remove(3); + addFirst(6); + for (int i = 0; i < size; i++) { + System.out.println("=="+get(i) +getNode(i)); + } + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java new file mode 100644 index 0000000000..d993812b9a --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.lqingchenl.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java new file mode 100644 index 0000000000..c617de0929 --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Queue.java @@ -0,0 +1,39 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class Queue { + private LinkedList queue = new LinkedList(); + private int size; + + public void enQueue(Object o) { + queue.addLast(o); + size++; + } + + public Object deQueue() { + Object o = queue.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + if (queue.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + + @Test + public void testStack() { + enQueue(1); + enQueue(2); + enQueue(3); + enQueue(4); + System.out.println(deQueue()); //出队列第一个元素 + System.out.println(size()); + } +} diff --git a/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java new file mode 100644 index 0000000000..d3d6caa4dc --- /dev/null +++ b/group02/953840070Learning/src/com/github/lqingchenl/coding2017/basic/Stack.java @@ -0,0 +1,46 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + size--; + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + + @Test + public void testStack(){ + push(1); + push(2); + push(3); + push(4); + System.out.println(pop()); + System.out.println(peek()); + System.out.println(isEmpty()); + System.out.println(size()); + } +} From 1a1b18312c316cb133883b3602c2419ab47e8097 Mon Sep 17 00:00:00 2001 From: liyao1 Date: Sun, 26 Feb 2017 08:59:07 +0800 Subject: [PATCH 183/432] first week homework --- group06/547958234/README.md | 0 .../src/com/coding/basic/ArrayList.java | 48 ++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 55 +++++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 116 ++++++++++++++++++ .../src/com/coding/basic/LinkedListTest.java | 20 +++ .../547958234/src/com/coding/basic/List.java | 9 ++ .../547958234/src/com/coding/basic/Queue.java | 26 ++++ .../547958234/src/com/coding/basic/Stack.java | 27 ++++ 9 files changed, 308 insertions(+) create mode 100644 group06/547958234/README.md create mode 100644 group06/547958234/src/com/coding/basic/ArrayList.java create mode 100644 group06/547958234/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group06/547958234/src/com/coding/basic/Iterator.java create mode 100644 group06/547958234/src/com/coding/basic/LinkedList.java create mode 100644 group06/547958234/src/com/coding/basic/LinkedListTest.java create mode 100644 group06/547958234/src/com/coding/basic/List.java create mode 100644 group06/547958234/src/com/coding/basic/Queue.java create mode 100644 group06/547958234/src/com/coding/basic/Stack.java diff --git a/group06/547958234/README.md b/group06/547958234/README.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group06/547958234/src/com/coding/basic/ArrayList.java b/group06/547958234/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..30be14a906 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/ArrayList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +import java.util.*; + +public class ArrayList implements List { + private int size = 0; + private static final int INCREMENT = 10; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + growLength(); + elementData[size++] = o; + } + public void add(int index, Object o){ + growLength(); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index) { + //如果之前分配了很多内存,多次remove后是否需要将elementData手动缩小容量 + if (index > size) throw new IndexOutOfBoundsException("index: "+index+", size: "+size); + Object retVal = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + elementData[--size] = null; + return retVal; + } + + public int size(){ + return this.size; + } + public Iterator iterator(){ + return null; + } + + private void growLength() { + if (this.size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length+INCREMENT); + } + } + +} diff --git a/group06/547958234/src/com/coding/basic/BinaryTreeNode.java b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4dff1cdb31 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +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) { + if (o < this.data) { + if (this.left != null) { + this.left.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.left = node; + } + } + if (o > this.data) { + if (this.right != null) { + this.right.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.right = node; + } + } + return this; + } + +} diff --git a/group06/547958234/src/com/coding/basic/Iterator.java b/group06/547958234/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group06/547958234/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/group06/547958234/src/com/coding/basic/LinkedList.java b/group06/547958234/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..bc4dd44b5b --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedList.java @@ -0,0 +1,116 @@ +package com.coding.basic; + +import sun.jvm.hotspot.debugger.win32.coff.AuxBfEfRecord; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + if (this.size == 0) { + addFirst(o); + } else { + Node node = findNode(size - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + node.next = newNode; + this.size++; + } + } + + public void add(int index, Object o) { + ensureNoOverStep(index); + if (index == 0) { + addFirst(o); + } else if (index == this.size) { + addLast(o); + } else { + Node beforeNode = findNode(index - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = beforeNode.next; + beforeNode.next = newNode; + this.size++; + } + + } + + public Object get(int index) { + ensureNoOverStep(index); + Node node = findNode(index); + return node.data; + } + + public Object remove(int index) { + //只需要把对应节点从链表中摘出来就行了? + ensureNoOverStep(index); + if (index == 0) { + return removeFirst(); + } else if (index == this.size - 1) { + return removeLast(); + } else { + Node beforeNode = findNode(index - 1); + Node selectNode = beforeNode.next; + beforeNode.next = selectNode.next; + this.size--; + return selectNode.data; + } + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + node.next = this.head; + this.head = node; + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = new Node(); + node = head; + head = head.next; + this.size--; + return node.data; + } + + public Object removeLast() { + Node beforeNode = findNode(this.size - 2); + Node node = beforeNode.next; + beforeNode.next = null; + this.size--; + return node.data; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + } + + private Node findNode(int index) { + ensureNoOverStep(index); + Node node = this.head; + while (index > 0) { + node = node.next; + index--; + } + return node; + } + + private void ensureNoOverStep(int index) { + if (index > this.size) throw new IndexOutOfBoundsException("Index: " + index + ", size: " + this.size); + } +} diff --git a/group06/547958234/src/com/coding/basic/LinkedListTest.java b/group06/547958234/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..afe137351f --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,20 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList; +/** + * Created by mac on 2017/2/21. + */ +public class LinkedListTest { + public static void main(String[] args) { + LinkedList l = new LinkedList(); + l.add(0); + l.add(1); + l.add(2); + l.add(3,3); + Object ret = l.remove(1); + + for(int i=0;i Date: Sun, 26 Feb 2017 09:07:20 +0800 Subject: [PATCH 184/432] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../275150374Learning/.idea/compiler.xml | 23 +++++++++++++++++++ .../275150374Learning/.idea/description.html | 1 + .../275150374Learning/.idea/encodings.xml | 5 ++++ .../275150374Learning/.idea/misc.xml | 12 ++++++++++ .../275150374Learning/.idea/modules.xml | 8 +++++++ .../275150374/275150374Learning/.idea/vcs.xml | 6 +++++ .../275150374Learning/275150374Learning.iml | 12 ++++++++++ .../src/task01/ArrayList.java | 8 +++++++ 8 files changed, 75 insertions(+) create mode 100644 group01/275150374/275150374Learning/.idea/compiler.xml create mode 100644 group01/275150374/275150374Learning/.idea/description.html create mode 100644 group01/275150374/275150374Learning/.idea/encodings.xml create mode 100644 group01/275150374/275150374Learning/.idea/misc.xml create mode 100644 group01/275150374/275150374Learning/.idea/modules.xml create mode 100644 group01/275150374/275150374Learning/.idea/vcs.xml create mode 100644 group01/275150374/275150374Learning/275150374Learning.iml create mode 100644 group01/275150374/275150374Learning/src/task01/ArrayList.java diff --git a/group01/275150374/275150374Learning/.idea/compiler.xml b/group01/275150374/275150374Learning/.idea/compiler.xml new file mode 100644 index 0000000000..217af471a9 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/group01/275150374/275150374Learning/.idea/description.html b/group01/275150374/275150374Learning/.idea/description.html new file mode 100644 index 0000000000..db5f129556 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/encodings.xml b/group01/275150374/275150374Learning/.idea/encodings.xml new file mode 100644 index 0000000000..e206d70d85 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/group01/275150374/275150374Learning/.idea/misc.xml b/group01/275150374/275150374Learning/.idea/misc.xml new file mode 100644 index 0000000000..de8f7c75a3 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/modules.xml b/group01/275150374/275150374Learning/.idea/modules.xml new file mode 100644 index 0000000000..5534fceb30 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/vcs.xml b/group01/275150374/275150374Learning/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/275150374Learning.iml b/group01/275150374/275150374Learning/275150374Learning.iml new file mode 100644 index 0000000000..d5c0743275 --- /dev/null +++ b/group01/275150374/275150374Learning/275150374Learning.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group01/275150374/275150374Learning/src/task01/ArrayList.java b/group01/275150374/275150374Learning/src/task01/ArrayList.java new file mode 100644 index 0000000000..71ed8b80d6 --- /dev/null +++ b/group01/275150374/275150374Learning/src/task01/ArrayList.java @@ -0,0 +1,8 @@ +package task01; + +/**第一周作业 + * 自己实现一个 + * Created by eurry on 2017/2/26. + */ +public class ArrayList { +} From dc99598379ce2fa8af225bbc82176b219c689275 Mon Sep 17 00:00:00 2001 From: VicotrShim Date: Sun, 26 Feb 2017 09:10:01 +0800 Subject: [PATCH 185/432] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/378213871/.classpath | 6 + group01/378213871/.gitignore | 1 + group01/378213871/.project | 17 +++ .../com/coding/basic/week01/ArrayList.java | 74 ++++++++++ .../coding/basic/week01/BinaryTreeNode.java | 32 +++++ .../src/com/coding/basic/week01/Iterator.java | 7 + .../com/coding/basic/week01/LinkedList.java | 129 ++++++++++++++++++ .../src/com/coding/basic/week01/List.java | 9 ++ .../src/com/coding/basic/week01/Queue.java | 24 ++++ .../src/com/coding/basic/week01/Stack.java | 35 +++++ 10 files changed, 334 insertions(+) create mode 100644 group01/378213871/.classpath create mode 100644 group01/378213871/.gitignore create mode 100644 group01/378213871/.project create mode 100644 group01/378213871/src/com/coding/basic/week01/ArrayList.java create mode 100644 group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java create mode 100644 group01/378213871/src/com/coding/basic/week01/Iterator.java create mode 100644 group01/378213871/src/com/coding/basic/week01/LinkedList.java create mode 100644 group01/378213871/src/com/coding/basic/week01/List.java create mode 100644 group01/378213871/src/com/coding/basic/week01/Queue.java create mode 100644 group01/378213871/src/com/coding/basic/week01/Stack.java diff --git a/group01/378213871/.classpath b/group01/378213871/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group01/378213871/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/378213871/.gitignore b/group01/378213871/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/378213871/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/378213871/.project b/group01/378213871/.project new file mode 100644 index 0000000000..a6666f301e --- /dev/null +++ b/group01/378213871/.project @@ -0,0 +1,17 @@ + + + coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/378213871/src/com/coding/basic/week01/ArrayList.java b/group01/378213871/src/com/coding/basic/week01/ArrayList.java new file mode 100644 index 0000000000..7726f9d8cf --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/ArrayList.java @@ -0,0 +1,74 @@ +package com.coding.basic.week01; + +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 ensureCapacity(int size) { + if (size > elementData.length) { + grow(); + } + } + + public void grow() { + elementData = Arrays.copyOf(elementData, size * 2); + } + + public void add(int index, Object o){ + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + 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 ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + Object removedItem = elementData[index]; + System.arraycopy(elementData, index, elementData, index + 1, size - index - 1); + size--; + return removedItem; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + private int current = 0; + + public boolean hasNext() { + return current < size(); + } + + public Object next() { + if(!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return elementData[current++]; + } + }; + } + +} diff --git a/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java b/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java new file mode 100644 index 0000000000..36e20a95c5 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic.week01; + +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/378213871/src/com/coding/basic/week01/Iterator.java b/group01/378213871/src/com/coding/basic/week01/Iterator.java new file mode 100644 index 0000000000..365e98e8b4 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic.week01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/378213871/src/com/coding/basic/week01/LinkedList.java b/group01/378213871/src/com/coding/basic/week01/LinkedList.java new file mode 100644 index 0000000000..dd6f3fb2e6 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/LinkedList.java @@ -0,0 +1,129 @@ +package com.coding.basic.week01; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(Object o){ + if (head == null) { + head = new Node(o); + size++; + } else{ + addLast(o); + } + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == 0) { + addFirst(o); + } else { + //定义标记节点sentinelNode,标记节点的下一个节点即为要新加的元素 + Node sentinelNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + Node node = new Node(o); + node.next = sentinelNode.next; + sentinelNode.next = node; + size++; + } + } + public Object get(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else { + Node indexNode = head; + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + return indexNode.data; + } + } + public Object remove(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else { + /** + * sentinelNode是所删除节点的上一个节点; + * indexNode是需要被删除的节点 + */ + Node sentinelNode = head; + Node indexNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + Node nextIndexNode = indexNode.next; + sentinelNode.next = nextIndexNode; + indexNode.next = null; + size--; + return indexNode.data; + } + } + + 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){ + //定义尾节点并通过while循环找到当前链表的尾节点 + Node tailNode = head; + while (tailNode.next != null) { + tailNode = tailNode.next; + } + Node node = new Node(o); + tailNode.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + head = head.next; + size--; + return newNode.data; + } + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + while (newNode.next.next != null) { + newNode = newNode.next; + } + Node lastNode = newNode.next; + newNode.next = null; + size--; + return lastNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; // 下一个节点 + + private Node(Object data) { + this.data = data; + next = null; + } + } +} diff --git a/group01/378213871/src/com/coding/basic/week01/List.java b/group01/378213871/src/com/coding/basic/week01/List.java new file mode 100644 index 0000000000..966ca016e8 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/List.java @@ -0,0 +1,9 @@ +package com.coding.basic.week01; + +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/378213871/src/com/coding/basic/week01/Queue.java b/group01/378213871/src/com/coding/basic/week01/Queue.java new file mode 100644 index 0000000000..1b25880f67 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic.week01; + +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/378213871/src/com/coding/basic/week01/Stack.java b/group01/378213871/src/com/coding/basic/week01/Stack.java new file mode 100644 index 0000000000..64cfa30da6 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic.week01; + +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 size() == 0; + } + public int size(){ + return elementData.size(); + } +} From 30ddee02de874e5a13af1fff589e7c0bb76d4cc2 Mon Sep 17 00:00:00 2001 From: easonzhang1992 Date: Sun, 26 Feb 2017 09:39:33 +0800 Subject: [PATCH 186/432] =?UTF-8?q?=E6=8F=90=E4=BA=A4easonzhang1992?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../git\345\221\275\344\273\244.txt" | 11 -- group07/1058267830/week1/.classpath | 8 ++ group07/1058267830/week1/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../week1/src/com/coding/basic/ArrayList.java | 108 ++++++++++++++++++ .../src/com/coding/basic/BinaryTree.java | 78 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 16 +++ .../week1/src/com/coding/basic/Iterator.java | 8 ++ .../src/com/coding/basic/LinkedList.java | 97 ++++++++++++++++ .../week1/src/com/coding/basic/List.java | 14 +++ .../week1/src/com/coding/basic/Node.java | 39 +++++++ .../week1/src/com/coding/basic/Queue.java | 25 ++++ .../week1/src/com/coding/basic/Stack.java | 29 +++++ .../test/com/coding/test/TestArrayList.java | 47 ++++++++ .../test/com/coding/test/TestBinaryTree.java | 29 +++++ .../test/com/coding/test/TestLinkedList.java | 40 +++++++ .../week1/test/com/coding/test/TestQueue.java | 26 +++++ .../week1/test/com/coding/test/TestStack.java | 31 +++++ 18 files changed, 623 insertions(+), 11 deletions(-) delete mode 100644 "group07/1058267830/git\345\221\275\344\273\244.txt" create mode 100644 group07/1058267830/week1/.classpath create mode 100644 group07/1058267830/week1/.project create mode 100644 group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs create mode 100644 group07/1058267830/week1/src/com/coding/basic/ArrayList.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/BinaryTree.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/Iterator.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/LinkedList.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/List.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/Node.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/Queue.java create mode 100644 group07/1058267830/week1/src/com/coding/basic/Stack.java create mode 100644 group07/1058267830/week1/test/com/coding/test/TestArrayList.java create mode 100644 group07/1058267830/week1/test/com/coding/test/TestBinaryTree.java create mode 100644 group07/1058267830/week1/test/com/coding/test/TestLinkedList.java create mode 100644 group07/1058267830/week1/test/com/coding/test/TestQueue.java create mode 100644 group07/1058267830/week1/test/com/coding/test/TestStack.java diff --git "a/group07/1058267830/git\345\221\275\344\273\244.txt" "b/group07/1058267830/git\345\221\275\344\273\244.txt" deleted file mode 100644 index 8bcf2ffa0f..0000000000 --- "a/group07/1058267830/git\345\221\275\344\273\244.txt" +++ /dev/null @@ -1,11 +0,0 @@ -��װgit gui��������git bash���� -1.��¡ -git clone git@github.com:leijing1992/coding2017.git -2.�����޸ĵ��ļ� -git add -A -3.�ύ���ݴ��� -git commit -m "�ύ***����" -4.����master -git pull origin master -5.�ύ��master -git push origin master \ No newline at end of file diff --git a/group07/1058267830/week1/.classpath b/group07/1058267830/week1/.classpath new file mode 100644 index 0000000000..91c6d8f6c5 --- /dev/null +++ b/group07/1058267830/week1/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/1058267830/week1/.project b/group07/1058267830/week1/.project new file mode 100644 index 0000000000..d3e8bd8c59 --- /dev/null +++ b/group07/1058267830/week1/.project @@ -0,0 +1,17 @@ + + + week1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs b/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/1058267830/week1/.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/group07/1058267830/week1/src/com/coding/basic/ArrayList.java b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8d05511d8a --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private Object[] obj ; + private int length; // 数组总长度 + private int size; // 元素个数 + private int currentIndex; // 当前索引位置,默认为-1 + + public int getLength() { + return length; + } + + public ArrayList(){ + this.obj = new Object[10]; + this.length = 10; + this.size = 0; + this.currentIndex = -1; + } + + public ArrayList(int initSize){ + this.obj = new Object[initSize]; + this.length = initSize; + this.size = 0; + this.currentIndex = -1; + } + + @Override + public void add(Object o) { + if(this.size < length){ + obj[size] = o; + this.size++; + + }else{ + // 扩容,add数据 + Object[] obj1 = new Object[length * 2]; + System.arraycopy(obj, 0, obj1, 0, length); + this.length = this.length * 2; + this.obj = obj1; + obj[this.size] = o; + this.size++; + } + } + + @Override + public void add(int index, Object o) { + if(index < length){ + // 容量扩1,add数据 + Object[] obj1 = new Object[length + 1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index, obj1, index+1, length-index); + obj1[index] = o; + this.obj = obj1; + this.length++; + this.size++; + }else{ + // 容量扩到index+1, add数据 + Object[] obj1 = new Object[index + 1]; + System.arraycopy(obj, 0, obj1, 0, length); + obj1[index] = o; + this.obj = obj1; + this.length = index + 1; + this.size++; + } + } + + @Override + public Object get(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + return this.obj[index]; + } + + @Override + public Object remove(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + Object tmp = obj[index];// 取值,最后返回 + Object[] obj1 = new Object[length -1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index+1, obj1, index, length-index-1); + this.obj = obj1; + this.length--; + this.size--; + return tmp; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean hasNext() { + if(currentIndex == length-1){ + return false; + }else{ + currentIndex++; + return true; + } + } + + @Override + public Object next() { + return this.get(currentIndex); + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..e326a21f75 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; // 根节点 + public BinaryTree( ){ + BinaryTreeNode node = new BinaryTreeNode(); + this.root = node; + } + + public boolean isEmpty(){ + return this.root == null; + } + + public void insert(Object o){ + // 如果是第一次添加节点,就是root节点 + if(root.data == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + root = bnode; + }else{ + insert(o, root); + } + } + + // 递归添加非root节点 + private BinaryTreeNode insert(Object o, BinaryTreeNode node) { + if(node == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + return bnode; + } + if((int)o <= (int)node.data){ + node.left = insert(o, node.left); + }else{ + node.right = insert(o, node.right); + } + + return node; + } + + // 中序遍历 + public void middlePrint(){ + middleOrder(this.root); + } + + private void middleOrder(BinaryTreeNode node) { + if(node != null){ + middleOrder(node.left); + System.out.print(node.data + " "); + middleOrder(node.right); + } + } + + // 前序遍历 + public void prePrint(){ + preOrder(this.root); + } + + private void preOrder(BinaryTreeNode node) { + if(node != null){ + System.out.print(node.data + " "); + preOrder(node.left); + preOrder(node.right); + } + } + + // 后序遍历 + public void postPrint(){ + postOrder(this.root); + } + + private void postOrder(BinaryTreeNode node) { + if(node != null){ + postOrder(node.right); + System.out.print(node.data + " "); + postOrder(node.left); + } + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a9e6593160 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + protected Object data; + protected BinaryTreeNode left; + protected BinaryTreeNode right; + + public BinaryTreeNode(){} + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/Iterator.java b/group07/1058267830/week1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d369bbc50c --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); +} diff --git a/group07/1058267830/week1/src/com/coding/basic/LinkedList.java b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..df65b13601 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterator{ + private Node head; + private Node tail; + private Node currentNode; + private int size; + + public LinkedList(){ + this.head = new Node(null); + this.tail = head; + this.currentNode = head; + this.size = 0; + } + + @Override + public void add(Object o) { + Node node = new Node(o, null); + tail.setNext(node); + tail = node; + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0 || index > size+1){ + throw new RuntimeException("插入的位置错误..."); + } + Node pre = head; // 得到待插入位置的前一个节点 + for(int i=0; i= size){ + throw new RuntimeException("index参数错误..."); + } + Node node = head; // 得到待插入位置的前一个节点 + for(int i=0; i<=index; i++){ + node = node.getNext(); + } + return node; + } + + @Override + public Object remove(int index) { + if(index < 0 || index >= size){ + throw new RuntimeException("index参数错误..."); + } + Node pre = head; // 得到待删除位置的前一个节点 + for(int i=0; i Date: Sun, 26 Feb 2017 09:40:52 +0800 Subject: [PATCH 187/432] Add my first code v1.1 --- .../HarryHook/coding2017/basic/BinaryTreeNode.java | 4 ++-- .../HarryHook/coding2017/basic/BinaryTreeNodeTest.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java index 09ad8c0fd5..2d962d9083 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNode.java @@ -25,7 +25,7 @@ public void inOrder(BinaryTreeNode node) //获取给节点的值 public Integer getData() { - return this.data; + return data; } //给一个节点赋值 public void setData(Integer data) @@ -64,7 +64,7 @@ public BinaryTreeNode insert(Integer obj) // 上个节点 BinaryTreeNode parent = null; // 如果根节点为空 - if (this.data == null) + if (current.data == null) { newNode.setData(obj); newNode.setLeft(null); diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java index 7f706c24b5..60d1979713 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/BinaryTreeNodeTest.java @@ -10,29 +10,29 @@ public class BinaryTreeNodeTest { - BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + BinaryTreeNode binaryTreeNode; @Before public void setUpBinaryTreeNode() { - //binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode = new BinaryTreeNode(); } @Test public void testBinaryTreeNodeFunctional() { - binaryTreeNode.insert(4); + binaryTreeNode = binaryTreeNode.insert(4); binaryTreeNode.insert(1); binaryTreeNode.insert(3); binaryTreeNode.insert(5); binaryTreeNode.insert(2); - + assertEquals(true, 4 == binaryTreeNode.getData()); assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); assertEquals(true, 5 == binaryTreeNode.getRight().getData()); assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); - + //节点为空 说明值没有插进去 binaryTreeNode.inOrder(binaryTreeNode); } From 027588236ce5b3c24f1e3ae09a825c4f7c158bba Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 26 Feb 2017 09:41:03 +0800 Subject: [PATCH 188/432] add ignore --- .gitignore | 19 +++++++++++++++++++ group02/727171008/.gitignore | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 group02/727171008/.gitignore diff --git a/.gitignore b/.gitignore index ec55baf87d..ecebe0642e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,22 @@ hs_err_pid* #ide config .metadata .recommenders +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file diff --git a/group02/727171008/.gitignore b/group02/727171008/.gitignore new file mode 100644 index 0000000000..806b57f381 --- /dev/null +++ b/group02/727171008/.gitignore @@ -0,0 +1,36 @@ +/bin/ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file From d69a64d409c4ef2b38bc95bfb46e055298e6e2a7 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:43:38 +0800 Subject: [PATCH 189/432] Create aa --- group08/1509102580/2.27/aa | 1 + 1 file changed, 1 insertion(+) create mode 100644 group08/1509102580/2.27/aa diff --git a/group08/1509102580/2.27/aa b/group08/1509102580/2.27/aa new file mode 100644 index 0000000000..e61ef7b965 --- /dev/null +++ b/group08/1509102580/2.27/aa @@ -0,0 +1 @@ +aa From a34d8bbaf5abd4f0c7936f1b96553d4365cd94eb Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:45:57 +0800 Subject: [PATCH 190/432] Add files via upload --- group08/1509102580/2.27/ArrayList.java | 103 +++++++++++++ group08/1509102580/2.27/Iterator.java | 7 + group08/1509102580/2.27/LinkedList.java | 196 ++++++++++++++++++++++++ group08/1509102580/2.27/List.java | 9 ++ group08/1509102580/2.27/Queue.java | 33 ++++ group08/1509102580/2.27/Stack.java | 38 +++++ 6 files changed, 386 insertions(+) create mode 100644 group08/1509102580/2.27/ArrayList.java create mode 100644 group08/1509102580/2.27/Iterator.java create mode 100644 group08/1509102580/2.27/LinkedList.java create mode 100644 group08/1509102580/2.27/List.java create mode 100644 group08/1509102580/2.27/Queue.java create mode 100644 group08/1509102580/2.27/Stack.java diff --git a/group08/1509102580/2.27/ArrayList.java b/group08/1509102580/2.27/ArrayList.java new file mode 100644 index 0000000000..02af2fc04e --- /dev/null +++ b/group08/1509102580/2.27/ArrayList.java @@ -0,0 +1,103 @@ +package com.zzk.coding2017.zuoye_1; + + +public class ArrayList implements List { + + private int size = 0; + private int length = 100; + private Object[] elementData = new Object[length]; + + public void add(Object o){ + if(sizesize-1){ + return ; + }else{ + if(sizesize-1){ + return null; + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + if(index<0||index>size-1){ + return null; + }else{ + Object result = elementData[index]; + if(index+1==size){//即index是最后一个元素 + elementData[index] = null; + size--; + return result; + }else{ + System.arraycopy(elementData, index+1, elementData, index, size-1-index); + size--; + return result; + } + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + int current = 0; + @Override + public Object next() { + // TODO Auto-generated method stub + if(current Date: Sun, 26 Feb 2017 09:46:51 +0800 Subject: [PATCH 191/432] =?UTF-8?q?Create=20=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1509102580/2.27/\346\226\207\347\253\240" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "group08/1509102580/2.27/\346\226\207\347\253\240" diff --git "a/group08/1509102580/2.27/\346\226\207\347\253\240" "b/group08/1509102580/2.27/\346\226\207\347\253\240" new file mode 100644 index 0000000000..d1dd75f51f --- /dev/null +++ "b/group08/1509102580/2.27/\346\226\207\347\253\240" @@ -0,0 +1,25 @@ +CPU,内存,磁盘,指令以及他们之间的关系 + +一、Cpu: +cpu是central process unit的简称,是计算机的核心处理器。Cpu的作用是运行机器语言指令。高级语言和汇编语言程序被编译成cpu能理解的机器语言后,存入到内存中,cpu就会从内存中读取并执行这些指令序列,完成程序功能。 +包括控制器、寄存器、运算器、时钟四个部分组成。 +其中运算器是用来计算的, +寄存器是临时存放中间数据以及指令的,包括程序计数器、指令寄存器、数据寄存器、地址寄存器等。 寄存器是汇编语言所研究的对象,汇编语言通过对寄存器的控制(写入,读出,加),实现功能。eg: + +第一句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 +第二句——对象eax寄存器;动作:add;参数:内存中的数;具体地址:ebp寄存器中存储的地址。 +第三句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 +程序计数器的作用是指定当前执行指令的地址。通过改变程序计数器,可以实现程序的逻辑控制。但是,虽然通过改变程序计数器的值可以实现程序跳转,但是单纯通过改变程序计数器的值却无法实现函数调用。因为跳转不需要回来,而函数调用结束之后还要回到原指令的下一条指令。为了实现函数调用,需要将程序计数器与call/return指令结合使用。也就是说,在函数调用时,在改变程序计数器值的同时,还要调用call指令,将返回位置写入栈内存中还需要在被调用函数的末尾添加return指令,这样在被调用函数执行结束后会继续执行return指令,return指令会将程序计数器的值设置为栈的最上边的值,也就是函数调用的返回地址。 +程序计数器和内存的关系:程序计数器记录了当前执行的指令在内存中年存储的位置。 +控制器是用来控制何时将内存的数据及指令读入寄存器,何时将寄存器中的值输出到内存。 +时钟是同步各个部件之间的步调。 +二、内存: +内存又叫主存,与cpu的寄存器链接,用于将数据以及指令输入到寄存器中;同时也可以接受寄存器的输出。本质是作为磁盘与寄存器的中间件,起到缓冲作用。断电后会丢失所有数据。 +内存和磁盘的关系:由于磁盘的读写速度与cpu相差太大,如果cpu直接从磁盘中读取数据,会造成cpu计算能力极大的浪费。为了解决这个问题,在磁盘与cpu之间加一个内存,将磁盘存储的程序以及数据线读入内存中,cpu直接与内存打交道。此外,由于内存的空间比磁盘小很多,会出现内存不够用的情况,此时,会将一部分磁盘当做内存来使用,也就是虚拟内存。 +三、磁盘: +存放数据的,容量比内存大,速度慢。 +断电不会丢失数据。 +四、指令: +指令是控制cpu按照一定功能运算的机器码。每一种cpu有自己的一套指令集。高级语言编写的程序最终也要编译成可以让cpu理解的一条条机器指令,才能被cpu执行。Cpu除了可以计算数据以外,还可以控制硬件,方法是通过cpu寄存器与I/O端口进行数据交换。计算机中主机与外围硬件的链接方式是通过I/O端口进行的(端口是用于主机寄存器与外围设备交换数据时临时存储数据的寄存器)。所以,指令通过控制cpu的输出和输入位置,可以让cpu寄存器向I/O端口寄存器发送数据,外围设备通过与I/O寄存器交换数据,从而达到控制外围设备的效果。而操作系统的作用是封装一套指令序列,cpu通过执行这套指令序列,可以让硬件完成一系列动作。而应用程序如果需要对硬件进行操作,只需要将操作系统的这套指令序列复制到自己的程序中即可。复制这套指令的方式就是API。从这个角度来看,操作系统就是封装了控制硬件完成一系列动作的指令序列的集合,这套集合提供给应用程序以及计算机的使用者。从而达到了将人、应用程序与硬件隔离的效果。 +五、它们之间的关系 +可执行程序(也就是机器码序列)首先从磁盘中被读入到内存中,然后内存中的程序会被一条一条的读到指令寄存器中被执行。 From da84e6838864d1c96b2cb1922e3730d644d53b9b Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:47:16 +0800 Subject: [PATCH 192/432] Delete aa --- group08/1509102580/2.27/aa | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group08/1509102580/2.27/aa diff --git a/group08/1509102580/2.27/aa b/group08/1509102580/2.27/aa deleted file mode 100644 index e61ef7b965..0000000000 --- a/group08/1509102580/2.27/aa +++ /dev/null @@ -1 +0,0 @@ -aa From aaee1751f578b7abd6f6f460acbe2000d86fbc20 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:47:43 +0800 Subject: [PATCH 193/432] Delete ArrayList.java --- group08/1509102580/ArrayList.java | 103 ------------------------------ 1 file changed, 103 deletions(-) delete mode 100644 group08/1509102580/ArrayList.java diff --git a/group08/1509102580/ArrayList.java b/group08/1509102580/ArrayList.java deleted file mode 100644 index 02af2fc04e..0000000000 --- a/group08/1509102580/ArrayList.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - - -public class ArrayList implements List { - - private int size = 0; - private int length = 100; - private Object[] elementData = new Object[length]; - - public void add(Object o){ - if(sizesize-1){ - return ; - }else{ - if(sizesize-1){ - return null; - }else{ - return elementData[index]; - } - } - - public Object remove(int index){ - if(index<0||index>size-1){ - return null; - }else{ - Object result = elementData[index]; - if(index+1==size){//即index是最后一个元素 - elementData[index] = null; - size--; - return result; - }else{ - System.arraycopy(elementData, index+1, elementData, index, size-1-index); - size--; - return result; - } - } - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return new Iterator() { - int current = 0; - @Override - public Object next() { - // TODO Auto-generated method stub - if(current Date: Sun, 26 Feb 2017 09:47:49 +0800 Subject: [PATCH 194/432] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正ArrayList类里的错误 --- group01/378213871/src/com/coding/basic/week01/ArrayList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group01/378213871/src/com/coding/basic/week01/ArrayList.java b/group01/378213871/src/com/coding/basic/week01/ArrayList.java index 7726f9d8cf..5745209a08 100644 --- a/group01/378213871/src/com/coding/basic/week01/ArrayList.java +++ b/group01/378213871/src/com/coding/basic/week01/ArrayList.java @@ -45,7 +45,7 @@ public Object remove(int index){ throw new ArrayIndexOutOfBoundsException(); } Object removedItem = elementData[index]; - System.arraycopy(elementData, index, elementData, index + 1, size - index - 1); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); size--; return removedItem; } From f7577be046d2a0f98626613f6fcf1c2adedb8be5 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:47:52 +0800 Subject: [PATCH 195/432] Delete Iterator.java --- group08/1509102580/Iterator.java | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 group08/1509102580/Iterator.java diff --git a/group08/1509102580/Iterator.java b/group08/1509102580/Iterator.java deleted file mode 100644 index e30991a64a..0000000000 --- a/group08/1509102580/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} From 880f2c9f4e6538faca70b9263706f9cb1830dafb Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:48:05 +0800 Subject: [PATCH 196/432] Delete LinkedList.java --- group08/1509102580/LinkedList.java | 196 ----------------------------- 1 file changed, 196 deletions(-) delete mode 100644 group08/1509102580/LinkedList.java diff --git a/group08/1509102580/LinkedList.java b/group08/1509102580/LinkedList.java deleted file mode 100644 index bd97921dd8..0000000000 --- a/group08/1509102580/LinkedList.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - if(head==null){ - head = new Node(); - head.data = o; - head.next = null; - }else{ - Node tmp = new Node(); - tmp.data = o; - tmp.next = head; - head = tmp; - } - } - public void add(int index , Object o){ - if(head==null){//如果head==null,说明根本不存在这个链表,所以不可能指定一个index来插入。 - return ; - }else{ - if(index<0){ - return; - }else{ - Node p = head; - int i = 0; - while(i Date: Sun, 26 Feb 2017 09:48:22 +0800 Subject: [PATCH 197/432] Delete List.java --- group08/1509102580/List.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 group08/1509102580/List.java diff --git a/group08/1509102580/List.java b/group08/1509102580/List.java deleted file mode 100644 index 10874d52a1..0000000000 --- a/group08/1509102580/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - -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(); -} From d6643879fcc3cd2112edc16b8d25972b1ad37ba3 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:48:30 +0800 Subject: [PATCH 198/432] Delete Queue.java --- group08/1509102580/Queue.java | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 group08/1509102580/Queue.java diff --git a/group08/1509102580/Queue.java b/group08/1509102580/Queue.java deleted file mode 100644 index 64426c4544..0000000000 --- a/group08/1509102580/Queue.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - -public class Queue { - ArrayList queue; - public Queue() { - // TODO Auto-generated constructor stub - queue = new ArrayList(); - } - public void enQueue(Object o){ - queue.add(o); - } - - public Object deQueue(){ - if(queue.size()==0){ - return null; - }else{ - Object result = queue.get(0); - queue.remove(0); - return result; - } - } - - public boolean isEmpty(){ - if(queue.size()==0) - return true; - else - return false; - } - - public int size(){ - return queue.size(); - } -} From 25b99c42ea0c74c8586d2389bf0f8dd3692a97f1 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:48:38 +0800 Subject: [PATCH 199/432] Delete Stack.java --- group08/1509102580/Stack.java | 38 ----------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 group08/1509102580/Stack.java diff --git a/group08/1509102580/Stack.java b/group08/1509102580/Stack.java deleted file mode 100644 index 13ce4461cc..0000000000 --- a/group08/1509102580/Stack.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.zzk.coding2017.zuoye_1; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - if(elementData.size()==0){ - return null; - } - else{ - Object result = elementData.get(elementData.size()-1); - elementData.remove(elementData.size()-1); - return result; - } - } - - public Object peek(){ - if(elementData.size()==0){ - return null; - } - else{ - return elementData.get(elementData.size()-1); - } - } - public boolean isEmpty(){ - if(elementData.size()==0) - return true; - else - return false; - } - public int size(){ - return elementData.size(); - } -} From 4a8b6ccb19736d5b5fd14cf7b9ee0e69f5397a8b Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:48:45 +0800 Subject: [PATCH 200/432] Delete zzk.md --- group08/1509102580/zzk.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group08/1509102580/zzk.md diff --git a/group08/1509102580/zzk.md b/group08/1509102580/zzk.md deleted file mode 100644 index 19fffff42b..0000000000 --- a/group08/1509102580/zzk.md +++ /dev/null @@ -1 +0,0 @@ -I am zzk! From 437ac9482e9a53375671bfeeff197b3d50376290 Mon Sep 17 00:00:00 2001 From: zzkmz <1509102580@qq.com> Date: Sun, 26 Feb 2017 09:48:55 +0800 Subject: [PATCH 201/432] =?UTF-8?q?Delete=20=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "group08/1509102580/\346\226\207\347\253\240" | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 "group08/1509102580/\346\226\207\347\253\240" diff --git "a/group08/1509102580/\346\226\207\347\253\240" "b/group08/1509102580/\346\226\207\347\253\240" deleted file mode 100644 index d1dd75f51f..0000000000 --- "a/group08/1509102580/\346\226\207\347\253\240" +++ /dev/null @@ -1,25 +0,0 @@ -CPU,内存,磁盘,指令以及他们之间的关系 - -一、Cpu: -cpu是central process unit的简称,是计算机的核心处理器。Cpu的作用是运行机器语言指令。高级语言和汇编语言程序被编译成cpu能理解的机器语言后,存入到内存中,cpu就会从内存中读取并执行这些指令序列,完成程序功能。 -包括控制器、寄存器、运算器、时钟四个部分组成。 -其中运算器是用来计算的, -寄存器是临时存放中间数据以及指令的,包括程序计数器、指令寄存器、数据寄存器、地址寄存器等。 寄存器是汇编语言所研究的对象,汇编语言通过对寄存器的控制(写入,读出,加),实现功能。eg: - -第一句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 -第二句——对象eax寄存器;动作:add;参数:内存中的数;具体地址:ebp寄存器中存储的地址。 -第三句——对象:eax寄存器;动作:mov;目的地:内存(dword);具体地址:ebp寄存器中存储的地址。 -程序计数器的作用是指定当前执行指令的地址。通过改变程序计数器,可以实现程序的逻辑控制。但是,虽然通过改变程序计数器的值可以实现程序跳转,但是单纯通过改变程序计数器的值却无法实现函数调用。因为跳转不需要回来,而函数调用结束之后还要回到原指令的下一条指令。为了实现函数调用,需要将程序计数器与call/return指令结合使用。也就是说,在函数调用时,在改变程序计数器值的同时,还要调用call指令,将返回位置写入栈内存中还需要在被调用函数的末尾添加return指令,这样在被调用函数执行结束后会继续执行return指令,return指令会将程序计数器的值设置为栈的最上边的值,也就是函数调用的返回地址。 -程序计数器和内存的关系:程序计数器记录了当前执行的指令在内存中年存储的位置。 -控制器是用来控制何时将内存的数据及指令读入寄存器,何时将寄存器中的值输出到内存。 -时钟是同步各个部件之间的步调。 -二、内存: -内存又叫主存,与cpu的寄存器链接,用于将数据以及指令输入到寄存器中;同时也可以接受寄存器的输出。本质是作为磁盘与寄存器的中间件,起到缓冲作用。断电后会丢失所有数据。 -内存和磁盘的关系:由于磁盘的读写速度与cpu相差太大,如果cpu直接从磁盘中读取数据,会造成cpu计算能力极大的浪费。为了解决这个问题,在磁盘与cpu之间加一个内存,将磁盘存储的程序以及数据线读入内存中,cpu直接与内存打交道。此外,由于内存的空间比磁盘小很多,会出现内存不够用的情况,此时,会将一部分磁盘当做内存来使用,也就是虚拟内存。 -三、磁盘: -存放数据的,容量比内存大,速度慢。 -断电不会丢失数据。 -四、指令: -指令是控制cpu按照一定功能运算的机器码。每一种cpu有自己的一套指令集。高级语言编写的程序最终也要编译成可以让cpu理解的一条条机器指令,才能被cpu执行。Cpu除了可以计算数据以外,还可以控制硬件,方法是通过cpu寄存器与I/O端口进行数据交换。计算机中主机与外围硬件的链接方式是通过I/O端口进行的(端口是用于主机寄存器与外围设备交换数据时临时存储数据的寄存器)。所以,指令通过控制cpu的输出和输入位置,可以让cpu寄存器向I/O端口寄存器发送数据,外围设备通过与I/O寄存器交换数据,从而达到控制外围设备的效果。而操作系统的作用是封装一套指令序列,cpu通过执行这套指令序列,可以让硬件完成一系列动作。而应用程序如果需要对硬件进行操作,只需要将操作系统的这套指令序列复制到自己的程序中即可。复制这套指令的方式就是API。从这个角度来看,操作系统就是封装了控制硬件完成一系列动作的指令序列的集合,这套集合提供给应用程序以及计算机的使用者。从而达到了将人、应用程序与硬件隔离的效果。 -五、它们之间的关系 -可执行程序(也就是机器码序列)首先从磁盘中被读入到内存中,然后内存中的程序会被一条一条的读到指令寄存器中被执行。 From a8643e546b0e7ee2a9135784de7e18704d968acd Mon Sep 17 00:00:00 2001 From: xiaomingbai <498654356@qq.com> Date: Sun, 26 Feb 2017 09:49:55 +0800 Subject: [PATCH 202/432] add repository --- group04/498654356/one/.classpath | 6 ++++++ group04/498654356/one/.gitignore | 1 + group04/498654356/one/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group04/498654356/one/.classpath create mode 100644 group04/498654356/one/.gitignore create mode 100644 group04/498654356/one/.project diff --git a/group04/498654356/one/.classpath b/group04/498654356/one/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group04/498654356/one/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/498654356/one/.gitignore b/group04/498654356/one/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/498654356/one/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/498654356/one/.project b/group04/498654356/one/.project new file mode 100644 index 0000000000..3cad565ebc --- /dev/null +++ b/group04/498654356/one/.project @@ -0,0 +1,17 @@ + + + 498654356Learning_one + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From c02d8523cf66db03e011d7b6c063469cc973faac Mon Sep 17 00:00:00 2001 From: nanjing_yangyang Date: Sun, 26 Feb 2017 10:12:31 +0800 Subject: [PATCH 203/432] =?UTF-8?q?236995728=E6=8F=90=E4=BA=A4=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group06/236995728/.classpath | 7 + group06/236995728/.gitignore | 1 + group06/236995728/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 92 +++++++++++ .../src/com/coding/basic/ArrayListTest.java | 82 ++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/LinkedList.java | 149 ++++++++++++++++++ .../src/com/coding/basic/LinkedListTest.java | 78 +++++++++ .../236995728/src/com/coding/basic/List.java | 14 ++ .../236995728/src/com/coding/basic/Queue.java | 45 ++++++ .../src/com/coding/basic/QueueTest.java | 53 +++++++ .../236995728/src/com/coding/basic/Stack.java | 52 ++++++ .../src/com/coding/basic/StackTest.java | 58 +++++++ 15 files changed, 697 insertions(+) create mode 100644 group06/236995728/.classpath create mode 100644 group06/236995728/.gitignore create mode 100644 group06/236995728/.project create mode 100644 group06/236995728/.settings/org.eclipse.jdt.core.prefs create mode 100644 group06/236995728/src/com/coding/basic/ArrayList.java create mode 100644 group06/236995728/src/com/coding/basic/ArrayListTest.java create mode 100644 group06/236995728/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group06/236995728/src/com/coding/basic/Iterator.java create mode 100644 group06/236995728/src/com/coding/basic/LinkedList.java create mode 100644 group06/236995728/src/com/coding/basic/LinkedListTest.java create mode 100644 group06/236995728/src/com/coding/basic/List.java create mode 100644 group06/236995728/src/com/coding/basic/Queue.java create mode 100644 group06/236995728/src/com/coding/basic/QueueTest.java create mode 100644 group06/236995728/src/com/coding/basic/Stack.java create mode 100644 group06/236995728/src/com/coding/basic/StackTest.java diff --git a/group06/236995728/.classpath b/group06/236995728/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group06/236995728/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/236995728/.gitignore b/group06/236995728/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group06/236995728/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/236995728/.project b/group06/236995728/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group06/236995728/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/236995728/.settings/org.eclipse.jdt.core.prefs b/group06/236995728/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group06/236995728/.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/group06/236995728/src/com/coding/basic/ArrayList.java b/group06/236995728/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..54f7ee73d5 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayList implements List { + + private static int size = 0; + + private static Object[] elementData = new Object[100]; + + /** + * 添加元素 + */ + @Override + public void add(Object o){ + if(size >= elementData.length){ + grow(size); + } + elementData[size++] = o; + } + + /** + * 按索引添加元素 + */ + @Override + public void add(int index, Object o){ + if(index < 0){ + throw new IllegalArgumentException("param invalid"); + } + if(index >= elementData.length){ + grow(index); + } + for(int i=index; i<=size; i++){ + elementData[i] = elementData[i+1]; + } + elementData[index] = o; + size ++; + } + + /** + * 根据索引获取元素 + */ + @Override + public Object get(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + return elementData[index]; + } + + /** + * 根据索引删除元素 + */ + @Override + public Object remove(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + Object o = elementData[index]; + for(int i=index;i>1); + elementData = Arrays.copyOf(elementData, newCapacity); + } +} diff --git a/group06/236995728/src/com/coding/basic/ArrayListTest.java b/group06/236995728/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..1159bb1829 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayListTest { + private static ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<10;i++){ + list.add(i); + System.out.println(list.get(i)); + } + } + + @Test + public void testAddObject() { + list.add("www"); + assertEquals("www", list.get(10)); + } + + @Test + public void testAddIntObject() { + list.add(101, 101); + assertEquals(101, list.get(101)); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddIntObjectException1(){ + list.add(-1, -1); + } + + @Test + public void testGet() { + assertEquals(1, list.get(1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException1(){ + list.get(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException2(){ + list.get(11); + } + + @Test + public void testRemove() { + list.remove(3); + assertEquals(4, list.get(3)); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException1(){ + list.remove(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException2(){ + list.remove(1000000000); + } + + @Test + public void testSize() { + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group06/236995728/src/com/coding/basic/BinaryTreeNode.java b/group06/236995728/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group06/236995728/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/group06/236995728/src/com/coding/basic/Iterator.java b/group06/236995728/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group06/236995728/src/com/coding/basic/LinkedList.java b/group06/236995728/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..57ee5a6dd9 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/LinkedList.java @@ -0,0 +1,149 @@ +package com.coding.basic; + +/** + * 2017/2/24 + * @author 236995728 + * 单链表 + */ +public class LinkedList implements List { + + private Node head = new Node(null,null); + private Node current = new Node(null, null); + + private int size = 0; + + /** + * 在尾节点添加节点 + */ + @Override + public void add(Object o){ + Node newNode = new Node(null,o); + if(head.next == null){ + head.next = newNode; + }else{ + current.next = newNode; + } + current = newNode; + size ++; + } + + /** + * 按照索引添加节点 + */ + @Override + public void add(int index , Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node newNode = new Node(null,o); + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node nextNode = null; + Object o = null; + for(int i=0; i Date: Sun, 26 Feb 2017 10:18:43 +0800 Subject: [PATCH 204/432] =?UTF-8?q?236995728word=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\347\232\204\345\205\263\347\263\273.docx" | Bin 0 -> 12398 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" diff --git "a/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000000000000000000000000000000000..fa7a796a74472df3f58fd20f7b52d78ed38132cd GIT binary patch literal 12398 zcmeHt^Y@&7CT#Tuwh9I@+WMi#$Gx<`s@P=EU^r-T+fLLiwz z%?1vo&Yj{2i$6Ia!N8J>ZfRkc z!4HT-xs}MgEQGJ_Zs zSlsw(T27f(_FCo(HB3PiSSz3S&I$n%P246ffOlUZ7(-65LgHei6)i7k^+PIdBcFuz z8eE(OM7HOTiM_?;jPSg1$7?Xl+#SLG^$MG^am!5r%fXDFe!A4W`2@^&+s{G-gL^9v zwzm)fz{?8+K80^qRz1uJ zLT5owK~rtY%U#&H3QQ)GE7(ggu)5N6Xe*0W3(rq{i;Lj;2gZ^k(=!Q^Ud|a3uA7Pa z=eWu0h~aH;^LGY4x(}@$fTWPtd=`&+hi!!Py&q;z!(`Gl!$A?+7~vDR&~dk6sbigJ zUungDd@X!sNlG&@YeHU=o9U>&aFhCu?ej;Lxp^xoU2m8Ye)0BDg646YckpI{RVi!? zu`%82)%v|^hY5|WQC2kH>2YnCnJ6;i-j>4Sp0`dNdgXQ$_I(JUhs%ch!1Ul_riZHY zydS=YBglD1qFrmaWsjGkFMI_GOn>)}d0>t5HAopOAn(Bkz=FBkI{}&gr!Q0xN9`R^{J@gpFE0j=F3&q1la47|QdBD*#+BrEez0C7UWB6_&7>#dFo zDe0q0qvms`@>J&uf6kSOu5kCrS?|KngHFibA}hQXXg!JvD^7)xtu?KW2xzh6jC2hQ zY%(XU6l7<=2Z-fxt4y-IuS*2-;Io?we3oU3iV50d@7I8~HE@k^O4@g4RlrfoWbP~1 zKz9Gm5oU#OOH^;4w!Rs}We>q4ph_a>@De$Cz%8RipbLabV3rk&-51x84WA^6FkvlJ zqA%&p?JMpy&ivA$U!6I__*_rae^#05+^9XBeGyI9)_@k1^^KZDQNZgCw3Ye0)yTb` zzh?sVL?ZzJC?I3_&1(K^FSVMw&NC9|@6P02f+Q7BAmVlxFHFO5c(Fp?j{{{qPpPYI ztlwI4O!H9svhKbf580YVHX>$^b+O1`hL~N1yJMNt8AkKVHm|Gxgv`aw;v1*I5@!>UAsqCFPdQ}din!Pniv<;*mM>8QLCK&o3QKX0|B#d}k8r|H2BhY}W*eL(M~E>? zULbeQG13E<6YV~1Ow+tT?}USp$7UbIkmElU)w7!V%;+f5)4{<%YBz7n*wY@q9^P8K z8vrp~mSI(!H^~?d-LHoRyeyTMv%1@B%^?Z(ir8U}kix6PyMKpW$7k}6Gl$v*1qFWU z@>^*45J$QkyY$@3D?_O?lkM=WD=86o7a0hi)W$sIkd zdTj?8?r4#7kOlKbSSr64NZ-$4h)9Q196KCbwQ3)BmRvaDs0w(5H^nObh%sMD22P=wHR2Y+B2kEDlVqc9O#!)+ z!U?JPW_gWs&qd9LHvo8PSu|gHyJsH5bVJESxe-;X(4AGbPy?zoHX*oNFm4pK2AD_` zb|p}|NtuNwLhd~~R(UxdcATp(`^!&b$A(|J<88*dzc}w+a8BBmo-EIyYA-p{;VFE* z>7Plf5t#8%rg%T2;9F2t>BZhAkGW?=Su7VcR6f79&@lLE|MVK{mgl{z`^`Gs#$~Ji zM&6a+JBviDPfKOFQMkor?3s+5nYN2_6#Hugsr}rIS*Hn0n%6vr)_kkh^y<$vYtLRZ zu-QuFsK;vX?ID6+wew)y8=kf+|(@}b)_0BUY?38+A0@+(y%tLhE z4JV#eL)RvYMy_Y4%+jhsku`2zS)h5;>blvGELr0%6?u?;$5DIkS5wWDp;h|PfL^LE zraDB{Ew#slC)`Nu33H|z>Z1gEiw{R`-zk=SWPOqLsBeeK%=p;~q)9Ldn}p9;4#&xm}iEgyl$h24&IPZZk3OSr=JthJ5!V^LxGn3AML zdPq4l&K%uZLL%OMAy`OCAzz6)kK#SF3=@Civ=9X1N%l*=F6#8Pq}sH{aU4TU-jNXX zlK%X?$GT?Bx~5m&gmioiM(==ydS(F@(8u5(JJqhEVC4Z=>bOaw@X;X~kSSlH zRhg&&`SnSGVvTJAxY+<5zqZ?_?>CCiKIb8~HSSj|_gas#ZxRr}3SgWdi*RUdxXTBT zI86i!=21FrbrG=QcSJq9ZC^q3i%YJ!&Bo}jw`mz*8@G4W8|^MEG#d8Ds-ToK*l$lcyz0V3nQkk} zwsR_q$`C_v)DvNLrz(Hzbe481%$YM&G3|fWQ7U7#f499gH zPG_h8kqCcuL$T5;wDZgBa*12w0@Y}Q&M%HRU^j4G`XW)z&9now62@r~dtC>$VJ&c- zah5iI6eCXuV)WiXr-K2dTu+FI#30y$Sl#NFND}tcHDRfUBLR47V_~(d3ZCvc)|>8=4U6mBB7(N#D2&XTgnRMoDJX*f)!aHVccA-V&Y4K_ZudShDm@*KO0XTCuydPGu-d zJ_KxY#LcmO_hg%RTid#=$_G}rQL|2ppMD=Q?_?#G8lfPe2uF(jvY$<==%a0j_(*3C z&6+(hTVPKc4Ew`(Xl4Y{Ox6d!7Tt;K0;9v3a@>N2c)f-M%P2A9rUP>L z5oTx-J==Asa@~$Gm_{vFq1PA>rg!-s92Zz>?MYlCdiYybl;_l0 zdH>v6TNoe0Z!bCYXu}>zmE1wa6mQG0X8dO;8f5B7_HQ%et>2s(?#@cG z3^}j|f^AGzmxi~GeyF<1Fw!Pb6}~5a9aBb(DlBSX5g!HvVZinlQ zHRSyGwB!^4&iA3~QH&W)ig7<;^dbhBnIh?aXsZ#8Ps^B0kRe-`vd%L&RVOn> zu5-f>q#c?i%gZBhtgUn!?-cbk3sc#|F*3u?a;Ud=d)ix=DQ!}PDE(6y!4g?&b)@0=T*>aGl~jW z-HE`Kl~Qj)mpqYDkwiAWS3k6%iPm49S6bH8-sTI*yLCb(%rs{};pL}UR#1%~mdEk! zAesTn8;jxs6{}9?q*fc`8+KiT&}m>PtTv9`Z&5qg-O^h?rSo8|r6mT+wyP`yhc)R` z-rnpm^y+TwBtS8(;If&L{(vlqg)ny##XV@76FfrfjB>AjTBoe%`0mw0?bGAtuBWDm zX)mpa#~zrXCBywW?H&@27c$!LjaRrYbk!PW6diMnNDa@p&#lSp`wIL>@@Ez-(fu5% z8;*D9(d53e()2Fk@BzVX4&{WdZ`$gQROpw70E1)U7s-DaR8P@{3F zS@EIx5z%8`ott$DIwv)VoF}VeVFw(iJM=d>IfQ5ygL zTUYhwuteXuwvUGPj6&Qg;oGxGXYx9{m>hp;rv(!c?l+aY(9&5%jZPYf?87sS61whl zD9WR#W?;kp=Q5%XHD587uQUkqGgiu>@qk7BZtoR89j0kcx^=3>HW=#LZblzBaxy_U z775^kbHeReDP~*9eceT&D!qqnA>N$QxZP*FH2_aN7&P?fZFqn-#}>xOUU^9~+8bq+ zNwWrDMNTONcYCTf0@ihdgCpmib4*6VwzIj`2S0T&q3W@Y*u zXb1L5rou;wyMuEiLmE(oo0v;uyHqZZnCkAXhj%ga1{(GHEWxW5ih8=KLZvLRC(ya1 z5~aGHIr&Dh#M8z>js*_LyFV{ogoW!l0}OKus?=!br|3U7e~?&~>(YdZd2^*)`tbS) zuhgF9DnO=9d(tf;sPxQK(hGtK4)=&S_-aEu3gm*q<(EGio)qL;}gd#&`q4tWbW;$FtxI zOW7*ljS@+SkD#ZSjonj(-ZexHxr6AHuRebN#Yg!JR5F{;DBgU)U>4z_K!XJl~&|_&sDH*Jxo>3H$@#CiIf9rnZ`VF$0g#-}_Nn~_N<5)xAbtMN2{kXvk5HofNaLN(y zNlTWW`l4U%-bF&R_<-2{zuQHE+q^V3$S#CHz39K&g}don)j!3&#HlVB2o_}M&6Z8x z;MEnI2!?7GUF2d96k%U~_D{A!ALRz~?2cx5%hgesP8bq^0(<6W5&Y2Kp+EBXNyLOE zX&@7n`L43xfAxIRI3FGnWsOHIl8e;o#nRE5>go#fg^F7IT{S9&Q&joXii(OYLJPy^ zuOt*iAdD@D8KE2pdfWI<7 zy8=1bKWH-b$g18Iq{$JGCXqp`F^Kw8a@pNu5qu9dD|qvfb#lyT+a* zV?%5HMhqcc6n*eU;feQ9c^}X z96_df3F<}pv#FZcJN*_5{%5NoLj{${__3e18hj?zD!S`!MHLd5LyjSM2uDsHF0>li zR$EYIvh*t3J|>}-2JH2!8~=s^r^)`6qP{rMW^0MOa6B9t3bwMWsRop%7UQ-vs^_l6 zWeKNJEQ1FtQ%sDLF`F`PqjMe9dw3u8jL3;#hC$pZvqY~d=&>hqWXiCLx*2@YBaK<7 zsx7+DG$w2%U`@M&me$xSU2(_s`PlR)Enj#bXsFdbcYQ7#BF~1w?6Q2>7b^52tLrWc z5u9q{p3E{pG@4;gFk8+xehC&>TVHkWz@?ipqwr5`LdT&k{XBY*jgaK$=Lr$;bTJvM z!@a51aw%eEnkc#8Mo_1@JjzvRYenUoO5_TbuocGkXuXNG}36IPy_wi9LzyMnoFGH^(`f6=*R z)8g5v$Q|!-#c{Yl+B--f_kxb01ElP~2Pb%Ny$Qxw004sn0Pu6s{q0J{*~Qbw^tZEE z>nDBZTyD(Jvj(ddh_7GPB5HSh+zR{olk#TLXWdvoI(j|mK);z6BTkNSIlR z9z`*CPQvC!5~(ndmh^ubsi?@les@WZL7Ns$N%UHhFvbTX?3*Xg{qyzr&7DD6HcTkF zk}yR{>&)nPPmebpPu0y*HFdkig<#5FDEBi{tRveI7u?@d_<#p67*|qfdPU^GkI$)m zF`~(>yB7f!-KnN;8S@6`cXE4rYLr8y@0DiXlK4D}-+2z^j`qf&F>)x2AE@+N%Ikj5 zE>lD8{c!1-b{O&6bm-N`FwWE=I6z3ZqFM>&5{xg4`UeD=_lmE_{IXZU6&(G^0Nk=~ zUOVcL^dIo-R**4WGo5CVB~>=flKV<$GA5aK=1%C03Edgq27YCgjffdCQSmJm_$X+o zo|wOW7?|IVtk{f>ivcg+))uM4GVRSRTQj0oxi_9}rCa5vjlYO{P^fKkl?2;sp_g$T zLKV0ElrVJgt!Opzxo(NMxIqu=D{X_iq%x$ivvhRgNB<>r0%BKtyNTC|oE za|=EAz}(klKfEMldx0d$wqlmGXBX>?q=HHByY2K#Ll5SW>*#}TmE1||w@;@mD6~_E zemwWr#mn3~*^L;&ow@x1Q!0>;1M!j@s)RNTFVA!A>sCP(mr^f&J_0PX!|aWo-#zuT zgwLY1l(Uh<{pq8}`+n?FK6Y@FmJH0_QkaYTZOX5c%3Uaj<&u)x>e=$L`8l8LQO|ye z-Z&DYI&;mLx#5{w^8WPgu}$InqD=wF{4#;|fqJtdA7#rD;7T;GEx|tUmXm#8n*#KU z0s4hxADEt;!k6WhaHbza?;1+9w+@Nwe`jNPvbOqU5{&2m61&;ICJ5f>c;kL+xBB$d zg6Hn%e%imb;oSmin)k4KGk1AfJC~ad~^1)HIC^F8fH0sWGbXWRd6|m6gX7 zI@u9X`v(^feGKF?+LU;-Dng62RAa;mL)Gd2{l!WsG@FK zT;;Z;lZ2A>pMTWZbG*J-^c{f8r?dmmVNY! zp*Nm@i%2o3Dp`%THDLR}r%9{jt%z>294f|9!6m<|V^|k;9v*sux`ObP0Ine%AswAm`*Faw31BmBUYN-tsB9WSfMFXkhEb^qE#OR&o;fR+R}Vl zvLI{xb@ih>Hj_53TzPHKH^JEjuHH6pOr-YDl0#x*xhrGBgTdF|%#a%tZ#G(~Ft9Am zlv&t2>5@!o$xSrq0$y&r_FwX7HuDt%b2+Bw3{V-6a2D%`v4Jx2@L^J3X*8t5tf9E; z9d};kTBwtgglGl=LGg~H+mRuTZw`phlTWcUn_oy$5pmZ2v`E8UNoZC1<2rFiLM;Ve z5*~V5nqj`il_ESPo0~mN$+^n26WHCd8^dtPB`~ld%v6( zK16JFUN+_uH!~x(lhMwi<)sU(7$!_FR5`9*_E!RWe%Dlp^a=&!e!I zQnJe?IhcJTaI~~U+mu3>*0b>r)%x2-V$WUsKyf;)#IK}UD5fg3;caqPhk8ox4$1p?NN|p=Ms$`0ctSx1xEj z|E!oyNbSsg8e|moF&>h&7Ue#(5=_URn7$ov(V3!b{9db%Xo?s7FI3f`xy|If`R*G) zP;=fftcyLAB&41_)Q)2tMx^^e8F62J11|HcN-{4+m}A&lWfE}PSui#zUT{-27+ymsAmfj!CVbHhK)ATorN~95gSA0U+o}-TA0vp!(1~~bbCHU!qZsfia;(G zAqQ}ld}MmfOdC0XgVEmtLg@Xuk5=q+6g{b0I$gVuR0q!{`3DJ0T8 z73vx>+orPh-84y3B`yKKmeY4Vh0;W~`Lc5@$a|SI{#n;ikk+(R`tt2KaPh4P<;m-j zkTv58?+@XatfVRhu9lgL}fdbo@f+04sxS>x3i2BNUCD6 zetoMjr)1om)x1zq91Sa}P8mcvtzwwhgBp=7v&diRImDWz5vkm@ib=qJ!-f781g*`g z^E-&7OWMlcF$Fg#BR{l=v3QJjIC_>k1aGTRT8j*siK`Pwv8F85%cpAi6;6d2O z4JBImE%gpO^(U8j8f({z$w#6T!LT~i038e_WGb0?5_O8Fg9W@jT3roscH^DH$jZoP z6|CuscDEsoeo-Z<5w4Q7w>H)#0?j3NCj>K>jh|X_O)VMnN8wGIWwf5rF-rSYRCSvK z)T3MsyIffIs!QKJP3WUrbe7z|`BLrQMMtOX^}>;InVBT{W$|6nZba!S?;G}t*^VVa zrVfH}+^kf)UU=*?%!0?oHSFQDh>BDDm6H3S{F1RCOi^o3-SYd6>#LXlY^}k-89*G* zKPP5`2K}Y{Lz=dd>|X`^HI4Mo=mb#W+@F$4e+B+EzwXb#8qf{@|CMd`tE69ZuKtj9 z0Lni9TL#vz@L!V({(wuq{x|&B6oX$y{CZ*Zhlp|zo%XK-%b%#UU*W&fvVXwej1V*QH#E0geNbRyB8(7*Evzrue# j;Qb+hkMwu#{pYx+Bnt&HkDn>auK-;j<|2jRXYKz1lAHbI literal 0 HcmV?d00001 From 6d3967a597cb2074b80f81753ba57988b3b40dbd Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 13:23:33 +1100 Subject: [PATCH 205/432] BinaryTree --- .../coding2017/basic/BinaryTreeNode.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java index 74d7ae6f16..fb71311dc4 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -1,11 +1,23 @@ package com.github.congcongcong250.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode >{ private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + public BinaryTreeNode(){ + data = null; + left = null; + right = null; + } + + public BinaryTreeNode(Object obj){ + data = obj; + left = null; + right = null; + } + public Object getData() { return data; } @@ -26,7 +38,35 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Object o){ - return null; + //If is empty root + if(data == null){ + data = o; + return this; + } + + //If it is a normal root + BinaryTreeNode in; + + if(o.compareTo(data) <= 0){ + if(left == null){ + in = new BinaryTreeNode(o); + left = in; + }else{ + in = left.insert(o); + } + }else{ + if(right == null){ + in = new BinaryTreeNode(o); + right = in; + }else{ + in = right.insert(o); + } + } + + assert (in == null):"Insert error"; + return in; } + + } From d7cfb7c7124a055a9a03230e4864cd40f50e9f19 Mon Sep 17 00:00:00 2001 From: nanjing_yangyang Date: Sun, 26 Feb 2017 10:28:37 +0800 Subject: [PATCH 206/432] =?UTF-8?q?236995728=E6=8F=90=E4=BA=A4=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E4=B9=8B=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\347\232\204\345\205\263\347\263\273.docx" | Bin 0 -> 12398 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" diff --git "a/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000000000000000000000000000000000..fa7a796a74472df3f58fd20f7b52d78ed38132cd GIT binary patch literal 12398 zcmeHt^Y@&7CT#Tuwh9I@+WMi#$Gx<`s@P=EU^r-T+fLLiwz z%?1vo&Yj{2i$6Ia!N8J>ZfRkc z!4HT-xs}MgEQGJ_Zs zSlsw(T27f(_FCo(HB3PiSSz3S&I$n%P246ffOlUZ7(-65LgHei6)i7k^+PIdBcFuz z8eE(OM7HOTiM_?;jPSg1$7?Xl+#SLG^$MG^am!5r%fXDFe!A4W`2@^&+s{G-gL^9v zwzm)fz{?8+K80^qRz1uJ zLT5owK~rtY%U#&H3QQ)GE7(ggu)5N6Xe*0W3(rq{i;Lj;2gZ^k(=!Q^Ud|a3uA7Pa z=eWu0h~aH;^LGY4x(}@$fTWPtd=`&+hi!!Py&q;z!(`Gl!$A?+7~vDR&~dk6sbigJ zUungDd@X!sNlG&@YeHU=o9U>&aFhCu?ej;Lxp^xoU2m8Ye)0BDg646YckpI{RVi!? zu`%82)%v|^hY5|WQC2kH>2YnCnJ6;i-j>4Sp0`dNdgXQ$_I(JUhs%ch!1Ul_riZHY zydS=YBglD1qFrmaWsjGkFMI_GOn>)}d0>t5HAopOAn(Bkz=FBkI{}&gr!Q0xN9`R^{J@gpFE0j=F3&q1la47|QdBD*#+BrEez0C7UWB6_&7>#dFo zDe0q0qvms`@>J&uf6kSOu5kCrS?|KngHFibA}hQXXg!JvD^7)xtu?KW2xzh6jC2hQ zY%(XU6l7<=2Z-fxt4y-IuS*2-;Io?we3oU3iV50d@7I8~HE@k^O4@g4RlrfoWbP~1 zKz9Gm5oU#OOH^;4w!Rs}We>q4ph_a>@De$Cz%8RipbLabV3rk&-51x84WA^6FkvlJ zqA%&p?JMpy&ivA$U!6I__*_rae^#05+^9XBeGyI9)_@k1^^KZDQNZgCw3Ye0)yTb` zzh?sVL?ZzJC?I3_&1(K^FSVMw&NC9|@6P02f+Q7BAmVlxFHFO5c(Fp?j{{{qPpPYI ztlwI4O!H9svhKbf580YVHX>$^b+O1`hL~N1yJMNt8AkKVHm|Gxgv`aw;v1*I5@!>UAsqCFPdQ}din!Pniv<;*mM>8QLCK&o3QKX0|B#d}k8r|H2BhY}W*eL(M~E>? zULbeQG13E<6YV~1Ow+tT?}USp$7UbIkmElU)w7!V%;+f5)4{<%YBz7n*wY@q9^P8K z8vrp~mSI(!H^~?d-LHoRyeyTMv%1@B%^?Z(ir8U}kix6PyMKpW$7k}6Gl$v*1qFWU z@>^*45J$QkyY$@3D?_O?lkM=WD=86o7a0hi)W$sIkd zdTj?8?r4#7kOlKbSSr64NZ-$4h)9Q196KCbwQ3)BmRvaDs0w(5H^nObh%sMD22P=wHR2Y+B2kEDlVqc9O#!)+ z!U?JPW_gWs&qd9LHvo8PSu|gHyJsH5bVJESxe-;X(4AGbPy?zoHX*oNFm4pK2AD_` zb|p}|NtuNwLhd~~R(UxdcATp(`^!&b$A(|J<88*dzc}w+a8BBmo-EIyYA-p{;VFE* z>7Plf5t#8%rg%T2;9F2t>BZhAkGW?=Su7VcR6f79&@lLE|MVK{mgl{z`^`Gs#$~Ji zM&6a+JBviDPfKOFQMkor?3s+5nYN2_6#Hugsr}rIS*Hn0n%6vr)_kkh^y<$vYtLRZ zu-QuFsK;vX?ID6+wew)y8=kf+|(@}b)_0BUY?38+A0@+(y%tLhE z4JV#eL)RvYMy_Y4%+jhsku`2zS)h5;>blvGELr0%6?u?;$5DIkS5wWDp;h|PfL^LE zraDB{Ew#slC)`Nu33H|z>Z1gEiw{R`-zk=SWPOqLsBeeK%=p;~q)9Ldn}p9;4#&xm}iEgyl$h24&IPZZk3OSr=JthJ5!V^LxGn3AML zdPq4l&K%uZLL%OMAy`OCAzz6)kK#SF3=@Civ=9X1N%l*=F6#8Pq}sH{aU4TU-jNXX zlK%X?$GT?Bx~5m&gmioiM(==ydS(F@(8u5(JJqhEVC4Z=>bOaw@X;X~kSSlH zRhg&&`SnSGVvTJAxY+<5zqZ?_?>CCiKIb8~HSSj|_gas#ZxRr}3SgWdi*RUdxXTBT zI86i!=21FrbrG=QcSJq9ZC^q3i%YJ!&Bo}jw`mz*8@G4W8|^MEG#d8Ds-ToK*l$lcyz0V3nQkk} zwsR_q$`C_v)DvNLrz(Hzbe481%$YM&G3|fWQ7U7#f499gH zPG_h8kqCcuL$T5;wDZgBa*12w0@Y}Q&M%HRU^j4G`XW)z&9now62@r~dtC>$VJ&c- zah5iI6eCXuV)WiXr-K2dTu+FI#30y$Sl#NFND}tcHDRfUBLR47V_~(d3ZCvc)|>8=4U6mBB7(N#D2&XTgnRMoDJX*f)!aHVccA-V&Y4K_ZudShDm@*KO0XTCuydPGu-d zJ_KxY#LcmO_hg%RTid#=$_G}rQL|2ppMD=Q?_?#G8lfPe2uF(jvY$<==%a0j_(*3C z&6+(hTVPKc4Ew`(Xl4Y{Ox6d!7Tt;K0;9v3a@>N2c)f-M%P2A9rUP>L z5oTx-J==Asa@~$Gm_{vFq1PA>rg!-s92Zz>?MYlCdiYybl;_l0 zdH>v6TNoe0Z!bCYXu}>zmE1wa6mQG0X8dO;8f5B7_HQ%et>2s(?#@cG z3^}j|f^AGzmxi~GeyF<1Fw!Pb6}~5a9aBb(DlBSX5g!HvVZinlQ zHRSyGwB!^4&iA3~QH&W)ig7<;^dbhBnIh?aXsZ#8Ps^B0kRe-`vd%L&RVOn> zu5-f>q#c?i%gZBhtgUn!?-cbk3sc#|F*3u?a;Ud=d)ix=DQ!}PDE(6y!4g?&b)@0=T*>aGl~jW z-HE`Kl~Qj)mpqYDkwiAWS3k6%iPm49S6bH8-sTI*yLCb(%rs{};pL}UR#1%~mdEk! zAesTn8;jxs6{}9?q*fc`8+KiT&}m>PtTv9`Z&5qg-O^h?rSo8|r6mT+wyP`yhc)R` z-rnpm^y+TwBtS8(;If&L{(vlqg)ny##XV@76FfrfjB>AjTBoe%`0mw0?bGAtuBWDm zX)mpa#~zrXCBywW?H&@27c$!LjaRrYbk!PW6diMnNDa@p&#lSp`wIL>@@Ez-(fu5% z8;*D9(d53e()2Fk@BzVX4&{WdZ`$gQROpw70E1)U7s-DaR8P@{3F zS@EIx5z%8`ott$DIwv)VoF}VeVFw(iJM=d>IfQ5ygL zTUYhwuteXuwvUGPj6&Qg;oGxGXYx9{m>hp;rv(!c?l+aY(9&5%jZPYf?87sS61whl zD9WR#W?;kp=Q5%XHD587uQUkqGgiu>@qk7BZtoR89j0kcx^=3>HW=#LZblzBaxy_U z775^kbHeReDP~*9eceT&D!qqnA>N$QxZP*FH2_aN7&P?fZFqn-#}>xOUU^9~+8bq+ zNwWrDMNTONcYCTf0@ihdgCpmib4*6VwzIj`2S0T&q3W@Y*u zXb1L5rou;wyMuEiLmE(oo0v;uyHqZZnCkAXhj%ga1{(GHEWxW5ih8=KLZvLRC(ya1 z5~aGHIr&Dh#M8z>js*_LyFV{ogoW!l0}OKus?=!br|3U7e~?&~>(YdZd2^*)`tbS) zuhgF9DnO=9d(tf;sPxQK(hGtK4)=&S_-aEu3gm*q<(EGio)qL;}gd#&`q4tWbW;$FtxI zOW7*ljS@+SkD#ZSjonj(-ZexHxr6AHuRebN#Yg!JR5F{;DBgU)U>4z_K!XJl~&|_&sDH*Jxo>3H$@#CiIf9rnZ`VF$0g#-}_Nn~_N<5)xAbtMN2{kXvk5HofNaLN(y zNlTWW`l4U%-bF&R_<-2{zuQHE+q^V3$S#CHz39K&g}don)j!3&#HlVB2o_}M&6Z8x z;MEnI2!?7GUF2d96k%U~_D{A!ALRz~?2cx5%hgesP8bq^0(<6W5&Y2Kp+EBXNyLOE zX&@7n`L43xfAxIRI3FGnWsOHIl8e;o#nRE5>go#fg^F7IT{S9&Q&joXii(OYLJPy^ zuOt*iAdD@D8KE2pdfWI<7 zy8=1bKWH-b$g18Iq{$JGCXqp`F^Kw8a@pNu5qu9dD|qvfb#lyT+a* zV?%5HMhqcc6n*eU;feQ9c^}X z96_df3F<}pv#FZcJN*_5{%5NoLj{${__3e18hj?zD!S`!MHLd5LyjSM2uDsHF0>li zR$EYIvh*t3J|>}-2JH2!8~=s^r^)`6qP{rMW^0MOa6B9t3bwMWsRop%7UQ-vs^_l6 zWeKNJEQ1FtQ%sDLF`F`PqjMe9dw3u8jL3;#hC$pZvqY~d=&>hqWXiCLx*2@YBaK<7 zsx7+DG$w2%U`@M&me$xSU2(_s`PlR)Enj#bXsFdbcYQ7#BF~1w?6Q2>7b^52tLrWc z5u9q{p3E{pG@4;gFk8+xehC&>TVHkWz@?ipqwr5`LdT&k{XBY*jgaK$=Lr$;bTJvM z!@a51aw%eEnkc#8Mo_1@JjzvRYenUoO5_TbuocGkXuXNG}36IPy_wi9LzyMnoFGH^(`f6=*R z)8g5v$Q|!-#c{Yl+B--f_kxb01ElP~2Pb%Ny$Qxw004sn0Pu6s{q0J{*~Qbw^tZEE z>nDBZTyD(Jvj(ddh_7GPB5HSh+zR{olk#TLXWdvoI(j|mK);z6BTkNSIlR z9z`*CPQvC!5~(ndmh^ubsi?@les@WZL7Ns$N%UHhFvbTX?3*Xg{qyzr&7DD6HcTkF zk}yR{>&)nPPmebpPu0y*HFdkig<#5FDEBi{tRveI7u?@d_<#p67*|qfdPU^GkI$)m zF`~(>yB7f!-KnN;8S@6`cXE4rYLr8y@0DiXlK4D}-+2z^j`qf&F>)x2AE@+N%Ikj5 zE>lD8{c!1-b{O&6bm-N`FwWE=I6z3ZqFM>&5{xg4`UeD=_lmE_{IXZU6&(G^0Nk=~ zUOVcL^dIo-R**4WGo5CVB~>=flKV<$GA5aK=1%C03Edgq27YCgjffdCQSmJm_$X+o zo|wOW7?|IVtk{f>ivcg+))uM4GVRSRTQj0oxi_9}rCa5vjlYO{P^fKkl?2;sp_g$T zLKV0ElrVJgt!Opzxo(NMxIqu=D{X_iq%x$ivvhRgNB<>r0%BKtyNTC|oE za|=EAz}(klKfEMldx0d$wqlmGXBX>?q=HHByY2K#Ll5SW>*#}TmE1||w@;@mD6~_E zemwWr#mn3~*^L;&ow@x1Q!0>;1M!j@s)RNTFVA!A>sCP(mr^f&J_0PX!|aWo-#zuT zgwLY1l(Uh<{pq8}`+n?FK6Y@FmJH0_QkaYTZOX5c%3Uaj<&u)x>e=$L`8l8LQO|ye z-Z&DYI&;mLx#5{w^8WPgu}$InqD=wF{4#;|fqJtdA7#rD;7T;GEx|tUmXm#8n*#KU z0s4hxADEt;!k6WhaHbza?;1+9w+@Nwe`jNPvbOqU5{&2m61&;ICJ5f>c;kL+xBB$d zg6Hn%e%imb;oSmin)k4KGk1AfJC~ad~^1)HIC^F8fH0sWGbXWRd6|m6gX7 zI@u9X`v(^feGKF?+LU;-Dng62RAa;mL)Gd2{l!WsG@FK zT;;Z;lZ2A>pMTWZbG*J-^c{f8r?dmmVNY! zp*Nm@i%2o3Dp`%THDLR}r%9{jt%z>294f|9!6m<|V^|k;9v*sux`ObP0Ine%AswAm`*Faw31BmBUYN-tsB9WSfMFXkhEb^qE#OR&o;fR+R}Vl zvLI{xb@ih>Hj_53TzPHKH^JEjuHH6pOr-YDl0#x*xhrGBgTdF|%#a%tZ#G(~Ft9Am zlv&t2>5@!o$xSrq0$y&r_FwX7HuDt%b2+Bw3{V-6a2D%`v4Jx2@L^J3X*8t5tf9E; z9d};kTBwtgglGl=LGg~H+mRuTZw`phlTWcUn_oy$5pmZ2v`E8UNoZC1<2rFiLM;Ve z5*~V5nqj`il_ESPo0~mN$+^n26WHCd8^dtPB`~ld%v6( zK16JFUN+_uH!~x(lhMwi<)sU(7$!_FR5`9*_E!RWe%Dlp^a=&!e!I zQnJe?IhcJTaI~~U+mu3>*0b>r)%x2-V$WUsKyf;)#IK}UD5fg3;caqPhk8ox4$1p?NN|p=Ms$`0ctSx1xEj z|E!oyNbSsg8e|moF&>h&7Ue#(5=_URn7$ov(V3!b{9db%Xo?s7FI3f`xy|If`R*G) zP;=fftcyLAB&41_)Q)2tMx^^e8F62J11|HcN-{4+m}A&lWfE}PSui#zUT{-27+ymsAmfj!CVbHhK)ATorN~95gSA0U+o}-TA0vp!(1~~bbCHU!qZsfia;(G zAqQ}ld}MmfOdC0XgVEmtLg@Xuk5=q+6g{b0I$gVuR0q!{`3DJ0T8 z73vx>+orPh-84y3B`yKKmeY4Vh0;W~`Lc5@$a|SI{#n;ikk+(R`tt2KaPh4P<;m-j zkTv58?+@XatfVRhu9lgL}fdbo@f+04sxS>x3i2BNUCD6 zetoMjr)1om)x1zq91Sa}P8mcvtzwwhgBp=7v&diRImDWz5vkm@ib=qJ!-f781g*`g z^E-&7OWMlcF$Fg#BR{l=v3QJjIC_>k1aGTRT8j*siK`Pwv8F85%cpAi6;6d2O z4JBImE%gpO^(U8j8f({z$w#6T!LT~i038e_WGb0?5_O8Fg9W@jT3roscH^DH$jZoP z6|CuscDEsoeo-Z<5w4Q7w>H)#0?j3NCj>K>jh|X_O)VMnN8wGIWwf5rF-rSYRCSvK z)T3MsyIffIs!QKJP3WUrbe7z|`BLrQMMtOX^}>;InVBT{W$|6nZba!S?;G}t*^VVa zrVfH}+^kf)UU=*?%!0?oHSFQDh>BDDm6H3S{F1RCOi^o3-SYd6>#LXlY^}k-89*G* zKPP5`2K}Y{Lz=dd>|X`^HI4Mo=mb#W+@F$4e+B+EzwXb#8qf{@|CMd`tE69ZuKtj9 z0Lni9TL#vz@L!V({(wuq{x|&B6oX$y{CZ*Zhlp|zo%XK-%b%#UU*W&fvVXwej1V*QH#E0geNbRyB8(7*Evzrue# j;Qb+hkMwu#{pYx+Bnt&HkDn>auK-;j<|2jRXYKz1lAHbI literal 0 HcmV?d00001 From 6c10cd4e585578efbd0f33607aa478f7c249c2a8 Mon Sep 17 00:00:00 2001 From: xiaozhupig Date: Sun, 26 Feb 2017 10:37:14 +0800 Subject: [PATCH 207/432] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\221\250\346\226\207\347\253\240\345\234\260\345\235\200.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "group06/799237637/\347\254\254\344\270\200\345\221\250\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git "a/group06/799237637/\347\254\254\344\270\200\345\221\250\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group06/799237637/\347\254\254\344\270\200\345\221\250\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..86c003d948 --- /dev/null +++ "b/group06/799237637/\347\254\254\344\270\200\345\221\250\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +http://blog.csdn.net/codingxiaozhupig/article/details/57150919 \ No newline at end of file From 28621ba8bfbc5f4807e384e4e81c698cb1fe4bdf Mon Sep 17 00:00:00 2001 From: Kilien Date: Sun, 26 Feb 2017 10:39:36 +0800 Subject: [PATCH 208/432] Create test.txt hello world --- group09/790466157/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group09/790466157/test.txt diff --git a/group09/790466157/test.txt b/group09/790466157/test.txt new file mode 100644 index 0000000000..3b18e512db --- /dev/null +++ b/group09/790466157/test.txt @@ -0,0 +1 @@ +hello world From 3c803acdd0e35a2a5b62bb90c2a1c8f621a1e171 Mon Sep 17 00:00:00 2001 From: "LAPTOP-DBDFJEC7\\Liner" Date: Sun, 26 Feb 2017 10:42:40 +0800 Subject: [PATCH 209/432] week01 --- .../src/com/coding/basic/Iterator.java | 7 + .../790466157/src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/MyArrayList.java | 133 +++++++++++++++ .../src/com/coding/basic/MyLinkedList.java | 159 ++++++++++++++++++ .../790466157/src/com/coding/basic/Queue.java | 68 ++++++++ .../790466157/src/com/coding/basic/Stack.java | 73 ++++++++ 6 files changed, 449 insertions(+) create mode 100644 group09/790466157/src/com/coding/basic/Iterator.java create mode 100644 group09/790466157/src/com/coding/basic/List.java create mode 100644 group09/790466157/src/com/coding/basic/MyArrayList.java create mode 100644 group09/790466157/src/com/coding/basic/MyLinkedList.java create mode 100644 group09/790466157/src/com/coding/basic/Queue.java create mode 100644 group09/790466157/src/com/coding/basic/Stack.java diff --git a/group09/790466157/src/com/coding/basic/Iterator.java b/group09/790466157/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..7c02cc6e51 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/List.java b/group09/790466157/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group09/790466157/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(); +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyArrayList.java b/group09/790466157/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..cf232c5b72 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; +import java.util.Arrays; +import java.util.NoSuchElementException; + +import com.coding.basic.List; +import com.coding.basic.Iterator; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE; + + private static final int DEFAULT_CAPACITY = 10; + + + //�޳����캯�� + public MyArrayList(){ + this(DEFAULT_CAPACITY); + } + + public MyArrayList(int size){ + if (size < 0){ + throw new IllegalArgumentException("Ĭ�ϵĴ�С" + size); + } + else{ + elementData = new Object[size]; + } + } + + public void add(Object o){ + isCapacityEnough(size+1); + elementData[size++] = o; + } + + private void isCapacityEnough(int size){ + //�ж��Ƿ񳬹���ʼ�������Ƿ���Ҫ���� + if (size > DEFAULT_CAPACITY){ + explicitCapacity(size); + } + if (size < 0){ + throw new OutOfMemoryError(); + } + } + + private void explicitCapacity(int capacity){ + int oldCapacity = elementData.length; + //������=������ + ��������/2�� ����1.5�������Ʋ������൱�ڳ���2�� + int newLength = oldCapacity + (oldCapacity >> 1); + if (newLength - capacity < 0){ + newLength = capacity; + } + //�ж�newLength�ij��� + //����������涨���������󳤶����ж�Ҫ��Ҫ�����ݿռ��Ƿ����������󳤶� + //���������newLengthΪ MAX_VALUE ������Ϊ MAX_ARRAY_LENGTH�� + if (newLength > (MAX_ARRAY_LENGTH)){ + newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + //����copyof�������� + elementData = Arrays.copyOf(elementData, newLength); + } + + public void add(int index, Object o){ + + checkRangeForAdd(index); + isCapacityEnough(size +1); + // �� elementData�д�Indexλ�ÿ�ʼ������Ϊsize-index��Ԫ�أ� + // ���������±�Ϊindex+1λ�ÿ�ʼ���µ�elementData�����С� + // ������ǰλ�ڸ�λ�õ�Ԫ���Լ����к���Ԫ������һ��λ�á� + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + //�ж��Ƿ�Խ�� + private void checkRangeForAdd(int index){ + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("ָ����indexԽ��"); + } + } + + // ���ش��б���ָ��λ���ϵ�Ԫ�ء� + public Object get(int index){ + checkRange(index); + return elementData[index]; + } + + //�ж��Ƿ�Խ�� + private void checkRange(int index){ + if (index >= size || index < 0){ + throw new IndexOutOfBoundsException("ָ����indexԽ��"); + } + } + + public Object remove(int index){ + Object value = get(index); + int moveSize = size - index -1; + if (moveSize >0){ + System.arraycopy(elementData, index +1, elementData, index, size - index -1); + + } + elementData[--size] = null; + return value; + } + + public int size(){ + return size; + } + + //������ + public Iterator iterator(Object o){ + return new ArrayListIterator(); + } + + + private class ArrayListIterator implements Iterator{ + private int currentIndex=0; + + public boolean hasNext() { + return currentIndex < size(); + } + + public Object next() { + if (!hasNext()){ + throw new NoSuchElementException(); + } + return new Object[currentIndex + 1]; + } + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyLinkedList.java b/group09/790466157/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..4727db3a89 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,159 @@ +package com.coding.basic; + +public class MyLinkedList { + + private int size; + + private Node first; + + private Node last; + + + public boolean add(E element) { + addAtLast(element); + return true; + } + + private void addAtLast(E element) { + Node l = last; + Node node = new Node(element, null, l); + last = node; + if (l == null) { + first = node; + } else { + l.next = node; + } + size++; + } + + public void add(int index, E element) { + checkRangeForAdd(index); + if (index == size) { + addAtLast(element); + } else { + Node l = node(index); + addBeforeNode(element, l); + } + } + + private void addBeforeNode(E element, Node specifiedNode) { + Node preNode = specifiedNode.prev; + Node newNode = new Node(element, specifiedNode, preNode); + if (preNode == null) { + first = newNode; + } else { + preNode.next = newNode; + } + specifiedNode.prev = newNode; + size++; + } + + + private Node node(int index) { + if (index < (size << 1)) { + Node cursor = first; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } else { + Node cursor = last; + for (int i = size - 1; i > index; i--) { + cursor = cursor.prev; + } + return cursor; + } + } + + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("ָ����index��������"); + } + } + + public E get(int index) { + checkRange(index); + return node(index).item; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("ָ��index��������"); + } + } + + public int indexOf(Object element) { + Node cursor = first; + int count = 0; + while (cursor != null) { + if (element != null) { + if (element.equals(cursor.item)) { + return count; + } + }else{ + if (cursor.item == null) { + return count; + } + } + count ++; + cursor = cursor.next; + } + return -1; + } + + public E remove(int index) { + checkRange(index); + return deleteLink(index); + } + + public boolean remove(Object o) { + int index = indexOf(o); + if (index < 0){ + return false; + } + deleteLink(index); + return true; + } + + private E deleteLink(int index) { + Node l = node(index); + E item = l.item; + Node prevNode = l.prev; + Node nextNode = l.next; + + if (prevNode == null) { + first = nextNode; + }else{ + prevNode.next = nextNode; + l.next = null; + } + + if (nextNode == null) { + last = prevNode; + }else{ + nextNode.prev = prevNode; + l.prev = null; + } + size--; + l.item = null; + return item; + } + + + + public int size(){ + return size; + } + private static class Node { + E item; + Node next; + Node prev; + + public Node(E item, Node next, Node prev) { + this.item = item; + this.next = next; + this.prev = prev; + + } + } +} diff --git a/group09/790466157/src/com/coding/basic/Queue.java b/group09/790466157/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..80d0dc9835 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; +import java.util.Arrays; + +public class Queue { + private static final int CAPACITY = 10; + private static int capacity; + private static int front; + private static int tail; + private static Object[] array; + + public Queue(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + front = tail = 0; + } + + public void enQueue(Object o) throws ExceptionQueueFull { + if (size() == capacity -1) + throw new ExceptionQueueFull("Queue is full"); + array[tail] = o; + tail = (tail +1) % capacity; + } + + public Object deQueue() throws ExceptionQueueEmpty{ + Object o; + if (isEmpty()) + throw new ExceptionQueueEmpty("Queue is empty"); + o = array[front]; + front = (front + 1) % capacity; + return o; + } + + public boolean isEmpty(){ + return (front == tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (capacity + tail - front) % capacity; + } + + public class ExceptionQueueEmpty extends Exception { + // Constructor + public ExceptionQueueEmpty() { + + } + + // Constructor with parameters + public ExceptionQueueEmpty(String mag) { + System.out.println(mag); + } + } + + public class ExceptionQueueFull extends Exception { + + // Constructor + public ExceptionQueueFull() { + + } + + // Constructor with parameters + public ExceptionQueueFull(String mag) { + System.out.println(mag); + } + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/Stack.java b/group09/790466157/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..324cc5639e --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Stack.java @@ -0,0 +1,73 @@ +package com.coding.basic; +import java.util.Arrays; +import com.coding.basic.MyArrayList; +public class Stack { + private MyArrayList elementData = new MyArrayList(); + private static final int CAPACITY = 10; + private static int capacity; + private static int top = -1; + Object[] array; + + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws ExceptionStackFull{ + if(size()== CAPACITY){ + throw new ExceptionStackFull("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + + public class ExceptionStackEmpty extends Exception { + + //Constructor + public ExceptionStackEmpty(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackEmpty(String string){ + super(string); + } + } + + public class ExceptionStackFull extends Exception { + + //Constructor + public ExceptionStackFull(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackFull(String string){ + super(string); + } + } +} \ No newline at end of file From 6306e807721900ec2f5ba732584c6c28e16a91d2 Mon Sep 17 00:00:00 2001 From: ikook <935542673@qq.com> Date: Sun, 26 Feb 2017 10:44:15 +0800 Subject: [PATCH 210/432] Update ArrayList.java --- .../com/ikook/basic_data_structure/MyArrayList.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java index 6891a76879..79ec2865c3 100644 --- a/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java +++ b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java @@ -51,7 +51,9 @@ public void add(Object obj) { * @param obj */ public void add(int index, Object obj) { - rangeCheck(index); + if (index > size || index < 0) + throw new IndexOutOfBoundsException("索引越界异常"); + ensureCapacity(); System.arraycopy(elementData, index, elementData, index + 1, size-index); @@ -151,11 +153,7 @@ private void ensureCapacity() { */ private void rangeCheck(int index) { if(index < 0 || index >= size) { - try { - throw new Exception("索引异常"); - } catch (Exception e) { - e.printStackTrace(); - } + throw new IndexOutOfBoundsException("索引越界异常"); } } From dd964fcc02c934c7e0a7935badf1c811b6e42272 Mon Sep 17 00:00:00 2001 From: "wangxg922@chinaunincom.cn" <996108220@qq.com> Date: Sun, 26 Feb 2017 11:19:53 +0800 Subject: [PATCH 211/432] =?UTF-8?q?Signed-off-by:=20996108220@qq.com?= =?UTF-8?q?=EF=BC=8C11=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/996108220/.classpath | 6 + group11/996108220/.gitignore | 1 + group11/996108220/.project | 17 ++ .../src/com/coding/basic/ArrayList.java | 124 ++++++++++++ .../src/com/coding/basic/BinaryTree.java | 129 +++++++++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 176 ++++++++++++++++++ .../996108220/src/com/coding/basic/List.java | 9 + .../996108220/src/com/coding/basic/Queue.java | 20 ++ .../996108220/src/com/coding/basic/Stack.java | 24 +++ 10 files changed, 513 insertions(+) create mode 100644 group11/996108220/.classpath create mode 100644 group11/996108220/.gitignore create mode 100644 group11/996108220/.project create mode 100644 group11/996108220/src/com/coding/basic/ArrayList.java create mode 100644 group11/996108220/src/com/coding/basic/BinaryTree.java create mode 100644 group11/996108220/src/com/coding/basic/Iterator.java create mode 100644 group11/996108220/src/com/coding/basic/LinkedList.java create mode 100644 group11/996108220/src/com/coding/basic/List.java create mode 100644 group11/996108220/src/com/coding/basic/Queue.java create mode 100644 group11/996108220/src/com/coding/basic/Stack.java diff --git a/group11/996108220/.classpath b/group11/996108220/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group11/996108220/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group11/996108220/.gitignore b/group11/996108220/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group11/996108220/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group11/996108220/.project b/group11/996108220/.project new file mode 100644 index 0000000000..fe4e769dd4 --- /dev/null +++ b/group11/996108220/.project @@ -0,0 +1,17 @@ + + + 996108220Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/996108220/src/com/coding/basic/ArrayList.java b/group11/996108220/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6fe727645b --- /dev/null +++ b/group11/996108220/src/com/coding/basic/ArrayList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData = new Object[100]; + /** + * 在队尾添加元素 + */ + public void add(Object o){ + if(size+1>elementData.length)this.grow(elementData); + else elementData[size++]=o; + } + /** + * 在index处添加元素,index+1到size-1元素向后移动 + */ + public void add(int index, Object o){ + if(index<0||index>size){ + System.out.println("数组越界"+index); + return; + } + if(size+1>elementData.length)this.grow(elementData); + else { + for(int i=size;i>=index+1;) + { + elementData[i]=elementData[--i]; + } + size++; + elementData[index]=o; + } + + } + /** + * 获得index处的元素elementData[index] + */ + public Object get(int index){ + //TODO越界抛出异常 + if(index<0||index>size){ + System.out.println("数组越界"+index); + return null; + } + else{ + return elementData[index]; + } + } + /** + * 移除index处的元素,将index+1到size-1的元素向前移动 + */ + public Object remove(int index){ + //TODO越界抛出异常 + if(index<0||index>=size){ + System.out.println("数组越界"+index); + return null; + } + else{ + Object value=elementData[index]; + for(int i=index;i= size) + System.out.println("超过size");; + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + System.out.println("超过length"); + cursor = i + 1; + return elementData[i]; + } + + } + public void grow(Object[] elementData2){ + int[] elementData=new int[elementData2.length+elementData2.length/2]; + System.arraycopy(elementData2,0,elementData,0,elementData2.length); + } + /** + * 测试方法 + * @param args + */ + public static void main(String args[]){ + ArrayList list=new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + //list.add(2,0); + //list.remove(list.size-1); + System.out.println(list.size()); + Iterator itr=list.iterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + + } + } + +} + diff --git a/group11/996108220/src/com/coding/basic/BinaryTree.java b/group11/996108220/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..1b6d95b6d1 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +import com.sun.corba.se.impl.orbutil.graph.Node; + +public class BinaryTree { + + private BinaryTreeNode root=null; + private int size=0; + /** + * 插入节点,保持二叉树的性质 + * @param o + */ + public void insert(T o){ + if (size==0) { + root=new BinaryTreeNode(o); + } + else{ + insert(o,root); + } + size++; + } + + private void insert(T o, BinaryTreeNode ptr) { + if ((ptr.right==null&&(ptr.data.compareTo(o)<=0))){ + ptr.right=new BinaryTreeNode(o); + } + else if (ptr.left==null&&(ptr.data.compareTo(o)>0)) { + ptr.left=new BinaryTreeNode(o); + + } + else if (ptr.left!=null&&(ptr.data.compareTo(o)>0)) { + insert(o, ptr.left); + } + else { + insert(o, ptr.right); + } + } + private static class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode(T o) { + this.data=o; + this.left=null; + this.right=null; + } + private BinaryTreeNode() { + + } + } + /** + * 前序遍历 + */ + public void preOrder(BinaryTreeNode Node) + { + if (Node != null) + { + System.out.print(Node.data + " "); + preOrder(Node.left); + preOrder(Node.right); + } + } + + /** + * 中序遍历 + */ + public void midOrder(BinaryTreeNode Node) + { + if (Node != null) + { + midOrder(Node.left); + System.out.print(Node.data + " "); + midOrder(Node.right); + } + } + + /** + * 后序遍历 + */ + public void posOrder(BinaryTreeNode Node) + { + if (Node != null) + { + posOrder(Node.left); + posOrder(Node.right); + System.out.print(Node.data + " "); + } + } + /** + * @param key查找元素 + * @param node + * @return 返回date的node引用 + */ + public BinaryTreeNode searchNode(T key,BinaryTreeNode node) { + if (node!=null) { + if (node.data.compareTo(key)==0) { + return node; + } + else if (node.data.compareTo(key)>0) { + return searchNode(key,node.left); + } + else { + return searchNode(key,node.right); + } + } + else{ + return null; + } + } + + public static void main(String[] args) { + BinaryTree tree=new BinaryTree(); + tree.insert(5); + tree.insert(3); + tree.insert(1); + tree.insert(6); + tree.insert(5); + tree.insert(2); + tree.preOrder(tree.root); + System.out.println(); + tree.posOrder(tree.root); + System.out.println(); + tree.midOrder(tree.root); + System.out.println(tree.searchNode(1, tree.root).data); + + + } + +} diff --git a/group11/996108220/src/com/coding/basic/Iterator.java b/group11/996108220/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coding/basic/LinkedList.java b/group11/996108220/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be0b354bc2 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/LinkedList.java @@ -0,0 +1,176 @@ +package com.coding.basic; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size=0; +/** + * 增加节点 + */ + public void add(Object o){ + if(size==0){ + head=new Node(); + head.data=o; + head.next=null; + size++; + } + else{ + addLast(o); + } + + } +/** + * 在index(0~size)处添加元素 + */ + public void add(int index , Object o){ + + if(index<0||index>size){ + System.out.println("index超出范围"+index); + return; + } + if(index==0)addFirst( o); + else if(index==size)addLast(o); + else{ + Node ptr=head; + for(int i=1;i=size)return null; + else{ + Node ptr=head; + for(int i=0;i=size)return null; + else if(index==0)return removeFirst(); + else if(index==size-1)return removeLast(); + else{ + Node ptr=head; + for(int i=1;i Date: Sun, 26 Feb 2017 11:37:19 +0800 Subject: [PATCH 212/432] add all java --- .../Ven13/coding2017/basic/ArrayList.java | 97 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++++ .../Ven13/coding2017/basic/Iterator.java | 7 + .../Ven13/coding2017/basic/LinkedList.java | 148 ++++++++++++++++++ .../github/Ven13/coding2017/basic/List.java | 9 ++ .../github/Ven13/coding2017/basic/Queue.java | 30 ++++ .../github/Ven13/coding2017/basic/Stack.java | 30 ++++ 7 files changed, 353 insertions(+) create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java create mode 100644 group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..b636dc92fe --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.github.Ven13.coding2017.basic; + +public class ArrayList implements List { + + //���ؼ��ϴ�С + private int size = 0; + + //�ȸ���һ������Ϊ10������ + Object[] elementData = new Object[100]; + + @Override + //��̬����Ԫ�� + public void add(Object o) { + //���ж������Ƿ����� + if(size == elementData.length) { + Object[] newObjects = new Object[elementData.length * 2]; + System.arraycopy(elementData, 0, newObjects, 0, elementData.length); + elementData = newObjects; + } + + //Ϊ�����ӵ�Ԫ��ָ���±� + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + //���ж������Ƿ����� + if(size == elementData.length) { + Object[] newObjects = elementData; + this.elementData = new Object[elementData.length * 2]; + for(int j = 0; j < newObjects.length; j++) { + this.elementData[j] = newObjects[j]; + } + } + + for(int i = size - 1; i >= index; i--) { + elementData[i+1] = elementData[i]; + } + + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index > size) { + return null; + }; + + int moveSize = size - index - 1; + if (moveSize > 0) { + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + } + elementData[--size] = null; + + for(int i = index; i < elementData.length; i++) { + elementData[i] = elementData[i+1]; + } + size--; + + return elementData; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int currentIndex = 0; + + @Override + public boolean hasNext() { + if(currentIndex >= size) return false; + else return true; + } + + @Override + public Object next() { + Object o = elementData[currentIndex]; + currentIndex ++; + return o; + } + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..354b2ba7a0 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.Ven13.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3cf6540e5e --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.Ven13.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..0b605c28ee --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java @@ -0,0 +1,148 @@ +package com.github.Ven13.coding2017.basic; + +public class LinkedList implements List { + + //��ʾ�������ij��� + private int size; + + //������ͷԪ�� + private Node head; + //������βԪ�� + private Node tail; + + //ʹ���ڲ�����ʵ��������ÿһ���ڵ㣬ÿ���ڵ���һ��ָ����һ��Ԫ�ص�next���Լ�������data + private static class Node { + public Object data; + public Node next; + + public Node(Object data) { + this.data = data; + } + } + + //�����Ĺ��췽�� + public LinkedList() { + } + + @Override + public void add(Object o) { + add(size, o); + } + + @Override + public void add(int index, Object o) { + if(index == 0) { + addFirst(o); + } else { + if(index >= size) { + addLast(o); + } else { + Node node = head; + for (int i = 1; i < index; i++) { + head = head.next; + } + Node nextNode = node.next; + Node temp = new Node(o); + node.next = temp; + temp.next = nextNode; + size++; + } + } + } + + //����ǰ�� + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + if(tail == null) { + tail = head; + } + } + + //���Ӻ��� + public void addLast(Object o) { + if(tail == null) { + tail = head = new Node(o); + } else { + Node newNode = new Node(o); + tail.next = newNode; + tail = tail.next; + } + size++; + } + + + @Override + public Object get(int index) { + Node node = head; + for(int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + @Override + public Object remove(int index) { + if(index == 0) { + head.next = head; + return head.data; + } else { + if(index >= size) { + throw new java.util.NoSuchElementException(); + } else { + Node node = head; + for(int i = 1; i < index; i++) { + node = node.next; + } + Node temp = node.next; + node.next = temp.next; + size--; + return node.data; + } + } + + } + + @Override + public int size() { + return size; + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + private Node node = head.next; + + @Override + public boolean hasNext() { + return node != tail; + } + + @Override + public Object next() { + + if(!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Object nextData = node.data; + node = node.next; + return nextData; + } + + } + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java new file mode 100644 index 0000000000..525eb92857 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.Ven13.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java new file mode 100644 index 0000000000..112299b5e9 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + size++; + list.addLast(o); + } + + public Object deQueue(){ + size--; + return list.removeFirst(); + } + + public boolean isEmpty(){ + if(size == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return size; + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java new file mode 100644 index 0000000000..c0a2658bb3 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object pop(){ + Object o = null; + if(elementData.size() > 0) { + o = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + } + return o; + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } + +} From cb4ca62ace6022e547fd295eda8abee74cfb54e9 Mon Sep 17 00:00:00 2001 From: Rong Huang <851113375@qq.com> Date: Sun, 26 Feb 2017 11:38:34 +0800 Subject: [PATCH 213/432] =?UTF-8?q?Revert=20"=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwjcc969/coding2017/basic/ArrayList.java | 62 ---------- .../coding2017/basic/BinaryTreeNode.java | 32 ----- .../hwjcc969/coding2017/basic/Iterator.java | 7 -- .../hwjcc969/coding2017/basic/LinkedList.java | 117 ------------------ .../hwjcc969/coding2017/basic/List.java | 9 -- .../hwjcc969/coding2017/basic/Queue.java | 27 ---- .../hwjcc969/coding2017/basic/Stack.java | 31 ----- 7 files changed, 285 deletions(-) delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java delete mode 100644 coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java deleted file mode 100644 index c2acf20e4d..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -import javax.lang.model.element.Element; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - if (o == null){ - throw new NullPointerException("空对象"); - } - if (size >= 100){ - throw new RuntimeException("越界"); - } - elementData[size] = o; - size++; - } - public void add(int index, Object o){ - if (o == null){ - throw new NullPointerException("空对象"); - } - if (index >= 99){ - throw new RuntimeException("越界"); - } - for (int i = size; i >= index; i--) { - elementData[i + 1] = elementData[i]; - } - elementData[index] = o; - size++; - } - - public Object get(int index){ - if (index >= 100 || index < 0){ - throw new RuntimeException("越界"); - } - return elementData[index]; - } - - public Object remove(int index){ - if (index >= 100 || index < 0){ - throw new RuntimeException("越界"); - } - Object o = elementData[index]; - for (int i = index; i < size - 1; i++) { - elementData[i] = elementData[i + 1]; - } - size--; - return null; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index 0381f88c69..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java deleted file mode 100644 index 0e8394844b..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java deleted file mode 100644 index d000967c19..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class LinkedList implements List { - - private Node head; - private int size; - - public void add(Object o){ - Node newNode = new Node(o); - if (head == null) { - head = newNode; - } - else{ - Node last = head.next; - while (last != null){ - last = last.next; - } - last = newNode; - } - size++; - - } - public void add(int index , Object o){ - Node dummy = new Node(); - dummy.next = head; - Node pre = dummy; - while (index > 0) { - pre = pre.next; - } - Node cur = new Node(o); - cur.next = pre.next; - pre.next = cur; - size++; - } - public Object get(int index){ - Node cur = head; - while (index > 0 && cur != null) { - cur = cur.next; - } - return cur; - } - public Object remove(int index){ - Node dummy = new Node(); - dummy.next = head; - Node pre = dummy; - while (index > 0) { - pre = pre.next; - } - Node cur = pre.next; - Node next = pre.next.next; - pre.next = next; - size--; - return cur; - - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node firstNode = new Node(o); - firstNode.next = head; - head = firstNode; - } - public void addLast(Object o){ - Node pre = new Node(); - while (pre.next != null){ - pre = pre.next; - } - Node lastNode = new Node(o); - pre.next = lastNode; - } - public Object removeFirst(){ - Node dummy = new Node(); - dummy.next = head; - dummy.next = head.next; - return dummy.next; - } - public Object removeLast(){ - Node pre = new Node(); - while (pre.next.next != null) { - pre = pre.next; - } - Node lastNode = pre.next.next; - pre.next = null; - return lastNode; - - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - public Node(Object o) { - // TODO Auto-generated constructor stub - } - public Node() { - // TODO Auto-generated constructor stub - } - Object data; - Node next; - - } - - - public boolean isEmpty() { - // TODO Auto-generated method stub - if (size() == 0){ - return false; - } - return true; - } - - -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java deleted file mode 100644 index 56d67bdf29..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java deleted file mode 100644 index f7cb7b1ad4..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Queue.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class Queue { - private LinkedList list = new LinkedList(); - public void enQueue(Object o){ - list.addLast(o); - } - - public Object deQueue(){ - if (!list.isEmpty()){ - return list.removeFirst(); - } - return "空队列"; - - } - - public boolean isEmpty(){ - if (list.size() == 0){ - return false; - } - return true; - } - - public int size(){ - return list.size(); - } -} diff --git a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java b/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java deleted file mode 100644 index a4229a7d12..0000000000 --- a/coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/basic/Stack.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.hwjcc969.coding2017.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size = 0; - - public void push(Object o){ - if (elementData.size() <= 99) - elementData.add(o); - size++; - - } - - public Object pop(){ - size--; - return elementData.remove(elementData.size() - 1); - - } - - public Object peek(){ - return elementData.get(elementData.size() - 1); - } - public boolean isEmpty(){ - if (size == 0) - return false; - return true; - } - public int size(){ - return size; - } -} From fed426b4bd92f457d617fe26c54d30b720bd3ae1 Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sun, 26 Feb 2017 11:48:13 +0800 Subject: [PATCH 214/432] delete all files --- group08/769638826/.gitignore | 5 - group08/769638826/769638826.md | 2 - group08/769638826/pom.xml | 37 ---- .../main/java/com/coding/basic/ArrayList.java | 111 ---------- .../main/java/com/coding/basic/Iterator.java | 13 -- .../java/com/coding/basic/LinkedList.java | 190 ------------------ .../src/main/java/com/coding/basic/List.java | 19 -- .../src/main/java/com/coding/basic/Queue.java | 26 --- .../src/main/java/com/coding/basic/Stack.java | 32 --- .../java/com/coding/basic/ArrayListTest.java | 74 ------- .../java/com/coding/basic/LinkedListTest.java | 10 - .../test/java/com/coding/basic/QueueTest.java | 10 - .../test/java/com/coding/basic/StackTest.java | 9 - 13 files changed, 538 deletions(-) delete mode 100644 group08/769638826/.gitignore delete mode 100644 group08/769638826/769638826.md delete mode 100644 group08/769638826/pom.xml delete mode 100644 group08/769638826/src/main/java/com/coding/basic/ArrayList.java delete mode 100644 group08/769638826/src/main/java/com/coding/basic/Iterator.java delete mode 100644 group08/769638826/src/main/java/com/coding/basic/LinkedList.java delete mode 100644 group08/769638826/src/main/java/com/coding/basic/List.java delete mode 100644 group08/769638826/src/main/java/com/coding/basic/Queue.java delete mode 100644 group08/769638826/src/main/java/com/coding/basic/Stack.java delete mode 100644 group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java delete mode 100644 group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java delete mode 100644 group08/769638826/src/test/java/com/coding/basic/QueueTest.java delete mode 100644 group08/769638826/src/test/java/com/coding/basic/StackTest.java diff --git a/group08/769638826/.gitignore b/group08/769638826/.gitignore deleted file mode 100644 index 5132e3c66b..0000000000 --- a/group08/769638826/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# Created by .ignore support plugin (hsz.mobi) -.gitignore -.idea/ -coding.iml -target/ \ No newline at end of file diff --git a/group08/769638826/769638826.md b/group08/769638826/769638826.md deleted file mode 100644 index 4573496090..0000000000 --- a/group08/769638826/769638826.md +++ /dev/null @@ -1,2 +0,0 @@ -###自我介绍 - diff --git a/group08/769638826/pom.xml b/group08/769638826/pom.xml deleted file mode 100644 index 68c1b3095d..0000000000 --- a/group08/769638826/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - 4.0.0 - - com.coding2017 - coding - 1.0-SNAPSHOT - - - UTF-8 - 1.7 - - - - - junit - junit - 4.12 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${java.version} - ${java.version} - ${project.build.buildEncoding} - - - - - \ No newline at end of file diff --git a/group08/769638826/src/main/java/com/coding/basic/ArrayList.java b/group08/769638826/src/main/java/com/coding/basic/ArrayList.java deleted file mode 100644 index 1e6ccfcfcb..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.coding.basic; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -/** - * Created by huitailang on 17/2/25. - * - * @author zhangkun - * @date 2017年02月25日13:23:30 - */ -public class ArrayList implements List { - private int size = 0; - private static final int DEFAULT_SIZE = 16; - private Object[] elementData = null; - private int index; - - public ArrayList() { - elementData = new Object[DEFAULT_SIZE]; - } - - public ArrayList(final int size) { - elementData = new Object[size]; - } - - public void add(Object o) { - //如果当前元素个数大于数组长度的2/3 - if (size() > elementData.length * 2 / 3) { - raiseArray(); - } - - elementData[index++] = o; - size++; - } - - public void add(int index, Object o) { - checkParam(index); - - //如果当前元素个数大于数组长度的2/3 - if (size() > elementData.length * 2 / 3) { - raiseArray(); - } - - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkParam(index); - - return elementData[index]; - } - - public Object remove(int index) { - checkParam(index); - - Object o = elementData[index]; - elementData[index] = null; - size--; - return o; - } - - private void raiseArray() { - Object[] newElementData = Arrays.copyOf(elementData, size() * 2); - elementData = newElementData; - } - - private void checkParam(int index) { - if (index < 0 || index > elementData.length - 1) { - throw new IndexOutOfBoundsException(); - } - } - - public int size() { - return size; - } - - public Iterator iterator() { - return new ListIterator(); - } - - private class ListIterator implements Iterator{ - int cursor; - - @Override - public boolean hasNext() { - return cursor != size; - } - - public void remove(){ - throw new UnsupportedOperationException(); - } - - @Override - public Object next() { - if(!hasNext()) { - throw new NoSuchElementException(); - } - - int i = cursor; - if (i >= size) - throw new NoSuchElementException(); - - Object[] elementData = ArrayList.this.elementData; - - cursor = i + 1; - - return elementData[i]; - } - } -} diff --git a/group08/769638826/src/main/java/com/coding/basic/Iterator.java b/group08/769638826/src/main/java/com/coding/basic/Iterator.java deleted file mode 100644 index ce3da0d118..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/Iterator.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * - * @author zhangkun - * @date 2017年02月25日16:25:42 - */ -public interface Iterator { - public boolean hasNext(); - - public Object next(); -} diff --git a/group08/769638826/src/main/java/com/coding/basic/LinkedList.java b/group08/769638826/src/main/java/com/coding/basic/LinkedList.java deleted file mode 100644 index 981822c43e..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,190 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -/** - * Created by huitailang on 17/2/25. - * - * @author zhangkun - * @date 2017年02月25日13:57:58 - */ -public class LinkedList implements List { - private Node head; - private int size; - - public LinkedList() { - head = null; - size = 0; - } - - @Override - public void add(Object o) { - if (head == null) { - Node newNode = new Node(); - newNode.data = o; - head = newNode; - } - - Node oldhead = head; - head = new Node(); - head.data = o; - head.next = oldhead; - size++; - } - - @Override - public void add(int index, Object o) { - Node newNode = new Node(); - newNode.data = o; - - if (head == null) { - head = newNode; - } - - if (index < 1 || index > size + 1) { - throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1); - } - - if (index == 1) { - newNode.next = head; - } else { - Node currentNode = head; - int count = 1; - while (count < index - 1) { - count++; - currentNode = currentNode.next; - } - newNode.next = currentNode.next; - currentNode.next = newNode; - } - } - - @Override - public Object get(int index) { - if (head == null) { - return null; - } - - if (index == 1) { - return head.next.data; - } else { - Node currentNode = head; - int count = 1; - while (count < index - 1) { - count++; - currentNode = currentNode.next; - } - - return currentNode.next.data; - } - } - - @Override - public Object remove(int index) { - Object result = null; - - if (index < 1 || index > size) { - throw new IllegalArgumentException("invalid index, it's should be 1 and " + size); - } - - if (index == 1) { - Node currentNode = head.next; - head = null; - return currentNode; - } else { - Node prevNode = head; - - int count = 1; - while (count < index - 1) { - prevNode = prevNode.next; - count++; - } - Node currentNode = prevNode.next; - prevNode.next = currentNode.next; - result = currentNode.data; - currentNode = null; - } - - return result; - } - - @Override - public int size() { - return size; - } - - public void addFirst(Object o) { - add(1, o); - } - - public void addLast(Object o) { - add(size + 1, o); - } - - public Object removeFirst() { - return remove(1); - } - - public Object removeLast() { - return remove(size); - } - - public Iterator iterator() { - return new ListIterator(); - } - - public void print() { - if (head == null) { - System.out.println("No elements in the list!"); - } - - Node currentNode = head; - while (currentNode != null) { - System.out.println(currentNode.data + "->"); - currentNode = currentNode.next; - } - - System.out.println(); - } - - public int length() { - int count = 0; - - Node currentNode = head; - while (currentNode != null) { - count++; - currentNode = currentNode.next; - } - - return count; - } - - private static class Node { - Object data; - Node next; - } - - private class ListIterator implements Iterator { - private Node current = head; - - @Override - public boolean hasNext() { - return current != null; - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public Object next() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - - Object o = current.data; - current = current.next; - return o; - } - } -} diff --git a/group08/769638826/src/main/java/com/coding/basic/List.java b/group08/769638826/src/main/java/com/coding/basic/List.java deleted file mode 100644 index e9eba47da6..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/List.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * - * @author zhangkun - * @date 2017年02月25日13:54:33 - */ -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/group08/769638826/src/main/java/com/coding/basic/Queue.java b/group08/769638826/src/main/java/com/coding/basic/Queue.java deleted file mode 100644 index abb7e57112..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/Queue.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * @author zhangkun - * @date 2017年02月25日17:40:02 - */ -public class Queue { - private ArrayList elementData = new ArrayList(); - - public void enQueue(Object o){ - elementData.add(o); - } - - public Object deQueue(){ - return elementData.get(size()); - } - - public boolean isEmpty(){ - return elementData.size() != 0; - } - - public int size(){ - return elementData.size(); - } -} diff --git a/group08/769638826/src/main/java/com/coding/basic/Stack.java b/group08/769638826/src/main/java/com/coding/basic/Stack.java deleted file mode 100644 index 46f80bc25d..0000000000 --- a/group08/769638826/src/main/java/com/coding/basic/Stack.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * @author zhangkun - * @date 2017年02月25日17:34:22 - */ -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - Object o = elementData.get(1); - elementData.remove(1); - return o; - } - - public Object peek(){ - return elementData.get(1); - } - - public boolean isEmpty(){ - return elementData.size() != 0; - } - - public int size(){ - return elementData.size(); - } -} diff --git a/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java b/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java deleted file mode 100644 index 199e3d14cb..0000000000 --- a/group08/769638826/src/test/java/com/coding/basic/ArrayListTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * Created by huitailang on 17/2/25. - * test arraylist - */ -public class ArrayListTest { - ArrayList arrayList = null; - - @Before - public void setUp() { - arrayList = new ArrayList(); - } - - @Test - public void testArrayLength() { - int[] array = new int[10]; - Assert.assertEquals(10, array.length); - } - - @Test - public void testAddElement() { - arrayList.add(11); - Assert.assertEquals(11, arrayList.get(0)); - printElementSize(arrayList); - - for (int i = 0; i < 18; i++) { - - } - } - - @Test - public void testAriseArray() { - for (int i = 0; i < 18; i++) { - arrayList.add(i + 1); - } - - Assert.assertEquals(18, arrayList.size()); - - for (int i = 0; i < 18; i++) { - System.out.println(arrayList.get(i)); - } - } - - @Test - public void testRemoveElement() { - for (int i = 0; i < 18; i++) { - arrayList.add(i + 1); - } - - Assert.assertEquals(18, arrayList.size()); - - arrayList.remove(17); - - Assert.assertEquals(17, arrayList.size()); - - for (int i = 0; i < 18; i++) { - System.out.println(arrayList.get(i)); - } - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testInValidGet() { - arrayList.get(19); - } - - private void printElementSize(ArrayList arrayList) { - System.out.println("array size => " + arrayList.size()); - } -} diff --git a/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java b/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java deleted file mode 100644 index 786e5d93a7..0000000000 --- a/group08/769638826/src/test/java/com/coding/basic/LinkedListTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * @author zhangkun - * @date 2017年02月25日17:33:40 - */ -public class LinkedListTest { - -} diff --git a/group08/769638826/src/test/java/com/coding/basic/QueueTest.java b/group08/769638826/src/test/java/com/coding/basic/QueueTest.java deleted file mode 100644 index 83ee1ff30a..0000000000 --- a/group08/769638826/src/test/java/com/coding/basic/QueueTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * @author zhangkun - * @date 2017年02月25日17:44:48 - */ -public class QueueTest { - -} diff --git a/group08/769638826/src/test/java/com/coding/basic/StackTest.java b/group08/769638826/src/test/java/com/coding/basic/StackTest.java deleted file mode 100644 index 5380bbaa0c..0000000000 --- a/group08/769638826/src/test/java/com/coding/basic/StackTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -/** - * Created by huitailang on 17/2/25. - * @author zhangkun - * @date 2017年02月25日17:44:32 - */ -public class StackTest { -} From cde441677d7fac07aa43c5ce8161e0c8ff2f24c6 Mon Sep 17 00:00:00 2001 From: BlankKelly Date: Sun, 26 Feb 2017 11:53:02 +0800 Subject: [PATCH 215/432] commit 02-27 homework --- group08/769638826/2-27/.DS_Store | Bin 0 -> 6148 bytes group08/769638826/2-27/ArrayList.java | 111 ++++++++++++ group08/769638826/2-27/ArrayListTest.java | 74 ++++++++ group08/769638826/2-27/Iterator.java | 13 ++ group08/769638826/2-27/LinkedList.java | 190 +++++++++++++++++++++ group08/769638826/2-27/LinkedListTest.java | 10 ++ group08/769638826/2-27/List.java | 19 +++ group08/769638826/2-27/Queue.java | 26 +++ group08/769638826/2-27/QueueTest.java | 10 ++ group08/769638826/2-27/Stack.java | 32 ++++ group08/769638826/2-27/StackTest.java | 9 + 11 files changed, 494 insertions(+) create mode 100644 group08/769638826/2-27/.DS_Store create mode 100644 group08/769638826/2-27/ArrayList.java create mode 100644 group08/769638826/2-27/ArrayListTest.java create mode 100644 group08/769638826/2-27/Iterator.java create mode 100644 group08/769638826/2-27/LinkedList.java create mode 100644 group08/769638826/2-27/LinkedListTest.java create mode 100644 group08/769638826/2-27/List.java create mode 100644 group08/769638826/2-27/Queue.java create mode 100644 group08/769638826/2-27/QueueTest.java create mode 100644 group08/769638826/2-27/Stack.java create mode 100644 group08/769638826/2-27/StackTest.java diff --git a/group08/769638826/2-27/.DS_Store b/group08/769638826/2-27/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7b359c155c5a8f884b8bf3a05563b34baeafff37 GIT binary patch literal 6148 zcmeHK!A=4(5Pek+h!KfiJnqpGiGQ#XG$HY#_yGcPP>Cc8Cf@cl{3t)bztK0-iFRel z-i(?VGJV~)uhTxV(=C9|TV5Xl9RN+bV57yN!lYk(#Wt)(ctPiQ#u;w#hy}VUqrJfu za0Pyw0`l&*af}E4Mo8b^TRuNwG?>jN4)!XtO0u!+)GR?2UN}xF35XH z=Gei28Dpv|SYS@VM+rp6gjxeE4>*9&B<#=D5x_IQ4;&CdsKcE25Y_aBc zL@!+dSHKncR6z1WMi++c(oXjvcEF!Xp79y1xsll!oA=25N z*|^-Wh#2VzcKHYvIoJ&)Xmrk>>2QQRqL;3KD^ONoGd_0Y{6ATL|1XpL$rW$~{*?lu z(I57E+>)KGE1Q$EHlttB#bjI&Q7J6!IM#|B#RqgX_GMBb=7vQ?w$S`TKxOd475GsF FJ^ elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index++] = o; + size++; + } + + public void add(int index, Object o) { + checkParam(index); + + //如果当前元素个数大于数组长度的2/3 + if (size() > elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkParam(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkParam(index); + + Object o = elementData[index]; + elementData[index] = null; + size--; + return o; + } + + private void raiseArray() { + Object[] newElementData = Arrays.copyOf(elementData, size() * 2); + elementData = newElementData; + } + + private void checkParam(int index) { + if (index < 0 || index > elementData.length - 1) { + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator{ + int cursor; + + @Override + public boolean hasNext() { + return cursor != size; + } + + public void remove(){ + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + + Object[] elementData = ArrayList.this.elementData; + + cursor = i + 1; + + return elementData[i]; + } + } +} diff --git a/group08/769638826/2-27/ArrayListTest.java b/group08/769638826/2-27/ArrayListTest.java new file mode 100644 index 0000000000..199e3d14cb --- /dev/null +++ b/group08/769638826/2-27/ArrayListTest.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by huitailang on 17/2/25. + * test arraylist + */ +public class ArrayListTest { + ArrayList arrayList = null; + + @Before + public void setUp() { + arrayList = new ArrayList(); + } + + @Test + public void testArrayLength() { + int[] array = new int[10]; + Assert.assertEquals(10, array.length); + } + + @Test + public void testAddElement() { + arrayList.add(11); + Assert.assertEquals(11, arrayList.get(0)); + printElementSize(arrayList); + + for (int i = 0; i < 18; i++) { + + } + } + + @Test + public void testAriseArray() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test + public void testRemoveElement() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + arrayList.remove(17); + + Assert.assertEquals(17, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testInValidGet() { + arrayList.get(19); + } + + private void printElementSize(ArrayList arrayList) { + System.out.println("array size => " + arrayList.size()); + } +} diff --git a/group08/769638826/2-27/Iterator.java b/group08/769638826/2-27/Iterator.java new file mode 100644 index 0000000000..ce3da0d118 --- /dev/null +++ b/group08/769638826/2-27/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日16:25:42 + */ +public interface Iterator { + public boolean hasNext(); + + public Object next(); +} diff --git a/group08/769638826/2-27/LinkedList.java b/group08/769638826/2-27/LinkedList.java new file mode 100644 index 0000000000..981822c43e --- /dev/null +++ b/group08/769638826/2-27/LinkedList.java @@ -0,0 +1,190 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:57:58 + */ +public class LinkedList implements List { + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + @Override + public void add(Object o) { + if (head == null) { + Node newNode = new Node(); + newNode.data = o; + head = newNode; + } + + Node oldhead = head; + head = new Node(); + head.data = o; + head.next = oldhead; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + newNode.data = o; + + if (head == null) { + head = newNode; + } + + if (index < 1 || index > size + 1) { + throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1); + } + + if (index == 1) { + newNode.next = head; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + newNode.next = currentNode.next; + currentNode.next = newNode; + } + } + + @Override + public Object get(int index) { + if (head == null) { + return null; + } + + if (index == 1) { + return head.next.data; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + + return currentNode.next.data; + } + } + + @Override + public Object remove(int index) { + Object result = null; + + if (index < 1 || index > size) { + throw new IllegalArgumentException("invalid index, it's should be 1 and " + size); + } + + if (index == 1) { + Node currentNode = head.next; + head = null; + return currentNode; + } else { + Node prevNode = head; + + int count = 1; + while (count < index - 1) { + prevNode = prevNode.next; + count++; + } + Node currentNode = prevNode.next; + prevNode.next = currentNode.next; + result = currentNode.data; + currentNode = null; + } + + return result; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + add(1, o); + } + + public void addLast(Object o) { + add(size + 1, o); + } + + public Object removeFirst() { + return remove(1); + } + + public Object removeLast() { + return remove(size); + } + + public Iterator iterator() { + return new ListIterator(); + } + + public void print() { + if (head == null) { + System.out.println("No elements in the list!"); + } + + Node currentNode = head; + while (currentNode != null) { + System.out.println(currentNode.data + "->"); + currentNode = currentNode.next; + } + + System.out.println(); + } + + public int length() { + int count = 0; + + Node currentNode = head; + while (currentNode != null) { + count++; + currentNode = currentNode.next; + } + + return count; + } + + private static class Node { + Object data; + Node next; + } + + private class ListIterator implements Iterator { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + Object o = current.data; + current = current.next; + return o; + } + } +} diff --git a/group08/769638826/2-27/LinkedListTest.java b/group08/769638826/2-27/LinkedListTest.java new file mode 100644 index 0000000000..786e5d93a7 --- /dev/null +++ b/group08/769638826/2-27/LinkedListTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:33:40 + */ +public class LinkedListTest { + +} diff --git a/group08/769638826/2-27/List.java b/group08/769638826/2-27/List.java new file mode 100644 index 0000000000..e9eba47da6 --- /dev/null +++ b/group08/769638826/2-27/List.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:54:33 + */ +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/group08/769638826/2-27/Queue.java b/group08/769638826/2-27/Queue.java new file mode 100644 index 0000000000..abb7e57112 --- /dev/null +++ b/group08/769638826/2-27/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:40:02 + */ +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.get(size()); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/2-27/QueueTest.java b/group08/769638826/2-27/QueueTest.java new file mode 100644 index 0000000000..83ee1ff30a --- /dev/null +++ b/group08/769638826/2-27/QueueTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:48 + */ +public class QueueTest { + +} diff --git a/group08/769638826/2-27/Stack.java b/group08/769638826/2-27/Stack.java new file mode 100644 index 0000000000..46f80bc25d --- /dev/null +++ b/group08/769638826/2-27/Stack.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:34:22 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.get(1); + elementData.remove(1); + return o; + } + + public Object peek(){ + return elementData.get(1); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/2-27/StackTest.java b/group08/769638826/2-27/StackTest.java new file mode 100644 index 0000000000..5380bbaa0c --- /dev/null +++ b/group08/769638826/2-27/StackTest.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:32 + */ +public class StackTest { +} From a676094b11a9d269d3929bda1f76e4ad6fbccb79 Mon Sep 17 00:00:00 2001 From: xdx54321 <20409287@qq.com> Date: Sun, 26 Feb 2017 11:54:28 +0800 Subject: [PATCH 216/432] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 454 +++++++++--------- .../java/xdx/homework/first/LinkedList.java | 245 +++++++++- .../xdx/homework/first/SortBinaryTree.java | 7 - .../xdx/homework/first/LinkedListTest.java | 222 +++++++++ 4 files changed, 683 insertions(+), 245 deletions(-) delete mode 100644 group07/20409287/src/main/java/xdx/homework/first/SortBinaryTree.java create mode 100644 group07/20409287/src/test/xdx/homework/first/LinkedListTest.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1a4c6f97dc..6a564d2e9c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,10 @@ + + + @@ -23,48 +26,11 @@ - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -73,8 +39,8 @@ - - + + @@ -83,48 +49,72 @@ - - - + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + - + - + @@ -151,7 +141,6 @@ @@ -216,7 +210,6 @@ - @@ -570,6 +563,7 @@ + @@ -590,11 +584,12 @@ + - - + + - + - + - @@ -657,8 +652,8 @@ - + - @@ -683,8 +678,8 @@ - + - @@ -709,8 +704,8 @@ - - - - - + + + + + - - - - - + + + + + @@ -1172,44 +1167,45 @@ - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - @@ -1217,12 +1213,13 @@ + + - @@ -1231,13 +1228,12 @@ - + - @@ -1255,7 +1251,7 @@ - @@ -1264,38 +1260,6 @@