From 4b19a0be08cb09df0b322fcb6393047d8cf2cdd2 Mon Sep 17 00:00:00 2001 From: tennysons Date: Fri, 24 Feb 2017 21:35:00 +0800 Subject: [PATCH] Tennyson's task --- .../296933284/DataStructuresTest/.classpath | 7 + .../296933284/DataStructuresTest/.gitignore | 14 ++ group14/296933284/DataStructuresTest/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/coding/basic/ArrayList.java | 147 ++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 77 +++++++ .../src/com/coding/basic/Iterator.java | 6 + .../src/com/coding/basic/JavaTest.java | 185 +++++++++++++++++ .../src/com/coding/basic/LinkedList.java | 188 ++++++++++++++++++ .../src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 50 +++++ .../src/com/coding/basic/Stack.java | 61 ++++++ 12 files changed, 772 insertions(+) create mode 100644 group14/296933284/DataStructuresTest/.classpath create mode 100644 group14/296933284/DataStructuresTest/.gitignore create mode 100644 group14/296933284/DataStructuresTest/.project create mode 100644 group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/List.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java create mode 100644 group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java diff --git a/group14/296933284/DataStructuresTest/.classpath b/group14/296933284/DataStructuresTest/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group14/296933284/DataStructuresTest/.gitignore b/group14/296933284/DataStructuresTest/.gitignore new file mode 100644 index 0000000000..e32d56a40c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.gitignore @@ -0,0 +1,14 @@ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ diff --git a/group14/296933284/DataStructuresTest/.project b/group14/296933284/DataStructuresTest/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs b/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..116020466d --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java @@ -0,0 +1,147 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList 实现 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class ArrayList implements List { + + private int size; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initCapacity) { + elementData = new Object[initCapacity]; + } + + /** + * 在数组末尾添加指定元素,若数组已满,则自动扩展为原来长度的两倍 + */ + public void add(Object obj) { + + ensureCapacityInternal(size); + + elementData[size] = obj; + size++; + } + + + /** + * 在数组的指定位置插入元素 + */ + public void add(int index, Object obj) { + + rangCheckForAdd(index); + ensureCapacityInternal(size + 1); + + for (int i = size - 1; i >= index; i--) + elementData[i + 1] = elementData[i]; + + elementData[index] = obj; + size++; + } + + /** + * 给数组扩容 + */ + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity - elementData.length > 0) { + int newCapacity = elementData.length * 2; + elementData = Arrays.copyOf(elementData, newCapacity); + // elementData = tempElementData; + } + } + + /** + * 用于在 add() 中检查数组下表是否越界 + */ + private void rangCheckForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } + + /** + * 返回指定索引位置的元素值 + */ + public Object get(int index) { + + rangCheck(index); + + return elementData[index]; + } + + /** + * 删除指定索引位置的元素,并返回该值 + */ + public Object remove(int index) { + rangCheck(index); + + Object obj = elementData[index]; + + for (int i = index; i < size; i++) + elementData[i] = elementData[i + 1]; + + size--; + + return obj; + } + + /** + * 检查数组下表是否越界 + * + * @param index + */ + private void rangCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + + /** + * 返回数组长度 + */ + public int size() { + return size; + } + + /** + * 迭代器 + * + * @return + */ + public Iterator iterator() { + return new Iter(); + } + + //迭代器内部类 + private class Iter implements Iterator { + int current; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + + int i = current; + rangCheck(i); + current++; + + return elementData[i]; + } + + } + +} + + diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d01667aaa8 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable { + + private Object data; + private BinaryTreeNode leftChild; + private BinaryTreeNode rightChild; + + public BinaryTreeNode() { + super(); + this.data = null; + this.leftChild = null; + this.rightChild = null; + } + + public BinaryTreeNode(Object data) { + super(); + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinaryTreeNode leftChild) { + this.leftChild = leftChild; + } + + public BinaryTreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(BinaryTreeNode rightChild) { + this.rightChild = rightChild; + } + + /** + * 向树中插入节点 + * + * @param i + */ + public void insert(int i) { + + } + + // ... + @Override + public int compareTo(Object obj) { + + return 0; + } + + +} + + + + + + + + + + + + + + + diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java new file mode 100644 index 0000000000..bc0878cb50 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java @@ -0,0 +1,185 @@ +package com.coding.basic; + +import org.junit.Test; + +public class JavaTest { + + @Test + public void binaryTreeNodeTest() { +// BinaryTreeNode tree = new BinaryTreeNode(5); +// +// System.out.println(tree.getData()); + } + + + @Test + public void queueTest() { + Queue queue = new Queue(); + + // enQueue() + for (int i = 0; i < 10; i++) { + queue.enQueue("hello: " + i); + } + + // size() + System.out.println(queue.size()); // 10 + // isEmpty + System.out.println(queue.isEmpty()); + + // deQueue() + for (int i = 0; i < 10; i++) { + System.out.println(queue.deQueue()); + } + + // size() + System.out.println(queue.size()); // 0 + + // isEmpty + System.out.println(queue.isEmpty()); + } + + + @Test + public void stackTest() { + Stack stack = new Stack(); + + // push() + for (int i = 0; i < 10; i++) { + stack.push("hello: " + i); + } + + // size() + System.out.println(stack.size()); + + // pop() + for (int i = 0; i < 10; i++) { + System.out.println(stack.pop()); + } + + // isEmpty() + System.out.println(stack.isEmpty()); + System.out.println(stack.size()); + + stack.push("hello world 1"); + stack.push("hello world 2"); + stack.push("hello world 3"); + stack.push("hello world 4"); + + // peek() + System.out.println(stack.peek()); + + // isEmpty() + System.out.println(stack.isEmpty()); + } + + @Test + public void linkedListTest() { + LinkedList linkedList = new LinkedList(); + + // add(obj) + for (int i = 0; i < 10; i++) { + linkedList.add("hello: " + i); + } + + // size() + System.out.println(linkedList.size()); // 10 + + // get(index) + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + +// System.out.println("---->" + linkedList.get(10)); // java.lang.NullPointerException + + // remove() + System.out.println("---->" + linkedList.remove(9)); // 9 + System.out.println("---->" + linkedList.remove(5)); // 5 + System.out.println(linkedList.size()); // 8 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + + // removeFirst() + System.out.println("---->" + linkedList.removeFirst()); // 0 + // removeLast + System.out.println("---->" + linkedList.removeLast()); // 8 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("-->" + linkedList.get(i)); + } + + // add(index, obj) + linkedList.add(4, "^_^"); + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("&&&&" + linkedList.get(i)); + } + // addFirst() + linkedList.addFirst("***first***"); + // addLast() + linkedList.addLast("---last---"); + + System.out.println(linkedList.get(0)); // ***first*** + System.out.println(linkedList.get(linkedList.size() - 1)); // ---last--- + System.err.println(linkedList.size()); // 9 + + for (int i = 0; i < linkedList.size(); i++) { + System.out.println("&&&&" + linkedList.get(i)); + } + + System.out.println("-------------------------------"); + // iterator + Iterator iter = linkedList.iterator(); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + + System.out.println("-------------------------------"); + } + + @Test + public void arrayListTest() { + ArrayList arrayList = new ArrayList(); + + // add(obj) + for (int i = 0; i < 10; i++) { + arrayList.add("hello: " + i); + } + + // get(index) + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("-->" + arrayList.get(i)); + } + + // add(index, obj) + arrayList.add(5, "Tennyson"); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("++>" + arrayList.get(i)); + } + + // size() + System.out.println("size: " + arrayList.size()); + System.out.println("index 5: " + arrayList.get(5)); + + // remove() + Object value = arrayList.remove(5); + System.out.println("index 5: " + value); + System.out.println("size: " + arrayList.size()); + System.out.println("index5: " + arrayList.get(5)); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("index " + i + " : " + arrayList.get(i)); + } + + System.out.println("-------------------------------"); + // iterator + Iterator iter = arrayList.iterator(); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + + System.out.println("-------------------------------"); + + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..72b4ba8a90 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java @@ -0,0 +1,188 @@ +package com.coding.basic; + +/** + * LinkedList 实现 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + super(); + this.head = new Node(); + this.size = 0; + } + + /** + * 尾插法在链表末尾插入节点 + */ + public void add(Object obj) { + + if (head.data == null) { + head.data = obj; + head.next = null; + } else { + + Node r = head; + while (r.next != null) + r = r.next; + + Node node = new Node(); + node.data = obj; + node.next = null; + + r.next = node; + } + + size++; + + } + + /** + * 在指定索引位置插入节点 + */ + public void add(int index, Object obj) { + + if (index > size) + throw new IndexOutOfBoundsException(); + + Node r = head; + for (int i = 0; i < index - 1; i++) + r = r.next; + + Node node = new Node(); + node.data = obj; + node.next = null; + node.next = r.next; + r.next = node; + size++; + } + + /** + * 返回指定位置的值 + */ + public Object get(int index) { + if (index > size) + throw new IndexOutOfBoundsException(); + + Node r = head; + for (int i = 0; i < index; i++) + r = r.next; + + return r.data; + } + + /** + * 删除指定位置节点并返回其值 + */ + public Object remove(int index) { + if (index > size) + throw new IndexOutOfBoundsException(); + + Node node = new Node(); + + Node r = head; + for (int i = 0; i < index - 1; i++) + r = r.next; + + node = r.next; + r.next = node.next; + node.next = null; + size--; + return node.data; + } + + /** + * 返回链表的长度 + */ + public int size() { + return size; + } + + /** + * 在链表的起始位置插入节点 + * + * @param obj + */ + public void addFirst(Object obj) { + Node node = new Node(); + node.data = obj; + node.next = head; + head = node; + size++; + } + + /** + * 在链表尾部插入节点 + * + * @param obj + */ + public void addLast(Object obj) { + add(obj); + } + + /** + * 删除链表的第一个节点 + * + * @return 所删除节点的值 + */ + public Object removeFirst() { + Node node = new Node(); + node = head; + head = head.next; + size--; + + return node.data; + } + + /** + * 删除链表的最后一个节点 + * + * @return 所删除节点的值 + */ + public Object removeLast() { + return remove(size - 1); + } + + /** + * 迭代器 + * + * @return 返回一个迭代器对象 + */ + public Iterator iterator() { + return new Iter(); + } + + // 迭代器内部类 + private class Iter implements Iterator { + int current; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + int i = current; + + if (i >= size) + throw new IndexOutOfBoundsException(); + + current++; + + return get(i); + } + + } + + // 节点类定义 + private static class Node { + Object data; + Node next; + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java new file mode 100644 index 0000000000..969e6dd82b --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object obj); + public void add(int index, Object obj); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9257eb04ca --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +/** + * Queue 实现 + * First In First Out + * 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * 向队列中插入元素 + * + * @param obj + */ + public void enQueue(Object obj){ + elementData.addLast(obj); + } + + /** + * 删除队首元素 + * + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * 判断队列是否为空 + * + * @return + */ + public boolean isEmpty(){ + return elementData.size() == 0; + } + + /** + * 返回队列的元素个数 + * + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e28a9e3760 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +/** + * Stack 实现 + * Last In First Out + * 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + private int top = 0; + + /** + * 向栈中插入元素 + * + * @param obj + */ + public void push(Object obj) { + elementData.add(obj); + top++; + } + + /** + * 从栈中取出元素 + * + * @return + */ + public Object pop() { + return elementData.remove(--top); + } + + /** + * 获取栈顶元素 + * + * @return + */ + public Object peek() { + return elementData.get(top - 1); + } + + /** + * 判断栈是否为空 + * + * @return + */ + public boolean isEmpty() { + return top == 0; + } + + /** + * 获取栈中元素个数 + * + * @return + */ + public int size() { + return top; + } +}