diff --git a/coding2017-1 b/coding2017-1 new file mode 160000 index 0000000000..b5be7e853c --- /dev/null +++ b/coding2017-1 @@ -0,0 +1 @@ +Subproject commit b5be7e853cd9d2f5bbcc43149dc4df83749759a2 diff --git a/group05/371492887/task_01/.classpath b/group05/371492887/task_01/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group05/371492887/task_01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/371492887/task_01/.gitignore b/group05/371492887/task_01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/371492887/task_01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/371492887/task_01/.project b/group05/371492887/task_01/.project new file mode 100644 index 0000000000..eca593c703 --- /dev/null +++ b/group05/371492887/task_01/.project @@ -0,0 +1,17 @@ + + + task_01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java new file mode 100644 index 0000000000..848b4fafe9 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java @@ -0,0 +1,95 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; + +public class ArrayListTest { + + private ArrayList list; + + @Before + public void init(){ + list=new ArrayList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testAddObject() { + list.add(100); + Assert.assertEquals(101, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(3,"test"); + Assert.assertEquals("test", list.get(3)); + } + + @Test + public void testRemoveInt() { + list.add(3,"test"); + list.remove(3); + Assert.assertEquals(3, list.get(3)); + } + + @Test + public void testRemoveObject() { + list.add(0,"test"); + list.remove("test"); + Assert.assertEquals(0, list.get(0)); + } + + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + Assert.assertEquals(false, list.contains("test")); + list.add("test"); + Assert.assertEquals(true, list.contains("test")); + } + + + + @Test + public void testSet() { + Assert.assertEquals(true, list.contains(3)); + list.set(3, "test"); + Assert.assertEquals(true, list.contains("test")); + Assert.assertEquals(false, list.contains(3)); + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java new file mode 100644 index 0000000000..abb27b5691 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java @@ -0,0 +1,66 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.BinaryTree; + +public class BinaryTreeTest { + + BinaryTree tree; + + @Before + public void init(){ + tree=new BinaryTree(); + tree.insert(5); + tree.insert(3); + tree.insert(8); + tree.insert(2); + tree.insert(7); + tree.insert(9); + tree.insert(1); + tree.insert(4); + tree.insert(10); + tree.insert(6); + } + + @Test + public void testMakeEmpty() { + tree.makeEmpty(); + Assert.assertEquals(true, tree.isEmpty()); + } + + @Test + public void testGetHeight() { + Assert.assertEquals(3, tree.getHeight()); + } + + @Test + public void testContains() { + for (int i = 1; i < 11; i++) { + Assert.assertEquals(true, tree.contains(i)); + } + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin()); + } + + @Test + public void testFindMax() { + Assert.assertEquals(10, tree.findMax()); + } + + + @Test + public void testRemove() { + tree.remove(3); + Assert.assertEquals(false, tree.contains(3)); + } + + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java new file mode 100644 index 0000000000..dcbe6353c3 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java @@ -0,0 +1,142 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; +import com.nitasty.util.LinkedList; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void init(){ + list=new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testGet() { + IndexOutOfBoundsException tx=null; + for (int i = 0; i < 100; i++) { + Assert.assertEquals(i, list.get(i)); + } + + try { + list.get(100); + } catch (IndexOutOfBoundsException e) { + tx=e; + } + Assert.assertEquals(IndexOutOfBoundsException.class,tx.getClass()); + } + + @Test + public void testRemoveInt() { + for (int i = 99; i >= 0; i--) { + Assert.assertEquals(i, list.remove(i)); + } + } + + @Test + public void testSize() { + Assert.assertEquals(100, list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst(-1); + for (int i = 0; i < 101; i++) { + Assert.assertEquals(i-1, list.get(i)); + } + } + + @Test + public void testAddLast() { + + for (int i = 100; i < 1000; i++) { + list.addLast(i); + } + + for (int i = 0; i < 1000; i++) { + Assert.assertEquals(i, list.get(i)); + } + } + + @Test + public void testAddBefore() { + list.addBefore(66,list.node(3)); + Assert.assertEquals(66, list.get(3)); + } + + @Test + public void testAddAfter() { + list.addAfter(66,list.node(3)); + Assert.assertEquals(66, list.get(4)); + } + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + for (int i = 0; i < 100; i++) { + Assert.assertEquals(true, list.contains(i)); + Assert.assertEquals(false, list.contains(i+100)); + } + } + + + @Test + public void testAddIntObject() { + list.add(20,"test"); + Assert.assertEquals("test", list.get(20)); + } + + @Test + public void testRemoveObject() { + list.remove(30); + Assert.assertEquals(31, list.get(30)); + } + + @Test + public void testSet() { + for (int i = 0; i < 100; i++) { + list.set(i, i+100); + Assert.assertEquals(i+100, list.get(i)); + } + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java new file mode 100644 index 0000000000..fa17263108 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java @@ -0,0 +1,49 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Queue; + +public class QueueTest { + + Queue queue; + + @Before + public void init(){ + queue=new Queue(); + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + } + + @Test + public void testDeQueue() { + for(int i=0; i<100;i++){ + Assert.assertEquals(i, queue.deQueue()); + } + } + + @Test + public void testIsEmpty() { + for(int i=0; i<100;i++){ + queue.deQueue(); + if(i<99) + Assert.assertEquals(false, queue.isEmpty()); + } + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + for(int i=99; i>0;i--){ + queue.deQueue(); + Assert.assertEquals(i, queue.size()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/StackTest.java b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java new file mode 100644 index 0000000000..3ecd59219a --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java @@ -0,0 +1,51 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Stack; + +public class StackTest { + + Stack stack; + + @Before + public void init(){ + stack=new Stack(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + } + + @Test + public void testPop() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(i, stack.pop()); + } + } + + @Test + public void testPeek() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(99, stack.peek()); + } + } + + @Test + public void testIsEmpty() { + for (int i = 99; i >=0; i--) { + stack.pop(); + } + Assert.assertEquals(true,stack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(100,stack.size()); + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java new file mode 100644 index 0000000000..88ef682cf9 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java @@ -0,0 +1,307 @@ +package com.nitasty.util; + +import java.util.Arrays; +import java.util.Objects; + +public class ArrayList implements List { + + // �����ڲ����� + private static final int DEFAULT_CAPACITY = 10; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int size; + + private Object[] elementData; + + /** + * + * �޲γ�ʼ��Ϊ�����飬��ʡ�ռ� + */ + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + /** + * + * @param initCapacity + */ + public ArrayList(int initCapacity) { + if (initCapacity > 0) { + elementData = new Object[initCapacity]; + } else if (initCapacity == 0) { + elementData = EMPTY_ELEMENTDATA; + } else { + throw new IllegalArgumentException("�Ƿ���ʼ������" + initCapacity); + } + + } + + // TODO + public ArrayList(List list) { + list.toArray(); + + } + + /** + * ����У�� + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + + if (elementData.length < minCapacity) { + grow(minCapacity); + } + + } + + /** + * �������� + * + * @param minCapacity + * @return + */ + private void grow(int minCapacity) { + // ������ + int oldCapacity = this.elementData.length; + // ������Ϊ��������1.5�� + int newCapacity = oldCapacity + oldCapacity >> 1; + // ����������С�����Ƚ� + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + } + // ���������ܴ���int���ֵ + if (newCapacity > Integer.MAX_VALUE) { + newCapacity = Integer.MAX_VALUE; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + return indexOf(o) >= 0; //��д=��������ֵ�bug + } + + @Override + public boolean add(Object o) { + + ensureCapacity(size + 1); + + elementData[size++] = o;// size��index��1 + + return true; + } + + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + @Override + public boolean add(int index, Object o) { + + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size + - index);// size������ը�� + elementData[index] = o; + return true; + } + + @Override + public boolean addAll(Object[] o) { + int numNew = o.length; + ensureCapacity(size + numNew); + System.arraycopy(o, 0, elementData, size, numNew); + size += numNew;// size���� + return numNew != 0; + } + + @Override + public boolean addAll(int index, Object[] o) { + rangeCheck(index); + int numNew = o.length; + ensureCapacity(size + numNew); + + int numMoved = size - index;// ����rangeCheck��index�϶�С��size�� + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, size + numNew, + numNew);// ������ԭԪ���Ƶ����� + System.arraycopy(o, 0, elementData, index, numNew);// ���ƽ�Ҫ���ӵ�Ԫ�� + + size += numNew;// ��Ҫ���˰����������� + return numNew != 0; + } + + @Override + public Object remove(int index) { + rangeCheck(index); + Object oldValue = elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + elementData[--size] = null;// Clear to let gc do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index=this.indexOf(o); + if(index==-1){ + return false; + }else{ + this.remove(index); + return true; + } + } + + @Override + /** + * ����ɾ��list + */ + public boolean removeAll(List list) { + Objects.requireNonNull(list); + return batchRemove(list,false); + } + + /** + * ����ʵ��removeALl��retainAll + * @param list + * @param complement + * @return + */ + private boolean batchRemove(List list, boolean complement) { + final Object[] elementData=this.elementData; + int r=0,w=0; + boolean modified=false; + try{ + for(;r= 0; i--) { + if (elementData[i] == null) { + return i; + } + } + } else { + for (int i = size-1; i >= 0; i--) { + if (o.equals(elementData[i])) { + return i; + } + } + } + return -1;// û�ҵ� + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size);//��Ҫֱ�ӷ���elementData + } + + @Override + public void clear() { + for(int i=0; i ������ʵ����Comparable���� + */ +public class BinaryTree> { + + private BinaryNode root; + + public BinaryTree(){ + this.root=null; + } + + public BinaryTree(BinaryNode root) { + this.root = root; + } + + public void makeEmpty(){ + root=null; + } + + public boolean isEmpty(){ + return root==null; + } + + public int getHeight(){ + return height(root); + } + + public boolean contains(E x){ + return contains(x,root); + } + + + public E findMin(){ + if(isEmpty()) + throw new NullPointerException();//�׸�ʲô��������أ�TODO + return (E) findMin(root).data; //ΪʲôҪ��ת�ͣ� + } + + + + public E findMax(){ + if(isEmpty()) + throw new NullPointerException();//�׸�ʲô��������أ�TODO + return (E) findMax(root).data;//ΪʲôҪ��ת�ͣ� + } + + public void insert(E x){ + root=insert(x,root);//ΪɶҪ��root�ӷ���ֵ����Ϊ�ⷽ���ǵݹ�� + } + + public void remove(E x){ + root=remove(x,root);//ΪɶҪ��root�ӷ���ֵ����Ϊ�ⷽ���ǵݹ�� + } + + //��ӡ����data + public void printTree(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTree(root); + } + + public void printTreeStructure(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTreeStructure(root,0); + } + + private void printTreeStructure(BinaryNode t,int i) { + StringBuffer buff=new StringBuffer(); + if(t!=null){ + for(int j=0;j= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; // postion�������ӵ���� + } + + @Override + public boolean remove(Object o) { + + return false; + } + + @Override + public boolean removeAll(List list) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object set(int index, Object o) { + checkElementIndex(index); + Node node=node(index); + Object oldValue=node.data; + node.data=o; + return oldValue; + } + + @Override + public int indexOf(Object o) { + int index = 0; + if (o == null) { + for (Node x = first; x != null; x = x.next) { // �µ�ѭ����ʽ + if (x.data == null) + return index; + index++; + } + } else { + for (Node x = first; x != null; x = x.next) { // �µ�ѭ����ʽ + if (o.equals(x.data)) + return index; + index++; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + int index = size-1; + if (o == null) { + for (Node x = last; x != null; x = x.prev) { // �µ�ѭ����ʽ + if (x.data == null) + return index; + index--; + } + } else { + for (Node x = last; x != null; x = x.prev) { // �µ�ѭ����ʽ + if (o.equals(x.data)) + return index; + index--; + } + } + return -1; + } + + @Override + public Object[] toArray() { + Object[] elementData = new Object[size]; + int i = 0; + for (Node x = first; x != null; x = x.next) { + elementData[i++] = x.data; + } + return elementData; + } + + @Override + public void clear() { + // bugը�� + // for(Node x=first;x!=null;x=x.next){ + // x=null; + // } + + for (Node x = first; x != null;) { + Node next = x.next; + x.prev = null; + x.data = null; + x.next = null; + x = next; + } + size = 0; + first = last = null; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + private Node lastRetured; + private Node next; + int cursor; + + @Override + public boolean hasNext() { + return cursor