From 270670d1df9cda1b432112c368c9c8e01541d8c7 Mon Sep 17 00:00:00 2001 From: PreciousLiu Date: Fri, 3 Mar 2017 17:15:50 +0800 Subject: [PATCH] 286674878 week01 --- group09/286674878/.gitignore | 1 + group09/286674878/src/week01/ArrayList.java | 101 +++++++++++++ group09/286674878/src/week01/BinaryTree.java | 81 +++++++++++ group09/286674878/src/week01/Iterator.java | 7 + group09/286674878/src/week01/LinkedList.java | 145 +++++++++++++++++++ group09/286674878/src/week01/List.java | 10 ++ group09/286674878/src/week01/Queue.java | 30 ++++ group09/286674878/src/week01/Stack.java | 51 +++++++ 8 files changed, 426 insertions(+) create mode 100644 group09/286674878/.gitignore create mode 100644 group09/286674878/src/week01/ArrayList.java create mode 100644 group09/286674878/src/week01/BinaryTree.java create mode 100644 group09/286674878/src/week01/Iterator.java create mode 100644 group09/286674878/src/week01/LinkedList.java create mode 100644 group09/286674878/src/week01/List.java create mode 100644 group09/286674878/src/week01/Queue.java create mode 100644 group09/286674878/src/week01/Stack.java diff --git a/group09/286674878/.gitignore b/group09/286674878/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group09/286674878/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group09/286674878/src/week01/ArrayList.java b/group09/286674878/src/week01/ArrayList.java new file mode 100644 index 0000000000..f379af550b --- /dev/null +++ b/group09/286674878/src/week01/ArrayList.java @@ -0,0 +1,101 @@ +package week01; + +import java.util.Arrays; + + + +public class ArrayList implements List { + /** + * The current array's size. + */ + private int size = 0; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + /** + * Return an empty Array instance if no initial elements has specified. + */ + private static final Object[] EMPTY_ELEMENTDATA ={}; + /** + * Construct an ArrayList with default capacity. + */ + public ArrayList(){ + this(DEFAULT_CAPACITY); + } + /** + * Construct an ArrayList instance with specified capacity. + * @param initialSize the specified capacity + */ + public ArrayList(int initialSize){ + if(initialSize>0){ //Does it right >0 but > DEFAULTCAPACITY? + this.elementData = new Object[initialSize]; + }else if(initialSize==0){ + this.elementData = EMPTY_ELEMENTDATA; + }else{ + throw new IllegalArgumentException("IllegalCapacity:" + initialSize); + } + } + /** + * Increase the capacity + */ + public void grow(int capacity){ + elementData = Arrays.copyOf(elementData, capacity); + } + /** + * Add an element to ArrayList's tail + */ + public boolean add(Object o){ + if(this.size == elementData.length){ + grow(size+10); + } + elementData[size++] = o; + return true; + } + /** + * Add an element to an Array with specified location + */ + public boolean add(int index, Object o){ + if(index<0 || index>this.size){ + throw new IllegalArgumentException("IllegalIndex"); + }else if(this.size+1>elementData.length){ + grow(size+10); + }else{ + for(int i=this.size+1;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + } + return true; + } + /** + * Return an element that specified + */ + public Object get(int index){ + if(index<0 || index>this.size){ + throw new IllegalArgumentException("IllegalIndex"); + } + return elementData[index]; + } + + public boolean remove(int index){ + if(index<0 || index>this.size){ + throw new IllegalArgumentException("IllegalIndex"); + }else{ + for(int i=index;i<=this.size;i++){ + elementData[i] = elementData[i+1]; + } + size--; + } + return true; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} + diff --git a/group09/286674878/src/week01/BinaryTree.java b/group09/286674878/src/week01/BinaryTree.java new file mode 100644 index 0000000000..db334fdf7a --- /dev/null +++ b/group09/286674878/src/week01/BinaryTree.java @@ -0,0 +1,81 @@ +package week01; + + + +public class BinaryTree { + class BinaryTreeNode { + private int data; + private BinaryTreeNode left = null; + private BinaryTreeNode right = null; + public BinaryTreeNode(int data){ + this.data = data; + } + } + //前序遍历 + private BinaryTreeNode root; + public void preorder(BinaryTreeNode bt){ + if(bt!=null) + { + getData(bt); + preorder(bt.left); + preorder(bt.right); + } + } + public int getData(BinaryTreeNode bt){ + preorder(bt); + return bt.data; + } + public void setData(int data) { + BinaryTreeNode newadd = new BinaryTree.BinaryTreeNode(data); + Insert(newadd,data); + + } + public BinaryTreeNode getLeft(BinaryTreeNode bt) { + preorder(bt); + return bt.left; + } + //public void setLeft(BinaryTreeNode left) { + // this.left = left; + //} + public BinaryTreeNode getRight(BinaryTreeNode bt) { + preorder(bt); + return bt.right; + } + + //public void setRight(BinaryTreeNode right) { + // this.right = right; + //} + + public boolean Insert(BinaryTreeNode bt,int data){ + if(bt == null){ + bt.data = data; + return true; + } + else{ + if(data==bt.data) + { + System.out.println("The data has already exist"); + return false;} + else if(data < bt.data){ + return Insert(bt.left,data); + }else return Insert(bt.right,data); + } + + } + public BinaryTreeNode Search(BinaryTreeNode root, int data){ + if(root==null){ + //throw new IllegalArgumentException("The Tree is Empty "+root.data); + System.out.println("The Tree is an empty tree."); + return root; + }else{ + if(root.data==data) + {return root;} + else if(data>root.data) + {return Search(root.right,data);} + else + {return Search(root.left,data);} + } + } + +} + diff --git a/group09/286674878/src/week01/Iterator.java b/group09/286674878/src/week01/Iterator.java new file mode 100644 index 0000000000..42cf1ca740 --- /dev/null +++ b/group09/286674878/src/week01/Iterator.java @@ -0,0 +1,7 @@ +package week01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} + diff --git a/group09/286674878/src/week01/LinkedList.java b/group09/286674878/src/week01/LinkedList.java new file mode 100644 index 0000000000..6b652a9c2d --- /dev/null +++ b/group09/286674878/src/week01/LinkedList.java @@ -0,0 +1,145 @@ +package week01; + + + +public class LinkedList implements List{ + /** + * Class Node is an inner class + */ + class Node{ + private Object data; + private Node next = null; + private Node prev = null; + public Node(Object data){ + this.data = data; + } + } + private Node head; + private Node tail; + private int size = 0; + + /** + * Add an element in the LinkedList + */ + public boolean add(Object data){ + if(head.next==null){ + addFirst(data); + }else{ + addLast(data); + } + return true; + } + /** + * Add an element in the LinkedList that with specified location + */ + public boolean add(int index , Object data){ + if(index<0 || index>this.size){ + throw new IllegalArgumentException("IllegalArgument"+index); + }else{ + Node indexnodehere = findIndex(index); + Node newadd = new Node(data); + newadd.next = indexnodehere.next; + newadd.prev = indexnodehere; + indexnodehere.next.prev = newadd; + indexnodehere.next = newadd; + size++; + } + return true; + } + /** + * Find node according index + * @param index + * @return + */ + public Node findIndex(int index){ + Node indexnode = this.head; + if(index<0 || index>this.size){ + throw new IllegalArgumentException("IllegalArgument"+index); + }else{ + for(int i=1;ithis.size){ + throw new IllegalArgumentException("IllegalArgument"+index); + }else{ + Node indexnodehere2 = findIndex(index); + if(indexnodehere2.prev == this.head){ + removeFirst(); + }else if(indexnodehere2.next == this.tail){ + removeLast(); + }else{ + indexnodehere2.prev.next = indexnodehere2.next; + indexnodehere2.next.prev = indexnodehere2.prev; + size--; + } + + } + return true; + } + + public int size(){ + return this.size; + } + /** + * Add an new element in the beginning of an LinledList(just after head) + * @param data + */ + public void addFirst(Object data){ + Node newfirstincrease = new Node(data); + newfirstincrease.next = head.next; + head.next = newfirstincrease; + newfirstincrease.prev = head; + newfirstincrease.next = tail; + tail.prev = newfirstincrease; + size++; + } + /** + * Add an new element in the tail of an LinkedList + * @param data + */ + public void addLast(Object data){ + Node newincrease = new Node(data); + newincrease.next = tail; + newincrease.prev = tail.prev; + tail.prev.next = newincrease; + tail.prev = newincrease; + size++; + } + /** + * Delete the first element(node) which just after the head + * @return + */ + public boolean removeFirst(){ + head.next = head.next.next; + head.next.next.prev = head; + size--; + return true; + } + /** + * Delete the last element(node) which just before the tail + * @return + */ + public boolean removeLast(){ + tail.prev = tail.prev.prev; + tail.prev.prev.next= tail; + size--; + return true; + } + public Iterator iterator(){ + return null; + } + + + + +} + diff --git a/group09/286674878/src/week01/List.java b/group09/286674878/src/week01/List.java new file mode 100644 index 0000000000..c6f8dcbce3 --- /dev/null +++ b/group09/286674878/src/week01/List.java @@ -0,0 +1,10 @@ +package week01; + +public interface List { + public boolean add(Object o); + public boolean add(int index, Object o); + public Object get(int index); + public boolean remove(int index); + public int size(); + +} diff --git a/group09/286674878/src/week01/Queue.java b/group09/286674878/src/week01/Queue.java new file mode 100644 index 0000000000..0c8fb2ea54 --- /dev/null +++ b/group09/286674878/src/week01/Queue.java @@ -0,0 +1,30 @@ +package week01; + +import java.util.Arrays; +//以后有时间要改成循环队列 +public class Queue { + private ArrayList elementData = new ArrayList(); + private int front; + private int rear; + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + return null; + } + + public boolean isEmpty(){ + if(elementData.size()>0){ + return false; + }else return true; + } + + public int size(){ + return elementData.size(); + } + +} + diff --git a/group09/286674878/src/week01/Stack.java b/group09/286674878/src/week01/Stack.java new file mode 100644 index 0000000000..2b6ef12e0a --- /dev/null +++ b/group09/286674878/src/week01/Stack.java @@ -0,0 +1,51 @@ +package week01; + +import java.util.Arrays; + +public class Stack { +private ArrayList elementData = new ArrayList(); + + /** + * Pushes an item onto the top of this stack + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + /** + * Removes the object at the top of this stack + * @return + */ + public Object pop(){ + if(isEmpty()){ + throw new UnsupportedOperationException(); + } + elementData.remove(elementData.size()); + return null; + } + /** + * Looks at the object at the top of this stack without removing it from the stack + * @return + */ + public Object peek(){ + if(isEmpty()){ + throw new UnsupportedOperationException(); + }else{ + return elementData.get(elementData.size()); + } + } + /** + * Tests if this stack is empty + * @return + */ + public boolean isEmpty(){ + if(elementData.size()>0){ + return false; + }else return true; + + } + public int size(){ + return elementData.size(); + } + +}