From 28621ba8bfbc5f4807e384e4e81c698cb1fe4bdf Mon Sep 17 00:00:00 2001 From: Kilien Date: Sun, 26 Feb 2017 10:39:36 +0800 Subject: [PATCH 01/11] Create test.txt hello world --- group09/790466157/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group09/790466157/test.txt diff --git a/group09/790466157/test.txt b/group09/790466157/test.txt new file mode 100644 index 0000000000..3b18e512db --- /dev/null +++ b/group09/790466157/test.txt @@ -0,0 +1 @@ +hello world From 3c803acdd0e35a2a5b62bb90c2a1c8f621a1e171 Mon Sep 17 00:00:00 2001 From: "LAPTOP-DBDFJEC7\\Liner" Date: Sun, 26 Feb 2017 10:42:40 +0800 Subject: [PATCH 02/11] week01 --- .../src/com/coding/basic/Iterator.java | 7 + .../790466157/src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/MyArrayList.java | 133 +++++++++++++++ .../src/com/coding/basic/MyLinkedList.java | 159 ++++++++++++++++++ .../790466157/src/com/coding/basic/Queue.java | 68 ++++++++ .../790466157/src/com/coding/basic/Stack.java | 73 ++++++++ 6 files changed, 449 insertions(+) create mode 100644 group09/790466157/src/com/coding/basic/Iterator.java create mode 100644 group09/790466157/src/com/coding/basic/List.java create mode 100644 group09/790466157/src/com/coding/basic/MyArrayList.java create mode 100644 group09/790466157/src/com/coding/basic/MyLinkedList.java create mode 100644 group09/790466157/src/com/coding/basic/Queue.java create mode 100644 group09/790466157/src/com/coding/basic/Stack.java diff --git a/group09/790466157/src/com/coding/basic/Iterator.java b/group09/790466157/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..7c02cc6e51 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/List.java b/group09/790466157/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.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(); +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyArrayList.java b/group09/790466157/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..cf232c5b72 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; +import java.util.Arrays; +import java.util.NoSuchElementException; + +import com.coding.basic.List; +import com.coding.basic.Iterator; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE; + + private static final int DEFAULT_CAPACITY = 10; + + + //�޳����캯�� + public MyArrayList(){ + this(DEFAULT_CAPACITY); + } + + public MyArrayList(int size){ + if (size < 0){ + throw new IllegalArgumentException("Ĭ�ϵĴ�С" + size); + } + else{ + elementData = new Object[size]; + } + } + + public void add(Object o){ + isCapacityEnough(size+1); + elementData[size++] = o; + } + + private void isCapacityEnough(int size){ + //�ж��Ƿ񳬹���ʼ�������Ƿ���Ҫ���� + if (size > DEFAULT_CAPACITY){ + explicitCapacity(size); + } + if (size < 0){ + throw new OutOfMemoryError(); + } + } + + private void explicitCapacity(int capacity){ + int oldCapacity = elementData.length; + //������=������ + ��������/2�� ����1.5�������Ʋ������൱�ڳ���2�� + int newLength = oldCapacity + (oldCapacity >> 1); + if (newLength - capacity < 0){ + newLength = capacity; + } + //�ж�newLength�ij��� + //����������涨���������󳤶����ж�Ҫ��Ҫ�����ݿռ��Ƿ����������󳤶� + //���������newLengthΪ MAX_VALUE ������Ϊ MAX_ARRAY_LENGTH�� + if (newLength > (MAX_ARRAY_LENGTH)){ + newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + //����copyof�������� + elementData = Arrays.copyOf(elementData, newLength); + } + + public void add(int index, Object o){ + + checkRangeForAdd(index); + isCapacityEnough(size +1); + // �� elementData�д�Indexλ�ÿ�ʼ������Ϊsize-index��Ԫ�أ� + // ���������±�Ϊindex+1λ�ÿ�ʼ���µ�elementData�����С� + // ������ǰλ�ڸ�λ�õ�Ԫ���Լ����к���Ԫ������һ��λ�á� + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + //�ж��Ƿ�Խ�� + private void checkRangeForAdd(int index){ + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("ָ����indexԽ��"); + } + } + + // ���ش��б���ָ��λ���ϵ�Ԫ�ء� + public Object get(int index){ + checkRange(index); + return elementData[index]; + } + + //�ж��Ƿ�Խ�� + private void checkRange(int index){ + if (index >= size || index < 0){ + throw new IndexOutOfBoundsException("ָ����indexԽ��"); + } + } + + public Object remove(int index){ + Object value = get(index); + int moveSize = size - index -1; + if (moveSize >0){ + System.arraycopy(elementData, index +1, elementData, index, size - index -1); + + } + elementData[--size] = null; + return value; + } + + public int size(){ + return size; + } + + //������ + public Iterator iterator(Object o){ + return new ArrayListIterator(); + } + + + private class ArrayListIterator implements Iterator{ + private int currentIndex=0; + + public boolean hasNext() { + return currentIndex < size(); + } + + public Object next() { + if (!hasNext()){ + throw new NoSuchElementException(); + } + return new Object[currentIndex + 1]; + } + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyLinkedList.java b/group09/790466157/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..4727db3a89 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,159 @@ +package com.coding.basic; + +public class MyLinkedList { + + private int size; + + private Node first; + + private Node last; + + + public boolean add(E element) { + addAtLast(element); + return true; + } + + private void addAtLast(E element) { + Node l = last; + Node node = new Node(element, null, l); + last = node; + if (l == null) { + first = node; + } else { + l.next = node; + } + size++; + } + + public void add(int index, E element) { + checkRangeForAdd(index); + if (index == size) { + addAtLast(element); + } else { + Node l = node(index); + addBeforeNode(element, l); + } + } + + private void addBeforeNode(E element, Node specifiedNode) { + Node preNode = specifiedNode.prev; + Node newNode = new Node(element, specifiedNode, preNode); + if (preNode == null) { + first = newNode; + } else { + preNode.next = newNode; + } + specifiedNode.prev = newNode; + size++; + } + + + private Node node(int index) { + if (index < (size << 1)) { + Node cursor = first; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } else { + Node cursor = last; + for (int i = size - 1; i > index; i--) { + cursor = cursor.prev; + } + return cursor; + } + } + + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("ָ����index��������"); + } + } + + public E get(int index) { + checkRange(index); + return node(index).item; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("ָ��index��������"); + } + } + + public int indexOf(Object element) { + Node cursor = first; + int count = 0; + while (cursor != null) { + if (element != null) { + if (element.equals(cursor.item)) { + return count; + } + }else{ + if (cursor.item == null) { + return count; + } + } + count ++; + cursor = cursor.next; + } + return -1; + } + + public E remove(int index) { + checkRange(index); + return deleteLink(index); + } + + public boolean remove(Object o) { + int index = indexOf(o); + if (index < 0){ + return false; + } + deleteLink(index); + return true; + } + + private E deleteLink(int index) { + Node l = node(index); + E item = l.item; + Node prevNode = l.prev; + Node nextNode = l.next; + + if (prevNode == null) { + first = nextNode; + }else{ + prevNode.next = nextNode; + l.next = null; + } + + if (nextNode == null) { + last = prevNode; + }else{ + nextNode.prev = prevNode; + l.prev = null; + } + size--; + l.item = null; + return item; + } + + + + public int size(){ + return size; + } + private static class Node { + E item; + Node next; + Node prev; + + public Node(E item, Node next, Node prev) { + this.item = item; + this.next = next; + this.prev = prev; + + } + } +} diff --git a/group09/790466157/src/com/coding/basic/Queue.java b/group09/790466157/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..80d0dc9835 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; +import java.util.Arrays; + +public class Queue { + private static final int CAPACITY = 10; + private static int capacity; + private static int front; + private static int tail; + private static Object[] array; + + public Queue(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + front = tail = 0; + } + + public void enQueue(Object o) throws ExceptionQueueFull { + if (size() == capacity -1) + throw new ExceptionQueueFull("Queue is full"); + array[tail] = o; + tail = (tail +1) % capacity; + } + + public Object deQueue() throws ExceptionQueueEmpty{ + Object o; + if (isEmpty()) + throw new ExceptionQueueEmpty("Queue is empty"); + o = array[front]; + front = (front + 1) % capacity; + return o; + } + + public boolean isEmpty(){ + return (front == tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (capacity + tail - front) % capacity; + } + + public class ExceptionQueueEmpty extends Exception { + // Constructor + public ExceptionQueueEmpty() { + + } + + // Constructor with parameters + public ExceptionQueueEmpty(String mag) { + System.out.println(mag); + } + } + + public class ExceptionQueueFull extends Exception { + + // Constructor + public ExceptionQueueFull() { + + } + + // Constructor with parameters + public ExceptionQueueFull(String mag) { + System.out.println(mag); + } + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/Stack.java b/group09/790466157/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..324cc5639e --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Stack.java @@ -0,0 +1,73 @@ +package com.coding.basic; +import java.util.Arrays; +import com.coding.basic.MyArrayList; +public class Stack { + private MyArrayList elementData = new MyArrayList(); + private static final int CAPACITY = 10; + private static int capacity; + private static int top = -1; + Object[] array; + + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws ExceptionStackFull{ + if(size()== CAPACITY){ + throw new ExceptionStackFull("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + + public class ExceptionStackEmpty extends Exception { + + //Constructor + public ExceptionStackEmpty(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackEmpty(String string){ + super(string); + } + } + + public class ExceptionStackFull extends Exception { + + //Constructor + public ExceptionStackFull(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackFull(String string){ + super(string); + } + } +} \ No newline at end of file From 9b12a29ffbd54ddfcef8100daeaa4c62c318fe0c Mon Sep 17 00:00:00 2001 From: glorychou Date: Sun, 26 Feb 2017 14:12:14 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20170226/src/per/zyf/bds/ArrayList.java | 174 +++++++++++++ .../20170226/src/per/zyf/bds/BinaryTree.java | 81 ++++++ .../20170226/src/per/zyf/bds/LinkedList.java | 246 ++++++++++++++++++ .../20170226/src/per/zyf/bds/List.java | 55 ++++ .../20170226/src/per/zyf/bds/Queue.java | 59 +++++ .../20170226/src/per/zyf/bds/Stack.java | 71 +++++ 6 files changed, 686 insertions(+) create mode 100644 group09/396077060/20170226/src/per/zyf/bds/ArrayList.java create mode 100644 group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java create mode 100644 group09/396077060/20170226/src/per/zyf/bds/LinkedList.java create mode 100644 group09/396077060/20170226/src/per/zyf/bds/List.java create mode 100644 group09/396077060/20170226/src/per/zyf/bds/Queue.java create mode 100644 group09/396077060/20170226/src/per/zyf/bds/Stack.java diff --git a/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java new file mode 100644 index 0000000000..a64cb7cd19 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java @@ -0,0 +1,174 @@ +/** +* @Title: ArrayList.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月22日 下午10:41:58 +*/ +package per.zyf.bds; + +import java.util.Arrays; + +/** + * ArrayList的存储结构其实就是一维数组 + * 只不过在这个类中将对数组的一些操作(包括添加元素、删除元素等)封装了起来 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +public class ArrayList implements List { + // 默认数组大小 + private static final int DEFAULT_CAPACITY = 10; + + // 数组实际大小 + private int size; + + // 存储元素的数组 + protected Object[] elementData; + + // 一个用来记录初始状态的空数组实例 + private static final Object[] CAPACITY_EMPTY_ELEMENTDATA = {}; + + /*** + * 构造初始元素数组 + */ + public ArrayList() { + this.elementData = CAPACITY_EMPTY_ELEMENTDATA; + } + + /*** + * + * @Description: 在末尾添加元素 + * @param e 元素 + */ + @Override + public boolean add(E e) { + int minCapacity = size + 1; + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 添加元素 + elementData[size++] = e; + + return true; + } + + /*** + * + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 元素 + */ + @Override + public boolean add(int index, E e) { + int minCapacity = size + 1; + + // 索引位置不合法抛出异常 + rangeCheck(index); + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 插入点后的元素后移 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + // 在索引处加入数据 + elementData[index] = e; + + return true; + } + + /*** + * + * @Description: 得到索引指定位置的元素 + * @param index 索引 + * @return E 索引指定的元素 + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + return (E) elementData[index]; + } + + /*** + * + * @Description: 删除索引指定位置的元素 + * @param index 索引 + * @return void + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + E removeElement = (E) elementData[index]; + + // 将要移除元素后的元素前移 + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + // 数组大小减一,并清除多余元素 + elementData[--size] = null; + + return removeElement; + } + + /* + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + + /* + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + return true; + } + return false; + } + + /*** + * + * @Description: 溢出时增长空间 + * @param minCapacity 最小容量 + * @return void + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + // 容量增大一半 + int newCapacity = oldCapacity + (oldCapacity >> 1); + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} \ No newline at end of file diff --git a/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java new file mode 100644 index 0000000000..87d7010456 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java @@ -0,0 +1,81 @@ +/** +* @Title: BinaryTree.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月25日 下午10:22:03 +*/ +package per.zyf.bds; + +import java.util.Comparator; + +/** + * @author glorychou + * + */ +public class BinaryTree> { + // 根节点 + private Node root; + // 树大小 + private int size; + + /** + * @Description: 在树中插入元素 + * @param e 节点数据 + * @return boolean 处理情况 + */ + public boolean add(E e) { + + // 创建新节点 + final Node newNode = new Node<>(null, e, null); + + // 按照二叉排序方式插入 + if (root != null) { + Node parentNode = null; + Node compareNode = root; + + while(compareNode != null) { + // 新节点大于比较节点则插入右子树中 + if(e.compareTo(compareNode.item) > 0) { + parentNode = compareNode; + compareNode = compareNode.rightChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } else {// 新节点小于或等于比较节点则插入左子树中 + parentNode = compareNode; + compareNode = compareNode.leftChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } + } + } else + root = newNode; + + return true; + } + + /** + * @Description: 中序遍历输出 + * @return void 返回类型 + */ + public void inorderPrint(Node e) { + if(e == null) return; + inorderPrint(e.leftChild); + System.out.print(e.item.toString() + " "); + inorderPrint(e.rightChild); + } + + // 树节点 + private static class Node { + E item; + Node leftChild; + Node rightChild; + + Node(Node l, E e, Node r) { + leftChild = l; + item = e; + rightChild = r; + } + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java new file mode 100644 index 0000000000..3955ef22ab --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java @@ -0,0 +1,246 @@ +/** +* @Title: LinkedList.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 上午12:23:00 +*/ +package per.zyf.bds; + +/** + * LinkedList的存储结构其实就是链表 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +/** + * @author glorychou + * + * @param + */ +/** + * @author glorychou + * + * @param + */ +public class LinkedList implements List { + // 链表头 + private Node first; + + // 链表尾 + private Node last; + + // 链表大小 + private int size; + + /* + * @see per.zyf.bds.List#add(java.lang.Object) + */ + @Override + public boolean add(E e) { + linkLast(e); + return true; + } + + /* + * @see per.zyf.bds.List#add(int, java.lang.Object) + */ + @Override + public boolean add(int index, E e) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + // 索引值为链表长度,则添加到链表末尾 + if (index == size) { + linkLast(e); + } else { + // 找到索引指定的节点,然后将新节点插入该节点后面 + final Node p = node(index); + final Node newNode = new Node<>(p, e, p.next); + p.next = newNode; + size++; + } + + return true; + } + + /* + * @see per.zyf.bds.List#get(int) + */ + @Override + public E get(int index) { + return node(index).item; + } + + + /* (non-Javadoc) + * @see per.zyf.bds.List#remove(int) + */ + @Override + public E remove(int index) { + rangeCheck(index); + + Node p = node(index); + + // 所需删除节点的前面有节点,则改变前一节点的下一跳 + if(p.prev != null) + p.prev.next = p.next; + // 所需删除节点的后面有节点,则改变后一节点的上一跳 + if(p.next != null) + p.next.prev = p.prev; + + size--; + + return p.item; + } + + /** + * @Description: 在链表头部增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + public void addFirst(E e) { + final Node newNode = new Node<>(null, e, first); + first.prev = newNode; + size++; + } + + /** + * @Description: 移除首节点 + * @return E 所删除节点的数据 + */ + public E removeFirst() { + if(first != null) { + final E e = first.item; + final Node n = first.next; + + if(n != null) + n.prev = null; + + // 清空数据,让GC来回收内存 + first.item = null; + first.next = null; + // 指定新的头节点 + first = n; + + size--; + return e; + } else + return null; + } + + /** + * @Description: 删除最后一个节点 + * @return 设定文件 + * @return E 所删除数据 + * @throws + */ + public E removeLast() { + if(last != null) { + final E e = last.item; + final Node p = last.prev; + + if(p != null) + p.next = null; + + // 清空数据,让GC来回收内存 + last.item = null; + last.prev = null; + // 指定新的尾节点 + last = p; + + size--; + return e; + } else + return null; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + return true; + } + + /** + * @Description: 在链尾增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + private void linkLast(E e) { + // 保存旧链尾节点 + final Node p = last; + + // 创建新节点,并将新节点设置为链尾节点 + final Node newNode = new Node<>(p, e, null); + last = newNode; + + // 若链表无节点,则将新节点设置为表头节点 + if (p == null) + first = newNode; + else // 若链表已经有节点,则将新节点设置为表尾节点 + p.next = newNode; + + // 表长度加一 + size++; + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + + /** + * @Description: 获取索引位置的节点 + * @param index 索引 + * @return Node 指定的节点 + */ + private Node node(int index) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + Node specifyNode; + + // 根据判断索引位置在链表的前半部还是后半部,选择不同的检索方式获取指定节点 + if(index < (size >> 1)) { + specifyNode = first; + for (int i = 0; i < index; i++) + specifyNode = specifyNode.next; + } else { + specifyNode = last; + for (int i = 0; i > index; i--) + specifyNode = specifyNode.prev; + } + + return specifyNode; + } + + // 链表节点 + private static class Node { + E item; + Node next; + Node prev; + + Node(Node prev, E e, Node next) { + this.prev = prev; + this.item = e; + this.next = next; + } + } + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/List.java b/group09/396077060/20170226/src/per/zyf/bds/List.java new file mode 100644 index 0000000000..847edd1013 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/List.java @@ -0,0 +1,55 @@ +/** +* @Title: List.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:02:34 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public interface List { + /** + * @Description: 添加元素 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(E e); + + /** + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(int index, E e); + + /** + * @Description: 获取指定元素 + * @param index 索引 + * @return E 返回元素 + */ + E get(int index); + + /** + * @Description: 删除指定元素 + * @param index 索引 + * @return E 返回被删除的元素 + */ + E remove(int index); + + /** + * @Description: 获取List容量 + * @return int List容量 + */ + int size(); + + /** + * @Description: 判断是否为空 + * @return boolean 是否为空 + */ + boolean isEmpty(); + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Queue.java b/group09/396077060/20170226/src/per/zyf/bds/Queue.java new file mode 100644 index 0000000000..70b72c9b49 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Queue.java @@ -0,0 +1,59 @@ +/** +* @Title: Queue.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:10:08 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Queue { + // 队列元素 + private LinkedList elementData; + + // 队列大小 + private int size; + + public Queue() { + elementData = new LinkedList<>(); + } + + /** + * @Description: 入队 + * @param e 入队数据 + * @return void 返回类型 + */ + public void enQueue(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 出队 + * @return E 出队的数据 + */ + public E deQueue() { + final E e = elementData.removeFirst(); + size = elementData.size(); + return e; + } + + /** + * @Description: 判断队列是否为空 + * @return boolean 是否为空 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取队列大小 + * @return int 队列大小 + */ + public int size() { + return size; + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Stack.java b/group09/396077060/20170226/src/per/zyf/bds/Stack.java new file mode 100644 index 0000000000..bf8d4f5611 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Stack.java @@ -0,0 +1,71 @@ +/** +* @Title: Stack.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:05:29 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Stack { + // 栈元素 + private ArrayList elementData; + + // 栈大小 + private int size; + + /** + * 初始化栈 + */ + public Stack() { + elementData = new ArrayList<>(); + } + + /** + * @Description: 压栈 + * @param e 数据 + * @return void 返回类型 + */ + public void push(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 弹栈 + * @return E 所弹出的数据 + */ + public E pop() { + // 移除最后一项数据 + final E e = elementData.remove(elementData.size()); + size = elementData.size(); + return e; + } + + /** + * @Description: 获取栈顶元素 + * @return E 返回栈顶元素数据 + */ + public E peek() { + return elementData.get(size - 1); + } + + /** + * @Description: 判断栈是否为空 + * @return boolean 返回类型 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取栈大小 + * @return int 栈大小 + */ + public int size() { + return size; + } +} From 1a4d1746c087a2c5f7a4f880405c3c0578bfda9f Mon Sep 17 00:00:00 2001 From: guohairui Date: Sun, 26 Feb 2017 19:05:03 +0800 Subject: [PATCH 04/11] mylist --- group09/85839593/assignment-0215-0226/pom.xml | 15 +++ .../src/main/java/MyArrayList.java | 84 ++++++++++++++++ .../src/main/java/MyLinkedList.java | 99 +++++++++++++++++++ .../src/test/java/MyArrayListTest.java | 75 ++++++++++++++ .../src/test/java/MyLinkedListTest.java | 70 +++++++++++++ group09/85839593/pom.xml | 16 +++ 6 files changed, 359 insertions(+) create mode 100644 group09/85839593/assignment-0215-0226/pom.xml create mode 100644 group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java create mode 100644 group09/85839593/assignment-0215-0226/src/main/java/MyLinkedList.java create mode 100644 group09/85839593/assignment-0215-0226/src/test/java/MyArrayListTest.java create mode 100644 group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java create mode 100644 group09/85839593/pom.xml diff --git a/group09/85839593/assignment-0215-0226/pom.xml b/group09/85839593/assignment-0215-0226/pom.xml new file mode 100644 index 0000000000..9d40995970 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/pom.xml @@ -0,0 +1,15 @@ + + + + assignment + assignment + 1.0-SNAPSHOT + + 4.0.0 + + assignment-0215-0226 + + + \ No newline at end of file diff --git a/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java b/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java new file mode 100644 index 0000000000..0fbd7771b4 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java @@ -0,0 +1,84 @@ +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-22 + * Time: 上午12:06 + * To change this template use File | Settings | File Templates. + */ +public class MyArrayList { + public int size = 0; + private Object [] elementData = new Object[5]; + public void add(int index,Object obj){ + if(index>size() ||index<0) + throw new IndexOutOfBoundsException("哎呀我去,不够了."); + elementData[index]=obj; + size++; + } + public void insert(int index,Object obj){ + if(size>elementData.length-1){ + System.out.println("当前size:" + size + " 当前length:" + elementData.length+",再插不够了,需要扩容"); + Object [] tmpData = elementData; + elementData =new Object[size+5] ; + System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length); + System.arraycopy(tmpData,0,elementData,0,index); + elementData[index]=obj; + System.arraycopy(tmpData,index,elementData,index+1,tmpData.length-index); + }else { + if(elementData[index]==null){ + elementData[index]=obj; + }else { + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index]=obj; + } + } + size++; + } + public void add(Object obj){ + if(size>=elementData.length){ + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + Object [] tmpData = elementData; + elementData =new Object[size+5] ; + System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length); + System.arraycopy(tmpData,0,elementData,0,size); + elementData[size]=obj; + }else { + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + elementData[size]=obj; + } + size++; + } + public Object get(int index) { + if(index>=size) + throw new IndexOutOfBoundsException("越了"); + return elementData[index]; + } + public Object remove(int index){ + Object delValue = elementData[index]; + int movesize = size-index-1; + System.out.print("size:"+size+" index:"+index+" ,size-index-1:"+movesize); + System.arraycopy(elementData,index+1,elementData,index,movesize); + System.out.print("删除后前移位,数组末位清空"); + elementData[--size]=null; + + return delValue; + } + public int size(){ + return size; + } + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append('['); + for (int i=0;i=size) + throw new RuntimeException("超出范围了"); + Node node = head; + if(index<(size>>1)){//当偏向于前一半时从头找,否则从尾找 + for ( int i=0;i<=index;i++) { + node = node.next; + } + }else { + for (int i=size;i>index;i--){ + node=node.previous; + } + } + return node; + } + + private static class Node{ + Object data;//当前Entry + Node next;//下一个 + Node previous;//前一个 + public Node(Object data,Node next,Node previous){ + this.data=data; + this.next=next; + this.previous=previous; + } + } + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append('['); + Node n =getNode(0) ; + for (int i=0;i>2); + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java new file mode 100644 index 0000000000..f0db06e6f8 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java @@ -0,0 +1,70 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午6:12 + * To change this template use File | Settings | File Templates. + */ +public class MyLinkedListTest { + @org.junit.Test + public void testAdd() throws Exception { + MyLinkedList linkedList = new MyLinkedList(); + linkedList.add("abc1"); + linkedList.add("abc2"); + linkedList.add("abc3"); + linkedList.add("abc4"); + System.out.println(linkedList.get(1)); + System.out.println(linkedList); + linkedList.add("abc5"); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.get(4)); + System.out.println(linkedList); + linkedList.add(2,"abcaddtmp"); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.get(4)); + System.out.println(linkedList); + linkedList.remove(2); + System.out.println(linkedList.toString()); + linkedList.removeLast(); + System.out.println(linkedList.toString()); + linkedList.removeFirst(); + System.out.println(linkedList.toString()); + } + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + + } + + @Test + public void testRemoveFirst() throws Exception { + + } + + @Test + public void testRemoveLast() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testGetNode() throws Exception { + + } +} diff --git a/group09/85839593/pom.xml b/group09/85839593/pom.xml new file mode 100644 index 0000000000..627fff7a1f --- /dev/null +++ b/group09/85839593/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + assignment + assignment + pom + 1.0-SNAPSHOT + + assignment-0215-0226 + + + + \ No newline at end of file From f22d1c4931e92ac2aebe3eb2aac91f2e808f955c Mon Sep 17 00:00:00 2001 From: guohairui Date: Sun, 26 Feb 2017 19:36:50 +0800 Subject: [PATCH 05/11] mystack myqueue --- .../src/main/java/MyQueue.java | 21 +++++++++++ .../src/main/java/MyStack.java | 30 ++++++++++++++++ .../src/test/java/MyQueueTest.java | 29 +++++++++++++++ .../src/test/java/MyStackTest.java | 36 +++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 group09/85839593/assignment-0215-0226/src/main/java/MyQueue.java create mode 100644 group09/85839593/assignment-0215-0226/src/main/java/MyStack.java create mode 100644 group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java create mode 100644 group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java diff --git a/group09/85839593/assignment-0215-0226/src/main/java/MyQueue.java b/group09/85839593/assignment-0215-0226/src/main/java/MyQueue.java new file mode 100644 index 0000000000..a085feebab --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/main/java/MyQueue.java @@ -0,0 +1,21 @@ +/** + * 先进先出 + */ +public class MyQueue { + private MyLinkedList myLinkedList = new MyLinkedList(); + public void enQueue(Object o){ + myLinkedList.add(o); + } + public void deQueue(){ + myLinkedList.remove(0) ; + } + public boolean isEmpty(){ + return myLinkedList.size<1; + } + public int size(){ + return myLinkedList.size; + } + public String toString(){ + return myLinkedList.toString(); + } +} diff --git a/group09/85839593/assignment-0215-0226/src/main/java/MyStack.java b/group09/85839593/assignment-0215-0226/src/main/java/MyStack.java new file mode 100644 index 0000000000..a6e93cd45f --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/main/java/MyStack.java @@ -0,0 +1,30 @@ +/** + * 先进后出 + */ +public class MyStack { + private MyArrayList myArrayList = new MyArrayList(); + public MyStack(){} + public void push(Object o) { + myArrayList.add(o); + } + public Object pop(){ + Object popo=myArrayList.get(myArrayList.size-1); + myArrayList.remove(myArrayList.size-1); + return popo; + } + public Object peek(){ + + return myArrayList.get(myArrayList.size-1); + + } + public boolean isEmpty(){ + + return myArrayList.size<1; + } + public int size(){ + return myArrayList.size; + } + public String toString(){ + return myArrayList.toString(); + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java new file mode 100644 index 0000000000..d93a70ba81 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java @@ -0,0 +1,29 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午7:33 + * To change this template use File | Settings | File Templates. + */ +public class MyQueueTest { + @org.junit.Test + public void testEnQueue() throws Exception { + MyQueue myQueue=new MyQueue(); + myQueue.enQueue("abc1"); + myQueue.enQueue("abc2"); + myQueue.enQueue("abc3"); + System.out.println(myQueue); + myQueue.enQueue("abc4"); + System.out.println(myQueue); + myQueue.deQueue(); + System.out.println(myQueue); + } + + @Test + public void testDeQueue() throws Exception { + + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java new file mode 100644 index 0000000000..b96d77dbbf --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java @@ -0,0 +1,36 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午7:20 + * To change this template use File | Settings | File Templates. + */ +public class MyStackTest { + @org.junit.Test + public void testPush() throws Exception { + MyStack myStack = new MyStack(); + myStack.push("abc1"); + myStack.push("abc2"); + myStack.push("abc3"); + System.out.println(myStack); + myStack.push("abc4"); + System.out.println(myStack); + System.out.println("myStack.peek:"+myStack.peek()); + myStack.pop(); + System.out.println("myStack.size"+myStack.size()); + System.out.println(myStack); + } + + @Test + public void testPop() throws Exception { + + } + + @Test + public void testPeek() throws Exception { + + } +} From 32f1254103b4c37496c9a05e23b1e00ed881f3b2 Mon Sep 17 00:00:00 2001 From: shaoqianqin <715061147@qq.com> Date: Sun, 26 Feb 2017 20:17:32 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group09/715061147/.gitignore | 4 + group09/715061147/mvnhomework1/.gitignore | 4 + group09/715061147/mvnhomework1/pom.xml | 29 +++ .../main/java/com/qsq/study/ArrayList.java | 143 +++++++++++++++ .../main/java/com/qsq/study/LinkedList.java | 169 ++++++++++++++++++ .../src/main/java/com/qsq/study/List.java | 12 ++ .../java/com/qsq/study/ArrayListTest.java | 70 ++++++++ .../java/com/qsq/study/LinkedListTest.java | 70 ++++++++ 8 files changed, 501 insertions(+) create mode 100644 group09/715061147/.gitignore create mode 100644 group09/715061147/mvnhomework1/.gitignore create mode 100644 group09/715061147/mvnhomework1/pom.xml create mode 100644 group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java create mode 100644 group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java create mode 100644 group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java create mode 100644 group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java create mode 100644 group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java diff --git a/group09/715061147/.gitignore b/group09/715061147/.gitignore new file mode 100644 index 0000000000..920bb9729c --- /dev/null +++ b/group09/715061147/.gitignore @@ -0,0 +1,4 @@ +.metadata +.swp +RemoteSystemsTempFiles/ +target diff --git a/group09/715061147/mvnhomework1/.gitignore b/group09/715061147/mvnhomework1/.gitignore new file mode 100644 index 0000000000..a65ec10715 --- /dev/null +++ b/group09/715061147/mvnhomework1/.gitignore @@ -0,0 +1,4 @@ +.classpath +.settings +.project +target diff --git a/group09/715061147/mvnhomework1/pom.xml b/group09/715061147/mvnhomework1/pom.xml new file mode 100644 index 0000000000..b6702aac65 --- /dev/null +++ b/group09/715061147/mvnhomework1/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + com.qsq.study + mvnhomework1 + 0.0.1-SNAPSHOT + MvnHomeWork1 + MvnHomeWork1 + + + + junit + junit + 4.12 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java new file mode 100644 index 0000000000..f2bdc27de9 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java @@ -0,0 +1,143 @@ +package com.qsq.study; + +import java.util.Arrays; + +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 16; + private static final Object[] EMPTY_ELEMENT_DATA = {}; + private Object[] elementData; + private int size = 0; + + /* + * ����һ��Ĭ��������ArrayList + */ + public ArrayList() { + this(DEFAULT_CAPACITY); + } + + /* + * ����һ��ָ����ʼ������ArrayList + * + * @param initialCapacity ��ʼ���� + * + * @throws IllegalArgumentException ָ����ʼ����Ϊ����ʱ�׳��Ƿ������쳣 + */ + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = EMPTY_ELEMENT_DATA; + } else { + throw new IllegalArgumentException("Illegal Capacity " + initialCapacity); + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + /* + * ����elementData�������� + * + * @param capacity �µ��������� + */ + private void grow(int capacity) { + if (capacity < elementData.length) { + return; + } + elementData = Arrays.copyOf(elementData, capacity); + } + + @Override + public boolean add(E e) { + if (size == elementData.length) { + grow(size * 2); + } + elementData[size++] = e; + return true; + } + + @Override + public E remove(int index) { + rangeCheck(index); + + // move elements after index forward by 1 + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + --size; + + // TODO: JDK��ʵ��: 1.Ч�� 2.GC���� + // System.arraycopy(elementData, index + 1, elementData, index, size - + // index - 1); + // elementData[--size] = null; + + return null; + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + + if (index < 0) { + return false; + } + + remove(index); + return true; + } + + @SuppressWarnings({ "unchecked" }) + private E elementData(int index) { + return (E) elementData[index]; + } + + private void rangeCheck(int index) { + // TODO: JDKԴ����δ��index<0�������������Ϊ�Σ� + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public E get(int index) { + rangeCheck(index); + + return elementData(index); + } + + @Override + public E set(int index, E element) { + rangeCheck(index); + + E oldElement = elementData(index); + elementData[index] = element; + return oldElement; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) { + if (elementData[i] == null) { + return i; + } + } + } else { + for (int i = 0; i < size; i++) { + if (o.equals(elementData[i])) { + return i; + } + } + } + return -1; + } + +} diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java new file mode 100644 index 0000000000..e1b3f0a686 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java @@ -0,0 +1,169 @@ +package com.qsq.study; + +public class LinkedList implements List{ + + private Node first; + private Node last; + private int size; + + private static class Node { + T item; + Node prev; + Node next; + + public Node(Node prev, T item, Node next) { + this.prev = prev; + this.item = item; + this.next = next; + } + } + + /* + * �޲������캯�� + */ + public LinkedList() { + first = null; + last = first; + size = 0; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + private void rangeCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public boolean add(E e) { + if (first == null) { + // ����Ϊ�� + first = new Node(null, e, null); + } else if (last == null) { + // β���Ϊ�� + last = new Node(first, e, null); + first.next = last; + } else { + Node node = new Node(last, e, null); + last.next = node; + last = node; + } + ++size; + return true; + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + if (index < 0 || index > size) { + return false; + } + + remove(index); + return true; + } + + @Override + public E remove(int index) { + rangeCheck(index); + + Node current = first; + while (index > 0) { + --index; + if (current == null) { + return null; + } + current = current.next; + } + + E oldItem = current.item; + + if (current.prev == null) { + // ɾ������ͷ��� + first = current.next; + if (current.next != null) { + current.next.prev = null; + } + } else { + current.prev.next = current.next; + if (current.next != null) { + current.next.prev = current.prev; + } + } + --size; + + return oldItem; + } + + @Override + public E get(int index) { + rangeCheck(index); + + int i = 0; + Node current = first; + while (i < index) { + if (current == null) { + return null; + } + ++i; + current = current.next; + } + return current.item; + } + + @Override + public E set(int index, E element) { + rangeCheck(index); + + Node current = first; + while (index > 0) { + --index; + current = current.next; + } + E oldElement = current.item; + current.item = element; + + return oldElement; + } + + @Override + public int indexOf(Object o) { + if (first == null) { + return -1; + } + + Node current = first; + int index = 0; + if (o == null) { + while (current != null) { + if (current.item == null) { + return index; + } else { + current = current.next; + ++index; + } + } + } else { + while (current != null) { + if (o.equals(current.item)) { + return index; + } else { + current = current.next; + ++index; + } + } + } + + return -1; + } + +} diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java new file mode 100644 index 0000000000..7b509a361e --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java @@ -0,0 +1,12 @@ +package com.qsq.study; + +public interface List { + int size(); + boolean isEmpty(); + boolean add(E e); + boolean remove(Object o); + E remove(int index); + E get(int index); + E set(int index, E element); + int indexOf(Object o); +} diff --git a/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java new file mode 100644 index 0000000000..d1c6353f79 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java @@ -0,0 +1,70 @@ +package com.qsq.study; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testIsEmpty() { + ArrayList list = new ArrayList<>(); + assertEquals(true, list.isEmpty()); + list.add(1); + assertEquals(false, list.isEmpty()); + } + + @Test + public void testSize() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.add(1); + assertEquals(1, list.size()); + list.add(2); + assertEquals(2, list.size()); + for (int i=3; i<=20; i++) { + list.add(i); + } + assertEquals(20, list.size()); + + } + @Test + public void testAdd() { + ArrayList list = new ArrayList<>(); + list.add(1); + assertEquals(1, list.size()); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3, list.size()); + list.remove(1); + assertEquals(2, list.size()); + list.remove(1); + assertEquals(1, list.size()); + list.remove(0); + assertEquals(0, list.size()); + } + + @Test + public void testGet() { + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(2); + assertEquals(1, (int)list.get(0)); + assertEquals(2, (int)list.get(1)); + } + + @Test + public void TestSet() { + ArrayList list = new ArrayList<>(); + list.add(1); + assertEquals(1, (int)list.get(0)); + list.set(0, 2); + assertEquals(2, (int)list.get(0)); + } +} diff --git a/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java new file mode 100644 index 0000000000..7b0bd3807a --- /dev/null +++ b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java @@ -0,0 +1,70 @@ +package com.qsq.study; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void testIsEmpty() { + LinkedList list = new LinkedList<>(); + assertEquals(true, list.isEmpty()); + list.add(1); + assertEquals(false, list.isEmpty()); + } + + @Test + public void testSize() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + list.add(1); + assertEquals(1, list.size()); + list.add(2); + assertEquals(2, list.size()); + for (int i=3; i<=20; i++) { + list.add(i); + } + assertEquals(20, list.size()); + + } + @Test + public void testAdd() { + LinkedList list = new LinkedList<>(); + list.add(1); + assertEquals(1, list.size()); + } + + @Test + public void testRemove() { + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3, list.size()); + list.remove(1); + assertEquals(2, list.size()); + list.remove(1); + assertEquals(1, list.size()); + list.remove(0); + assertEquals(0, list.size()); + } + + @Test + public void testGet() { + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + assertEquals(1, (int)list.get(0)); + assertEquals(2, (int)list.get(1)); + } + + @Test + public void TestSet() { + LinkedList list = new LinkedList<>(); + list.add(1); + assertEquals(1, (int)list.get(0)); + list.set(0, 2); + assertEquals(2, (int)list.get(0)); + } +} From 1982414590d832fa36f8964f7c3afb22f6150420 Mon Sep 17 00:00:00 2001 From: eulerlcs Date: Sun, 26 Feb 2017 22:01:27 +0900 Subject: [PATCH 07/11] 20170226.homework from 41689722.eulerlcs --- .../20170226-eulerlcs-collection/.gitignore | 1 + .../collection-aggregator/.project | 17 + .../.settings/org.eclipse.m2e.core.prefs | 4 + .../collection-aggregator/pom.xml | 13 + .../collection-aggregator/src/site/.gitkeep | 0 .../collection-lib/.classpath | 37 ++ .../collection-lib/.gitignore | 1 + .../collection-lib/.project | 23 + .../.settings/org.eclipse.jdt.core.prefs | 5 + .../.settings/org.eclipse.m2e.core.prefs | 4 + .../collection-lib/pom.xml | 27 ++ .../com/eulerlcs/collection/ArrayList.java | 438 ++++++++++++++++++ .../src/main/resources/.gitkeep | 0 .../eulerlcs/collection/TestArrayList.java | 44 ++ .../src/test/resources/.gitkeep | 0 .../collection-parent/.project | 17 + .../.settings/org.eclipse.m2e.core.prefs | 4 + .../collection-parent/pom.xml | 112 +++++ .../collection-parent/src/site/.gitkeep | 0 19 files changed, 747 insertions(+) create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml create mode 100644 group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore new file mode 100644 index 0000000000..e10e727be5 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project new file mode 100644 index 0000000000..47cffea382 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project @@ -0,0 +1,17 @@ + + + collection-aggregator + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml new file mode 100644 index 0000000000..a09edb357b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + com.eulerlcs.collection + collection-aggregator + 0.0.1-SNAPSHOT + pom + + + ../collection-parent + ../collection-lib + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath new file mode 100644 index 0000000000..5131f04311 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project new file mode 100644 index 0000000000..69766f62bc --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project @@ -0,0 +1,23 @@ + + + collection-lib + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..714351aec1 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml new file mode 100644 index 0000000000..6d1654e23a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + ../collection-parent/pom.xml + + collection-lib + + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + junit + junit + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java new file mode 100644 index 0000000000..219919719b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java @@ -0,0 +1,438 @@ +/** + * 90% or more copy from jdk + */ +package com.eulerlcs.collection; + +import java.util.Arrays; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.RandomAccess; +import java.util.function.Consumer; + +public class ArrayList implements List, RandomAccess { + private static final int MAX�QARRAY�QSIZE = 1 << 10; + private transient Object[] elementData = new Object[0]; + private int size; + private transient int modCount = 0; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + if (o == null) { + for (Object obi : elementData) { + if (obi == null) { + return true; + } + } + } else { + for (Object obj : elementData) { + if (o.equals(obj)) { + return true; + } + } + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + for (Object e : c) + if (!contains(e)) + return false; + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size, elementData.getClass()); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] a) { + if (a.length < size) { + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + } else { + System.arraycopy(elementData, 0, a, 0, size); + if (a.length > size) + a[size] = null; + return a; + } + } + + @Override + public boolean add(E e) { + ensureExplicitCapacity(size + 1); // Increments modCount!! + elementData[size] = e; + size++; + return true; + } + + @Override + public void add(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + ensureExplicitCapacity(size + 1); // Increments modCount!! + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + @Override + public E remove(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + modCount++; + @SuppressWarnings("unchecked") + E oldValue = (E) 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 = -1; + + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) { + index = i; + break; + } + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) { + index = i; + break; + } + } + + if (index > 0) { + modCount++; + 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 true; + } + + return false; + } + + @Override + public boolean removeAll(Collection c) { + boolean modified = false; + for (Object obj : c) { + modified |= remove(obj); + } + + return modified; + } + + @Override + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean addAll(int index, Collection c) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + + int numMoved = size - index; + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, index + numNew, numMoved); + + System.arraycopy(a, 0, elementData, index, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean retainAll(Collection c) { + final Object[] elementData = this.elementData; + int r = 0, w = 0; + boolean modified = false; + for (; r < size; r++) + if (c.contains(elementData[r])) + elementData[w++] = elementData[r]; + + if (w != size) { + // clear to let GC do its work + for (int i = w; i < size; i++) + elementData[i] = null; + modCount += size - w; + size = w; + modified = true; + } + + return modified; + } + + @Override + public void clear() { + modCount++; + for (int i = 0; i < size; i++) + elementData[i] = null; + + size = 0; + } + + @Override + public List subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + return (E) elementData[index]; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + E oldValue = (E) elementData[index]; + elementData[index] = element; + return oldValue; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + @Override + public int lastIndexOf(Object o) { + if (o == null) { + for (int i = size - 1; i >= 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; + } + + private void ensureExplicitCapacity(int minCapacity) { + modCount++; + + if (elementData.length > minCapacity) { + return; + } else if (minCapacity > MAX�QARRAY�QSIZE) { + throw new OutOfMemoryError(); + } + + int oldCapacity = elementData.length; + + int newCapacity = oldCapacity == 0 ? 10 : (oldCapacity + (oldCapacity >> 1)); + if (newCapacity > MAX�QARRAY�QSIZE) { + newCapacity = MAX�QARRAY�QSIZE; + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public ListIterator listIterator() { + return new ListItr(0); + } + + @Override + public ListIterator listIterator(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index); + return new ListItr(index); + } + + /** + * fully copy from jdk ArrayList.Itr + */ + private class Itr implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + int expectedModCount = modCount; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + checkForComodification(); + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public void forEachRemaining(Consumer consumer) { + Objects.requireNonNull(consumer); + final int size = ArrayList.this.size; + int i = cursor; + if (i >= size) { + return; + } + final Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) { + throw new ConcurrentModificationException(); + } + while (i != size && modCount == expectedModCount) { + consumer.accept((E) elementData[i++]); + } + // update once at end of iteration to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + /** + * fully copy from jdk ArrayList.ListItr + */ + private class ListItr extends Itr implements ListIterator { + ListItr(int index) { + super(); + cursor = index; + } + + @Override + public boolean hasPrevious() { + return cursor != 0; + } + + @Override + public int nextIndex() { + return cursor; + } + + @Override + public int previousIndex() { + return cursor - 1; + } + + @Override + @SuppressWarnings("unchecked") + public E previous() { + checkForComodification(); + int i = cursor - 1; + if (i < 0) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i; + return (E) elementData[lastRet = i]; + } + + @Override + public void set(E e) { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.set(lastRet, e); + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + public void add(E e) { + checkForComodification(); + + try { + int i = cursor; + ArrayList.this.add(i, e); + cursor = i + 1; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java new file mode 100644 index 0000000000..b66dd278e3 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java @@ -0,0 +1,44 @@ +package com.eulerlcs.collection; + +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestArrayList { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test_foreach() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + int sum = 0; + for (Integer item : list) { + sum += item; + } + + Assert.assertEquals(sum, 6); + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project new file mode 100644 index 0000000000..3467a254de --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project @@ -0,0 +1,17 @@ + + + collection-parent + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml new file mode 100644 index 0000000000..6f05e517e2 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml @@ -0,0 +1,112 @@ + + 4.0.0 + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + pom + + + + 1.7.23 + 4.12 + 2.17 + 1.8 + 1.8 + + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-source + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + true + 1.8 + protected + UTF-8 + UTF-8 + UTF-8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.19.1 + + true + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 From 7f7243d0c7a81392f3b24fcbb35446e9f75ea6bf Mon Sep 17 00:00:00 2001 From: XiTang Date: Sun, 26 Feb 2017 21:07:56 +0800 Subject: [PATCH 08/11] week01 --- group09/277123057/Week01/ArrayList.java | 55 ++++++ group09/277123057/Week01/BinaryTreeNode.java | 26 +++ group09/277123057/Week01/Iterator.java | 6 + group09/277123057/Week01/LinkedList.java | 173 +++++++++++++++++++ group09/277123057/Week01/List.java | 9 + group09/277123057/Week01/Queue.java | 29 ++++ group09/277123057/Week01/Stack.java | 30 ++++ group09/277123057/Week01/Test.java | 9 + 8 files changed, 337 insertions(+) create mode 100644 group09/277123057/Week01/ArrayList.java create mode 100644 group09/277123057/Week01/BinaryTreeNode.java create mode 100644 group09/277123057/Week01/Iterator.java create mode 100644 group09/277123057/Week01/LinkedList.java create mode 100644 group09/277123057/Week01/List.java create mode 100644 group09/277123057/Week01/Queue.java create mode 100644 group09/277123057/Week01/Stack.java create mode 100644 group09/277123057/Week01/Test.java diff --git a/group09/277123057/Week01/ArrayList.java b/group09/277123057/Week01/ArrayList.java new file mode 100644 index 0000000000..5af3f5e6b0 --- /dev/null +++ b/group09/277123057/Week01/ArrayList.java @@ -0,0 +1,55 @@ +package Week01; +/* + * time:2017-2-20 21:51 created + * + */ +public class ArrayList implements List{ + + private int size = 0; + //���ٵĿռ�ֻ��100�� + private Object[] elementData = new Object[100]; + + //��ĩλ���� + public void add(Object o){ + elementData[size++] = o; + } + + //��ǰλ����Ԫ�أ��������ƶ���ǰλ�ڸ�λ�õ�Ԫ�ؼ����к���Ԫ�� + public void add(int index, Object o){ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + //�Ƴ���������ָ����Ԫ��,�ұ�Ԫ������ + public Object remove(int index){ + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, numMoved); + elementData[--size] = null; + return elementData[index]; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int pos = 0; + + public boolean hashNext() { + return pos < size(); + } + + public Object next() { + return elementData[pos++]; + } + } +} diff --git a/group09/277123057/Week01/BinaryTreeNode.java b/group09/277123057/Week01/BinaryTreeNode.java new file mode 100644 index 0000000000..787b5a1d76 --- /dev/null +++ b/group09/277123057/Week01/BinaryTreeNode.java @@ -0,0 +1,26 @@ +package Week01; +// +/* + *��û���� + * */ +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; + } +} diff --git a/group09/277123057/Week01/Iterator.java b/group09/277123057/Week01/Iterator.java new file mode 100644 index 0000000000..2be5cbc254 --- /dev/null +++ b/group09/277123057/Week01/Iterator.java @@ -0,0 +1,6 @@ +package Week01; +//time +public interface Iterator { + public boolean hashNext(); + public Object next(); +} diff --git a/group09/277123057/Week01/LinkedList.java b/group09/277123057/Week01/LinkedList.java new file mode 100644 index 0000000000..014fe8f149 --- /dev/null +++ b/group09/277123057/Week01/LinkedList.java @@ -0,0 +1,173 @@ +package Week01; + +import java.util.NoSuchElementException; + +/* + * time:2017-2-22 13:00 + * �ο���http://blog.csdn.net/jianyuerensheng/article/details/51204598 + * http://www.jianshu.com/p/681802a00cdf + * jdk1.8Դ�� + * */ + +//������õ���˫��������jdk1.6��linkedList����˫��ѭ������ʵ�� +public class LinkedList implements List { + + private int size = 0; + private Node first; //ָ��ͷ��� + private Node last; //ָ��β�ڵ� + + //��������end����Ԫ�أ������Լ���addLast()���� + public void add(Object o){ + addLast(o); + } + + //����index����,��δ���ο�ͬ��ͬѧ spike + public void add(int index, Object o){ + if (index < 0 || index > size) + throw new IllegalArgumentException(); + size++; + if (index == size){ + addLast(o); + }else{ + Node target = findIndex(index); + Node newNode = new Node(o, target,target.next); + if (last == target){ + last = newNode; + }else{ + //target.next = newNode;����Ҫ��Ҫ�� + target.next.prev = newNode;//�е����� + } + } + size++; + } + + public Object get(int index){ + if ( index < 0 || index > size){ + throw new IllegalArgumentException(); + } + return findIndex(index).data; + } + //ɾ��indexָ����Ԫ�� + public Object remove(int index){ + if (index < 0 || index > size){ + throw new IllegalArgumentException(); + } + + Node target = findIndex(index); + if (target == first){ + first = first.next; + first.prev = null; + }else if(target == last){ + last = last.prev; + last.next = null; + }else{ + target.prev.next = target.next; + target.next.prev = target.prev; + } + return target.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + Node f = first; + Node newNode = new Node(o,null,f); + first = newNode; + if (f == null) + last = newNode; //���fΪnull��˵��ֻ��last����ָ�� + else + f.prev = newNode; + size++; + } + + public void addLast(Object o){ + Node l = last; + Node newNode = new Node(o, l, null); + last = newNode; + if (l == null) + first = newNode; + else + l.next = newNode; + size++; + } + + + public Object removeFirst() { + if ( first == null) + throw new NoSuchElementException(); + Node f = first; + Object data = f.data; + Node next = f.next; + //ȥ����Ԫ��ָΪnull + f.data = null; + f.next = null; + first = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return data; + } + + public Object removeLast(){ + if (last == null) + throw new NoSuchElementException(); + Node l = last; + Object data = l.data; + Node previous = l.prev; + l.data = null; + l.prev = null; + last = previous; + if (previous == null) + first = null; + else + previous.next = null; + size--; + return data; + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + Node curNode = first; + public boolean hashNext() { + return curNode != null; + } + public Object next() { + if (!hashNext()) + throw new NoSuchElementException(); + Object data = curNode.data; + curNode = curNode.next; + return data; + } + } + private Node findIndex(int index) { + Node target = first; + int i = 0; + while(i < index){ + target = target.next; + i++; + } + return target; + } + + //������ + private static class Node{ + private Object data; + //Ĭ��Ҳ��null + private Node prev = null; //��һ��Ԫ�ؽڵ� + private Node next = null;//��һ��Ԫ�ؽڵ� + + public Node(Object data, Node pre, Node next){ + this.data = data; + this.prev = pre; + this.next = next; + } + } +} diff --git a/group09/277123057/Week01/List.java b/group09/277123057/Week01/List.java new file mode 100644 index 0000000000..733a02a0d6 --- /dev/null +++ b/group09/277123057/Week01/List.java @@ -0,0 +1,9 @@ +package Week01; +//time: +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/group09/277123057/Week01/Queue.java b/group09/277123057/Week01/Queue.java new file mode 100644 index 0000000000..dd786574b8 --- /dev/null +++ b/group09/277123057/Week01/Queue.java @@ -0,0 +1,29 @@ +package Week01; +/* + * time:2017-2-25 13:46 created by Doen + * + * */ +public class Queue { + private LinkedList elementData = new LinkedList(); + //������ + + public void enQueue(Object o){ + elementData.add(o); + } + + //������ + public Object deQueue(){ + if (isEmpty()) + throw new UnsupportedOperationException(); + return elementData.remove(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group09/277123057/Week01/Stack.java b/group09/277123057/Week01/Stack.java new file mode 100644 index 0000000000..9a10468385 --- /dev/null +++ b/group09/277123057/Week01/Stack.java @@ -0,0 +1,30 @@ +package Week01; + +import java.util.NoSuchElementException; + +/* + * time:2017-2-25 13:19 created by Doen + * change + * */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) + throw new NoSuchElementException(); + return elementData.remove(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group09/277123057/Week01/Test.java b/group09/277123057/Week01/Test.java new file mode 100644 index 0000000000..5d4517d9af --- /dev/null +++ b/group09/277123057/Week01/Test.java @@ -0,0 +1,9 @@ +package Week01; +//time +public class Test { + public static void main(String[] args){ + ArrayList arraylist = new ArrayList(); + arraylist.add(1); + arraylist.add("A"); + } +} From 9a08449ace578325741e863470ca882c619bda85 Mon Sep 17 00:00:00 2001 From: glorychou Date: Sun, 26 Feb 2017 22:03:24 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=96=87=E7=AB=A0&?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=B8=80=E4=BA=9B=E5=B0=8F=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\347\241\200\347\237\245\350\257\206.txt" | 9 ++++++++ .../20170226/src/per/zyf/bds/ArrayList.java | 12 +++++------ .../20170226/src/per/zyf/bds/BinaryTree.java | 21 ++++++++++++++++--- .../20170226/src/per/zyf/bds/LinkedList.java | 10 +++++++-- .../20170226/src/per/zyf/bds/List.java | 2 +- .../20170226/src/per/zyf/bds/Queue.java | 2 +- .../20170226/src/per/zyf/bds/Stack.java | 2 +- 7 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 "group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" diff --git "a/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" "b/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" new file mode 100644 index 0000000000..70bbd879af --- /dev/null +++ "b/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" @@ -0,0 +1,9 @@ +现代计算机基本都是冯·诺依曼结构,而冯·诺依曼结构中,最核心的思想便是“存储程序思想”,即:把运算程序存在机器的存储器中。 + +在这种结构下,计算机被分成了五个部分:运算器、控制器、存储器、输入设备和输出设备。其中运算器和控制器构成了CPU的最重要部分。早期的冯·诺依曼结构型计算机是以运算器为核心的,譬如穿孔纸带机,运算核心通过从穿孔纸带中获取控制信息与运算信息完成指定的任务。在这种结构下,任何操作都要先与运算核心打交道。直到后来发明了内存,将大量的控制信息与运算信息存储到集成电路上,让计算机有了飞跃式的发展。内存拥有着大大超过于穿孔纸带的存储效率,于是,计算机的中心慢慢地从运算器转移到了存储器。在这种模式下,运算器、控制器、以至于输入输出设备,都是通过与内存交换信息得到很好的协作。 + +存储器的读写效率确实高,但是它也有着单位造价高和相对容量较小的缺点,虽然现在SSD已经也很便宜了,但是对于以前的工程师来说,这都是不可想象的。于是乎,经过伟大的科学家们与工程师们的探索,他们发现了计算机程序运行过程中的“局部性原理”。“局部性原理”包括时间局部性和空间局部性,时间局部性是指:一段被访问过的程序在不久的时间内很有可能会被再次访问,空间局部性是指:一段被访问过的程序的临近的程序很可能马上被访问到。在有的资料中还提到了顺序局部性,它的意思是指:大部分程序是顺序执行的。在“局部性原理”的指导下,存储分级结构便出现了。存储器被分为若干个级别,越靠近CPU计算速度越快但存储容量小、造价高,如寄存器、Cache,越远离CPU,从内存到硬盘,计算速度越慢,但容量大,造价也越低。采用这种结构,最终使工程师们在性能与造价上得到了很好的平衡。 + +再回到冯·诺依曼结构来,它的“把程序存储起来”到底是什么意思呢?好比我们照着菜谱学做菜,菜谱就是存储好的程序。我们要按照菜谱上的步骤,从选材、买菜、炒菜、出锅、上桌等一系列步骤来达到最终的目标。“存储程序”的思想就是要求把这些基本的步骤存到存储器中,然后当我们要实现某一运算的时候,就将实现这一运算所需要的操作调取出来。菜谱上的一条条步骤,就相当于计算机中的指令,程序指令指挥着计算机各个部件的运行。 + +一个计算机指令包含操作码和操作数两个部分,顾名思义,操作码指示的是计算机需要做的操作,操作数则标识了某一次操作需要用到的数据。由于指令长度有限,操作数的最大寻址范围又远小于内存的寻址范围,于是操作数的获取方式又分为很多种,包括立即数寻址、直接寻址、间接寻址、相对寻址、基址寻址、变址寻址等。一条指令的执行主要包括如下几个步骤:取指令、分析指令、执行指令,有的指令还包括内存写回。 diff --git a/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java index a64cb7cd19..c256ab61aa 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java +++ b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java @@ -1,6 +1,6 @@ /** * @Title: ArrayList.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: ArrayList的实现 * @author glorychou * @date 2017年2月22日 下午10:41:58 */ @@ -33,8 +33,8 @@ public class ArrayList implements List { * 构造初始元素数组 */ public ArrayList() { - this.elementData = CAPACITY_EMPTY_ELEMENTDATA; - } + this.elementData = CAPACITY_EMPTY_ELEMENTDATA; + } /*** * @@ -158,7 +158,7 @@ private void grow(int minCapacity) { int oldCapacity = elementData.length; // 容量增大一半 int newCapacity = oldCapacity + (oldCapacity >> 1); - elementData = Arrays.copyOf(elementData, newCapacity); + elementData = Arrays.copyOf(elementData, newCapacity); } /*** @@ -169,6 +169,6 @@ private void grow(int minCapacity) { */ private void rangeCheck(int index) { if (index < 0 || index > size) - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } } \ No newline at end of file diff --git a/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java index 87d7010456..062894ec95 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java +++ b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java @@ -1,13 +1,11 @@ /** * @Title: BinaryTree.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: 二叉排序树的实现 * @author glorychou * @date 2017年2月25日 下午10:22:03 */ package per.zyf.bds; -import java.util.Comparator; - /** * @author glorychou * @@ -52,6 +50,7 @@ public boolean add(E e) { } else root = newNode; + size++; return true; } @@ -66,6 +65,22 @@ public void inorderPrint(Node e) { inorderPrint(e.rightChild); } + /** + * @Description: 判断树是否为空 + * @return boolean 是否为空 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取树的节点数 + * @return int 树节点数 + */ + public int size() { + return size; + } + // 树节点 private static class Node { E item; diff --git a/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java index 3955ef22ab..07eb7d7d46 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java +++ b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java @@ -1,6 +1,6 @@ /** * @Title: LinkedList.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: 双向链表的实现 * @author glorychou * @date 2017年2月24日 上午12:23:00 */ @@ -81,6 +81,7 @@ public E remove(int index) { rangeCheck(index); Node p = node(index); + E e = p.item; // 所需删除节点的前面有节点,则改变前一节点的下一跳 if(p.prev != null) @@ -88,10 +89,15 @@ public E remove(int index) { // 所需删除节点的后面有节点,则改变后一节点的上一跳 if(p.next != null) p.next.prev = p.prev; + + // 清空数据 + p.prev = null; + p.item = null; + p.next = null; size--; - return p.item; + return e; } /** diff --git a/group09/396077060/20170226/src/per/zyf/bds/List.java b/group09/396077060/20170226/src/per/zyf/bds/List.java index 847edd1013..d8edbaabce 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/List.java +++ b/group09/396077060/20170226/src/per/zyf/bds/List.java @@ -1,6 +1,6 @@ /** * @Title: List.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: List接口的实现 * @author glorychou * @date 2017年2月24日 下午3:02:34 */ diff --git a/group09/396077060/20170226/src/per/zyf/bds/Queue.java b/group09/396077060/20170226/src/per/zyf/bds/Queue.java index 70b72c9b49..39c66972a0 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/Queue.java +++ b/group09/396077060/20170226/src/per/zyf/bds/Queue.java @@ -1,6 +1,6 @@ /** * @Title: Queue.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: 队列的实现 * @author glorychou * @date 2017年2月24日 下午3:10:08 */ diff --git a/group09/396077060/20170226/src/per/zyf/bds/Stack.java b/group09/396077060/20170226/src/per/zyf/bds/Stack.java index bf8d4f5611..8e3f70eb51 100644 --- a/group09/396077060/20170226/src/per/zyf/bds/Stack.java +++ b/group09/396077060/20170226/src/per/zyf/bds/Stack.java @@ -1,6 +1,6 @@ /** * @Title: Stack.java -* @Description: TODO(用一句话描述该文件做什么) +* @Description: 栈的实现 * @author glorychou * @date 2017年2月24日 下午3:05:29 */ From 011550bae6acba09d226139f896b659c87554397 Mon Sep 17 00:00:00 2001 From: jacky <1271620150@qq.com> Date: Sun, 26 Feb 2017 22:23:36 +0800 Subject: [PATCH 10/11] Work01 commit --- group09/1271620150/Work01/.gitignore | 19 ++ .../src/com/coding/basic/ArrayList.java | 100 +++++++ .../Work01/src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 277 ++++++++++++++++++ .../Work01/src/com/coding/basic/List.java | 9 + .../Work01/src/com/coding/basic/Queue.java | 44 +++ .../Work01/src/com/coding/basic/Stack.java | 45 +++ 7 files changed, 501 insertions(+) create mode 100644 group09/1271620150/Work01/.gitignore create mode 100644 group09/1271620150/Work01/src/com/coding/basic/ArrayList.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Iterator.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/LinkedList.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/List.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Queue.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Stack.java diff --git a/group09/1271620150/Work01/.gitignore b/group09/1271620150/Work01/.gitignore new file mode 100644 index 0000000000..1af1a6638d --- /dev/null +++ b/group09/1271620150/Work01/.gitignore @@ -0,0 +1,19 @@ +*.class +*.classpath +*.project + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ diff --git a/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4e6dc8c929 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private Object[] elements; + + private int size; + + public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + this.elements = new Object[initialCapacity]; + } + + public ArrayList() { + this(10); + } + + public void add(Object obj) { + ensureCapacity(size + 1); + elements[size++] = obj; + + } + + public void add(int index, Object obj) { + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = obj; + size++; + } + + public Object get(int index) { + rangeCheck(index); + return elements[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object toRemove = elements[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elements, index + 1, elements, index, numMoved); + elements[--size] = null; + return toRemove; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elements.length; + if (minCapacity > oldCapacity) { + int newCapacity = oldCapacity * 2; + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void rangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + this.size; + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos != size; + } + + public Object next() { + int i = pos; + if (i >= size) + throw new NoSuchElementException(); + Object[] elements = ArrayList.this.elements; + if (i >= elements.length) + throw new ConcurrentModificationException(); + pos = i + 1; + return (Object) elements[i]; + } + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/Iterator.java b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e60b443310 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..302d048d74 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,277 @@ +package com.coding.basic; + +import java.util.Collection; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node first; + private Node last; + private int size; + + public LinkedList() { + } + + public LinkedList(Collection c) { + this(); + addAll(size, c); + } + + private void addAll(int index, Collection c) { + checkPositionIndex(size); + + Object[] a = c.toArray(); + int numNew = a.length; + if (numNew == 0) + return; + + Node pred, succ; + if (index == size) { + succ = null; + pred = last; + } else { + succ = node(index); + pred = succ.prev; + } + + for (Object o : a) { + Node newNode = new Node(pred, o, null); + if (pred == null) + first = newNode; + else + pred.next = newNode; + pred = newNode; + } + + if (succ == null) { + last = pred; + } else { + pred.next = succ; + succ.prev = pred; + } + + size += numNew; + } + + public void add(Object o) { + linkLast(o); + } + + private void linkLast(Object o) { + Node l = last; + Node newNode = new Node(l, o, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linkLast(o); + } else { + Node l = node(index); + linkBefore(o, l); + } + + } + + public void linkBefore(Object o, Node succ) { + final Node pred = succ.prev; + final Node newNode = new Node(pred, o, succ); + succ.prev = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + + public Object get(int index) { + checkElementIndex(index); + return node(index).data; + } + + public Object remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + private Object unlink(Node node) { + final Object element = node.data; + final Node next = node.next; + final Node prev = node.prev; + + if (prev == null) { + first = next; + } else { + prev.next = next; + node.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + node.next = null; + } + + node.data = null; + size--; + return element; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + linkFirst(o); + } + + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(null, o, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object o) { + linkLast(o); + } + + public Object removeFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + + private Object unlinkFirst(Node f) { + final Object element = f.data; + final Node next = f.next; + f.data = null; + f.next = null; + first = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return element; + } + + public Object removeLast() { + final Node l = last; + if (l == null) + throw new NoSuchElementException(); + return unlinkLast(l); + } + + private Object unlinkLast(Node l) { + final Object element = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if (prev == null) + first = null; + else + prev.next = null; + size--; + return element; + } + + public Iterator iterator(int index) { + return new LinkListIterator(index); + } + + private static class Node { + Object data; + Node next; + Node prev; + + Node(Node prev, Object obj, Node next) { + this.data = obj; + this.next = next; + this.prev = prev; + } + + } + + Node node(int index) { + // assert isElementIndex(index); + + if (index < (size >> 1)) { + Node x = first; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private class LinkListIterator implements Iterator { + private Node lastReturned = null; + private Node next; + private int nextIndex; + + LinkListIterator(int index) { + next = (index == size) ? null : node(index); + nextIndex = index; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/List.java b/group09/1271620150/Work01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..216d97e9ad --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.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(); +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/Queue.java b/group09/1271620150/Work01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4484081ac6 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Queue.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +public class Queue { + private static final int CAPACITY = 10; + private int size; + private int front; + private int tail; + private Object[] array; + + public Queue(){ + this.size = CAPACITY; + array = new Object[size]; + front = tail = 0; + } + + + public void enQueue(Object o) throws Exception{ + if (size() == size -1) + throw new Exception("Queue is full"); + array[tail] = o; + tail = (tail +1) % size; + } + + public Object deQueue() throws Exception{ + Object o; + if (isEmpty()) + throw new Exception("Queue is empty"); + o = array[front]; + front = (front + 1) % size; + return o; + } + + public boolean isEmpty(){ + return (front==tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (size + tail - front) % size; + } + +} diff --git a/group09/1271620150/Work01/src/com/coding/basic/Stack.java b/group09/1271620150/Work01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..58322c0130 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Stack.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Stack { + private static final int CAPACITY = 10; + private int capacity; + private int top = -1; + Object[] array; + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws Exception{ + if(size()== CAPACITY){ + throw new Exception("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + +} \ No newline at end of file From ac3d7feb558d740953e68ccb6eea7bf3ae5cede1 Mon Sep 17 00:00:00 2001 From: Sid Date: Mon, 27 Feb 2017 20:48:25 +0800 Subject: [PATCH 11/11] Add week 01 homework --- group09/601862675/Week01/pom.xml | 29 ++++ .../Week01/src/main/java/ArrayList.java | 102 ++++++++++++++ .../Week01/src/main/java/BinaryTree.java | 29 ++++ .../Week01/src/main/java/Iterator.java | 7 + .../Week01/src/main/java/LinkedList.java | 128 ++++++++++++++++++ .../601862675/Week01/src/main/java/List.java | 7 + .../601862675/Week01/src/main/java/Queue.java | 35 +++++ .../601862675/Week01/src/main/java/Stack.java | 38 ++++++ .../Week01/src/main/java/Watcher.java | 26 ++++ .../Week01/src/test/java/ArrayListTest.java | 104 ++++++++++++++ .../Week01/src/test/java/LinkedListTest.java | 106 +++++++++++++++ .../Week01/src/test/java/QueueTest.java | 44 ++++++ .../Week01/src/test/java/StackTest.java | 61 +++++++++ group09/601862675/pom.xml | 16 +++ 14 files changed, 732 insertions(+) create mode 100644 group09/601862675/Week01/pom.xml create mode 100644 group09/601862675/Week01/src/main/java/ArrayList.java create mode 100644 group09/601862675/Week01/src/main/java/BinaryTree.java create mode 100644 group09/601862675/Week01/src/main/java/Iterator.java create mode 100644 group09/601862675/Week01/src/main/java/LinkedList.java create mode 100644 group09/601862675/Week01/src/main/java/List.java create mode 100644 group09/601862675/Week01/src/main/java/Queue.java create mode 100644 group09/601862675/Week01/src/main/java/Stack.java create mode 100644 group09/601862675/Week01/src/main/java/Watcher.java create mode 100644 group09/601862675/Week01/src/test/java/ArrayListTest.java create mode 100644 group09/601862675/Week01/src/test/java/LinkedListTest.java create mode 100644 group09/601862675/Week01/src/test/java/QueueTest.java create mode 100644 group09/601862675/Week01/src/test/java/StackTest.java create mode 100644 group09/601862675/pom.xml diff --git a/group09/601862675/Week01/pom.xml b/group09/601862675/Week01/pom.xml new file mode 100644 index 0000000000..d457efbf95 --- /dev/null +++ b/group09/601862675/Week01/pom.xml @@ -0,0 +1,29 @@ + + + + coding2017 + me.sidzh + 1.0-SNAPSHOT + + 4.0.0 + + Week01 + + + junit + junit + 4.12 + test + + + com.github.stefanbirkner + system-rules + 1.16.0 + test + + + + + \ No newline at end of file diff --git a/group09/601862675/Week01/src/main/java/ArrayList.java b/group09/601862675/Week01/src/main/java/ArrayList.java new file mode 100644 index 0000000000..1923ba3592 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/ArrayList.java @@ -0,0 +1,102 @@ +public class ArrayList implements List{ + + private Object[] elements; + + private static final int INITIAL_SIZE = 16; + + public static final int MAX_LIST_SIZE = 48; + + private int size = 0; + + private int capacity = 0; + + public ArrayList() { + elements = new Object[INITIAL_SIZE]; + capacity = INITIAL_SIZE; + } + + public void add(int index, Object obj) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + ensureSpace(); + if (index == size) { + add(obj); + } else { + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = obj; + size++; + } + } + + public void add(Object obj) { + ensureSpace(); + elements[size++] = obj; + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("List: [ "); + for (int i = 0; i < size; ++ i) { + builder.append(elements[i]).append(" "); + } + builder.append("]"); + return builder.toString(); + } + + private void ensureSpace() { + if (size == capacity) { + if (size == MAX_LIST_SIZE) { + throw new IndexOutOfBoundsException(); + } + int newCapacity = capacity*2 > MAX_LIST_SIZE ? MAX_LIST_SIZE : capacity*2; + grow(newCapacity); + } + } + + private void grow(int newLength) { + Object[] newElements = new Object[newLength]; + System.arraycopy(elements, 0, newElements, 0, elements.length); + elements = newElements; + capacity = newLength; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + + return elements[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + Object toRemove = elements[index]; + System.arraycopy(elements, index + 1, elements, index, size - index -1); + --size; + return toRemove; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos < size(); + } + + public Object next() { + return elements[pos++]; + } + } +} diff --git a/group09/601862675/Week01/src/main/java/BinaryTree.java b/group09/601862675/Week01/src/main/java/BinaryTree.java new file mode 100644 index 0000000000..2aa7d4a43e --- /dev/null +++ b/group09/601862675/Week01/src/main/java/BinaryTree.java @@ -0,0 +1,29 @@ +public class BinaryTree { + + private BinaryTreeNode root; + + private static class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + } + + public BinaryTree getLeft() { + + + return null; + } + + public void setLeft() { + + } + + public BinaryTree getRight() { + + return null; + } + + public void setRight() { + + } +} diff --git a/group09/601862675/Week01/src/main/java/Iterator.java b/group09/601862675/Week01/src/main/java/Iterator.java new file mode 100644 index 0000000000..f4f8fa52cd --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Iterator.java @@ -0,0 +1,7 @@ +/** + * Created by spike on 2/19/17. + */ +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group09/601862675/Week01/src/main/java/LinkedList.java b/group09/601862675/Week01/src/main/java/LinkedList.java new file mode 100644 index 0000000000..dca3992000 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/LinkedList.java @@ -0,0 +1,128 @@ +/** + * Created by spike on 2/19/17. + */ +public class LinkedList implements List { + + private LinkedListNode head; + private LinkedListNode tail; + private int size; + + private static class LinkedListNode { + private Object data; + private LinkedListNode prev; + private LinkedListNode next; + + private LinkedListNode(Object data, LinkedListNode prev, LinkedListNode next) { + this.data = data; + this.prev = prev; + this.next = next; + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("List: [ "); + LinkedListNode idx = head; + while (idx != null) { + builder.append(idx.data); + builder.append(" "); + idx = idx.next; + } + + builder.append("]"); + return builder.toString(); + } + + public void add(int index, Object object) { + if (index < 0 || index > size) { + throw new IllegalArgumentException(); + } + if (index == size) { // insert after + add(object); + } else { // insert before + LinkedListNode target = findNodeByIndex(index); + LinkedListNode nd = new LinkedListNode(object, target.prev, target); + if (head == target) { + head = nd; + } else { + target.prev.next = nd; + } + } + ++size; + } + + public void add(Object object) { + if (head == null) { + LinkedListNode nd = new LinkedListNode(object, null, null); + head = tail = nd; + } else { + LinkedListNode nd = new LinkedListNode(object, tail, null); + tail.next = nd; + tail = nd; + } + ++size; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + LinkedListNode target = findNodeByIndex(index); + return target.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + LinkedListNode target = findNodeByIndex(index); + if (target == head) { + if (head == tail) { + head = tail = null; + } else { + head = head.next; + head.prev = null; + } + } else if (target == tail) { + tail = tail.prev; + tail.next = null; + } else { + target.prev.next = target.next; + target.next.prev = target.prev; + } + target.prev = target.next = null; + --size; + return target.data; + } + + private LinkedListNode findNodeByIndex(int index) { + LinkedListNode target = head; + for (int i = 0; i != index; ++i) { + target = target.next; + } + return target; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + LinkedListNode cursor = head; + + public boolean hasNext() { + return cursor != null; + } + + public Object next() { + Object toRet = cursor.data; + cursor = cursor.next; + return toRet; + } + } +} diff --git a/group09/601862675/Week01/src/main/java/List.java b/group09/601862675/Week01/src/main/java/List.java new file mode 100644 index 0000000000..2e5e4477f5 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/List.java @@ -0,0 +1,7 @@ +public interface List { + void add(int index, Object object); + void add(Object object); + Object get(int i); + Object remove(int i); + int size(); +} diff --git a/group09/601862675/Week01/src/main/java/Queue.java b/group09/601862675/Week01/src/main/java/Queue.java new file mode 100644 index 0000000000..60f32933a3 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Queue.java @@ -0,0 +1,35 @@ +public class Queue { + + private LinkedList llist = new LinkedList(); + + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + if (llist.size() == 0) { + throw new UnsupportedOperationException(); + } + return llist.remove(0); + } + + public boolean isEmpty(){ + return llist.size() == 0; + } + + public int size(){ + return llist.size(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("Queue: [ "); + Iterator iter = llist.iterator(); + while (iter.hasNext()) { + builder.append(iter.next()); + builder.append(" "); + } + builder.append("]"); + return builder.toString(); + } +} diff --git a/group09/601862675/Week01/src/main/java/Stack.java b/group09/601862675/Week01/src/main/java/Stack.java new file mode 100644 index 0000000000..0aa097357d --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Stack.java @@ -0,0 +1,38 @@ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new UnsupportedOperationException(); + } + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("Stack: [ "); + Iterator iter = elementData.iterator(); + while (iter.hasNext()) { + builder.append(iter.next()); + builder.append(" "); + } + builder.append("]"); + return builder.toString(); + } +} \ No newline at end of file diff --git a/group09/601862675/Week01/src/main/java/Watcher.java b/group09/601862675/Week01/src/main/java/Watcher.java new file mode 100644 index 0000000000..eddc2ee28a --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Watcher.java @@ -0,0 +1,26 @@ +import java.nio.file.*; +import java.util.*; +import java.util.List; + +public class Watcher { + public static void main(String[] args) { + Path this_dir = Paths.get("."); + System.out.println("Now watching the current directory ..."); + + try { + WatchService watcher = this_dir.getFileSystem().newWatchService(); + this_dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); + + WatchKey watckKey = watcher.take(); + + List> events = watckKey.pollEvents(); + for (WatchEvent event : events) { + System.out.println("Someone just created the file '" + event.context().toString() + "'."); + + } + + } catch (Exception e) { + System.out.println("Error: " + e.toString()); + } + } +} \ No newline at end of file diff --git a/group09/601862675/Week01/src/test/java/ArrayListTest.java b/group09/601862675/Week01/src/test/java/ArrayListTest.java new file mode 100644 index 0000000000..923c76ce1f --- /dev/null +++ b/group09/601862675/Week01/src/test/java/ArrayListTest.java @@ -0,0 +1,104 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class ArrayListTest { + + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testAddWithIndex() { + log.clearLog(); + ArrayList list = initListWithSize(10); + list.add(3, 10); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 0 1 2 10 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size(), 11); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 0 1 2 10 3 4 5 6 7 8 9 11 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testAdd() { + log.clearLog(); + ArrayList list = new ArrayList(); + list.add(10); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 10 ]", log.getLog()); + System.out.println(); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddWithIndexOutOfBoundsException() { + ArrayList list = initListWithSize(ArrayList.MAX_LIST_SIZE); + Assert.assertEquals(48, list.size()); + list.add(1); + } + + @Test + public void testRemove() { + ArrayList list = initListWithSize(10); + + log.clearLog(); + Object removed = list.remove(0); + System.out.print(list); + Assert.assertEquals(0, removed); + Assert.assertEquals(9, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + removed = list.remove(list.size()-1); + System.out.print(list); + Assert.assertEquals(9, removed); + Assert.assertEquals(8, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 ]", log.getLog()); + System.out.println(); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetWithIllegalArgumentException() { + ArrayList list = new ArrayList(); + list.add(1); + Assert.assertEquals(1, list.size()); + list.get(list.size()); + } + + @Test + public void testSize() { + ArrayList list = new ArrayList(); + list.add(10); + list.add(20); + list.add(30); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + log.clearLog(); + ArrayList list = new ArrayList(); + for (int i = 0; i < 10; ++i) { + list.add(i); + } + Iterator iter = list.iterator(); + while (iter.hasNext()) { + System.out.print(iter.next()); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + private ArrayList initListWithSize(int size) { + ArrayList list = new ArrayList(); + for (int i = 0; i < size; ++i) { + list.add(i); + } + return list; + } +} diff --git a/group09/601862675/Week01/src/test/java/LinkedListTest.java b/group09/601862675/Week01/src/test/java/LinkedListTest.java new file mode 100644 index 0000000000..d36f67101e --- /dev/null +++ b/group09/601862675/Week01/src/test/java/LinkedListTest.java @@ -0,0 +1,106 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class LinkedListTest { + + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testAdd() { + log.clearLog(); + LinkedList list = initListWithSize(10); + System.out.print(list); + Assert.assertEquals("List: [ 0 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testAddWithIndex() { + log.clearLog(); + LinkedList list = initListWithSize(10); + list.add(0, -1); + System.out.print(list); + Assert.assertEquals(11, list.size()); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size()-1, 10); + System.out.print(list); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 10 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size(), 11); + System.out.print(list); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 10 9 11 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testRemove() { + log.clearLog(); + LinkedList list = initListWithSize(10); + list.remove(0); + System.out.print(list); + Assert.assertEquals(9, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.remove(list.size()-1); + System.out.print(list); + Assert.assertEquals(8, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.remove(list.size()-2); + System.out.print(list); + Assert.assertEquals(7, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 8 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testGet() { + log.clearLog(); + LinkedList list = initListWithSize(10); + for (int i = 0; i < list.size(); ++i) { + System.out.print(list.get(i)); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + @Test + public void testSize() { + log.clearLog(); + LinkedList list = initListWithSize(10); + Assert.assertEquals(10, list.size()); + System.out.println(); + } + + @Test + public void testIterator() { + log.clearLog(); + LinkedList list = initListWithSize(10); + Iterator iter = list.iterator(); + while (iter.hasNext()) { + System.out.print(iter.next()); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + private LinkedList initListWithSize(int size) { + LinkedList list = new LinkedList(); + for (int i = 0; i < size; ++i) { + list.add(i); + } + return list; + } +} diff --git a/group09/601862675/Week01/src/test/java/QueueTest.java b/group09/601862675/Week01/src/test/java/QueueTest.java new file mode 100644 index 0000000000..bd4e24a4e3 --- /dev/null +++ b/group09/601862675/Week01/src/test/java/QueueTest.java @@ -0,0 +1,44 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class QueueTest { + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testEnqueue() { + log.clearLog(); + Queue queue = new Queue(); + queue.enQueue(10); + System.out.print(queue); + Assert.assertEquals("Queue: [ 10 ]", log.getLog()); + } + + @Test + public void testDequeue() { + log.clearLog(); + Queue queue = new Queue(); + queue.enQueue(10); + queue.deQueue(); + System.out.print(queue); + Assert.assertEquals("Queue: [ ]", log.getLog()); + } + + @Test + public void testIsEmpty() { + Queue queue = new Queue(); + queue.enQueue(10); + Assert.assertEquals(false, queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + Queue queue = new Queue(); + queue.enQueue(10); + Assert.assertEquals(1, queue.size()); + } +} diff --git a/group09/601862675/Week01/src/test/java/StackTest.java b/group09/601862675/Week01/src/test/java/StackTest.java new file mode 100644 index 0000000000..0afac4c0da --- /dev/null +++ b/group09/601862675/Week01/src/test/java/StackTest.java @@ -0,0 +1,61 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class StackTest { + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testPush() { + log.clearLog(); + Stack stack = new Stack(); + stack.push("1"); + System.out.print(stack); + Assert.assertEquals(1, stack.size()); + Assert.assertEquals("Stack: [ 1 ]", log.getLog()); + } + + @Test + public void testPop() { + Stack stack = new Stack(); + stack.push(10); + Object o = stack.pop(); + Assert.assertEquals(10, o); + Assert.assertEquals(0, stack.size()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testPopWithException() { + Stack stack = new Stack(); + stack.push(10); + stack.pop(); + stack.pop(); + } + + @Test + public void testPeek() { + Stack stack = new Stack(); + stack.push(10); + Object o = stack.peek(); + Assert.assertEquals(10, o); + Assert.assertEquals(1, stack.size()); + } + + @Test + public void testIsEmpty() { + Stack stack = new Stack(); + stack.push(10); + Assert.assertEquals(false, stack.isEmpty()); + stack.pop(); + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + Stack stack = new Stack(); + stack.push(1); + Assert.assertEquals(1, stack.size()); + } +} diff --git a/group09/601862675/pom.xml b/group09/601862675/pom.xml new file mode 100644 index 0000000000..90585b6284 --- /dev/null +++ b/group09/601862675/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + me.sidzh + coding2017 + pom + 1.0-SNAPSHOT + + Week01 + + + + \ No newline at end of file