From b8d284ea10ac280ec72450e9e28336757c0cef30 Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Thu, 23 Feb 2017 14:04:59 +0800 Subject: [PATCH 01/22] 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 2a62e4b39dc7b1743ee24536c54f5ef53e33e881 Mon Sep 17 00:00:00 2001 From: byhieg Date: Thu, 23 Feb 2017 14:49:08 +0800 Subject: [PATCH 02/22] =?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 5e07f16a4eb16d907ad9f21e7a29f4e158b44e0d Mon Sep 17 00:00:00 2001 From: dream <763878069@qq.coom> Date: Fri, 24 Feb 2017 15:52:48 +0800 Subject: [PATCH 03/22] 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 58f30c7a4e9671735bebe7a7b510eec70329df07 Mon Sep 17 00:00:00 2001 From: khalil2333 Date: Fri, 24 Feb 2017 17:52:51 +0800 Subject: [PATCH 04/22] 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 64878674b8fa3ebdc1dc74353121f3d6c8c27b83 Mon Sep 17 00:00:00 2001 From: longcloud Date: Fri, 24 Feb 2017 23:24:00 +0800 Subject: [PATCH 05/22] 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 3ba0f68994bd531f5f189afb26cb917dc23149af Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 11:43:25 +0800 Subject: [PATCH 06/22] 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 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 07/22] =?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 43fc71637bfac0b55d6afe08a8f1eca5131bee79 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 21:27:23 +0800 Subject: [PATCH 08/22] 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 c39f7f93c2d4a957bc7d87efa3ae76edc275fdd2 Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 21:32:59 +0800 Subject: [PATCH 09/22] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayList,LinkedList,St?= =?UTF-8?q?ack,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 10/22] 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 5918d329620437a1138bf5de5fdabdc5034b8aa1 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 22:17:02 +0800 Subject: [PATCH 11/22] 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 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 12/22] 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 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 13/22] 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 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 14/22] 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 15/22] 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 288a420ecd1389488177c316fe42078ee3ed9327 Mon Sep 17 00:00:00 2001 From: wangmeng Date: Sun, 26 Feb 2017 04:48:03 +0800 Subject: [PATCH 16/22] =?UTF-8?q?1,=20=E4=B8=BA=E5=88=98=E8=80=81=E5=B8=88?= =?UTF-8?q?=E4=B8=B2=E8=AE=B2=E7=9A=84.gitignore=E6=B7=BB=E5=8A=A0.idea?= =?UTF-8?q?=E7=9A=84=E8=BF=87=E6=BB=A4=E8=A7=84=E5=88=99=E3=80=82=202?= =?UTF-8?q?=EF=BC=8C=20=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A=E4=B8=AD?= =?UTF-8?q?=E9=9B=86=E4=B8=AD=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E7=9A=84?= =?UTF-8?q?=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 9140cf594cd01f98c512ffb646f5c8540d6538ab Mon Sep 17 00:00:00 2001 From: wangmeng Date: Sun, 26 Feb 2017 17:18:33 +0800 Subject: [PATCH 17/22] =?UTF-8?q?=E8=BF=98=E5=8E=9F.gitignore=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index aefc6cd3ea..57c35943b8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,4 @@ hs_err_pid* #ide config .metadata -.recommenders - -#idea config -.idea -.idea/* +.recommenders \ No newline at end of file From a20b4674f2f9cb4312d37a264a719058cb66adfd Mon Sep 17 00:00:00 2001 From: wangmeng Date: Sun, 26 Feb 2017 17:35:30 +0800 Subject: [PATCH 18/22] =?UTF-8?q?=E8=BF=98=E5=8E=9F.gitignore=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 57c35943b8..ec55baf87d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ hs_err_pid* #ide config .metadata -.recommenders \ No newline at end of file +.recommenders From 5edf94a8d7621323049a3d88bf8541d974b8d5af Mon Sep 17 00:00:00 2001 From: fumingbo1 Date: Sun, 26 Feb 2017 18:42:18 +0800 Subject: [PATCH 19/22] test --- group03/2864885311/DS/.classpath | 8 +++ group03/2864885311/DS/.gitignore | 1 + group03/2864885311/DS/.project | 17 +++++ .../DS/.settings/org.eclipse.jdt.core.prefs | 11 +++ .../DS/src/com/coding/basic/ArrayList.java | 68 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++++++ .../DS/src/com/coding/basic/Iterator.java | 7 ++ .../DS/src/com/coding/basic/LinkedList.java | 46 +++++++++++++ .../DS/src/com/coding/basic/List.java | 10 +++ .../DS/src/com/coding/basic/Queue.java | 19 ++++++ .../DS/src/com/coding/basic/Stack.java | 22 ++++++ 11 files changed, 241 insertions(+) create mode 100644 group03/2864885311/DS/.classpath create mode 100644 group03/2864885311/DS/.gitignore create mode 100644 group03/2864885311/DS/.project create mode 100644 group03/2864885311/DS/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/2864885311/DS/src/com/coding/basic/ArrayList.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/Iterator.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/LinkedList.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/List.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/Queue.java create mode 100644 group03/2864885311/DS/src/com/coding/basic/Stack.java diff --git a/group03/2864885311/DS/.classpath b/group03/2864885311/DS/.classpath new file mode 100644 index 0000000000..6177796265 --- /dev/null +++ b/group03/2864885311/DS/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group03/2864885311/DS/.gitignore b/group03/2864885311/DS/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/2864885311/DS/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/2864885311/DS/.project b/group03/2864885311/DS/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group03/2864885311/DS/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/2864885311/DS/.settings/org.eclipse.jdt.core.prefs b/group03/2864885311/DS/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/2864885311/DS/.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/2864885311/DS/src/com/coding/basic/ArrayList.java b/group03/2864885311/DS/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..31bc2649db --- /dev/null +++ b/group03/2864885311/DS/src/com/coding/basic/ArrayList.java @@ -0,0 +1,68 @@ +package com.coding.basic; +//import java.util.Iterator; + +//import java.util.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //int int_inarry = 10; + grow(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; + } + + private void grow(Object nbradd){ + + if (this.size>this.elementData.length){ + Object[] arrayRefVar = new Object[this.elementData.length+1]; + growcopy(nbradd,arrayRefVar); + }else{ + Object[] arrayRefVar = new Object[this.elementData.length]; + growcopy(nbradd,arrayRefVar); + } + } + private void growcopy(Object nbraddcopy,Object[] arrayRefVarcopy){ + System.arraycopy(this.elementData, 0, arrayRefVarcopy, 0, this.elementData.length); + this.elementData[0]=nbraddcopy; + System.arraycopy(arrayRefVarcopy, 0, this.elementData, 1, this.size+1); + this.size++; + } + private void instrgrow(int nbrindex,Object[] arraynbo,Object ino){ + + //Object[] arrayRefVar2 = new Object[nbrindex]; + Object[] arrayRefVar3 = new Object[this.size-nbrindex]; + + + System.arraycopy(this.elementData, nbrindex, arrayRefVar3, nbrindex, this.size); + this.elementData[nbrindex]=ino; + System.arraycopy(arrayRefVar3, 0, this.elementData, nbrindex+1, this.size); + + + } +} diff --git a/group03/2864885311/DS/src/com/coding/basic/BinaryTreeNode.java b/group03/2864885311/DS/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/2864885311/DS/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/2864885311/DS/src/com/coding/basic/Iterator.java b/group03/2864885311/DS/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/2864885311/DS/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/2864885311/DS/src/com/coding/basic/LinkedList.java b/group03/2864885311/DS/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/2864885311/DS/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/2864885311/DS/src/com/coding/basic/List.java b/group03/2864885311/DS/src/com/coding/basic/List.java new file mode 100644 index 0000000000..d47df585d9 --- /dev/null +++ b/group03/2864885311/DS/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/2864885311/DS/src/com/coding/basic/Queue.java b/group03/2864885311/DS/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/2864885311/DS/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/2864885311/DS/src/com/coding/basic/Stack.java b/group03/2864885311/DS/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/2864885311/DS/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 ef92c011001b654290bf367a82c57158c1641336 Mon Sep 17 00:00:00 2001 From: unknown <664269713@qq.com> Date: Sun, 26 Feb 2017 19:29:45 +0800 Subject: [PATCH 20/22] the first homework 2.26 --- group03/664269713/DataStructure/.gitignore | 3 + .../src/com/ace/coding/ArrayList.java | 84 ++++++++++++ .../src/com/ace/coding/BinaryTreeNode.java | 61 +++++++++ .../src/com/ace/coding/Iterator.java | 7 + .../src/com/ace/coding/LinkedList.java | 123 ++++++++++++++++++ .../src/com/ace/coding/List.java | 9 ++ .../src/com/ace/coding/Queue.java | 27 ++++ .../src/com/ace/coding/Stack.java | 36 +++++ 8 files changed, 350 insertions(+) create mode 100644 group03/664269713/DataStructure/.gitignore create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/Iterator.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/List.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/Queue.java create mode 100644 group03/664269713/DataStructure/src/com/ace/coding/Stack.java diff --git a/group03/664269713/DataStructure/.gitignore b/group03/664269713/DataStructure/.gitignore new file mode 100644 index 0000000000..fb921cf6e9 --- /dev/null +++ b/group03/664269713/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/.classpath +/.project \ No newline at end of file diff --git a/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java new file mode 100644 index 0000000000..a529faef7b --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java @@ -0,0 +1,84 @@ +package com.ace.coding; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + checkArrayLength(); + elementData[size++] = o; + } + + private void checkArrayLength(){ + if(elementData.length < size() + 1){ + // expand the origin length of the array + int newLength = size * 2 + 1; + elementData = Arrays.copyOf(elementData, newLength); + } + } + + private void checkIndex(int index){ + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException("Index " + index + " is invalid."); + } + } + + public void add(int index, Object o){ + checkIndex(index); + checkArrayLength(); + 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 obj = elementData[index]; + if(index == size() - 1){ + elementData[index] = null; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); + } + size--; + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; +// return new ListIterator(); + } + + /*private class ListIterator implements Iterator{ + private int index = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return index != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(index >= size){ + throw new IndexOutOfBoundsException("There's no next element."); + } + return elementData[index++]; + } + + }*/ + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java b/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java new file mode 100644 index 0000000000..e6253b221c --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java @@ -0,0 +1,61 @@ +package com.ace.coding; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode rootNode; + + 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(); + newNode.setData(o); + + if(rootNode == null){ + rootNode = newNode; + rootNode.data = data; + left = null; + right = null; + } else { + BinaryTreeNode currentNode = rootNode; + while(true){ + BinaryTreeNode pNode = currentNode; + if((int)newNode.getData() > (int)currentNode.getData()){ + currentNode = currentNode.right; + if(currentNode.right == null){ + pNode.right = newNode; + return newNode; + } + } else { + currentNode = currentNode.left; + if(currentNode.left == null){ + pNode.left = newNode; + return newNode; + } + } + } + + } + return newNode; + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java b/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java new file mode 100644 index 0000000000..09c5442076 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java @@ -0,0 +1,7 @@ +package com.ace.coding; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java b/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java new file mode 100644 index 0000000000..351a6a8277 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java @@ -0,0 +1,123 @@ +package com.ace.coding; + +public class LinkedList implements List { + private Node head = null; + private int size = 0; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + if(head == null){ + head = newNode; + } else { + Node pNode = head; + while(pNode.next != null){ + pNode = pNode.next; + } + pNode.next = newNode; + } + size++; + } + + public void add(int index , Object o){ + checkLinkedListIndex(index); + + Node newNode = new Node(); + newNode.data = o; + Node pNode = getNode(index); + newNode.next = pNode.next; + pNode.next = newNode; + + size++; + } + + private Node getNode(int index){ + Node pNode = head; + for(int i = 0; i < index; i++){ + pNode = pNode.next; + } + return pNode; + } + + public Object get(int index){ + Node pNode = getNode(index); + return pNode.data; + } + public Object remove(int index){ + checkLinkedListIndex(index); + + Node pNode = head; + for(int i = 0; i < index - 1; i++){ + pNode = pNode.next; + } + Node tempNode = getNode(index); + pNode.next = tempNode.next; + size--; + return tempNode.data; + } + + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head = newNode; + size++; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + Node pNode = getNode(size() - 1); + pNode.next = newNode; + size++; + } + public Object removeFirst(){ + Node pNode = head; + head = pNode.next; + size--; + return pNode.data; + } + public Object removeLast(){ + Object obj = remove(size() - 1); + return obj; + } + + public int size(){ + return size; + } + + private void checkLinkedListIndex(int index){ + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException("The index " + index + " is invalid."); + } + } + + public Iterator iterator(){ + return null; +// return new ListIterator(); + } + + /*private class ListIterator implements Iterator{ + private Node pNode = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return pNode.next != null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + Object obj = pNode.data; + pNode = pNode.next; + return obj; + } + + }*/ + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/List.java b/group03/664269713/DataStructure/src/com/ace/coding/List.java new file mode 100644 index 0000000000..33bddf1899 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/List.java @@ -0,0 +1,9 @@ +package com.ace.coding; + +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/664269713/DataStructure/src/com/ace/coding/Queue.java b/group03/664269713/DataStructure/src/com/ace/coding/Queue.java new file mode 100644 index 0000000000..15bcf5d497 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Queue.java @@ -0,0 +1,27 @@ +package com.ace.coding; + +public class Queue { + private ArrayList arrayList = new ArrayList(); + private int size = 0; + + public void enQueue(Object data){ + arrayList.add(size(), data); + size++; + } + + public Object deQueue(){ + if(isEmpty()){ + throw new IndexOutOfBoundsException("The Queue is Empty."); + } + size--; + return arrayList.remove(0); + } + + public boolean isEmpty(){ + return size()>0; + } + + public int size(){ + return size; + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/Stack.java b/group03/664269713/DataStructure/src/com/ace/coding/Stack.java new file mode 100644 index 0000000000..4ef1f68084 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Stack.java @@ -0,0 +1,36 @@ +package com.ace.coding; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + checkStack(); + Object obj = elementData.remove(size()-1); + size--; + return obj; + } + + public Object peek(){ + checkStack(); + return elementData.get(size()-1); + } + + private void checkStack(){ + if(isEmpty()){ + throw new IndexOutOfBoundsException("This stack is empty"); + } + } + + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} From 70235dfb6050684ab2ac205c5517682caa7b565e Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Sun, 26 Feb 2017 19:47:55 +0800 Subject: [PATCH 21/22] basic data structure --- .../src/com/coding/basic/ArrayList.java | 115 ++++++++++---- .../src/com/coding/basic/BinaryTree.java | 132 ++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ---- .../src/com/coding/basic/LinkedList.java | 141 ++++++++++++++---- .../src/com/coding/basic/Queue.java | 45 ++++-- .../src/com/coding/basic/Stack.java | 53 +++++-- .../src/com/coding/test/ArrayListTest.java | 48 ++++++ .../src/com/coding/test/BinaryTreeTest.java | 25 ++++ .../src/com/coding/test/LinkedListTest.java | 44 ++++++ .../src/com/coding/test/QueueTest.java | 23 +++ .../src/com/coding/test/StackTest.java | 24 +++ 11 files changed, 570 insertions(+), 112 deletions(-) create mode 100644 group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java delete mode 100644 group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java create mode 100644 group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java create mode 100644 group03/345943980/2017Learning/src/com/coding/test/LinkedListTest.java create mode 100644 group03/345943980/2017Learning/src/com/coding/test/QueueTest.java create mode 100644 group03/345943980/2017Learning/src/com/coding/test/StackTest.java diff --git a/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java index 57412dcf7f..023a1686ae 100644 --- a/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java +++ b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java @@ -1,32 +1,91 @@ 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){ - - } - 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; - } - + + private int size = 0; // 记录数组当前长度 + + private Object[] elementData = new Object[10]; // 初始长度 + + /* + * (non-Javadoc) + * + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + this.elementData[size++] = o; + } + + /* + * 在指定下标位置插入元素 (non-Javadoc) + * + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + // 1、先要判断index所在处有无值,没有则返回null + Object o = elementData[index]; + if (null == o) + throw new IndexOutOfBoundsException(); + return o; + } + + public Object remove(int index) { + Object oldVal = elementData[index]; // 保留要删除的元素 + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved);// 讲移除位置之后的元素向前 挪动 + } + elementData[--size] = null; // 将数组末尾元素置为空 + return oldVal; + } + + /** + * 获取数组元素个数 + */ + public int size() { + return this.size; + } + + public Object[] grow(Object[] src, int size) { + // Arrays.copyOf(src, src.length+size); + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public Iterator iterator() { + + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor=0; + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + return elementData[cursor++]; + } + + } + } diff --git a/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..2bc37a07e6 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,132 @@ +package com.coding.basic; + +/** + * 二叉树 + * + * @author cm + */ +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o); + if (null == root) { + root = new BinaryTreeNode(o); + } else { + boolean flag = false; + BinaryTreeNode cursorNode = root; + while (!flag) { + if (binaryTreeNode.compareTo(cursorNode) < 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(); + } + } + } + } + return binaryTreeNode; + } + + 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; + } + + private 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(Object o) { + setData(o); + } + + @Override + public int compareTo(BinaryTreeNode binaryTreeNode) { + Integer currVal = (Integer) root.getData(); + Integer compVal = (Integer) binaryTreeNode.getData(); + if (currVal < compVal) + return -1; + else if (currVal == compVal) + return 0; + else + return 1; + } + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index 266eff3d56..0000000000 --- a/group03/345943980/2017Learning/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/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java index 1fd99bf26b..b31c724a2f 100644 --- a/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java +++ b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java @@ -1,46 +1,129 @@ package com.coding.basic; +/** + * 单向链表 + * + * @author Administrator + * + */ public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - + + private Node head = new Node(null, null); + private int size = 0; + + public LinkedList() { + head.next = head; } - public void add(int index , Object o){ - + + public void add(Object o) { + addLast(o); } - public Object get(int index){ - return null; + + public void add(int index, Object o) { + // 1、检查是否在合理范围内 + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node currNode = findNodeByIndex(index); + Node newNode = new Node(o, currNode); + if (index == 0) { // 直接插入到第一个位置 + head = newNode; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = newNode; + } + size++; } - public Object remove(int index){ - return null; + + public Object get(int index) { + return findNodeByIndex(index).data; } - - public int size(){ - return -1; + + public Object remove(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node targetNode = this.findNodeByIndex(index); + Object obj = targetNode.data; + if (index == 0) { + targetNode.data = null; + head = targetNode.next; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = targetNode.next; + } + // targetNode.data = null; + size--; + return obj; } - - public void addFirst(Object o){ - + + public int size() { + return this.size; } - public void addLast(Object o){ - + + public void addFirst(Object o) { + Node nextNode = head; + Node newNode = new Node(o, nextNode); + head = newNode; + size++; } - public Object removeFirst(){ - return null; + + public void addLast(Object o) { + Node subNode = new Node(o, null); + if (size == 0) { + head = subNode; + } else { + Node lastNode = findNodeByIndex(size - 1); + lastNode.next = subNode; + } + size++; } - public Object removeLast(){ - return null; + + public Object removeFirst() { + return this.remove(0); } - public Iterator iterator(){ - return null; + + public Object removeLast() { + return this.remove(size - 1); } - - - private static class Node{ + + private Node findNodeByIndex(int index) { + Node lastNode = head; + for (int i = 0; i < index; i++) { + lastNode = lastNode.next; + } + return lastNode; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { Object data; Node next; - + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private int cursor = 0; + private Node cursorNode = head; + + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + Object obj = cursorNode.data; + cursorNode = cursorNode.next; + cursor++; + return obj; + } + } } diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Queue.java b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java index 08d2d86b14..6abba1993b 100644 --- a/group03/345943980/2017Learning/src/com/coding/basic/Queue.java +++ b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java @@ -1,19 +1,42 @@ package com.coding.basic; +/** + * 队列(堆):特点,先进先出 + * + * @author Administrator + * + */ public class Queue { - - public void enQueue(Object o){ + + private LinkedList linkedList = new LinkedList(); + private int size = 0; + + /** + * 入队列 + * + * @param o + */ + public void enQueue(Object obj) { + linkedList.add(obj); + size++; } - - public Object deQueue(){ - return null; + + /** + * 出队列 + * + * @return + */ + public Object deQueue() { + Object obj = linkedList.remove(0); + size--; + return obj; } - - public boolean isEmpty(){ - return false; + + public boolean isEmpty() { + return size == 0; } - - public int size(){ - return -1; + + public int size() { + return size; } } diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Stack.java b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java index 4bfe28057f..1dd8b15f59 100644 --- a/group03/345943980/2017Learning/src/com/coding/basic/Stack.java +++ b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java @@ -1,22 +1,51 @@ package com.coding.basic; +/** + * 栈(堆栈),特点先进后出的特点 + * + * @author Administrator + * + */ public class Stack { private ArrayList elementData = new ArrayList(); - - public void push(Object o){ + private int size = 0; + + /** + * 在堆栈顶部中添加一个元素 + * + * @param o + */ + public void push(Object o) { + elementData.add(o); + size++; } - - public Object pop(){ - return null; + + /** + * 在堆栈顶部移去一个元素 + * + * @return + */ + public Object pop() { + Object o = elementData.remove(size - 1); + size--; + return o; + } - - public Object peek(){ - return null; + + /** + * 总是返回栈顶的元素 + * + * @return + */ + public Object peek() { + return elementData.get(size - 1); } - public boolean isEmpty(){ - return false; + + public boolean isEmpty() { + return size == 0; } - public int size(){ - return -1; + + public int size() { + return size; } } diff --git a/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..8d812b9f71 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,48 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(100); + arrayList.add(0, 1000); + System.out.println(arrayList.size()); + for(int i =0;i array = new java.util.ArrayList(); + array.add(1); + array.add(1, 20); + System.out.println(array.size()); + for(Object o:array){ + System.out.println(o.toString()); + } + //System.out.println(array.get(100)); + } + + +} diff --git a/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java new file mode 100644 index 0000000000..68962223c7 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java @@ -0,0 +1,25 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.LinkedList; + +public class BinaryTreeTest { + + @Test + public void test01(){ + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(2); + binaryTree.insert(7); + binaryTree.insert(1); + binaryTree.insert(4); + binaryTree.insert(6); + binaryTree.insert(8); + LinkedList linkedList=binaryTree.inOrder(); + for(int i=0;i Date: Mon, 27 Feb 2017 00:11:54 +0800 Subject: [PATCH 22/22] =?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 --- group03/894844916/coding2017-01/.classpath | 6 + group03/894844916/coding2017-01/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 149 ++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 67 +++++++ .../src/com/coding/basic/Iterator.java | 22 +++ .../src/com/coding/basic/LinkedList.java | 168 ++++++++++++++++++ .../src/com/coding/basic/List.java | 54 ++++++ .../com/coding/basic/ListIndexException.java | 21 +++ .../src/com/coding/basic/Queue.java | 36 ++++ .../src/com/coding/basic/Stack.java | 43 +++++ 11 files changed, 594 insertions(+) create mode 100644 group03/894844916/coding2017-01/.classpath create mode 100644 group03/894844916/coding2017-01/.project create mode 100644 group03/894844916/coding2017-01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/List.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/ListIndexException.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/Queue.java create mode 100644 group03/894844916/coding2017-01/src/com/coding/basic/Stack.java diff --git a/group03/894844916/coding2017-01/.classpath b/group03/894844916/coding2017-01/.classpath new file mode 100644 index 0000000000..63b7e892d1 --- /dev/null +++ b/group03/894844916/coding2017-01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/894844916/coding2017-01/.project b/group03/894844916/coding2017-01/.project new file mode 100644 index 0000000000..34cd5ea671 --- /dev/null +++ b/group03/894844916/coding2017-01/.project @@ -0,0 +1,17 @@ + + + coding2017-01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/894844916/coding2017-01/.settings/org.eclipse.jdt.core.prefs b/group03/894844916/coding2017-01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group03/894844916/coding2017-01/.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/894844916/coding2017-01/src/com/coding/basic/ArrayList.java b/group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f245739d4c --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,149 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public class ArrayList implements List { + + /** + * 数组列表初始默认容量100. + */ + private static final int DEFAULT_CAPACITY=100; + + /** + * 数组列表中元素的数量。 + */ + private int size = 0; + + /** + * 数组列表当前容量 + */ + private int capacity; + + /** + * 存放元素的数组。 + */ + private Object[] elementData; + + /** + * 默认构造函数,构造一个初始容量为100的数组列表 + */ + public ArrayList() { + capacity=DEFAULT_CAPACITY; + elementData=new Object[DEFAULT_CAPACITY]; + } + + /* + * (non-Javadoc) + * + * @see nusub.coding2017.basic.List#add(java.lang.Object) + */ + @Override + public void add(Object o) { + expand(); + elementData[size]=o; + size=size++; + } + + /* + * (non-Javadoc) + * + * @see nusub.coding2017.basic.List#add(int, java.lang.Object) + */ + @Override + public void add(int index, Object o) throws ListIndexException { + if (index==size) { + add(o); + return; + } + if (0<=index&&indexindex; i--) { + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + return; + } + throw new ListIndexException("index不在[0,size]之间"); + } + + /** + * index在[0,size)之间返回通过数组下标访问的方式获取位置index处的元素,index超出这个范围抛出ListIndexException。 + * @param index 元素的位置 + * @return 获取的对象 + * @throws ListIndexException + */ + @Override + public Object get(int index) throws ListIndexException { + if (0<=index&&index + * + */ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){} + + 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 (o.toString().compareTo(data.toString())<0) { + if (left==null) { + left=new BinaryTreeNode(o); + return left; + } + insert(o); + } + else { + if (right==null) { + right=new BinaryTreeNode(o); + return right; + } + insert(o); + } + return null; + } + +} diff --git a/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java b/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..75a1ffaf3e --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java @@ -0,0 +1,22 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public interface Iterator { + /** + * 集合中存在下一个元素返回true,不存在下一个元素返回false。 + * @return + */ + public boolean hasNext(); + + /** + * 返回集合中下一个元素 + * @return + */ + public Object next(); +} diff --git a/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java b/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e811536093 --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,168 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public class LinkedList implements List, Iterator { + + private Node head; + private Node current=head; + private int size=0; + + /* (non-Javadoc) + * @see nusub.coding2017.basic.Iterator#hasNext() + */ + @Override + public boolean hasNext() { + if (current.next==null) { + return false; + } + return true; + } + + /* (non-Javadoc) + * @see nusub.coding2017.basic.Iterator#next() + */ + @Override + public Object next() { + current=current.next; + return current; + } + + /** + * 从头结点遍历,找到最后一个节点,最后一个节点引用下一个元素。最后把current指向头结点。 + * @param o 要添加的对象 + */ + @Override + public void add(Object o) { + Node node=new LinkedList.Node(); + node.data=o; + if (head==null) { + head=node; + current=head; + size++; + return; + } + while (hasNext()) { + next(); + } + current.next=node; + current=head; + size++; + } + + /** + * 在指定的位置插入一个元素,并把当前节点指向head。 + * @throws ListIndexException + */ + @Override + public void add(int index, Object o) throws ListIndexException { + if (index<0||index>size) { + throw new ListIndexException("index必须在[0,size]之间"); + } + Node node=new LinkedList.Node(); + node.data=o; + if (index==0) { + node.next=current; + head=node; + current=node; + } + else{ + int i=0; + while (i=size) { + throw new ListIndexException("index必须在[0,size)之间"); + } + if (index==0) { + return current; + } + int i=0; + while (i=size) { + throw new ListIndexException("index必须在[0,size)之间"); + } + if (size==0) { + return null; + } + Node node=null; + if (index==0) { + node=current; + current=current.next; + } + else if (index