From c04c587e050c5513497c414a3d9d44b618a26938 Mon Sep 17 00:00:00 2001 From: waking2017 Date: Fri, 24 Feb 2017 15:59:37 +0800 Subject: [PATCH 01/31] work1 --- group05/441517454/work1/.classpath | 6 + group05/441517454/work1/.gitignore | 1 + group05/441517454/work1/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../work1/src/com/coding/basic/ArrayList.java | 106 ++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++ .../work1/src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 156 ++++++++++++++++++ .../work1/src/com/coding/basic/List.java | 9 + .../work1/src/com/coding/basic/Queue.java | 30 ++++ .../work1/src/com/coding/basic/Stack.java | 30 ++++ .../work1/src/com/coding/basic/Test1.java | 48 ++++++ 12 files changed, 453 insertions(+) create mode 100644 group05/441517454/work1/.classpath create mode 100644 group05/441517454/work1/.gitignore create mode 100644 group05/441517454/work1/.project create mode 100644 group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/441517454/work1/src/com/coding/basic/ArrayList.java create mode 100644 group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Iterator.java create mode 100644 group05/441517454/work1/src/com/coding/basic/LinkedList.java create mode 100644 group05/441517454/work1/src/com/coding/basic/List.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Queue.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Stack.java create mode 100644 group05/441517454/work1/src/com/coding/basic/Test1.java diff --git a/group05/441517454/work1/.classpath b/group05/441517454/work1/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/441517454/work1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/441517454/work1/.gitignore b/group05/441517454/work1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/441517454/work1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/441517454/work1/.project b/group05/441517454/work1/.project new file mode 100644 index 0000000000..49a7fabffd --- /dev/null +++ b/group05/441517454/work1/.project @@ -0,0 +1,17 @@ + + + work1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs b/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group05/441517454/work1/src/com/coding/basic/ArrayList.java b/group05/441517454/work1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..01671f55d8 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + if(sizeindex;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index] = o; + this.size++; + } + else if(index<(this.size)&&this.size==elementData.length) + { + elementData = grow(elementData,1); + elementData[index] = o; + this.size++; + } + else{ + System.out.println("index/>size加入失败"); + + } + + + } + + public Object get(int index){ + + if(this.size>0&&index<(this.size)) + return elementData[index]; + else{ + return null; + } + } + + public Object remove(int index){ + + if(this.size>0&&index<(this.size)) + { Object o= elementData[index]; + for(int i=index;iarrayList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return arrayList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java b/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/441517454/work1/src/com/coding/basic/Iterator.java b/group05/441517454/work1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/441517454/work1/src/com/coding/basic/LinkedList.java b/group05/441517454/work1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5eefddce8b --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,156 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + private Node body; + //private Node body1; + private int size = 0; + + + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + size++; + } +// else if(head.next == null){ +// +// head.next =new Node(); +// head.next.data = o; +// head.next.next = null; +// body=head.next; +// size++; +// } + else { + body=head; + while(!(body.next ==null)) + {body =body.next;} + body.next =new Node(); + body.next.data =o; + body.next.next =null; + size++; + } + + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + if (index1) + {body=head; + for (int i=0;ilinkedList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return linkedList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/List.java b/group05/441517454/work1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/441517454/work1/src/com/coding/basic/Queue.java b/group05/441517454/work1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..cae520dbce --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + if(elementData.size()>0) + + { + elementData.remove(0); + return elementData.remove(0); + } + else return null; + } + + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Stack.java b/group05/441517454/work1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..40e8518b24 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Stack.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()>0) + + { int size =elementData.size(); + elementData.remove(size-1); + return elementData.remove(size-1);} + else return null; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Test1.java b/group05/441517454/work1/src/com/coding/basic/Test1.java new file mode 100644 index 0000000000..b7f644c95d --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Test1.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class Test1 { + public static void main (String[] args){ +// ArrayList arr = new ArrayList(); +// arr.add(1); +// arr.add(2); +// arr.add(3); +// arr.add(4); +// arr.remove(3); +// arr.add(6); +// arr.add(7); +// System.out.println(arr.size()+"_"); +// Iterator it =arr.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } +// LinkedList link = new LinkedList(); +// link.add(0); +// link.add(1); +// link.add(2); +// link.add(3); +// link.add(4); +// link.add(5); +// link.remove(2); +// link.addFirst(100); +// link.addLast(200); +// //link.removeFirst(); +// //link.removeLast(); +//// System.out.println(link.size()+"_"); +//// System.out.println(link.get(0)); +//// System.out.println(link.get(link.size()-1)); +//// +// +// Iterator it =link.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } + Stack st = new Stack(); + st.push(0); + st.push(1); + st.push(2); + st.push(4); + st.push(5); + st.pop(); + System.out.println(st.peek()); + } +} \ No newline at end of file From afde9da6a16036a963db4e81e57633d7606795ac Mon Sep 17 00:00:00 2001 From: C-BoBo <183127807@qq.com> Date: Fri, 24 Feb 2017 17:10:17 +0800 Subject: [PATCH 02/31] HomeWork0226 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ArrayList,LinkList,Stack,Queue鐨勫疄鐜 --- group05/183127807/HomeWork0226/.idea/misc.xml | 22 + .../183127807/HomeWork0226/.idea/modules.xml | 8 + group05/183127807/HomeWork0226/.idea/vcs.xml | 6 + .../HomeWork0226/.idea/workspace.xml | 1056 +++++++++++++++++ .../183127807/HomeWork0226/HomeWork0226.iml | 11 + .../src/com/coding/basic/ArrayList.java | 85 ++ .../src/com/coding/basic/BinaryTreeNode.java | 32 + .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 142 +++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 22 + .../src/com/coding/basic/Stack.java | 35 + 12 files changed, 1435 insertions(+) create mode 100644 group05/183127807/HomeWork0226/.idea/misc.xml create mode 100644 group05/183127807/HomeWork0226/.idea/modules.xml create mode 100644 group05/183127807/HomeWork0226/.idea/vcs.xml create mode 100644 group05/183127807/HomeWork0226/.idea/workspace.xml create mode 100644 group05/183127807/HomeWork0226/HomeWork0226.iml create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/List.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Queue.java create mode 100644 group05/183127807/HomeWork0226/src/com/coding/basic/Stack.java diff --git a/group05/183127807/HomeWork0226/.idea/misc.xml b/group05/183127807/HomeWork0226/.idea/misc.xml new file mode 100644 index 0000000000..2e47c90c3c --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/modules.xml b/group05/183127807/HomeWork0226/.idea/modules.xml new file mode 100644 index 0000000000..e8f27e7b12 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/vcs.xml b/group05/183127807/HomeWork0226/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/workspace.xml b/group05/183127807/HomeWork0226/.idea/workspace.xml new file mode 100644 index 0000000000..bea69f9904 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/workspace.xml @@ -0,0 +1,1056 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487584408499 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/HomeWork0226.iml b/group05/183127807/HomeWork0226/HomeWork0226.iml new file mode 100644 index 0000000000..c90834f2d6 --- /dev/null +++ b/group05/183127807/HomeWork0226/HomeWork0226.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..d8609f5ca3 --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,85 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 100; + + private int size = 0; + + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(size+1); + for (int i = size;i>index;i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + + public Object remove(int index){ + Object[] removeData = (Object[]) elementData[index]; + for (int i =index;i elementData.length) { + + elementData = Arrays.copyOf(elementData, minCapacity + DEFAULT_CAPACITY); + } + } + +} diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java b/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java b/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1bbcad9db --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,142 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size ; + + private Node current = head; + public LinkedList() { + head = null; + size = 0; + } + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + if (current.next == null) + { + current.next = newNode; + } + + while (current.next != null){ + current = current.next; + } + current.next = newNode; + size++; + + } + public void add(int index , Object o){ + + Node newNode = new Node(); + newNode.data = o; + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + for (int i = 0; i < index - 2; i++) { + + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + size++; + } + public Object get(int index){ + + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else if (index == 0) { + return head; + } + for (int i = 0;i Date: Sat, 25 Feb 2017 17:06:40 +0800 Subject: [PATCH 03/31] 2/19-2/16 homework --- group05/399258474/.classpath | 7 ++ group05/399258474/.gitignore | 1 + group05/399258474/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 71 ++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 104 ++++++++++++++++++ .../399258474/src/com/coding/basic/List.java | 9 ++ .../399258474/src/com/coding/basic/Queue.java | 35 ++++++ .../399258474/src/com/coding/basic/Stack.java | 39 +++++++ group05/399258474/src/test/BasicTest.java | 78 +++++++++++++ 12 files changed, 411 insertions(+) create mode 100644 group05/399258474/.classpath create mode 100644 group05/399258474/.gitignore create mode 100644 group05/399258474/.project create mode 100644 group05/399258474/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/399258474/src/com/coding/basic/ArrayList.java create mode 100644 group05/399258474/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/399258474/src/com/coding/basic/Iterator.java create mode 100644 group05/399258474/src/com/coding/basic/LinkedList.java create mode 100644 group05/399258474/src/com/coding/basic/List.java create mode 100644 group05/399258474/src/com/coding/basic/Queue.java create mode 100644 group05/399258474/src/com/coding/basic/Stack.java create mode 100644 group05/399258474/src/test/BasicTest.java diff --git a/group05/399258474/.classpath b/group05/399258474/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group05/399258474/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/399258474/.gitignore b/group05/399258474/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group05/399258474/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/399258474/.project b/group05/399258474/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group05/399258474/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/399258474/.settings/org.eclipse.jdt.core.prefs b/group05/399258474/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group05/399258474/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group05/399258474/src/com/coding/basic/ArrayList.java b/group05/399258474/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..5030b8d001 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[2]; + + public void add(Object o){ + int len = elementData.length; + if(size >= len){ + Object[] new_elmentData = new Object[len*2]; + System.arraycopy(elementData, 0, new_elmentData, 0, size); + elementData = new_elmentData; + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + if(index >= size){ + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + Object[] new_elementData = new Object[size+1]; + System.arraycopy(elementData, 0, new_elementData, 0, index); + System.arraycopy(elementData, index, new_elementData, index+1, size-index); + new_elementData[index] = o; + elementData = new_elementData; + size++; + } + + public Object get(int index){ + if(index >= size){ + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + Object oldElement = elementData[index]; + if((index+1) != size){ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + elementData[size-1] = null; + size --; + return oldElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + @Override + public String toString() { + String s = "{"; + for (int i = 0; i < size; i++) { + if(i == (size -1)){ + s += elementData[i] + "}"; + }else{ + s += elementData[i]+","; + } + } + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/BinaryTreeNode.java b/group05/399258474/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/399258474/src/com/coding/basic/Iterator.java b/group05/399258474/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/399258474/src/com/coding/basic/LinkedList.java b/group05/399258474/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ff69d8d341 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/LinkedList.java @@ -0,0 +1,104 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head = new Node(new Object(),null,null); + } + + public void add(Object o){ + Node last = head; + for (int i = 0; i < size; i++) { + last = last.next; + } + Node newNode = new Node(o,null,last); + last.next = newNode; + size++; + } + public void add(int index , Object o){ + Node oldNode = getNode(index); + Node newNode = new Node(o, oldNode, oldNode.prev); + oldNode.prev.next = newNode; + oldNode.prev = newNode; + size ++; + } + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + private Node getNode(int index){ + Node n = head.next; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Object remove(int index){ + Node node = getNode(index); + Object o =node.data; + Node prevNode = node.prev; + Node nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + node.next = node.prev =null; + node.data = null; + size --; + return o; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + Object o = remove(0); + return o; + } + public Object removeLast(){ + Object o = remove(size); + return o; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + + public Node(Object o,Node next,Node prev){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + @Override + public String toString() { + String s = "{"; + Node n = head; + for (int i = 0; i < size; i++) { + n = n.next; + if(i == (size -1)){ + s += n.data; + }else{ + s += n.data + ","; + } + } + s += "}"; + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/List.java b/group05/399258474/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/399258474/src/com/coding/basic/Queue.java b/group05/399258474/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a0bb03e6f2 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("宸蹭负绌"); + } + Object o = list.get(0); + list.remove(0); + return o; + } + + public boolean isEmpty(){ + if(size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group05/399258474/src/com/coding/basic/Stack.java b/group05/399258474/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d137996b6 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new RuntimeException("宸蹭负绌"); + } + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group05/399258474/src/test/BasicTest.java b/group05/399258474/src/test/BasicTest.java new file mode 100644 index 0000000000..7339881525 --- /dev/null +++ b/group05/399258474/src/test/BasicTest.java @@ -0,0 +1,78 @@ +package test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class BasicTest { + + @Test + public void ArrayListTest() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + System.out.println(list); + list.add(1, 99); + System.out.println(list); + list.remove(1); + System.out.println(list); + } + + @Test + public void LinkedListTest(){ + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + System.out.println(l); + l.add(1, 99); + System.out.println(l); + l.remove(1); + System.out.println(l); + System.out.println(l.size()); + } + + @Test + public void StackTest(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("绌"); + }else{ + System.out.println("闈炵┖"); + + } + System.out.println(s.peek()); + s.pop(); + System.out.println(s); + s.pop(); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("绌"); + }else{ + System.out.println("闈炵┖"); + + } + s.pop(); + } + + @Test + public void QueueTest(){ + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + System.out.println(q); + q.deQueue(); + System.out.println(q); + q.enQueue(3); + System.out.println(q); + System.out.println(q.size()); + } + +} From 5d323074adc75b9462e73472796ec3a1da85fb2a Mon Sep 17 00:00:00 2001 From: wangzhengliang <1026626960@qq.com> Date: Sat, 25 Feb 2017 22:25:57 +0800 Subject: [PATCH 04/31] one --- group05/1026626960/.classpath | 6 +++ group05/1026626960/.gitignore | 1 + group05/1026626960/.project | 17 +++++++++ .../1026626960/src/cn/study1/myIterator.java | 5 +++ group05/1026626960/src/cn/study1/myQueue.java | 38 +++++++++++++++++++ group05/1026626960/src/cn/study1/myStack.java | 29 ++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 group05/1026626960/.classpath create mode 100644 group05/1026626960/.gitignore create mode 100644 group05/1026626960/.project create mode 100644 group05/1026626960/src/cn/study1/myIterator.java create mode 100644 group05/1026626960/src/cn/study1/myQueue.java create mode 100644 group05/1026626960/src/cn/study1/myStack.java diff --git a/group05/1026626960/.classpath b/group05/1026626960/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group05/1026626960/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/1026626960/.gitignore b/group05/1026626960/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1026626960/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1026626960/.project b/group05/1026626960/.project new file mode 100644 index 0000000000..a8522c9339 --- /dev/null +++ b/group05/1026626960/.project @@ -0,0 +1,17 @@ + + + 1026626960Coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1026626960/src/cn/study1/myIterator.java b/group05/1026626960/src/cn/study1/myIterator.java new file mode 100644 index 0000000000..a5e5528559 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myIterator.java @@ -0,0 +1,5 @@ +package cn.study1; + +public class myIterator { + // +} diff --git a/group05/1026626960/src/cn/study1/myQueue.java b/group05/1026626960/src/cn/study1/myQueue.java new file mode 100644 index 0000000000..cfb5e061d7 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myQueue.java @@ -0,0 +1,38 @@ +package cn.study1; + +public class myQueue { + private class Node{ + T t; + Node next; + } + private Node first; + private Node last; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void enqueue(T t){ + Node oldlast = last; + last = new Node(); + last.t = t; + last.next = null; + if(isEmpty()){ + first = last; + }else{ + oldlast.next = last; + } + N++; + } + public T dequeue(){ + T t = first.t; + first = first.next; + if(isEmpty()){ + last = null; + } + N--; + return t; + } +} diff --git a/group05/1026626960/src/cn/study1/myStack.java b/group05/1026626960/src/cn/study1/myStack.java new file mode 100644 index 0000000000..8364401c45 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myStack.java @@ -0,0 +1,29 @@ +package cn.study1; + +public class myStack { + private class Node{ + T t; + Node next; + } + private Node first; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void push(T t){ + Node oldfirst = first; + first = new Node(); + first.t = t; + first.next = oldfirst; + N++; + } + public T pop(){ + T t = first.t; + first = first.next; + N--; + return t; + } +} From 1dbd5aee827accb108dc6fbe2d482a1da19b2875 Mon Sep 17 00:00:00 2001 From: Leon1900 Date: Sat, 25 Feb 2017 23:08:40 +0800 Subject: [PATCH 05/31] Task01 @Leon1900 --- .../515505513/RemoteSystemsTempFiles/.project | 12 +++ group05/515505513/Task01/.classpath | 6 ++ group05/515505513/Task01/.gitignore | 1 + group05/515505513/Task01/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ .../src/com/coding/basic/ArrayList.java | 78 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++++ .../Task01/src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 48 ++++++++++++ .../Task01/src/com/coding/basic/List.java | 9 +++ .../Task01/src/com/coding/basic/Queue.java | 49 ++++++++++++ .../Task01/src/com/coding/basic/Stack.java | 49 ++++++++++++ .../src/com/coding/basic/TestExample.java | 27 +++++++ 13 files changed, 346 insertions(+) create mode 100644 group05/515505513/RemoteSystemsTempFiles/.project create mode 100644 group05/515505513/Task01/.classpath create mode 100644 group05/515505513/Task01/.gitignore create mode 100644 group05/515505513/Task01/.project create mode 100644 group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/515505513/Task01/src/com/coding/basic/ArrayList.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Iterator.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/LinkedList.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/List.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Queue.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/Stack.java create mode 100644 group05/515505513/Task01/src/com/coding/basic/TestExample.java diff --git a/group05/515505513/RemoteSystemsTempFiles/.project b/group05/515505513/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group05/515505513/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group05/515505513/Task01/.classpath b/group05/515505513/Task01/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/515505513/Task01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/515505513/Task01/.gitignore b/group05/515505513/Task01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/515505513/Task01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/515505513/Task01/.project b/group05/515505513/Task01/.project new file mode 100644 index 0000000000..cf494f7a91 --- /dev/null +++ b/group05/515505513/Task01/.project @@ -0,0 +1,17 @@ + + + Task01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs b/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group05/515505513/Task01/src/com/coding/basic/ArrayList.java b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..29fc9b98cb --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + + private int size = 0; + private static final int DEFAULT_SIZE = 100; + private Object[] elementData = new Object[DEFAULT_SIZE]; + + //娣诲姞鍏冪礌 + public void add(Object o){ + add(size(),o); + } + + + public void add(int index, Object o){ + if(elementData.length==size()){ + ensureCapacity(size()*2 + 1); + } + for (int i = size; i > index; i--) + elementData[i]=elementData[i-1]; + elementData[index] = o; + size++; + } + //鎵╁ + public void ensureCapacity(int newCapacity){ + if(newCapacity < size){ + return; + } + Object[] oldElements = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size; i++) { + elementData[i] = oldElements[i]; + } + } + //杩斿洖鍥哄畾涓嬫爣鐨勫厓绱 + public Object get(int index){ + if(index<0 || index >=size){ + throw new ArrayIndexOutOfBoundsException("鎸囧畾鐨刬ndex瓒呰繃鐣岄檺"); + } + return elementData[index]; + } + //鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱 + public Object remove(int index){ + Object removeElement = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i+1]; + } + size--; + return removeElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int current = 0; + @Override + public boolean hasNext() { + return current < size; + } + @Override + public Object next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + return elementData[current+1]; + } + } + +} diff --git a/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java b/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/515505513/Task01/src/com/coding/basic/Iterator.java b/group05/515505513/Task01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/515505513/Task01/src/com/coding/basic/LinkedList.java b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ea3d6a07f3 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head;//鎸囬拡 + private Object element;//鍏冪礌 + //娣诲姞涓涓厓绱 + public void add(Object o){ + + } + //鍦╥ndex澶勬坊鍔犱竴涓厓绱 + public void add(int index , Object o){ + + } + + public Object get(int index){ + return null; + } + //娓呴櫎涓涓厓绱 + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data;//鍏冪礌 + Node next;//鎸囬拡 + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/List.java b/group05/515505513/Task01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/515505513/Task01/src/com/coding/basic/Queue.java b/group05/515505513/Task01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fd316ac7bf --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Queue.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Queue { + private int maxSize;//闃熷垪瀹归噺 + private Object[] queue;//闃熷垪 + private int head;//闃熷垪澶达紝鍙互鍒犻櫎 + private int tail;//闃熷垪灏撅紝鍙互鎻掑叆 + + + public Queue() { + this(10); + } + + public Queue(int maxSize) { + if(maxSize >=0){ + this.maxSize = maxSize; + this.queue = new Object[maxSize]; + head = tail = 0; + }else { + throw new RuntimeException("鍒濆鍖栧ぇ灏忎笉鑳藉皬浜0"); + } + } + + public void enQueue(Object o){ + if(tail == maxSize){ + throw new RuntimeException("闃熷垪宸叉弧锛屾棤娉曟彃鍏ユ柊鐨勫厓绱狅紒"); + }else { + queue[tail++] = o; + } + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("绌洪槦鍒楀紓甯革紒"); + }else { + Object value = queue[head]; + queue[head++] = null; + return value; + } + } + //闃熷垪鏄惁涓虹┖ + public boolean isEmpty(){ + return head==tail?true:false; + } + + public int size(){ + return tail-head; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/Stack.java b/group05/515505513/Task01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a85cfed9d2 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Stack.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Stack { + private Object[] data; + private int capacity; + private int size; + public Stack(){ + capacity = 16; + size = 0; + data = new Object[capacity]; + } + public void push(Object o){ + if(size < capacity){ + data[size++] = o; + }else { + ensureCapacity(); + data[size++] = o; + } + } + + private void ensureCapacity() { + capacity = capacity*2; + } + public Object pop(){ + if(size > 0){ + System.out.println(data[size-1]); + //data[size--] = null; + }else { + System.out.println("Empty stack"); + } + size--; + return data[size]; + } + + public Object peek(){ + if(size>0){ + return data[size-1]; + }else { + return null; + } + } + public boolean isEmpty(){ + + return size==0; + } + public int size(){ + return size; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/TestExample.java b/group05/515505513/Task01/src/com/coding/basic/TestExample.java new file mode 100644 index 0000000000..59bfa47a3c --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/TestExample.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class TestExample { + + public static void main(String[] args) { + /*Stack mystack = new Stack(); + for (int i = 0; i < 10; i++) { + mystack.push(i); + } + for (int i = 0; i < 10; i++) { + mystack.pop(); + System.out.println("==="+mystack.peek()); + }*/ + + /*Test Queue + Queue myQqueue = new Queue(10); + for (int i = 0; i < 10; i++) { + myQqueue.enQueue(i); + } + for (int i = 0; i < 10; i++) { + System.out.println(myQqueue.deQueue()); + }*/ + + + } + +} From 4f7b4808b96c1803f8e2f6cf52724f1ab3693b39 Mon Sep 17 00:00:00 2001 From: ZhaoHongxin Date: Sun, 26 Feb 2017 00:19:49 +0800 Subject: [PATCH 06/31] test01 --- group05/1094051862/test01/.classpath | 7 + group05/1094051862/test01/.gitignore | 1 + group05/1094051862/test01/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 12 ++ .../src/com/coding1094051862/basic/.gitignore | 1 + .../coding1094051862/basic/ArrayListTest.java | 23 +++ .../basic/BinaryTreeNode.java | 32 ++++ .../com/coding1094051862/basic/Iterator.java | 7 + .../coding1094051862/basic/LinkedList.java | 142 ++++++++++++++++++ .../basic/LinkedListTest.java | 32 ++++ .../src/com/coding1094051862/basic/List.java | 9 ++ .../src/com/coding1094051862/basic/Queue.java | 19 +++ .../src/com/coding1094051862/basic/Stack.java | 22 +++ 13 files changed, 324 insertions(+) create mode 100644 group05/1094051862/test01/.classpath create mode 100644 group05/1094051862/test01/.gitignore create mode 100644 group05/1094051862/test01/.project create mode 100644 group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/List.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java create mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java diff --git a/group05/1094051862/test01/.classpath b/group05/1094051862/test01/.classpath new file mode 100644 index 0000000000..04cc82dc42 --- /dev/null +++ b/group05/1094051862/test01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/1094051862/test01/.gitignore b/group05/1094051862/test01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1094051862/test01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1094051862/test01/.project b/group05/1094051862/test01/.project new file mode 100644 index 0000000000..1dfd9165c6 --- /dev/null +++ b/group05/1094051862/test01/.project @@ -0,0 +1,17 @@ + + + test01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..d17b6724d1 --- /dev/null +++ b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore b/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore new file mode 100644 index 0000000000..709f74c2f7 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore @@ -0,0 +1 @@ +/ArrayList.java diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java b/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java new file mode 100644 index 0000000000..6345b9c57e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java @@ -0,0 +1,23 @@ +package com.coding1094051862.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void test() { + ArrayList list = new ArrayList(); + for(int i = 0; i < 10; i++) { + list.add(i); + } + Assert.assertEquals(10, list.size()); + list.add(11); + list.add(3,99); + Assert.assertEquals(99, list.get(3)); + Assert.assertEquals(12, list.size()); + Assert.assertEquals(99, list.remove(3)); + Assert.assertEquals(11, list.size()); + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java b/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..09d35349b8 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding1094051862.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java new file mode 100644 index 0000000000..33dcc89a6a --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding1094051862.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java new file mode 100644 index 0000000000..ea3b54241b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java @@ -0,0 +1,142 @@ +package com.coding1094051862.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size = 0; + + public void add(Object o){ + if (head == null) { + head = new Node(o, null); + size ++; + return; + } + Node n = new Node(o, null); + if (last == null) { + last = n; + head.next = last; + } + last.next = n; + last = n; + size ++; + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + System.out.println("linkedList.add: index < 0 || index > size"); + return; + } + if (index == size) { + add(o); + return; + } + if (index == 0) { + addFirst(o); + return; + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node post = pre.next; + Node n = new Node(o,post); + pre.next = n; + size ++; + } + public Object get(int index){ + if (index == 0) { + return head.data; + } + Node n = head; + for (int i = 1; i <= index; i++) { + n = n.next; + } + return n.data; + } + public Object remove(int index){ + if (index < 0 || index >= size) { + System.out.println("remove :index < 0 || index >= size"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node n = pre.next; + Node post = n.next; + n.next = null; + pre.next = post; + size --; + return n.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node n = new Node(o,head); + head = n; + size ++; + return; + } + public void addLast(Object o){ + Node n = new Node(o,null); + last.next = n; + last = n; + size ++; + return; + } + public Object removeFirst(){ + Object o = head.data; + Node n = head.next; + head.next = null; + head = n; + size --; + return o; + } + public Object removeLast(){ + Node preLast = head; + for (int i = 1; i < size; i++) { + preLast = preLast.next; + } + preLast.next = null; + Object o = last.data; + last = preLast; + size --; + return o; + } + public Iterator iterator(){ + return new Iterator() { + @Override + public Object next() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return false; + } + }; + } + + + private static class Node { + + Object data; + Node next; + + public Node (Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java new file mode 100644 index 0000000000..dccc8e1e86 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java @@ -0,0 +1,32 @@ +package com.coding1094051862.basic; + +import junit.framework.Assert; + +import org.junit.Test; + +public class LinkedListTest extends LinkedList { + + @Test + public void test() { + List list = new LinkedList(); + list.add(0); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(3, 33); + list.add(0, 100); + list.add(8,800); + Assert.assertEquals(9, list.size()); + Assert.assertEquals(100, list.get(0)); + Assert.assertEquals(0, list.get(1)); + Assert.assertEquals(1, list.get(2)); + Assert.assertEquals(2, list.get(3)); + Assert.assertEquals(33, list.get(4)); + Assert.assertEquals(3, list.get(5)); + Assert.assertEquals(4, list.get(6)); + Assert.assertEquals(800, list.get(8)); + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/List.java b/group05/1094051862/test01/src/com/coding1094051862/basic/List.java new file mode 100644 index 0000000000..7f1430b67b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/List.java @@ -0,0 +1,9 @@ +package com.coding1094051862.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java new file mode 100644 index 0000000000..fb9f6107fd --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding1094051862.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java new file mode 100644 index 0000000000..a9fbb4635b --- /dev/null +++ b/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding1094051862.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From de5bb1b050944e92592708fb8767b916ddc29c73 Mon Sep 17 00:00:00 2001 From: xiaoqin0000 Date: Sun, 26 Feb 2017 01:45:30 +0800 Subject: [PATCH 07/31] add ArrayList and LinkedList --- .../.idea/compiler.xml | 23 + .../.idea/description.html | 1 + .../.idea/encodings.xml | 5 + .../.idea/misc.xml | 12 + .../.idea/modules.xml | 8 + .../.idea/uiDesigner.xml | 124 +++ .../.idea/vcs.xml | 7 + .../.idea/workspace.xml | 786 ++++++++++++++++++ .../job_2017_02_19_data_structure.iml | 12 + .../com/java/xiaoqin/impl/ArrayListImpl.java | 112 +++ .../com/java/xiaoqin/impl/LinkedListImpl.java | 146 ++++ .../java/xiaoqin/interfaces/IIterator.java | 19 + .../com/java/xiaoqin/interfaces/IList.java | 47 ++ 13 files changed, 1302 insertions(+) create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml new file mode 100644 index 0000000000..217af471a9 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html new file mode 100644 index 0000000000..db5f129556 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml new file mode 100644 index 0000000000..e206d70d85 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml new file mode 100644 index 0000000000..91362b9dce --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml new file mode 100644 index 0000000000..46ad2c6b17 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml new file mode 100644 index 0000000000..def6a6a184 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml new file mode 100644 index 0000000000..1dc6e0b616 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml @@ -0,0 +1,786 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + package com.java.xiaoqin.impl; import com.java.xiaoqin.interfaces.IList; /** * Created by xiaoqin on 17-2-25. */ public class ArrayListImpl<T> implements IList<T> { private static final int DEFAULT_INIT_SIZE = 20; private T[] data; private int size = 0; public ArrayListImpl(){ this(DEFAULT_INIT_SIZE); } public ArrayListImpl(int capacity){ if (capacity < 0){ throw new IllegalArgumentException("鏃犳晥鐨刢apacity锛" + capacity); } if (0 == capacity){ capacity = DEFAULT_INIT_SIZE; } data = (T[]) new Object[capacity]; } @Override public void add(T t) { } @Override public void add(int index, T t) { } @Override public T get(int index) { return null; } @Override public T remove(int index) { return null; } @Override public int size() { return 0; } } + gro + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488008946620 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 36 + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 53 + + + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java + 84 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml new file mode 100644 index 0000000000..d5c0743275 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java new file mode 100644 index 0000000000..c96d0b4f30 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java @@ -0,0 +1,112 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; + +/** + * Created by xiaoqin on 17-2-25. + */ +public class ArrayListImpl implements IList { + + private static final int DEFAULT_INIT_SIZE = 20; + + private T[] data; + + private int size = 0; + + public ArrayListImpl() { + this(DEFAULT_INIT_SIZE); + } + + public ArrayListImpl(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException("鏃犳晥鐨刢apacity锛" + capacity); + } + if (0 == capacity) { + capacity = DEFAULT_INIT_SIZE; + } + data = (T[]) new Object[capacity]; + } + + @Override + public void add(T t) { + group(size); + data[size++] = t; + } + + @Override + public void add(int index, T t) { + if (index < 0) { + throw new IllegalArgumentException("index < 0锛宨ndex:" + index); + } + if (index > size) { + throw new IndexOutOfBoundsException("index >= size銆俰ndex:" + index + "\tsize:" + size); + } + group(size); + T[] temp = (T[]) new Object[size - index]; + System.arraycopy(data, index, temp, 0, temp.length); + data[index] = t; + size++; + System.arraycopy(temp, 0, data, index + 1, temp.length); + } + + @Override + public T get(int index) { + if (index < data.length) { + return data[index]; + } else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + @Override + public T remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException("index invalid!!!index:" + index); + } + T[] temp = (T[]) new Object[size - index - 1]; + System.arraycopy(data, index + 1, temp, 0, temp.length); + T result = data[index]; + System.arraycopy(temp, 0, data, index, temp.length); + data[--size] = null; + return result; + } + + @Override + public int size() { + return size; + } + + @Override + public IIterator iterator() { + return new ArrayIteratorImpl<>(); + } + + private void group(int minSize) { + if (minSize >= data.length) { + T[] temp = (T[]) new Object[size]; + System.arraycopy(data, 0, temp, 0, size); + int groupSize = (size >> 1) + size; + data = (T[]) new Object[Math.max(groupSize, minSize)]; + System.arraycopy(temp, 0, data, 0, size); + } + } + + private class ArrayIteratorImpl implements IIterator { + + private int index = 0; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index out"); + } + return (T) data[index++]; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java new file mode 100644 index 0000000000..90f8c326af --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java @@ -0,0 +1,146 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; + +/** + * Created by xiaoqin on 17-2-26. + */ +public class LinkedListImpl implements IList { + + private Node head; + private Node last; + private int size = 0; + + public LinkedListImpl() { + } + + @Override + public void add(T t) { + if (size == 0) { + head = new Node<>(); + head.data = t; + head.index = size++; + last = head; + } else { + last.right = new Node<>(); + last.right.left = last; + last = last.right; + last.data = t; + last.index = size++; + } + } + + @Override + public void add(int index, T t) { + Node node = findNodeByIndex(index); + if (node.index == 0) { + node.left = new Node<>(); + node.left.right = node; + node.left.index = 0; + node.left.data = t; + head = node.left; + } else { + node.left.right = new Node<>(); + node.left.right.index = node.index; + node.left.right.right = node; + node.left.right.data = t; + node.left = node.left.right; + } + while (node.right != null) { + node.index++; + node = node.right; + } + size++; + } + + private Node findNodeByIndex(int index) { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index锛" + index); + } + Node resultNode = null; + Node origin = null; + int half = size >> 1; + if (index > half) { + origin = last; + while (origin.index != index) { + origin = origin.left; + } + } else { + origin = head; + while (origin.index != index) { + origin = origin.right; + } + } + return origin; + } + + @Override + public T get(int index) { + return findNodeByIndex(index).data; + } + + @Override + public T remove(int index) { + Node node = findNodeByIndex(index); + if (null != node.left) { + node.left.right = node.right; + } else { + node.right.left = null; + head = node.right; + } + if (null != node.right) { + node.right.left = node.left; + while (node.right == null) { + node.right.index--; + } + } else { + node.left.right = null; + last = node.left; + } + size--; + return node.data; + } + + @Override + public int size() { + return size; + } + + @Override + public IIterator iterator() { + return new LinkedIteratorImpl<>(); + } + + private static class Node { + private T data = null; + private Node left = null; + private Node right = null; + private int index = 0; + } + + private class LinkedIteratorImpl implements IIterator { + + private int index = 0; + private Node next; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (0 == index) { + next = (Node) head; + } + if (null == next) { + throw new ArrayIndexOutOfBoundsException("next is null"); + } + Node result = next; + next = next.right; + index++; + return result.data; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java new file mode 100644 index 0000000000..bd83985af4 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java @@ -0,0 +1,19 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-25. + */ +public interface IIterator { + + /** + * 鏄惁鏈変笅涓涓 + * @return + */ + boolean hasNext(); + + /** + * 鍙栧嚭涓嬩竴涓殑鍏冪礌 + * @return + */ + T next(); +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java new file mode 100644 index 0000000000..1a7a01cbca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java @@ -0,0 +1,47 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-19. + */ +public interface IList { + + /** + * 娣诲姞鍏冪礌 + * @param t + */ + void add(T t); + + /** + * 娣诲姞鍏冪礌鍦ㄧ鍑犱釜 + * @param index + * @param t + */ + void add(int index,T t); + + /** + * 鑾峰彇绗琲ndex涓殑鍏冪礌 + * @param index + * @return + */ + T get(int index); + + /** + * 绉婚櫎绗琲ndex涓殑鍏冪礌 + * @param index + * @return + */ + T remove(int index); + + /** + * 杩斿洖List鐨勫ぇ灏 + * @return + */ + int size(); + + /** + * 杩斿洖杩唬鍣 + * @return + */ + IIterator iterator(); + +} From 649038500e4120196fdf8be8dcfb3b9dd1a09059 Mon Sep 17 00:00:00 2001 From: xiaoqin0000 Date: Sun, 26 Feb 2017 01:48:03 +0800 Subject: [PATCH 08/31] add .gitignore --- .../job_2017_02_19_data_structure/.gitignore | 6 ++ .../.idea/workspace.xml | 65 ++++++++----------- 2 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore new file mode 100644 index 0000000000..a2b48b352f --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore @@ -0,0 +1,6 @@ +/vendor +/node_modules +Homestead.yaml +Homestead.json +.env +.idea \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml index 1dc6e0b616..2b612eeea0 100644 --- a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml @@ -16,18 +16,6 @@ - - - - - - - - - - - - @@ -48,6 +36,16 @@ + + + + + + + + + + @@ -88,8 +86,9 @@ @@ -131,20 +130,6 @@ - @@ -609,10 +594,10 @@ - + - + @@ -754,6 +739,14 @@ + + + + + + + + @@ -772,13 +765,11 @@ - + - - - - - + + + From 000f23dbdce3551d2d4d882b52fc78a5fce9677e Mon Sep 17 00:00:00 2001 From: nitasty Date: Sun, 26 Feb 2017 16:16:15 +0800 Subject: [PATCH 09/31] =?UTF-8?q?ArrayList,=20LinkedList,=20Stack,=20Queue?= =?UTF-8?q?,=20Iteartor,=20BinaryTree=20=E5=AE=9E=E7=8E=B0=E5=8F=8A=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/nitasty/test/ArrayListTest.java | 95 +++++ .../src/com/nitasty/test/BinaryTreeTest.java | 66 +++ .../src/com/nitasty/test/LinkedListTest.java | 142 +++++++ .../src/com/nitasty/test/QueueTest.java | 49 +++ .../src/com/nitasty/test/StackTest.java | 51 +++ .../src/com/nitasty/util/ArrayList.java | 307 ++++++++++++++ .../src/com/nitasty/util/BinaryTree.java | 190 +++++++++ .../src/com/nitasty/util/BinaryTreeNode.java | 43 ++ .../src/com/nitasty/util/Iterator.java | 7 + .../src/com/nitasty/util/LinkedList.java | 393 ++++++++++++++++++ .../task_01/src/com/nitasty/util/List.java | 46 ++ .../task_01/src/com/nitasty/util/Queue.java | 27 ++ .../task_01/src/com/nitasty/util/Stack.java | 31 ++ 13 files changed, 1447 insertions(+) create mode 100644 group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java create mode 100644 group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java create mode 100644 group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java create mode 100644 group05/371492887/task_01/src/com/nitasty/test/QueueTest.java create mode 100644 group05/371492887/task_01/src/com/nitasty/test/StackTest.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/ArrayList.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/BinaryTree.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/BinaryTreeNode.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/Iterator.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/LinkedList.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/List.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/Queue.java create mode 100644 group05/371492887/task_01/src/com/nitasty/util/Stack.java diff --git a/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java new file mode 100644 index 0000000000..907302f3c1 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java @@ -0,0 +1,95 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; + +public class ArrayListTest { + + private ArrayList list; + + @Before + public void init(){ + list=new ArrayList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testAddObject() { + list.add(100); + Assert.assertEquals(101, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(3,"test"); + Assert.assertEquals("test", list.get(3)); + } + + @Test + public void testRemoveInt() { + list.add(3,"test"); + list.remove(3); + Assert.assertEquals(3, list.get(3)); + } + + @Test + public void testRemoveObject() { + list.add(0,"test"); + list.remove("test"); + Assert.assertEquals(0, list.get(0)); + } + + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + Assert.assertEquals(false, list.contains("test")); + list.add("test"); + Assert.assertEquals(true, list.contains("test")); + } + + + + @Test + public void testSet() { + Assert.assertEquals(true, list.contains(3)); + list.set(3, "test"); + Assert.assertEquals(true, list.contains("test")); + Assert.assertEquals(false, list.contains(3)); + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java new file mode 100644 index 0000000000..abb27b5691 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java @@ -0,0 +1,66 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.BinaryTree; + +public class BinaryTreeTest { + + BinaryTree tree; + + @Before + public void init(){ + tree=new BinaryTree(); + tree.insert(5); + tree.insert(3); + tree.insert(8); + tree.insert(2); + tree.insert(7); + tree.insert(9); + tree.insert(1); + tree.insert(4); + tree.insert(10); + tree.insert(6); + } + + @Test + public void testMakeEmpty() { + tree.makeEmpty(); + Assert.assertEquals(true, tree.isEmpty()); + } + + @Test + public void testGetHeight() { + Assert.assertEquals(3, tree.getHeight()); + } + + @Test + public void testContains() { + for (int i = 1; i < 11; i++) { + Assert.assertEquals(true, tree.contains(i)); + } + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin()); + } + + @Test + public void testFindMax() { + Assert.assertEquals(10, tree.findMax()); + } + + + @Test + public void testRemove() { + tree.remove(3); + Assert.assertEquals(false, tree.contains(3)); + } + + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java new file mode 100644 index 0000000000..dcbe6353c3 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java @@ -0,0 +1,142 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; +import com.nitasty.util.LinkedList; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void init(){ + list=new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testGet() { + IndexOutOfBoundsException tx=null; + for (int i = 0; i < 100; i++) { + Assert.assertEquals(i, list.get(i)); + } + + try { + list.get(100); + } catch (IndexOutOfBoundsException e) { + tx=e; + } + Assert.assertEquals(IndexOutOfBoundsException.class,tx.getClass()); + } + + @Test + public void testRemoveInt() { + for (int i = 99; i >= 0; i--) { + Assert.assertEquals(i, list.remove(i)); + } + } + + @Test + public void testSize() { + Assert.assertEquals(100, list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst(-1); + for (int i = 0; i < 101; i++) { + Assert.assertEquals(i-1, list.get(i)); + } + } + + @Test + public void testAddLast() { + + for (int i = 100; i < 1000; i++) { + list.addLast(i); + } + + for (int i = 0; i < 1000; i++) { + Assert.assertEquals(i, list.get(i)); + } + } + + @Test + public void testAddBefore() { + list.addBefore(66,list.node(3)); + Assert.assertEquals(66, list.get(3)); + } + + @Test + public void testAddAfter() { + list.addAfter(66,list.node(3)); + Assert.assertEquals(66, list.get(4)); + } + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + for (int i = 0; i < 100; i++) { + Assert.assertEquals(true, list.contains(i)); + Assert.assertEquals(false, list.contains(i+100)); + } + } + + + @Test + public void testAddIntObject() { + list.add(20,"test"); + Assert.assertEquals("test", list.get(20)); + } + + @Test + public void testRemoveObject() { + list.remove(30); + Assert.assertEquals(31, list.get(30)); + } + + @Test + public void testSet() { + for (int i = 0; i < 100; i++) { + list.set(i, i+100); + Assert.assertEquals(i+100, list.get(i)); + } + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java new file mode 100644 index 0000000000..fa17263108 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java @@ -0,0 +1,49 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Queue; + +public class QueueTest { + + Queue queue; + + @Before + public void init(){ + queue=new Queue(); + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + } + + @Test + public void testDeQueue() { + for(int i=0; i<100;i++){ + Assert.assertEquals(i, queue.deQueue()); + } + } + + @Test + public void testIsEmpty() { + for(int i=0; i<100;i++){ + queue.deQueue(); + if(i<99) + Assert.assertEquals(false, queue.isEmpty()); + } + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + for(int i=99; i>0;i--){ + queue.deQueue(); + Assert.assertEquals(i, queue.size()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/StackTest.java b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java new file mode 100644 index 0000000000..3ecd59219a --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java @@ -0,0 +1,51 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Stack; + +public class StackTest { + + Stack stack; + + @Before + public void init(){ + stack=new Stack(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + } + + @Test + public void testPop() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(i, stack.pop()); + } + } + + @Test + public void testPeek() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(99, stack.peek()); + } + } + + @Test + public void testIsEmpty() { + for (int i = 99; i >=0; i--) { + stack.pop(); + } + Assert.assertEquals(true,stack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(100,stack.size()); + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java new file mode 100644 index 0000000000..88ef682cf9 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java @@ -0,0 +1,307 @@ +package com.nitasty.util; + +import java.util.Arrays; +import java.util.Objects; + +public class ArrayList implements List { + + // 定义内部常量 + private static final int DEFAULT_CAPACITY = 10; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int size; + + private Object[] elementData; + + /** + * + * 无参初始化为空数组,节省空间 + */ + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + /** + * + * @param initCapacity + */ + public ArrayList(int initCapacity) { + if (initCapacity > 0) { + elementData = new Object[initCapacity]; + } else if (initCapacity == 0) { + elementData = EMPTY_ELEMENTDATA; + } else { + throw new IllegalArgumentException("非法初始化容量" + initCapacity); + } + + } + + // TODO + public ArrayList(List list) { + list.toArray(); + + } + + /** + * 容量校验 + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + + if (elementData.length < minCapacity) { + grow(minCapacity); + } + + } + + /** + * 容量增长 + * + * @param minCapacity + * @return + */ + private void grow(int minCapacity) { + // 老容量 + int oldCapacity = this.elementData.length; + // 新容量为老容量的1.5倍 + int newCapacity = oldCapacity + oldCapacity >> 1; + // 新容量与最小容量比较 + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + } + // 新容量不能大于int最大值 + if (newCapacity > Integer.MAX_VALUE) { + newCapacity = Integer.MAX_VALUE; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + return indexOf(o) >= 0; //不写=会引发奇怪的bug + } + + @Override + public boolean add(Object o) { + + ensureCapacity(size + 1); + + elementData[size++] = o;// size比index大1 + + return true; + } + + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + @Override + public boolean add(int index, Object o) { + + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size + - index);// size出错就炸了 + elementData[index] = o; + return true; + } + + @Override + public boolean addAll(Object[] o) { + int numNew = o.length; + ensureCapacity(size + numNew); + System.arraycopy(o, 0, elementData, size, numNew); + size += numNew;// size增加 + return numNew != 0; + } + + @Override + public boolean addAll(int index, Object[] o) { + rangeCheck(index); + int numNew = o.length; + ensureCapacity(size + numNew); + + int numMoved = size - index;// 经过rangeCheck的index肯定小于size啊 + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, size + numNew, + numNew);// 将数组原元素移到后面 + System.arraycopy(o, 0, elementData, index, numNew);// 复制将要添加的元素 + + size += numNew;// 不要忘了啊啊啊!!! + return numNew != 0; + } + + @Override + public Object remove(int index) { + rangeCheck(index); + Object oldValue = elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + elementData[--size] = null;// Clear to let gc do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index=this.indexOf(o); + if(index==-1){ + return false; + }else{ + this.remove(index); + return true; + } + } + + @Override + /** + * 批量删除list + */ + public boolean removeAll(List list) { + Objects.requireNonNull(list); + return batchRemove(list,false); + } + + /** + * 用于实现removeALl和retainAll + * @param list + * @param complement + * @return + */ + private boolean batchRemove(List list, boolean complement) { + final Object[] elementData=this.elementData; + int r=0,w=0; + boolean modified=false; + try{ + for(;r= 0; i--) { + if (elementData[i] == null) { + return i; + } + } + } else { + for (int i = size-1; i >= 0; i--) { + if (o.equals(elementData[i])) { + return i; + } + } + } + return -1;// 没找到 + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size);//不要直接返回elementData + } + + @Override + public void clear() { + for(int i=0; i 必须是实现了Comparable的类 + */ +public class BinaryTree> { + + private BinaryNode root; + + public BinaryTree(){ + this.root=null; + } + + public BinaryTree(BinaryNode root) { + this.root = root; + } + + public void makeEmpty(){ + root=null; + } + + public boolean isEmpty(){ + return root==null; + } + + public int getHeight(){ + return height(root); + } + + public boolean contains(E x){ + return contains(x,root); + } + + + public E findMin(){ + if(isEmpty()) + throw new NullPointerException();//抛个什么错误合适呢?TODO + return (E) findMin(root).data; //为什么要求转型? + } + + + + public E findMax(){ + if(isEmpty()) + throw new NullPointerException();//抛个什么错误合适呢?TODO + return (E) findMax(root).data;//为什么要求转型? + } + + public void insert(E x){ + root=insert(x,root);//为啥要用root接返回值,因为这方法是递归的 + } + + public void remove(E x){ + root=remove(x,root);//为啥要用root接返回值,因为这方法是递归的 + } + + //打印树的data + public void printTree(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTree(root); + } + + public void printTreeStructure(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTreeStructure(root,0); + } + + private void printTreeStructure(BinaryNode t,int i) { + StringBuffer buff=new StringBuffer(); + if(t!=null){ + for(int j=0;j= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; // postion可以添加到最后 + } + + @Override + public boolean remove(Object o) { + + return false; + } + + @Override + public boolean removeAll(List list) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object set(int index, Object o) { + checkElementIndex(index); + Node node=node(index); + Object oldValue=node.data; + node.data=o; + return oldValue; + } + + @Override + public int indexOf(Object o) { + int index = 0; + if (o == null) { + for (Node x = first; x != null; x = x.next) { // 新的循环方式 + if (x.data == null) + return index; + index++; + } + } else { + for (Node x = first; x != null; x = x.next) { // 新的循环方式 + if (o.equals(x.data)) + return index; + index++; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + int index = size-1; + if (o == null) { + for (Node x = last; x != null; x = x.prev) { // 新的循环方式 + if (x.data == null) + return index; + index--; + } + } else { + for (Node x = last; x != null; x = x.prev) { // 新的循环方式 + if (o.equals(x.data)) + return index; + index--; + } + } + return -1; + } + + @Override + public Object[] toArray() { + Object[] elementData = new Object[size]; + int i = 0; + for (Node x = first; x != null; x = x.next) { + elementData[i++] = x.data; + } + return elementData; + } + + @Override + public void clear() { + // bug炸了 + // for(Node x=first;x!=null;x=x.next){ + // x=null; + // } + + for (Node x = first; x != null;) { + Node next = x.next; + x.prev = null; + x.data = null; + x.next = null; + x = next; + } + size = 0; + first = last = null; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + private Node lastRetured; + private Node next; + int cursor; + + @Override + public boolean hasNext() { + return cursor Date: Sun, 26 Feb 2017 16:20:28 +0800 Subject: [PATCH 10/31] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coding2017-1 | 1 + group05/371492887/task_01/.classpath | 7 +++++++ group05/371492887/task_01/.gitignore | 1 + group05/371492887/task_01/.project | 17 +++++++++++++++++ 4 files changed, 26 insertions(+) create mode 160000 coding2017-1 create mode 100644 group05/371492887/task_01/.classpath create mode 100644 group05/371492887/task_01/.gitignore create mode 100644 group05/371492887/task_01/.project diff --git a/coding2017-1 b/coding2017-1 new file mode 160000 index 0000000000..b5be7e853c --- /dev/null +++ b/coding2017-1 @@ -0,0 +1 @@ +Subproject commit b5be7e853cd9d2f5bbcc43149dc4df83749759a2 diff --git a/group05/371492887/task_01/.classpath b/group05/371492887/task_01/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group05/371492887/task_01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/371492887/task_01/.gitignore b/group05/371492887/task_01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/371492887/task_01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/371492887/task_01/.project b/group05/371492887/task_01/.project new file mode 100644 index 0000000000..eca593c703 --- /dev/null +++ b/group05/371492887/task_01/.project @@ -0,0 +1,17 @@ + + + task_01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From e978f24e217323f33e9320f2ba1f8e75e3050144 Mon Sep 17 00:00:00 2001 From: nitasty Date: Sun, 26 Feb 2017 16:23:33 +0800 Subject: [PATCH 11/31] test --- .../371492887/task_01/src/com/nitasty/test/ArrayListTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java index 907302f3c1..848b4fafe9 100644 --- a/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java +++ b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java @@ -17,7 +17,7 @@ public class ArrayListTest { public void init(){ list=new ArrayList(); for (int i = 0; i < 100; i++) { - list.add(i); + list.add(i); } } From 061e00c34520bee767c75ce29501fece42754dc0 Mon Sep 17 00:00:00 2001 From: zhanglifeng <284422826@qq.com> Date: Sun, 26 Feb 2017 21:40:28 +0800 Subject: [PATCH 12/31] =?UTF-8?q?=E5=AE=9E=E7=8E=B0ArrayList=E7=AD=89?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group05/284422826/.gitignore | 9 + .../coding2017/basic/ArrayList.java | 132 ++++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 37 ++++ .../coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 168 ++++++++++++++++++ .../zhanglifeng/coding2017/basic/List.java | 9 + .../zhanglifeng/coding2017/basic/Queue.java | 20 +++ .../zhanglifeng/coding2017/basic/Stack.java | 23 +++ 8 files changed, 405 insertions(+) create mode 100644 group05/284422826/.gitignore create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java create mode 100644 group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java diff --git a/group05/284422826/.gitignore b/group05/284422826/.gitignore new file mode 100644 index 0000000000..0aca6d5fe8 --- /dev/null +++ b/group05/284422826/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* \ No newline at end of file diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e0fefc0ab9 --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java @@ -0,0 +1,132 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.*; + +/** + * 鍔熻兘锛氬疄鐜癆rrayList. + * @author zhanglifeng. + */ +public class ArrayList implements List { + private int size = 0; //褰撳墠鏁扮粍澶у皬 + + private Object[] elementData = new Object[5]; //鍒濆鏁扮粍 + + /** + * 灏嗗璞娣诲姞鍒癆rrayList涓. + * @param o:闇瑕佹坊鍔犵殑瀵硅薄. + */ + public void add(Object o) { + ensureCapacity(size + 1); //纭繚鏁扮粍鐨勫閲忓彲浠ヨ鐨勪笅size + 1涓厓绱狅紝濡傛灉涓嶅鍒欐墿瀹 + + elementData[size] = o; //灏唎娣诲姞鍒版暟缁勪腑 + size++; //鏁扮粍澶у皬澧炲姞1 + } + + /** + * 灏嗗璞娣诲姞鍒癆rrayList鐨勬寚瀹氫綅缃. + * @param index: 鎸囧畾浣嶇疆. + * @param o: 闇瑕佹坊鍔犵殑瀵硅薄. + */ + public void add(int index, Object o) { + rangeCheck(index); //鍒ゆ柇鎸囧畾鐨勪綅缃甶ndex鏄惁鍚堟硶 + + ensureCapacity(size + 1); //纭繚鏁扮粍鐨勫閲忓彲浠ヨ鐨勪笅size + 1涓厓绱狅紝濡傛灉涓嶅鍒欐墿瀹 + + System.arraycopy(elementData, index, elementData, index + 1, size - index); //灏唅ndex浣嶇疆鍒扮粨鏉熶綅缃墍鏈夌殑鏁扮粍寰鍚庣Щ鍔ㄤ竴涓綅缃 + elementData[index] = o; //灏嗗璞娣诲姞鍒癷ndex浣嶇疆 + size++;//鏁扮粍澶у皬澧炲姞1 + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + + if (index != elementData.length - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + } + + size--; + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int number) { + if (number > elementData.length) { + elementData = grow(elementData, 1); + } + } + + public Object[] grow(Object[] src, int step) { + Object[] target = new Object[src.length + step]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public void rangeCheck(int index){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private class ArrayListIterator implements Iterator { + ArrayList arrayList = null; + int current = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + current++; + return current > arrayList.size() ? false : true; + } + + @Override + public Object next() { + return elementData[current]; + } + + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + arrayList.add("s1"); + arrayList.add("s2"); + arrayList.add("s3"); + arrayList.add("s4"); + arrayList.add(3, "s33"); + arrayList.add("s5"); + + System.out.println(arrayList.size()); + + System.out.println(arrayList.get(2)); + + arrayList.remove(3); + + System.out.println(arrayList.size()); + + arrayList.add("s1"); + System.out.println(arrayList.size()); + arrayList.remove(5); + System.out.println(arrayList.size()); + + Iterator it = arrayList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + } + +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..772f30c092 --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.github.zhanglifeng.coding2017.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return this.data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return this.left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return this.right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..288204f51f --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.zhanglifeng.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..c93a677bfd --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * 鍔熻兘锛氬疄鐜癓inkedList. + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + + if (0 == index) { + addFirst(o); + }else{ + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size ++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + + if(0 == index){ + return removeFirst(); + }else if(size - 1 == index){ + return removeLast(); + }else{ + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size --; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if(currentHead == null){ + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if(currentTail == null){ + head = newNode; + }else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if(head == null){ + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size --; + return node.data; + } + + public Object removeLast() { + if(tail == null){ + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size --; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current ++); + } + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("s1"); + linkedList.add("s2"); + linkedList.add("s3"); + linkedList.addFirst("s0"); + linkedList.addLast("s4"); + + Iterator it = linkedList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + System.out.println("绗3涓厓绱狅細" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("绗2涓厓绱狅細" + linkedList.remove(2)); + System.out.println(linkedList.size()); + } +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java new file mode 100644 index 0000000000..e6f5743399 --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.zhanglifeng.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java new file mode 100644 index 0000000000..39b9e4e98b --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java @@ -0,0 +1,20 @@ +package com.github.zhanglifeng.coding2017.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ba642a8c77 --- /dev/null +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java @@ -0,0 +1,23 @@ +package com.github.zhanglifeng.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0); + } + + public Object pop(){ + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} From 54db23ef96296b2361f10c46b3a0d319738a4e3e Mon Sep 17 00:00:00 2001 From: ZhaoHongxin Date: Sun, 26 Feb 2017 22:29:50 +0800 Subject: [PATCH 13/31] a --- .../basic/.gitignore | 0 .../basic/ArrayListTest.java | 12 ++++++-- .../basic/BinaryTreeNode.java | 2 +- .../basic/Iterator.java | 2 +- .../basic/LinkedList.java | 17 +++++++---- .../basic/LinkedListTest.java | 6 +++- .../basic/List.java | 3 +- .../test01/src/com/coding/basic/Queue.java | 25 +++++++++++++++++ .../src/com/coding/basic/QueueTest.java | 26 +++++++++++++++++ .../test01/src/com/coding/basic/Stack.java | 28 +++++++++++++++++++ .../src/com/coding/basic/StackTest.java | 26 +++++++++++++++++ .../src/com/coding1094051862/basic/Queue.java | 19 ------------- .../src/com/coding1094051862/basic/Stack.java | 22 --------------- 13 files changed, 136 insertions(+), 52 deletions(-) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/.gitignore (100%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/ArrayListTest.java (58%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/BinaryTreeNode.java (93%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/Iterator.java (69%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/LinkedList.java (89%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/LinkedListTest.java (82%) rename group05/1094051862/test01/src/com/{coding1094051862 => coding}/basic/List.java (76%) create mode 100644 group05/1094051862/test01/src/com/coding/basic/Queue.java create mode 100644 group05/1094051862/test01/src/com/coding/basic/QueueTest.java create mode 100644 group05/1094051862/test01/src/com/coding/basic/Stack.java create mode 100644 group05/1094051862/test01/src/com/coding/basic/StackTest.java delete mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java delete mode 100644 group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore b/group05/1094051862/test01/src/com/coding/basic/.gitignore similarity index 100% rename from group05/1094051862/test01/src/com/coding1094051862/basic/.gitignore rename to group05/1094051862/test01/src/com/coding/basic/.gitignore diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java b/group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java similarity index 58% rename from group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java rename to group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java index 6345b9c57e..8a5875427e 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/ArrayListTest.java +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; import org.junit.Assert; import org.junit.Test; @@ -7,7 +7,7 @@ public class ArrayListTest { @Test public void test() { - ArrayList list = new ArrayList(); + List list = new ArrayList(); for(int i = 0; i < 10; i++) { list.add(i); } @@ -18,6 +18,14 @@ public void test() { Assert.assertEquals(12, list.size()); Assert.assertEquals(99, list.remove(3)); Assert.assertEquals(11, list.size()); + Iterator iterator = list.iterator(); + for (int i = 0; i< list.size(); i++) { + System.out.println(list.get(i)); + } + System.out.println("======"); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } } } diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java b/group05/1094051862/test01/src/com/coding/basic/BinaryTreeNode.java similarity index 93% rename from group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java rename to group05/1094051862/test01/src/com/coding/basic/BinaryTreeNode.java index 09d35349b8..d7ac820192 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/BinaryTreeNode.java +++ b/group05/1094051862/test01/src/com/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; public class BinaryTreeNode { diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java b/group05/1094051862/test01/src/com/coding/basic/Iterator.java similarity index 69% rename from group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java rename to group05/1094051862/test01/src/com/coding/basic/Iterator.java index 33dcc89a6a..06ef6311b2 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/Iterator.java +++ b/group05/1094051862/test01/src/com/coding/basic/Iterator.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; public interface Iterator { public boolean hasNext(); diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java b/group05/1094051862/test01/src/com/coding/basic/LinkedList.java similarity index 89% rename from group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java rename to group05/1094051862/test01/src/com/coding/basic/LinkedList.java index ea3b54241b..4ef910fc16 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedList.java +++ b/group05/1094051862/test01/src/com/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; public class LinkedList implements List { @@ -114,16 +114,23 @@ public Object removeLast(){ } public Iterator iterator(){ return new Iterator() { + int cusor = 0; + Node current = head; @Override public Object next() { - // TODO Auto-generated method stub - return null; + if (!hasNext()) { + System.out.println("next : !hasNext"); + return null; + } + Object o = current.data; + current = current.next; + cusor ++; + return o; } @Override public boolean hasNext() { - // TODO Auto-generated method stub - return false; + return cusor < size; } }; } diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java b/group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java similarity index 82% rename from group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java rename to group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java index dccc8e1e86..ca269f7d9f 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/LinkedListTest.java +++ b/group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; import junit.framework.Assert; @@ -27,6 +27,10 @@ public void test() { Assert.assertEquals(3, list.get(5)); Assert.assertEquals(4, list.get(6)); Assert.assertEquals(800, list.get(8)); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } } } diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/List.java b/group05/1094051862/test01/src/com/coding/basic/List.java similarity index 76% rename from group05/1094051862/test01/src/com/coding1094051862/basic/List.java rename to group05/1094051862/test01/src/com/coding/basic/List.java index 7f1430b67b..ef939ae2cc 100644 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/List.java +++ b/group05/1094051862/test01/src/com/coding/basic/List.java @@ -1,4 +1,4 @@ -package com.coding1094051862.basic; +package com.coding.basic; public interface List { public void add(Object o); @@ -6,4 +6,5 @@ public interface List { public Object get(int index); public Object remove(int index); public int size(); + public Iterator iterator(); } diff --git a/group05/1094051862/test01/src/com/coding/basic/Queue.java b/group05/1094051862/test01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b65d01ab8e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + private List list = new ArrayList(); + private int size = 0; + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + if (size == 0) + return null; + size --; + return list.remove(0); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/QueueTest.java b/group05/1094051862/test01/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..f75beb397f --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/QueueTest.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Test; + +import sun.org.mozilla.javascript.internal.ast.NewExpression; + +public class QueueTest { + + @Test + public void test() { + Queue queue = new Queue(); + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + Assert.assertEquals(100, queue.size()); + for (int i = 0; i < 100; i++) { + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertEquals(0, queue.size()); + + } + +} diff --git a/group05/1094051862/test01/src/com/coding/basic/Stack.java b/group05/1094051862/test01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7fabb3494f --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private List elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if (size == 0) + return null; + return elementData.remove(--size); + } + + public Object peek(){ + if (size == 0) + return null; + return elementData.get(size - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/StackTest.java b/group05/1094051862/test01/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..adcb2c6522 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/StackTest.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class StackTest { + + @Test + public void test() { + Stack stack = new Stack(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + Assert.assertEquals(100, stack.size()); + Assert.assertEquals(99, stack.pop()); + for (int i = 98; i >= 0; i--) { + Assert.assertEquals(i, stack.peek()); + Assert.assertEquals(i, stack.pop()); + } + Assert.assertEquals(0, stack.size()); + } + +} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java deleted file mode 100644 index fb9f6107fd..0000000000 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding1094051862.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java b/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java deleted file mode 100644 index a9fbb4635b..0000000000 --- a/group05/1094051862/test01/src/com/coding1094051862/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.coding1094051862.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} From 2466967ef16be7bacd8db66a490e4a5951fe5903 Mon Sep 17 00:00:00 2001 From: zhanglifeng <284422826@qq.com> Date: Sun, 26 Feb 2017 22:34:21 +0800 Subject: [PATCH 14/31] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=92=8C=E6=A0=88=E7=9A=84=E8=B6=8A=E7=95=8C=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/zhanglifeng/coding2017/basic/Queue.java | 5 +++++ .../com/github/zhanglifeng/coding2017/basic/Stack.java | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java index 39b9e4e98b..42ef512321 100644 --- a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java @@ -1,5 +1,7 @@ package com.github.zhanglifeng.coding2017.basic; +import java.util.EmptyStackException; + public class Queue { private LinkedList elementData = new LinkedList(); public void enQueue(Object o){ @@ -7,6 +9,9 @@ public void enQueue(Object o){ } public Object deQueue(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } return elementData.removeFirst(); } diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java index ba642a8c77..0761bdd9e7 100644 --- a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java +++ b/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java @@ -1,5 +1,7 @@ package com.github.zhanglifeng.coding2017.basic; +import java.util.EmptyStackException; + public class Stack { private ArrayList elementData = new ArrayList(); @@ -8,10 +10,16 @@ public void push(Object o){ } public Object pop(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } return elementData.remove(elementData.size() - 1); } public Object peek(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ From 64bae7c49104a49344f6c2ea37f788422e0592b4 Mon Sep 17 00:00:00 2001 From: ZhaoHongxin Date: Sun, 26 Feb 2017 22:34:38 +0800 Subject: [PATCH 15/31] arraylist --- .../src/com/coding/basic/ArrayList.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 group05/1094051862/test01/src/com/coding/basic/ArrayList.java diff --git a/group05/1094051862/test01/src/com/coding/basic/ArrayList.java b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..af757a217e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + private int increaseSize = 3; + private void increaseArray() { + Object[] newData = Arrays.copyOf(elementData, elementData.length + increaseSize); + elementData = newData; + } + public void add(Object o){ + if (size == elementData.length) { + increaseArray(); + elementData[size++] = o; + } else { + elementData[size++] = o; + } + } + public void add(int index, Object o){ + if (index < 0 || index > size) { + System.out.println("閿欒鎻愮ず锛歩ndex > size || index < 0"); + return; + } + Object temp; + for (int i = index; i < size; i++) { + temp = elementData[i]; + elementData[i] = o; + o = temp; + } + elementData[size ++] = o; + } + + public Object get(int index){ + if (index < 0 || index > size ){ + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index > size ){ + return null; + } + Object result = elementData[index]; + for (int i = index; i < size-1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size-1] = null; + size --; + return result; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + private int cusor = 0; + @Override + public Object next() { + if (!hasNext()) { + System.out.println("next: !hasNext"); + return null; + } + return elementData[cusor ++]; + } + @Override + public boolean hasNext() { + return cusor < size; + } + }; + } +} From 079d6c4d84964491856f51e518026bafd1aa926b Mon Sep 17 00:00:00 2001 From: ZhaoHongxin Date: Sun, 26 Feb 2017 22:41:53 +0800 Subject: [PATCH 16/31] empty gitignore --- group05/1094051862/test01/src/com/coding/basic/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/group05/1094051862/test01/src/com/coding/basic/.gitignore b/group05/1094051862/test01/src/com/coding/basic/.gitignore index 709f74c2f7..e69de29bb2 100644 --- a/group05/1094051862/test01/src/com/coding/basic/.gitignore +++ b/group05/1094051862/test01/src/com/coding/basic/.gitignore @@ -1 +0,0 @@ -/ArrayList.java From 8f1a3ae50056684330844e2a201e2c2d0d839c2a Mon Sep 17 00:00:00 2001 From: xiaoqin0000 Date: Sun, 26 Feb 2017 23:10:57 +0800 Subject: [PATCH 17/31] add impl --- .../job_2017_02_19_data_structure/.gitignore | 3 +- .../.idea/workspace.xml | 448 ++++++++++++++---- .../src/com/java/xiaoqin/Main.java | 36 ++ .../com/java/xiaoqin/impl/ArrayListImpl.java | 5 + .../com/java/xiaoqin/impl/BinaryTreeNode.java | 82 ++++ .../com/java/xiaoqin/impl/LinkedListImpl.java | 39 +- .../src/com/java/xiaoqin/impl/StackImpl.java | 60 +++ .../com/java/xiaoqin/interfaces/IList.java | 6 + .../com/java/xiaoqin/interfaces/IQueue.java | 15 + 9 files changed, 590 insertions(+), 104 deletions(-) create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/Main.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/BinaryTreeNode.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/StackImpl.java create mode 100644 group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IQueue.java diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore index a2b48b352f..dd9d181eaa 100644 --- a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore @@ -3,4 +3,5 @@ Homestead.yaml Homestead.json .env -.idea \ No newline at end of file +.idea +.xml diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml index 2b612eeea0..0e2c5d87f8 100644 --- a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.idea/workspace.xml @@ -16,32 +16,64 @@ - - + + - - + + + + + + + + + + + + - - - - - - - - - - + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -49,8 +81,8 @@ - - + + @@ -72,7 +104,11 @@ package com.java.xiaoqin.impl; import com.java.xiaoqin.interfaces.IList; /** * Created by xiaoqin on 17-2-25. */ public class ArrayListImpl<T> implements IList<T> { private static final int DEFAULT_INIT_SIZE = 20; private T[] data; private int size = 0; public ArrayListImpl(){ this(DEFAULT_INIT_SIZE); } public ArrayListImpl(int capacity){ if (capacity < 0){ throw new IllegalArgumentException("鏃犳晥鐨刢apacity锛" + capacity); } if (0 == capacity){ capacity = DEFAULT_INIT_SIZE; } data = (T[]) new Object[capacity]; } @Override public void add(T t) { } @Override public void add(int index, T t) { } @Override public T get(int index) { return null; } @Override public T remove(int index) { return null; } @Override public int size() { return 0; } } gro + Object + + T + @@ -117,7 +159,9 @@ + + @@ -147,10 +191,6 @@ @@ -176,8 +216,6 @@ - - @@ -186,23 +224,27 @@ + - - - @@ -574,46 +624,49 @@ - - + - - - - - - - + + + + + + + - - - - - + + + + - + - - + + + - - - + + + @@ -627,24 +680,30 @@ file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java - 36 + 37 file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java - 53 + 54 file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/LinkedListImpl.java - 84 + 85 + + file://$PROJECT_DIR$/src/com/java/xiaoqin/impl/StackImpl.java + 13 + + - @@ -662,19 +724,139 @@