From 088001650a6d2e0c4cd6c4c6762154d94a09e114 Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:15:28 +0800 Subject: [PATCH 1/6] work01 Imitation ArrayList LinkedList Stack Queue BinaryTree --- .../736464448/RemoteSystemsTempFiles/.project | 12 + group06/736464448/work01/.classpath | 7 + group06/736464448/work01/.gitignore | 1 + group06/736464448/work01/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/data_structure/MyArrayList.java | 167 ++++++++++++++ .../src/data_structure/MyBinaryTree.java | 198 +++++++++++++++++ .../src/data_structure/MyLinkedList.java | 209 ++++++++++++++++++ .../work01/src/data_structure/MyQueue.java | 21 ++ .../work01/src/data_structure/MyStack.java | 22 ++ .../test_data_structure/TestMyArrayList.java | 93 ++++++++ .../test_data_structure/TestMyBinaryTree.java | 59 +++++ .../test_data_structure/TestMyLinkedList.java | 111 ++++++++++ .../src/test_data_structure/TestMyQueue.java | 49 ++++ .../src/test_data_structure/TestMyStack.java | 60 +++++ 15 files changed, 1037 insertions(+) create mode 100644 group06/736464448/RemoteSystemsTempFiles/.project create mode 100644 group06/736464448/work01/.classpath create mode 100644 group06/736464448/work01/.gitignore create mode 100644 group06/736464448/work01/.project create mode 100644 group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs create mode 100644 group06/736464448/work01/src/data_structure/MyArrayList.java create mode 100644 group06/736464448/work01/src/data_structure/MyBinaryTree.java create mode 100644 group06/736464448/work01/src/data_structure/MyLinkedList.java create mode 100644 group06/736464448/work01/src/data_structure/MyQueue.java create mode 100644 group06/736464448/work01/src/data_structure/MyStack.java create mode 100644 group06/736464448/work01/src/test_data_structure/TestMyArrayList.java create mode 100644 group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java create mode 100644 group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java create mode 100644 group06/736464448/work01/src/test_data_structure/TestMyQueue.java create mode 100644 group06/736464448/work01/src/test_data_structure/TestMyStack.java diff --git a/group06/736464448/RemoteSystemsTempFiles/.project b/group06/736464448/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group06/736464448/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group06/736464448/work01/.classpath b/group06/736464448/work01/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group06/736464448/work01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/736464448/work01/.gitignore b/group06/736464448/work01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/736464448/work01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/736464448/work01/.project b/group06/736464448/work01/.project new file mode 100644 index 0000000000..a703634d61 --- /dev/null +++ b/group06/736464448/work01/.project @@ -0,0 +1,17 @@ + + + work01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs b/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group06/736464448/work01/src/data_structure/MyArrayList.java b/group06/736464448/work01/src/data_structure/MyArrayList.java new file mode 100644 index 0000000000..eb2cd98752 --- /dev/null +++ b/group06/736464448/work01/src/data_structure/MyArrayList.java @@ -0,0 +1,167 @@ +package data_structure; + + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.NoSuchElementException; + + +public class MyArrayList { + private int modCount; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + //���� + private int capacity; + //ʵ������Ԫ�س��� + private int size=0; + //����һ��˽�е�element���� + private Object[] elementData; + + public MyArrayList(){ + elementData=EMPTY_ELEMENTDATA; + capacity=0; + } + + public MyArrayList(int initialCapacity) { + + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: "+ + initialCapacity); + this.elementData = new Object[initialCapacity]; + capacity=initialCapacity; + } + + public void add(Object o){ + + + ensureCapacityInternal(size + 1); + elementData[size++] = o; + + + } + + public void add(int index ,Object o){ + + rangCheck(index); + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1,size - index); + elementData[index] =o; + size++; + + + + + } + public Object get(int index){ + rangCheck(index); + return elementData[index]; + } + public Object remove(int index){ + modCount++; + rangCheck(index); + Object o=null; + o=elementData[index] ; + int numMoved = size - index - 1; + if (numMoved > 0) + + System.arraycopy(elementData, index+1, elementData, index ,size - index-1); + elementData[--size] = null; + + + return o; + } + public int size(){ + return size; + + } + public int Capacity(){ + return capacity; + } + private void rangCheck(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +// private class MyIndexOutOfBoundsException extends RuntimeException{ +// @SuppressWarnings("unused") +// public MyIndexOutOfBoundsException(String e) { +// super(); +// } +// } + private void ensureCapacityInternal(int minCapacity) { + + modCount++; + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(10, minCapacity); + capacity=minCapacity; + } + if (minCapacity - elementData.length > 0) + + grow(minCapacity); + } + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + capacity=newCapacity; + + elementData = Arrays.copyOf(elementData, newCapacity); + } + public Iterator iterator() { + return new Itr(); + } + private class Itr implements Iterator { + int cursor; + int lastRet = -1; + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData[lastRet = i]; + + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + + } + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + +} diff --git a/group06/736464448/work01/src/data_structure/MyBinaryTree.java b/group06/736464448/work01/src/data_structure/MyBinaryTree.java new file mode 100644 index 0000000000..aed7ddbc8d --- /dev/null +++ b/group06/736464448/work01/src/data_structure/MyBinaryTree.java @@ -0,0 +1,198 @@ +package data_structure; + + + +public class MyBinaryTree { + + private BinaryTreeNode root; + private int size; + + + + public void add(int key,Object o) { + size++; + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); + if(parent==null) + root=newnode; + else{ + target=compareKey(key,parent); + + if (key < target.key) { + target.left = newnode; + newnode.top = target; + } else if(key > target.key){ + target.right = newnode; + newnode.top = target; + } + else{ + target.data=o; + size--; + } + + } + + } + public Object get(int key){ + BinaryTreeNode target=null; + target=search( key); + if(target==null) + return null; + else + return target.data; + } + private BinaryTreeNode search(int key){ + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + if(parent==null) + return null; + + else + target=compareKey(key,parent); + if (key == target.key) { + + return target; + } + return null; + } + public Object remove(int key){ + BinaryTreeNode replace=null; + BinaryTreeNode target=null; + BinaryTreeNode oldnode=null; + + target=search( key); + if(target==null) + return null; + else + { + oldnode=target; + if(target.left==null&&target.right==null){ + + changeParent( target,null); + target=null; + } + else if(target.left!=null&&target.right==null){ + // replace=next(target.left); + // target=replace; + // changeParent(target,replace); + // changeChild(target, replace); + // changeParent(replace,null); + // target=null; + + replace=target.left; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left==null&&target.right!=null){ +// replace=prev(target.right); +// target=replace; +// changeParent(target,replace); +// changeChild(target, replace); +// changeParent(replace,null); +// replace=null; + + replace=target.right; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left!=null&&target.right!=null){ + int prev=prev(target.right).key; + int next=next(target.left).key; + if((next-key)>(key-prev)) + replace=prev(target.right); + else + replace=next(target.left); + target=replace; + changeParent(target,replace); + changeChild(target, replace); + changeParent(replace,null); + replace=null; + } + } + size--; + return oldnode.data; + } + private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ + BinaryTreeNode targetparent=null; + targetparent=target.top; + if(targetparent.key>target.key) + targetparent.left=child; + else + targetparent.right=child; + + } + private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ + BinaryTreeNode targetleftchild=null; + BinaryTreeNode targetrightchild=null; + targetleftchild=target.left; + targetrightchild=target.right; + if(targetleftchild!=null) + targetleftchild.top=parent; + if(targetrightchild!=null) + targetrightchild.top=parent; + + } + //�ҵ�ǰ���ڵ� + private BinaryTreeNode prev(BinaryTreeNode target){ + // BinaryTreeNode prev=null; + while(target.left!=null){ + target=target.left; + } + return target; + + } + //�ҵ������ڵ� + private BinaryTreeNode next(BinaryTreeNode target){ +// BinaryTreeNode next=null; + while(target.right!=null){ + target=target.right; + } + return target; + + } + public int size(){ + + return size; + } + private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { + BinaryTreeNode parent=node; + while (parent != null) { + + if (key < parent.key&&parent.left!=null) { + parent = parent.left; + } else if (key > parent.key&&parent.right!=null) { + parent = parent.right; + } else { + return parent; + + } + } + return parent; + + + } + + + + + private static class BinaryTreeNode{ + Object data; + int key; + BinaryTreeNode left; + BinaryTreeNode right; + BinaryTreeNode top; + public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ + this.key=key; + this.data=o; + this.left=left; + this.right=right; + this.top=top; + + } + + + } +} diff --git a/group06/736464448/work01/src/data_structure/MyLinkedList.java b/group06/736464448/work01/src/data_structure/MyLinkedList.java new file mode 100644 index 0000000000..9ee94bbd61 --- /dev/null +++ b/group06/736464448/work01/src/data_structure/MyLinkedList.java @@ -0,0 +1,209 @@ +package data_structure; + + +import java.util.Iterator; +import java.util.NoSuchElementException; + + + +public class MyLinkedList { + private int size; + private Node head; + private Node last; + + public void add(Object o){ + + linkLast(o); + + } + + public Object get(int index){ + checkPositionIndex(index); + Node node=node(index); + return node.item; + } + public Object remove(int index){ + checkPositionIndex(index); + Node node=node(index); + isnull(node); + Object o=null; + Node before=null; + if(index==0){ + o=node.item; + node.next=head; + node=null; + } + else + { + before=node(index-1); + before.next=node.next; + o=node.item; + node=null; + + } + + return o; + } + public int size(){ + + return size; + } + public void addFirst(Object o){ + + linkFirst(o); + + + } + public void addLast(Object o){ + linkLast(o); + + } + public Object removeFirst(){ + Node f=head; + isnull(f); + final Node next=head.next; + Object o=f.item; + f=null; + head=next; + if(next==null) + last=null; + size--; + + return o; + } + //��������ö���Ҳ��ը + public Object removeLast(){ + Node l=last; + isnull(l); + Object o=null; + if(size>=2){ + final Node before=node(size-1); + o=l.item; + l=null; + last=before; + + if(before==null) + head=null; + } + else{ + o=l.item; + l=null; + last=null; + head=null; + } + + + size--; + return o; + + } + public Iterator iterator(){ + + + return new ListItr(); + } + + + private static class Node{ + Object item; + Node next; + Node(Object o, Node next){ + this.item=o; + this.next=next; + } + } + public void add(int index, Object o) { + checkPositionIndex(index); + + if (index == 0) + linkFirst(o); + else + linkNext(o, node(index-1)); + } + private void linkNext(Object o,Node node){ + final Node next=node.next; + final Node newnode=new Node(o,next); + node.next=newnode; + size++; + } + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + private void linkLast(Object o){ + final Node l=last; + final Node node=new Node(o,null); + last=node; + if(head==null) + head=node; + else + l.next=node; + size++; + } + private void linkFirst(Object o){ + final Node f=head; + final Node node=new Node(o,null); + head=node; + if(last==null) + last=node; + else + head.next=f; + size++; + } + private void isnull(Node node){ + if (node == null) + throw new NoSuchElementException(); + } + Node node(int index) { + // assert isElementIndex(index); + + + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + + + } + + private class ListItr implements Iterator { + private Node next=head; + private int nextIndex; + private Node lastReturned = null; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + //������ѭ���� +// if(nextIndex==size){ +// next=head; +// nextIndex=0; +// } + return lastReturned.item; + } + + @Override + public void remove() { + // TODO Auto-generated method stub + + } + } + + + +} diff --git a/group06/736464448/work01/src/data_structure/MyQueue.java b/group06/736464448/work01/src/data_structure/MyQueue.java new file mode 100644 index 0000000000..8e3f50e0bb --- /dev/null +++ b/group06/736464448/work01/src/data_structure/MyQueue.java @@ -0,0 +1,21 @@ +package data_structure; + +public class MyQueue { + private MyLinkedList elementData =new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/736464448/work01/src/data_structure/MyStack.java b/group06/736464448/work01/src/data_structure/MyStack.java new file mode 100644 index 0000000000..f442468fb2 --- /dev/null +++ b/group06/736464448/work01/src/data_structure/MyStack.java @@ -0,0 +1,22 @@ +package data_structure; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java b/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java new file mode 100644 index 0000000000..6ce24c90a8 --- /dev/null +++ b/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java @@ -0,0 +1,93 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyArrayList; + +public class TestMyArrayList { + MyArrayList list; + + @Before + public void setUp() throws Exception { + list=new MyArrayList(); + System.out.println("begin"); + } + + @After + public void tearDown() throws Exception { + System.out.println("end"); + } + + @Test + public void testMyArrayList() { + + Assert.assertEquals(0, list.Capacity()); + } + + @Test + public void testAddObject() { + list.add(new Integer(10)); + Assert.assertEquals(10, list.get(0)); + } + + @Test + public void testAddIntObject() { + list.add(1); + list.add(1); + list.add(1); + list.add(1,2); + Assert.assertEquals(2, list.get(1)); + + + } + + @Test + public void testGet() { + list.add(1); + list.add(2); + Assert.assertEquals(2, list.get(1)); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(2, list.remove(1)); + + } + + @Test + public void testSize() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testCapacity() { + list=new MyArrayList(5); + Assert.assertEquals(5, list.Capacity()); + + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + Iterator itr=list.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + +} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java b/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java new file mode 100644 index 0000000000..66e6b55f43 --- /dev/null +++ b/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java @@ -0,0 +1,59 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyBinaryTree; + + +public class TestMyBinaryTree { + MyBinaryTree bt; + + @Before + public void setUp() throws Exception { + System.out.println("��ʼ����"); + bt=new MyBinaryTree(); + } + + @After + public void tearDown() throws Exception { + System.out.println("��������"); + } + + @Test + public void testAdd() { + bt.add(1, "c"); + + } + + @Test + public void testGet() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.get(2)); + } + + @Test + public void testRemove() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.remove(2)); + + Assert.assertEquals(2, bt.size()); + } + + @Test + public void testSize() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(3, bt.size()); + } + +} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java b/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java new file mode 100644 index 0000000000..c6093e9d91 --- /dev/null +++ b/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java @@ -0,0 +1,111 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyLinkedList; + +public class TestMyLinkedList { + MyLinkedList link=null; + + @Before + public void setUp() throws Exception { + link=new MyLinkedList(); + System.out.println("��ʼ����"); + } + + @After + public void tearDown() throws Exception { + System.out.println("��������"); + } + + @Test + public void testAddObject() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testGet() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testRemove() { + link.add(1); + Assert.assertEquals(1,link.remove(0)); + } + + @Test + public void testSize() { + link.add(1); + Assert.assertEquals(1,link.size()); + } + + @Test + public void testAddFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.get(0)); + } + + @Test + public void testAddLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.get(link.size()-1)); + } + + @Test + public void testRemoveFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.removeFirst()); + } + + @Test + public void testRemoveLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.removeLast()); + } + + @Test + public void testIterator() { + link.add(1); + link.add(2); + link.add(3); + Iterator itr=link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + + @Test + public void testAddIntObject() { + link.add(1); + link.add(1); + link.add(1); + link.add(2, 3); + Assert.assertEquals(3,link.get(2)); + } + + + +} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyQueue.java b/group06/736464448/work01/src/test_data_structure/TestMyQueue.java new file mode 100644 index 0000000000..18fbdae5d7 --- /dev/null +++ b/group06/736464448/work01/src/test_data_structure/TestMyQueue.java @@ -0,0 +1,49 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyQueue; + +public class TestMyQueue { + MyQueue queue; + + @Before + public void setUp() throws Exception { + queue =new MyQueue(); + System.out.println("��ʼ����"); + } + + @After + public void tearDown() throws Exception { + System.out.println("��������"); + } + + @Test + public void testEnQueue() { + queue.enQueue("hello"); + } + + @Test + public void testDeQueue() { + queue.enQueue("hello"); + queue.enQueue("world"); + Assert.assertEquals("hello",queue.deQueue()); + } + + @Test + public void testIsEmpty() { + queue.enQueue("hello"); + Assert.assertEquals(false,queue.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0,queue.size()); + } + +} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyStack.java b/group06/736464448/work01/src/test_data_structure/TestMyStack.java new file mode 100644 index 0000000000..bea66c25d6 --- /dev/null +++ b/group06/736464448/work01/src/test_data_structure/TestMyStack.java @@ -0,0 +1,60 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyStack; + +public class TestMyStack { + MyStack mystack; + + @Before + public void setUp() throws Exception { + mystack=new MyStack(); + System.out.println("��ʼ����"); + } + + @After + public void tearDown() throws Exception { + System.out.println("��������"); + } + + @Test + public void testPush() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + } + + @Test + public void testPop() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + Assert.assertEquals("World", (String)mystack.pop()); + Assert.assertEquals(",", (String)mystack.pop()); + + } + + @Test + public void testPeek() { + mystack.push("Hello"); + Assert.assertEquals("Hello", (String)mystack.peek()); + } + + @Test + public void testIsEmpty() { + + Assert.assertEquals(true, mystack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, mystack.size()); + } + +} From a0a102f39a54a7eb0e92d636e99db68045261de8 Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:18:14 +0800 Subject: [PATCH 2/6] Delete .project --- group06/736464448/RemoteSystemsTempFiles/.project | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 group06/736464448/RemoteSystemsTempFiles/.project diff --git a/group06/736464448/RemoteSystemsTempFiles/.project b/group06/736464448/RemoteSystemsTempFiles/.project deleted file mode 100644 index 5447a64fa9..0000000000 --- a/group06/736464448/RemoteSystemsTempFiles/.project +++ /dev/null @@ -1,12 +0,0 @@ - - - RemoteSystemsTempFiles - - - - - - - org.eclipse.rse.ui.remoteSystemsTempNature - - From 4d486d5fb7e84f1582504afdc0fc625dc52a02ea Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:24:40 +0800 Subject: [PATCH 3/6] delete success haha --- .../736464448/RemoteSystemsTempFiles/.project | 12 ------------ group06/736464448/work01/.classpath | 7 ------- group06/736464448/work01/.gitignore | 1 - group06/736464448/work01/.project | 17 ----------------- .../work01/.settings/org.eclipse.jdt.core.prefs | 11 ----------- 5 files changed, 48 deletions(-) delete mode 100644 group06/736464448/RemoteSystemsTempFiles/.project delete mode 100644 group06/736464448/work01/.classpath delete mode 100644 group06/736464448/work01/.gitignore delete mode 100644 group06/736464448/work01/.project delete mode 100644 group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs diff --git a/group06/736464448/RemoteSystemsTempFiles/.project b/group06/736464448/RemoteSystemsTempFiles/.project deleted file mode 100644 index 5447a64fa9..0000000000 --- a/group06/736464448/RemoteSystemsTempFiles/.project +++ /dev/null @@ -1,12 +0,0 @@ - - - RemoteSystemsTempFiles - - - - - - - org.eclipse.rse.ui.remoteSystemsTempNature - - diff --git a/group06/736464448/work01/.classpath b/group06/736464448/work01/.classpath deleted file mode 100644 index 373dce4005..0000000000 --- a/group06/736464448/work01/.classpath +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/group06/736464448/work01/.gitignore b/group06/736464448/work01/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group06/736464448/work01/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/group06/736464448/work01/.project b/group06/736464448/work01/.project deleted file mode 100644 index a703634d61..0000000000 --- a/group06/736464448/work01/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - work01 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs b/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 3a21537071..0000000000 --- a/group06/736464448/work01/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 From 36245f76dedfde9281bf03011c524fee2f3206da Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:30:42 +0800 Subject: [PATCH 4/6] haha hah --- .../src/data_structure/MyArrayList.java | 167 -------------- .../src/data_structure/MyBinaryTree.java | 198 ----------------- .../src/data_structure/MyLinkedList.java | 209 ------------------ .../work01/src/data_structure/MyQueue.java | 21 -- .../work01/src/data_structure/MyStack.java | 22 -- .../test_data_structure/TestMyArrayList.java | 93 -------- .../test_data_structure/TestMyBinaryTree.java | 59 ----- .../test_data_structure/TestMyLinkedList.java | 111 ---------- .../src/test_data_structure/TestMyQueue.java | 49 ---- .../src/test_data_structure/TestMyStack.java | 60 ----- 10 files changed, 989 deletions(-) delete mode 100644 group06/736464448/work01/src/data_structure/MyArrayList.java delete mode 100644 group06/736464448/work01/src/data_structure/MyBinaryTree.java delete mode 100644 group06/736464448/work01/src/data_structure/MyLinkedList.java delete mode 100644 group06/736464448/work01/src/data_structure/MyQueue.java delete mode 100644 group06/736464448/work01/src/data_structure/MyStack.java delete mode 100644 group06/736464448/work01/src/test_data_structure/TestMyArrayList.java delete mode 100644 group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java delete mode 100644 group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java delete mode 100644 group06/736464448/work01/src/test_data_structure/TestMyQueue.java delete mode 100644 group06/736464448/work01/src/test_data_structure/TestMyStack.java diff --git a/group06/736464448/work01/src/data_structure/MyArrayList.java b/group06/736464448/work01/src/data_structure/MyArrayList.java deleted file mode 100644 index eb2cd98752..0000000000 --- a/group06/736464448/work01/src/data_structure/MyArrayList.java +++ /dev/null @@ -1,167 +0,0 @@ -package data_structure; - - -import java.util.Arrays; -import java.util.ConcurrentModificationException; -import java.util.Iterator; -import java.util.NoSuchElementException; - - -public class MyArrayList { - private int modCount; - - private static final Object[] EMPTY_ELEMENTDATA = {}; - //���� - private int capacity; - //ʵ������Ԫ�س��� - private int size=0; - //����һ��˽�е�element���� - private Object[] elementData; - - public MyArrayList(){ - elementData=EMPTY_ELEMENTDATA; - capacity=0; - } - - public MyArrayList(int initialCapacity) { - - if (initialCapacity < 0) - throw new IllegalArgumentException("Illegal Capacity: "+ - initialCapacity); - this.elementData = new Object[initialCapacity]; - capacity=initialCapacity; - } - - public void add(Object o){ - - - ensureCapacityInternal(size + 1); - elementData[size++] = o; - - - } - - public void add(int index ,Object o){ - - rangCheck(index); - ensureCapacityInternal(size + 1); - - System.arraycopy(elementData, index, elementData, index + 1,size - index); - elementData[index] =o; - size++; - - - - - } - public Object get(int index){ - rangCheck(index); - return elementData[index]; - } - public Object remove(int index){ - modCount++; - rangCheck(index); - Object o=null; - o=elementData[index] ; - int numMoved = size - index - 1; - if (numMoved > 0) - - System.arraycopy(elementData, index+1, elementData, index ,size - index-1); - elementData[--size] = null; - - - return o; - } - public int size(){ - return size; - - } - public int Capacity(){ - return capacity; - } - private void rangCheck(int index){ - if(index > size || index < 0) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - private String outOfBoundsMsg(int index) { - return "Index: "+index+", Size: "+size; - } - -// private class MyIndexOutOfBoundsException extends RuntimeException{ -// @SuppressWarnings("unused") -// public MyIndexOutOfBoundsException(String e) { -// super(); -// } -// } - private void ensureCapacityInternal(int minCapacity) { - - modCount++; - if (elementData == EMPTY_ELEMENTDATA) { - minCapacity = Math.max(10, minCapacity); - capacity=minCapacity; - } - if (minCapacity - elementData.length > 0) - - grow(minCapacity); - } - private void grow(int minCapacity) { - // overflow-conscious code - int oldCapacity = elementData.length; - int newCapacity = oldCapacity + (oldCapacity >> 1); - if (newCapacity - minCapacity < 0) - newCapacity = minCapacity; - capacity=newCapacity; - - elementData = Arrays.copyOf(elementData, newCapacity); - } - public Iterator iterator() { - return new Itr(); - } - private class Itr implements Iterator { - int cursor; - int lastRet = -1; - int expectedModCount = modCount; - - public boolean hasNext() { - return cursor != size; - } - - @Override - public Object next() { - - int i = cursor; - if (i >= size) - throw new NoSuchElementException(); - Object[] elementData = MyArrayList.this.elementData; - if (i >= elementData.length) - throw new ConcurrentModificationException(); - cursor = i + 1; - return elementData[lastRet = i]; - - } - - @Override - public void remove() { - if (lastRet < 0) - throw new IllegalStateException(); - checkForComodification(); - - try { - MyArrayList.this.remove(lastRet); - cursor = lastRet; - lastRet = -1; - expectedModCount = modCount; - } catch (IndexOutOfBoundsException ex) { - throw new ConcurrentModificationException(); - } - - } - final void checkForComodification() { - if (modCount != expectedModCount) - throw new ConcurrentModificationException(); - } - } - - -} diff --git a/group06/736464448/work01/src/data_structure/MyBinaryTree.java b/group06/736464448/work01/src/data_structure/MyBinaryTree.java deleted file mode 100644 index aed7ddbc8d..0000000000 --- a/group06/736464448/work01/src/data_structure/MyBinaryTree.java +++ /dev/null @@ -1,198 +0,0 @@ -package data_structure; - - - -public class MyBinaryTree { - - private BinaryTreeNode root; - private int size; - - - - public void add(int key,Object o) { - size++; - BinaryTreeNode target=null; - final BinaryTreeNode parent=root; - final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); - if(parent==null) - root=newnode; - else{ - target=compareKey(key,parent); - - if (key < target.key) { - target.left = newnode; - newnode.top = target; - } else if(key > target.key){ - target.right = newnode; - newnode.top = target; - } - else{ - target.data=o; - size--; - } - - } - - } - public Object get(int key){ - BinaryTreeNode target=null; - target=search( key); - if(target==null) - return null; - else - return target.data; - } - private BinaryTreeNode search(int key){ - BinaryTreeNode target=null; - final BinaryTreeNode parent=root; - if(parent==null) - return null; - - else - target=compareKey(key,parent); - if (key == target.key) { - - return target; - } - return null; - } - public Object remove(int key){ - BinaryTreeNode replace=null; - BinaryTreeNode target=null; - BinaryTreeNode oldnode=null; - - target=search( key); - if(target==null) - return null; - else - { - oldnode=target; - if(target.left==null&&target.right==null){ - - changeParent( target,null); - target=null; - } - else if(target.left!=null&&target.right==null){ - // replace=next(target.left); - // target=replace; - // changeParent(target,replace); - // changeChild(target, replace); - // changeParent(replace,null); - // target=null; - - replace=target.left; - changeParent(target,replace); - replace.top=target.top; - target=null; - } - else if(target.left==null&&target.right!=null){ -// replace=prev(target.right); -// target=replace; -// changeParent(target,replace); -// changeChild(target, replace); -// changeParent(replace,null); -// replace=null; - - replace=target.right; - changeParent(target,replace); - replace.top=target.top; - target=null; - } - else if(target.left!=null&&target.right!=null){ - int prev=prev(target.right).key; - int next=next(target.left).key; - if((next-key)>(key-prev)) - replace=prev(target.right); - else - replace=next(target.left); - target=replace; - changeParent(target,replace); - changeChild(target, replace); - changeParent(replace,null); - replace=null; - } - } - size--; - return oldnode.data; - } - private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ - BinaryTreeNode targetparent=null; - targetparent=target.top; - if(targetparent.key>target.key) - targetparent.left=child; - else - targetparent.right=child; - - } - private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ - BinaryTreeNode targetleftchild=null; - BinaryTreeNode targetrightchild=null; - targetleftchild=target.left; - targetrightchild=target.right; - if(targetleftchild!=null) - targetleftchild.top=parent; - if(targetrightchild!=null) - targetrightchild.top=parent; - - } - //�ҵ�ǰ���ڵ� - private BinaryTreeNode prev(BinaryTreeNode target){ - // BinaryTreeNode prev=null; - while(target.left!=null){ - target=target.left; - } - return target; - - } - //�ҵ������ڵ� - private BinaryTreeNode next(BinaryTreeNode target){ -// BinaryTreeNode next=null; - while(target.right!=null){ - target=target.right; - } - return target; - - } - public int size(){ - - return size; - } - private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { - BinaryTreeNode parent=node; - while (parent != null) { - - if (key < parent.key&&parent.left!=null) { - parent = parent.left; - } else if (key > parent.key&&parent.right!=null) { - parent = parent.right; - } else { - return parent; - - } - } - return parent; - - - } - - - - - private static class BinaryTreeNode{ - Object data; - int key; - BinaryTreeNode left; - BinaryTreeNode right; - BinaryTreeNode top; - public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ - this.key=key; - this.data=o; - this.left=left; - this.right=right; - this.top=top; - - } - - - } -} diff --git a/group06/736464448/work01/src/data_structure/MyLinkedList.java b/group06/736464448/work01/src/data_structure/MyLinkedList.java deleted file mode 100644 index 9ee94bbd61..0000000000 --- a/group06/736464448/work01/src/data_structure/MyLinkedList.java +++ /dev/null @@ -1,209 +0,0 @@ -package data_structure; - - -import java.util.Iterator; -import java.util.NoSuchElementException; - - - -public class MyLinkedList { - private int size; - private Node head; - private Node last; - - public void add(Object o){ - - linkLast(o); - - } - - public Object get(int index){ - checkPositionIndex(index); - Node node=node(index); - return node.item; - } - public Object remove(int index){ - checkPositionIndex(index); - Node node=node(index); - isnull(node); - Object o=null; - Node before=null; - if(index==0){ - o=node.item; - node.next=head; - node=null; - } - else - { - before=node(index-1); - before.next=node.next; - o=node.item; - node=null; - - } - - return o; - } - public int size(){ - - return size; - } - public void addFirst(Object o){ - - linkFirst(o); - - - } - public void addLast(Object o){ - linkLast(o); - - } - public Object removeFirst(){ - Node f=head; - isnull(f); - final Node next=head.next; - Object o=f.item; - f=null; - head=next; - if(next==null) - last=null; - size--; - - return o; - } - //��������ö���Ҳ��ը - public Object removeLast(){ - Node l=last; - isnull(l); - Object o=null; - if(size>=2){ - final Node before=node(size-1); - o=l.item; - l=null; - last=before; - - if(before==null) - head=null; - } - else{ - o=l.item; - l=null; - last=null; - head=null; - } - - - size--; - return o; - - } - public Iterator iterator(){ - - - return new ListItr(); - } - - - private static class Node{ - Object item; - Node next; - Node(Object o, Node next){ - this.item=o; - this.next=next; - } - } - public void add(int index, Object o) { - checkPositionIndex(index); - - if (index == 0) - linkFirst(o); - else - linkNext(o, node(index-1)); - } - private void linkNext(Object o,Node node){ - final Node next=node.next; - final Node newnode=new Node(o,next); - node.next=newnode; - size++; - } - private void checkPositionIndex(int index) { - if (!isPositionIndex(index)) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - private boolean isPositionIndex(int index) { - return index >= 0 && index <= size; - } - private String outOfBoundsMsg(int index) { - return "Index: "+index+", Size: "+size; - } - private void linkLast(Object o){ - final Node l=last; - final Node node=new Node(o,null); - last=node; - if(head==null) - head=node; - else - l.next=node; - size++; - } - private void linkFirst(Object o){ - final Node f=head; - final Node node=new Node(o,null); - head=node; - if(last==null) - last=node; - else - head.next=f; - size++; - } - private void isnull(Node node){ - if (node == null) - throw new NoSuchElementException(); - } - Node node(int index) { - // assert isElementIndex(index); - - - Node x = head; - for (int i = 0; i < index; i++) - x = x.next; - return x; - - - } - - private class ListItr implements Iterator { - private Node next=head; - private int nextIndex; - private Node lastReturned = null; - - public boolean hasNext() { - return nextIndex < size; - } - - public Object next() { - - if (!hasNext()) - throw new NoSuchElementException(); - - lastReturned = next; - next = next.next; - nextIndex++; - //������ѭ���� -// if(nextIndex==size){ -// next=head; -// nextIndex=0; -// } - return lastReturned.item; - } - - @Override - public void remove() { - // TODO Auto-generated method stub - - } - } - - - -} diff --git a/group06/736464448/work01/src/data_structure/MyQueue.java b/group06/736464448/work01/src/data_structure/MyQueue.java deleted file mode 100644 index 8e3f50e0bb..0000000000 --- a/group06/736464448/work01/src/data_structure/MyQueue.java +++ /dev/null @@ -1,21 +0,0 @@ -package data_structure; - -public class MyQueue { - private MyLinkedList elementData =new MyLinkedList(); - public void enQueue(Object o){ - elementData.add(o); - } - - public Object deQueue(){ - return elementData.removeFirst(); - } - - public boolean isEmpty(){ - return elementData.size()==0; - } - - public int size(){ - return elementData.size(); - } - -} diff --git a/group06/736464448/work01/src/data_structure/MyStack.java b/group06/736464448/work01/src/data_structure/MyStack.java deleted file mode 100644 index f442468fb2..0000000000 --- a/group06/736464448/work01/src/data_structure/MyStack.java +++ /dev/null @@ -1,22 +0,0 @@ -package data_structure; - -public class MyStack { - private MyArrayList elementData = new MyArrayList(); - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - return elementData.remove(elementData.size()-1); - } - - public Object peek(){ - return elementData.get(0); - } - public boolean isEmpty(){ - return elementData.size()==0; - } - public int size(){ - return elementData.size(); - } -} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java b/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java deleted file mode 100644 index 6ce24c90a8..0000000000 --- a/group06/736464448/work01/src/test_data_structure/TestMyArrayList.java +++ /dev/null @@ -1,93 +0,0 @@ -package test_data_structure; - - - -import java.util.Iterator; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import data_structure.MyArrayList; - -public class TestMyArrayList { - MyArrayList list; - - @Before - public void setUp() throws Exception { - list=new MyArrayList(); - System.out.println("begin"); - } - - @After - public void tearDown() throws Exception { - System.out.println("end"); - } - - @Test - public void testMyArrayList() { - - Assert.assertEquals(0, list.Capacity()); - } - - @Test - public void testAddObject() { - list.add(new Integer(10)); - Assert.assertEquals(10, list.get(0)); - } - - @Test - public void testAddIntObject() { - list.add(1); - list.add(1); - list.add(1); - list.add(1,2); - Assert.assertEquals(2, list.get(1)); - - - } - - @Test - public void testGet() { - list.add(1); - list.add(2); - Assert.assertEquals(2, list.get(1)); - } - - @Test - public void testRemove() { - list.add(1); - list.add(2); - list.add(3); - Assert.assertEquals(2, list.remove(1)); - - } - - @Test - public void testSize() { - list.add(1); - list.add(2); - list.add(3); - Assert.assertEquals(3, list.size()); - } - - @Test - public void testCapacity() { - list=new MyArrayList(5); - Assert.assertEquals(5, list.Capacity()); - - } - @Test - public void testIterator(){ - list.add(1); - list.add(2); - list.add(3); - Iterator itr=list.iterator(); - while(itr.hasNext()){ - System.out.println(itr.next()); - } - - } - -} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java b/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java deleted file mode 100644 index 66e6b55f43..0000000000 --- a/group06/736464448/work01/src/test_data_structure/TestMyBinaryTree.java +++ /dev/null @@ -1,59 +0,0 @@ -package test_data_structure; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import data_structure.MyBinaryTree; - - -public class TestMyBinaryTree { - MyBinaryTree bt; - - @Before - public void setUp() throws Exception { - System.out.println("��ʼ����"); - bt=new MyBinaryTree(); - } - - @After - public void tearDown() throws Exception { - System.out.println("��������"); - } - - @Test - public void testAdd() { - bt.add(1, "c"); - - } - - @Test - public void testGet() { - bt.add(1, 1); - bt.add(2, 2); - bt.add(3, 3); - Assert.assertEquals(2, bt.get(2)); - } - - @Test - public void testRemove() { - bt.add(1, 1); - bt.add(2, 2); - bt.add(3, 3); - Assert.assertEquals(2, bt.remove(2)); - - Assert.assertEquals(2, bt.size()); - } - - @Test - public void testSize() { - bt.add(1, 1); - bt.add(2, 2); - bt.add(3, 3); - Assert.assertEquals(3, bt.size()); - } - -} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java b/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java deleted file mode 100644 index c6093e9d91..0000000000 --- a/group06/736464448/work01/src/test_data_structure/TestMyLinkedList.java +++ /dev/null @@ -1,111 +0,0 @@ -package test_data_structure; - - - -import java.util.Iterator; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import data_structure.MyLinkedList; - -public class TestMyLinkedList { - MyLinkedList link=null; - - @Before - public void setUp() throws Exception { - link=new MyLinkedList(); - System.out.println("��ʼ����"); - } - - @After - public void tearDown() throws Exception { - System.out.println("��������"); - } - - @Test - public void testAddObject() { - link.add(1); - Assert.assertEquals(1,link.get(0)); - } - - @Test - public void testGet() { - link.add(1); - Assert.assertEquals(1,link.get(0)); - } - - @Test - public void testRemove() { - link.add(1); - Assert.assertEquals(1,link.remove(0)); - } - - @Test - public void testSize() { - link.add(1); - Assert.assertEquals(1,link.size()); - } - - @Test - public void testAddFirst() { - link.add(1); - link.add(1); - link.add(1); - link.addFirst(2); - Assert.assertEquals(2,link.get(0)); - } - - @Test - public void testAddLast() { - link.add(1); - link.add(1); - link.add(1); - link.addLast(2); - Assert.assertEquals(2,link.get(link.size()-1)); - } - - @Test - public void testRemoveFirst() { - link.add(1); - link.add(1); - link.add(1); - link.addFirst(2); - Assert.assertEquals(2,link.removeFirst()); - } - - @Test - public void testRemoveLast() { - link.add(1); - link.add(1); - link.add(1); - link.addLast(2); - Assert.assertEquals(2,link.removeLast()); - } - - @Test - public void testIterator() { - link.add(1); - link.add(2); - link.add(3); - Iterator itr=link.iterator(); - while(itr.hasNext()){ - System.out.println(itr.next()); - } - - } - - @Test - public void testAddIntObject() { - link.add(1); - link.add(1); - link.add(1); - link.add(2, 3); - Assert.assertEquals(3,link.get(2)); - } - - - -} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyQueue.java b/group06/736464448/work01/src/test_data_structure/TestMyQueue.java deleted file mode 100644 index 18fbdae5d7..0000000000 --- a/group06/736464448/work01/src/test_data_structure/TestMyQueue.java +++ /dev/null @@ -1,49 +0,0 @@ -package test_data_structure; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import data_structure.MyQueue; - -public class TestMyQueue { - MyQueue queue; - - @Before - public void setUp() throws Exception { - queue =new MyQueue(); - System.out.println("��ʼ����"); - } - - @After - public void tearDown() throws Exception { - System.out.println("��������"); - } - - @Test - public void testEnQueue() { - queue.enQueue("hello"); - } - - @Test - public void testDeQueue() { - queue.enQueue("hello"); - queue.enQueue("world"); - Assert.assertEquals("hello",queue.deQueue()); - } - - @Test - public void testIsEmpty() { - queue.enQueue("hello"); - Assert.assertEquals(false,queue.isEmpty()); - } - - @Test - public void testSize() { - Assert.assertEquals(0,queue.size()); - } - -} diff --git a/group06/736464448/work01/src/test_data_structure/TestMyStack.java b/group06/736464448/work01/src/test_data_structure/TestMyStack.java deleted file mode 100644 index bea66c25d6..0000000000 --- a/group06/736464448/work01/src/test_data_structure/TestMyStack.java +++ /dev/null @@ -1,60 +0,0 @@ -package test_data_structure; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import data_structure.MyStack; - -public class TestMyStack { - MyStack mystack; - - @Before - public void setUp() throws Exception { - mystack=new MyStack(); - System.out.println("��ʼ����"); - } - - @After - public void tearDown() throws Exception { - System.out.println("��������"); - } - - @Test - public void testPush() { - mystack.push("Hello"); - mystack.push(","); - mystack.push("World"); - } - - @Test - public void testPop() { - mystack.push("Hello"); - mystack.push(","); - mystack.push("World"); - Assert.assertEquals("World", (String)mystack.pop()); - Assert.assertEquals(",", (String)mystack.pop()); - - } - - @Test - public void testPeek() { - mystack.push("Hello"); - Assert.assertEquals("Hello", (String)mystack.peek()); - } - - @Test - public void testIsEmpty() { - - Assert.assertEquals(true, mystack.isEmpty()); - } - - @Test - public void testSize() { - Assert.assertEquals(0, mystack.size()); - } - -} From c4cbad188612a77c7c33ecdddd993d922af3313f Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:34:26 +0800 Subject: [PATCH 5/6] This time it's right hah --- .../src/data_structure/MyArrayList.java | 167 ++++++++++++++ .../src/data_structure/MyBinaryTree.java | 198 +++++++++++++++++ .../src/data_structure/MyLinkedList.java | 209 ++++++++++++++++++ .../736464448/src/data_structure/MyQueue.java | 21 ++ .../736464448/src/data_structure/MyStack.java | 22 ++ .../test_data_structure/TestMyArrayList.java | 93 ++++++++ .../test_data_structure/TestMyBinaryTree.java | 59 +++++ .../test_data_structure/TestMyLinkedList.java | 111 ++++++++++ .../src/test_data_structure/TestMyQueue.java | 49 ++++ .../src/test_data_structure/TestMyStack.java | 60 +++++ 10 files changed, 989 insertions(+) create mode 100644 group06/736464448/src/data_structure/MyArrayList.java create mode 100644 group06/736464448/src/data_structure/MyBinaryTree.java create mode 100644 group06/736464448/src/data_structure/MyLinkedList.java create mode 100644 group06/736464448/src/data_structure/MyQueue.java create mode 100644 group06/736464448/src/data_structure/MyStack.java create mode 100644 group06/736464448/src/test_data_structure/TestMyArrayList.java create mode 100644 group06/736464448/src/test_data_structure/TestMyBinaryTree.java create mode 100644 group06/736464448/src/test_data_structure/TestMyLinkedList.java create mode 100644 group06/736464448/src/test_data_structure/TestMyQueue.java create mode 100644 group06/736464448/src/test_data_structure/TestMyStack.java diff --git a/group06/736464448/src/data_structure/MyArrayList.java b/group06/736464448/src/data_structure/MyArrayList.java new file mode 100644 index 0000000000..1b47dd89b6 --- /dev/null +++ b/group06/736464448/src/data_structure/MyArrayList.java @@ -0,0 +1,167 @@ +package data_structure; + + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.NoSuchElementException; + + +public class MyArrayList { + private int modCount; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + //容量 + private int capacity; + //实际添加元素长度 + private int size=0; + //定义一个私有的element数组 + private Object[] elementData; + + public MyArrayList(){ + elementData=EMPTY_ELEMENTDATA; + capacity=0; + } + + public MyArrayList(int initialCapacity) { + + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: "+ + initialCapacity); + this.elementData = new Object[initialCapacity]; + capacity=initialCapacity; + } + + public void add(Object o){ + + + ensureCapacityInternal(size + 1); + elementData[size++] = o; + + + } + + public void add(int index ,Object o){ + + rangCheck(index); + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1,size - index); + elementData[index] =o; + size++; + + + + + } + public Object get(int index){ + rangCheck(index); + return elementData[index]; + } + public Object remove(int index){ + modCount++; + rangCheck(index); + Object o=null; + o=elementData[index] ; + int numMoved = size - index - 1; + if (numMoved > 0) + + System.arraycopy(elementData, index+1, elementData, index ,size - index-1); + elementData[--size] = null; + + + return o; + } + public int size(){ + return size; + + } + public int Capacity(){ + return capacity; + } + private void rangCheck(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +// private class MyIndexOutOfBoundsException extends RuntimeException{ +// @SuppressWarnings("unused") +// public MyIndexOutOfBoundsException(String e) { +// super(); +// } +// } + private void ensureCapacityInternal(int minCapacity) { + + modCount++; + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(10, minCapacity); + capacity=minCapacity; + } + if (minCapacity - elementData.length > 0) + + grow(minCapacity); + } + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + capacity=newCapacity; + + elementData = Arrays.copyOf(elementData, newCapacity); + } + public Iterator iterator() { + return new Itr(); + } + private class Itr implements Iterator { + int cursor; + int lastRet = -1; + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData[lastRet = i]; + + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + + } + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + +} diff --git a/group06/736464448/src/data_structure/MyBinaryTree.java b/group06/736464448/src/data_structure/MyBinaryTree.java new file mode 100644 index 0000000000..f7ba05f9cd --- /dev/null +++ b/group06/736464448/src/data_structure/MyBinaryTree.java @@ -0,0 +1,198 @@ +package data_structure; + + + +public class MyBinaryTree { + + private BinaryTreeNode root; + private int size; + + + + public void add(int key,Object o) { + size++; + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); + if(parent==null) + root=newnode; + else{ + target=compareKey(key,parent); + + if (key < target.key) { + target.left = newnode; + newnode.top = target; + } else if(key > target.key){ + target.right = newnode; + newnode.top = target; + } + else{ + target.data=o; + size--; + } + + } + + } + public Object get(int key){ + BinaryTreeNode target=null; + target=search( key); + if(target==null) + return null; + else + return target.data; + } + private BinaryTreeNode search(int key){ + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + if(parent==null) + return null; + + else + target=compareKey(key,parent); + if (key == target.key) { + + return target; + } + return null; + } + public Object remove(int key){ + BinaryTreeNode replace=null; + BinaryTreeNode target=null; + BinaryTreeNode oldnode=null; + + target=search( key); + if(target==null) + return null; + else + { + oldnode=target; + if(target.left==null&&target.right==null){ + + changeParent( target,null); + target=null; + } + else if(target.left!=null&&target.right==null){ + // replace=next(target.left); + // target=replace; + // changeParent(target,replace); + // changeChild(target, replace); + // changeParent(replace,null); + // target=null; + + replace=target.left; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left==null&&target.right!=null){ +// replace=prev(target.right); +// target=replace; +// changeParent(target,replace); +// changeChild(target, replace); +// changeParent(replace,null); +// replace=null; + + replace=target.right; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left!=null&&target.right!=null){ + int prev=prev(target.right).key; + int next=next(target.left).key; + if((next-key)>(key-prev)) + replace=prev(target.right); + else + replace=next(target.left); + target=replace; + changeParent(target,replace); + changeChild(target, replace); + changeParent(replace,null); + replace=null; + } + } + size--; + return oldnode.data; + } + private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ + BinaryTreeNode targetparent=null; + targetparent=target.top; + if(targetparent.key>target.key) + targetparent.left=child; + else + targetparent.right=child; + + } + private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ + BinaryTreeNode targetleftchild=null; + BinaryTreeNode targetrightchild=null; + targetleftchild=target.left; + targetrightchild=target.right; + if(targetleftchild!=null) + targetleftchild.top=parent; + if(targetrightchild!=null) + targetrightchild.top=parent; + + } + //找到前驱节点 + private BinaryTreeNode prev(BinaryTreeNode target){ + // BinaryTreeNode prev=null; + while(target.left!=null){ + target=target.left; + } + return target; + + } + //找到后驱节点 + private BinaryTreeNode next(BinaryTreeNode target){ +// BinaryTreeNode next=null; + while(target.right!=null){ + target=target.right; + } + return target; + + } + public int size(){ + + return size; + } + private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { + BinaryTreeNode parent=node; + while (parent != null) { + + if (key < parent.key&&parent.left!=null) { + parent = parent.left; + } else if (key > parent.key&&parent.right!=null) { + parent = parent.right; + } else { + return parent; + + } + } + return parent; + + + } + + + + + private static class BinaryTreeNode{ + Object data; + int key; + BinaryTreeNode left; + BinaryTreeNode right; + BinaryTreeNode top; + public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ + this.key=key; + this.data=o; + this.left=left; + this.right=right; + this.top=top; + + } + + + } +} diff --git a/group06/736464448/src/data_structure/MyLinkedList.java b/group06/736464448/src/data_structure/MyLinkedList.java new file mode 100644 index 0000000000..32097115e7 --- /dev/null +++ b/group06/736464448/src/data_structure/MyLinkedList.java @@ -0,0 +1,209 @@ +package data_structure; + + +import java.util.Iterator; +import java.util.NoSuchElementException; + + + +public class MyLinkedList { + private int size; + private Node head; + private Node last; + + public void add(Object o){ + + linkLast(o); + + } + + public Object get(int index){ + checkPositionIndex(index); + Node node=node(index); + return node.item; + } + public Object remove(int index){ + checkPositionIndex(index); + Node node=node(index); + isnull(node); + Object o=null; + Node before=null; + if(index==0){ + o=node.item; + node.next=head; + node=null; + } + else + { + before=node(index-1); + before.next=node.next; + o=node.item; + node=null; + + } + + return o; + } + public int size(){ + + return size; + } + public void addFirst(Object o){ + + linkFirst(o); + + + } + public void addLast(Object o){ + linkLast(o); + + } + public Object removeFirst(){ + Node f=head; + isnull(f); + final Node next=head.next; + Object o=f.item; + f=null; + head=next; + if(next==null) + last=null; + size--; + + return o; + } + //这个方法用多了也爆炸 + public Object removeLast(){ + Node l=last; + isnull(l); + Object o=null; + if(size>=2){ + final Node before=node(size-1); + o=l.item; + l=null; + last=before; + + if(before==null) + head=null; + } + else{ + o=l.item; + l=null; + last=null; + head=null; + } + + + size--; + return o; + + } + public Iterator iterator(){ + + + return new ListItr(); + } + + + private static class Node{ + Object item; + Node next; + Node(Object o, Node next){ + this.item=o; + this.next=next; + } + } + public void add(int index, Object o) { + checkPositionIndex(index); + + if (index == 0) + linkFirst(o); + else + linkNext(o, node(index-1)); + } + private void linkNext(Object o,Node node){ + final Node next=node.next; + final Node newnode=new Node(o,next); + node.next=newnode; + size++; + } + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + private void linkLast(Object o){ + final Node l=last; + final Node node=new Node(o,null); + last=node; + if(head==null) + head=node; + else + l.next=node; + size++; + } + private void linkFirst(Object o){ + final Node f=head; + final Node node=new Node(o,null); + head=node; + if(last==null) + last=node; + else + head.next=f; + size++; + } + private void isnull(Node node){ + if (node == null) + throw new NoSuchElementException(); + } + Node node(int index) { + // assert isElementIndex(index); + + + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + + + } + + private class ListItr implements Iterator { + private Node next=head; + private int nextIndex; + private Node lastReturned = null; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + //加上死循环了 +// if(nextIndex==size){ +// next=head; +// nextIndex=0; +// } + return lastReturned.item; + } + + @Override + public void remove() { + // TODO Auto-generated method stub + + } + } + + + +} diff --git a/group06/736464448/src/data_structure/MyQueue.java b/group06/736464448/src/data_structure/MyQueue.java new file mode 100644 index 0000000000..8e3f50e0bb --- /dev/null +++ b/group06/736464448/src/data_structure/MyQueue.java @@ -0,0 +1,21 @@ +package data_structure; + +public class MyQueue { + private MyLinkedList elementData =new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/736464448/src/data_structure/MyStack.java b/group06/736464448/src/data_structure/MyStack.java new file mode 100644 index 0000000000..f442468fb2 --- /dev/null +++ b/group06/736464448/src/data_structure/MyStack.java @@ -0,0 +1,22 @@ +package data_structure; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/736464448/src/test_data_structure/TestMyArrayList.java b/group06/736464448/src/test_data_structure/TestMyArrayList.java new file mode 100644 index 0000000000..6ce24c90a8 --- /dev/null +++ b/group06/736464448/src/test_data_structure/TestMyArrayList.java @@ -0,0 +1,93 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyArrayList; + +public class TestMyArrayList { + MyArrayList list; + + @Before + public void setUp() throws Exception { + list=new MyArrayList(); + System.out.println("begin"); + } + + @After + public void tearDown() throws Exception { + System.out.println("end"); + } + + @Test + public void testMyArrayList() { + + Assert.assertEquals(0, list.Capacity()); + } + + @Test + public void testAddObject() { + list.add(new Integer(10)); + Assert.assertEquals(10, list.get(0)); + } + + @Test + public void testAddIntObject() { + list.add(1); + list.add(1); + list.add(1); + list.add(1,2); + Assert.assertEquals(2, list.get(1)); + + + } + + @Test + public void testGet() { + list.add(1); + list.add(2); + Assert.assertEquals(2, list.get(1)); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(2, list.remove(1)); + + } + + @Test + public void testSize() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testCapacity() { + list=new MyArrayList(5); + Assert.assertEquals(5, list.Capacity()); + + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + Iterator itr=list.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + +} diff --git a/group06/736464448/src/test_data_structure/TestMyBinaryTree.java b/group06/736464448/src/test_data_structure/TestMyBinaryTree.java new file mode 100644 index 0000000000..f56696d4f7 --- /dev/null +++ b/group06/736464448/src/test_data_structure/TestMyBinaryTree.java @@ -0,0 +1,59 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyBinaryTree; + + +public class TestMyBinaryTree { + MyBinaryTree bt; + + @Before + public void setUp() throws Exception { + System.out.println("开始测试"); + bt=new MyBinaryTree(); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testAdd() { + bt.add(1, "c"); + + } + + @Test + public void testGet() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.get(2)); + } + + @Test + public void testRemove() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.remove(2)); + + Assert.assertEquals(2, bt.size()); + } + + @Test + public void testSize() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(3, bt.size()); + } + +} diff --git a/group06/736464448/src/test_data_structure/TestMyLinkedList.java b/group06/736464448/src/test_data_structure/TestMyLinkedList.java new file mode 100644 index 0000000000..9280b0f65c --- /dev/null +++ b/group06/736464448/src/test_data_structure/TestMyLinkedList.java @@ -0,0 +1,111 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyLinkedList; + +public class TestMyLinkedList { + MyLinkedList link=null; + + @Before + public void setUp() throws Exception { + link=new MyLinkedList(); + System.out.println("测试开始"); + } + + @After + public void tearDown() throws Exception { + System.out.println("测试结束"); + } + + @Test + public void testAddObject() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testGet() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testRemove() { + link.add(1); + Assert.assertEquals(1,link.remove(0)); + } + + @Test + public void testSize() { + link.add(1); + Assert.assertEquals(1,link.size()); + } + + @Test + public void testAddFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.get(0)); + } + + @Test + public void testAddLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.get(link.size()-1)); + } + + @Test + public void testRemoveFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.removeFirst()); + } + + @Test + public void testRemoveLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.removeLast()); + } + + @Test + public void testIterator() { + link.add(1); + link.add(2); + link.add(3); + Iterator itr=link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + + @Test + public void testAddIntObject() { + link.add(1); + link.add(1); + link.add(1); + link.add(2, 3); + Assert.assertEquals(3,link.get(2)); + } + + + +} diff --git a/group06/736464448/src/test_data_structure/TestMyQueue.java b/group06/736464448/src/test_data_structure/TestMyQueue.java new file mode 100644 index 0000000000..dd0e96e699 --- /dev/null +++ b/group06/736464448/src/test_data_structure/TestMyQueue.java @@ -0,0 +1,49 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyQueue; + +public class TestMyQueue { + MyQueue queue; + + @Before + public void setUp() throws Exception { + queue =new MyQueue(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testEnQueue() { + queue.enQueue("hello"); + } + + @Test + public void testDeQueue() { + queue.enQueue("hello"); + queue.enQueue("world"); + Assert.assertEquals("hello",queue.deQueue()); + } + + @Test + public void testIsEmpty() { + queue.enQueue("hello"); + Assert.assertEquals(false,queue.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0,queue.size()); + } + +} diff --git a/group06/736464448/src/test_data_structure/TestMyStack.java b/group06/736464448/src/test_data_structure/TestMyStack.java new file mode 100644 index 0000000000..7ddaba1e62 --- /dev/null +++ b/group06/736464448/src/test_data_structure/TestMyStack.java @@ -0,0 +1,60 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyStack; + +public class TestMyStack { + MyStack mystack; + + @Before + public void setUp() throws Exception { + mystack=new MyStack(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testPush() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + } + + @Test + public void testPop() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + Assert.assertEquals("World", (String)mystack.pop()); + Assert.assertEquals(",", (String)mystack.pop()); + + } + + @Test + public void testPeek() { + mystack.push("Hello"); + Assert.assertEquals("Hello", (String)mystack.peek()); + } + + @Test + public void testIsEmpty() { + + Assert.assertEquals(true, mystack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, mystack.size()); + } + +} From 734a4c12235f0c61753089655391c4324768a1ec Mon Sep 17 00:00:00 2001 From: liushurencx <736464448@qq.com> Date: Sun, 26 Feb 2017 14:37:29 +0800 Subject: [PATCH 6/6] Do it again yeh --- group06/736464448/{src => }/data_structure/MyArrayList.java | 0 group06/736464448/{src => }/data_structure/MyBinaryTree.java | 0 group06/736464448/{src => }/data_structure/MyLinkedList.java | 0 group06/736464448/{src => }/data_structure/MyQueue.java | 0 group06/736464448/{src => }/data_structure/MyStack.java | 0 .../736464448/{src => }/test_data_structure/TestMyArrayList.java | 0 .../736464448/{src => }/test_data_structure/TestMyBinaryTree.java | 0 .../736464448/{src => }/test_data_structure/TestMyLinkedList.java | 0 group06/736464448/{src => }/test_data_structure/TestMyQueue.java | 0 group06/736464448/{src => }/test_data_structure/TestMyStack.java | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename group06/736464448/{src => }/data_structure/MyArrayList.java (100%) rename group06/736464448/{src => }/data_structure/MyBinaryTree.java (100%) rename group06/736464448/{src => }/data_structure/MyLinkedList.java (100%) rename group06/736464448/{src => }/data_structure/MyQueue.java (100%) rename group06/736464448/{src => }/data_structure/MyStack.java (100%) rename group06/736464448/{src => }/test_data_structure/TestMyArrayList.java (100%) rename group06/736464448/{src => }/test_data_structure/TestMyBinaryTree.java (100%) rename group06/736464448/{src => }/test_data_structure/TestMyLinkedList.java (100%) rename group06/736464448/{src => }/test_data_structure/TestMyQueue.java (100%) rename group06/736464448/{src => }/test_data_structure/TestMyStack.java (100%) diff --git a/group06/736464448/src/data_structure/MyArrayList.java b/group06/736464448/data_structure/MyArrayList.java similarity index 100% rename from group06/736464448/src/data_structure/MyArrayList.java rename to group06/736464448/data_structure/MyArrayList.java diff --git a/group06/736464448/src/data_structure/MyBinaryTree.java b/group06/736464448/data_structure/MyBinaryTree.java similarity index 100% rename from group06/736464448/src/data_structure/MyBinaryTree.java rename to group06/736464448/data_structure/MyBinaryTree.java diff --git a/group06/736464448/src/data_structure/MyLinkedList.java b/group06/736464448/data_structure/MyLinkedList.java similarity index 100% rename from group06/736464448/src/data_structure/MyLinkedList.java rename to group06/736464448/data_structure/MyLinkedList.java diff --git a/group06/736464448/src/data_structure/MyQueue.java b/group06/736464448/data_structure/MyQueue.java similarity index 100% rename from group06/736464448/src/data_structure/MyQueue.java rename to group06/736464448/data_structure/MyQueue.java diff --git a/group06/736464448/src/data_structure/MyStack.java b/group06/736464448/data_structure/MyStack.java similarity index 100% rename from group06/736464448/src/data_structure/MyStack.java rename to group06/736464448/data_structure/MyStack.java diff --git a/group06/736464448/src/test_data_structure/TestMyArrayList.java b/group06/736464448/test_data_structure/TestMyArrayList.java similarity index 100% rename from group06/736464448/src/test_data_structure/TestMyArrayList.java rename to group06/736464448/test_data_structure/TestMyArrayList.java diff --git a/group06/736464448/src/test_data_structure/TestMyBinaryTree.java b/group06/736464448/test_data_structure/TestMyBinaryTree.java similarity index 100% rename from group06/736464448/src/test_data_structure/TestMyBinaryTree.java rename to group06/736464448/test_data_structure/TestMyBinaryTree.java diff --git a/group06/736464448/src/test_data_structure/TestMyLinkedList.java b/group06/736464448/test_data_structure/TestMyLinkedList.java similarity index 100% rename from group06/736464448/src/test_data_structure/TestMyLinkedList.java rename to group06/736464448/test_data_structure/TestMyLinkedList.java diff --git a/group06/736464448/src/test_data_structure/TestMyQueue.java b/group06/736464448/test_data_structure/TestMyQueue.java similarity index 100% rename from group06/736464448/src/test_data_structure/TestMyQueue.java rename to group06/736464448/test_data_structure/TestMyQueue.java diff --git a/group06/736464448/src/test_data_structure/TestMyStack.java b/group06/736464448/test_data_structure/TestMyStack.java similarity index 100% rename from group06/736464448/src/test_data_structure/TestMyStack.java rename to group06/736464448/test_data_structure/TestMyStack.java