diff --git a/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..023a1686ae --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +//import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; // 记录数组当前长度 + + private Object[] elementData = new Object[10]; // 初始长度 + + /* + * (non-Javadoc) + * + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + this.elementData[size++] = o; + } + + /* + * 在指定下标位置插入元素 (non-Javadoc) + * + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + // 1、先要判断index所在处有无值,没有则返回null + Object o = elementData[index]; + if (null == o) + throw new IndexOutOfBoundsException(); + return o; + } + + public Object remove(int index) { + Object oldVal = elementData[index]; // 保留要删除的元素 + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved);// 讲移除位置之后的元素向前 挪动 + } + elementData[--size] = null; // 将数组末尾元素置为空 + return oldVal; + } + + /** + * 获取数组元素个数 + */ + public int size() { + return this.size; + } + + public Object[] grow(Object[] src, int size) { + // Arrays.copyOf(src, src.length+size); + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public Iterator iterator() { + + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor=0; + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + return elementData[cursor++]; + } + + } + +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..2bc37a07e6 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,132 @@ +package com.coding.basic; + +/** + * 二叉树 + * + * @author cm + */ +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o); + if (null == root) { + root = new BinaryTreeNode(o); + } else { + boolean flag = false; + BinaryTreeNode cursorNode = root; + while (!flag) { + if (binaryTreeNode.compareTo(cursorNode) < 0) { + if (cursorNode.getLeft() == null) { + cursorNode.setLeft(binaryTreeNode); + flag = true; + } else { + cursorNode = cursorNode.getLeft(); + } + } else { + if (cursorNode.getRight() == null) { + cursorNode.setRight(binaryTreeNode); + flag = true; + } else { + cursorNode = cursorNode.getRight(); + } + } + } + } + return binaryTreeNode; + } + + public LinkedList inOrder() { + LinkedList linkedList = new LinkedList(); + sortLeft(linkedList, root); + sortRight(linkedList, root); + return linkedList; + } + + private void sortRight(LinkedList linkedList, BinaryTreeNode binaryTreeNode) { + Queue queue = getRightList(binaryTreeNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList, queueNode); + } + } + + private void sortLeft(LinkedList linkedList, BinaryTreeNode binaryTreeNode) { + Stack stack = getLeftList(binaryTreeNode); + while (!stack.isEmpty()) { + BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop(); + linkedList.add(stackNode.getData()); + Queue queue = getRightList(stackNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList, queueNode); + } + } + linkedList.add(binaryTreeNode.getData()); + } + + private Stack getLeftList(BinaryTreeNode binaryTreeNode) { + Stack stack = new Stack(); + while (binaryTreeNode.getLeft() != null) { + binaryTreeNode = binaryTreeNode.getLeft(); + stack.push(binaryTreeNode); + } + return stack; + } + + private Queue getRightList(BinaryTreeNode binaryTreeNode) { + Queue queue = new Queue(); + while (binaryTreeNode.getRight() != null) { + binaryTreeNode = binaryTreeNode.getRight(); + queue.enQueue(binaryTreeNode); + } + return queue; + } + + private class BinaryTreeNode implements Comparable { + 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(Object o) { + setData(o); + } + + @Override + public int compareTo(BinaryTreeNode binaryTreeNode) { + Integer currVal = (Integer) root.getData(); + Integer compVal = (Integer) binaryTreeNode.getData(); + if (currVal < compVal) + return -1; + else if (currVal == compVal) + return 0; + else + return 1; + } + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java b/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..b31c724a2f --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +/** + * 单向链表 + * + * @author Administrator + * + */ +public class LinkedList implements List { + + private Node head = new Node(null, null); + private int size = 0; + + public LinkedList() { + head.next = head; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + // 1、检查是否在合理范围内 + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node currNode = findNodeByIndex(index); + Node newNode = new Node(o, currNode); + if (index == 0) { // 直接插入到第一个位置 + head = newNode; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = newNode; + } + size++; + } + + public Object get(int index) { + return findNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node targetNode = this.findNodeByIndex(index); + Object obj = targetNode.data; + if (index == 0) { + targetNode.data = null; + head = targetNode.next; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = targetNode.next; + } + // targetNode.data = null; + size--; + return obj; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node nextNode = head; + Node newNode = new Node(o, nextNode); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node subNode = new Node(o, null); + if (size == 0) { + head = subNode; + } else { + Node lastNode = findNodeByIndex(size - 1); + lastNode.next = subNode; + } + size++; + } + + public Object removeFirst() { + return this.remove(0); + } + + public Object removeLast() { + return this.remove(size - 1); + } + + private Node findNodeByIndex(int index) { + Node lastNode = head; + for (int i = 0; i < index; i++) { + lastNode = lastNode.next; + } + return lastNode; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private int cursor = 0; + private Node cursorNode = head; + + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + Object obj = cursorNode.data; + cursorNode = cursorNode.next; + cursor++; + return obj; + } + + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/List.java b/group03/345943980/2017Learning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +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/group03/345943980/2017Learning/src/com/coding/basic/Queue.java b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6abba1993b --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +/** + * 队列(堆):特点,先进先出 + * + * @author Administrator + * + */ +public class Queue { + + private LinkedList linkedList = new LinkedList(); + private int size = 0; + + /** + * 入队列 + * + * @param o + */ + public void enQueue(Object obj) { + linkedList.add(obj); + size++; + } + + /** + * 出队列 + * + * @return + */ + public Object deQueue() { + Object obj = linkedList.remove(0); + size--; + return obj; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Stack.java b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1dd8b15f59 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈(堆栈),特点先进后出的特点 + * + * @author Administrator + * + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + /** + * 在堆栈顶部中添加一个元素 + * + * @param o + */ + public void push(Object o) { + elementData.add(o); + size++; + } + + /** + * 在堆栈顶部移去一个元素 + * + * @return + */ + public Object pop() { + Object o = elementData.remove(size - 1); + size--; + return o; + + } + + /** + * 总是返回栈顶的元素 + * + * @return + */ + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..8d812b9f71 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,48 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(100); + arrayList.add(0, 1000); + System.out.println(arrayList.size()); + for(int i =0;i array = new java.util.ArrayList(); + array.add(1); + array.add(1, 20); + System.out.println(array.size()); + for(Object o:array){ + System.out.println(o.toString()); + } + //System.out.println(array.get(100)); + } + + +} diff --git a/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java new file mode 100644 index 0000000000..68962223c7 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java @@ -0,0 +1,25 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.LinkedList; + +public class BinaryTreeTest { + + @Test + public void test01(){ + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(2); + binaryTree.insert(7); + binaryTree.insert(1); + binaryTree.insert(4); + binaryTree.insert(6); + binaryTree.insert(8); + LinkedList linkedList=binaryTree.inOrder(); + for(int i=0;i