diff --git a/group03/569045298/pom.xml b/group03/569045298/pom.xml new file mode 100644 index 0000000000..338f3870aa --- /dev/null +++ b/group03/569045298/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.ztc + JavaLevelUp + war + 1.0-SNAPSHOT + JavaLevelUp Maven Webapp + http://maven.apache.org + + + + junit + junit + 3.8.1 + test + + + org.junit.jupiter + junit-jupiter-api + RELEASE + + + junit + junit + 4.12 + + + + + JavaLevelUp + + + diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java new file mode 100644 index 0000000000..dee00342d2 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 10; + private int size = 0; + private Object[] elementData = null; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) { + throw new RuntimeException("initialCapacity is smaller than zero"); + } + elementData = new Object[initialCapacity]; + } + + @Override + public void add(Object o) { + checkCapacity(size + 1); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + checkCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObject = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, elementData.length - index - 1); + elementData[--size] = null; + return removedObject; + } + + @Override + public int size() { + return size; + } + + private void checkRange(int index) { + if (index < 0 || index > elementData.length) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkCapacity(int size) { + if (size > elementData.length) { + int newLength = elementData.length * 2; + Object[] newObject = new Object[newLength]; + System.arraycopy(elementData, 0, newObject, 0, elementData.length); + elementData = newObject; + } + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + ArrayList arrayList = null; + int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + // TODO + pos++; + if (pos > size) { + return false; + } + return true; + } + + @Override + public Object next() { + // TODO + return elementData[pos]; + } + + @Override + public Object remove() { + // TODO + return null; + } + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java new file mode 100644 index 0000000000..b69f8932ed --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +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 object) { + return null; + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java new file mode 100644 index 0000000000..8bf2ae7ed5 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public interface Iterator { + + boolean hasNext(); + + Object next(); + + Object remove(); +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java new file mode 100644 index 0000000000..8814ea82e8 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java @@ -0,0 +1,167 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size = 0; + + public LinkedList() { + + } + + @Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + tail = head; + size++; + } else { + // 尾插法 + Node newNode = new Node(object); + tail.next = newNode; + tail = newNode; + tail.next = null; + size++; + } + } + + @Override + public void add(int index, Object object) { + checkRange(index); + if (null == head) { + add(object); + return; + } + if (index == 0) { + addFirst(object); + return; + } + Node pre = node(index - 1); + Node newNode = new Node(object); + newNode.next = pre.next; + pre.next = newNode; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + checkNodeNotNull(); + Node node = node(index); + return node.data; + } + + @Override + public Object remove(int index) { + checkRange(index); + checkNodeNotNull(); + if (index == 0) { + removeFirst(); + return head; + } + Node pre = node(index - 1); + pre.next = pre.next.next; + size--; + return head; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + size++; + } else { + Node firstNode = new Node(object); + firstNode.next = head; + head = firstNode; + size++; + } + } + + public Object removeFirst() { + checkNodeNotNull(); + head = head.next; + size--; + return head; + } + + public Object removeLast() { + checkNodeNotNull(); + if (size == 1) { + head = null; + } + Node pre = node(size() - 2); + pre.next = null; + size--; + return head; + } + + private void checkRange(int index) { + if (index > size - 1 || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkNodeNotNull() { + if (null == head) { + throw new NullPointerException(); + } + } + + private Node node(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node { + Node next; + private Object data; + + public Node() { + + } + + public Node(Object data) { + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Node(Object data, Node next, Node prev) { + this.data = data; + this.next = next; + } + } + + /*@Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + } else { + // 头插法 + Node nextNode = new Node(object); + nextNode.next = head.next; + head.next = nextNode; + } + }*/ + +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/List.java b/group03/569045298/src/main/com/coding/basic/datastructure/List.java new file mode 100644 index 0000000000..4d9292f156 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/List.java @@ -0,0 +1,17 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public interface List { + + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java new file mode 100644 index 0000000000..d3d4ebfbab --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class Queue { + + private ArrayList elementData; + + private int size; + + public Queue() { + elementData = new ArrayList(); + } + + public void enQueue(Object object) { + elementData.add(object); + size++; + } + + public Object deQueue() { + checkIsEmpty(); + Object object = elementData.get(0); + elementData.remove(0); + size--; + return object; + } + + private void checkIsEmpty() { + if (elementData.size() == 0) { + throw new RuntimeException("queue is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java new file mode 100644 index 0000000000..c8dbc6b3af --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java @@ -0,0 +1,47 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class Stack { + + private ArrayList elementData = null; + + private int size = 0; + + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object object) { + elementData.add(object); + size++; + } + + public Object pop() { + checkIsEmpty(); + Object peekObject = peek(); + elementData.remove(size - 1); + size--; + return peekObject; + } + + public Object peek() { + checkIsEmpty(); + return elementData.get(size - 1); + } + + private void checkIsEmpty() { + if (isEmpty()) { + throw new RuntimeException("stack is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java new file mode 100644 index 0000000000..9f90242595 --- /dev/null +++ b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java @@ -0,0 +1,66 @@ +package com.coding.basic.datastructure; + +import org.junit.Test; + +/** + * Created by zt on 2017/2/19. + */ +public class TestDataStructure { + + @Test + public void testLinedList() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 5; i++) { + list.add(i); + } + list.add(0, -1); + list.remove(1); + list.removeLast(); + list.addFirst(999); + list.removeFirst(); + System.out.println("list size : " + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + java.util.LinkedList list1 = new java.util.LinkedList(); + list1.add(0, 2); + System.out.print(list1.get(0)); + } + + @Test + public void testStack() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.pop(); + System.out.println(stack.size()); + Object obj = stack.peek(); + } + + @Test + public void testQueue() { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + Object object = queue.deQueue(); + System.out.println("dqueue object : " + object); + System.out.println(queue.isEmpty()); + System.out.println(queue.size()); + } + + @Test + public void testArrayList() { + List arrayList = new ArrayList(); + for (int i = 0; i < 30; i++) { + arrayList.add(i); + } + arrayList.add(0, -2); + arrayList.add(1, -1); + System.out.println(arrayList.remove(1)); + System.out.println("ArrayList size : " + arrayList.size()); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println(arrayList.get(i)); + } + } +}