diff --git a/.gitignore b/.gitignore index ec55baf87d..57c35943b8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ hs_err_pid* #ide config .metadata -.recommenders +.recommenders \ No newline at end of file diff --git a/group01/496740686/src/Impl/MyArraryList.java b/group01/496740686/src/Impl/MyArraryList.java new file mode 100644 index 0000000000..20fb5dcfdf --- /dev/null +++ b/group01/496740686/src/Impl/MyArraryList.java @@ -0,0 +1,141 @@ +package Impl; + +import Interface.ArrayList; +import Interface.Iterator; +import ex.MyArrest; + +/** + * Created by Administrator on 2017/2/25. + */ +public class MyArraryList extends ArrayList { + private Object[] objArr; + private int size; + private int postion; + + public MyArraryList() { + this.objArr = new Object[10]; + this.size = 10; + this.postion = 0; + } + + + public MyArraryList(int size) { + this.objArr = new Object[size]; + this.size = size; + this.postion = 0; + } + + public MyArraryList(Object[] objArr) { + this.objArr = objArr; + this.size = objArr.length; + this.postion = objArr.length - 1; + } + + @Override + public void add(Object o) { + int limit = this.size + (this.size / 2); + Object[] newObjArr = new Object[limit]; + //public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)从指定源数组中复制一个数组, + // 复制从指定的位置开始,到目标数组的指定位置结束。从src引用的源数组到dest引用的目标数组, + // 数组组件的一个子序列被复制下来。被复制的组件的编号等于length参数。 + // 源数组中位置在srcPos到srcPos+length-1之间的组件被分别复制到目标数组中的destPos到destPos+length-1位置。 + System.arraycopy(this.objArr, 0, newObjArr, 0, objArr.length); + this.postion = this.size - 1; + newObjArr[this.postion] = o; + this.size = limit; + objArr = null; + this.objArr = newObjArr; + } + + @Override + public void add(int index, Object o) { + arrIndexVildate(index); + objArr[index - 1] = o; + size++; + } + + @Override + public Object get(int index) { + arrIndexVildate(index); + return objArr[index - 1]; + } + + @Override + public Object remove(int index) { + arrIndexVildate(index); + Object remoteObj = objArr[index - 1]; + objArr[index - 1] = null; + size--; + //TODO need GC ccontrol + return remoteObj; + } + + @Override + public int size() { + return this.size; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private MyArraryList arraryList; + private int index; + + public ArrayListIterator(MyArraryList arraryList) { + this.arraryList = arraryList; + this.index = arraryList.size - 1; + } + + @Override + public boolean hasNext() { + if (index > arraryList.size) { + return true; + } else { + return false; + } + } + + @Override + public Object next() { + Object obj = arraryList.get(index); + index++; + return obj; + } + } + + private void arrIndexVildate(int index) { + if (index > size - 1 || index < 0) { + new Exception(String.format("cant than that array index %s,but got %", size - 1, index)); + } + } + + //test method + public static void main(String[] args) { + MyArraryList myArrary = new MyArraryList(); + MyArrest.arrestEq(10, myArrary.size()); + myArrary.add(1, 10); + MyArrest.arrestEq(10, myArrary.get(1)); + myArrary.add(100); + System.out.println(myArrary.get(11)); + myArrary.remove(1); + MyArrest.arrestIsNull(myArrary.get(1)); + if (myArrary.iterator().hasNext()) { + myArrary.iterator().next(); + } + System.out.println("test myArrary2"); + MyArraryList myArrary2 = new MyArraryList(20); + MyArrest.arrestEq(20, myArrary2.size()); + myArrary2.add(1, 10); + MyArrest.arrestEq(10, myArrary2.get(1)); + myArrary2.add(100); + MyArrest.arrestIsNull(myArrary2.get(20)); + myArrary2.remove(1); + MyArrest.arrestIsNull(myArrary2.get(1)); + if (myArrary.iterator().hasNext()) { + myArrary2.iterator().next(); + } + } +} diff --git a/group01/496740686/src/Impl/MyLinkedList.java b/group01/496740686/src/Impl/MyLinkedList.java new file mode 100644 index 0000000000..017bac5baf --- /dev/null +++ b/group01/496740686/src/Impl/MyLinkedList.java @@ -0,0 +1,177 @@ +package Impl; + +import Interface.Iterator; +import Interface.LinkedList; +import ex.MyArrest; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyLinkedList extends LinkedList { + private MyLinkedList.Node head; + private int size = 1; + + public MyLinkedList() { + + } + + private static class Node { + Object data; + MyLinkedList.Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public void add(Object o) { + if (this.size == 1) { + head.data = o; + return; + } + MyLinkedList.Node newHead = new MyLinkedList.Node(o, this.head); + this.size += 1; + this.head = newHead; + } + + + public void add(int index, Object o) { + IndexVildate(index); + int pos = 0; + if (index == 1) { + this.head = new Node(o, null); + return; + } + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + pos += 1; + if (pos == index - 1) { + node.data = o; + this.size += 1; + } + } + } + + + public Object get(int index) { + int pos = 0; + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (pos == index - 1) { + return node.data; + } + pos += 1; + } + return null; + } + + + public Object remove(int index) { + IndexVildate(index); + int pos = 0; + MyLinkedList.Node preNode; + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + pos += 1; + if (pos == index - 2) { + //record previous node + preNode = node; + if (pos == index - 1) { + MyLinkedList.Node willDelNode = node; + preNode.next = node.next; + node = null; + this.size -= 1; + return willDelNode; + } + } + } + return null; + } + + + public int size() { + return this.size; + } + + + public void addFirst(Object o) { + MyLinkedList.Node newHead = this.head; + newHead.data = o; + newHead.next = this.head; + this.size += 1; + this.head = newHead; + } + + + public void addLast(Object o) { + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (node.next == null) { + MyLinkedList.Node lastNode = new MyLinkedList.Node(o, null); + node.next = lastNode; + this.size += 1; + } + } + } + + + public Object removeFirst() { + MyLinkedList.Node oldHead = this.head; + this.head = oldHead.next; + this.size -= 1; + return oldHead; + } + + + public Object removeLast() { + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (node.next == null) { + MyLinkedList.Node willDelNode = node.next; + node.next = null; + this.size -= 1; + return willDelNode; + } + } + return null; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + private MyLinkedList linkedList; + private int index; + + public LinkedListIterator(MyLinkedList linkedList) { + this.linkedList = linkedList; + this.index = linkedList.size; + } + + @Override + public boolean hasNext() { + if (index > linkedList.size) { + return true; + } else { + return false; + } + } + + @Override + public Object next() { + Object obj = linkedList.get(index); + index++; + return obj; + } + } + + private void IndexVildate(int index) { + if (index > this.size || index < 0) { + System.out.println("happend error"); + } + } + + public static void main(String[] args) { + MyLinkedList linkedList = new MyLinkedList(); + linkedList.add(1, 23); + MyArrest.arrestEqByBasicType(1, linkedList.size()); + + } +} diff --git a/group01/496740686/src/Impl/MyQueue.java b/group01/496740686/src/Impl/MyQueue.java new file mode 100644 index 0000000000..1a029738d2 --- /dev/null +++ b/group01/496740686/src/Impl/MyQueue.java @@ -0,0 +1,68 @@ +package Impl; + +import Interface.Queue; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyQueue extends Queue { + + private Node first; // beginning of queue + private Node last; // end of queue + private int size; // number of elements on queue + + private static class Node { + private Object value; + private Node next; + + public Node(Object value, Node next) { + this.value = value; + this.next = next; + } + } + + public MyQueue() { + first = null; + last = null; + int n = 0; + } + + @Override + public void enQueue(Object o) { + Node oldlast = this.last; + this.last = new Node(o, null); + size += 1; + //第一个进队列 + if (isEmpty()) { + first = last; + } else { + oldlast.next = this.last; + } + + } + + @Override + public Object deQueue() { + if (isEmpty()) { + return null; + } else { + Node oldFirst = this.first; + Node newFirst = this.first.next; + this.first = null; + this.first = newFirst; + this.size -= 1; + return oldFirst; + + } + } + + @Override + public boolean isEmpty() { + return first == null; + } + + @Override + public int size() { + return size; + } +} diff --git a/group01/496740686/src/Impl/MyStack.java b/group01/496740686/src/Impl/MyStack.java new file mode 100644 index 0000000000..3a7c119e99 --- /dev/null +++ b/group01/496740686/src/Impl/MyStack.java @@ -0,0 +1,70 @@ +package Impl; + +import Interface.ArrayList; +import Interface.Stack; + + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyStack extends Stack { + + private MyStack.Node first; // beginning of queue + private MyStack.Node last; // end of queue + private int size; // number of elements on queue + + private static class Node { + private Object value; + private MyStack.Node next; + + public Node(Object value, MyStack.Node next) { + this.value = value; + this.next = next; + } + } + + public MyStack() { + first = null; + last = null; + int n = 0; + } + + @Override + public void push(Object o) { + if (isEmpty()) { + MyStack.Node oldFirst = this.first; + this.first = new MyStack.Node(o, null); + size += 1; + //第一个进栈 + if (isEmpty()) { + first = last; + } else { + oldFirst.next = this.last; + } + } + } + + @Override + public Object pop() { + if (isEmpty()) { + return null; + } else { + MyStack.Node oldFirst = this.first; + this.first = oldFirst.next; + this.size -= 1; + return oldFirst; + + } + + } + + @Override + public Object peek() { + return this.first; + } + + @Override + public boolean isEmpty() { + return first == null; + } +} diff --git a/group01/496740686/src/Interface/ArrayList.java b/group01/496740686/src/Interface/ArrayList.java new file mode 100644 index 0000000000..567c1996b1 --- /dev/null +++ b/group01/496740686/src/Interface/ArrayList.java @@ -0,0 +1,34 @@ +package Interface; + + + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group01/496740686/src/Interface/BinaryTreeNode.java b/group01/496740686/src/Interface/BinaryTreeNode.java new file mode 100644 index 0000000000..c5480a614f --- /dev/null +++ b/group01/496740686/src/Interface/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package Interface; + +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 o){ + return null; + } + +} diff --git a/group01/496740686/src/Interface/Iterator.java b/group01/496740686/src/Interface/Iterator.java new file mode 100644 index 0000000000..77e3c0b216 --- /dev/null +++ b/group01/496740686/src/Interface/Iterator.java @@ -0,0 +1,7 @@ +package Interface; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/496740686/src/Interface/LinkedList.java b/group01/496740686/src/Interface/LinkedList.java new file mode 100644 index 0000000000..b7166a1731 --- /dev/null +++ b/group01/496740686/src/Interface/LinkedList.java @@ -0,0 +1,47 @@ +package Interface; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + +} diff --git a/group01/496740686/src/Interface/List.java b/group01/496740686/src/Interface/List.java new file mode 100644 index 0000000000..98d6a4da0a --- /dev/null +++ b/group01/496740686/src/Interface/List.java @@ -0,0 +1,9 @@ +package Interface; + +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/group01/496740686/src/Interface/Queue.java b/group01/496740686/src/Interface/Queue.java new file mode 100644 index 0000000000..8e3a5d5f43 --- /dev/null +++ b/group01/496740686/src/Interface/Queue.java @@ -0,0 +1,19 @@ +package Interface; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group01/496740686/src/Interface/Stack.java b/group01/496740686/src/Interface/Stack.java new file mode 100644 index 0000000000..7e536e250a --- /dev/null +++ b/group01/496740686/src/Interface/Stack.java @@ -0,0 +1,22 @@ +package Interface; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group01/496740686/src/ex/MyArrest.java b/group01/496740686/src/ex/MyArrest.java new file mode 100644 index 0000000000..9987b83535 --- /dev/null +++ b/group01/496740686/src/ex/MyArrest.java @@ -0,0 +1,75 @@ +package ex; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyArrest { + + + public static void arrestEq(T expect, T value) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect.equals(value)) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestEq(T expect, T value, String errorInfo) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect.equals(value)) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestEqByBasicType(T expect, T value) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestIsNull(Object obj) { + if (obj == null) { + System.out.println("it's null , you're right \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } +}