From ebda0625eed1c6f8a58cc383946e2caf78c2022b Mon Sep 17 00:00:00 2001 From: liangduoduo666666 <798277403@qq.com> Date: Sun, 12 Mar 2017 23:48:16 +0800 Subject: [PATCH] =?UTF-8?q?zl=E7=9A=84=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/798277403/src/week1/ArrayList.java | 147 ++++++++++++++++ .../798277403/src/week1/ArrayListTest.java | 55 ++++++ group24/798277403/src/week1/BinaryTree.java | 124 ++++++++++++++ .../798277403/src/week1/BinaryTreeNode.java | 72 ++++++++ group24/798277403/src/week1/Iterator.java | 10 ++ group24/798277403/src/week1/LinkedList.java | 161 ++++++++++++++++++ .../798277403/src/week1/LinkedListTest.java | 53 ++++++ group24/798277403/src/week1/List.java | 13 ++ group24/798277403/src/week1/Queue.java | 33 ++++ group24/798277403/src/week1/QueueTest.java | 36 ++++ group24/798277403/src/week1/Stack.java | 34 ++++ group24/798277403/src/week1/StackTest.java | 39 +++++ 12 files changed, 777 insertions(+) create mode 100644 group24/798277403/src/week1/ArrayList.java create mode 100644 group24/798277403/src/week1/ArrayListTest.java create mode 100644 group24/798277403/src/week1/BinaryTree.java create mode 100644 group24/798277403/src/week1/BinaryTreeNode.java create mode 100644 group24/798277403/src/week1/Iterator.java create mode 100644 group24/798277403/src/week1/LinkedList.java create mode 100644 group24/798277403/src/week1/LinkedListTest.java create mode 100644 group24/798277403/src/week1/List.java create mode 100644 group24/798277403/src/week1/Queue.java create mode 100644 group24/798277403/src/week1/QueueTest.java create mode 100644 group24/798277403/src/week1/Stack.java create mode 100644 group24/798277403/src/week1/StackTest.java diff --git a/group24/798277403/src/week1/ArrayList.java b/group24/798277403/src/week1/ArrayList.java new file mode 100644 index 0000000000..03257803df --- /dev/null +++ b/group24/798277403/src/week1/ArrayList.java @@ -0,0 +1,147 @@ +package week1; + +import java.util.Arrays; + +/** + java泛型: + ? 表示不确定的java类型。 + T 表示java类型。 + K V 分别代表java键值中的Key Value。 + E 代表Element。 + + * 自己实现的ArrayList + * Created by zhouliang on 2017-03-10. + */ +class ArrayList implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int size){ + this.elementData = new Object[size]; + } + + //默认大小为10 + public ArrayList(){ + this.elementData = new Object[10]; + } + + //判断是否需要扩展容量 ((旧容量 * 3) / 2) + 1 + private void checkcapacity(int index){ + if(index>elementData.length){ + int length = (elementData.length*3)/2+1; + /* + Object[] temp = new Object[length]; + for(int i=0; i= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private ArrayList list = null; + private int position = 0; + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public E next() { + return list.get(position++); + } + } +} diff --git a/group24/798277403/src/week1/ArrayListTest.java b/group24/798277403/src/week1/ArrayListTest.java new file mode 100644 index 0000000000..98e30a222a --- /dev/null +++ b/group24/798277403/src/week1/ArrayListTest.java @@ -0,0 +1,55 @@ +package week1; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp(){ + for(int i=0; i<100; i++){ + arrayList.add(i); + } + } + + @Test + public void add() throws Exception { + for(int i=100; i<1000; i++){ + arrayList.add(i); + } + Assert.assertEquals(1000,arrayList.size()); + } + + @Test + public void add1() throws Exception { + java.util.LinkedList l = new java.util.LinkedList(); + } + + @Test + public void get() throws Exception { + System.out.println(arrayList.get(99)); + } + + @Test + public void remove() throws Exception { + System.out.println(arrayList.size()); + arrayList.remove(arrayList.size()-1); + System.out.println(arrayList.size()); + //Assert.assertEquals((Integer)99,(Integer)arrayList.size()); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git a/group24/798277403/src/week1/BinaryTree.java b/group24/798277403/src/week1/BinaryTree.java new file mode 100644 index 0000000000..3c480dc012 --- /dev/null +++ b/group24/798277403/src/week1/BinaryTree.java @@ -0,0 +1,124 @@ +package week1; + +/** + * Created by zhouliang on 2017-03-11. + */ +class BinaryTree { + private TreeNode root; + + class TreeNode { + int val = 0; + TreeNode left = null; + TreeNode right = null; + public TreeNode(int val) { + this.val = val; + } + } + + /** + * 递归创建二叉树 + * @param node + * @param data + */ + public void buildTree(TreeNode node,int data){ + if(root == null){ + root = new TreeNode(data); + }else{ + if(data < node.val){ + if(node.left == null){ + node.left = new TreeNode(data); + }else{ + buildTree(node.left,data); + } + }else{ + if(node.right == null){ + node.right = new TreeNode(data); + }else{ + buildTree(node.right,data); + } + } + } + } + + /** + * 前序遍历 + * @param node + */ + public void preOrder(TreeNode node){ + if(node != null){ + System.out.println(node.val); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * @param node + */ + public void inOrder(TreeNode node){ + if(node != null){ + inOrder(node.left); + System.out.println(node.val); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * @param node 一般是传入根节点 + */ + public void postOrder(TreeNode node){ + if(node != null){ + postOrder(node.left); + postOrder(node.right); + System.out.println(node.val); + } + } + + + /** + * 分层打印 + * @param root 树的根节点 + * @return 分层后的数组 + */ + public int[][] printTree(TreeNode root) { + if(root == null){ + return null; + } + java.util.LinkedList queue = new java.util.LinkedList(); + TreeNode last = root; + TreeNode nlast = null ; + TreeNode currentNode = null; + java.util.ArrayList> lists = new java.util.ArrayList>(); + java.util.ArrayList list = new java.util.ArrayList(); + queue.add(last); + while(!queue.isEmpty()){ + currentNode = queue.poll(); + list.add(currentNode.val); + + if(currentNode.left!=null){ + queue.add(currentNode.left); + nlast = currentNode.left; + } + if(currentNode.right!=null){ + queue.add(currentNode.right); + nlast = currentNode.right; + } + if(currentNode == last){ + lists.add(list); + last = nlast; + list = new java.util.ArrayList(); + } + } + + int[][] result = new int[lists.size()][]; + for(int i = 0 ; i < lists.size() ; i++){ + result[i] = new int[lists.get(i).size()]; + for(int j = 0 ; j < lists.get(i).size() ; j++){ + result[i][j] = lists.get(i).get(j); + } + } + return result; + } +} diff --git a/group24/798277403/src/week1/BinaryTreeNode.java b/group24/798277403/src/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..e680d5ed15 --- /dev/null +++ b/group24/798277403/src/week1/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package week1; + +/** + * 自己实现的BinaryTreeNode + * Created by zhouliang on 2017-03-10. + */ +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 o){ + if(data==null && left==null && right==null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + }else{ + BinaryTreeNode temp = this; + BinaryTreeNode node = new BinaryTreeNode(); + while(true){ + if((Integer)o > (Integer)temp.getData()){ + if(temp.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setRight(node); + return this; + }else{ + temp = temp.getRight(); + } + }else{ + if(temp.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setLeft(node); + return this; + }else{ + temp = temp.getLeft(); + } + } + } + } + } +} diff --git a/group24/798277403/src/week1/Iterator.java b/group24/798277403/src/week1/Iterator.java new file mode 100644 index 0000000000..73ba87c125 --- /dev/null +++ b/group24/798277403/src/week1/Iterator.java @@ -0,0 +1,10 @@ +package week1; + +/** + * 自己实现的Iterator + * Created by zhouliang on 2017-03-10. + */ +interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group24/798277403/src/week1/LinkedList.java b/group24/798277403/src/week1/LinkedList.java new file mode 100644 index 0000000000..d0933cfcb1 --- /dev/null +++ b/group24/798277403/src/week1/LinkedList.java @@ -0,0 +1,161 @@ +package week1; + +/** + * 自己实现的LinkedList + * Created by zhouliang on 2017-03-10. + */ +class LinkedList implements List { + + private int size; + private Node first; + private Node last; + + public LinkedList(){ + } + + @Override + public void add(E e) { + Node temp = new Node(e); + if(first != null){ + last.next = temp; + last = temp; + }else{ + first = temp; + last = temp; + } + size++; + } + + /** + * 指定下标添加元素 + * @param index 可以在链表末尾加,就是可以的等于size,不能大于size + * @param e 代表Element + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + + Node temp = new Node(e); + if(index == size){ + last.next = temp; + last = temp; + }else{ + Node begin = first; + index--; + while(index>0){ + begin = begin.next; + index--; + } + Node next = begin.next; + begin.next = temp; + temp.next = next; + } + size++; + } + + @Override + public E get(int index) { + checkElementIndex(index); + + Node temp = first; + while(index>0){ + temp = temp.next; + index--; + } + return temp.value; + } + + @Override + public E remove(int index) { + checkElementIndex(index); + + Node temp; + if(index == 0){ + temp = first; + first = first.next; + size--; + return temp.value; + }else{ + temp = first; + index--; + //找到要删除节点的前一个节点 + while(index>0){ + temp = temp.next; + index--; + } + Node removeNode = temp.next; + temp.next = removeNode.next; + size--; + return removeNode.value; + + } + + } + + public E removeLast(){ + return remove(size-1); + } + + public void addFirst(E e){ + Node temp = new Node(e); + if(first == null){ + first = temp; + last = temp; + }else{ + temp.next = first; + first = temp; + } + size++; + } + + public void addLast(E e){ + Node temp = new Node(); + if(first == null){ + first = temp; + last = temp; + }else{ + last.next = temp; + last = temp; + } + size++; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + @Override + public int size() { + return size; + } + + private static class Node{ + E value; + Node next; + + Node(){ + + } + + Node(E e){ + this.value = e; + } + } +} diff --git a/group24/798277403/src/week1/LinkedListTest.java b/group24/798277403/src/week1/LinkedListTest.java new file mode 100644 index 0000000000..9a8a9d936d --- /dev/null +++ b/group24/798277403/src/week1/LinkedListTest.java @@ -0,0 +1,53 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class LinkedListTest { + + private LinkedList myLinkedList = new LinkedList<>(); + + private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + myLinkedList.add(i); + systemLinkedList.add(i); + } + } + @Test + public void add() throws Exception { + for(int i=0; i<10; i++){ + System.out.println(myLinkedList.get(i)); + } + } + + @Test + public void remove() throws Exception { + myLinkedList.remove(5); + for(int i=0; i { + void add(E e); + void add(int index, E e); + E get(int index); + E remove(int index); + int size(); +} diff --git a/group24/798277403/src/week1/Queue.java b/group24/798277403/src/week1/Queue.java new file mode 100644 index 0000000000..184f3a5336 --- /dev/null +++ b/group24/798277403/src/week1/Queue.java @@ -0,0 +1,33 @@ +package week1; + +/** + * 自己实现的Queue,用自己的LinkedList实现 + * Created by zhouliang on 2017-03-10. + */ +class Queue { + + private LinkedList linkedList; + + public Queue(){ + this.linkedList = new LinkedList(); + } + /** + * 从队列头部添加元素 + * @param e 代表Element + */ + public void enQueue(E e){ + linkedList.addFirst(e); + } + + public E deQueue(){ + return linkedList.removeLast(); + } + + public boolean isEmpty(){ + return linkedList.size() > 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group24/798277403/src/week1/QueueTest.java b/group24/798277403/src/week1/QueueTest.java new file mode 100644 index 0000000000..b5a333f64d --- /dev/null +++ b/group24/798277403/src/week1/QueueTest.java @@ -0,0 +1,36 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * 自己实现的队列,先进先出 + * Created by zhouliang on 2017-03-10. + */ +public class QueueTest { + private Queue queue = new Queue<>(); + + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + queue.enQueue(i); + } + } + + @Test + public void enQueue() throws Exception { + System.out.println(queue.size()); + } + + @Test + public void deQueue() throws Exception { + System.out.println(queue.deQueue()+" "+queue.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file diff --git a/group24/798277403/src/week1/Stack.java b/group24/798277403/src/week1/Stack.java new file mode 100644 index 0000000000..f85aa2ada5 --- /dev/null +++ b/group24/798277403/src/week1/Stack.java @@ -0,0 +1,34 @@ +package week1; + +/** + * 自己实现的Stack + * Created by zhouliang on 2017-03-10. + */ +class Stack { + + private ArrayList elementData; + + public Stack(){ + this.elementData = new ArrayList(); + } + + public void push(E e){ + elementData.add(e); + } + + public E pop(){ + return elementData.remove(elementData.size()-1); + } + + public E peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() > 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group24/798277403/src/week1/StackTest.java b/group24/798277403/src/week1/StackTest.java new file mode 100644 index 0000000000..f4213d66d3 --- /dev/null +++ b/group24/798277403/src/week1/StackTest.java @@ -0,0 +1,39 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class StackTest { + private Stack stack = new Stack<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + stack.push(i); + } + } + + @Test + public void pop() throws Exception { + System.out.println("size "+stack.size()); + while(stack.size()>0){ + System.out.println(stack.pop()); + } + } + + @Test + public void peek() throws Exception { + System.out.println(stack.size()); + int i = stack.peek(); + System.out.println(i+" "+stack.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file