From 82c0d8297fcee87f5f0e248c8b31158dd661978f Mon Sep 17 00:00:00 2001 From: HaoSong Date: Wed, 15 Mar 2017 11:12:22 +0800 Subject: [PATCH 1/4] 3.15 --- group24/330657387/.project | 2 +- .../src/com/coding/basic/BinaryTreeNode.java | 32 ------ .../330657387/src/com/coding/basic/Queue.java | 19 ---- .../330657387/src/com/coding/basic/Stack.java | 22 ---- .../week01/data_structure}/ArrayList.java | 2 +- .../week01/data_structure/BinaryTreeNode.java | 102 ++++++++++++++++++ .../week01/data_structure}/Iterator.java | 2 +- .../week01/data_structure}/LinkedList.java | 2 +- .../week01/data_structure}/List.java | 3 +- .../src/main/week01/data_structure/Queue.java | 23 ++++ .../src/main/week01/data_structure/Stack.java | 28 +++++ .../week01/data_structure/ArrayListTest.java | 49 +++++++++ .../org.eclipse.core.resources.prefs | 2 + 13 files changed, 210 insertions(+), 78 deletions(-) delete mode 100644 group24/330657387/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 group24/330657387/src/com/coding/basic/Queue.java delete mode 100644 group24/330657387/src/com/coding/basic/Stack.java rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/ArrayList.java (97%) create mode 100644 group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/Iterator.java (69%) rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/LinkedList.java (98%) rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/List.java (82%) create mode 100644 group24/330657387/src/main/week01/data_structure/Queue.java create mode 100644 group24/330657387/src/main/week01/data_structure/Stack.java create mode 100644 group24/330657387/src/test/week01/data_structure/ArrayListTest.java create mode 100644 liuxin/.settings/org.eclipse.core.resources.prefs diff --git a/group24/330657387/.project b/group24/330657387/.project index fab8d7f04c..e929d098c9 100644 --- a/group24/330657387/.project +++ b/group24/330657387/.project @@ -1,6 +1,6 @@ - 2017Learning + 2017Learning_330657387 diff --git a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java b/group24/330657387/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/group24/330657387/src/com/coding/basic/Queue.java b/group24/330657387/src/com/coding/basic/Queue.java deleted file mode 100644 index 36e516e266..0000000000 --- a/group24/330657387/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/group24/330657387/src/com/coding/basic/Stack.java b/group24/330657387/src/com/coding/basic/Stack.java deleted file mode 100644 index a5a04de76d..0000000000 --- a/group24/330657387/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/group24/330657387/src/com/coding/basic/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java similarity index 97% rename from group24/330657387/src/com/coding/basic/ArrayList.java rename to group24/330657387/src/main/week01/data_structure/ArrayList.java index 1728a28291..190402488c 100644 --- a/group24/330657387/src/com/coding/basic/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; import java.util.Arrays; diff --git a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java new file mode 100644 index 0000000000..357a7d32a7 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java @@ -0,0 +1,102 @@ +package main.week01.data_structure; + +import com.sun.org.apache.regexp.internal.recompile; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + + public BinaryTreeNode(){} + + public BinaryTreeNode(T data) + { + this.data=data; + this.left=null; + this.right=null; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public 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; + } + + /** + * data�������ظ� + * @param data + * @return + */ + public BinaryTreeNode insert(T data){ + int compareResult = this.data.compareTo(data); + if(compareResult == 0){ + return this; + } + if(compareResult > 0){ + if(this.left == null){ + this.left = new BinaryTreeNode(data); + return this.left; + }else{ + return this.left.insert(data); + } + }else{ + if(this.right == null){ + this.right = new BinaryTreeNode(data); + return this.right; + }else{ + return this.right.insert(data); + } + } + } + + /** + * �����ڵ�Ϊ�� + * @param data + * @return + */ + public BinaryTreeNode search(T data){ + if(this.data == null){ + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + + } + + + +} diff --git a/group24/330657387/src/com/coding/basic/Iterator.java b/group24/330657387/src/main/week01/data_structure/Iterator.java similarity index 69% rename from group24/330657387/src/com/coding/basic/Iterator.java rename to group24/330657387/src/main/week01/data_structure/Iterator.java index 06ef6311b2..194891e56a 100644 --- a/group24/330657387/src/com/coding/basic/Iterator.java +++ b/group24/330657387/src/main/week01/data_structure/Iterator.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; public interface Iterator { public boolean hasNext(); diff --git a/group24/330657387/src/com/coding/basic/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java similarity index 98% rename from group24/330657387/src/com/coding/basic/LinkedList.java rename to group24/330657387/src/main/week01/data_structure/LinkedList.java index 8357e13254..f4c967ba44 100644 --- a/group24/330657387/src/com/coding/basic/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; import java.util.NoSuchElementException; diff --git a/group24/330657387/src/com/coding/basic/List.java b/group24/330657387/src/main/week01/data_structure/List.java similarity index 82% rename from group24/330657387/src/com/coding/basic/List.java rename to group24/330657387/src/main/week01/data_structure/List.java index 10d13b5832..6cbc15f8ab 100644 --- a/group24/330657387/src/com/coding/basic/List.java +++ b/group24/330657387/src/main/week01/data_structure/List.java @@ -1,6 +1,7 @@ -package com.coding.basic; +package main.week01.data_structure; public interface List { + public void add(Object o); public void add(int index, Object o); public Object get(int index); diff --git a/group24/330657387/src/main/week01/data_structure/Queue.java b/group24/330657387/src/main/week01/data_structure/Queue.java new file mode 100644 index 0000000000..7da6edf433 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Queue.java @@ -0,0 +1,23 @@ +package main.week01.data_structure; + +public class Queue { + + private LinkedList elementData; + private int size = 0; + + public void enQueue(Object o){ + elementData.add(size++,o); + } + + public Object deQueue(){ + return elementData.remove(size---1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group24/330657387/src/main/week01/data_structure/Stack.java b/group24/330657387/src/main/week01/data_structure/Stack.java new file mode 100644 index 0000000000..5fc5410fde --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Stack.java @@ -0,0 +1,28 @@ +package main.week01.data_structure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(size++,o); + } + + public Object pop() { + Object temp = elementData.get(size-1); + elementData.remove(size---1); + return temp; + } + + public Object peek() { + return elementData.get(size-1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java new file mode 100644 index 0000000000..a9f00d99c1 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -0,0 +1,49 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnsureCapacity() { + fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + } + + @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"); + } + +} diff --git a/liuxin/.settings/org.eclipse.core.resources.prefs b/liuxin/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/liuxin/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 From 94071e5fe6a7d688e6a138da1531d0bb4ab205af Mon Sep 17 00:00:00 2001 From: HaoSong Date: Wed, 15 Mar 2017 17:11:39 +0800 Subject: [PATCH 2/4] 3.15 --- .../main/week01/data_structure/ArrayList.java | 96 ++++++++++++------- .../week01/data_structure/BinaryTreeNode.java | 19 ++++ .../week01/data_structure/LinkedList.java | 68 +++++++++---- .../week01/data_structure/ArrayListTest.java | 59 ++++++++---- 4 files changed, 170 insertions(+), 72 deletions(-) diff --git a/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java index 190402488c..93aee4cc92 100644 --- a/group24/330657387/src/main/week01/data_structure/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -3,60 +3,86 @@ import java.util.Arrays; public class ArrayList implements List { - + private int size = 0; - + private Object[] elementData = new Object[10]; + + private void ensureCapacity(int input) { + if (input > elementData.length) { + growCapacity(); + } + } - public void ensureCapacity(int input) { - if (input > elementData.length) { - growCapacity(); - } - } - - private void growCapacity(){ - elementData = Arrays.copyOf(elementData, size * 2); - } + private void growCapacity() { + elementData = Arrays.copyOf(elementData, size * 2); + } - public void add(Object o){ + private void rangeCheck(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(Object o) { ensureCapacity(size + 1); elementData[size++] = o; } - - - public void add(int index, Object o){ + + public void add(int index, Object o) { rangeCheck(index); ensureCapacity(size + 1); - System.arraycopy(elementData,index, elementData, index + 1, size - index); + System.arraycopy(elementData, index, elementData, index + 1, size + - index); elementData[index] = o; - size ++; - } - - private void rangeCheck(int index){ - if (index > size || index < 0){ - throw new IndexOutOfBoundsException(); - } + size++; } - - public Object get(int index){ + + public Object get(int index) { rangeCheck(index); return elementData[index]; } - - public Object remove(int index){ + + public Object remove(int index) { rangeCheck(index); Object dest = elementData[index]; - System.arraycopy(elementData, index +1, elementData, index, size-index-1); - size --; + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + size--; return dest; } - - public int size(){ + + public int size() { return size; } - - public Iterator iterator(){ - return null; + + public class ArrayListIterator implements Iterator { + + private ArrayList list; + + private int position = 0; + + private ArrayListIterator() { + } + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + } - + + public ArrayListIterator iterator() { + return new ArrayListIterator(this); + } + } diff --git a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java index 357a7d32a7..296744c188 100644 --- a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java @@ -74,6 +74,7 @@ public BinaryTreeNode insert(T data){ * @param data * @return */ + @SuppressWarnings("unchecked") public BinaryTreeNode search(T data){ if(this.data == null){ return null; @@ -97,6 +98,24 @@ public BinaryTreeNode search(T data){ } + private BinaryTreeNode findMin() { + if (this.data == null) { + return null; + } + if (this.left == null) { + return this; + } + return this.left.findMin(); + } + private BinaryTreeNode findMax() { + if (this.data == null) { + return null; + } + if (this.right == null) { + return this; + } + return this.right.findMin(); + } } diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index f4c967ba44..d89842f166 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -2,6 +2,8 @@ import java.util.NoSuchElementException; +import main.week01.data_structure.ArrayList.ArrayListIterator; + public class LinkedList implements List { private Node head; @@ -11,7 +13,7 @@ public void add(Object o) { if (isEmpty()) { head = new Node(o); } else { - Node tail = (Node)get(size-1); + Node tail = (Node) get(size - 1); Node node = new Node(o); tail.next = node; } @@ -24,28 +26,28 @@ public boolean isEmpty() { public void add(int index, Object o) { rangeCheck(index); - if (index ==0) { + if (index == 0) { Node node = new Node(o); node.next = head; head = node; } else { - Node pre = (Node)get(index-1); + Node pre = (Node) get(index - 1); Node node = new Node(o); node.next = pre.next; pre.next = node; } } - - private void rangeCheck(int index){ - if (index >= size || index <0){ - throw new IndexOutOfBoundsException(); + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); } } - + public Object get(int index) { rangeCheck(index); Node dest = head; - for (int i = 0; i< index; i++){ + for (int i = 0; i < index; i++) { dest = dest.next; } return dest.data; @@ -53,10 +55,10 @@ public Object get(int index) { public Object remove(int index) { rangeCheck(index); - Node pre = (Node)get(index); + Node pre = (Node) get(index); Node dest = pre.next; pre.next = dest.next; - size --; + size--; return dest; } @@ -68,11 +70,11 @@ public void addFirst(Object o) { Node node = new Node(o); node.next = head; head = node; - size ++; + size++; } public void addLast(Object o) { - Node last = (Node)get(size-1); + Node last = (Node) get(size - 1); Node node = new Node(o); last.next = node; size++; @@ -84,7 +86,7 @@ public Object removeFirst() { } Node newhead = head; head = head.next; - size --; + size--; return newhead; } @@ -95,20 +97,16 @@ public Object removeLast() { if (head.next == null) { Node tmp = head; head = null; - size --; + size--; return tmp; } - Node newLastNode = (Node)get(size-2); + Node newLastNode = (Node) get(size - 2); Node oldLastNode = newLastNode.next; newLastNode.next = null; - size --; + size--; return oldLastNode; } - public Iterator iterator() { - return null; - } - private static class Node { Object data; Node next; @@ -119,4 +117,32 @@ private static class Node { } } + public class LinkedListIterator implements Iterator { + + private LinkedList list; + + private int position = 0; + + private LinkedListIterator() { + } + + private LinkedListIterator(LinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + + } + + public LinkedListIterator iterator() { + return new LinkedListIterator(this); + } } diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java index a9f00d99c1..dacef0cbcc 100644 --- a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -1,49 +1,76 @@ package test.week01.data_structure; import static org.junit.Assert.*; +import main.week01.data_structure.ArrayList; +import main.week01.data_structure.ArrayList.ArrayListIterator; -import org.junit.After; import org.junit.Before; import org.junit.Test; public class ArrayListTest { + public static ArrayList list; + @Before public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEnsureCapacity() { - fail("Not yet implemented"); + list = new ArrayList(); } @Test public void testAddObject() { - fail("Not yet implemented"); + list.add(1); + list.add(2); + list.add(2); + assertEquals(3,list.size()); } @Test public void testAddIntObject() { - fail("Not yet implemented"); + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(2,list.get(0)); } @Test public void testGet() { - fail("Not yet implemented"); + list.add("songhao"); + assertEquals("songhao", list.get(0)); } @Test public void testRemove() { - fail("Not yet implemented"); + list.add("songhao"); + assertEquals("songhao", list.remove(0)); } @Test - public void testSize() { - fail("Not yet implemented"); + public void testSize(){ + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(4,list.size()); + } + + @Test + public void testIterator() { + list.add(0,1); + list.add(1,2); + list.add(2,3); + list.add(0,4); + ArrayListIterator iter = list.iterator(); + assertTrue(iter.hasNext()); + assertEquals(4, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(1, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(2, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(3, iter.next()); + assertFalse(iter.hasNext()); + } } From 60424407b836cd5d579e2544bc66a8ec976315ea Mon Sep 17 00:00:00 2001 From: HaoSong Date: Thu, 16 Mar 2017 13:30:53 +0800 Subject: [PATCH 3/4] 3.16 --- .../main/week01/data_structure/ArrayList.java | 2 +- .../week01/data_structure/LinkedList.java | 52 +++++++++--------- .../week01/data_structure/ArrayListTest.java | 8 +++ .../data_structure/BinaryTreeNodeTest.java | 28 ++++++++++ .../week01/data_structure/LinkedListTest.java | 54 +++++++++++++++++++ 5 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java create mode 100644 group24/330657387/src/test/week01/data_structure/LinkedListTest.java diff --git a/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java index 93aee4cc92..9a2f4d2c8b 100644 --- a/group24/330657387/src/main/week01/data_structure/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -48,7 +48,7 @@ public Object remove(int index) { Object dest = elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - size--; + elementData[size---1]=null;//��ֹ�ڴ�й© return dest; } diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index d89842f166..b61df9f254 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -2,8 +2,6 @@ import java.util.NoSuchElementException; -import main.week01.data_structure.ArrayList.ArrayListIterator; - public class LinkedList implements List { private Node head; @@ -11,13 +9,10 @@ public class LinkedList implements List { public void add(Object o) { if (isEmpty()) { - head = new Node(o); + addFirst(o); } else { - Node tail = (Node) get(size - 1); - Node node = new Node(o); - tail.next = node; + addLast(o); } - size++; } public boolean isEmpty() { @@ -27,14 +22,15 @@ public boolean isEmpty() { public void add(int index, Object o) { rangeCheck(index); if (index == 0) { - Node node = new Node(o); - node.next = head; - head = node; + addFirst(o); + } else if (index == size) { + addLast(o); } else { - Node pre = (Node) get(index - 1); + Node pre = getNode(index - 1); Node node = new Node(o); node.next = pre.next; pre.next = node; + size++; } } @@ -53,13 +49,27 @@ public Object get(int index) { return dest.data; } + public Node getNode(int index) { + rangeCheck(index); + Node dest = head; + for (int i = 0; i < index; i++) { + dest = dest.next; + } + return dest; + } + public Object remove(int index) { rangeCheck(index); - Node pre = (Node) get(index); + if (index == 0) { + return removeFirst(); + }else if(index == size){ + return removeLast(); + } + Node pre = getNode(index - 1); Node dest = pre.next; pre.next = dest.next; size--; - return dest; + return dest.data; } public int size() { @@ -74,7 +84,7 @@ public void addFirst(Object o) { } public void addLast(Object o) { - Node last = (Node) get(size - 1); + Node last = getNode(size - 1); Node node = new Node(o); last.next = node; size++; @@ -85,22 +95,14 @@ public Object removeFirst() { throw new NoSuchElementException(); } Node newhead = head; + Node dest = head; head = head.next; size--; - return newhead; + return dest.data; } public Object removeLast() { - if (head == null) { - throw new NoSuchElementException(); - } - if (head.next == null) { - Node tmp = head; - head = null; - size--; - return tmp; - } - Node newLastNode = (Node) get(size - 2); + Node newLastNode = getNode(size - 2); Node oldLastNode = newLastNode.next; newLastNode.next = null; size--; diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java index dacef0cbcc..d9d63339b2 100644 --- a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -31,6 +31,14 @@ public void testAddIntObject() { list.add(2,2); list.add(0,2); assertEquals(2,list.get(0)); + try{ + list.add(-1 , "test"); + fail("-1 can't be index"); + list.add(1000, "test"); + fail("out of range"); + }catch (Exception e){ + + } } @Test diff --git a/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..515da2e7d4 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.BinaryTreeNode; + +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + BinaryTreeNode head=new BinaryTreeNode(); + + head.setData(5); + head.insert(2); + head.insert(7); + head.insert(1); + head.insert(4); + head.insert(3); + assertEquals(3,head.getLeft().getRight().getLeft().getData()); + } + +} diff --git a/group24/330657387/src/test/week01/data_structure/LinkedListTest.java b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..cf374a6dcb --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java @@ -0,0 +1,54 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.LinkedList; +import main.week01.data_structure.LinkedList.LinkedListIterator; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @Test + public void testGet() { + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertEquals("C", list.get(0)); + } + + @Test + public void testRemove() { + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add(0, "E"); + assertEquals("E", list.remove(0)); + assertEquals("D", list.remove(list.size()-1)); + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + LinkedListIterator iter = list.iterator(); + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertTrue(iter.hasNext()); + assertEquals("C", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("A", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("B", iter.next()); + assertFalse(iter.hasNext()); + } + +} From 6212acf96c0068d952f9d45321612019b54c9b70 Mon Sep 17 00:00:00 2001 From: sargeles <330657387@qq.com> Date: Sun, 19 Mar 2017 23:41:41 +0800 Subject: [PATCH 4/4] 3.19 --- .../src/main/week02/practice/ArrayUtil.java | 115 ++++++++++++++++++ .../test/week02/practice/ArrayUtilTest.java | 79 ++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 group24/330657387/src/main/week02/practice/ArrayUtil.java create mode 100644 group24/330657387/src/test/week02/practice/ArrayUtilTest.java diff --git a/group24/330657387/src/main/week02/practice/ArrayUtil.java b/group24/330657387/src/main/week02/practice/ArrayUtil.java new file mode 100644 index 0000000000..3ab82a31e8 --- /dev/null +++ b/group24/330657387/src/main/week02/practice/ArrayUtil.java @@ -0,0 +1,115 @@ +package main.week02.practice; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (origin.length <= 1) { + return; + } + int i = 0, j = origin.length - 1; + int temp; + while (j > i) { + origin[i] = origin[i] ^ origin[j]; + origin[j] = origin[i] ^ origin[j]; + origin[i] = origin[i] ^ origin[j]; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int i = 0; + for (int j = 0; j < oldArray.length; j++) { + if (0 != oldArray[j]) { + oldArray[i++] = oldArray[j]; + } + } + int[] newArray = new int[i]; + System.arraycopy(oldArray, 0, newArray, 0,newArray.length); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + return null; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + return null; + } + +} diff --git a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java new file mode 100644 index 0000000000..1bf97fced3 --- /dev/null +++ b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java @@ -0,0 +1,79 @@ +package test.week02.practice; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import main.week02.practice.ArrayUtil; + +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[][] origin = {{1,20,5,3,65,4,6,9,7}, + {1}, + {1,2,3}, + {}, + {23,32}}; + for(int[] a : origin){ + System.out.println("前:"+Arrays.toString(a)); + util.reverseArray(a); + System.out.println("后:"+Arrays.toString(a)); + } + } + + @Test + public void testRemoveZero() { + int[][] origin = {{1,20,0,0,5,3,65,4,0,6,9,7}, + {1,0}, + {1,0,2,3,0}, + {}, + {23,0,0,32}}; + for(int[] a : origin){ + System.out.println("前:"+Arrays.toString(a)); + System.out.println("后:"+Arrays.toString(util.removeZero(a))); + } + } + + @Test + public void testMerge() { + fail("Not yet implemented"); + } + + @Test + public void testGrow() { + fail("Not yet implemented"); + } + + @Test + public void testFibonacci() { + fail("Not yet implemented"); + } + + @Test + public void testGetPrimes() { + fail("Not yet implemented"); + } + + @Test + public void testGetPerfectNumbers() { + fail("Not yet implemented"); + } + + @Test + public void testJoin() { + fail("Not yet implemented"); + } + +}