From 12c971e71672f1b36040675005cd6a71b80a0755 Mon Sep 17 00:00:00 2001 From: tanghaojie Date: Thu, 23 Feb 2017 13:11:38 +0800 Subject: [PATCH 01/18] create --- group12/495473393/Coding/.gitignore | 1 + .../495473393/Coding/src/Base/ArrayList.java | 32 +++++++++++++ .../Coding/src/Base/BinaryTreeNode.java | 32 +++++++++++++ .../495473393/Coding/src/Base/Iterator.java | 7 +++ .../495473393/Coding/src/Base/LinkedList.java | 46 +++++++++++++++++++ group12/495473393/Coding/src/Base/List.java | 9 ++++ group12/495473393/Coding/src/Base/Queue.java | 19 ++++++++ group12/495473393/Coding/src/Base/Stack.java | 22 +++++++++ 8 files changed, 168 insertions(+) create mode 100644 group12/495473393/Coding/.gitignore create mode 100644 group12/495473393/Coding/src/Base/ArrayList.java create mode 100644 group12/495473393/Coding/src/Base/BinaryTreeNode.java create mode 100644 group12/495473393/Coding/src/Base/Iterator.java create mode 100644 group12/495473393/Coding/src/Base/LinkedList.java create mode 100644 group12/495473393/Coding/src/Base/List.java create mode 100644 group12/495473393/Coding/src/Base/Queue.java create mode 100644 group12/495473393/Coding/src/Base/Stack.java diff --git a/group12/495473393/Coding/.gitignore b/group12/495473393/Coding/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/495473393/Coding/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/495473393/Coding/src/Base/ArrayList.java b/group12/495473393/Coding/src/Base/ArrayList.java new file mode 100644 index 0000000000..0b99f87873 --- /dev/null +++ b/group12/495473393/Coding/src/Base/ArrayList.java @@ -0,0 +1,32 @@ +package Base; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group12/495473393/Coding/src/Base/BinaryTreeNode.java b/group12/495473393/Coding/src/Base/BinaryTreeNode.java new file mode 100644 index 0000000000..875abe8c80 --- /dev/null +++ b/group12/495473393/Coding/src/Base/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package Base; + +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/group12/495473393/Coding/src/Base/Iterator.java b/group12/495473393/Coding/src/Base/Iterator.java new file mode 100644 index 0000000000..19b65c3637 --- /dev/null +++ b/group12/495473393/Coding/src/Base/Iterator.java @@ -0,0 +1,7 @@ +package Base; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/495473393/Coding/src/Base/LinkedList.java b/group12/495473393/Coding/src/Base/LinkedList.java new file mode 100644 index 0000000000..0d4ce20167 --- /dev/null +++ b/group12/495473393/Coding/src/Base/LinkedList.java @@ -0,0 +1,46 @@ +package Base; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group12/495473393/Coding/src/Base/List.java b/group12/495473393/Coding/src/Base/List.java new file mode 100644 index 0000000000..6c910af600 --- /dev/null +++ b/group12/495473393/Coding/src/Base/List.java @@ -0,0 +1,9 @@ +package Base; + +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/group12/495473393/Coding/src/Base/Queue.java b/group12/495473393/Coding/src/Base/Queue.java new file mode 100644 index 0000000000..9c2948d7e3 --- /dev/null +++ b/group12/495473393/Coding/src/Base/Queue.java @@ -0,0 +1,19 @@ +package Base; + +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/group12/495473393/Coding/src/Base/Stack.java b/group12/495473393/Coding/src/Base/Stack.java new file mode 100644 index 0000000000..8f59343d4b --- /dev/null +++ b/group12/495473393/Coding/src/Base/Stack.java @@ -0,0 +1,22 @@ +package Base; + +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 ccf95b4f73d4edee62204968c9e763201cacea19 Mon Sep 17 00:00:00 2001 From: guodongym Date: Thu, 23 Feb 2017 13:28:16 +0800 Subject: [PATCH 02/18] =?UTF-8?q?list=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group12/377401843/learning_1/.gitignore | 3 + .../com/guodong/datastructure/ArrayList.java | 171 ++++++++++++++++++ .../guodong/datastructure/BinaryTreeNode.java | 32 ++++ .../com/guodong/datastructure/Iterator.java | 7 + .../com/guodong/datastructure/LinkedList.java | 45 +++++ .../src/com/guodong/datastructure/List.java | 14 ++ .../src/com/guodong/datastructure/Queue.java | 19 ++ .../src/com/guodong/datastructure/Stack.java | 22 +++ .../datastructure/test/ArrayListTest.java | 53 ++++++ 9 files changed, 366 insertions(+) create mode 100644 group12/377401843/learning_1/.gitignore create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/List.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java diff --git a/group12/377401843/learning_1/.gitignore b/group12/377401843/learning_1/.gitignore new file mode 100644 index 0000000000..9c47ca7e58 --- /dev/null +++ b/group12/377401843/learning_1/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/.project +/.classpath diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java new file mode 100644 index 0000000000..a1ee0ee339 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java @@ -0,0 +1,171 @@ +package com.guodong.datastructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 按下标顺序新增元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size] = o; + size++; + } + + /** + * 在指定下标位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkRangeForAdd(index); + + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + /** + * 根据下标获取列表数据 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkRangeForGetOrRemove(index); + + return elementData[index]; + } + + /** + * 根据下标移除元素,并返回移除的元素值 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkRangeForGetOrRemove(index); + + Object oldValue = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index -1); + + size--; + elementData[size] = null; + + return oldValue; + } + + /** + * 获得列表长度 + * + * @Method size + * @return + * @see com.guodong.datastructure.List#size() + */ + public int size() { + return size; + } + + /** + * 获取ArrayList的迭代器 + * + * @MethodName iterator + * @author zhaogd + * @date 2017年2月21日 下午8:19:28 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + /** + * 确保数组容量足够,如果不够则扩充数组 + * + * @MethodName ensureCapacityInternal + * @author zhaogd + * @date 2017年2月21日 下午5:06:46 + * @param minCapacity + */ + private void ensureCapacityInternal(int minCapacity) { + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + /** + * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 + * + * @MethodName grow + * @author zhaogd + * @date 2017年2月21日 下午5:20:55 + * @param minCapacity + */ + private void grow(int minCapacity) { + minCapacity = elementData.length + elementData.length / 2; + elementData = Arrays.copyOf(elementData, minCapacity); + } + + /** + * 检查Add方法的下标范围是否合法 + * + * @MethodName checkRangeForAdd + * @author zhaogd + * @date 2017年2月21日 下午7:32:55 + * @param index + */ + private void checkRangeForAdd(int index) { + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查Get或者Remove方法的下标范围是否合法 + * + * @MethodName checkRangeForGetOrRemove + * @author zhaogd + * @date 2017年2月21日 下午7:33:21 + * @param index + */ + private void checkRangeForGetOrRemove(int index) { + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + + private class ArrayListIterator implements Iterator{ + + private int lastIndex = 0; + + @Override + public boolean hasNext() { + return lastIndex < size; + } + + @Override + public Object next() { + return elementData[lastIndex++]; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java new file mode 100644 index 0000000000..933e72acda --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.guodong.datastructure; + +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/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java new file mode 100644 index 0000000000..1a9bc5ad8a --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java @@ -0,0 +1,7 @@ +package com.guodong.datastructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java new file mode 100644 index 0000000000..fc801505f1 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -0,0 +1,45 @@ +package com.guodong.datastructure; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java new file mode 100644 index 0000000000..2471c15d21 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java @@ -0,0 +1,14 @@ +package com.guodong.datastructure; + +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/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java new file mode 100644 index 0000000000..d016046bc5 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java @@ -0,0 +1,19 @@ +package com.guodong.datastructure; + +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/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java new file mode 100644 index 0000000000..a61c7424aa --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java @@ -0,0 +1,22 @@ +package com.guodong.datastructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java new file mode 100644 index 0000000000..e22d84f72c --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java @@ -0,0 +1,53 @@ +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.ArrayList; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + ArrayList arrayList = new ArrayList(); + arrayList.add(1); + assertEquals(1, arrayList.get(0)); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } +} From bddcf6e28a648405325343e52a3c4eb5b79cfdeb Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Thu, 23 Feb 2017 13:40:27 +0800 Subject: [PATCH 03/18] my project --- group12/2258659044/readme.txt | 1 + .../src/com/coding/basic/ArrayList.java | 100 +++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 81 +++++++++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 137 ++++++++++++++++++ .../zj-2017/src/com/coding/basic/List.java | 10 ++ .../zj-2017/src/com/coding/basic/Queue.java | 26 ++++ .../zj-2017/src/com/coding/basic/Stack.java | 28 ++++ .../test/com/coding/basic/ArrayListTest.java | 109 ++++++++++++++ .../test/com/coding/basic/LinkedListTest.java | 109 ++++++++++++++ 10 files changed, 609 insertions(+) create mode 100644 group12/2258659044/readme.txt create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/List.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/Queue.java create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/Stack.java create mode 100644 group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java create mode 100644 group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java diff --git a/group12/2258659044/readme.txt b/group12/2258659044/readme.txt new file mode 100644 index 0000000000..59c627dd7e --- /dev/null +++ b/group12/2258659044/readme.txt @@ -0,0 +1 @@ +this is my projectWorkcpace! \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..16cd3a0fc1 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + /*扩容因子*/ + private static final int GENE = 10; + + private Object[] elementData = new Object[10]; + /*扩容引用*/ + private Object[] newElementData; + + public void add(Object o){ + if(size>=elementData.length){//扩容 + grow(); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + if(size>=elementData.length||index>=elementData.length-1){//长度不够需要扩容 + grow(); + } + if(indexsize){ + size = index+1; + }else{ + size ++; + } + } + + public Object get(int index){ + + if(index>=size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..c167c877c3 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,81 @@ +package com.coding.basic; +public class BinaryTreeNode { + + private Object data; + //根节点 + private BinaryTreeNode root; + //父节点 + private BinaryTreeNode parent; + private BinaryTreeNode left; + private BinaryTreeNode right; + //所有数据集合 + private final List datas = new ArrayList(); + 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; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public > BinaryTreeNode insert(T o){ + + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.data = o; + if(root == null){ + root = treeNode; + root.root = treeNode; + }else{ + BinaryTreeNode currentNode = root; + while(true){ + parent = currentNode; + if(((Comparable)currentNode.data).compareTo(o)>0){//向左放 + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parent.left = treeNode; + treeNode.parent = parent; + treeNode.root = root; + break; + } + }else{//向右放 + currentNode = currentNode.getRight(); + if(currentNode == null){ + parent.right = treeNode; + treeNode.parent = parent; + treeNode.root = root; + break; + } + } + } + } + return treeNode; + } + + /** + * 先序遍历 + * @param node + * @return + */ + public List traversal(BinaryTreeNode node){ + + if(node !=null){ + datas.add(node.data); + traversal(node.left); + traversal(node.right); + } + return datas; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java b/group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..c854120212 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java b/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..460298ff56 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + + Node addNode = new Node(); + addNode.data = o; + if(size==0){ + head = addNode; + }else{ + //获取最后一个节点 + Node lastNode = getPointNode(size-1); + lastNode.next = addNode; + } + size++; + } + public void add(int index , Object o){ + + Node addNode = new Node(); + addNode.data = o; + if(index == 0){ + addFirst(o); + return; + } + if(index == size){ + Node lastNode = getPointNode(size-1); + lastNode.next = addNode; + }else{ + Node pointNode = getPointNode(index); + Node prePointNode = getPointNode(index-1); + prePointNode.next = addNode; + addNode.next = pointNode; + } + size ++; + } + public Object get(int index){ + + Node node = getPointNode(index); + return node.data; + } + + public Object remove(int index){ + + Node pointNode = getPointNode(index); + Node nextPointNode = getPointNode(index+1); + if(index ==0){ + head = nextPointNode; + }else{ + Node prePointNode = getPointNode(index-1); + prePointNode.next = nextPointNode; + } + size --; + return pointNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + Node secondNode = head; + head = new Node(); + head.data = o; + if(size>0){ + head.next = secondNode; + } + size ++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + + return remove(0); + } + + public Object removeLast(){ + + return remove(size-1); + } + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != LinkedList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= LinkedList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return LinkedList.this.get(i); + } + + } + + /** + * 获取指定的节点 + * @return + */ + private Node getPointNode(int index){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size+""); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node{ + Object data; + Node next; + + } +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/List.java b/group12/2258659044/zj-2017/src/com/coding/basic/List.java new file mode 100644 index 0000000000..a5a3688eb6 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java b/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..e29ff65ddf --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList element = new LinkedList(); + + public void enQueue(Object o){ + + element.add(o); + } + + public Object deQueue(){ + + return element.removeFirst(); + } + + public boolean isEmpty(){ + + return element.size()==0; + } + + public int size(){ + + return element.size(); + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Stack.java b/group12/2258659044/zj-2017/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..03709097e5 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object pop(){ + + return elementData.remove(size()-1); + } + + public Object peek(){ + + return elementData.get(size()-1); + } + public boolean isEmpty(){ + + return size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..e7613ee689 --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,109 @@ +package test.com.coding.basic; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayListTest { + + ArrayList ls ; + @Before + public void setup() { + ls = new ArrayList(); + } + + /** + * 测试一个参数的add方法 + * ArrayList当数据超过10时进行第一次扩容 + */ + @Test + public void add(){ + + ls.add(3); + ls.add("a"); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.size(), 12); + Assert.assertEquals(ls.get(1), "a"); + } + + /** + * 两个参数的add方法 + */ + @Test(expected = IndexOutOfBoundsException.class) + public void add4ToPramter(){ + + ls.add(0, 0); + ls.add(1,1); + ls.add(2, 2); + ls.add(3,3); + for (int i = 0; i < 10; i++) { + ls.add(3,i); + } + Assert.assertEquals(ls.size(), 14); + Assert.assertEquals(ls.get(3), 9); + Assert.assertEquals(ls.get(13), 3); + ls.add(15, "a"); + } + + /** + * get(i) + */ + @Test(expected = IndexOutOfBoundsException.class) + public void get(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + ls.add(11,11); + Assert.assertEquals(ls.get(9), 9); + Assert.assertEquals(ls.get(10),null); + ls.get(12); + } + + @Test + public void remove(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.remove(5),5); + Assert.assertEquals(ls.size(),9); + Assert.assertEquals(ls.remove(8),9); + Assert.assertEquals(ls.size(),8); + } + + @Test + public void size(){ + + Assert.assertEquals(ls.size(),0); + ls.add("a"); + Assert.assertEquals(ls.size(),1); + ls.add(0,0); + Assert.assertEquals(ls.size(),2); + ls.remove(0); + Assert.assertEquals(ls.size(),1); + + } + + @Test(expected = NoSuchElementException.class) + public void iterator(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Iterator it = ls.iterator(); + Assert.assertEquals(it.hasNext(),true); + for (int i = 0; i < 10; i++) { + it.next(); + } + Assert.assertEquals(it.hasNext(),false); + it.next(); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..e7b56eccff --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,109 @@ +package test.com.coding.basic; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + LinkedList ls ; + @Before + public void setup() { + ls = new LinkedList(); + } + + /** + * 测试一个参数的add方法 + * ArrayList当数据超过10时进行第一次扩容 + */ + @Test + public void add(){ + + ls.add(3); + ls.add("a"); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.size(), 12); + Assert.assertEquals(ls.get(1), "a"); + } + + /** + * 两个参数的add方法 + */ + @Test(expected = IndexOutOfBoundsException.class) + public void add4ToPramter(){ + + ls.add(0, 0); + ls.add(1,1); + ls.add(2, 2); + ls.add(3,3); + for (int i = 0; i < 10; i++) { + ls.add(3,i); + } + Assert.assertEquals(ls.size(), 14); + Assert.assertEquals(ls.get(3), 9); + Assert.assertEquals(ls.get(13), 3); + ls.add(15, "a"); + } + + /** + * get(i) + */ + @Test(expected = IndexOutOfBoundsException.class) + public void get(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + ls.add(11,11); + Assert.assertEquals(ls.get(9), 9); + Assert.assertEquals(ls.get(10),null); + ls.get(12); + } + + @Test + public void remove(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.remove(5),5); + Assert.assertEquals(ls.size(),9); + Assert.assertEquals(ls.remove(8),9); + Assert.assertEquals(ls.size(),8); + } + + @Test + public void size(){ + + Assert.assertEquals(ls.size(),0); + ls.add("a"); + Assert.assertEquals(ls.size(),1); + ls.add(0,0); + Assert.assertEquals(ls.size(),2); + ls.remove(0); + Assert.assertEquals(ls.size(),1); + + } + + @Test(expected = NoSuchElementException.class) + public void iterator(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Iterator it = ls.iterator(); + Assert.assertEquals(it.hasNext(),true); + for (int i = 0; i < 10; i++) { + it.next(); + } + Assert.assertEquals(it.hasNext(),false); + it.next(); + } +} From 9f1df17e632be18aacd30ed801defb083f30b9d2 Mon Sep 17 00:00:00 2001 From: guodongym Date: Thu, 23 Feb 2017 17:24:13 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/guodong/datastructure/LinkedList.java | 166 +++++++++++++++--- 1 file changed, 144 insertions(+), 22 deletions(-) diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index fc801505f1..7616768930 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -1,45 +1,167 @@ package com.guodong.datastructure; public class LinkedList implements List { - + + private int size; + private Node head; - - public void add(Object o){ - + + private Node last; + + /** + * 向 链表尾端插入元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + linkLast(o); } - public void add(int index , Object o){ - + + /** + * 向链表指定位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkIndexForAdd(index); + + if (index == size) { + linkLast(o); + } else { + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 + head = newNode; + } else { + prevNode.next = newNode; + } + size++; + } } - public Object get(int index){ - return null; + + /** + * 根据下标获取链表中元素 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkIndexForGet(index); + return getNodeByIndex(index).data; } - public Object remove(int index){ + + public Object remove(int index) { return null; } - - public int size(){ - return -1; + + public int size() { + return size; } - - public void addFirst(Object o){ - + + public void addFirst(Object o) { + } - public void addLast(Object o){ - + + public void addLast(Object o) { + linkLast(o); } - public Object removeFirst(){ + + public Object removeFirst() { return null; } - public Object removeLast(){ + + public Object removeLast() { return null; } - public Iterator iterator(){ + + public Iterator iterator() { return null; } + + /** + * 根据下标获取对应的节点 + * + * @MethodName getNodeByIndex + * @author zhaogd + * @date 2017年2月23日 下午3:32:48 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + /** + * 在链表尾端插入节点 + * + * @MethodName linkLast + * @author zhaogd + * @date 2017年2月23日 下午3:14:28 + * @param o + */ + private void linkLast(Object o) { + Node n = last; // 取出原尾端数据 + Node newNode = new Node(o, null); // 创建新节点 + last = newNode; // 把新节点放入链表尾端 + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 + // 如果不为空,把原尾端节点指向新节点 + if (n == null) { + head = newNode; + } else { + n.next = newNode; + } + + size++; + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForAdd + * @author zhaogd + * @date 2017年2月23日 下午3:05:07 + * @param index + */ + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } - - private static class Node{ + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForGet + * @author zhaogd + * @date 2017年2月23日 下午4:21:35 + * @param index + */ + private void checkIndexForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private static class Node { Object data; Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } } } From 77348a7293058cc6b2aacd8b4f92c3f49d5facd1 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Thu, 23 Feb 2017 17:45:10 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/com/coding/basic/ArrayListTest.java | 22 +- .../test/com/coding/basic/LinkedListTest.java | 218 +++++++++--------- 2 files changed, 120 insertions(+), 120 deletions(-) diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java index e7613ee689..badcb2968f 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java @@ -1,7 +1,5 @@ package test.com.coding.basic; -import java.util.NoSuchElementException; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -36,7 +34,7 @@ public void add(){ /** * 两个参数的add方法 */ - @Test(expected = IndexOutOfBoundsException.class) + @Test//(expected = IndexOutOfBoundsException.class) public void add4ToPramter(){ ls.add(0, 0); @@ -49,22 +47,23 @@ public void add4ToPramter(){ Assert.assertEquals(ls.size(), 14); Assert.assertEquals(ls.get(3), 9); Assert.assertEquals(ls.get(13), 3); - ls.add(15, "a"); + //打开下面操作抛出异常 + //ls.add(15, "a"); } /** * get(i) */ - @Test(expected = IndexOutOfBoundsException.class) + @Test//(expected = IndexOutOfBoundsException.class) public void get(){ for (int i = 0; i < 10; i++) { ls.add(i); } - ls.add(11,11); - Assert.assertEquals(ls.get(9), 9); - Assert.assertEquals(ls.get(10),null); - ls.get(12); + + Assert.assertEquals(ls.get(9), 9); + //打开下面操作抛出异常 + //ls.get(12); } @Test @@ -92,7 +91,7 @@ public void size(){ } - @Test(expected = NoSuchElementException.class) + @Test//(expected = NoSuchElementException.class) public void iterator(){ for (int i = 0; i < 10; i++) { @@ -104,6 +103,7 @@ public void iterator(){ it.next(); } Assert.assertEquals(it.hasNext(),false); - it.next(); + //打开下面操作抛出异常 + //it.next(); } } diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java index e7b56eccff..a55b2d5a3f 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java @@ -1,109 +1,109 @@ -package test.com.coding.basic; - -import java.util.NoSuchElementException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Iterator; -import com.coding.basic.LinkedList; - -public class LinkedListTest { - - LinkedList ls ; - @Before - public void setup() { - ls = new LinkedList(); - } - - /** - * 测试一个参数的add方法 - * ArrayList当数据超过10时进行第一次扩容 - */ - @Test - public void add(){ - - ls.add(3); - ls.add("a"); - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.size(), 12); - Assert.assertEquals(ls.get(1), "a"); - } - - /** - * 两个参数的add方法 - */ - @Test(expected = IndexOutOfBoundsException.class) - public void add4ToPramter(){ - - ls.add(0, 0); - ls.add(1,1); - ls.add(2, 2); - ls.add(3,3); - for (int i = 0; i < 10; i++) { - ls.add(3,i); - } - Assert.assertEquals(ls.size(), 14); - Assert.assertEquals(ls.get(3), 9); - Assert.assertEquals(ls.get(13), 3); - ls.add(15, "a"); - } - - /** - * get(i) - */ - @Test(expected = IndexOutOfBoundsException.class) - public void get(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - ls.add(11,11); - Assert.assertEquals(ls.get(9), 9); - Assert.assertEquals(ls.get(10),null); - ls.get(12); - } - - @Test - public void remove(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.remove(5),5); - Assert.assertEquals(ls.size(),9); - Assert.assertEquals(ls.remove(8),9); - Assert.assertEquals(ls.size(),8); - } - - @Test - public void size(){ - - Assert.assertEquals(ls.size(),0); - ls.add("a"); - Assert.assertEquals(ls.size(),1); - ls.add(0,0); - Assert.assertEquals(ls.size(),2); - ls.remove(0); - Assert.assertEquals(ls.size(),1); - - } - - @Test(expected = NoSuchElementException.class) - public void iterator(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Iterator it = ls.iterator(); - Assert.assertEquals(it.hasNext(),true); - for (int i = 0; i < 10; i++) { - it.next(); - } - Assert.assertEquals(it.hasNext(),false); - it.next(); - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + LinkedList ls ; + @Before + public void setup() { + ls = new LinkedList(); + } + + /** + * 测试一个参数的add方法 + * ArrayList当数据超过10时进行第一次扩容 + */ + @Test + public void add(){ + + ls.add(3); + ls.add("a"); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.size(), 12); + Assert.assertEquals(ls.get(1), "a"); + } + + /** + * 两个参数的add方法 + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void add4ToPramter(){ + + ls.add(0, 0); + ls.add(1,1); + ls.add(2, 2); + ls.add(3,3); + for (int i = 0; i < 10; i++) { + ls.add(3,i); + } + Assert.assertEquals(ls.size(), 14); + Assert.assertEquals(ls.get(3), 9); + Assert.assertEquals(ls.get(13), 3); + //打开下面操作抛出异常 + //ls.add(15, "a"); + } + + /** + * get(i) + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void get(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + + Assert.assertEquals(ls.get(9), 9); + //打开下面操作抛出异常 + //ls.get(12); + } + + @Test + public void remove(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.remove(5),5); + Assert.assertEquals(ls.size(),9); + Assert.assertEquals(ls.remove(8),9); + Assert.assertEquals(ls.size(),8); + } + + @Test + public void size(){ + + Assert.assertEquals(ls.size(),0); + ls.add("a"); + Assert.assertEquals(ls.size(),1); + ls.add(0,0); + Assert.assertEquals(ls.size(),2); + ls.remove(0); + Assert.assertEquals(ls.size(),1); + + } + + @Test//(expected = NoSuchElementException.class) + public void iterator(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Iterator it = ls.iterator(); + Assert.assertEquals(it.hasNext(),true); + for (int i = 0; i < 10; i++) { + it.next(); + } + Assert.assertEquals(it.hasNext(),false); + //打开下面操作抛出异常 + //it.next(); + } +} From 3dd4ea97a602fd26cfe3a59109e4b9f45a18e987 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Fri, 24 Feb 2017 13:17:53 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=9B=B4=E6=96=B0=E4=BB=A5=E5=8F=8A=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/ArrayList.java | 200 +++++++++--------- .../src/com/coding/basic/BinaryTree.java | 64 ++++++ .../src/com/coding/basic/BinaryTreeNode.java | 118 ++++------- .../test/com/coding/basic/BinaryTreeTest.java | 58 +++++ .../src/test/com/coding/basic/QueueTest.java | 64 ++++++ .../src/test/com/coding/basic/StackTest.java | 76 +++++++ 6 files changed, 399 insertions(+), 181 deletions(-) create mode 100644 group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java create mode 100644 group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java create mode 100644 group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java create mode 100644 group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index 16cd3a0fc1..d08cf467eb 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -1,100 +1,100 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - /*扩容因子*/ - private static final int GENE = 10; - - private Object[] elementData = new Object[10]; - /*扩容引用*/ - private Object[] newElementData; - - public void add(Object o){ - if(size>=elementData.length){//扩容 - grow(); - } - elementData[size] = o; - size ++; - } - public void add(int index, Object o){ - - if(index>size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - if(size>=elementData.length||index>=elementData.length-1){//长度不够需要扩容 - grow(); - } - if(indexsize){ - size = index+1; - }else{ - size ++; - } - } - - public Object get(int index){ - - if(index>=size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - return elementData[index]; - } - - public Object remove(int index){ - - Object o = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); - size --; - return o; - } - - public int size(){ - return size; - } - - /** - * 扩容,扩容因子为10 - */ - private void grow(){ - - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; - } - - - public Iterator iterator(){ - - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != ArrayList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= ArrayList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return ArrayList.this.elementData[i]; - } - - } -} +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + /*扩容因子*/ + private static final int GENE = 10; + + private Object[] elementData = new Object[10]; + /*扩容引用*/ + private Object[] newElementData; + + public void add(Object o){ + if(size>=elementData.length){//扩容 + grow(); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + if(size>=elementData.length||index>=elementData.length-1){//长度不够需要扩容 + grow(); + } + if(indexsize){ + size = index+1; + }else{ + size ++; + } + } + + public Object get(int index){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..e5fae50203 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +public class BinaryTree { + + //根节点 + private BinaryTreeNode root; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public > BinaryTreeNode insert(T o){ + + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.setData(o); + if(root == null){ + root = treeNode; + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parent; + while(true){ + parent = currentNode; + if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parent.setLeft(treeNode); + treeNode.setParent(parent); + break; + } + }else{//向右放 + currentNode = currentNode.getRight(); + if(currentNode == null){ + parent.setRight(treeNode); + treeNode.setParent(parent); + break; + } + } + } + } + return treeNode; + } + + /** + * 先序遍历 + * @param node + * @return + */ + public List traversalBefore(BinaryTreeNode node){ + //所有数据集合 + List datas = new ArrayList(); + return traversal(node,datas); + } + private List traversal(BinaryTreeNode node,List datas){ + + if(node !=null){ + datas.add(node.getData()); + traversal(node.getLeft(),datas); + traversal(node.getRight(),datas); + } + return datas; + } + + public BinaryTreeNode getRoot() { + return root; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java index c167c877c3..557728a02a 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java @@ -1,81 +1,37 @@ -package com.coding.basic; -public class BinaryTreeNode { - - private Object data; - //根节点 - private BinaryTreeNode root; - //父节点 - private BinaryTreeNode parent; - private BinaryTreeNode left; - private BinaryTreeNode right; - //所有数据集合 - private final List datas = new ArrayList(); - 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; - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public > BinaryTreeNode insert(T o){ - - BinaryTreeNode treeNode = new BinaryTreeNode(); - treeNode.data = o; - if(root == null){ - root = treeNode; - root.root = treeNode; - }else{ - BinaryTreeNode currentNode = root; - while(true){ - parent = currentNode; - if(((Comparable)currentNode.data).compareTo(o)>0){//向左放 - currentNode = currentNode.getLeft(); - if(currentNode == null){ - parent.left = treeNode; - treeNode.parent = parent; - treeNode.root = root; - break; - } - }else{//向右放 - currentNode = currentNode.getRight(); - if(currentNode == null){ - parent.right = treeNode; - treeNode.parent = parent; - treeNode.root = root; - break; - } - } - } - } - return treeNode; - } - - /** - * 先序遍历 - * @param node - * @return - */ - public List traversal(BinaryTreeNode node){ - - if(node !=null){ - datas.add(node.data); - traversal(node.left); - traversal(node.right); - } - return datas; - } - -} +package com.coding.basic; +public class BinaryTreeNode { + + private Object data; + //父节点 + private BinaryTreeNode parent; + 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 getParent() { + return parent; + } + public void setParent(BinaryTreeNode parent) { + this.parent = parent; + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java new file mode 100644 index 0000000000..0f343ed895 --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java @@ -0,0 +1,58 @@ +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.List; + +public class BinaryTreeTest { + + BinaryTree tree ; + + @Before + public void setup() { + + tree = new BinaryTree(); + Assert.assertEquals(tree.getRoot(), null); + tree.insert(5); + tree.insert(2); + tree.insert(7); + tree.insert(1); + tree.insert(6); + } + @Test + public void insert(){ + + BinaryTreeNode node = tree.insert(4); + Assert.assertEquals(node.getParent().getData(), 2); + Assert.assertEquals(node.getParent().getLeft().getData(), 1); + + BinaryTreeNode node2 = tree.insert(8); + Assert.assertEquals(node2.getParent().getData(), 7); + Assert.assertEquals(node2.getParent().getLeft().getData(), 6); + } + + @Test + public void traversal(){ + + insert(); + //以根节点为起点先序遍历 + List treeList = tree.traversalBefore(tree.getRoot()); + //expected value + int[] exValue = {5,2,1,4,7,6,8}; + for (int i = 0; i < exValue.length; i++) { + Assert.assertEquals(treeList.get(i),exValue[i]); + } + + //以数据2位起点先序遍历 + List treeList2 = tree.traversalBefore(tree.getRoot().getLeft()); + //expected value + int[] exValue2 = {2,1,4}; + for (int i = 0; i < exValue2.length; i++) { + Assert.assertEquals(treeList2.get(i),exValue2[i]); + } + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..e688d9b41f --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java @@ -0,0 +1,64 @@ +package test.com.coding.basic; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + Queue qe ; + + @Before + public void setup() { + qe = new Queue(); + for (int i = 0; i < 10; i++) { + qe.enQueue(i); + } + } + + @Test + public void enQueue(){ + + Assert.assertEquals(qe.size(), 10); + qe.enQueue("abcd"); + Assert.assertEquals(qe.size(), 11); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void deQueue(){ + + Assert.assertEquals(qe.size(), 10); + for (int i = 0; i < 10; i++) { + Assert.assertEquals(qe.deQueue(), i); + } + Assert.assertEquals(qe.size(), 0); + //打开下列语句与期望异常测试 + //qe.deQueue(); + } + + public void isEmpty(){ + + Assert.assertEquals(qe.isEmpty(),false); + for (int i = 0; i < 10; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.isEmpty(),true); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(qe.size(),10); + qe.enQueue("lk"); + qe.enQueue('h'); + Assert.assertEquals(qe.size(),12); + for (int i = 0; i < 12; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.size(),0); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.size(), 0); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..a0875c8f3c --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java @@ -0,0 +1,76 @@ +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + Stack st ; + + @Before + public void setup() { + st = new Stack(); + for (int i = 0; i < 10; i++) { + st.push(i); + } + } + + @Test + public void push(){ + + Assert.assertEquals(st.size(), 10); + st.push(10); + st.push('a'); + Assert.assertEquals(st.size(), 12); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void pop(){ + + Assert.assertEquals(st.size(), 10); + for (int i = 9; i >= 0; i--) { + Assert.assertEquals(st.pop(), i); + } + //打开下列语句抛出期望异常 + //st.pop(); + } + + @Test + public void peek(){ + + Assert.assertEquals(st.size(), 10); + Assert.assertEquals(st.peek(), 9); + Assert.assertEquals(st.size(), 10); + } + + @Test + public void isEmpty(){ + + Assert.assertEquals(st.isEmpty(), false); + for (int i = 0; i < 10; i++) { + st.pop(); + } + Assert.assertEquals(st.isEmpty(), true); + Stack st1 = new Stack(); + Assert.assertEquals(st1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(st.size(),10); + st.push("lk"); + st.push('h'); + Assert.assertEquals(st.size(),12); + for (int i = 0; i < 12; i++) { + st.pop(); + } + Assert.assertEquals(st.size(),0); + st.peek(); + Assert.assertEquals(st.size(),0); + Stack st1 = new Stack(); + Assert.assertEquals(st1.size(), 0); + } +} From 879f3631c4ae0a9fe4cb736e5ebfeef7f8ddc944 Mon Sep 17 00:00:00 2001 From: guodongym Date: Fri, 24 Feb 2017 16:35:27 +0800 Subject: [PATCH 07/18] add add --- .../com/guodong/datastructure/LinkedList.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index 7616768930..fc3c1ef42d 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -34,9 +34,9 @@ public void add(int index, Object o) { linkLast(o); } else { Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 - Node currentNode = getNodeByIndex(index); // 取到当前下标节点 - Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 - + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 head = newNode; } else { @@ -59,14 +59,46 @@ public Object get(int index) { return getNodeByIndex(index).data; } + /** + * 根据下标移除链表元素 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ public Object remove(int index) { - return null; + checkIndexForGet(index); + + Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 + Node currentNode = null; + if (prevNode == null) { + currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 + head = currentNode.next; + } else { + currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 + prevNode.next = currentNode.next; + } + Node nextNode = currentNode.next; + + if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 + last = prevNode; + } else { + currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 + } + Object data = currentNode.data; + currentNode.data = null; // 清空当前节点的值,等待垃圾回收 + + size--; + + return data; } public int size() { return size; } + public void addFirst(Object o) { } @@ -140,7 +172,7 @@ private void checkIndexForAdd(int index) { throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); } } - + /** * 检查下标是否合法 * From 16dfd34a181b2472e92173fd3bdc4489de53355e Mon Sep 17 00:00:00 2001 From: guodongym Date: Fri, 24 Feb 2017 20:09:56 +0800 Subject: [PATCH 08/18] 11 11 --- .../learning_1/src/com/guodong/datastructure/LinkedList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index fc3c1ef42d..2424a304dc 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -100,7 +100,6 @@ public int size() { public void addFirst(Object o) { - } public void addLast(Object o) { From dbd3f5c95d3fbf1697dbe1474b8b9913557708f5 Mon Sep 17 00:00:00 2001 From: zj <2258659044@qq.com> Date: Fri, 24 Feb 2017 20:38:36 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E4=BC=98=E5=8C=96ArrayList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/ArrayList.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index d08cf467eb..5de89da950 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -13,9 +13,7 @@ public class ArrayList implements List { private Object[] newElementData; public void add(Object o){ - if(size>=elementData.length){//扩容 - grow(); - } + grow(); elementData[size] = o; size ++; } @@ -24,9 +22,7 @@ public void add(int index, Object o){ if(index>size){ throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); } - if(size>=elementData.length||index>=elementData.length-1){//长度不够需要扩容 - grow(); - } + grow(); if(indexsize){ - size = index+1; - }else{ - size ++; - } + size ++; } public Object get(int index){ @@ -66,9 +58,11 @@ public int size(){ */ private void grow(){ - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; + if(size>=elementData.length){//长度不够需要扩容 + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } } From 2b8d0e2aefd1ede614d790e4d3955d25ff1141ee Mon Sep 17 00:00:00 2001 From: vegetableDogBai Date: Sat, 25 Feb 2017 15:26:25 +0800 Subject: [PATCH 10/18] structure --- group12/563253496/datastructure/.classpath | 6 + group12/563253496/datastructure/.gitignore | 1 + group12/563253496/datastructure/.project | 17 ++ .../src/Collection/ArrayList.java | 173 +++++++++++++ .../src/Collection/LinkedList.java | 227 ++++++++++++++++++ .../datastructure/src/Collection/Queue.java | 44 ++++ .../datastructure/src/Collection/Stack.java | 55 +++++ .../src/Collection/TestArrayList.java | 33 +++ .../src/Collection/TestStack.java | 15 ++ .../src/com/coding/basic/ArrayList.java | 32 +++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 46 ++++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 19 ++ .../src/com/coding/basic/Stack.java | 22 ++ 16 files changed, 738 insertions(+) create mode 100644 group12/563253496/datastructure/.classpath create mode 100644 group12/563253496/datastructure/.gitignore create mode 100644 group12/563253496/datastructure/.project create mode 100644 group12/563253496/datastructure/src/Collection/ArrayList.java create mode 100644 group12/563253496/datastructure/src/Collection/LinkedList.java create mode 100644 group12/563253496/datastructure/src/Collection/Queue.java create mode 100644 group12/563253496/datastructure/src/Collection/Stack.java create mode 100644 group12/563253496/datastructure/src/Collection/TestArrayList.java create mode 100644 group12/563253496/datastructure/src/Collection/TestStack.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/ArrayList.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/Iterator.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/LinkedList.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/List.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/Queue.java create mode 100644 group12/563253496/datastructure/src/com/coding/basic/Stack.java diff --git a/group12/563253496/datastructure/.classpath b/group12/563253496/datastructure/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group12/563253496/datastructure/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group12/563253496/datastructure/.gitignore b/group12/563253496/datastructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/563253496/datastructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/563253496/datastructure/.project b/group12/563253496/datastructure/.project new file mode 100644 index 0000000000..4ae7fd9359 --- /dev/null +++ b/group12/563253496/datastructure/.project @@ -0,0 +1,17 @@ + + + datastructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group12/563253496/datastructure/src/Collection/ArrayList.java b/group12/563253496/datastructure/src/Collection/ArrayList.java new file mode 100644 index 0000000000..597a9b1a3a --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/ArrayList.java @@ -0,0 +1,173 @@ +package Collection; + +import com.coding.basic.List; + +import java.util.NoSuchElementException; + +import com.coding.basic.Iterator; + +public class ArrayList implements List { + + private int size; + private Object[] elementData; + + public ArrayList() { + size = 0; + elementData = new Object[10]; + } + + public ArrayList(Object o) { + size = 0; + elementData = new Object[10]; + this.add(o); + } + + public ArrayList(int initialCapacity) { + size = 0; + elementData = new Object[initialCapacity]; + } + + @Override + public void add(Object o) { + if (size <= elementData.length - 1) { + elementData[size] = o; + size++; + } else { + this.extendCapacity(); + elementData[size] = o; + size++; + + } + } + + @Override + public void add(int index, Object o) { + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + if (index > elementData.length - 1) { + while (index > elementData.length - 1) { + this.extendCapacity(); + } + elementData[index] = o; + size = index + 1; + return; + } + + if (index >= size) { + size = index + 1; + elementData[index] = o; + return; + } + if (index >= 0 && index < size) { + this.moveRearward(index); + elementData[index] = o; + size++; + return; + } + } + + @Override + public Object get(int index) { + checkCapacity(index); + if (index < size) { + return elementData[index]; + } + return null; + } + + @Override + public Object remove(int index) { + checkCapacity(index); + + if (index == size - 1) { + size--; + return elementData[size - 1]; + } + if (index < size - 1) { + Object tmp = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return tmp; + } + return null; + } + + private void checkCapacity(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void extendCapacity() { + Object[] elements = new Object[elementData.length + 10]; + for (int i = 0; i < size; i++) { + elements[i] = elementData[i]; + } + elementData = elements; + + } + + @Override + public int size() { + return size; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]); + sb.append(","); + } + sb.deleteCharAt(sb.length() - 1); + sb.append("]"); + return sb.toString(); + } + + private void moveRearward(int index) { + size++; + + if (size >= elementData.length - 1) + this.extendCapacity(); + + for (int i = size - 1; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int pos; + + public ArrayListIterator() { + + pos = 0; + } + + @Override + public boolean hasNext() { + if (pos < size) { + return true; + } + return false; + } + + @Override + public Object next() { + if (hasNext()) { + return elementData[pos++]; + } else + throw new NoSuchElementException(); + + } + + } + +} diff --git a/group12/563253496/datastructure/src/Collection/LinkedList.java b/group12/563253496/datastructure/src/Collection/LinkedList.java new file mode 100644 index 0000000000..94aefb188e --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/LinkedList.java @@ -0,0 +1,227 @@ +package Collection; + +import com.coding.basic.List; +import com.coding.basic.Iterator; + +public class LinkedList implements List { + + public Node head; + public int size; + + public LinkedList() { + head = new Node(); + size = 0; + } + + public LinkedList(Object o) { + head = new Node(o); + size = 1; + } + + public void add(Object o) { + if (size == 0) { + addfirst(o); + return; + } + addlast(o); + } + + public void add(int index, Object o) { + this.checkCapacity(index); + if (index == 0) { + addfirst(o); + return; + } + if (index == size) { + addlast(o); + return; + } + addmid(index, o); + } + + public void checkCapacity(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + public void addfirst(Object o) { + Node tmp = new Node(head); + head.data = o; + head.next = tmp; + size++; + } + + public void addlast(Object o) { + Node tmp = new Node(head); + //Node last = new Node(o); + //last.data=o; + for (int i = 0; i < size-1; i++) { + tmp = tmp.next; + } + + tmp.next = new Node(o); + size++; + + } + + public void addmid(int index, Object o) { + Node tmp = new Node(head); + Node add = new Node(o); + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + add.next = tmp.next; + tmp.next = add; + size++; + } + + public Object get(int index) { + checkCapacity(index); + Node tmp = new Node(head); + if (index == 0) { + return head; + } + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + return tmp.next; + } + + public Object remove(int index) { + checkCapacity(index); + Node tmp = new Node(head); + if (index == 0) { + return removeFirst(); + } + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + Node result = new Node(tmp.next); + tmp.next = result.next; + return result; + } + + public int size() { + return this.size; + } + + public Object removeFirst() { + Node tmp = new Node(head); + head = head.next; + return tmp; + } + + public Object removeLast() { + if (size == 0) { + return null; + } + if (size == 1) { + Node tmp = new Node(head); + head = null; + return tmp; + } + Node tmp = new Node(head); + for (int i = 0; i < size - 2; i++) { + tmp = tmp.next; + } + Node result = new Node(tmp.next); + tmp.next = result.next; + return result; + } + + public String toString(){ + StringBuilder sb= new StringBuilder(); + sb.append("["); + Node tmp=new Node(head); + for(int i=0;ielementData.size()){ + throw new IndexOutOfBoundsException(); + } + } + public Object pop(){ + checkCapacity(); + Object o = elementData.remove(size-1); + size--; + return o; + } + + public Object peek() { + checkCapacity(); + Object o = elementData.get(size-1); + return o; + } + + public boolean isEmpty() { + if(size!=0){ + return true; + } + return false; + } + + public String toString(){ + return super.toString(); + } + + public int size() { + return size; + } +} diff --git a/group12/563253496/datastructure/src/Collection/TestArrayList.java b/group12/563253496/datastructure/src/Collection/TestArrayList.java new file mode 100644 index 0000000000..eef55fae6b --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/TestArrayList.java @@ -0,0 +1,33 @@ +package Collection; + +import java.lang.reflect.Array; + +/** + * Created by bdl19 on 2017/2/23. + */ +public class TestArrayList { + /*public static void main(String[] args){ + /* + ArrayList al= new ArrayList("test1"); + System.out.println(al); + al.add("test2"); + System.out.println(al); + al.add(1,2); + System.out.println(al); + System.out.println(al.get(2)); + System.out.println(al.get(1)); + System.out.println(al.get(0)); + // System.out.println(al.get(3)); + System.out.println(al.size()); + System.out.println(al.remove(2)); + System.out.println(al); + + ArrayList al =new ArrayList(); + al.add(3,2); + al.add(0,0); + System.out.println(al.size()); + System.out.println(al); + + } + **/ +} diff --git a/group12/563253496/datastructure/src/Collection/TestStack.java b/group12/563253496/datastructure/src/Collection/TestStack.java new file mode 100644 index 0000000000..4702e49079 --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/TestStack.java @@ -0,0 +1,15 @@ +package Collection; + +/** + * Created by bdl19 on 2017/2/25. + */ +public class TestStack { + public static void main(String[] args) { + Stack s=new Stack(); + s.push("a"); + s.push("b"); + System.out.println(s.pop()); + System.out.println(s.pop()); + + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java b/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/BinaryTreeNode.java b/group12/563253496/datastructure/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/Iterator.java b/group12/563253496/datastructure/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java b/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/List.java b/group12/563253496/datastructure/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/Queue.java b/group12/563253496/datastructure/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/Stack.java b/group12/563253496/datastructure/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From 6e39ef65d41ba58d08b422fa31782b417d245fee Mon Sep 17 00:00:00 2001 From: maishihang <446031103@qq.com> Date: Sat, 25 Feb 2017 17:39:20 +0800 Subject: [PATCH 11/18] complete 20170225 complete --- .../src/com/coding/basic/ArrayList.java | 121 ++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 44 ++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 225 ++++++++++++++++++ .../446031103/src/com/coding/basic/List.java | 9 + .../446031103/src/com/coding/basic/Queue.java | 68 ++++++ .../446031103/src/com/coding/basic/Stack.java | 81 +++++++ 7 files changed, 555 insertions(+) create mode 100644 group12/446031103/src/com/coding/basic/ArrayList.java create mode 100644 group12/446031103/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group12/446031103/src/com/coding/basic/Iterator.java create mode 100644 group12/446031103/src/com/coding/basic/LinkedList.java create mode 100644 group12/446031103/src/com/coding/basic/List.java create mode 100644 group12/446031103/src/com/coding/basic/Queue.java create mode 100644 group12/446031103/src/com/coding/basic/Stack.java diff --git a/group12/446031103/src/com/coding/basic/ArrayList.java b/group12/446031103/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c627a7d2fa --- /dev/null +++ b/group12/446031103/src/com/coding/basic/ArrayList.java @@ -0,0 +1,121 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * + * arrayList集合-数组 + * + * @ClassName ArrayList + * @author msh + * @date 2017年2月21日 下午3:49:24 + */ +public class ArrayList implements List { + // 记录ArrayList集合大小 + private int size = 0; + // 初始化存储数组 + private Object[] elementData = new Object[100]; + /** + * + * 向最后插入元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + // 数组不够时增长 + growOrNot(size + 1); + elementData[size] = o; + ++size; + } + /** + * + * 向指定位置插入元素 + * + * @Method add 添加 + * @param index 下标 + * @param o 元素 + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o){ + validate(index); + growOrNot(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + ++size; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + return elementData[index]; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return 删除的元素 + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index){ + Object oldValue = elementData[index]; + validate(index); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[size] = null; + --size; + return oldValue; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.coding.basic.List#size() + */ + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + /** + * + * 判断是否需要增长数组 + * + * @MethodName growOrNot + * @author msh + * @date 2017年2月21日 下午3:53:29 + * @param minCapacity + */ + private void growOrNot(int minCapacity) { + // 当增加长度大于数组长度时,增长 + if (minCapacity > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} diff --git a/group12/446031103/src/com/coding/basic/BinaryTreeNode.java b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..ed26d946bb --- /dev/null +++ b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,44 @@ +package com.coding.basic; + + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode( Object data,BinaryTreeNode left,BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = 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(BinaryTreeNode tree,Object o){ + if(null== tree){ + tree = new BinaryTreeNode(o, null, null); + } + if(Integer.valueOf(o.toString())>Integer.valueOf(tree.data.toString())){ + tree.right =insert(tree.right,o); + }else{ + tree.left = insert(tree.left,o); + } + return tree; + } + +} diff --git a/group12/446031103/src/com/coding/basic/Iterator.java b/group12/446031103/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/LinkedList.java b/group12/446031103/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de508694a0 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/LinkedList.java @@ -0,0 +1,225 @@ +package com.coding.basic; + +/** + * + * LinkedList集合-链 + * + * @ClassName LinkedList + * @author msh + * @date 2017年2月21日 下午4:08:01 + */ +public class LinkedList implements List { + //链头 + private Node head; + //集合大小 + private int size=0; + /** + * + * 向链中添加元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + Node newNode = new Node(o, null); + if (null == head) { + head = newNode; + } else { + Node lastNode = null; + for (int i = 0; i < size; i++) { + lastNode = (Node) get(i); + } + lastNode.next = newNode; + } + size++; + } + /** + * + * 向链中添加元素 + * + * @Method add 增加 + * @param index 下标 + * @param o 元素 + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + validate(index); + Node newNode = null; + Node perNode = null; + Node nextNode = null; + // 当为最后插入时 + if (index == size - 1) { + newNode = new Node(o, null); + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + perNode.next = newNode; + } else if (0 == index) { + nextNode = head.next; + newNode = new Node(o, nextNode); + head = newNode; + } else { + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + nextNode = perNode.next.next; + newNode = new Node(o, nextNode); + perNode.next = newNode; + } + size++; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + Node tempNode = head; + for (int i = 0; i <= index; i++) { + tempNode = tempNode.next; + } + return tempNode; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index){ + Node removeNode = (Node) get(index); + validate(index); + if (index == size - 1) { + Node tempNode = head; + for (int i = 0; i < index; i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + } else if (index == 0) { + Node tempNode = head.next; + head.next = null; + head = tempNode; + } else { + } + size--; + return removeNode; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.coding.basic.List#size() + */ + public int size(){ + return size; + } + /** + * + * 想链头中插入元素 + * + * @MethodName addFirst + * @author msh + * @date 2017年2月21日 下午4:10:56 + * @param o + */ + public void addFirst(Object o){ + Node newNode = new Node(o, head); + head = newNode; + } + /** + * + * 向链后加入元素 + * + * @MethodName addLast + * @author msh + * @date 2017年2月21日 下午4:11:43 + * @param o + */ + public void addLast(Object o){ + add(o); + } + /** + * + * 删除链头 + * + * @MethodName removeFirst + * @author msh + * @date 2017年2月21日 下午4:12:14 + * @return + */ + public Object removeFirst(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node orgHead = head; + Node tempNode = head.next; + head.next = null; + head = tempNode; + return orgHead; + } + /** + * + * 删除链尾 + * + * @MethodName removeLast + * @author zhaogd + * @date 2017年2月21日 下午4:12:44 + * @return + */ + public Object removeLast(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node lastNode = (Node) get(size); + Node tempNode = head; + for (int i = 0; i < (size - 1); i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + return lastNode; + } + public Iterator iterator(){ + return null; + } + + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + /** + * + * 链中元素 + * + * @ClassName Node + * @author zhaogd + * @date 2017年2月21日 下午4:13:10 + */ + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group12/446031103/src/com/coding/basic/List.java b/group12/446031103/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/Queue.java b/group12/446031103/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3844d9dd24 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +/** + * + * 队列-先进先出 + * + * @ClassName Queue + * @author msh + * @date 2017年2月21日 下午9:29:03 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + /** + * + * 入队列 + * + * @MethodName enQueue + * @author msh + * @date 2017年2月21日 下午9:45:15 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + /** + * + * 离开队列 + * + * @MethodName deQueue + * @author msh + * @date 2017年2月21日 下午9:56:06 + * @return + */ + public Object deQueue(){ + if(isEmpty()) + throw new IndexOutOfBoundsException("size:"+size()); + Object o=elementData.get(0); + elementData.removeFirst(); + return o; + } + /** + * + * 是否为空 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:57:14 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==elementData.size()) + temp= true; + return temp; + } + /** + * + * 队列中元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:57:28 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group12/446031103/src/com/coding/basic/Stack.java b/group12/446031103/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d1c58b671 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Stack.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +/** + * + * 栈-先进后出 + * + * @ClassName Stack + * @author msh + * @date 2017年2月21日 下午9:05:39 + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + /** + * + * 向栈中加入元素 + * + * @MethodName push + * @author msh + * @date 2017年2月21日 下午9:12:03 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + /** + * + * 从栈中取出元素 + * + * @MethodName pop + * @author msh + * @date 2017年2月21日 下午9:12:51 + * @return + */ + public Object pop(){ + Object o= peek(); + elementData.remove(size()-1); + return o; + } + /** + * + * 取出栈顶元素 + * + * @MethodName peek + * @author msh + * @date 2017年2月21日 下午9:13:08 + * @return + */ + public Object peek(){ + Object o=elementData.get(size()-1); + return o; + } + /** + * + * 判断栈中是否有元素 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:14:26 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==size()) + temp = true; + return temp; + } + /** + * + * 栈中有多少元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:16:42 + * @return + */ + public int size(){ + return elementData.size(); + } + +} From 0a04395219417638fe3835f01dd12775b37fbbfd Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Sun, 26 Feb 2017 10:48:50 +0800 Subject: [PATCH 12/18] add 5 soc files as homework --- group12/247565311/week1/ArrayList.java | 249 +++++++++++++++++++++++++ group12/247565311/week1/Deque.java | 34 ++++ group12/247565311/week1/Stack.java | 24 +++ 3 files changed, 307 insertions(+) create mode 100644 group12/247565311/week1/ArrayList.java create mode 100644 group12/247565311/week1/Deque.java create mode 100644 group12/247565311/week1/Stack.java diff --git a/group12/247565311/week1/ArrayList.java b/group12/247565311/week1/ArrayList.java new file mode 100644 index 0000000000..3e4c12d7a7 --- /dev/null +++ b/group12/247565311/week1/ArrayList.java @@ -0,0 +1,249 @@ +package week1; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + + +public class ArrayList implements List { + private int size=0,offset=10; + private Object[] data = null; + public ArrayList(){ + data = new Object[offset]; + } + public ArrayList(int arg0){ + if(arg0<0) arg0=0; + size = arg0; + data = new Object[size]; + } + @Override + public boolean add(E arg0) { + if(arg0 == null) return false; + size += 1; + int leng = data.length; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;isize+1 || 0>arg0) return ; + size += 1; + int leng = data.length; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg0) { + if (arg0 == null) return false; + int leng = data.length,newobjnum = arg0.size(),lastsize=size; + size += newobjnum; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg1) { + int newobjnum = arg1.size(),lastsize = size; + if(arg1 == null || arg0>size+1 || 0>arg0 || newobjnum==0) return false; + size += newobjnum; + int leng = data.length; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg0) { + for(Object o:arg0){ + if(!this.contains(o)) return false; + } + return true; + } + + @Override + public E get(int arg0) { + if(arg0 >-1 && arg0-1;i--){ + if(this.data[i].equals(arg0)) return i; + } + return -1; + } + + @Override + public Iterator iterator() { + + return null; + } + + @Override + public ListIterator listIterator() { + + return null; + } + + @Override + public ListIterator listIterator(int arg0) { + + return null; + } + + @Override + public boolean remove(Object arg0) { + for(int i=0;ithis.size-1) return null; + E res = (E)data[arg0]; + for(int i=arg0;i arg0) { + int toberemovednums = arg0.size(); + if(!this.containsAll(arg0)) return false; + int index=0; + for(int i=0;i arg0) { + // what does this mean? + return false; + } + + @Override + public E set(int arg0, E arg1) { + if(arg0<0||arg0>this.size-1) return null; + this.data[arg0] = arg1; + return arg1; + } + + @Override + public int size() { + return this.size; + } + + @Override + public List subList(int arg0, int arg1) { + if(arg0>=arg1 || arg0<0 || arg1>this.size-1) return null; + List res = new ArrayList(); + for(int i=arg0;i T[] toArray(T[] arg0) { + T[] res = (T[])(new Object[this.size]); + for(int i=0;i { + private LinkedList data = new LinkedList(); + private int size = 0; + + + public Deque(){ + + } + public Deque(int arg0){ + data = new LinkedList(arg0); + } + public boolean push(E arg0){ + data.add(arg0); + size += 1; + return true; + } + public E pop(){ + size -= 1; + E res = data.get(size); + data.remove(size); + return res; + } + public E peek(){ + return data.get(size-1); + } + public int size(){ + return this.size; + } + public boolean isEmpty(){ + return this.size==0; + } + +} diff --git a/group12/247565311/week1/Stack.java b/group12/247565311/week1/Stack.java new file mode 100644 index 0000000000..2d4692f05b --- /dev/null +++ b/group12/247565311/week1/Stack.java @@ -0,0 +1,24 @@ +package week1; + +import java.util.List; + +public class Stack { + private List data = new ArrayList(); + private int size = 0; + + public Stack(){ + + } + + public Stack(int arg0){ + if(arg0 < 0) arg0 = 0; + size = arg0; + data = new ArrayList(size); + + } + public boolean push(E arg0){ + size += 1; + data.add(arg0); + } + public E pop() +} From d4aad3d9f3b4c45dc19396a0800c3e27ee190116 Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Sun, 26 Feb 2017 10:49:42 +0800 Subject: [PATCH 13/18] add to list files --- group12/247565311/week1/Link.java | 5 + group12/247565311/week1/LinkedList.java | 204 ++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 group12/247565311/week1/Link.java create mode 100644 group12/247565311/week1/LinkedList.java diff --git a/group12/247565311/week1/Link.java b/group12/247565311/week1/Link.java new file mode 100644 index 0000000000..e2910e53d3 --- /dev/null +++ b/group12/247565311/week1/Link.java @@ -0,0 +1,5 @@ +package week1; + +public class Link { + +} diff --git a/group12/247565311/week1/LinkedList.java b/group12/247565311/week1/LinkedList.java new file mode 100644 index 0000000000..debd53d8d2 --- /dev/null +++ b/group12/247565311/week1/LinkedList.java @@ -0,0 +1,204 @@ +package week1; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class LinkedList implements List { + private Node head = null; + private Node tail = null; + private int size = 0; + + public LinkedList(){ + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + public LinkedList(int arg0){ + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + @Override + public boolean add(E arg0) { + Node n = new Node(arg0); + n.next = tail; + n.ahead = tail.ahead; + tail.ahead.next = n; + tail.ahead = n; + size += 1; + return true; + } + + @Override + public void add(int arg0, E arg1) { + if(arg0<0) arg0=0; + Node n=new Node(arg1),p=head; + for(int i=0;i arg0) { + for(E o:arg0){ + this.add(o); + } + return true; + } + + @Override + public boolean addAll(int arg0, Collection arg1) { + for(E e:arg1){ + this.add(arg0,e); + arg0+=1; + } + return true; + } + + @Override + public void clear() { + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + + @Override + public boolean contains(Object arg0) { + boolean flag = arg0==null; + Node n = head; + for(int i=0;i arg0) { + for(Object e:arg0){ + if(!this.contains(e)) return false; + } + return true; + } + + @Override + public E get(int arg0) { + E res = null; + if(arg0>-1 && arg0 < size){ + + } + return res; + } + + @Override + public int indexOf(Object arg0) { + + return 0; + } + + @Override + public boolean isEmpty() { + + return false; + } + + @Override + public Iterator iterator() { + + return null; + } + + @Override + public int lastIndexOf(Object arg0) { + + return 0; + } + + @Override + public ListIterator listIterator() { + + return null; + } + + @Override + public ListIterator listIterator(int arg0) { + + return null; + } + + @Override + public boolean remove(Object arg0) { + + return false; + } + + @Override + public E remove(int arg0) { + + return null; + } + + @Override + public boolean removeAll(Collection arg0) { + + return false; + } + + @Override + public boolean retainAll(Collection arg0) { + + return false; + } + + @Override + public E set(int arg0, E arg1) { + + return null; + } + + @Override + public int size() { + + return 0; + } + + @Override + public List subList(int arg0, int arg1) { + + return null; + } + + @Override + public Object[] toArray() { + + return null; + } + + @Override + public T[] toArray(T[] arg0) { + + return null; + } + private static class Node{ + Object val = null; + Node next = null,ahead=null; + public Node(Object arg0){val = arg0;} + } +} From 3ea180fc331621bbeff3684cc9ee023009682e6c Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Feb 2017 13:04:07 +0800 Subject: [PATCH 14/18] commit --- group12/382266293/.classpath | 7 + group12/382266293/.gitignore | 1 + group12/382266293/.project | 17 ++ .../src/Collection/AbstractList.java | 43 ++++ .../src/Collection/Concrete/ArrayList.java | 148 ++++++++++++ .../Collection/Concrete/BinaryTreeNode.java | 126 +++++++++++ .../src/Collection/Concrete/LinkedList.java | 196 ++++++++++++++++ .../src/Collection/Concrete/Queue.java | 84 +++++++ .../src/Collection/Concrete/Stack.java | 106 +++++++++ .../382266293/src/Collection/Iterator.java | 7 + group12/382266293/src/Collection/List.java | 16 ++ .../src/TestCollection/AllTests.java | 11 + .../src/TestCollection/ArrayListTest.java | 173 ++++++++++++++ .../TestCollection/BinaryTreeNodeTest.java | 54 +++++ .../src/TestCollection/LinkedListTest.java | 212 ++++++++++++++++++ .../src/TestCollection/QueueTest.java | 98 ++++++++ .../src/TestCollection/StackTest.java | 129 +++++++++++ group12/382266293/src/test.java | 15 ++ group12/382266293/src/util/Print.java | 14 ++ group12/382266293/src/util/TestUtil.java | 78 +++++++ 20 files changed, 1535 insertions(+) create mode 100644 group12/382266293/.classpath create mode 100644 group12/382266293/.gitignore create mode 100644 group12/382266293/.project create mode 100644 group12/382266293/src/Collection/AbstractList.java create mode 100644 group12/382266293/src/Collection/Concrete/ArrayList.java create mode 100644 group12/382266293/src/Collection/Concrete/BinaryTreeNode.java create mode 100644 group12/382266293/src/Collection/Concrete/LinkedList.java create mode 100644 group12/382266293/src/Collection/Concrete/Queue.java create mode 100644 group12/382266293/src/Collection/Concrete/Stack.java create mode 100644 group12/382266293/src/Collection/Iterator.java create mode 100644 group12/382266293/src/Collection/List.java create mode 100644 group12/382266293/src/TestCollection/AllTests.java create mode 100644 group12/382266293/src/TestCollection/ArrayListTest.java create mode 100644 group12/382266293/src/TestCollection/BinaryTreeNodeTest.java create mode 100644 group12/382266293/src/TestCollection/LinkedListTest.java create mode 100644 group12/382266293/src/TestCollection/QueueTest.java create mode 100644 group12/382266293/src/TestCollection/StackTest.java create mode 100644 group12/382266293/src/test.java create mode 100644 group12/382266293/src/util/Print.java create mode 100644 group12/382266293/src/util/TestUtil.java diff --git a/group12/382266293/.classpath b/group12/382266293/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group12/382266293/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group12/382266293/.gitignore b/group12/382266293/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/382266293/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/382266293/.project b/group12/382266293/.project new file mode 100644 index 0000000000..1282023911 --- /dev/null +++ b/group12/382266293/.project @@ -0,0 +1,17 @@ + + + 382266293Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group12/382266293/src/Collection/AbstractList.java b/group12/382266293/src/Collection/AbstractList.java new file mode 100644 index 0000000000..a81e76b587 --- /dev/null +++ b/group12/382266293/src/Collection/AbstractList.java @@ -0,0 +1,43 @@ +package Collection; + +public abstract class AbstractList implements List { + + protected static final String PREFIX = "["; + protected static final String SUFFIX = "]"; + protected static final String SEPERATOR = ", "; + protected static final int MAX_SIZE = Integer.MAX_VALUE/3; + + protected void checkIndex(int i) { + if( i < 0 || i > Math.min(size(), MAX_SIZE)) + throw new IndexOutOfBoundsException("Size :" + size()+", Index: " + i); + } + + public boolean isEmpty() { + return size() == 0; + } + + + public int indexOf(E e) { + for (int i = 0; i < size()-1; i++) { + if (get(i).equals(e)) + return i; + } + return -1; + } + + protected abstract Iterator iterator(); + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(PREFIX); + for (int i = 0; i < size(); i++) { + sb.append(get(i)); + if (i < size()-1) + sb.append(SEPERATOR); + } + sb.append(SUFFIX); + return sb.toString(); + } + +} diff --git a/group12/382266293/src/Collection/Concrete/ArrayList.java b/group12/382266293/src/Collection/Concrete/ArrayList.java new file mode 100644 index 0000000000..3db5ab47e6 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/ArrayList.java @@ -0,0 +1,148 @@ +package Collection.Concrete; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class ArrayList extends AbstractList { + + private E[] elements; + private int size; + private static final int INITIAL_SIZE = 16; + + + public ArrayList() { + elements = (E[]) new Object[INITIAL_SIZE]; + size = 0; + } + + @Override + public void add(E e) { + checkCapacity(); + elements[size++] = e; + } + + private void checkCapacity() { + if (size >= MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max size"); + + if (elements.length - size < INITIAL_SIZE) + grow(); + } + + synchronized private void grow() { + + int newCapacity = size * 2; + newCapacity = (newCapacity < 0 || newCapacity - MAX_SIZE > 0) ? MAX_SIZE : newCapacity; + E[] target = (E[]) new Object[newCapacity]; + System.arraycopy(elements, 0, target, 0, size); + elements = target; + + } + + public void add(int index, E e) { + checkCapacity(); + if (index == size) { + add(e); + return; + } + checkIndex(index); + synchronized (this) { + System.arraycopy(elements, index, elements, index+1, size-index+1); + elements[index] = e; + size++; + } + } + + @Override + public E get(int index) { + checkIndex(index); + return elements[index]; + } + + + public E getLast() { + return get(size-1); + } + + public void addLast(E e) { + add(e); + } + + public E removeLast() { + return elements[--size]; + } + + public E remove(int index) { + checkIndex(index); + E result = elements[index]; + synchronized (this) { + System.arraycopy(elements, index+1, elements, index, size-index-1); + elements[--size] = null; + } + return result; + } + + @Override + public int size() { + return size; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(elements); + result = prime * result + size; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ArrayList other = (ArrayList) obj; + if (!Arrays.equals(elements, other.elements)) + return false; + if (size != other.size) + return false; + return true; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public ArrayListIterator(ArrayList arrayList) { + myArrayList = arrayList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) elements[pos++]; + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java new file mode 100644 index 0000000000..34a9d4253e --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java @@ -0,0 +1,126 @@ +package Collection.Concrete; + +public class BinaryTreeNode> { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + private ArrayList myList; + + + + public BinaryTreeNode() { + this.data = null; + this.left = null; + this.right = null; + } + + public BinaryTreeNode(E data) { + this.data = data; + this.left = null; + this.right = null; + } + + public boolean isEmpty() { + return data == null; + } + + public int size() { + return size; + } + + public BinaryTreeNode insert(E o) { + BinaryTreeNode res; + if (isEmpty()) { + data = o; + size++; + return this; + } else { + BinaryTreeNode p = this; + res = new BinaryTreeNode(o); + while (true) { + if (res.getData().compareTo(p.getData()) < 0) { + if (p.left == null) { + p.setLeft(res); + break; + } + p = p.left; + } else if (res.getData().compareTo(p.getData()) > 0) { + if (p.right == null) { + p.setRight(res); + break; + } + p = p.right; + } else { + return null; + } + } + size++; + } + return res; + } + + + + public ArrayList preOrderTraversal(BinaryTreeNode node) { + + if (node != null) { + preOrderTraversal(node.left); + myList.add(node.data); + preOrderTraversal(node.right); + } + return myList; + } + + @Override + public String toString() { + myList = new ArrayList(); + return preOrderTraversal(this).toString(); + } + + public E getData() { + return data; + } + public void setData(E 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; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BinaryTreeNode other = (BinaryTreeNode) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + +} \ No newline at end of file diff --git a/group12/382266293/src/Collection/Concrete/LinkedList.java b/group12/382266293/src/Collection/Concrete/LinkedList.java new file mode 100644 index 0000000000..66dbd9d8d0 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/LinkedList.java @@ -0,0 +1,196 @@ +package Collection.Concrete; + +import java.util.NoSuchElementException; +import Collection.AbstractList; +import Collection.Iterator; + + +public class LinkedList extends AbstractList { + + private Node head; + private int size; + + public LinkedList() { + this.head = new Node(null); + this.size = 0; + } + + @Override + public void add(E e) { + addLast(e); + } + + + @Override + public E get(int index) { + checkIndex(index); + return getNode(index).data; + } + + public E getFirst() { + return get(0); + } + + public E getLast() { + return get(size-1); + } + + + public void add(int index, E e) { + if (index == size) { + addLast(e); + return; + } + + if (index == 0) { + addFirst(e); + return; + } + + checkIndex(index); + Node pNode = new Node(e); + Node p = getNode(index); + synchronized (this) { + getNode(index-1).next = pNode; + pNode.next = p; + size++; + } + } + + public void addFirst(E e){ + checkCapacity(); + Node pNode = new Node(e); + Node oldHead = head; + head = pNode; + pNode.next = oldHead; + size++; + return; + } + + public void addLast(E e){ + if (size == 0) { + addFirst(e); + return; + } + + checkCapacity(); + Node res = new Node(e); + setLastNode(res); + size++; + return; + } + + public E removeFirst(){ + return remove(0); + } + public E removeLast(){ + return remove(size-1); + } + + public E remove(int index) { + checkIndex(index); + Node pNode = getNode(index); + if (index == 0) { + head = head.next; + } else if (index == size-1 ) { + getNode(index-1).next = null; + } else { + getNode(index-1).next = getNode(index+1); + } + size--; + return pNode.data; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private void checkCapacity() { + if (size > MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max capacity: "+ MAX_SIZE); + } + + private Node getNode(int index) { + if (size == 0) + return head; + + Node pNode = head; + for ( int i = 0; i < index ; i++) { + pNode = pNode.next; + } + return pNode; + } + + private void setLastNode(Node res) { + getNode(size-1).next = res; + } + + private static class Node { + E data; + Node next; + + public Node(E data) { + this.data = data; + this.next = null; + } + + @Override + public String toString() { + return data.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Node other = (Node) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + } + + @SuppressWarnings("hiding") + private class LinkedListIterator implements Iterator { + + private LinkedList myLinkedList; + private int pos; + + public LinkedListIterator(LinkedList linkedList) { + myLinkedList = linkedList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos++); + throw new NoSuchElementException(); + } + } +} diff --git a/group12/382266293/src/Collection/Concrete/Queue.java b/group12/382266293/src/Collection/Concrete/Queue.java new file mode 100644 index 0000000000..300fb633af --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Queue.java @@ -0,0 +1,84 @@ +package Collection.Concrete; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Queue extends AbstractList { + + private LinkedList myList; + + public Queue() { + this.myList = new LinkedList(); + } + + public void enQueue(E e){ + myList.addLast(e); + } + + public E deQueue(){ + if (0 == size()) + return null; + return myList.removeFirst(); + } + + @Override + public void add(E e) { + enQueue(e); + } + + @Override + public E get(int index) { + if (0 == size()) + return null; + return myList.get(index); + } + + public E element() { + if (0 == size()) + return null; + return myList.getFirst(); + } + + + @Override + public int size() { + return myList.size(); + } + + @Override + protected Iterator iterator() { + return myList.iterator(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Queue other = (Queue) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + + + + + +} diff --git a/group12/382266293/src/Collection/Concrete/Stack.java b/group12/382266293/src/Collection/Concrete/Stack.java new file mode 100644 index 0000000000..17ec4364fa --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Stack.java @@ -0,0 +1,106 @@ +package Collection.Concrete; + +import java.util.EmptyStackException; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Stack extends AbstractList { + + private ArrayList myList; + + public Stack() { + this.myList = new ArrayList(); + } + + public void push(E e){ + myList.addLast(e); + } + + public E pop(){ + checkEmpty(); + return myList.removeLast(); + } + + private void checkEmpty() { + if (0 == size()) + throw new EmptyStackException(); + } + + public E peek(){ + checkEmpty(); + return myList.getLast(); + } + + public int size(){ + return myList.size(); + } + + @Override + public void add(E e) { + push(e); + } + + @Override + public E get(int index) { + checkEmpty(); + return myList.get(size() - index - 1); + } + + @Override + protected Iterator iterator() { + return new StackIterator(myList); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Stack other = (Stack) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + private class StackIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public StackIterator(ArrayList myList) { + myArrayList = myList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos); + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Iterator.java b/group12/382266293/src/Collection/Iterator.java new file mode 100644 index 0000000000..d51656a3a8 --- /dev/null +++ b/group12/382266293/src/Collection/Iterator.java @@ -0,0 +1,7 @@ +package Collection; + +public interface Iterator { + + public boolean hasNext(); + public E next(); +} diff --git a/group12/382266293/src/Collection/List.java b/group12/382266293/src/Collection/List.java new file mode 100644 index 0000000000..7ddb685cac --- /dev/null +++ b/group12/382266293/src/Collection/List.java @@ -0,0 +1,16 @@ +package Collection; + +public interface List { + + public void add(E e); + + public int size(); + + public E get(int index); + + public boolean isEmpty(); + + public int indexOf(E e); + + +} diff --git a/group12/382266293/src/TestCollection/AllTests.java b/group12/382266293/src/TestCollection/AllTests.java new file mode 100644 index 0000000000..cd4eeadb58 --- /dev/null +++ b/group12/382266293/src/TestCollection/AllTests.java @@ -0,0 +1,11 @@ +package TestCollection; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayListTest.class, BinaryTreeNodeTest.class, LinkedListTest.class, QueueTest.class, StackTest.class }) +public class AllTests { + +} diff --git a/group12/382266293/src/TestCollection/ArrayListTest.java b/group12/382266293/src/TestCollection/ArrayListTest.java new file mode 100644 index 0000000000..f29580616f --- /dev/null +++ b/group12/382266293/src/TestCollection/ArrayListTest.java @@ -0,0 +1,173 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.ArrayList; +import junit.framework.TestCase; + +public class ArrayListTest extends TestCase { + + + private ArrayList myAL; + private static Random rnd = new Random(); + + @Before + public void setUp() throws Exception { + + myAL = new ArrayList(); + assertEquals(true,myAL.isEmpty()); + + } + + @After + public void tearDown() throws Exception { + myAL = null; + } + + @Test + public void testRawTypeArrayList() { + + List rawList = new ArrayList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + } + + @Test + public void testEmpty() { + + assertEquals(true,myAL.isEmpty()); + + myAL.add(5); + assertEquals(false,myAL.isEmpty()); + + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(false,myAL.isEmpty()); + + } + + @Test + public void testAddIntAutoBoxing() { + + myAL.add(5); + myAL.add(5); + myAL.add(5); + myAL.add(1,10); + int c = myAL.get(1); + assertEquals(10,c); + + assertEquals(4,myAL.size()); + myAL.add(4,15); + int a = myAL.get(0); + Integer b = myAL.get(1); + c = myAL.get(2); + int d = myAL.get(3); + int e = myAL.get(4); + assertEquals(5,a); + assertEquals(new Integer(10),b); + assertEquals(5,c); + assertEquals(5,d); + assertEquals(15,e); + } + + @Test + public void testGet() { + + int[] result = addRandomInt(myAL, getRandomNumber()); + + int actual,expected; + + for (int i = 0; i < result.length; i++) { + actual = myAL.get(i); + expected = result[i]; + assertEquals(expected, actual); + } + + } + + @Test + public void testRemove() { + + addIntWithNatureOrder(myAL, 100); + + testRemoveAndGetFromTail(myAL); + try { + myAL.remove(10); + fail("no exception thrown"); + } catch (Exception e) { + assertEquals(IndexOutOfBoundsException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + + assertEquals(0,myAL.size()); + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(num,myAL.size()); + } + + + + @Test + public void testGrow() { + + int actualSize = 12345; + + addIntWithNatureOrder(myAL, actualSize); + + assertEquals(actualSize,myAL.size()); + } + + + @Test + public void testIterator() { + + addIntWithNatureOrder(myAL,100); + + Iterator it = myAL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + int actual = it.next(); + assertEquals(i,actual); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + @Test + public void testIndexOf() { + + int num = 200; + addIntWithNatureOrder(myAL,num); + + int expected,actual; + for (int i = 0; i < num-1; i++) { + expected = i; + actual = myAL.indexOf(i); + assertEquals(expected, actual); + } + + assertEquals(-1, myAL.indexOf(-1*getRandomNumber())); + + } + +} diff --git a/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..275ef59484 --- /dev/null +++ b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java @@ -0,0 +1,54 @@ +package TestCollection; + +import static util.Print.*; +import java.util.Random; +import java.util.Set; +import java.util.TreeSet; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.BinaryTreeNode; +import junit.framework.TestCase; + +public class BinaryTreeNodeTest extends TestCase { + + private BinaryTreeNode myTree; + + @Before + public void setUp() throws Exception { + myTree = new BinaryTreeNode(); + assertEquals(0, myTree.size()); + } + + @After + public void tearDown() throws Exception { + myTree = null; + } + + @Test + public void testInsert() { + Set expected = new TreeSet(); + int size = getRandomNumber(); + int j = 0 ; + while (expected.size() != size) { + j = getRandomNumber(); + expected.add(j); + myTree.insert(j); + } + + assertEquals(size,myTree.size()); + assertEquals(expected.toString(),myTree.toString()); + } + + public void testSize() { + + for (int i = 0; i < getRandomNumber(); i++) { + myTree.insert(18); + myTree.insert(-19); + myTree.insert(1); + assertEquals(3,myTree.size()); + } + } +} \ No newline at end of file diff --git a/group12/382266293/src/TestCollection/LinkedListTest.java b/group12/382266293/src/TestCollection/LinkedListTest.java new file mode 100644 index 0000000000..8b9fad15eb --- /dev/null +++ b/group12/382266293/src/TestCollection/LinkedListTest.java @@ -0,0 +1,212 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.LinkedList; +import junit.framework.TestCase; + + +public class LinkedListTest extends TestCase { + + LinkedList myLL; + + @Before + public void setUp() throws Exception { + myLL = new LinkedList(); + assertEquals(0,myLL.size()); + } + + @After + public void tearDown() throws Exception { + myLL = null; + } + + @Test + public void testLinkedList() { + List rawList = new LinkedList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + + } + + @Test + public void testAddE() { + myLL.add("s"); + assertEquals(1,myLL.size()); + assertEquals(false,myLL.isEmpty()); + } + + @Test + public void testAddStringE() { + String a; + + addString(myLL,30); + println(myLL.get(0)); + +// for (int i = 0 ; i < 29; i++) { +// a = "" + i; +// assertEquals(myLL.get(i),a); +// } + } + + @Test + public void testAddIndex() { + String a; + for (int i = 0 ; i < 30; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "ss"; + myLL.add(3,ss); + assertEquals(myLL.get(3), ss); + assertEquals(myLL.get(2), "2"); + assertEquals(myLL.get(4), "3"); + + } + + public void testAddFirst() { + String a; + for (int i = 0 ; i < 20; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "bba"; + myLL.addFirst(ss); + assertEquals(ss,myLL.get(0)); + assertEquals(21, myLL.size()); + + ; + for (int i = 1 ; i < myLL.size(); i++) { + a = (i-1) + ""; + assertEquals(a, myLL.get(i)); + } + } + + public void testAddLast() { + String a; + for (int i = 0 ; i < 25; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "25"; + myLL.addLast(ss); + int size = myLL.size(); + assertEquals(26, size); + + for (int i = 0 ; i < size; i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + } + + @Test + public void testRemoveFirst() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeFirst(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i+1 + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = i+1 +""; + result = myLL.removeFirst(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + } + + @Test + public void testRemoveLast() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeLast(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = myLL.size()-1 +""; + result = myLL.removeLast(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + + } + + + @Test + public void testRemove() { + + String res = ""; + String a = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + for(int i = myLL.size()-1; i >= 0; i--) { + a = myLL.get(i); + res = myLL.remove(i); + assertEquals(i, myLL.size()); + assertEquals(a,res); + } + } + + @Test + public void testSize() { + assertEquals(0,myLL.size()); + } + + @Test + public void testIterator() { + for(int i = 0; i<10; i++) { + myLL.add(i+""); + } + Iterator it = myLL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + String res = it.next(); + assertEquals(i+"",res); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/QueueTest.java b/group12/382266293/src/TestCollection/QueueTest.java new file mode 100644 index 0000000000..01a9aa31f2 --- /dev/null +++ b/group12/382266293/src/TestCollection/QueueTest.java @@ -0,0 +1,98 @@ +package TestCollection; + +import static util.Print.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static util.TestUtil.*; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + +public class QueueTest extends TestCase { + + private Queue myQueue; + + @Before + public void setUp() throws Exception { + myQueue= new Queue(); + } + + @After + public void tearDown() throws Exception { + myQueue = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myQueue.isEmpty()); + myQueue.enQueue(getRandomNumber()); + assertEquals(false, myQueue.isEmpty()); + } + + @Test + public void testEnQueue() { + + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + + } + + @Test + public void testDeQueue() { + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + int size = myQueue.size(); + for (int i = 0; i < size ; i++) { + assertEquals(size-i, myQueue.size()); + int expect = i; + int actual = myQueue.deQueue(); + assertEquals(expect, actual); + } + + assertEquals(null, myQueue.deQueue()); + assertEquals(null, myQueue.element()); + assertEquals(null, myQueue.get(0)); + + } + + @Test + public void testelement() { + + int expected = 0; + int element1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myQueue.enQueue(i); + expected = i; + + element1 = myQueue.element(); + assertEquals(expected, element1); + + for (int j = 0; j < i; j++) { + repeated = myQueue.element(); + assertEquals(expected, repeated); + } + myQueue.deQueue(); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myQueue.size()); + myQueue.enQueue(i); + } + } + + @Test + public void testAdd() { + for (int i = 0; i < 10; i++) { + myQueue.add(i); + Integer actual = new Integer(myQueue.get(i)); + Integer expected = new Integer(i); + assertEquals(expected, actual); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/src/TestCollection/StackTest.java new file mode 100644 index 0000000000..3784a9b972 --- /dev/null +++ b/group12/382266293/src/TestCollection/StackTest.java @@ -0,0 +1,129 @@ +package TestCollection; + +import static util.Print.*; + +import java.util.EmptyStackException; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.Stack; +import junit.framework.TestCase; + +public class StackTest extends TestCase { + + Stack myStack; + + @Before + public void setUp() throws Exception { + myStack= new Stack(); + } + + @After + public void tearDown() throws Exception { + myStack = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myStack.isEmpty()); + myStack.push(getRandomNumber()); + assertEquals(false, myStack.isEmpty()); + } + + @Test + public void testPush() { + for (int i = 0; i < 10; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testPop() { + testPush(); + int size = myStack.size(); + for (int i = size; i > 0; i--) { + assertEquals(i, myStack.size()); + int expect = i-1; + assertEquals(i, myStack.size()); + int actual = myStack.pop(); + assertEquals(expect, actual); + } + + try { + myStack.pop(); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + } + + @Test + public void testPeek() { + + int expected = 0; + int peek1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myStack.push(i); + expected = i; + + peek1 = myStack.peek(); + assertEquals(expected, peek1); + + for (int j = 0; j < i; j++) { + repeated = myStack.peek(); + assertEquals(expected, repeated); + } + } + + } + + + public void testGet() { + + try { + myStack.get(getRandomNumber()); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testAdd() { + myStack.push(5); + myStack.push(10); + myStack.push(15); + println(myStack.get(0)); + println(myStack.get(1)); + println(myStack.get(2)); + + } + + @Test + public void testPopPushAndPeek() { + for (int i = 0; i < 10; i++) { + int expected = i; + myStack.push(i); + int a = 0; + myStack.push(a); + myStack.pop(); + int actual = myStack.peek(); + assertEquals(expected, actual); + } + } + +} diff --git a/group12/382266293/src/test.java b/group12/382266293/src/test.java new file mode 100644 index 0000000000..e4e7fead2d --- /dev/null +++ b/group12/382266293/src/test.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; +import java.util.Queue; +import java.util.Stack; +import java.util.concurrent.PriorityBlockingQueue; +import static util.Print.*; + +public class test { + + public static void main(String[] args) { + Queue queue = new PriorityBlockingQueue(); + println(queue.poll()); + + } + +} diff --git a/group12/382266293/src/util/Print.java b/group12/382266293/src/util/Print.java new file mode 100644 index 0000000000..b2ae48552b --- /dev/null +++ b/group12/382266293/src/util/Print.java @@ -0,0 +1,14 @@ +package util; + + +public class Print { + + public static void print(Object o){ + System.out.print(o); + } + + public static void println(Object o){ + System.out.println(o); + } + +} diff --git a/group12/382266293/src/util/TestUtil.java b/group12/382266293/src/util/TestUtil.java new file mode 100644 index 0000000000..2dfeeade7f --- /dev/null +++ b/group12/382266293/src/util/TestUtil.java @@ -0,0 +1,78 @@ +package util; + +import java.util.Random; + +import Collection.List; +import Collection.Concrete.ArrayList; +import Collection.Concrete.LinkedList; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + + +public class TestUtil extends TestCase { + + private static Random random = new Random(); + private static final int RANDOM_BOUND = 2<<15; + private static final int RANDOM_SIZE = 500; + + + public static int getRandomNumber() { + return random.nextInt(RANDOM_SIZE); + } + + public static void addIntWithNatureOrder(List myList, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myList.add(acutal); + } + } + + public static int[] addRandomInt(List myList, int numbers) { + + int actual = 0; + int[] result = new int[numbers]; + for (int i = 0; i < numbers ; i++) { + actual = random.nextInt(RANDOM_BOUND); + result[i] = actual; + myList.add(actual); + } + return result; + } + + public static void addString(List myList, int num) { + + String actual; + for (int index = 0; index < num ; index++) { + actual = index + ""; + myList.add(actual); + } + } + + public static void testRemoveAndGetFromTail(ArrayList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void testRemoveAndGetFromTail(LinkedList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void enQueueIntWithNatureOrder(Queue myQueue, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myQueue.enQueue(acutal); + } + } + +} From 22d30e299d5006d7c59306d5112b389160a3eee9 Mon Sep 17 00:00:00 2001 From: Lucifer Date: Sun, 26 Feb 2017 14:39:45 +0800 Subject: [PATCH 15/18] 441908378 week1 homework --- group12/441908378/ArrayList.java | 50 +++++++++++ group12/441908378/BinaryTreeNode.java | 47 ++++++++++ group12/441908378/LinkedList.java | 121 ++++++++++++++++++++++++++ group12/441908378/Queue.java | 39 +++++++++ group12/441908378/Stack.java | 30 +++++++ 5 files changed, 287 insertions(+) create mode 100755 group12/441908378/ArrayList.java create mode 100755 group12/441908378/BinaryTreeNode.java create mode 100755 group12/441908378/LinkedList.java create mode 100755 group12/441908378/Queue.java create mode 100755 group12/441908378/Stack.java diff --git a/group12/441908378/ArrayList.java b/group12/441908378/ArrayList.java new file mode 100755 index 0000000000..45e495867c --- /dev/null +++ b/group12/441908378/ArrayList.java @@ -0,0 +1,50 @@ +import java.util.Arrays; + +public class ArrayList { + +private int size = 0; + +private Object[] elementData = new Object[100]; + +public void enlargeCapacity(int minCapacity){ + int oldCapacity=elementData.length; + if(oldCapacityb){ + return left; + }else{ + return right; + } + } + + +} diff --git a/group12/441908378/LinkedList.java b/group12/441908378/LinkedList.java new file mode 100755 index 0000000000..0d0339bc01 --- /dev/null +++ b/group12/441908378/LinkedList.java @@ -0,0 +1,121 @@ +public class LinkedList { + +private Node head; + +private static class Node{ + Object data; + Node next; +} + +public boolean hasNext(Node a){ + if(a.next!=null){ + return true; + } + return false; +} + +public Node getIndex(int index){ + Node a=head.next; + for(int i=0;i Date: Sun, 26 Feb 2017 15:35:34 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../guodong/datastructure/BinaryTreeNode.java | 44 ++++-- .../com/guodong/datastructure/LinkedList.java | 99 +++++++++++- .../src/com/guodong/datastructure/Queue.java | 24 +-- .../src/com/guodong/datastructure/Stack.java | 29 ++-- .../datastructure/test/ArrayListTest.java | 100 ++++++++++-- .../test/BinaryTreeNodeTest.java | 37 +++++ .../datastructure/test/LinkedListTest.java | 143 ++++++++++++++++++ .../guodong/datastructure/test/QueueTest.java | 62 ++++++++ .../guodong/datastructure/test/StackTest.java | 46 ++++++ 9 files changed, 536 insertions(+), 48 deletions(-) create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java create mode 100644 group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java index 933e72acda..677eff3dab 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java @@ -1,32 +1,58 @@ package com.guodong.datastructure; public class BinaryTreeNode { - - private Object data; + + private int data; private BinaryTreeNode left; private BinaryTreeNode right; - - public Object getData() { + + public BinaryTreeNode(int data) { + this.data = data; + } + + public int getData() { return data; } - public void setData(Object data) { + + public void setData(int data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } - - public BinaryTreeNode insert(Object o){ - return null; + + public BinaryTreeNode insert(int o) { + + if (o < data) { + if (left != null) { + left.insert(o); + } else { + left = new BinaryTreeNode(o); + return left; + } + } else { + if (right != null) { + right.insert(o); + } else { + right = new BinaryTreeNode(o); + return right; + } + } + + return null; } - + } diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index 2424a304dc..a7dc12694e 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -1,5 +1,7 @@ package com.guodong.datastructure; +import java.util.NoSuchElementException; + public class LinkedList implements List { private int size; @@ -59,6 +61,10 @@ public Object get(int index) { return getNodeByIndex(index).data; } + public Object getLast() { + return last.data; + } + /** * 根据下标移除链表元素 * @@ -94,28 +100,80 @@ public Object remove(int index) { return data; } + /** + * 返回List长度 + */ public int size() { return size; } - + /** + * 向列表头部添加元素 + * + * @param o + */ public void addFirst(Object o) { + Node n = head; + Node newNode = new Node(o, n); + + head = newNode; + if (n == null) { + last = newNode; + } + size++; } + /** + * 向列表尾部添加元素 + * + * @param o + */ public void addLast(Object o) { linkLast(o); } + /** + * 移除链表第一个元素 + * + * @return + */ public Object removeFirst() { - return null; + Node n = head; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node nextNode = n.next; + + n.data = null; + n.next = null; + + head = nextNode; + if (nextNode == null) { + last = null; + } + + size--; + return data; } public Object removeLast() { - return null; - } + Node n = last; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node prevNode = getNodeByIndex(size - 2); + n.data = null; + if (prevNode == null) { + head = null; + } else { + prevNode.next = null; + } + last = prevNode; - public Iterator iterator() { - return null; + size--; + return data; } /** @@ -128,6 +186,9 @@ public Iterator iterator() { * @return */ private Node getNodeByIndex(int index) { + if (index < 0) { + return null; + } Node n = head; for (int i = 0; i < index; i++) { n = n.next; @@ -195,4 +256,30 @@ private static class Node { this.next = next; } } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current; + + private int index; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (current == null) { + current = getNodeByIndex(index); + } + Object data = current.data; + current = current.next; + index++; + return data; + } + } } diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java index d016046bc5..b14751aab7 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java @@ -1,19 +1,21 @@ package com.guodong.datastructure; public class Queue { - - public void enQueue(Object o){ + private LinkedList element = new LinkedList(); + + public void enQueue(Object o) { + element.addLast(o); } - - public Object deQueue(){ - return null; + + public Object deQueue() { + return element.removeFirst(); } - - public boolean isEmpty(){ - return false; + + public boolean isEmpty() { + return element.size() == 0; } - - public int size(){ - return -1; + + public int size() { + return element.size(); } } diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java index a61c7424aa..f743d0dd3b 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java @@ -1,22 +1,25 @@ package com.guodong.datastructure; public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ + private LinkedList elementData = new LinkedList(); + + public void push(Object o) { + elementData.addLast(o); } - - public Object pop(){ - return null; + + public Object pop() { + return elementData.removeLast(); } - - public Object peek(){ - return null; + + public Object peek() { + return elementData.getLast(); } - public boolean isEmpty(){ - return false; + + public boolean isEmpty() { + return elementData.size() == 0; } - public int size(){ - return -1; + + public int size() { + return elementData.size(); } } diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java index e22d84f72c..ec3a7600a4 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java @@ -2,17 +2,20 @@ import static org.junit.Assert.*; - import org.junit.After; import org.junit.Before; import org.junit.Test; import com.guodong.datastructure.ArrayList; +import com.guodong.datastructure.Iterator; public class ArrayListTest { + ArrayList arrayList; + @Before public void setUp() throws Exception { + arrayList = new ArrayList(); } @After @@ -21,33 +24,112 @@ public void tearDown() throws Exception { @Test public void testAddObject() { - ArrayList arrayList = new ArrayList(); - arrayList.add(1); - assertEquals(1, arrayList.get(0)); + // 测试新增 + arrayList.add(0); + assertEquals(0, arrayList.get(0)); + assertEquals(1, arrayList.size()); + + // 测试扩充 + for (int i = 1; i < 101; i++) { + arrayList.add(i); + } + assertEquals(101, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(-1, 2); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(1, 3); } @Test public void testAddIntObject() { - fail("Not yet implemented"); + // 测试下标新增 + arrayList.add(0, 1); + arrayList.add(1, 2); + arrayList.add(2, 3); + arrayList.add(3, 4); + assertEquals(4, arrayList.size()); + + // 测试中间插入 + arrayList.add(2, 5); + assertEquals(5, arrayList.size()); // 测试插入之后长度 + assertEquals(5, arrayList.get(2)); + assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 + assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 + + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(0); } @Test public void testGet() { - fail("Not yet implemented"); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + assertEquals(1, arrayList.get(0)); + assertEquals(2, arrayList.get(1)); + assertEquals(3, arrayList.get(2)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove1() { + arrayList.remove(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove2() { + arrayList.remove(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove3() { + arrayList.remove(1); } @Test public void testRemove() { - fail("Not yet implemented"); + arrayList.add(1); + arrayList.remove(0); + assertEquals(0, arrayList.size()); + + arrayList.add(1); + arrayList.add(2); + arrayList.remove(0); + assertEquals(1, arrayList.size()); + assertEquals(2, arrayList.get(0)); } @Test public void testSize() { - fail("Not yet implemented"); + arrayList.add(1); + assertEquals(1, arrayList.size()); } @Test public void testIterator() { - fail("Not yet implemented"); + Iterator iterator = arrayList.iterator(); + assertFalse(iterator.hasNext()); + + arrayList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); } } diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..83972b7776 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java @@ -0,0 +1,37 @@ +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUp() throws Exception { + binaryTreeNode = new BinaryTreeNode(50); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + binaryTreeNode.insert(20); + binaryTreeNode.insert(30); + binaryTreeNode.insert(60); + binaryTreeNode.insert(80); + + assertEquals(20, binaryTreeNode.getLeft().getData()); + assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(60, binaryTreeNode.getRight().getData()); + assertEquals(80, binaryTreeNode.getRight().getRight().getData()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java new file mode 100644 index 0000000000..054e8f81b4 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java @@ -0,0 +1,143 @@ +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Iterator; +import com.guodong.datastructure.LinkedList; + +public class LinkedListTest { + + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add(1); + assertEquals(1, linkedList.size()); + assertEquals(1, linkedList.get(0)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + linkedList.add(-1, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + linkedList.add(1, 1); + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(1, linkedList.get(0)); + + linkedList.add(1,3); + assertEquals(2, linkedList.get(2)); + assertEquals(3, linkedList.get(1)); + assertEquals(3, linkedList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + linkedList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + linkedList.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet3() { + linkedList.get(1); + } + + @Test + public void testGet() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(2, linkedList.get(1)); + } + + @Test + public void testGetLast() { + linkedList.add(1); + assertEquals(1, linkedList.getLast()); + + linkedList.add(2); + assertEquals(2, linkedList.getLast()); + } + + @Test + public void testRemove() { + linkedList.add(1); + assertEquals(1, linkedList.remove(0)); + assertEquals(0, linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add(1); + linkedList.add(1); + linkedList.add(1); + assertEquals(3, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.get(0)); + + linkedList.addFirst(2); + linkedList.addFirst(3); + assertEquals(3, linkedList.get(0)); + assertEquals(1, linkedList.getLast()); + } + + @Test + public void testAddLast() { + linkedList.addLast(1); + assertEquals(1, linkedList.getLast()); + assertEquals(1, linkedList.get(0)); + } + + @Test + public void testRemoveFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testRemoveLast() { + linkedList.addLast(2); + assertEquals(2, linkedList.removeLast()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + assertFalse(iterator.hasNext()); + + linkedList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java new file mode 100644 index 0000000000..86a4ebdd68 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java @@ -0,0 +1,62 @@ +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Queue; + +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + } + + @Test(expected = NoSuchElementException.class) + public void testDeQueueExecption() { + queue.deQueue(); + } + + @Test + public void testDeQueue() { + queue.enQueue(1); + assertEquals(1, queue.deQueue()); + assertTrue(queue.isEmpty()); + } + + @Test + public void testIsEmpty() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + + queue.deQueue(); + assertTrue(queue.isEmpty()); + } + + @Test + public void testSize() { + queue.enQueue(1); + queue.enQueue(1); + queue.enQueue(1); + + assertEquals(3, queue.size()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java new file mode 100644 index 0000000000..36781c863f --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java @@ -0,0 +1,46 @@ +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPush() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPeek() { + stack.push(11); + assertEquals(11, stack.peek()); + assertFalse(stack.isEmpty()); + assertEquals(1, stack.size()); + } + +} From 5be5c11e941991fb15ac6963696bbedb19f223d6 Mon Sep 17 00:00:00 2001 From: GUK0 <1685605435@qq.com> Date: Sun, 26 Feb 2017 16:43:52 +0800 Subject: [PATCH 17/18] to be test --- group12/247565311/week1/ArrayList.java | 4 +- group12/247565311/week1/Deque.java | 8 +- group12/247565311/week1/LinkedList.java | 110 ++++++++++++++++++------ group12/247565311/week1/Stack.java | 17 +++- 4 files changed, 108 insertions(+), 31 deletions(-) diff --git a/group12/247565311/week1/ArrayList.java b/group12/247565311/week1/ArrayList.java index 3e4c12d7a7..c2643af683 100644 --- a/group12/247565311/week1/ArrayList.java +++ b/group12/247565311/week1/ArrayList.java @@ -36,7 +36,7 @@ public boolean add(E arg0) { @Override public void add(int arg0, E arg1) { - if(arg1 == null || arg0>size+1 || 0>arg0) return ; + if( arg0>size || 0leng){ @@ -134,7 +134,7 @@ public int indexOf(Object arg0) { @Override public boolean isEmpty() { - return this.size==0?true:false; + return this.size==0; } @Override public int lastIndexOf(Object arg0) { diff --git a/group12/247565311/week1/Deque.java b/group12/247565311/week1/Deque.java index 93004bf80d..b71c4b9fac 100644 --- a/group12/247565311/week1/Deque.java +++ b/group12/247565311/week1/Deque.java @@ -11,18 +11,18 @@ public Deque(int arg0){ data = new LinkedList(arg0); } public boolean push(E arg0){ - data.add(arg0); + data.add(data.size(),arg0); size += 1; return true; } public E pop(){ size -= 1; - E res = data.get(size); - data.remove(size); + E res = data.get(0); + data.remove(0); return res; } public E peek(){ - return data.get(size-1); + return data.get(0); } public int size(){ return this.size; diff --git a/group12/247565311/week1/LinkedList.java b/group12/247565311/week1/LinkedList.java index debd53d8d2..c3f0ca2eb8 100644 --- a/group12/247565311/week1/LinkedList.java +++ b/group12/247565311/week1/LinkedList.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.ListIterator; -public class LinkedList implements List { +public class LinkedList implements List,Cloneable { private Node head = null; private Node tail = null; private int size = 0; @@ -24,9 +24,24 @@ public LinkedList(int arg0){ tail.ahead = head; size = 0; } + public Object clone(){ + LinkedList clone = null; + try { + clone = (LinkedList)(super.clone()); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + clone.head = new Node(null); + clone.tail = new Node(null); + clone.size = 0; + for(Node x = head.next;x!=null;x = x.next){ + clone.add(x.val); + } + return clone; + } @Override - public boolean add(E arg0) { - Node n = new Node(arg0); + public boolean add(Object val) { + Node n = new Node(val); n.next = tail; n.ahead = tail.ahead; tail.ahead.next = n; @@ -37,7 +52,7 @@ public boolean add(E arg0) { @Override public void add(int arg0, E arg1) { - if(arg0<0) arg0=0; + if(arg0<0 || arg0>size) arg0=0; Node n=new Node(arg1),p=head; for(int i=0;i arg0) { public E get(int arg0) { E res = null; if(arg0>-1 && arg0 < size){ - + Node n = head; + for(int i=0;i iterator() { @Override public int lastIndexOf(Object arg0) { - - return 0; + boolean flag = arg0==null; + Node n = tail; + for(int i=size-1;i>-1;i--){ + n = n.ahead; + if(flag){ + if(n.val == null) return i; + }else{ + if(arg0.equals(n.val)) return i; + } + } + return -1; } @Override @@ -145,38 +181,59 @@ public ListIterator listIterator(int arg0) { @Override public boolean remove(Object arg0) { - - return false; + Node n = head; + int index = this.indexOf(arg0); + if(index == -1) return false; + for(int i=0;isize-1) return null; + for(int i=0;i arg0) { - - return false; + for(Object o:arg0){ + if(!this.remove(o)) return false; + } + return true; } @Override public boolean retainAll(Collection arg0) { - + // ? return false; } @Override public E set(int arg0, E arg1) { - - return null; + if(arg0<0 || arg0>size-1) return null; + Node n=head; + for(int i=0;i subList(int arg0, int arg1) { @Override public Object[] toArray() { - - return null; + Object[]res = new Object[size]; + Node n = head; + for(int i=0;i(size); } + public boolean isEmpty(){ + return size==0; + } public boolean push(E arg0){ size += 1; data.add(arg0); + return true; + } + public E pop(){ + if(this.isEmpty()) return null; + size -= 1; + E res = data.get(size); + data.remove(size); + return res; + } + public E peek(){ + if(this.isEmpty()) return null; + E res = data.get(size-1); + return res; } - public E pop() } From 1398fcd89dccef9efda7e8112cc1a11f20cb9d13 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 26 Feb 2017 18:37:42 +0800 Subject: [PATCH 18/18] home work --- group12/251822722/ArrayList.java | 87 +++++++++++++++++++++++++++ group12/251822722/BinaryTreeNode.java | 35 +++++++++++ group12/251822722/Iterator.java | 6 ++ group12/251822722/LinkedList.java | 51 ++++++++++++++++ group12/251822722/List.java | 11 ++++ group12/251822722/Queue.java | 38 ++++++++++++ group12/251822722/Stack.java | 33 ++++++++++ 7 files changed, 261 insertions(+) create mode 100755 group12/251822722/ArrayList.java create mode 100755 group12/251822722/BinaryTreeNode.java create mode 100755 group12/251822722/Iterator.java create mode 100755 group12/251822722/LinkedList.java create mode 100755 group12/251822722/List.java create mode 100755 group12/251822722/Queue.java create mode 100755 group12/251822722/Stack.java diff --git a/group12/251822722/ArrayList.java b/group12/251822722/ArrayList.java new file mode 100755 index 0000000000..77b8052cc7 --- /dev/null +++ b/group12/251822722/ArrayList.java @@ -0,0 +1,87 @@ +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int index =0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + elementData[size] = o; + size = size+1; + + } + + public void add(int index, Object o) { + + Object[] elementDataNew =null; + if(size