diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..03f6710788 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java @@ -0,0 +1,83 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyArrayList { + + private Object[] initialArray = {}; + private Object[] dataArray; + private int initSize = 10; + private int arraySize; + public MyArrayList() { + dataArray = initialArray; + } + + public MyArrayList(int init) { + dataArray = new Object[init]; + } + + public void ensureCapacity(int newCapacity) { + if (newCapacity < arraySize) + return; + + Object[] old = dataArray; + dataArray = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + dataArray[i] = old[i]; + } + } + + public void add(T element) { + add(size(), element); + } + + public void add(int index, T element) { + if (size() == dataArray.length) { + ensureCapacity(size()*2 + 1); + } + for(int i=arraySize;i>index;i--) { + dataArray[i] = dataArray[i - 1]; + } + dataArray[index] = element; + arraySize++; + } + + public T delete(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T removeElement = (T)dataArray[index]; + for (int i = index; i < size() -1; i++) { + dataArray[i] = dataArray[i + 1]; + } + arraySize--; + return removeElement; + } + + public T get(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + public T set(int index, T newElement) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T oldElement = (T) dataArray[index]; + dataArray[index] = newElement; + + return oldElement; + } + + public int size() { + return arraySize; + } + + public boolean isEmpty() { + return size() == 0; + } + +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..9e7eab3401 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java @@ -0,0 +1,119 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyLinkedList { + private class Node { + public Node pre; + public Node next; + public T data; + + public Node(Node pre,Node next,T data) { + this.pre = pre; + this.next = next; + this.data = data; + } + } + + private int dataSize; + + private Node head; + private Node tail; + + public MyLinkedList() { + head = new Node(null,null,null); + tail = new Node(head, null, null); + head.next = tail; + dataSize = 0; + } + + public void add(T t) { +// add(size(), t); + Node newNode = new Node<>(null, tail, t); + newNode.pre = tail.pre; + tail.pre.next = newNode; + tail.pre = newNode; + dataSize++; + + } + + /** + * 根据索引添加没有实现 + * @param index + * @param element + */ + public void add(int index,T element) { + //TODO 根据索引添加元素 +// addBefore(getNode(index,0,size()-1),element); +// if (index == dataSize) { +// add(element); +// } else { + // +// } + } + + public T get(int index) { + return getNode(index).data; + } + + public T set(int index, T newValue) { + Node node = getNode(index); + T oldData = node.data; + node.data = newValue; + return oldData; + } + + public T remove(int index) { + Node node = getNode(index); + node.next.pre = node.pre; + node.pre.next = node.next; + dataSize--; + + return node.data; + + } + + private void addBefore(Node node, T element) { +// newNode.pre.next = newNode; +// node.pre = newNode; + Node pre = node.pre; + Node newNode = new Node<>(node.pre, node, element); + node.pre = newNode; + pre.next = newNode; + + dataSize++; + } + + private Node getNode(int index) { + return getNode(index, 0, size()); + } + + private Node getNode(int index, int lower, int upper) { + Node p; + if (index < lower || index > upper) { + throw new IndexOutOfBoundsException(); + } + + if (index < size() / 2) { + p = head.next; + for (int i = 0; i < index; i++) { + p = p.next; + } + } else { + p = tail.pre; + for (int i = size()-1; i > index; i--) { + p = p.pre; + } + } + return p; + } + + public int size() { + return dataSize; + } + + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..6d0c970b65 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java @@ -0,0 +1,29 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyQueue { + private MyLinkedList link = new MyLinkedList<>(); + + public void enQueue(T t) { + link.add(t); + } + + public T deQueue() { + if (size() <= 0) { + return null; + } + T t = link.get(0); + link.remove(0); + return t; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return link.size(); + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..3fd9d2f5c2 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java @@ -0,0 +1,30 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyStack { + private MyArrayList list = new MyArrayList<>(); + + public void push(T t) { + list.add(t); + } + + public T pop() { + if (size() <= 0) { + throw new IndexOutOfBoundsException(); + } + return list.delete(size() - 1); + } + + public T peek() { + return list.get(size() - 1); + } + + public boolean isEmpty() { + return list.size() == 0; + } + public int size() { + return list.size(); + } +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java new file mode 100644 index 0000000000..e29d41feac --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java @@ -0,0 +1,113 @@ +package com.github.lhpmatlab.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class MyArrayListTest { + private MyArrayList list; + + @Before + public void init(){ + list = new MyArrayList<>(); + } + + @Test + public void testEnsureCapacity() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.ensureCapacity(10); + assertEquals("ensureCapacity size is 10 ", list.size(),1); + } + + /** + * 在列表的末尾添加元素 + */ + @Test + public void testAddT() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.add("2"); + assertEquals("add list size ", list.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyLinkedListTest { + private MyLinkedList linkedList; + + @Before + public void before() throws Exception { + linkedList = new MyLinkedList<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: add(T t) + * + */ + @Test + public void testAddT() throws Exception { + assertEquals("init list size is 0 ", linkedList.size(), 0); + linkedList.add("1"); + linkedList.add("2"); + assertEquals("add list size ", linkedList.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyQueueTest { + private MyQueue queue; + + @Before + public void init() throws Exception { + queue = new MyQueue<>(); + } + + /** + * + * Method: enQueue(T t) + * + */ + @Test + public void testEnQueue() throws Exception { + queue.enQueue("1"); + assertEquals("size ", queue.size(), 1); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); +// queue.deQueue(); + assertEquals("dequeue element ",queue.deQueue(),"1"); + assertEquals("size ", queue.size(), 1); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("isEmpty method",queue.isEmpty(),true); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); + assertEquals("size method", queue.size(),2); + } + + +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java new file mode 100644 index 0000000000..a90af5d720 --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java @@ -0,0 +1,104 @@ +package com.github.lhpmatlab.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.*; + +/** +* MyStack Tester. +* +* @author +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyStackTest { + + MyStack stack; + + + @Before + public void init() throws Exception { + stack = new MyStack<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(T t) + * + */ + @Test + public void testPush() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + assertEquals("pust stack ", stack.size(),1); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + stack.pop(); + assertEquals("after pop ",stack.size(),1); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("peek ", stack.peek(),"2"); + } + + /** + *测试判空方法 + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("stack is empty ", stack.isEmpty(), true); + } + + /** + *测试判空方法,不为空的情况 + * Method: isEmpty() + * + */ + @Test + public void testIsNotEmpty() throws Exception { + stack.push("1"); + assertEquals("stack is empty ", stack.isEmpty(), false); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("size is 2", stack.size(), 2); + } + + +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..34d062b327 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java new file mode 100644 index 0000000000..7a13f14c83 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.ZhoufeifeiJAVA.coding2017.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/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..0666530694 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java @@ -0,0 +1,78 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o)throws RuntimeException{ + if(index > size) + throw new RuntimeException("index is bigger than the size of MyArrayList"); + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + if(index == size) + add(o); + else{ + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size ++; + } + private Object[] arrayGrow(Object[] src,int size){ + Object[] target = new Object[src.length+size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + public Object get(int index){ + if(index >= size) + return null; + else + return elementData[index]; + } + + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index>=size || index<0) + throw new IndexOutOfBoundsException("index is bigger than the size of MyArrayList"); + Object removeObject = elementData[index]; + for(int i=index;isize || index<0) + throw new IndexOutOfBoundsException("the index is bigger than the size of MyLinkedList"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0){ + newNode.next = head; + head = newNode; + } + else{ + int i = 0; + Node pointer = head; + while(isize-1) + return null; + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index --; + } + return pointer.data; + + } + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index<0 || index>size-1) + throw new IndexOutOfBoundsException("the index is not legal"); + Node pointer = head; + if(index == 0){ + head = head.next; + size --; + return pointer.data; + } + else{ + while(index>1){ + pointer = pointer.next; + index --; + } + Node temp = pointer.next; + pointer.next = pointer.next.next; + size --; + return temp.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + + private static class Node{ + Object data; + Node next; + } + private class MyLinkedListIterator implements Iterator{ + private Node pointer; + MyLinkedListIterator(){ + pointer = head; + } + public boolean hasNext() { + if(pointer.next != null) + return true; + else + return false; + } + public Object next() { + Node temp = pointer; + pointer = pointer.next; + return temp.data; + } + } + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + //public void showData(int index){ + // System.out.println("the data of "+index+" is "+get(index).data); + //} +} + + + + + + + diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..6d5ca14e70 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java @@ -0,0 +1,34 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class MyLinkedListTest{ + public static void main(String[] args){ + MyLinkedList al = new MyLinkedList(); + al.add("string0"); + al.add("string1"); + al.add("string2"); + al.add("string3"); + al.add("string4"); + al.add("string5"); + al.add("string6"); + al.add("string7"); + al.add("string8"); + al.add("string9"); + al.add("string10"); + al.add("string11"); + al.add("string12"); + al.add("string13"); + al.add("string14"); + al.add(11,"string10.5"); + sop(al.remove(4)); + sop(al.get(10)); + sop(al.get(11)); + sop(al.get(12)); + sop("the size of al is "+al.size()); + sop("the iterator method used,so the result is as follows:"); + for(Iterator it = al.iterator();it.hasNext();){ + sop(it.next()); + } + } + public static void sop(Object o){ + System.out.println(o); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java new file mode 100644 index 0000000000..0ce735f150 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java @@ -0,0 +1,26 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Queue { + private MyLinkedList llist = null; + Queue(){ + llist = new MyLinkedList(); + } + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + return llist.removeFirst(); + } + + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + + public int size(){ + return llist.size(); + } +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..95cc5117ff --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class QueueTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("String0"); + q.enQueue("String1"); + q.enQueue("String2"); + sop("the queue is not empty "+q.isEmpty()); + sop("the size of queue is "+q.size()); + sop("out queue "+q.deQueue()); + sop("out queue "+q.deQueue()); + sop("the size of queue is "+q.size()); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3992f89f86 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Stack { + private MyLinkedList llist = null; + Stack(){ + llist = new MyLinkedList(); + } + public void push(Object o){ + llist.add(o); + } + + public Object pop(){ + return llist.removeLast(); + } + + public Object peek(){ + return llist.get(llist.size()-1); + } + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + public int size(){ + return llist.size(); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..4bcb60e35d --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class StackTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Stack s = new Stack(); + s.push("String0"); + s.push("String1"); + s.push("String2"); + sop("the queue is not empty "+s.isEmpty()); + sop("the size of queue is "+s.size()); + sop("out queue "+s.pop()); + sop("just watch queue,not delete "+s.peek()); + sop("the size of queue is "+s.size()); + } +} \ No newline at end of file