From ce769fdeadedfc820f74a0fede0492dfe982cb0c Mon Sep 17 00:00:00 2001 From: "songbao.yang" Date: Sun, 26 Feb 2017 23:55:49 +0800 Subject: [PATCH 1/2] task1 --- group05/578505552/.gitignore | 17 +++ group05/578505552/578505552.iml | 16 +++ group05/578505552/pom.xml | 24 ++++ .../main/java/com/coding/basic/ArrayList.java | 104 ++++++++++++++++++ .../java/com/coding/basic/BinaryTreeNode.java | 36 ++++++ .../main/java/com/coding/basic/Iterator.java | 11 ++ .../java/com/coding/basic/LinkedList.java | 100 +++++++++++++++++ .../src/main/java/com/coding/basic/List.java | 13 +++ .../src/main/java/com/coding/basic/Queue.java | 90 +++++++++++++++ .../src/main/java/com/coding/basic/Stack.java | 63 +++++++++++ group05/578505552/src/test/java/JavaTest.java | 25 +++++ .../java/com/coding/basic/ArrayListTest.java | 97 ++++++++++++++++ .../test/java/com/coding/basic/QueueTest.java | 75 +++++++++++++ .../test/java/com/coding/basic/StackTest.java | 65 +++++++++++ 14 files changed, 736 insertions(+) create mode 100644 group05/578505552/.gitignore create mode 100644 group05/578505552/578505552.iml create mode 100644 group05/578505552/pom.xml create mode 100644 group05/578505552/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/Iterator.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/List.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/Queue.java create mode 100644 group05/578505552/src/main/java/com/coding/basic/Stack.java create mode 100644 group05/578505552/src/test/java/JavaTest.java create mode 100644 group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java create mode 100644 group05/578505552/src/test/java/com/coding/basic/QueueTest.java create mode 100644 group05/578505552/src/test/java/com/coding/basic/StackTest.java diff --git a/group05/578505552/.gitignore b/group05/578505552/.gitignore new file mode 100644 index 0000000000..05ba5e4384 --- /dev/null +++ b/group05/578505552/.gitignore @@ -0,0 +1,17 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ diff --git a/group05/578505552/578505552.iml b/group05/578505552/578505552.iml new file mode 100644 index 0000000000..faa2a57144 --- /dev/null +++ b/group05/578505552/578505552.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/578505552/pom.xml b/group05/578505552/pom.xml new file mode 100644 index 0000000000..3e841624e0 --- /dev/null +++ b/group05/578505552/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + + com.coding2017.yang + basic + 1.0-SNAPSHOT + jar + + basic + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + + + diff --git a/group05/578505552/src/main/java/com/coding/basic/ArrayList.java b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1620048907 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,104 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData; + private static final int MIN_CAPACITY = 10; + + public ArrayList(int size) { + if (size < 0){ + throw new IllegalArgumentException("illega size: " + size); + } + this.elementData = new Object[size]; + } + + public ArrayList() { + this.elementData = new Object[0]; + } + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + + private void ensureCapacity(int minCapacity){ + if (minCapacity < 0 ){ + throw new OutOfMemoryError(); + } + + int newCapcity = size; + if(minCapacity < MIN_CAPACITY){ + newCapcity = MIN_CAPACITY; + } else if(minCapacity > elementData.length){ + int tmp = elementData.length << 1; + newCapcity = tmp > elementData.length ? tmp : Integer.MAX_VALUE; + } + + newCapcity = minCapacity; + Object[] newData = new Object[newCapcity]; + System.arraycopy(elementData, 0, newData, 0, size); + elementData = newData; + } + + public void add(int index, Object o){ + indexCheck(index); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + indexCheck(index); + return elementData[index]; + } + + private void indexCheck(int index){ + if(index < 0){ + throw new IllegalArgumentException("illegal index: " + index); + } + if(index >= size){ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + indexCheck(index); + Object rm = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return rm; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + //静态内部类的访问权限不同有何区别?? + private class Itr implements Iterator{ + private int cursor = 0; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + if (hasNext()){ + return elementData[cursor++]; + } + + throw new NoSuchElementException(); + } + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..0169aeb79a --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Iterator.java b/group05/578505552/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..6765eae519 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,11 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..c3eeb54950 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class LinkedList implements List { + + private Node head; + private int elementCount; + + //head作为一个节点,其next的值指向List中的真正的第一个节点 + public LinkedList() { + Node head = new Node(); + head.next = null; + head.data = null; + elementCount = 0; + } + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + Node cursor = head; + while (cursor.next != null){ + cursor = cursor.next; + } + cursor.next = newNode; + elementCount++; + } + + + public void add(int index , Object o){ + indexRangeCheck(index); + Node newNode = new Node(); + newNode.data = o; + + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; //将cursor移动到index-1节点处; + } + + newNode.next = cursor.next; //将新节点指向原index处的节点 + cursor.next = newNode;//将原index-1处的节点指向新节点 + elementCount++; + } + + private void indexRangeCheck(int index){ + if (index < 0 || index >= size()){ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + indexRangeCheck(index); + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor.next; + } + + public Object remove(int index){ + indexRangeCheck(index); + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + Node indexNode = cursor.next; + cursor.next = indexNode.next; + return indexNode; + } + + public int size(){ + return elementCount; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/List.java b/group05/578505552/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..a979b2fdb9 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Queue.java b/group05/578505552/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..ec31573ee7 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,90 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by songbao.yang on 2017/2/22. + * + */ +public class Queue { + + private Object[] elementData; + private int head; //对头的位置 + private int tail; //队尾的位置 + private int size; //队列中元素的个数 + private static final int MIN_INITIAL_CAPACITY = 10; + + public Queue() { + this.elementData = new Object[MIN_INITIAL_CAPACITY]; + this.head = 0; + this.tail = 0; + this.size = 0; + } + + public Queue(int initCapcacity) { + if (initCapcacity < MIN_INITIAL_CAPACITY){ + initCapcacity = MIN_INITIAL_CAPACITY; + } + this.elementData = new Object[initCapcacity]; + this.head = 0; + this.tail = 0; + this.size = 0; + } + + public void enQueue(Object o){ + ensureCapacity(size+1); + if(size != 0){ + tail++; + } + if(tail == elementData.length){ + tail = 0; + } + elementData[tail] = o; + size++; + } + + private void ensureCapacity(int minCapcacity){ + if(minCapcacity <= elementData.length){ + return; + } + + int newCapcacity = elementData.length << 1; + if (newCapcacity < elementData.length){ + newCapcacity = Integer.MAX_VALUE; + } + + Object[] newData = new Object[newCapcacity]; + if(size != 0){ + if(tail >= head){ + System.arraycopy(elementData, head, newData, 0, size); + } else { + System.arraycopy(elementData, head, newData, 0, elementData.length - head); + System.arraycopy(elementData, 0, newData, elementData.length - head, tail + 1); + } + elementData = newData; + head = 0; + tail = this.size - 1; + } + } + + public Object deQueue(){ + if (isEmpty()){ + throw new NoSuchElementException("empty queue"); + } + Object ele = elementData[head]; + size--; + head++; + if(head == elementData.length){ + head = 0; + } + return ele; + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Stack.java b/group05/578505552/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..ffc4915bef --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,63 @@ +package com.coding.basic; + + +import java.util.EmptyStackException; + +/** + * Created by songbao.yang on 2017/2/22. + * + */ +public class Stack { + + private Object[] elementData; + private static final int MIN_INITIAL_CAPACITY = 10; + private int cursor; + + public Stack() { + elementData = new Object[MIN_INITIAL_CAPACITY]; + cursor = -1; + } + + public void push(Object o){ + ensureCapacity(size() + 1); + cursor++; + elementData[cursor] = o; + } + + private void ensureCapacity(int minCapacity){ + if(minCapacity <= elementData.length){ + return; + } + + int newSize = elementData.length << 1; + if (newSize < elementData.length){ + newSize = Integer.MAX_VALUE; + } + + Object[] newDataArray = new Object[newSize]; + System.arraycopy(elementData, 0, newDataArray, 0, size()); + elementData = newDataArray; + } + + + public Object pop(){ + Object ele = peek(); + cursor--; + return ele; + } + + public Object peek(){ + if (isEmpty()){ + throw new EmptyStackException(); + } + return elementData[cursor]; + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return cursor + 1; + } +} diff --git a/group05/578505552/src/test/java/JavaTest.java b/group05/578505552/src/test/java/JavaTest.java new file mode 100644 index 0000000000..26d2197ffe --- /dev/null +++ b/group05/578505552/src/test/java/JavaTest.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Queue; +import java.util.Stack; + +/** + * Created by songbao.yang on 2017/2/21. + */ +public class JavaTest { + + public static void main(String[] args) { + + ArrayList ss = new ArrayList(); + ss.add("a"); + ss.add("b"); + ss.add("c"); + ss.add("d"); + + System.out.println(ss.size()); + ss.remove(0); + System.out.println(ss.size()); + + + } +} diff --git a/group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java b/group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..87b9906c29 --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class ArrayListTest { + + private ArrayList list; + + public static final int SIZE = 10000; + + @Before + public void setUp() throws Exception { + + list = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void add() throws Exception { + + for (int i = 0; i < SIZE; i++) { + list.add(i); + Assert.assertEquals(i+1, list.size()); + } + } + + @Test + public void add1() throws Exception { + + add(); + for (int i = 0; i < 1000; i++) { + int oldSize = list.size(); + int randomIndex = (int)Math.floor(Math.random() * oldSize); + + list.add(randomIndex, randomIndex); + int newSize = list.size(); + + Assert.assertEquals(newSize, oldSize+1); + Assert.assertEquals(list.get(randomIndex), randomIndex); + } + } + + @Test + public void get() throws Exception { + + add(); + for (int i = 0; i < SIZE * 100; i++) { + int randomIndex = (int)Math.floor(Math.random() * list.size()); + if(randomIndex < 0 || randomIndex >= SIZE){ + System.out.println("illegal index: " + randomIndex); + throw new RuntimeException(); + } + int o = (Integer) list.get(randomIndex); + Assert.assertEquals(randomIndex, o); + } + } + + @Test + public void remove() throws Exception { + + add(); + for (int i = 0; i < SIZE; i++) { + System.out.println("remove: " + i); + list.remove(0); + } + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void iterator() throws Exception { + add(); + Iterator iterator1 = list.iterator(); + int i = 0; + while (iterator1.hasNext()){ + Object next = iterator1.next(); + Assert.assertEquals(i, next); + i++; + } + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/QueueTest.java b/group05/578505552/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..905054670d --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/24. + */ +public class QueueTest { + + private static final int SIZE = 2000; + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void enQueue() throws Exception { + for (int i = 0; i < SIZE; i++) { + queue.enQueue(i); + Assert.assertEquals(i+1, queue.size()); + } + } + + @Test + public void deQueue1() throws Exception { + enQueue(); + + int i = 0; + int startSize = queue.size(); + while (!queue.isEmpty()) { + Assert.assertEquals(startSize - i, queue.size()); + Object o = queue.deQueue(); + Assert.assertEquals(SIZE - i - 1, queue.size()); + Assert.assertEquals(i, o); + i++; + } + } + + @Test + public void deQueue2() throws Exception { + enQueue(); + int startSize = queue.size(); + + for (int i = 0; i < startSize; i++) { + queue.deQueue(); + Assert.assertEquals(startSize - 1, queue.size()); + queue.enQueue(i+1000); + Assert.assertEquals(startSize, queue.size()); + } + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/StackTest.java b/group05/578505552/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b65d446c97 --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,65 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/24. + * + */ +public class StackTest { + + private Stack stack; + + public static final int SIZE = 100; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void push() throws Exception { + for (int i = 0; i < SIZE; i++) { + stack.push(i); + Assert.assertEquals(i+1, stack.size()); + } + System.out.println(); + } + + @Test + public void pop() throws Exception { + push(); + int beginSize = stack.size(); + for (int i = 0; i < beginSize; i++) { + Object ele = stack.pop(); + Assert.assertEquals(beginSize-i-1, stack.size()); + Assert.assertEquals(beginSize-i-1, ele); + } + } + + @Test + public void peek() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + +} \ No newline at end of file From 929efcb1bddbb1a44b709c58caac751bb5a0724a Mon Sep 17 00:00:00 2001 From: "songbao.yang" Date: Mon, 27 Feb 2017 00:47:08 +0800 Subject: [PATCH 2/2] BinaryTreeNode --- .idea/coding2017.iml | 12 + .idea/compiler.xml | 32 + .idea/copyright/profiles_settings.xml | 3 + .idea/encodings.xml | 6 + .idea/libraries/Maven__junit_junit_4_12.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .idea/misc.xml | 88 ++ .idea/modules.xml | 9 + .idea/vcs.xml | 6 + .idea/workspace.xml | 1021 +++++++++++++++++ group05/578505552/578505552.iml | 2 +- .../main/java/com/coding/basic/ArrayList.java | 1 - .../java/com/coding/basic/BinaryTreeNode.java | 33 +- .../java/com/coding/basic/LinkedList.java | 78 +- 14 files changed, 1298 insertions(+), 19 deletions(-) create mode 100644 .idea/coding2017.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/coding2017.iml b/.idea/coding2017.iml new file mode 100644 index 0000000000..92999fd331 --- /dev/null +++ b/.idea/coding2017.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000..710011a27c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..c7d1c5a837 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000..6b31afc95b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000000..d3b71924f0 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000000..e5d35d8742 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..4c4ade3a7c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..739a5428bb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000..702d9b36d0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1021 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488124662643 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/578505552/578505552.iml b/group05/578505552/578505552.iml index faa2a57144..95e7551d7a 100644 --- a/group05/578505552/578505552.iml +++ b/group05/578505552/578505552.iml @@ -8,7 +8,7 @@ - + diff --git a/group05/578505552/src/main/java/com/coding/basic/ArrayList.java b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java index 1620048907..54d8f05f02 100644 --- a/group05/578505552/src/main/java/com/coding/basic/ArrayList.java +++ b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java @@ -97,7 +97,6 @@ public Object next() { if (hasNext()){ return elementData[cursor++]; } - throw new NoSuchElementException(); } } diff --git a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java index 0169aeb79a..0610a542b5 100644 --- a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -6,14 +6,14 @@ */ public class BinaryTreeNode { - private Object data; + private Integer data; private BinaryTreeNode left; private BinaryTreeNode right; public Object getData() { return data; } - public void setData(Object data) { + public void setData(Integer data) { this.data = data; } public BinaryTreeNode getLeft() { @@ -29,8 +29,29 @@ public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; - } - + public BinaryTreeNode insert(Integer o){ + + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.data = o; + newNode.left = null; + newNode.right = null; + + BinaryTreeNode cursor = this; + BinaryTreeNode pre = cursor; + while (cursor != null){ + pre = cursor; + if (o.compareTo(cursor.data) < 0){ + cursor = cursor.left; + } else { + cursor = cursor.right; + } + } + + if (o.compareTo(pre.data) < 0){ + pre.left = newNode; + } else { + pre.right = newNode; + } + return this; + } } diff --git a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java index c3eeb54950..58accde4d8 100644 --- a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java +++ b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.NoSuchElementException; + /** * Created by songbao.yang on 2017/2/21. * @@ -9,7 +11,7 @@ public class LinkedList implements List { private Node head; private int elementCount; - //head作为一个节点,其next的值指向List中的真正的第一个节点 + //head作为一个节点,其next的值指向List中真正的第一个节点 public LinkedList() { Node head = new Node(); head.next = null; @@ -69,6 +71,7 @@ public Object remove(int index){ } Node indexNode = cursor.next; cursor.next = indexNode.next; + elementCount--; return indexNode; } @@ -77,22 +80,75 @@ public int size(){ } public void addFirst(Object o){ - - } + Node node = new Node(); + node.data = o; + node.next = head.next; + head.next = node; + elementCount++; + } + public void addLast(Object o){ - - } + + Node cursor = head; + while (cursor.next != null){ + cursor = cursor.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + cursor.next = newNode; + elementCount++; + } + public Object removeFirst(){ - return null; + + if (size() == 0){ + throw new RuntimeException("no element in list"); + } + Node firstNode = head.next; + head.next = head.next.next; + elementCount--; + return firstNode; } + public Object removeLast(){ - return null; - } + if (size() == 0){ + throw new RuntimeException("no element in list"); + } + + Node cursor = head; + for (int i = 0; i < size() - 1; i++) { + cursor = cursor.next; + } + + Node lastNode = cursor.next; + cursor.next = null; + elementCount--; + + return lastNode; + } + public Iterator iterator(){ - return null; + return new Itr(); } - - + + private class Itr implements Iterator { + + private Node itrCursor = head; + + public boolean hasNext() { + + return itrCursor.next != null; + } + + public Object next() { + if (hasNext()){ + return itrCursor.next; + } + throw new NoSuchElementException(); + } + } + private static class Node{ Object data; Node next;