diff --git a/group22/1335499238/week01/src/basic/ArrayList.java b/group22/1335499238/week01/src/basic/ArrayList.java new file mode 100644 index 0000000000..64b3045312 --- /dev/null +++ b/group22/1335499238/week01/src/basic/ArrayList.java @@ -0,0 +1,123 @@ +package basic; + +import java.util.Arrays; + +public class ArrayList implements List{ + + private int size; + + private Object[] elementData = {}; + + public ArrayList(){ + this(16); + } + + public ArrayList(int capacity){ + if(capacity > 0){ + elementData = new Object[capacity]; + }else if(capacity == 0){ + + }else{ + new IllegalArgumentException("initsize:"+capacity); + } + } + + @Override + public void add(Object o) { + ensureCapacity(elementData.length + 1); + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + checkIndex(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Object removeparam = elementData[index]; + int numMoved = size - index - 1; + if(numMoved > 0){ + System.arraycopy(elementData, index+1, elementData, index, numMoved); + } + elementData[--size] = null; //置空末尾元素 + return removeparam; + } + + @Override + public int size() { + return size; + } + + /** + * 检查是否越界 + * @param index + */ + public void checkIndex(int index){ + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("current:"+index+" size:"+size); + } + } + + /** + * 判断当前容量是否足够 + * @param minCapacity + */ + private void ensureCapacity(int minCapacity){ + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + /** + * 扩容 + * @param minCapacity + */ + private void grow(int minCapacity){ + Object [] target = new Object [minCapacity+10]; + System.arraycopy(elementData, 0, target, 0, elementData.length); + elementData = target; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + int i = current; + if(current >= size){ + throw new IndexOutOfBoundsException("current:"+current+" size:"+size); + } + current++; + return elementData[i]; + } + + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData, size)); + } + +} diff --git a/group22/1335499238/week01/src/basic/BinaryTreeNode.java b/group22/1335499238/week01/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..92ca37ef18 --- /dev/null +++ b/group22/1335499238/week01/src/basic/BinaryTreeNode.java @@ -0,0 +1,81 @@ +package basic; + +public class BinaryTreeNode >{ + + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T o){ + this.data = o; + this.left = null; + this.right = null; + } + + 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){ + BinaryTreeNode current = this; + BinaryTreeNode addTreeNode = new BinaryTreeNode<>(o); + while(true){ + //如果传入的值比但前节点的值小 + if(o.compareTo(current.data) < 0){ + if(current.left != null){ + current = current.left; + }else { + current.left = addTreeNode; + break; + } + }else{ + if(current.right != null){ + current = current.right; + }else{ + current.right =addTreeNode; + break; + } + } + } + return addTreeNode; + } + + public LinkedList prevOrder(BinaryTreeNode binaryTreeNode){ + LinkedList linkedList = new LinkedList(); + preOrder(binaryTreeNode, linkedList); + return linkedList; + } + + private void preOrder(BinaryTreeNode binaryTreeNode,LinkedList linkedList){ + if(binaryTreeNode.left != null){ + preOrder(binaryTreeNode.left, linkedList); + + } + linkedList.add(binaryTreeNode.data); + if(binaryTreeNode.right != null){ + preOrder(binaryTreeNode.right, linkedList); + } + } + +} diff --git a/group22/1335499238/week01/src/basic/Iterator.java b/group22/1335499238/week01/src/basic/Iterator.java new file mode 100644 index 0000000000..576b1a4af4 --- /dev/null +++ b/group22/1335499238/week01/src/basic/Iterator.java @@ -0,0 +1,9 @@ +package basic; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + +} diff --git a/group22/1335499238/week01/src/basic/LinkedList.java b/group22/1335499238/week01/src/basic/LinkedList.java new file mode 100644 index 0000000000..9af1471bfb --- /dev/null +++ b/group22/1335499238/week01/src/basic/LinkedList.java @@ -0,0 +1,206 @@ +package basic; + + +public class LinkedList implements List{ + + private Node head; + + private int size; + + @Override + public void add(Object o) { + addLast(o); + } + + @Override + public void add(int index, Object o) { + checkIndex(index); + Node current = findByIndex(index); + Node newNode = new Node(o, current); + if(index == 0){ + head = newNode; + }else{ + Node perv = findByIndex(index-1); + perv.next = newNode; + } + size++; + } + + @Override + public Object get(int index) { + checkIndex(index); + return findByIndex(index).data; + } + + @Override + public Object remove(int index) { + Node remove = null; + checkIndex(index); + Node next = findByIndex(index+1); + if(index == 0){ + remove = head; + if(next == null){ + head = null; + }else { + head = next; + } + }else{ + Node perv = findByIndex(index-1); + remove = perv.next; + perv.next = next; + } + size--; + return remove.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o){ + head = new Node(o, head); + size++; + } + + public void addLast(Object o){ + Node nextNode = new Node(o, null); + if(head == null){ + head = nextNode; + }else{ + Node lastNode = findByIndex(size-1); + lastNode.next = nextNode; + } + 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 current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + Node findByIndex = findByIndex(current); + if(current >= size){ + throw new IndexOutOfBoundsException("current:"+current+" size:"+size); + } + current++; + return findByIndex.data; + } + + } + + private static class Node{ + Object data; + Node next; + + Node(Object data, Node next){ + this.data = data; + this.next = next; + } + } + + private Node findByIndex(int index){ + Node lastNode = head; + for (int i = 0; i < index; i++) { + lastNode = lastNode.next; + } + return lastNode; + } + + private void checkIndex(int index){ + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("current:"+index+" size:"+size); + } + } + /** + * 把该链表逆置 + * 例如链表为 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){ + + } + /** + * 假定当前链表和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/group22/1335499238/week01/src/basic/List.java b/group22/1335499238/week01/src/basic/List.java new file mode 100644 index 0000000000..82612a4487 --- /dev/null +++ b/group22/1335499238/week01/src/basic/List.java @@ -0,0 +1,15 @@ +package 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/group22/1335499238/week01/src/basic/Queue.java b/group22/1335499238/week01/src/basic/Queue.java new file mode 100644 index 0000000000..81eb073387 --- /dev/null +++ b/group22/1335499238/week01/src/basic/Queue.java @@ -0,0 +1,21 @@ +package basic; + +public class Queue { + + private LinkedList linkList = new LinkedList(); + + public void enQueue(Object o){ + linkList.add(o); + } + + public Object deQueue(){ + return linkList.removeFirst(); + } + + public boolean isEmpty(){ + return linkList.size() == 0; + } + public int size(){ + return linkList.size(); + } +} diff --git a/group22/1335499238/week01/src/basic/Stack.java b/group22/1335499238/week01/src/basic/Stack.java new file mode 100644 index 0000000000..38baac269a --- /dev/null +++ b/group22/1335499238/week01/src/basic/Stack.java @@ -0,0 +1,29 @@ +package basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + return elementData.remove(size-1); + } + + public Object peek(){ + return elementData.get(size-1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group22/1335499238/week01/src/test/ArrayListTest.java b/group22/1335499238/week01/src/test/ArrayListTest.java new file mode 100644 index 0000000000..bf54c307dc --- /dev/null +++ b/group22/1335499238/week01/src/test/ArrayListTest.java @@ -0,0 +1,40 @@ +package test; + + +import org.junit.Assert; +import org.junit.Test; + +import basic.ArrayList; +import basic.Iterator; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList arrayList = new ArrayList(); + arrayList.add("612"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("5"); + arrayList.add("6"); + Assert.assertEquals("[612, 1, 2, 5, 6]", arrayList.toString()); + + Object remove = arrayList.remove(2); + Assert.assertEquals("2", remove); + + arrayList.add(2, "13"); + Assert.assertEquals("[612, 1, 13, 5, 6]", arrayList.toString()); + + Object object = arrayList.get(2); + Assert.assertEquals("13", object); + + Assert.assertEquals(5, arrayList.size()); + + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next()+" "); + + } + } + +} diff --git a/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java b/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..18f3b7897b --- /dev/null +++ b/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java @@ -0,0 +1,26 @@ +package test; + +import org.junit.Test; + +import basic.BinaryTreeNode; +import basic.Iterator; +import basic.LinkedList; + +public class BinaryTreeNodeTest { + + @Test + public void test01(){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(20); + binaryTreeNode.insert(5); + binaryTreeNode.insert(40); + binaryTreeNode.insert(30); + binaryTreeNode.insert(10); + binaryTreeNode.insert(15); + LinkedList prevOrder = binaryTreeNode.prevOrder(binaryTreeNode); + Iterator iterator = prevOrder.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next()+" "); + } + } + +} diff --git a/group22/1335499238/week01/src/test/LinkedListTest.java b/group22/1335499238/week01/src/test/LinkedListTest.java new file mode 100644 index 0000000000..8a7fa7f444 --- /dev/null +++ b/group22/1335499238/week01/src/test/LinkedListTest.java @@ -0,0 +1,52 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Iterator; +import basic.LinkedList; + +public class LinkedListTest { + + @Test + public void test01(){ + LinkedList linkedList = new LinkedList(); + linkedList.add(122); + linkedList.add("qwe"); + linkedList.add(133); + iterator(linkedList); + + linkedList.add(1, "asd"); + iterator(linkedList); + + linkedList.addFirst("1"); + iterator(linkedList); + + linkedList.addLast("zxc"); + iterator(linkedList); + + Object remove = linkedList.remove(2); + Assert.assertEquals("asd", remove); + + Object removeFirst = linkedList.removeFirst(); + Assert.assertEquals("1", removeFirst); + + Object removeLast = linkedList.removeLast(); + Assert.assertEquals("zxc", removeLast); + + int size = linkedList.size(); + Assert.assertEquals(3, size); + + + + } + + public static void iterator(LinkedList linkedList){ + Iterator iterator = linkedList.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next()+" "); + } + System.out.println(); + } + +} diff --git a/group22/1335499238/week01/src/test/QueueTest.java b/group22/1335499238/week01/src/test/QueueTest.java new file mode 100644 index 0000000000..61d4eb91c9 --- /dev/null +++ b/group22/1335499238/week01/src/test/QueueTest.java @@ -0,0 +1,30 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Queue; + +public class QueueTest { + + @Test + public void test01(){ + + Queue queue = new Queue(); + boolean empty1 = queue.isEmpty(); + Assert.assertEquals(true, empty1); + queue.enQueue("111"); + queue.enQueue("222"); + queue.enQueue("333"); + Object deQueue = queue.deQueue(); + Assert.assertEquals("111", deQueue); + + boolean empty2 = queue.isEmpty(); + Assert.assertEquals(false, empty2); + + int size = queue.size(); + Assert.assertEquals(2, size); + } + + +} diff --git a/group22/1335499238/week01/src/test/StackTest.java b/group22/1335499238/week01/src/test/StackTest.java new file mode 100644 index 0000000000..95c440b8af --- /dev/null +++ b/group22/1335499238/week01/src/test/StackTest.java @@ -0,0 +1,32 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Stack; + +public class StackTest { + + @Test + public void test1(){ + Stack stack = new Stack(); + + Assert.assertEquals(true, stack.isEmpty()); + + stack.push(123); + stack.push("qwe"); + stack.push(456); + + Assert.assertEquals(false, stack.isEmpty()); + + int size = stack.size(); + Assert.assertEquals(3, size); + + Object peek = stack.peek(); + Assert.assertEquals(456, peek); + + Object pop = stack.pop(); + Assert.assertEquals(456, pop); + } + +}