diff --git a/group27/772642286/basic/ArrayList.java b/group27/772642286/basic/ArrayList.java new file mode 100644 index 0000000000..108eb02ff4 --- /dev/null +++ b/group27/772642286/basic/ArrayList.java @@ -0,0 +1,80 @@ +package week01.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + add(size , o); + } + public void add(int index, Object o){ + if(index<0||index > size){ + throw new ArrayIndexOutOfBoundsException(index); + } + size++; + if(size>=elementData.length){ + expand(); + } + for(int i = size -1 ;i> index; i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + + public Object get(int index){ + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException(index); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException(index); + } + Object o = elementData[index]; + for(int i = index ;i< size - 1; i++){ + elementData[i] = elementData[i+1]; + } + elementData[size - 1] = null; + size--; + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + class ArrayListIterator implements Iterator{ + + int count = 0; + @Override + public boolean hasNext() { + count++; + if(size<= count){ + return false; + } + return true; + } + + @Override + public Object next() { + return elementData[count]; + } + + } + + + private void expand(){ + elementData = Arrays.copyOf(elementData, elementData.length*2); + } + +} diff --git a/group27/772642286/basic/BinaryTreeNode.java b/group27/772642286/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..75a9327421 --- /dev/null +++ b/group27/772642286/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +package week01.basic; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(T 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(T o){ + return insert(this,o); + } + + + public BinaryTreeNode insert(BinaryTreeNode root,T o){ + if(o.compareTo(root) >= 0){ + if(root.left == null){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + root.left = binaryTreeNode; + binaryTreeNode.data = o; + return binaryTreeNode; + } + return insert(root.left, o); + } + else{ + if (root.right == null) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + root.right = binaryTreeNode; + binaryTreeNode.data = o; + return binaryTreeNode; + } + return insert(root.right, o); + } + } + + +} diff --git a/group27/772642286/basic/Iterator.java b/group27/772642286/basic/Iterator.java new file mode 100644 index 0000000000..305ed43120 --- /dev/null +++ b/group27/772642286/basic/Iterator.java @@ -0,0 +1,7 @@ +package week01.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group27/772642286/basic/LinkedList.java b/group27/772642286/basic/LinkedList.java new file mode 100644 index 0000000000..98cf2d651b --- /dev/null +++ b/group27/772642286/basic/LinkedList.java @@ -0,0 +1,208 @@ +package week01.basic; + +import java.util.Objects; + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size; + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if(Objects.isNull(head)){ + head = node; + tail = head; + size++; + return ; + } + tail.next = node; + tail = node; + size++; + } + + public void add(int index , Object o){ + if(index<0 || index >size){ + throw new ArrayIndexOutOfBoundsException(index); + } + if(Objects.isNull(head)||index==size){ + add(o); + return; + } + + Node headNode = getNode(index - 1); + Node temp = headNode.next; + Node node = new Node(); + node.data = o; + node.next = temp; + headNode.next = node; + size++; + } + + + public Object get(int index){ + if(index< 0 || index >= size){ + throw new ArrayIndexOutOfBoundsException(index); + } + + return getNode(index).data; + } + + private Node getNode(int index){ + + + int count = 0; + Node headNode = head; + while(count< index){ + headNode = headNode.next; + count++; + } + return headNode; + } + + public Object remove(int index){ + if(index< 0 || index >= size){ + throw new ArrayIndexOutOfBoundsException(index); + } + + if(index==0){ + Node node = head; + head = head.next; + size --; + return node.data; + } + + Node headNode = getNode(index - 1); + Node node = headNode.next; + head.next = node.next; + size --; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator{ + + private Node currentNode; + + @Override + public boolean hasNext() { + if(currentNode==null){ + currentNode = head; + }else{ + currentNode = currentNode.next; + } + return Objects.nonNull(currentNode); + } + + @Override + public Object next() { + return currentNode.data; + } + + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->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){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @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/group27/772642286/basic/List.java b/group27/772642286/basic/List.java new file mode 100644 index 0000000000..912285e171 --- /dev/null +++ b/group27/772642286/basic/List.java @@ -0,0 +1,9 @@ +package week01.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group27/772642286/basic/Queue.java b/group27/772642286/basic/Queue.java new file mode 100644 index 0000000000..320280635f --- /dev/null +++ b/group27/772642286/basic/Queue.java @@ -0,0 +1,21 @@ +package week01.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(elementData.size()); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group27/772642286/basic/Stack.java b/group27/772642286/basic/Stack.java new file mode 100644 index 0000000000..0e6901c0ce --- /dev/null +++ b/group27/772642286/basic/Stack.java @@ -0,0 +1,23 @@ +package week01.basic; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group27/772642286/test/ArrayListTest.java b/group27/772642286/test/ArrayListTest.java new file mode 100644 index 0000000000..e8409a7d40 --- /dev/null +++ b/group27/772642286/test/ArrayListTest.java @@ -0,0 +1,55 @@ +package week01.test; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.ArrayList; +import week01.basic.Iterator; + + +public class ArrayListTest { + ArrayList list = null; + + @Before + public void init(){ + list = new ArrayList(); + for(int i=1;i<=500;i++){ + list.add(i); + } + } + + @Test + public void addTest(){ + Assert.assertEquals(500, list.size()); + for(int i=1;i<=list.size();i++){ + Assert.assertEquals(i, list.get(i-1)); + } + } + + @Test + public void addIndexTest(){ + list.add(250, 3333); + Assert.assertEquals(3333, list.get(250)); + Assert.assertEquals(500, list.get(500)); + } + + @Test + public void removeIndexTest(){ + list.remove(250); + Assert.assertEquals(499, list.size()); + Assert.assertEquals(252, list.get(250)); + Assert.assertEquals(500, list.get(498)); + } + + @Test + public void iteratorTest(){ + Iterator iterator = list.iterator(); + int count = 1; + while(iterator.hasNext()){ + Assert.assertEquals(++count, iterator.next()); + } + } + +} diff --git a/group27/772642286/test/BinaryTreeNodeTest.java b/group27/772642286/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..c0e12d2eb5 --- /dev/null +++ b/group27/772642286/test/BinaryTreeNodeTest.java @@ -0,0 +1,6 @@ +package week01.test; + +public class BinaryTreeNodeTest { + + +} diff --git a/group27/772642286/test/LinkedListTest.java b/group27/772642286/test/LinkedListTest.java new file mode 100644 index 0000000000..cae56aad19 --- /dev/null +++ b/group27/772642286/test/LinkedListTest.java @@ -0,0 +1,52 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.Iterator; +import week01.basic.LinkedList; + +public class LinkedListTest { + LinkedList list = null; + + @Before + public void init(){ + list = new LinkedList(); + for(int i=1;i<=500;i++){ + list.add(i); + } + } + + @Test + public void addTest(){ + Assert.assertEquals(500, list.size()); + for(int i=1;i<=list.size();i++){ + Assert.assertEquals(i, list.get(i-1)); + } + } + + @Test + public void addIndexTest(){ + list.add(250, 3333); + Assert.assertEquals(3333, list.get(250)); + Assert.assertEquals(500, list.get(500)); + } + + @Test + public void removeIndexTest(){ + list.remove(250); + Assert.assertEquals(499, list.size()); + Assert.assertEquals(252, list.get(250)); + Assert.assertEquals(500, list.get(498)); + } + + @Test + public void iteratorTest(){ + Iterator iterator = list.iterator(); + int count = 0; + while(iterator.hasNext()){ + Assert.assertEquals(++count, iterator.next()); + } + } +} diff --git a/group27/772642286/test/QueueTest.java b/group27/772642286/test/QueueTest.java new file mode 100644 index 0000000000..14008f51b2 --- /dev/null +++ b/group27/772642286/test/QueueTest.java @@ -0,0 +1,45 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; + +import week01.basic.Queue; + +public class QueueTest { + + private Queue queue; + + @Before + public void init(){ + queue = new Queue(); + for(int i=1;i<=500;i++){ + queue.enQueue(i); + } + } + + public void enQueueTest(){ + Assert.assertEquals(500, queue.size()); + } + + public void deQueue(){ + for(int i=500;i>=1 ;i--){ + Assert.assertEquals(i, queue.deQueue()); + } + + } + + public void isEmpty(){ + Assert.assertEquals(false, queue.isEmpty()); + for(int i=500;i>=1 ;i--){ + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertEquals(true, queue.isEmpty()); + } + + public void size(){ + for(int i=499;i>0 ;i--){ + queue.deQueue(); + Assert.assertEquals(i, queue.size()); + } + } +} diff --git a/group27/772642286/test/StackTest.java b/group27/772642286/test/StackTest.java new file mode 100644 index 0000000000..40f0928a14 --- /dev/null +++ b/group27/772642286/test/StackTest.java @@ -0,0 +1,58 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void init(){ + stack = new Stack(); + for(int i=1;i<=500;i++){ + stack.push(i); + } + } + + @Test + public void pushTest(){ + Assert.assertEquals(500, stack.size()); + } + + @Test + public void popTest(){ + for(int i=1;i<=500 ;i++){ + Assert.assertEquals(i, stack.pop()); + } + } + + @Test + public void peekTest(){ + Assert.assertEquals(1, stack.peek()); + Assert.assertEquals(1, stack.peek()); + Assert.assertEquals(1, stack.pop()); + Assert.assertEquals(2, stack.peek()); + Assert.assertEquals(2, stack.peek()); + } + + @Test + public void isEmpty(){ + Assert.assertEquals(false, stack.isEmpty()); + for(int i=1;i<=500 ;i++){ + Assert.assertEquals(i, stack.pop()); + } + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void size(){ + for(int i=499;i>0 ;i--){ + stack.pop(); + Assert.assertEquals(i, stack.size()); + } + } +}