diff --git a/group24/635501270/week1/collections/ArrayList.java b/group24/635501270/week1/collections/ArrayList.java new file mode 100644 index 0000000000..317e5d1e76 --- /dev/null +++ b/group24/635501270/week1/collections/ArrayList.java @@ -0,0 +1,96 @@ +package week1.collections; + +import java.util.Arrays; + +public class ArrayList implements List { + private int size = 0; //声明数组长度 + + private Object[] elementData = new Object[0]; //声明一个Object数组,初始大小为10 + + /** + * 将元素添加到末尾 + */ + public boolean add(Object o){ + ensureCapacity(size+1); + elementData[size] = o; + size++; + return true; + } + + /** + * 将元素插入到index位置 + */ + public void add(int index, Object o){ + exception(index); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + /** + * 获得index位置的元素 + */ + public Object get(int index){ + exception(index); + return elementData[index]; + } + + /** + * 删除index位置的元素并返回 + */ + public Object remove(int index){ + exception(index); + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return o; + } + + private void exception(int index) { + if(index > size || index < 0){ + throw new ArrayIndexOutOfBoundsException("index"+index+"越界"); + } + } + + /** + * 获取元素个数 + */ + public int size(){ + return size; + } + + /** + * 获取迭代器 + * @return + */ + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + int pos = 0; + @Override + public boolean hasNext() { + return pos elementData.length){ + int newCapacity = Math.max(minCapacity, elementData.length*2); + elementData = Arrays.copyOf(elementData, newCapacity); + } + } +} diff --git a/group24/635501270/week1/collections/BinaryTreeNode.java b/group24/635501270/week1/collections/BinaryTreeNode.java new file mode 100644 index 0000000000..c120fd5f12 --- /dev/null +++ b/group24/635501270/week1/collections/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package week1.collections; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(data == null){ + data = o; + return this; + } + if(o instanceof Integer || this.data instanceof Integer){ + int compare = (Integer)this.data - (Integer)o; + if(compare > 0){ + if(this.left == null){ + this.left = new BinaryTreeNode(); + this.left.data = o; + return this.left; + }else{ + return this.left.insert(o); + } + }else if(compare < 0){ + if(this.right == null){ + this.right = new BinaryTreeNode(); + this.right.data = o; + return this.right; + }else{ + return this.right.insert(o); + } + }else{ + return this; + } + } + return null; + } +} diff --git a/group24/635501270/week1/collections/Iterator.java b/group24/635501270/week1/collections/Iterator.java new file mode 100644 index 0000000000..2fc31b5a3a --- /dev/null +++ b/group24/635501270/week1/collections/Iterator.java @@ -0,0 +1,6 @@ +package week1.collections; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group24/635501270/week1/collections/LinkedList.java b/group24/635501270/week1/collections/LinkedList.java new file mode 100644 index 0000000000..bb00b7c3d8 --- /dev/null +++ b/group24/635501270/week1/collections/LinkedList.java @@ -0,0 +1,204 @@ +package week1.collections; + +public class LinkedList implements List { + private int size = 0; + private Node head; + private Node last; + + public boolean add(Object o){ + Node newNode = new Node(o); + if(head == null){ + last = newNode; + head = newNode; + }else{ + Node oldLast = last; + last = newNode; + oldLast.next = last; + } + size++; + return true; + } + public void add(int index , Object o){ + outOfIndex(index); + if(index == 0){ + Node oldHead = head; + head = new Node(o); + head.next = oldHead; + }else{ + Node h = getNode(index-1); + Node newNode = new Node(o); + newNode.next = h.next; + h.next = newNode; + } + size++; + } + public Object get(int index){ + Node h = getNode(index); + return h.data; + } + private Node getNode(int index) { + outOfIndex(index); + Node h = head; + for(int i=0;i= size || index < 0){ + throw new IndexOutOfBoundsException("Index"+index+"越界"); + } + } + public Object remove(int index){ + outOfIndex(index); + Object data; + if(index==0){ + Node oldHead = head; + head = head.next; + data = oldHead.data; + oldHead = null; + }else{ + Node preNode = getNode(index-1); + if(preNode.next==last){ + Node oldLast = last; + last = preNode; + data = oldLast.data; + oldLast = null; + }else{ + Node removeNode = preNode.next; + preNode.next = preNode.next.next; + data = removeNode.data; + removeNode = null; + } + } + size--; + return data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + if(last==null){ + add(o); + }else{ + Node oldLast = last; + last = new Node(o); + oldLast.next = last; + } + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + private class LinkedListIterator implements Iterator{ + int pos = 0; + @Override + public boolean hasNext() { + return pos7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/635501270/week1/collections/List.java b/group24/635501270/week1/collections/List.java new file mode 100644 index 0000000000..248fb2e27f --- /dev/null +++ b/group24/635501270/week1/collections/List.java @@ -0,0 +1,9 @@ +package week1.collections; + +public interface List { + public boolean add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group24/635501270/week1/collections/Queue.java b/group24/635501270/week1/collections/Queue.java new file mode 100644 index 0000000000..b1f431deeb --- /dev/null +++ b/group24/635501270/week1/collections/Queue.java @@ -0,0 +1,24 @@ +package week1.collections; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public boolean enQueue(Object o){ + list.add(o); + return true; + } + + public Object deQueue(){ + Object o = list.removeFirst(); + return o; + } + + public boolean isEmpty(){ + return list.size()==0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group24/635501270/week1/collections/Stack.java b/group24/635501270/week1/collections/Stack.java new file mode 100644 index 0000000000..e25d6491eb --- /dev/null +++ b/group24/635501270/week1/collections/Stack.java @@ -0,0 +1,25 @@ +package week1.collections; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.remove(elementData.size()-1); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group24/635501270/week1/collectiontest/ArrayListTest.java b/group24/635501270/week1/collectiontest/ArrayListTest.java new file mode 100644 index 0000000000..d71f1d63da --- /dev/null +++ b/group24/635501270/week1/collectiontest/ArrayListTest.java @@ -0,0 +1,43 @@ +package week1.collectiontest; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import week1.collections.ArrayList; +import week1.collections.Iterator; + +public class ArrayListTest { + ArrayList list; + @Before + public void init(){ + list = new ArrayList(); + for(int i=1;i<=10;i++){ + list.add(i); + } + } + + @Test + public void test1(){ + list.add(4,4.5); + assertEquals(4.5, list.get(4)); + } + + @Test + public void test2(){ + for(int i=10;i>=1;i--){ + assertEquals(i, list.remove(i-1)); + } + } + + @Test + public void test3(){ + Iterator it = list.iterator(); + while(it.hasNext()){ + for(int i=1;i<=10;i++){ + assertEquals(it.next(), i); + } + } + } +} diff --git a/group24/635501270/week1/collectiontest/BinaryTreeNodeTest.java b/group24/635501270/week1/collectiontest/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..fa0984130c --- /dev/null +++ b/group24/635501270/week1/collectiontest/BinaryTreeNodeTest.java @@ -0,0 +1,36 @@ +package week1.collectiontest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week1.collections.BinaryTreeNode; + +public class BinaryTreeNodeTest { + BinaryTreeNode BTN = null; + @Before + public void init(){ + BTN = new BinaryTreeNode(); + } + + @Test + public void test1(){ + Object o = BTN.getData(); + Assert.assertEquals(null, o); + } + + @Test + public void test2(){ + Object o1 = BTN.insert(5).getData(); + Assert.assertEquals(o1, 5); + Object o2 = BTN.insert(7).getData(); + Assert.assertEquals(o2, 7); + Object o3 = BTN.insert(1).getData(); + Assert.assertEquals(o3, 1); + Assert.assertEquals(5, BTN.getData()); + Assert.assertEquals(7, BTN.getRight().getData()); + Assert.assertEquals(1, BTN.getLeft().getData()); + BTN.insert(6).getData(); + Assert.assertEquals(6, BTN.getRight().getLeft().getData()); + } +} diff --git a/group24/635501270/week1/collectiontest/LinkedListTest.java b/group24/635501270/week1/collectiontest/LinkedListTest.java new file mode 100644 index 0000000000..d4c94b6d61 --- /dev/null +++ b/group24/635501270/week1/collectiontest/LinkedListTest.java @@ -0,0 +1,87 @@ +package week1.collectiontest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import week1.collections.Iterator; +import week1.collections.LinkedList; + +public class LinkedListTest { + LinkedList list; + @Before + public void init(){ + list = new LinkedList(); + for(int i=1;i<=10;i++){ + list.add(i); + } + } + + @Test + public void test1(){ + for(int i=1;i<=10;i++){ + assertEquals(i, list.get(i-1)); + } + } + + @Test + public void test2(){ + list.add(0,4.5); + assertEquals(list.get(0), 4.5); + assertEquals(list.get(1), 1); + } + + @Test + public void test3(){ + assertEquals(list.remove(0), 1); + assertEquals(list.get(5), 7); + assertEquals(list.remove(4), 6); + System.out.println(list.size()); + } + + @Test + public void test4(){ + list.addFirst(0.5); + list.addLast(10.5); + assertEquals(list.get(0),0.5); + assertEquals(list.get(11), 10.5); + } + + @Test + public void test5(){ + assertEquals(list.remove(9), 10); + list.addLast(10.5); + assertEquals(list.get(9), 10.5); + list.addFirst(1.5); + assertEquals(list.get(0), 1.5); + } + + @Test + public void test6(){ + assertEquals(list.removeFirst(), 1); + assertEquals(list.removeLast(),10); + list.addFirst(55); + list.addLast(100); + assertEquals(list.get(0), 55); + assertEquals(list.get(9), 100); + } + + @Test + public void test7(){ + Iterator it = list.iterator(); + while(it.hasNext()){ + for(int i=1;i<=10;i++){ + assertEquals(it.next(), i); + } + } + } + + @Test + public void test8(){ + for(int i=1;i<=10;i++){ + assertEquals(list.removeFirst(), i); + System.out.println(list.size()); + } + } +} diff --git a/group24/635501270/week1/collectiontest/QueueTest.java b/group24/635501270/week1/collectiontest/QueueTest.java new file mode 100644 index 0000000000..e679d857a4 --- /dev/null +++ b/group24/635501270/week1/collectiontest/QueueTest.java @@ -0,0 +1,31 @@ +package week1.collectiontest; + +import static org.junit.Assert.*; + +import java.awt.List; + +import org.junit.Before; +import org.junit.Test; + +import week1.collections.Queue; + +public class QueueTest { + Queue queue = null; + @Before + public void init(){ + queue = new Queue(); + for(int i=1;i<=10;i++){ + queue.enQueue(i); + } + } + + @Test + public void test1(){ + for(int i=1;i<=10;i++){ + System.out.println(queue.size()); + assertEquals(queue.deQueue(), i); + } + assertEquals(queue.isEmpty(), true); + } + +} diff --git a/group24/635501270/week1/collectiontest/StackTest.java b/group24/635501270/week1/collectiontest/StackTest.java new file mode 100644 index 0000000000..d2f5921b79 --- /dev/null +++ b/group24/635501270/week1/collectiontest/StackTest.java @@ -0,0 +1,36 @@ +package week1.collectiontest; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +import week1.collections.Stack; + +public class StackTest { + Stack stack = null; + @Before + public void init(){ + stack = new Stack(); + for(int i=1;i<=10;i++){ + stack.push(i); + } + } + + @Test + public void test1(){ + assertEquals(stack.peek(), 10); + assertEquals(stack.peek(), 10); + assertEquals(stack.peek(), 10); + assertEquals(stack.pop(), 10); + assertEquals(stack.pop(), 9); + assertEquals(stack.size(), 8); + } + + @Test + public void test2(){ + for(int i=10;i>=1;i--){ + assertEquals(stack.pop(), i); + } + assertEquals(stack.isEmpty(), true); + } +}