From 057c437ad67fcdedc616e7145c71815c9a81f11c Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 12 Mar 2017 19:44:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 167 +++++++++++++ .../coding2017/basic/LinkedList.java | 231 ++++++++++++++++++ .../com/johnChnia/coding2017/basic/Queue.java | 83 +++++++ .../com/johnChnia/coding2017/basic/Stack.java | 79 ++++++ .../coding2017/basic/test/ArrayListTest.java | 61 +++++ .../coding2017/basic/test/LinkedListTest.java | 92 +++++++ .../coding2017/basic/test/QueueTest.java | 56 +++++ .../coding2017/basic/test/StackTest.java | 69 ++++++ 8 files changed, 838 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..4881c6518c --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -0,0 +1,167 @@ +package com.johnChnia.coding2017.basic; + +import java.util.Arrays; + +/** + * Created by john on 2017/3/8. + * @// TODO: 2017/3/15 支持泛型 + */ + +public class ArrayList { + private int[] elementData; + private int size = 0; + + /** + * Constructs an list with the specified initial capacity. + * + * @param initialCapacity + * @throws IllegalArgumentException if the specified initial capacity + * is negative or zero + */ + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + elementData = new int[initialCapacity]; + } else { + throw new IllegalArgumentException("Illegal Capacity: " + + initialCapacity); + } + } + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position in this list + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public int get(int index) { + rangeCheck(index); + rangeCheckForAdd(index); + return elementData[index]; + } + + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list + */ + public void add(int element) { + ensureCapacityInternal(size + 1); + elementData[size++] = element; + } + + + /** + * Inserts the specified element at the specified position in this + * list. Shifts the element currently at that position (if any) and + * any subsequent elements to the right (adds one to their indices). + * + * @param element element to be inserted + * @param index index at which the specified element is to be inserted + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public void add(int element, int index) { + rangeCheckForAdd(index); + ensureCapacityInternal(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = element; + size++; + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent elements to the left (subtracts one from their + * indices). + * + * @param index the index of the element to be removed + * @return the element that was removed from the list + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public int remove(int index) { + rangeCheckForAdd(index); + int oldValue = elementData[index]; + int numMoved = size() - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, + numMoved); + } + elementData[--size] = 0; // let jc to clear + return oldValue; + } + + /** + * Returns true if this list contains no elements. + * + * @return true if this list contains no elements + */ + public boolean empty() { + return size == 0; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + + + /** + * Increases the capacity to ensure that it can hold at least the + * number of elements specified by the double length of list. + */ + private void grow() { + elementData = Arrays.copyOf(elementData, 2 * elementData.length); + } + + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("["); + for (int index = 0; index < size(); index++) { + stringBuilder.append(elementData[index]); + stringBuilder.append(", "); + } + stringBuilder.append("]"); + return stringBuilder.toString(); + } + + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity - elementData.length > 0) + grow(); + } + + /** + * A version of rangeCheck used by add and addAll. + */ + private void rangeCheckForAdd(int index) { + if (index > elementData.length - 1 || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + /** + * Constructs an IndexOutOfBoundsException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + elementData.length; + } + + /** + * Checks if the given index is in range. If not, throws an appropriate + * runtime exception. This method does *not* check if the index is + * negative: It is always used immediately prior to an array access, + * which throws an ArrayIndexOutOfBoundsException if index is negative. + */ + private void rangeCheck(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..f2825659b9 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java @@ -0,0 +1,231 @@ +package com.johnChnia.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * Created by john on 2017/3/9. + * + * @// TODO: 2017/3/15 支持泛型 + */ + +public class LinkedList { + + private Node first = null; + private int size = 0; + + /** + * Constructs an empty list. + */ + public LinkedList() { + + } + + private static class Node { + int element; + Node next; + Node prev; + } + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list + */ + public void add(int element) { + Node newNode = new Node(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + Node last = first; + while (last.next != null) + last = last.next; + last.next = newNode; + newNode.prev = last; + newNode.next = null; + newNode.element = element; + size++; + } + + private void addWhenListIsEmpty(Node newNode, int element) { + first = newNode; + first.element = element; + first.next = null; + first.prev = null; + size++; + } + + /** + * Inserts the specified element at the beginning of this list. + * + * @param element the element to add + */ + public void addFirst(int element) { + Node newNode = new Node(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + newNode.next = first; + newNode.prev = null; + newNode.element = element; + + first.prev = newNode; + first = newNode; + size++; + } + + + /** + * Inserts the specified element at the specified position in this list. + * Shifts the element currently at that position (if any) and any + * subsequent elements to the right (adds one to their indices). + * + * @param index index at which the specified element is to be inserted. + * @param element element to be inserted. + * @throws RuntimeException if list size less than 2. + */ + public void add(int index, int element) { + if (size() < 2) + throw new RuntimeException("list size should greater than or equal to 2"); + isElementIndex(index); + if (index == 0) { + addFirst(element); + return; + } else { + Node temp = new Node(); + Node temp2 = first; + for (int i = 0; i < index; i++) { + temp2 = temp2.next; + } + temp2.prev.next = temp; + temp.prev = temp2.prev; + + temp.next = temp2; + temp2.prev = temp; + temp.element = element; + } + size++; + + } + + + /** + * remove last element in the list. + * + * @throws RuntimeException if the list is empty. + */ + public void remove() { + if (size == 0) + throw new RuntimeException("linkList size should greater than or equal to 1"); + Node next = first.next; + if (next == null) { + first = null; + } else { + Node last = first; + while (last.next != null) + last = last.next; + last.prev.next = null; + last = null; // help GC + } + size--; + } + + + /** + * Removes and returns the first element from this list. + * + * @return the first element from this list + */ + public int removeFirst() { + Node f = first; + if (f == null) + throw new NoSuchElementException(); + int element = f.element; + Node next = first.next; + first.element = 0; + first.next = null; // help GC + + first = next; + if (next != null) { + next.prev = null; + } + size--; + return element; + } + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position in this list + */ + public int get(int index) { + checkElementIndex(index); + Node node = first; + if (index == 0) { + return first.element; + } + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.element; + } + + /** + * Returns the first element in this list. + * + * @return the first element in this list + * @throws NoSuchElementException if this list is empty + */ + public int getFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.element; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + + /** + * Tells if the argument is the index of an existing element. + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(first.element); + Node temp = first; + while (temp.next != null) { + temp = temp.next; + sb.append("→"); + sb.append(temp.element); + } + return sb.toString(); + } + + /** + * Constructs an IndexOutOfBoundsException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java new file mode 100644 index 0000000000..4fd2558ea3 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java @@ -0,0 +1,83 @@ +package com.johnChnia.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * Created by john on 2017/3/10. + * @// TODO: 2017/3/15 支持泛型 + */ +public class Queue { + + private ArrayList arrayList; + + /** + * Constructs an queue using 10 capacity of ArrayList. + */ + public Queue() { + arrayList = new ArrayList(10); + } + + + /** + * Inserts the specified element into this queue.returning + * {@code true} upon success. + * if no space is currently available. + * + * @param element the element to add + * @return {@code true} + */ + public boolean add(int element) { + arrayList.add(element); + return true; + } + + /** + * Retrieves and removes the head of this queue,throws an exception + * if this queue is empty. + * + * @return the head of this queue + * @throws NoSuchElementException if this queue is empty + */ + public int remove() { + if (arrayList.empty()) + throw new NoSuchElementException(emptyMsg()); + return arrayList.remove(0); + + } + + + /** + * Retrieves, but does not remove, the head of this queue, + * or returns {@code null} if this queue is empty. + * + * @return the head of this queue, or {@code 0} if this queue is empty + */ + public int peek() { + if (arrayList.empty()) + return 0; + return arrayList.get(0); + } + + + public String toString() { + return arrayList.toString(); + } + + /** + * Returns the number of elements in this queue. + * + * @return the number of elements in this queue. + */ + public int size() { + return arrayList.size(); + } + + /** + * Constructs an NoSuchElementException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String emptyMsg() { + return "Size: " + size(); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java new file mode 100644 index 0000000000..e11be853f9 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java @@ -0,0 +1,79 @@ +package com.johnChnia.coding2017.basic; + +import java.util.EmptyStackException; + +/** + * Created by john on 2017/3/10. + * @// TODO: 2017/3/15 支持泛型 + */ +public class Stack { + private LinkedList linkList; + + /** + * Creates an empty Stack. + */ + public Stack() { + linkList = new LinkedList(); + } + + + /** + * Pushes an item onto the top of this stack. + * + * @param element the element to be pushed onto this stack. + */ + public void push(int element) { + linkList.addFirst(element); + } + + /** + * Removes the object at the top of this stack and returns that + * object as the value of this function. + * + * @return The object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public int pop() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.removeFirst(); + } + + /** + * Looks at the object at the top of this stack without removing it + * from the stack. + * + * @return the object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public int peek() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.getFirst(); + } + + /** + * Tests if this stack is empty. + * + * @return true if and only if this stack contains + * no elements; false otherwise. + */ + public boolean empty() { + return linkList.size() == 0; + } + + public String toString() { + return linkList.toString(); + } + + /** + * Returns the number of elements in this stack. + * + * @return the number of elements in this stack + */ + public int size() { + return linkList.size(); + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..13c81724ad --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,61 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.ArrayList; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/8. + */ + +public class ArrayListTest { + private ArrayList arrayList1; + private ArrayList arrayList2; + private ArrayList arrayList3; + private ArrayList arrayList4; + + @Before + public void setUp() throws Exception { + arrayList1 = new ArrayList(3); + arrayList2 = new ArrayList(3); + arrayList3 = new ArrayList(3); + arrayList4 = new ArrayList(3); + } + + @Test + public void testAddAndGet() { + arrayList1.add(99); + assertThat(arrayList1.get(0), equalTo(99)); + } + + @Test + public void testGrow() { + for (int i = 0; i < 6; i++) { + arrayList2.add(10); + } + assertThat(arrayList2.size(), equalTo(6)); + } + + @Test + public void testAddElementByIndex() { + for (int i = 0; i < 3; i++) { + arrayList3.add(10); + } + arrayList3.add(1000, 1); + assertThat(arrayList3.get(1), equalTo(1000)); + } + + @Test + public void testRemoveElementByIndex() { + for (int i = 0; i < 6; i++) { + arrayList4.add(i); + } + int removed = arrayList4.remove(4); + System.out.println(arrayList4); + assertThat(removed, equalTo(4)); + assertThat(arrayList4.size(), equalTo(5)); + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..bd8ad5ac4c --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,92 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.LinkedList; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/9. + */ +public class LinkedListTest { + + private LinkedList linkList1; + private LinkedList linkList2; + private LinkedList linkList3; + private LinkedList linkList4; + private LinkedList linkList5; + private LinkedList linkList6; + + @Before + public void setUp() throws Exception { + linkList1 = new LinkedList(); + linkList2 = new LinkedList(); + linkList3 = new LinkedList(); + linkList4 = new LinkedList(); + linkList5 = new LinkedList(); + linkList6 = new LinkedList(); + } + + @Test + public void testAddAndGet() { + for (int i = 0; i < 4; i++) { + linkList1.add(i); + } + assertThat(linkList1.get(0), equalTo(0)); + assertThat(linkList1.get(1), equalTo(1)); + assertThat(linkList1.get(2), equalTo(2)); + } + + @Test + public void testSize() { + for (int i = 0; i < 4; i++) { + linkList3.add(i); + } + assertThat(linkList3.size(), equalTo(4)); + } + + @Test + public void testAddFirst() { + for (int i = 0; i < 4; i++) { + linkList2.addFirst(i); + } + assertThat(linkList2.get(0), equalTo(3)); + assertThat(linkList2.get(1), equalTo(2)); + } + + @Test + public void testRemove() { + for (int i = 0; i < 2; i++) { + linkList4.addFirst(i); + } + linkList4.remove(); + linkList4.remove(); + assertThat(linkList4.size(), equalTo(0)); + } + + + @Test + public void testAddByIndex() { + for (int i = 0; i < 2; i++) { + linkList5.add(i); + } + linkList5.add(0, 100); + linkList5.add(1, 1000); + System.out.println(linkList5); + assertThat(linkList5.get(1), equalTo(1000)); + } + + @Test + public void testRemoveFirst() { + for (int i = 0; i < 4; i++) { + linkList6.addFirst(i); + } + linkList6.removeFirst(); + linkList6.removeFirst(); + linkList6.removeFirst(); + assertThat(linkList6.get(0), equalTo(0)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..54cd7f9385 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java @@ -0,0 +1,56 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.Queue; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/11. + */ +public class QueueTest { + Queue queue1; + Queue queue2; + Queue queue3; + + @Before + public void setUp() throws Exception { + queue1 = new Queue(); + queue2 = new Queue(); + queue3 = new Queue(); + + } + + @Test + public void testAdd() throws Exception { + for (int i = 0; i < 3; i++) { + queue1.add(i); + } + System.out.println(queue1); + assertThat(queue1.peek(), equalTo(0)); + + } + + @Test + public void testRemove() throws Exception { + for (int i = 0; i < 3; i++) { + queue2.add(i); + } + assertThat(queue2.remove(), equalTo(0)); + assertThat(queue2.remove(), equalTo(1)); + assertThat(queue2.remove(), equalTo(2)); + assertThat(queue2.size(), equalTo(0)); + } + + @Test + public void testPeek() throws Exception { + for (int i = 0; i < 3; i++) { + queue3.add(i); + } + assertThat(queue3.peek(), equalTo(0)); + assertThat(queue3.size(), equalTo(3)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..7fb4a35757 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java @@ -0,0 +1,69 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.Stack; +import org.junit.Before; +import org.junit.Test; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/10. + */ +public class StackTest { + Stack stack1; + Stack stack2; + Stack stack3; + Stack stack4; + + @Before + public void setUp() throws Exception { + stack1 = new Stack(); + stack2 = new Stack(); + stack3 = new Stack(); + stack4 = new Stack(); + } + + @Test + public void testPush() throws Exception { + for (int i = 0; i < 6; i++) { + stack1.push(i); + } + assertThat(stack1.peek(), equalTo(5)); + + } + + @Test + public void testPop() throws Exception { + for (int i = 0; i < 6; i++) { + stack2.push(i); + } + assertThat(stack2.pop(), equalTo(5)); + assertThat(stack2.size(), equalTo(5)); + + } + + @Test + public void testPeek() throws Exception { + for (int i = 0; i < 6; i++) { + stack3.push(i); + } + assertThat(stack3.peek(), equalTo(5)); + assertThat(stack3.size(), equalTo(6)); + + } + + @Test() + public void testEmpty() throws Exception { + for (int i = 0; i < 2; i++) { + stack4.push(i); + } + assertFalse(stack4.empty()); + stack4.pop(); + stack4.pop(); + assertTrue(stack4.empty()); + } + +} \ No newline at end of file