diff --git a/group15/1521_653895972/src/com/coding/basic/ArrayList.java b/group15/1521_653895972/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2b692ab814 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/ArrayList.java @@ -0,0 +1,150 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by wanc on 2017/2/21. + * 实现ArrayList + */ +public class ArrayList implements List { + /** + * 实例化空数组 不用每次都new + */ + private static final Object[] Empty_elementData = {}; + /** + * 计数 + */ + private int size = 0; + /** + * 数据存放 + */ + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.elementData = Empty_elementData; + } + + /** + * 检查是否越界 + */ + private void checkLenght(int index) { + if (index - size > 0) + throw new IndexOutOfBoundsException(); + } + + /** + * 增加数组容量 + */ + private void kuorong() { + elementData = Arrays.copyOf(elementData, size + 1); + } + + /** + * 添加数据 + * + * @param o + */ + public void add(Object o) { + //扩容 + kuorong(); + //添加数据 + elementData[size++] = o; + } + + /** + * 在指定索引添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + //扩容 + kuorong(); + //移动数据 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + //添加数据 + elementData[index] = o; + size++; + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + //检查是否越界 + checkLenght(index); + return elementData[index]; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + //检查是否越界 + checkLenght(index); + Object element = elementData[index]; + //计算移除该元素后,要前移的个数 + int movesize = size - index - 1; + //移动数据 + System.arraycopy(elementData, index + 1, elementData, index, movesize); + //删除末尾元素 + elementData[--size] = null; + return element; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + /** + * 获取迭代器 + * + * @return + */ + public Iterator iterator() { + return new ArrayItr(); + } + + //迭代器实现类部类 + private class ArrayItr implements Iterator { + int cursor;//游标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > size) throw new NoSuchElementException(); + Object[] newElementData = ArrayList.this.elementData; + if (i > newElementData.length) throw new IndexOutOfBoundsException(); + cursor = i + 1; + return newElementData[i]; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + Object[] s = Arrays.copyOf(elementData, size); + return Arrays.toString(s); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BasicTest.java b/group15/1521_653895972/src/com/coding/basic/BasicTest.java new file mode 100644 index 0000000000..a181087104 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BasicTest.java @@ -0,0 +1,184 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by wanc on 2017/2/21. + */ +public class BasicTest { + + @Test + public void test() { + //测试 + testArrayList(); + testLinkedList(); + testBinaryTreeNode(); + testStack(); + testQueue(); + } + + + public void testQueue(){ + Queue queue = new Queue(); + queue.enQueue("S"); + queue.enQueue("Y"); + queue.enQueue(5); + System.out.println(queue); + System.out.println("queue.size()="+queue.size()); + System.out.println("queue.deQueue()="+queue.deQueue()); + System.out.println(queue); + System.out.println("queue.isEmpty()="+queue.isEmpty()); + System.out.println(queue); + } + public void testStack(){ + Stack stack = new Stack(); + stack.push("S"); + stack.push("Y"); + stack.push(5); + System.out.println("stack.size()="+stack.size()); + System.out.println("stack.peek()="+stack.peek()); + System.out.println(stack); + System.out.println("stack.isEmpty()="+stack.isEmpty()); + stack.pop(); + System.out.println(stack); + } + public void testBinaryTreeNode(){ + System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); + System.out.println("new 一个实例"); + BinaryTreeNode root = new BinaryTreeNode(); + root.insert(5); + root.insert(6); + root.insert(9); + root.insert(3); + root.insert(3); + root.insert(2); + root.insert(10); + System.out.println(root); + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testLinkedList() { + System.out.println("-------------------LinkedList 测试开始-------------------"); + + System.out.println("new 一个实例"); + LinkedList list = new LinkedList(); + + System.out.println("添加元素----A"); + list.add("A"); + Assert.assertEquals(list.get(list.size()-1),"A"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("添加元素----B"); + list.add("B"); + Assert.assertEquals(list.get(list.size()-1),"B"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("添加元素----3"); + list.add(3); + Assert.assertEquals(list.get(list.size()-1),3); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在下标1插入元素----3"); + list.add(1, 3); + Assert.assertEquals(list.get(1),3); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在下标3插入元素----6"); + list.add(3, 6); + Assert.assertEquals(list.get(3),6); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("获取size"); + System.out.println("结果:"+list.size()); + + System.out.println(); + System.out.println("在首位前插入F"); + list.addFirst("F"); + Assert.assertEquals(list.get(0),"F"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在末位前插入K"); + list.addLast("K"); + Assert.assertEquals(list.get(list.size()-1),"K"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除首位"); + list.removeFirst(); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除末尾"); + list.removeLast(); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("迭代器输出:"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.print(i.next()+" "); + } + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + + /** + * 测试 ArrayList + */ + public void testArrayList() { + System.out.println("-------------------ArrayList 测试开始-------------------"); + + System.out.println("new 一个实例"); + ArrayList list = new ArrayList(); + + System.out.println("添加元素 A"); + list.add("A"); + Assert.assertEquals(list.get(list.size()-1),"A"); + + System.out.println("添加元素 B"); + list.add("B"); + Assert.assertEquals(list.get(list.size()-1),"B"); + + System.out.println("添加元素 3"); + list.add(3); + Assert.assertEquals(list.get(list.size()-1),3); + System.out.println("输出:"+list); + + System.out.println("添加元素 3 到索引 1"); + list.add(1, 3); + Assert.assertEquals(list.get(1),3); + System.out.println("输出:"+list); + + System.out.println("添加元素 6 到索引 3"); + list.add(3, 6); + Assert.assertEquals(list.get(3),6); + System.out.println("输出:"+list); + + System.out.println("移除 索引 4 元素"); + Object rm = list.remove(4); + System.out.println("输出:"+list); + + System.out.println("获取 索引 4 元素"); + Object get = list.get(4); + Assert.assertNotEquals(rm,get); + + System.out.println("输出:"+list); + System.out.println("数量:"+list.size()); + Iterator i = list.iterator(); + System.out.print("迭代器输出:"); + while (i.hasNext()){ + System.out.print(i.next()+" "); + } + System.out.println("-------------------ArrayList 测试结束-------------------"); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..34d76db083 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,105 @@ +package com.coding.basic; + +/** + * 实现二叉树 + * left总比父节点小 + * right总比父节点大 + */ +public class BinaryTreeNode { + private Node root; + private int size = 0; + + /** + * 插入数据 + * @param data + */ + public void insert(int data) { + final Node newNode = new Node(data); + if (root == null) {//根节点为空 直接插入数据到根节点 + root = newNode; + } else { + Node current = root; + while (true) {//循环判断 + Node parent = current; + if (data < current.data) {//比父节点小 就是left + current = current.left; + //直到left节点不存在 + if (current == null) { + //插入数据 + parent.left = newNode; + return; + } + } else {//比父节点大 也就是right + current = current.right; + //直到right节点不存在 + if (current == null) { + //插入数据 + parent.right = newNode; + return; + } + } + } + } + size++; + } + + + /** + * 返回数量 + * @return + */ + public int size() { + return size; + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "["+midTraverse(root)+"]"; + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + int data; + Node left; + Node right; + + Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + //先序遍历 + private String preTraverse(Node node) { + if (node == null) + return ""; + else + return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); + } + //中序遍历 + private String midTraverse(Node node) { + if (node == null) + return ""; + else + return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); + } + //后序遍历 + private String posTraverse(Node node) { + if (node == null) + return ""; + else + return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; + } + + private String preJointComma(String str) { + return str == "" ? "" : "," + str; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Iterator.java b/group15/1521_653895972/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1521_653895972/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/group15/1521_653895972/src/com/coding/basic/LinkedList.java b/group15/1521_653895972/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..f1f942590d --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/LinkedList.java @@ -0,0 +1,271 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by wanc on 2017/2/21. + * 实现单向链表集合 + */ +public class LinkedList implements List { + /** + * 首节点 + */ + private Node head; + /** + * 计数 + */ + private int size = 0; + + /** + * 检查是否越界 利用jdk源码的检测方法 + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + /** + * JDK 源码 错误信息 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 获取对应下标的节点 + */ + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + /** + * 在末尾添加数据 + * + * @param o + */ + public void add(Object o) { + + if (head == null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + /** + * 指定位置添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + return node(index).data; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index - 1 < 0) { + prevNode.next = null; + head = x; + } else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + /** + * 在链首添加数据 + * + * @return + */ + public void addFirst(Object o) { + final Node h = head; + final Node newNode = new Node(o, h); + head = newNode; + size++; + } + + /** + * 在链尾添加数据 + * + * @return + */ + public void addLast(Object o) { + add(o); + } + + /** + * 移除链首数据 + * + * @return + */ + public Object removeFirst() { + final Node h = head; + if (h == null) + throw new NoSuchElementException(); + final Node newFirst = h.next; + h.next = null; + head = newFirst; + size--; + return h.data; + } + + /** + * 移除链尾数据 + * + * @return + */ + public Object removeLast() { + final Node prev = node(size - 1 - 1); + final Node l = prev.next; + prev.next = null; + l.next = null; + size--; + return l.data; + } + + /** + * 获取迭代器 + * + * @return + */ + public Iterator iterator() { + return new LinkedItr(); + } + + /** + * 迭代器实现内部类 + * + * @return + */ + private class LinkedItr implements Iterator { + int cursor;//游标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > size - 1) throw new NoSuchElementException(); + Node current = node(i); + if (current == null) throw new IndexOutOfBoundsException(); + cursor = i + 1; + return current.data; + } + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + String result = "["; + for (int i = 0; i < size; i++) { + Node n = node(i); + if (i == 0) + result += n.data; + else + result += "," + n.data; + + } + + return result + "]"; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/List.java b/group15/1521_653895972/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1521_653895972/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/group15/1521_653895972/src/com/coding/basic/Queue.java b/group15/1521_653895972/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4add2be9a4 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Queue.java @@ -0,0 +1,60 @@ +package com.coding.basic; + +/** + * Created by wanc on 2017/2/21. + * 利用LinkedList 实现队列 + */ +public class Queue { + /** + * 利用LinkedList 保存数据 + */ + private LinkedList elementData = new LinkedList(); + + /** + * 入队 + * + * @param o + */ + public void enQueue(Object o) { + elementData.add(o); + } + + /** + * 出队 + * + * @return + */ + public Object deQueue() { + return elementData.removeFirst(); + } + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + /** + * 返回队列长度 + * + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Queue{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Stack.java b/group15/1521_653895972/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..23c5ba6a7b --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Stack.java @@ -0,0 +1,64 @@ +package com.coding.basic; +/** + * Created by wanc on 2017/2/21. + * 利用ArrayList 实现栈 + */ +public class Stack { + /** + * 利用ArrayList 保存数据 + */ + private ArrayList elementData = new ArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o) { + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + elementData.remove(elementData.size()-1); + return null; + } + + /** + * 返回栈顶数据 + * @return + */ + public Object peek() { + return elementData.get(elementData.size()-1); + } + + /** + * 是否为空 + * @return + */ + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + /** + * 返回栈长度 + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Stack{" + + "elementData=" + elementData + + '}'; + } +}