diff --git a/.gitignore b/.gitignore index 1a08aba0c8..8b13789179 100644 --- a/.gitignore +++ b/.gitignore @@ -1,30 +1 @@ -*.class -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear -target - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -#ide config -.metadata -.recommenders -.idea/ - - - -#macOS -.DS_Store - - -*.iml -rebel.* -.rebel.* - -target diff --git a/README.md b/README.md new file mode 100644 index 0000000000..a7d23c04cf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群 + +2017编程提高社群代码仓库所在地 \ No newline at end of file diff --git a/group01/1664823950/.project b/group01/1664823950/.project index 14ce66b1b3..6cca5cf64b 100644 --- a/group01/1664823950/.project +++ b/group01/1664823950/.project @@ -1,6 +1,10 @@ +<<<<<<< HEAD:group01/1664823950/.project 1664823950 +======= + 1264835468 +>>>>>>> master:group17/1264835468/.project diff --git a/group01/group01.md b/group01/group01.md index d3f5a12faa..8b13789179 100644 --- a/group01/group01.md +++ b/group01/group01.md @@ -1 +1 @@ - + diff --git a/group02/group02.md b/group02/group02.md index d3f5a12faa..8b13789179 100644 --- a/group02/group02.md +++ b/group02/group02.md @@ -1 +1 @@ - + diff --git a/group03/group03.md b/group03/group03.md index d3f5a12faa..8b13789179 100644 --- a/group03/group03.md +++ b/group03/group03.md @@ -1 +1 @@ - + diff --git a/group04/group04.md b/group04/group04.md index d3f5a12faa..8b13789179 100644 --- a/group04/group04.md +++ b/group04/group04.md @@ -1 +1 @@ - + diff --git a/group05/group05.md b/group05/group05.md index d3f5a12faa..8b13789179 100644 --- a/group05/group05.md +++ b/group05/group05.md @@ -1 +1 @@ - + diff --git a/group06/1049564215/src/com/coding/basic/MyArrayList.java b/group06/1049564215/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..13a0bd64a0 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,49 @@ +import java.util.*; + +/** + * @author CCD + * + */ +public class MyArrayList { + + private Object[] elementData = new Object[100]; + private int size = 100 ; + + public void add(Object o){ + elementData[size++] = o; + } + public void add(int index, Object o){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + return elementData[index]; + } + + public Object remove(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + Object E = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index - 1); + elementData[--size] = null; + return E; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } +} diff --git a/group06/1049564215/src/com/coding/basic/MyLinkedList.java b/group06/1049564215/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..275bf14a4f --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,124 @@ +import java.util.*; + +public class MyLinkedList implements List { + + private Node first; + private Node last; + private int size = 0 ; + public void Myadd(Object o){ + final Node l = last; + final Node newNode = new Node(l,o,null); + last = newNode; + if(l == null) + first = newNode; + else + l.next = newNode; + size++; + } + public void Myadd(int index , Object o){ + checkPosition(index); + if(index == size){ + Myadd(o); + } + else{ + final Node PreNode =GetNodeByIndex(index).prev; + final Node newNode = new Node(PreNode,o,GetNodeByIndex(index)); + PreNode.next =newNode; + if(PreNode == null) + first = newNode; //ΪʲôҪ������ָ�룿 + else + PreNode.next = newNode; + size++; + } + } + public Object get(int index){ + checkPosition(index); + return GetNodeByIndex(index); + } + public void remove(int index){ + Node node = GetNodeByIndex(index); + node.prev.next = node.next; + node.next.prev = node.prev; + node = null; + size--; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + final Node FirstNode= first; + final Node newNode = new Node(null,o,first); + first = newNode; + if(FirstNode == null) + last = newNode; + else + first.prev = newNode; + size++; + + } + public void addLast(Object o){ + final Node LastNode = last; + final Node newNode = new Node(last,o,null); + last = newNode; + if(last == null) + first = newNode; + else + last.next = newNode; + size++; + } + public void removeFirst(){ + final Node f = first; + if(f == null) + throw new NoSuchElementException(); + first = f.next; + first = null; + size--; + } + public void removeLast(){ + final Node f = last; + if(f == null) + throw new NoSuchElementException(); + last = last.prev; + last = null; + size--; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object item; + Node next; + Node prev; + Node (Node prev,Object element ,Node next){ + this.item = element; + this.next = next; + this.prev = prev; + } + } + + private Node GetNodeByIndex(int index){ + if(index > size/2) + { + Node Temp = first; + for(int i = 0; i< index;i++) + Temp = Temp.next; // + return Temp; + } + else + { + Node Temp = last; + for(int i = size-1; i> index; i--) + Temp = Temp.prev; + return Temp; + } + } + + private void checkPosition(int index){ + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("index:"+ index+"is llegal"); + } +} \ No newline at end of file diff --git a/group06/1049564215/src/com/coding/basic/MyQueue.java b/group06/1049564215/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c36703c145 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyQueue.java @@ -0,0 +1,52 @@ + +/** + * @author CCD + * + */ + +import java.util.*; + +public class MyQueue { + + private static final int DEFAULT_SIZE = 10; + + private Object[] elementData; + private int head; + private int tail; + public MyQueue(){ + this(DEFAULT_SIZE); + } + public MyQueue(int size){ + this.elementData = new Object[size]; + this.head = 0; + this.tail = 0; + } + + public void enQueue(Object o){ + if((tail+1)%elementData.length == head){ + } + else{ + elementData[tail] = o; + tail = (tail+1)%elementData.length; + } + } + + public Object deQueue(){ + if(head == tail){ + return null; + } + else{ + Object o = elementData[head]; + head = (head+1)% elementData.length; + return o ; + } + } + + public boolean isEmpty(){ + return head == tail ; + } + + public int size(){ + return (tail-head)&(elementData.length -1); + } +} diff --git a/group06/1049564215/src/com/coding/basic/MyStack.java b/group06/1049564215/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..f7968adb00 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyStack.java @@ -0,0 +1,45 @@ +import java.util.*; + +/** + * + */ + +/** + * @author CCD + * + */ +public class MyStack { + + private ArrayList elementData = new ArrayList(); + private Object[] Myelement = elementData.toArray(); + private int Length = elementData.size(); + + public void push(Object E){ + Myelement[++Length] = E ; + } + + public Object pop(){ + int NowLength = size()-1; + Object obj = peek(); + Length--; + Myelement[Length] = null ; + return obj; + } + + public Object peek(){ + int NowLength = size(); + if(NowLength == 0) + throw new EmptyStackException(); + NowLength -= 1 ; + if(NowLength >= Length ) + throw new ArrayIndexOutOfBoundsException(NowLength + " >= " + Length); + return Myelement[NowLength]; + + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return Length; + } +} diff --git a/group06/1259131938/JavaLearning/.classpath b/group06/1259131938/JavaLearning/.classpath new file mode 100644 index 0000000000..18d70f02cb --- /dev/null +++ b/group06/1259131938/JavaLearning/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1259131938/JavaLearning/.gitignore b/group06/1259131938/JavaLearning/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group06/1259131938/JavaLearning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1259131938/JavaLearning/.project b/group06/1259131938/JavaLearning/.project new file mode 100644 index 0000000000..63b66d0b73 --- /dev/null +++ b/group06/1259131938/JavaLearning/.project @@ -0,0 +1,17 @@ + + + JavaLearning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1259131938/JavaLearning/.settings/org.eclipse.jdt.core.prefs b/group06/1259131938/JavaLearning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..8000cd6ca6 --- /dev/null +++ b/group06/1259131938/JavaLearning/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/BinaryTreeNode.java b/group06/1259131938/JavaLearning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/Iterator.java b/group06/1259131938/JavaLearning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/List.java b/group06/1259131938/JavaLearning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group06/1259131938/JavaLearning/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(); +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..255b748204 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +/** + * @deprecated 用数组实现list + * @author wang + * + */ +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + // 确保数组大小 + ensureCapacity(size + 1); + // 数组赋值并使得size+1; + elementData[size ++] = o; + size ++; + } + + /** + * 确保数组不越界,否则就扩容; + * @param minCapacity list的大小; + */ + public void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = (oldCapacity / 3) * 2 + 1; + if (minCapacity > newCapacity) { + // 对数组扩容 + Object[] newDate = new Object[newCapacity]; + System.arraycopy(elementData, 0, newDate, 0, oldCapacity); + elementData = newDate; + } + } + + public void add(int index, Object o){ + // 对index进行校验: + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size ++; + } + + /** + * 边界检查: + * @param index + */ + private void rangeCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("size" + size + ", index:" + + index); + } + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index, elementData, index - 1, size - index); + return oldValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..89016c4b35 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + + +public class MyLinkedList implements List { + + private Node headNode; + + private Node endNode; + + private int size; + + public void add(Object o){ + add(size,o); + } + public void add(int index , Object o){ + addBefore(getNode(index),o); + } + + // 执行添加元素: + private void addBefore(Node node,Object o) { + Node newNode = new Node(o, node.prev, node.next); + newNode.prev.next = newNode; + newNode.next.prev = newNode; + size ++; + } + + // 获得元素; + private Node getNode(int index) { + Node rtnNode; + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + if (index < size / 2) { + rtnNode = headNode.next; + for (int i = 0; i < index; i++) { + rtnNode = rtnNode.next; + } + } else { + rtnNode = endNode; + for (int i = size; i > index; i--) { + rtnNode = rtnNode.prev; + } + } + return rtnNode; + } + + public Object get(int index){ + return getNode(index).data; + } + public Object remove(int index){ + return remove(getNode(index)); + } + + private Object remove(Node node) { + node.prev.next = node.next; + node.next.prev = node.prev; + size --; + return node.data; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(headNode.prev); + } + public void addLast(Object o){ + add(endNode.next); + } + public Object removeFirst(){ + remove(headNode); + return headNode.data; + } + public Object removeLast(){ + remove(endNode); + return endNode.data; + } + public Iterator iterator(){ + return null; + } + + public boolean isEmpty(){ + return size == 0; + } + + private static class Node{ + //当前元素,下一个及前一个; + Object data; + Node next; + Node prev; + public Node(Object data,Node prev, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..e576a1826d --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + Object oldValue = elementData.get(elementData.size()); + elementData.remove(elementData.size()); + return oldValue; + } + /** + * 查看栈顶元素; + * @return + */ + public Object peek(){ + return elementData.get(elementData.size()); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java b/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9e84a6c6b8 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java @@ -0,0 +1,20 @@ +package com.coding.basic; + +public class Queue { + MyLinkedList elementData = new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(elementData.size()); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group17/102228177/.classpath b/group06/1378560653/.classpath similarity index 100% rename from group17/102228177/.classpath rename to group06/1378560653/.classpath diff --git a/group18/935542673/Coding/.gitignore b/group06/1378560653/.gitignore similarity index 100% rename from group18/935542673/Coding/.gitignore rename to group06/1378560653/.gitignore diff --git a/group06/1378560653/.project b/group06/1378560653/.project new file mode 100644 index 0000000000..0feed82399 --- /dev/null +++ b/group06/1378560653/.project @@ -0,0 +1,17 @@ + + + 1378560653Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1378560653/src/com/coding/basic/ArrayList.java b/group06/1378560653/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8d9d0c30fb --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/ArrayList.java @@ -0,0 +1,95 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if( size <= elementData.length){ + elementData[size + 1] = o; + size++; + }else{ + elementData = grow(elementData, 1); + elementData[size+1] = o; + size++; + } + } + public void add(int index, Object o){ + Object[] temp = new Object[elementData.length]; + for(int i = 0; i= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + public static Object[] grow(Object[]src, int size){ + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group06/1378560653/src/com/coding/basic/BinaryTree.java b/group06/1378560653/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..042d3d8488 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; + + public BinaryTreeNode getRoot(){ + return root; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + if(root == null){ + root = node; + root.setLeft(null); + root.setRight(null); + return root; + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true){ + parentNode = currentNode; + + if(((Integer)node.getData()) > ((Integer)currentNode.getData())){ + currentNode = currentNode.getRight(); + if(currentNode == null){ + parentNode.setRight(node); + return node; + } + }else{ + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parentNode.setLeft(node); + return node; + } + } + } + } + } + +} diff --git a/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java b/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a8745a43c3 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.left = null; + this.right = null; + this.data = data; + } + + public Object getData() { + return data; + } + public void setData(int data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } +} + diff --git a/group06/1378560653/src/com/coding/basic/Iterator.java b/group06/1378560653/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group06/1378560653/src/com/coding/basic/LinkedList.java b/group06/1378560653/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3c82bb9da2 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = head; + while(head.next != null){ + pt = pt.next; + } + pt.next = new Node(o); + pt.next.next =null; + } + size++; + } + + public void add(int index , Object o){ + if(index < size){ + Node pt = head; + for(int i = 0; i < index-1; i++){ + pt = pt.next; + } + Node pt1 = pt.next.next; + pt.next = new Node(o); + pt.next.next = pt1; + size ++; + } + } + public Object get(int index){ + if(index < size){ + Node pt = head; + for(int i = 0; i < index; i++){ + pt = pt.next; + } + return pt.data; + }else{ + return null; + } + } + public Object remove(int index){ + if(index < size){ + Node pt = head; + for(int i = 0; i< index -1;i++){ + pt = pt.next; + } + Node pt1 = pt.next; + pt.next = pt1.next; + return pt1.data; + }else{ + return null; + } + } + + public int size(){ + if(null == head){ + size = 0; + }else{ + Node pt = head; + while(pt.next != null){ + size++; + } + } + return size; + } + + public void addFirst(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = new Node(o); + pt.next = head; + head = pt; + } + size++; + } + public void addLast(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = head; + while(pt.next != null){ + pt = pt.next; + } + pt.next = new Node(o); + Node pt1 = pt.next; + pt1.next = null; + } + size++; + } + public Object removeFirst(){ + if(null != head){ + Node pt = head; + head = pt.next; + size--; + return head.data; + }else{ + return null; + } + } + public Object removeLast(){ + if(null != head){ + Node pt = head; + while(pt.next.next != null){ + pt = pt.next; + } + Node pt1 = pt.next; + pt.next = null; + size--; + return pt1.data; + }else{ + return null; + } + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedlist = null; + int pos = 0; + + private LinkedListIterator(LinkedList linkedlist) { + this.linkedlist = linkedlist; + } + @Override + public boolean hasNext() { + pos++; + if(pos >= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + Node pt = head; + for(int i = 0; i < pos; i++ ){ + while(pt.next != null){ + pt = pt.next; + } + } + return pt.data; + } + + } + + private static class Node{ + public Node(Object o) { + this.data = o; + } + Object data; + Node next; + } +} diff --git a/group06/1378560653/src/com/coding/basic/List.java b/group06/1378560653/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group06/1378560653/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(); +} diff --git a/group06/1378560653/src/com/coding/basic/Queue.java b/group06/1378560653/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a574f4b859 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedlist = new LinkedList(); + + public void enQueue(Object o){ + linkedlist.add(o); + } + + public Object deQueue(){ + if(!isEmpty()){ + return linkedlist.get(0); + }else{ + return null; + } + } + + public boolean isEmpty(){ + if(size() > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return linkedlist.size(); + } +} diff --git a/group06/1378560653/src/com/coding/basic/Stack.java b/group06/1378560653/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2d0b260880 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(!isEmpty()){ + return elementData.remove(size()-1); + }else{ + return null; + } + } + + public Object peek(){ + if(!isEmpty()){ + return elementData.get(size()-1); + }else{ + return null; + } + } + public boolean isEmpty(){ + if(size() > 0){ + return false; + }else{ + return true; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/1454385822/.classpath b/group06/1454385822/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/1454385822/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1454385822/.gitignore b/group06/1454385822/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1454385822/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1454385822/.project b/group06/1454385822/.project new file mode 100644 index 0000000000..35750341bb --- /dev/null +++ b/group06/1454385822/.project @@ -0,0 +1,17 @@ + + + 1454385822Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..4399ea1b1d Binary files /dev/null and "b/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group06/1454385822/src/com/coding/basic/ArrayList.java b/group06/1454385822/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..571ca71f21 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/ArrayList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int pos = 0; // 当前数组的末尾元素的下一位置 + + private Object[] elementData = new Object[3]; + + public void add(Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + elementData[pos++] = o; + + } + public void add(int index, Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + /* + * 情况1.index < pos && pos < elementData.length - 1 + * index 只能在pos前面 + */ + if(index <= pos && pos <= elementData.length - 1){ + Object[] tem = new Object[pos - index]; + System.arraycopy(elementData, index, tem, 0, tem.length); + elementData[index] = o; + System.arraycopy(tem, 0, elementData, index + 1, tem.length); + pos++; + }else{ + throw new IndexOutOfBoundsException(); + } + + + + } + + public Object get(int index){ + if(index < pos){ + return elementData[index]; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + //将数字删除并将该数字后面的元素向前移动一位 + if(index < pos ){ //只有index在pos之前才进行删除操作 + result = elementData[index]; + if(pos - index > 1){ //删除的不是最后一个元素 + Object[] tem = new Object[pos - index - 1]; + System.arraycopy(elementData, index + 1, tem, 0, tem.length); + for(int i=0; i i2 ? 1 : (i1 == i2 ? 0 : -1); +// return result; +// } +// +//} diff --git a/group06/1454385822/src/com/coding/basic/Iterator.java b/group06/1454385822/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d2e7a2c23c --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/1454385822/src/com/coding/basic/LinkedList.java b/group06/1454385822/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..6197b9fb35 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/LinkedList.java @@ -0,0 +1,241 @@ +package com.coding.basic; + + +public class LinkedList implements List{ + + //private Node head; + private Node pre; //指向当前结点的前一个元素 + private Node pHead; //头节点指向第一个元素 + private Node cur; //指向链表的最后一个元素 + private int num = 0; //链表中的元素个数 + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if(pHead == null){ //链表为空,从第一个元素添加 + pHead = cur = node; + pHead.pre = null; + }else{ + node.pre = cur; //前一结点向后移动一位 + cur.next = node; // 添加元素 + cur = cur.next; //当前结点向后移动一位 + } + num++; //链表数目增1 + } + + /** + * 根据索引找到对应的结点 + * @param index + * @return + */ + public Node findNode(int index){ + Node node = pHead; + int tem = 0; + while(tem++ != index){ + node = node.next; + } + return node; + } + + public void add(int index , Object o){ + if(num == 0 || index == num){ + add(o); + return; + } + if(index <= num-1 && index > 0){ + Node node = new Node(); + node.data = o; + Node tem = findNode(index); + Node preNode = tem.pre; + Node posNode = tem.next; + preNode.next = node; + node.next = posNode; + posNode.pre = node; + num++; + return; + } + if(index == 0){ + Node node = new Node(); + node.data = o; + pHead.pre = node; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object get(int index){ + if(index <= num - 1 && index >= 0){ + return findNode(index).data; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + if(index >0 && index < num - 1){ //删除链表中间的元素 + Node node = findNode(index); + result = node.data; + Node preNode = node.pre; + Node posNode = node.next; + preNode.next = posNode; + posNode.pre = preNode; + num--; + return result; + } + if(index == 0 && num > 0){ //删除第一个元素 + Node node = pHead.next; + result = pHead.data; + node.pre = null; + pHead = node; + num--; + return result; + } + if(index == num - 1 && num > 0){ //删除最后一个元素 + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return num; + } + + public void addFirst(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = null; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + + } + public void addLast(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = cur; + cur.next = node; + node.next = null; + cur = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object removeFirst(){ + Object result; + if(num > 0){ + result = pHead.data; + if(num == 1){ + pHead = null; + num = 0; + } + if(num > 1){ + pHead = pHead.next; + pHead.pre = null; + num--; + } + return result; + } + throw new IndexOutOfBoundsException(); + } + + public Object removeLast(){ + Object result; + if(num == 1){ + result = pHead.data; + pHead = null; + num = 0; + return result; + } + if(num > 1){ + + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + public Iterator iterator(){ + return new Iterator(){ + int cur = 0; + Node node = pHead; + @Override + public boolean hasNext() { + if(cur++ < num){ + return true; + } + return false; + } + + @Override + public Object next() { + Object result = node.data; + node = node.next; + return result; + } + + }; + } + + + private static class Node{ + Object data; + Node pre; + Node next; + + } + + public static void main(String[]args){ + LinkedList list = new LinkedList(); + list.add(1); +// list.add(2); +// list.add(3); +// list.add(4); +// list.add(0, 0); +// list.addFirst(0); +// list.addLast(5); +// list.removeFirst(); + System.out.println(list.removeLast()); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } + +} + + + + + + + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/List.java b/group06/1454385822/src/com/coding/basic/List.java new file mode 100644 index 0000000000..1fd3aa61b3 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/List.java @@ -0,0 +1,11 @@ +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(); + +} diff --git a/group06/1454385822/src/com/coding/basic/Queue.java b/group06/1454385822/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c77317ef21 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + private int num = 0; + + public void enQueue(Object o){ + elementData.add(o); + num++; + } + + public Object deQueue(){ + num--; + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return num <= 0; + } + + public int size(){ + return num; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + System.out.println("当前队列的长度为:"+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQueue()); + } + + } + +} + + + diff --git a/group06/1454385822/src/com/coding/basic/Stack.java b/group06/1454385822/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2900430728 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/Stack.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int num = 0; + + public void push(Object o){ + elementData.add(o); + num++; + } + + public Object pop(){ + + return elementData.remove(--num) ; + } + + public Object peek(){ + return elementData.get(num - 1); + } + public boolean isEmpty(){ + return num <= 0 ; + } + public int size(){ + return num; + } + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.peek()); + System.out.println(stack.size()); +// while(!stack.isEmpty()){ +// System.out.println(stack.pop()); +// } + } +} + + + + diff --git a/group06/1730798243/.classpath b/group06/1730798243/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group06/1730798243/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/1730798243/.gitignore b/group06/1730798243/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1730798243/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1730798243/.project b/group06/1730798243/.project new file mode 100644 index 0000000000..b4bd3f32d4 --- /dev/null +++ b/group06/1730798243/.project @@ -0,0 +1,17 @@ + + + homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1730798243/src/com/coding/basic/first/Iterator.java b/group06/1730798243/src/com/coding/basic/first/Iterator.java new file mode 100644 index 0000000000..9ade302dda --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/Iterator.java @@ -0,0 +1,15 @@ +package com.coding.basic.first; + +public interface Iterator { + /** + * 查看是否有下一个元素 + * @return 有的话返回true,否则返回false + */ + public boolean hasNext(); + /** + * 获得下一个元素,也就是当前元素 + * @return 获取当前位置的元素 + */ + public Object next(); + +} diff --git a/group06/1730798243/src/com/coding/basic/first/List.java b/group06/1730798243/src/com/coding/basic/first/List.java new file mode 100644 index 0000000000..f999f13052 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/List.java @@ -0,0 +1,37 @@ +package com.coding.basic.first; +/** + * 数组的接口 + * @author zap + * + */ + +public interface List { + /** + * 向数组中添加一个元素 + * @param o 添加的元素 + */ + public void add(Object o); + /** + * 向数组中某一个位置添加一个元素 + * @param index 添加元素的位置 + * @param o 添加的元素 + */ + public void add(int index,Object o); + /** + * 从数组中根据位置获取一个元素 + * @param index 想要获取的元素的位置 + * @return 想获取的元素 + */ + public Object get(int index); + /** + * 根据位置移除数组中的一个元素 + * @param index 被移除元素的位置 + * @return 被移除的元素 + */ + public Object remove(int index); + /** + * 获取数组的长度 + * @return 返回数组的长度 + */ + public int size(); +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java b/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java new file mode 100644 index 0000000000..d42381300b --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java @@ -0,0 +1,87 @@ +package com.coding.basic.first.impl; + +import com.coding.basic.first.List; + +/** + * 实现一个ArrayList + * @author zap + * + */ + +public class ArrayList implements List{ + + private int size; + private Object[] elementData = null; + private static final int DEFAULT_SIZE=100; + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public ArrayList(int size) { + if(size < 0) { + System.out.println("size 必须大于0"); + } else { + this.elementData = new Object[size]; + } + } + + @Override + public int size() { + return size; + } + + @Override + public void add(Object o) { + judgeSize(size+1); + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + judgeIndexRangge(index); + System.arraycopy(elementData, index, elementData, index + 1, + size - index);//整体往后挪移--当前往后挪 + elementData[index] = o; + size++; + + } + + @Override + public Object get(int index) { + judgeIndexRangge(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + judgeIndexRangge(index); + Object e = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, + size - index - 1);//整体往前挪移--后一位往前挪 + size--; + return e; + } + + private void judgeIndexRangge(int index){ + + if (index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + + + private void judgeSize(int size) { + if(size > elementData.length){ + Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; + System.arraycopy(elementData, 0, newarr, 0, elementData.length); + this.elementData = newarr; + } + } + + + +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java b/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java new file mode 100644 index 0000000000..a06b635e04 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java @@ -0,0 +1,143 @@ +package com.coding.basic.first.impl; + +import com.coding.basic.first.List; + +/** + * 链表 + * @author zap + * LinkedList + */ +public class LinkedList implements List { + + + + private Node head = new Node(); + private int size ;// + + private static class Node{ + Object data; + Node next; + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + + @Override + public void add(Object o) { + + if(size==0){ + Node node = new Node(o); + head = node; +// head = tail = node; + }else{ + Node current = getCurrentNode(size); + Node node = new Node(o); + current.next = node; +// tail = node; + } + size ++; + + + } + + @Override + public void add(int index, Object o) { + judgeIndexRangge(index); + if(size == 0 ){ + Node node = new Node(o); + head = node; +// head = tail = node; + }else if(index == 0){ + Node node = new Node(o); + node.next = head; + head = node; + }else{ + Node prev = getCurrentNode(index); + Node temp = prev.next; + Node node = new Node(o); + node.next = temp; + prev.next = node; + } + size ++; + + } + + @Override + public Object get(int index) { + judgeGetIndexRangge(index); + Node current = getCurrentNode(index + 1); + return current.data; + } + + @Override + public Object remove(int index) { + judgeIndexRangge(index);//下标 + Object obj = null; + if(index == 0){ + Node node = head.next; + obj = head.data; + head = node; + }else{ + Node prev = getCurrentNode(index); + Node temp = prev.next; + Node after =temp.next ; + prev.next = after; + obj = temp.data; + } + size -- ; + return obj; + } + + @Override + public int size() { + return size; + } + + + + private void judgeIndexRangge(int index){ + + if (index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void judgeGetIndexRangge(int index){ + + if (index >= size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + + private Node getCurrentNode(int index){ + + Node temp = head; + Node prev = head; + for(int i = 0 ;i < index;i++){//找到该个元素 + prev = temp;// + temp = temp.next;// + } + return prev; + + } + + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + Object o = remove(0); + return o; + } + + +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/Queue.java b/group06/1730798243/src/com/coding/basic/first/impl/Queue.java new file mode 100644 index 0000000000..a38f33c759 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/Queue.java @@ -0,0 +1,46 @@ +package com.coding.basic.first.impl; + +/** + * 基本数据结构-队列 + * @author zap + * + */ + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + /** + * 入队操作 + * @param o 入队的元素 + */ + public void enQueue(Object o){ + linkedList.addLast(o); + } + + /** + * 出队操作 + * @return 返回出队的元素 + */ + public Object deQueue(){ + if(!isEmpty()) + return linkedList.removeFirst(); + return null; + } + + /** + * 判断队列是否为空 + * @return 为空返回true,否则返回false + */ + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + /** + * 返回队列的长度 + * @return + */ + public int size(){ + return linkedList.size(); + } +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/Stack.java b/group06/1730798243/src/com/coding/basic/first/impl/Stack.java new file mode 100644 index 0000000000..9e19850fb7 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/Stack.java @@ -0,0 +1,53 @@ +package com.coding.basic.first.impl; + +/** + * 基本数据结构-栈 + * @author Pxshuo + * + */ + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + /** + * 使一个元素入栈 + * @param o 将要入栈的元素 + */ + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + /** + * 使一个元素出栈 + * @return 返回出栈的元素 + */ + public Object pop(){ + return size() == 0 ? null : elementData.remove(elementData.size() - 1); + } + + /** + * 获得栈顶元素 + * @return 返回栈顶元素 + */ + public Object peek(){ + return elementData.size() == 0 ? null : elementData.get(elementData.size() - 1); + } + + /** + * 查看栈是否为空 + * @return 空的话返回true + */ + public boolean isEmpty(){ + return size() == 0 ? true : false; + } + + /** + * 查看栈中元素的个数 + * @return 返回栈中的个数 + */ + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/1730798243/src/com/coding/test/ArrayListTest.java b/group06/1730798243/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..a92bbef7a3 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,58 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.List; +import com.coding.basic.first.impl.ArrayList; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + + @Test + public void testSize() { + List list = new ArrayList (10); + list.add(2); + list.add(1); + int str = (int)list.remove(1); + assertEquals(1, str); + assertEquals(1, list.size()); + } + + @Test + public void testAddObject() { + List list = new ArrayList (10); + list.add(1); + list.add(3); + list.add(2,2); + + assertEquals(2, list.get(2)); + } + + + @Test + public void testGet() { + List list = new ArrayList (10); + list.add(1); + list.add(3); + list.add(0,2); + assertEquals(2, list.get(0)); + } + + @Test + public void testRemove() { + List list = new ArrayList (10); + list.add(1); + list.add(2); + list.remove(1); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/LinkedListTest.java b/group06/1730798243/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..7d50c34c42 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.List; +import com.coding.basic.first.impl.LinkedList; + +public class LinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(1, list.get(2)); + } + + @Test + public void testAddIntObject() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(8, list.get(3)); + } + + @Test + public void testGet() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(3, list.get(6)); + } + + @Test + public void testRemove() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.remove(3); + assertEquals(2, list.get(3)); + } + + @Test + public void testSize() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(7, list.size()); + } + + @Test + public void testAddLast() { + LinkedList list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.addLast(7); + assertEquals(7, list.get(7)); + } + + @Test + public void testRemoveFirst() { + LinkedList list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.removeFirst(); + assertEquals(4, list.get(0)); + assertEquals(6, list.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/QueueTest.java b/group06/1730798243/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..b6fafbe3f9 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/QueueTest.java @@ -0,0 +1,78 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.impl.Queue; + +public class QueueTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + assertEquals(5, queue.size()); + } + + @Test + public void testDeQueue() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + + @Test + public void testIsEmpty() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + + @Test + public void testSize() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/StackTest.java b/group06/1730798243/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..973b0cca35 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/StackTest.java @@ -0,0 +1,105 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.impl.Stack; + +public class StackTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.push("8"); + stack.push("3"); + stack.push("4"); + stack.push("5"); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testPop() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testPeek() { + + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + assertEquals(5,stack.peek()); + } + + @Test + public void testIsEmpty() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testSize() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + +} diff --git a/group06/236995728/.classpath b/group06/236995728/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group06/236995728/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/236995728/.gitignore b/group06/236995728/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group06/236995728/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/236995728/.project b/group06/236995728/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group06/236995728/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/.settings/org.eclipse.jdt.core.prefs b/group06/236995728/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from liuxin/.settings/org.eclipse.jdt.core.prefs rename to group06/236995728/.settings/org.eclipse.jdt.core.prefs diff --git "a/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..fa7a796a74 Binary files /dev/null and "b/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group06/236995728/src/com/coding/basic/ArrayList.java b/group06/236995728/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..54f7ee73d5 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayList implements List { + + private static int size = 0; + + private static Object[] elementData = new Object[100]; + + /** + * 添加元素 + */ + @Override + public void add(Object o){ + if(size >= elementData.length){ + grow(size); + } + elementData[size++] = o; + } + + /** + * 按索引添加元素 + */ + @Override + public void add(int index, Object o){ + if(index < 0){ + throw new IllegalArgumentException("param invalid"); + } + if(index >= elementData.length){ + grow(index); + } + for(int i=index; i<=size; i++){ + elementData[i] = elementData[i+1]; + } + elementData[index] = o; + size ++; + } + + /** + * 根据索引获取元素 + */ + @Override + public Object get(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + return elementData[index]; + } + + /** + * 根据索引删除元素 + */ + @Override + public Object remove(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + Object o = elementData[index]; + for(int i=index;i>1); + elementData = Arrays.copyOf(elementData, newCapacity); + } +} diff --git a/group06/236995728/src/com/coding/basic/ArrayListTest.java b/group06/236995728/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..1159bb1829 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayListTest { + private static ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<10;i++){ + list.add(i); + System.out.println(list.get(i)); + } + } + + @Test + public void testAddObject() { + list.add("www"); + assertEquals("www", list.get(10)); + } + + @Test + public void testAddIntObject() { + list.add(101, 101); + assertEquals(101, list.get(101)); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddIntObjectException1(){ + list.add(-1, -1); + } + + @Test + public void testGet() { + assertEquals(1, list.get(1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException1(){ + list.get(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException2(){ + list.get(11); + } + + @Test + public void testRemove() { + list.remove(3); + assertEquals(4, list.get(3)); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException1(){ + list.remove(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException2(){ + list.remove(1000000000); + } + + @Test + public void testSize() { + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group06/236995728/src/com/coding/basic/BinaryTreeNode.java b/group06/236995728/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git "a/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..fa7a796a74 Binary files /dev/null and "b/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group06/236995728/src/com/coding/basic/Iterator.java b/group06/236995728/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group06/236995728/src/com/coding/basic/LinkedList.java b/group06/236995728/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..57ee5a6dd9 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/LinkedList.java @@ -0,0 +1,149 @@ +package com.coding.basic; + +/** + * 2017/2/24 + * @author 236995728 + * 单链表 + */ +public class LinkedList implements List { + + private Node head = new Node(null,null); + private Node current = new Node(null, null); + + private int size = 0; + + /** + * 在尾节点添加节点 + */ + @Override + public void add(Object o){ + Node newNode = new Node(null,o); + if(head.next == null){ + head.next = newNode; + }else{ + current.next = newNode; + } + current = newNode; + size ++; + } + + /** + * 按照索引添加节点 + */ + @Override + public void add(int index , Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node newNode = new Node(null,o); + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node nextNode = null; + Object o = null; + for(int i=0; i { + private int data; + + public TreeData(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + @Override + public String toString() { + return data + ""; + } + + @Override + public int compareTo(Object o) { + return data - ((TreeData)o).data; + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java new file mode 100644 index 0000000000..68108e41a2 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java @@ -0,0 +1,138 @@ +package com.pxshuo.basic.impl; + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.List; + +/** + * 实现一个ArrayList + * @author Pxshuo + * + */ + +public class ArrayList implements List{ + + private int size = -1;//数组的长度的下标 + private Object[] elements = new Object[10];//数组内容 + private int addSize = 10;//每次增加的长度 + + @Override + public void add(Object o) { + elements = grow(); + size++; + elements[size] = o;//size与index同一个概念 + } + + @Override + public void add(int index, Object o) { + if (index > size + 1) { + return; + } + elements = grow(); + int moveNum = size - index + 1;//本次操作需要移动的元素的个数; + size++; + if (index >= elements.length - 1) {//按照位置来看 + elements = grow(elements, index - (elements.length - 1)); + size = index;//size与index同一个概念 + } + + /** + * 整体向后移一位 + */ + if(moveNum > 0){ + System.arraycopy(elements, index, elements, index + 1, moveNum); + } +// for(int i = size - 1; i >= index; i--) +// { +// elements[i] = elements[i-1]; +// } + + elements[index] = o; + } + + @Override + public Object get(int index) { + return elements[index]; + } + + @Override + public Object remove(int index) { + if (index > size) { + return null; + } + Object removeEle = elements[index]; + int moveNum = size - index;//本次操作需要移动的元素的个数; + if (moveNum > 0) { + System.arraycopy(elements, index + 1, elements, index, size - index + 1); + } + elements[size] = null; + size--; + return removeEle; + } + + @Override + public int size() { + return size + 1; + } + + /** + * 设置迭代器 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + + ArrayList arrayList = null; + int position = -1; + + public ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + position ++; + if (position >= arrayList.size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return arrayList.elements[position]; + } + + } + + /** + * 自动控制是否增加数组长度 + * @return 如果增加一条数据会造成数组溢出,则增加数组的长度,否则不进行改变。 + */ + private Object[] grow(){ + if (size() >= elements.length) { + return grow(elements, addSize); + } + else { + return elements; + } + + } + + /** + * 动态增加数组长度 + * @param src + * @param addSize + * @return + */ + private Object[] grow(Object[] src,int addSize){ + Object[] target = new Object[src.length + addSize]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + + //return Arrays.copyOf(src, src.length + addSize);同理 + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java new file mode 100644 index 0000000000..7ea54eae78 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java @@ -0,0 +1,65 @@ +package com.pxshuo.basic.impl; + +import com.pxshuo.basic.Iterator; + +/** + * 排序二叉树 + * @author Pxshuo + * + */ + +public class BinaryTree { + BinaryTreeNode root = null; + + /** + * 添加一个二叉树的节点 + * @param o + */ + public void add(Comparable o){ + if (root == null) { + root = new BinaryTreeNode(); + root.setData(o); + } + else { + root.insert(o); + } + } + + public Object get(int index){ + Stack findChild = childPath(index); + BinaryTreeNode child = null; + int childNum = 0; + for(;!findChild.isEmpty();){ + childNum = (int)findChild.pop(); + if (childNum != -1) { + child = child.getChild(childNum); + } + else { + child = root; + } + } + return child == null ? null : child.getData(); + } + + public void display(){ + root.display(1); + } + + private Stack childPath(int index) { + Stack findChild = new Stack(); + + while(true){ + if (index == 1 || index <= 0) { + findChild.push(-1); + return findChild; + } + if (index%2 == 1) { + findChild.push(1); + } + else { + findChild.push(0); + } + index = index/2; + } + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java new file mode 100644 index 0000000000..d028dc5f48 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java @@ -0,0 +1,109 @@ +package com.pxshuo.basic.impl; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int index = 0; + + public BinaryTreeNode() { + data = null; + left = null; + right = null; + } + + /** + * 差入一个二叉树节点 + * @param o + * @return + */ + public BinaryTreeNode insert(Comparable o){ + if(data == null){ + data = o; + return this; + } + //本节点已经存过数据 + BinaryTreeNode child = new BinaryTreeNode(); + child.setData(o); + if (o.compareTo(data) > 0) { + if (right == null) { + right = child; + } + else { + right.insert(o); + } + } + else {//小于等于的数据放在左子树中 + if (left == null) { + left = child; + } + else { + left.insert(o); + } + } + return child; + } + + /** + * 根据二叉树的位置获取孩子节点 + * @param index 0代表左孩子,1代表右孩子 + * @return + */ + public BinaryTreeNode getChild(int index){ + if(index == 0){ + return getLeft(); + } + else if(index == 1){ + return getRight(); + } + else { + return null; + } + } + + /** + * 用于打印二叉树 + * @param myIndex 在二叉树中的位置--采用完全二叉树 + */ + public void display(int myIndex){ + + System.out.println(myIndex + ":" + data.toString()); + if (left != null) { + left.display(2 * myIndex); + } + if (right != null) { + right.display(2 * myIndex + 1); + } + } + + /////////////////get和set函数/////////////////////////////////////// + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public void setIndex(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java new file mode 100644 index 0000000000..7711e72a86 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java @@ -0,0 +1,181 @@ +package com.pxshuo.basic.impl; + +import java.time.Period; + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.List; + +public class LinkedList implements List { + + private Node head = new Node(); + private Node tail = null; + private int size = -1;//与index的位置等同 + + //封装空表时候的增加与最后一次的删除-- + + public void add(Object o){ + Node node = new Node(o); + node.next = null; + + if (head.next == null) {//初始化 + head.next = node; + tail = node; + } + else { + tail.next = node; + tail = node; + } + size ++; + } + public void add(int index , Object o){ + if (index > size + 1) { + add(o); + } + else{ + Node prev = head; + Node current = new Node(o); + for(int i=0; i < index; i++) + { + prev = prev.next; + } + current.next = prev.next; + prev.next = current; + size ++; + } + } + public Object get(int index){ + if (index <= size) { + Node node = head; + for(int i = 0; i <= index;i++){ + node = node.next; + } + return node.data; + } + return null; + } + public Object remove(int index){ + Node remove = null; + if (index <= size) { + Node prev = head; + for(int i=0; i < index; i++) + { + prev = prev.next; + } + remove = prev.next; + prev.next = remove.next; + remove.next = null; + + if (index == size) {//设置尾部 + tail = prev; + if (size == 0) { + tail = null; + } + } + size --; + } + return remove != null ? remove.data : null; + } + + public int size(){ + return size + 1; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head.next; + head.next = first; + if(tail == null) + { + tail = first; + } + size ++; + } + public void addLast(Object o){ + if(tail == null){ + add(o); + } + else { + Node last = new Node(o); + last.next = null; + tail.next = last; + tail = last; + size ++; + } + + } + public Object removeFirst(){ + Node first = head.next; + if(first != null) + { + head.next = first.next; + first.next = null; + } + else { + head.next = null; + } + if (head.next == null) {//如果链表为空 + tail = null; + } + size --; + return first!=null ? first.data : null; + } + public Object removeLast(){ + Node last = head; + for(;last.next != tail; last = last.next ){ + + } + tail = last; + last = last.next; + if(tail == head){//最后一个元素 + head.next=null; + tail = null; + }else { + tail.next = null; + } + + size --; + return last != null? last.data : null; + } + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private static class Node{ + Object data; + Node next; + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + private class LinkedListIterator implements Iterator{ + LinkedList linkedList = null; + Node position = new Node(); + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + this.position = head; + } + + @Override + public boolean hasNext() { + position = position.next; + if (position == null) { + return false; + } + return true; + } + + @Override + public Object next() { + return position.data; + } + + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java new file mode 100644 index 0000000000..b042f1d18c --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java @@ -0,0 +1,44 @@ +package com.pxshuo.basic.impl; + +/** + * 基本数据结构-队列 + * @author Pxshuo + * + */ + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + /** + * 入队操作 + * @param o 入队的元素 + */ + public void enQueue(Object o){ + linkedList.addLast(o); + } + + /** + * 出队操作 + * @return 返回出队的元素 + */ + public Object deQueue(){ + return linkedList.removeFirst(); + } + + /** + * 判断队列是否为空 + * @return 为空返回true,否则返回false + */ + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + /** + * 返回队列的长度 + * @return + */ + public int size(){ + return linkedList.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java new file mode 100644 index 0000000000..f3f837e117 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java @@ -0,0 +1,51 @@ +package com.pxshuo.basic.impl; + +/** + * 基本数据结构-栈 + * @author Pxshuo + * + */ + +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 使一个元素入栈 + * @param o 将要入栈的元素 + */ + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + /** + * 使一个元素出栈 + * @return 返回出栈的元素 + */ + public Object pop(){ + return elementData.size() == 0 ? null : elementData.remove(elementData.size() - 1); + } + + /** + * 获得栈顶元素 + * @return 返回栈顶元素 + */ + public Object peek(){ + return elementData.size() == 0 ? null : elementData.get(elementData.size() - 1); + } + + /** + * 查看栈是否为空 + * @return 空的话返回true + */ + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + /** + * 查看栈中元素的个数 + * @return 返回栈中的个数 + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java new file mode 100644 index 0000000000..e87dca3a61 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java @@ -0,0 +1,14 @@ +/** + * 用于实现基本数据类型,包括但不仅限于: + * ArrayList + * Stack + * LinkedList + * Queue + * Tree + * Iterator + */ +/** + * @author Pxshuo + * + */ +package com.pxshuo.basic.impl; \ No newline at end of file diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java new file mode 100644 index 0000000000..c40ddac87d --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -0,0 +1,59 @@ +package com.pxshuo.test; + + +import com.pxshuo.basic.Iterator; +import com.pxshuo.basic.TreeData; +import com.pxshuo.basic.impl.ArrayList; +import com.pxshuo.basic.impl.BinaryTree; +import com.pxshuo.basic.impl.LinkedList; +import com.pxshuo.basic.impl.Queue; +import com.pxshuo.basic.impl.Stack; + +public class Test { + public static void main(String[] args) { +// LinkedList arrayList = new LinkedList(); +// arrayList.add("hello1"); +// arrayList.add("hello2"); +// arrayList.add(9,"hello3"); +// //arrayList.add(10,"hello4"); +// arrayList.addLast("hi"); +// arrayList.addLast("hihi"); +// arrayList.addFirst("hi1"); +// arrayList.removeFirst(); +// arrayList.removeLast(); +// arrayList.add(1,"hi1"); +// arrayList.remove(1); +// //arrayList.add(0, "hi"); +// //arrayList.remove(8); +// //arrayList.remove(0); +// for (Iterator iterator = arrayList.iterator(); iterator.hasNext();) { +// System.out.println("hi"+iterator.next()); +// } + //Queue queue = new Queue(); +// Stack stack = new Stack(); +// +// for (int i = 0; i < 10; i++) { +// //queue.enQueue("test-" + i); +// stack.push("test-" + i); +// } +// for(int i =0; i< 11; i++) +// { +// System.out.println(stack.pop()); +// } +// stack.push("test-" + 233); +// System.out.println(stack.pop()); + + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + System.out.println(binaryTree.get(5).getClass()); + + //binaryTree.display(); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java new file mode 100644 index 0000000000..4249574817 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java @@ -0,0 +1,42 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.ArrayList; + +public class ArrayListTest { + ArrayList object = new ArrayList(); + + @Test + public void addTest() { + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(3) ); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java new file mode 100644 index 0000000000..a9d67f7ba1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java @@ -0,0 +1,26 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.TreeData; +import com.pxshuo.basic.impl.BinaryTree; + +public class BinaryTreeTest { + BinaryTree object = new BinaryTree(); + + @Test + public void binaryTest() { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + Assert.assertEquals("4", binaryTree.get(5).toString()); + Assert.assertEquals("8", binaryTree.get(7).toString()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java new file mode 100644 index 0000000000..72fd2c49f1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java @@ -0,0 +1,43 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.ArrayList; +import com.pxshuo.basic.impl.LinkedList; + +public class LinkedListTest { + LinkedList object = new LinkedList(); + + @Test + public void addTest() { + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(0)); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java new file mode 100644 index 0000000000..4f2b5735e4 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java @@ -0,0 +1,23 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.Queue; + +public class QueueTest { + public Queue object = new Queue(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.enQueue("hello"); + object.enQueue("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("hello", object.deQueue()); + Assert.assertEquals("world", object.deQueue()); + Assert.assertEquals(0, object.size()); + } + +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java new file mode 100644 index 0000000000..df1e254595 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java @@ -0,0 +1,25 @@ +package test.com.pxshuo.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.basic.impl.Queue; +import com.pxshuo.basic.impl.Stack; + +public class StackTest { + public Stack object = new Stack(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.push("hello"); + object.push("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("world", object.peek()); + Assert.assertEquals("world", object.pop()); + Assert.assertEquals("hello", object.pop()); + Assert.assertEquals(0, object.size()); + } + +} diff --git "a/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..ee1bdbd433 --- /dev/null +++ "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,13 @@ +# 计算机CPU、硬盘、内存以及指令之间的关系 + +by 2415980327 + +​ 但凡常见的事物,总会让人习以为常。伴随着计算机在在现代社会中的普及,人们可以日常生活中随处见到计算机在辅助我们进行各种各样的工作。在未经深究的情况下,我们就自然而然的认为,计算机本身就是这个样子。计算机实际上是什么样,我也不清楚。从小学知道这种事物开始,也是慢慢地在了解这种机器。 + +​ 就我所知,我们目前使用的常见的个人计算机在概念上是属于图灵机的。图灵机这个概念是研究将人们数学的运算过程使用机器来进行代替。它是由一个纸带作为输入与输出,同时使用一个处理器来处理这个纸带,达到运算的目的。类似是程序中函数的概念。或许来说程序中函数的概念要比图灵机的概念晚得多,但是作为一个程序员来说,就没有必要非要把自己置于一无所知的情形下再去学习这个概念。既然我已经知道了函数这个概念,那我就用函数来类比图灵机的概念吧。类似于函数的输入-处理-输出这种结构,想必大家也是一目了然的。 + +​ 图灵机只是作为一个理论上模拟出来的机器,只是为了证明这样是可行的。而实际上实现的是冯诺依曼体系结构的计算机。我们日常所见的计算机也是基于这种体系结构建立起来的。冯诺依曼体系结构是分为五部分的,包括存储器,运算器、控制器、输入设备和输出设备。同样也可以类比为函数,输入设备输出设备同图灵机一样只是作为输入与输出,剩下的存储器、控制器与运算器是做为处理器存在的,类似于函数中的变量,逻辑结构与逻辑运算的存在。存储器就是存储一些信息,运算器是做一些实际上的运算,控制器是进行程序指令的跳转,来实现各种不同的结构。 + +​ 而本文提及的CPU就是运算器与控制器的集合,内存就相当于函数中的变量,也就是相当于存储器。那硬盘是用来做什么的呢?硬盘是用作数据的持久化,一方面由于成本较低,所以是当做大量的数据存储单元。另一方面由于内存断电会清空,所以使用硬盘来存储信息更为方便。所以我认为硬盘应该是游离于这个体系之外的辅助设备。类似于程序的数据库或者程序的文本存档。 + +​ 指令与数据平时存储于硬盘之中,在需要运行程序时,将其读入到内存中来,通过CPU来处理内存中的数据。那么CPU为什么不直接处理硬盘中的数据呢?因为硬盘相比于内存的访问速度太慢了,相比于CPU的处理速度更是慢到离谱,所以才需要内存的当做存储器为CPU的处理工作提供支持。 \ No newline at end of file diff --git a/group06/263050006/article.txt b/group06/263050006/article.txt new file mode 100644 index 0000000000..1bec070839 --- /dev/null +++ b/group06/263050006/article.txt @@ -0,0 +1,2 @@ +CPU���ڴ桢Ӳ�̡�ָ���Լ�����֮��Ĺ�ϵ +https://zhuanlan.zhihu.com/p/25442061 \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyArrayList.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyArrayList.java new file mode 100644 index 0000000000..c7c1e175e0 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyArrayList.java @@ -0,0 +1,80 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.Arrays; + +public class MyArrayList { + private int size = 0; + private int initialSize; + private Object[] elements = null; + + public MyArrayList(int initialSize){ + this.initialSize = initialSize; + elements = new Object[initialSize]; + } + + public void add(E element){ + //�ﵽ�������ޣ���initialSize����50% + if(++size == elements.length){ + elements = Arrays.copyOf(elements, size + (int)Math.round(initialSize * 0.5)); + } + elements[size - 1] = element; + } + + //�� + public void add(int index, E element){ + //index=sizeʱ���൱�ڵ���add���� + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + for (int i=size; i > index; i--){ + elements[i] = elements[i - 1]; + } + elements[index] = element; + } + + //�� + public E remove(int index){ + E removed = (E)elements[index]; + for(int i=index; i 0){ + //�����ڸ�treenode���Ҳ���ɹ����򷵻�true + if(parent.getLeftChild() == null){ + TreeNode node = new TreeNode(obj); + node.setParent(parent); + parent.setLeftChild(node); + return true; + } + return insert(parent.getLeftChild(), obj); + } + if(parent.getRightChild() == null){ + TreeNode node = new TreeNode(obj); + node.setParent(parent); + parent.setRightChild(node); + return true; + } + return insert(parent.getRightChild(), obj); + } + + @Override + public String toString() { + if(root == null){ + return ""; + } + return root.toString(); + } + + private static class TreeNode { + private TreeNode parent; + private TreeNode leftChild; + private TreeNode rightChild; + private Comparable userObject;//�ڵ㶼�������洢���ݵ� + + public TreeNode(Comparable userObject){ + this.userObject = userObject; + } + + public Comparable getUserObject() { + return userObject; + } + + public TreeNode getParent() { + return parent; + } + + public void setParent(TreeNode parent) { + this.parent = parent; + } + + public TreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(TreeNode leftChild) { + this.leftChild = leftChild; + } + + public TreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(TreeNode rightChild) { + this.rightChild = rightChild; + } + + @Override + public String toString() { + return "TreeNode [parent=" + (parent==null?null:parent.getUserObject()) + ", leftChild=" + (leftChild==null?null:leftChild) + + ", rightChild=" + (rightChild==null?null:rightChild) + ", userObject=" + + userObject + "]"; + } + + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java new file mode 100644 index 0000000000..7c35be59aa --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java @@ -0,0 +1,8 @@ +package com.github.chaoswang.learning.java.collection.myown; + +public interface MyIterator { + /*��arraylist��ʵ��һ��������*/ + boolean hasNext(); + Object next(); + void remove(); +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java new file mode 100644 index 0000000000..007a6e48c3 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java @@ -0,0 +1,120 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.NoSuchElementException; + +public class MyLinkedList { + private int size = 0; + private Node head = null; + private Node tail = null; + + //�� + public void add(E element){ + Node tmp = new Node(element, null); + if(tail == null){ + head = tmp; + }else{ + tail.next = tmp;; + } + tail = tmp; + size++; + } + + public void add(int index, E element){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + Node tmpBefore = getElement(index -1); + Node tmpAfter = getElement(index); + Node tmp = new Node(element, tmpAfter); + tmpBefore.next = tmp; + + } + + public void addFirst(E element){ + Node tmp = new Node(element, null); + if(head != null){ + tmp.next = head; + }else{ + tail = tmp; + } + head = tmp; + } + + public E removeFirst(){ + if(size <= 0){ + throw new NoSuchElementException(); + } + Node tmp = head; + head = head.next; + return (E)tmp.element; + } + + //�� + public E remove(int index) { + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException(); + } + Node tmpBeore = this.getElement(index-1); + Node tmp = this.getElement(index); + Node tmpNext = this.getElement(index+1); + tmpBeore.next = tmpNext; + size--; + return (E)tmp.element; + } + + //�� + public E get(int index){ + return (E)this.getElement(index).element; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if(head == null){ + return "[]"; + } + StringBuffer sb = new StringBuffer("["); + Node tmp = head; + while(tmp != null){ + sb.append(tmp.element.toString()); + sb.append(","); + tmp = tmp.next; + } + String returnStr = sb.toString(); + returnStr = returnStr.substring(0, returnStr.length()-1); + return returnStr + "]"; + } + + private Node getElement(int index) { + Node tmp = head; + for(int i=0;i { + private int size = 0; + private int capacitySize; + private OneElement first = null; + private OneElement last = null;//���Ը�Ϊ���Լ���LinkedListʵ�� + + public MyQueue(int capacitySize){ + this.capacitySize = capacitySize; + } + + //�����β ������ʱ���쳣 + public void add(E element){ + if(size+1 > capacitySize){ + throw new IllegalStateException("over capacity."); + } + addLast(element); + } + + //�����β �����쳣���᷵���Ƿ����ɹ� + public boolean offer(E element){ + if(size+1 > capacitySize){ + return false; + } + addLast(element); + return true; + } + + private void addLast(E element){ + OneElement tmp = new OneElement(element, null); + if(last == null){ + first = tmp; + }else{ + last.next = tmp;; + } + last = tmp; + size++; + } + + //�Ƴ���ͷԪ�أ�����Ϊ��ʱ���쳣 + public E remove(){ + if(size == 0){ + throw new NoSuchElementException(); + } + return removeFirst(); + } + + //�Ƴ���ͷԪ�أ�����Ϊ��ʱ����null������queueһ�㲻�ò���nullԪ�� + public E poll(){ + if(size == 0){ + return null; + } + return removeFirst(); + } + + private E removeFirst(){ + OneElement tmp = first; + first = first.next; + size--; + return tmp.element; + } + + //���ض�ͷԪ�أ����Dz��Ƴ�������Ϊ��ʱ���쳣 + public E element(){ + if(size == 0){ + throw new NoSuchElementException(); + } + return first.element; + } + + //���ض�ͷԪ�أ����Dz��Ƴ�������Ϊ��ʱ����null + public E peek(){ + if(size == 0){ + return null; + } + return first.element; + } + + private class OneElement{ + private E element = null; + private OneElement next = null; + + public OneElement(E element, OneElement next) { + this.element = element; + this.next = next; + } + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java new file mode 100644 index 0000000000..f425c8de63 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java @@ -0,0 +1,63 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.Arrays; +import java.util.EmptyStackException; + +public class MyStack { + private int size = 0; + private int initialSize; + private Object[] elements = null;//���Ը�Ϊ���Լ���ArrayList��ʵ�� + + public MyStack(int initialSize){ + this.initialSize = initialSize; + elements = new Object[initialSize]; + } + + //ѹջ + public void push(E element){ + //�ﵽ�������ޣ���initialSize����100% + if(++size == elements.length){ + elements = Arrays.copyOf(elements, size + initialSize); + } + elements[size - 1] = element; + } + + //�ж�ջ�Ƿ�Ϊ�� + public boolean empty(){ + return size <= 0? true : false; + } + + public int size(){ + return size; + } + + //�鿴ջ��Ԫ�� + public E peek(){ + if(size == 0){ + throw new EmptyStackException(); + } + return (E)elements[size - 1]; + } + + //����ջ��Ԫ�� + public E pop(){ + if(size == 0){ + throw new EmptyStackException(); + } + E removed = (E)elements[size -1]; + elements[size -1] = null; + size--; + return removed; + } + + public int search(E element) { + int index = 0; + for (int i = 0; i < size - 1; i++) { + if (element.equals(elements[i])) { + index = (size -1) - i; + break; + } + } + return index; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java new file mode 100644 index 0000000000..dcb4a59a8b --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java @@ -0,0 +1,59 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.List; + +public class MyTree { + + public MyTree(MyTreeNode treeNode){ + + } + + + private class MyTreeNode { + private MyTreeNode parent; + private List children;//������ʱ������Ϊleft��right + private boolean allowsChildren; + private Object userObject;//�ڵ㶼�������洢���ݵ� + + public MyTreeNode(Object userObject){ + this.userObject = userObject; + } + + public List children(){ + return children; + } + + public MyTreeNode getChildAt(int childIndex){ + return children.get(childIndex); + } + + public int getIndex(MyTreeNode node){ + return children.indexOf(node); + } + + public MyTreeNode getParent(){ + return parent; + } + + public boolean isLeaf(){ + return children.size() > 0 ? false : true; + } + + public boolean getAllowsChildren(){ + return allowsChildren; + } + + public void insert(MyTreeNode node, int index){ + children.remove(node); + children.add(index, node); + } + + public void remove(int index){ + children.remove(index); + } + + public void remove(MyTreeNode node){ + children.remove(node); + } + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java new file mode 100644 index 0000000000..cb83d6e506 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java @@ -0,0 +1,53 @@ +package com.github.chaoswang.learning.java.collection.myown; + + +import org.junit.Assert; +import org.junit.Test; + +import com.github.chaoswang.learning.java.collection.myown.MyArrayList; + +public class MyArrayListTest { + + @Test + public void testAdd(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("3"); + Assert.assertEquals(3, myList.size()); + myList.add("4"); + Assert.assertEquals(4, myList.size()); + String str = myList.get(2); + Assert.assertEquals("3", str); + + } + + @Test + public void testInsert(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("4"); + String str = myList.get(2); + Assert.assertEquals("4", str); + myList.add(2,"3"); + str = myList.get(2); + Assert.assertEquals("3", str); + } + + @Test + public void testRemove(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.remove(2); + Assert.assertEquals("3", str); + str = myList.get(2); + Assert.assertEquals("4", str); + Assert.assertEquals(3, myList.size()); + } + + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java new file mode 100644 index 0000000000..9c76a8056c --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java @@ -0,0 +1,17 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import org.junit.Test; + +public class MyBinarySearchTreeTest { + @Test + public void testInsert(){ + MyBinarySearchTree tree = new MyBinarySearchTree(12); + tree.insert(5); + tree.insert(18); + tree.insert(2); + tree.insert(9); + tree.insert(15); + tree.insert(19); + System.out.println(tree); + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java new file mode 100644 index 0000000000..cdbbcf2812 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java @@ -0,0 +1,76 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import org.junit.Assert; +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void testAdd(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + Assert.assertEquals(3, myList.size()); + myList.add("4"); + Assert.assertEquals(4, myList.size()); + System.out.println(myList); + String str = myList.get(2); + Assert.assertEquals("3", str); + + } + + @Test + public void testInsert(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("4"); + String str = myList.get(2); + Assert.assertEquals("4", str); + myList.add(2,"3"); + str = myList.get(2); + Assert.assertEquals("3", str); + } + + @Test + public void testAddFirst(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("2"); + myList.add("3"); + myList.add("4"); + System.out.println(myList); + Assert.assertEquals("2", myList.get(0)); + myList.addFirst("1"); + Assert.assertEquals("1", myList.get(0)); + System.out.println(myList); + } + + @Test + public void testRemoveFirst(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.removeFirst(); + System.out.println(myList); + Assert.assertEquals("1", str); + Assert.assertEquals("2", myList.get(0)); + } + + @Test + public void testRemove(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.remove(2); + Assert.assertEquals("3", str); + str = myList.get(2); + Assert.assertEquals("4", str); + Assert.assertEquals(3, myList.size()); + } + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java new file mode 100644 index 0000000000..79579c6719 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java @@ -0,0 +1,55 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MyQueueTest { + + @Rule + public ExpectedException thrown= ExpectedException.none(); + + @Test + public void testAdd(){ + MyQueue myQueue = new MyQueue(3); + //��3��Ԫ�� + myQueue.add("1"); + myQueue.add("2"); + myQueue.offer("3"); + //�������ӷ���false + Assert.assertFalse(myQueue.offer("4")); + //��ȡ + Assert.assertEquals("1", myQueue.element()); + Assert.assertEquals("1", myQueue.peek()); + //��ʼ�Ƴ� + Assert.assertEquals("1", myQueue.remove()); + Assert.assertEquals("2", myQueue.remove()); + Assert.assertEquals("3", myQueue.poll()); + //�����Ƴ�����false + Assert.assertNull(myQueue.poll()); + } + + @Test + public void testAddWhenQueueIsFull(){ + thrown.expect(IllegalStateException.class); + MyQueue myQueue = new MyQueue(3); + myQueue.add("1"); + myQueue.add("2"); + myQueue.add("3"); + //�����������쳣 + myQueue.add("4"); + } + + @Test + public void testRemoveWhenQueueIsEmpty(){ + thrown.expect(NoSuchElementException.class); + MyQueue myQueue = new MyQueue(3); + myQueue.add("1"); + myQueue.remove(); + //�����Ƴ����쳣 + myQueue.remove(); + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java new file mode 100644 index 0000000000..4ae0c84989 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java @@ -0,0 +1,47 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.EmptyStackException; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MyStackTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testPushAndPop(){ + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + Assert.assertEquals("3", myStack.pop()); + Assert.assertEquals("2", myStack.peek()); + Assert.assertEquals("2", myStack.pop()); + Assert.assertEquals("1", myStack.peek()); + } + + @Test + public void testPopWhenQueueIsEmpty(){ + thrown.expect(EmptyStackException.class); + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.pop(); + //�����Ƴ����쳣 + myStack.pop(); + } + + @Test + public void testSearch(){ + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + Assert.assertEquals(2, myStack.search("1")); + Assert.assertEquals(1, myStack.search("2")); + Assert.assertEquals(0, myStack.search("3")); + } +} diff --git a/group06/284999210/.classpath b/group06/284999210/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/284999210/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/284999210/.gitignore b/group06/284999210/.gitignore new file mode 100644 index 0000000000..f5ebe83019 --- /dev/null +++ b/group06/284999210/.gitignore @@ -0,0 +1,3 @@ +*.class +/.metadata +/bin \ No newline at end of file diff --git a/group06/284999210/.project b/group06/284999210/.project new file mode 100644 index 0000000000..c3e8c69e46 --- /dev/null +++ b/group06/284999210/.project @@ -0,0 +1,17 @@ + + + 284999210 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" "b/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" new file mode 100644 index 0000000000..d7812844b3 Binary files /dev/null and "b/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" differ diff --git a/group06/284999210/src/com/coding/basic/container/ArrayList.java b/group06/284999210/src/com/coding/basic/container/ArrayList.java new file mode 100644 index 0000000000..3d091153c9 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/ArrayList.java @@ -0,0 +1,156 @@ +package com.coding.basic.container; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private Object[] elementData; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 8; + + public ArrayList() { + elementData = new Object[8]; + size = 0; + capacity = DEFAULT_CAPACITY; + } + + public boolean add(Object element) { + if (size == capacity) { + Object[] tempArray = new Object[capacity]; + for (int i = 0; i < size; i++) { + tempArray[i] = elementData[i]; + } + elementData = new Object[capacity * 2]; + for (int i = 0; i < size; i++) { + elementData[i] = tempArray[i]; + } + elementData[capacity] = element; + capacity = capacity * 2; + } + elementData[size++] = element; + return true; + } + + @Override + public void add(int index, Object element) { + checkIndex(index); + Object[] tempArray = new Object[capacity]; + for (int i = 0; i < size; i++) { + tempArray[i] = elementData[i]; + } + elementData = new Object[capacity * 2]; + for (int i = 0; i < size; i++) { + if (i < index) { + elementData[i] = tempArray[i]; + } else { + elementData[i + 1] = tempArray[i]; + } + } + elementData[index] = element; + capacity = capacity * 2; + size++; + } + + @SuppressWarnings("unchecked") + public Object remove(int index) { + checkIndex(index); + + Object o = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size] = null; + size = size - 1; + return o; + } + + private void checkIndex(int index) { + if (index >= capacity) { + throw new IndexOutOfBoundsException(); + } + } + + public Object set(int index, Object element) { + checkIndex(index); + + Object o = elementData[index]; + elementData[index] = element; + return o; + } + + @SuppressWarnings("unchecked") + public Object get(int index) { + checkIndex(index); + + return (Object) elementData[index]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + if (i != size - 1) { + sb.append(elementData[i] + ", "); + } else { + sb.append(elementData[i]); + } + } + sb.append("]"); + return sb.toString(); + } + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public Iterator iterator() { + return new IteratorArrayList(); + } + + @Override + public boolean remove(Object element) { + if (element == null) + return false; + int findIndex = -1; + for (int i = 0; i < size; i++) { + if (elementData[i].equals(element)) { + findIndex = i; + break; + } + } + + for (int i = findIndex; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return false; + } + + private class IteratorArrayList implements Iterator { + + private int cursor; + + @Override + public boolean hasNext() { + return cursor < size; + } + + @Override + public Object next() { + if (cursor < size) + return elementData[cursor++]; + else + throw new NoSuchElementException(); + } + + } +} diff --git a/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java b/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java new file mode 100644 index 0000000000..09b2a9db9f --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package com.coding.basic.container; + +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T o) { + if (null == root) { + root = new BinaryTreeNode(o, null, null); + return root; + } + + return insert(o, root); + } + public BinaryTreeNode insert(T o, BinaryTreeNode node) { + BinaryTreeNode nodeNew = new BinaryTreeNode(o, null, null); + BinaryTreeNode nodeCurrent = node; + if (o.compareTo(nodeCurrent.data) < 0) { + if (nodeCurrent.left == null) { + nodeCurrent.left = nodeNew; + return nodeNew; + } else { + return insert(o, nodeCurrent.left); + } + } else if (o.compareTo(nodeCurrent.data) > 0) { + if (nodeCurrent.right == null) { + nodeCurrent.right = nodeNew; + return nodeNew; + } else { + return insert(o, nodeCurrent.right); + } + } else { + return nodeCurrent; + } + } +} diff --git a/group06/284999210/src/com/coding/basic/container/Iterator.java b/group06/284999210/src/com/coding/basic/container/Iterator.java new file mode 100644 index 0000000000..5ec89a4983 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic.container; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); +} diff --git a/group06/284999210/src/com/coding/basic/container/LinkedList.java b/group06/284999210/src/com/coding/basic/container/LinkedList.java new file mode 100644 index 0000000000..239c4189ec --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/LinkedList.java @@ -0,0 +1,186 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class LinkedList implements List { + + private int size; + private Node head; + + public LinkedList() { + head = new Node(null, null, null); + size = 0; + } + + @Override + public boolean add(Object o) { + Node nodeAdd; + Node nodeCurrent = head; + while (nodeCurrent.next != null) { + nodeCurrent = nodeCurrent.next; + } + nodeAdd = new Node(o, nodeCurrent, null); + nodeCurrent.next = nodeAdd; + size++; + return true; + } + + @Override + public boolean remove(Object o) { + if (head.next == null) { + return false; + } + Node nodeCurrent = head; + + while (nodeCurrent.next != null) { + nodeCurrent = nodeCurrent.next; + if (nodeCurrent.data.equals(o)) { + nodeCurrent.previous.next = nodeCurrent.next; + nodeCurrent.next.previous = nodeCurrent.previous; + size--; + return true; + } + } + return false; + } + + @Override + public Object get(int index) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + return nodeCurrent.data; + } + + @Override + public Object set(int index, Object element) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + Object o = nodeCurrent.data; + nodeCurrent.data = element; + return o; + } + + @Override + public void add(int index, Object element) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + Node nodeNew = new Node(element, nodeCurrent.previous, nodeCurrent); + nodeCurrent.previous = nodeNew; + size++; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Node nodeCurrent = head; + + int i = 0; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + + Object o = nodeCurrent.data; + if (index == size - 1) { + nodeCurrent.previous.next = null; + } else { + nodeCurrent.previous.next = nodeCurrent.next; + nodeCurrent.next.previous = nodeCurrent.previous; + } + + size--; + return o; + } + + private void checkIndex(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return 0 == size; + } + + private static class Node { + public Node(Object data, Node pre, Node next) { + this.data = data; + this.previous = pre; + this.next = next; + } + + Object data; + Node previous; + Node next; + } + + public void addFirst(Object o) { + add(0, o); + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Object o = remove(0); + return o; + } + + public Object removeLast() { + Object o = remove(size); + return o; + } + + @Override + public Iterator iterator() { + return new IteratorlinkedList(); + } + + private class IteratorlinkedList implements Iterator { + + private int cursor; + + @Override + public boolean hasNext() { + return cursor < size - 1; + } + + @Override + public Object next() { + if (hasNext()) { + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < cursor + 1); + cursor++; + return nodeCurrent.data; + } else { + return null; + } + } + } +} diff --git a/group06/284999210/src/com/coding/basic/container/List.java b/group06/284999210/src/com/coding/basic/container/List.java new file mode 100644 index 0000000000..112d3c2f1e --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/List.java @@ -0,0 +1,28 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public interface List { + public boolean add(Object element); + + public void add(int index, Object element); + + public Object remove(int index); + + public boolean remove(Object element); + + public Object set(int index, Object element); + + public Object get(int index); + + public int size(); + + public boolean isEmpty(); + + public Iterator iterator(); +} diff --git a/group06/284999210/src/com/coding/basic/container/Queue.java b/group06/284999210/src/com/coding/basic/container/Queue.java new file mode 100644 index 0000000000..1157e8a473 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Queue.java @@ -0,0 +1,34 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class Queue { + + private ArrayList list = new ArrayList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + final int size = list.size(); + if (0 == size) + return null; + Object o = list.remove(size); + return o; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + +} diff --git a/group06/284999210/src/com/coding/basic/container/Stack.java b/group06/284999210/src/com/coding/basic/container/Stack.java new file mode 100644 index 0000000000..2fd59b0c9c --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Stack.java @@ -0,0 +1,39 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + final int size = elementData.size(); + if (0 == size) + return null; + return elementData.get(size); + } + + public Object peek() { + final int size = elementData.size(); + if (0 == size) + return null; + Object o = elementData.remove(size - 1); + return o; + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } +} diff --git a/group06/284999210/src/com/coding/basic/container/test/TestContainer.java b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java new file mode 100644 index 0000000000..d68104f703 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java @@ -0,0 +1,71 @@ +/** + * + */ +package com.coding.basic.container.test; + +import java.util.List; + +/** + * @author devin.yin + * + */ +public class TestContainer { + + /** + * @param args + */ + public static void main(String[] args) { + List list1 = new java.util.ArrayList(); + System.out.println(list1); + + // 4 basic operation for java.util.ArrayList--add remove change query + list1.add("0"); + list1.add("1"); + list1.add("2"); + list1.add("3"); + list1.add("4"); + list1.add("5"); + list1.add("6"); + list1.add("7"); + list1.add("8"); + list1.add("9"); + System.out.println(list1); + + list1.remove(0); + System.out.println(list1); + + list1.set(0, "set"); + System.out.println(list1); + + System.out.println(list1.get(0)); + + list1.add(9, "10"); + System.out.println(list1); + + System.out.println("------------------------------------------------"); + + // 4 basic operation for com.coding.basic.container.ArrayList--add remove change query + com.coding.basic.container.ArrayList list2 = new com.coding.basic.container.ArrayList(); + System.out.println(list2); + list2.add("0"); + list2.add("1"); + list2.add("2"); + list2.add("3"); + list2.add("4"); + list2.add("5"); + list2.add("6"); + list2.add("7"); + list2.add("8"); + list2.add("9"); + System.out.println(list2); + + list2.remove(0); + System.out.println(list2); + + list2.set(0, "set"); + System.out.println(list2); + + System.out.println(list2.get(0)); + } + +} diff --git a/group06/290149544/blog/README.md b/group06/290149544/blog/README.md new file mode 100644 index 0000000000..17ed336d0c --- /dev/null +++ b/group06/290149544/blog/README.md @@ -0,0 +1 @@ +[CSDN����:](http://blog.csdn.net/dzxxbj) ��Ų�����ҵ \ No newline at end of file diff --git a/group06/290149544/hw1/.classpath b/group06/290149544/hw1/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/290149544/hw1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/290149544/hw1/.gitattributes b/group06/290149544/hw1/.gitattributes new file mode 100644 index 0000000000..bdb0cabc87 --- /dev/null +++ b/group06/290149544/hw1/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/group06/290149544/hw1/.gitignore b/group06/290149544/hw1/.gitignore new file mode 100644 index 0000000000..b1f3107451 --- /dev/null +++ b/group06/290149544/hw1/.gitignore @@ -0,0 +1,63 @@ +*.class + +# 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* + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk diff --git a/group06/290149544/hw1/.project b/group06/290149544/hw1/.project new file mode 100644 index 0000000000..f2571d221d --- /dev/null +++ b/group06/290149544/hw1/.project @@ -0,0 +1,17 @@ + + + hw1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/290149544/hw1/README.md b/group06/290149544/hw1/README.md new file mode 100644 index 0000000000..724da1115d --- /dev/null +++ b/group06/290149544/hw1/README.md @@ -0,0 +1,3 @@ +1. 添加泛型 +2. 添加单元测试 +3. Object是什么类型,如果要处理各种不同的数据类型怎么办这些数据结构呢? \ No newline at end of file diff --git a/group06/290149544/hw1/src/hw1/ArrayList.java b/group06/290149544/hw1/src/hw1/ArrayList.java new file mode 100644 index 0000000000..20a48d10ef --- /dev/null +++ b/group06/290149544/hw1/src/hw1/ArrayList.java @@ -0,0 +1,58 @@ +package hw1; + +import java.util.Arrays; + +// 先不考虑线程安全,增删改查 +// 业务导向就是复制 +public class ArrayList implements List { + // 又想到阁成员函数 + private int size = 0; // 属于elementData的属性 + // 暂时不要泛型,以后优化 + private Object[] elementData = new Object[10]; + public void add(Object o) { + elementData = grow(elementData, 10); + elementData[size()] = o; + } + public void add(int index, Object o) { + elementData = grow(elementData, 10); + for (int i = elementData.length-1; i >= index ; i--) { + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + } + public Object get(int index) { + // 入境检查 + if (index >= elementData.length) { + System.out.println("如何抛出数组越界异常?"); + } + return elementData[index]; + } + public Object remove(int index) { + // 入境检查 + if (index >= elementData.length || index < 0) { + System.out.println("如何抛出数组越界异常?"); + } + for (int i = index; i < elementData.length; i++) { + elementData[i-1] = elementData[i]; + } + return null; + } + public int size() { // 元素的个数 +// return -1; + return size; + } + public void print() { + for (int i = 0; i < size(); i++) { + System.out.println(elementData[i]); + } + } + // 注意有返回值 + private Object[] grow(Object[] src, int size) { + if (size() < src.length) { + return src; // 说明至少还能再放一个 + } else { + // 放不下了,则增加10个,数据结构是层层服务的 + return Arrays.copyOf(src, src.length+size); + } + } +} \ No newline at end of file diff --git a/group06/290149544/hw1/src/hw1/JavaTest.java b/group06/290149544/hw1/src/hw1/JavaTest.java new file mode 100644 index 0000000000..a59b8e2e10 --- /dev/null +++ b/group06/290149544/hw1/src/hw1/JavaTest.java @@ -0,0 +1,24 @@ +package hw1; + +import java.util.Arrays; + +public class JavaTest { + public static void main(String[] args) { + int[] a = new int[10]; // 创建了一个数组对象 + a[0] = 0; + a[1] = 1; + a[2] = 2; + a[3] = 3; +// a[10] = 3; + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + } + // 然后就开始扩张了,ArrayList能自增长 + public static int[] grow(int[]src, int size) { + return Arrays.copyOf(src, src.length+size); +// int[] target = new int[src.length+size]; +// System.arraycopy(src, 0, target, 0, src.length); +// return target; + } +} diff --git a/group06/290149544/hw1/src/hw1/LinkedList.java b/group06/290149544/hw1/src/hw1/LinkedList.java new file mode 100644 index 0000000000..7966173b79 --- /dev/null +++ b/group06/290149544/hw1/src/hw1/LinkedList.java @@ -0,0 +1,115 @@ +package hw1; + +public class LinkedList implements List { +// public static void main(String[] args) { +// +// } + private Node head = null; + private int size; // 链表的节点个数,从1开始计数的哦 + + public void add(Object o) { + // 没有头节点,则创建头节点 + if (null == head) { + head = new Node(); + head.next = null; + } +// Node temp = new Node(); +// temp = head.next; + Node newNode = new Node(); + newNode.data = o; + newNode.next = head.next; + head.next = newNode; + + size++; + } + public void add(int index, Object o) { + + size++; + } + // 头节点的索引为0 + public Object get(int index) { + Node ptr = new Node(); + ptr = head; + // 如果index越界,则抛出异常啊?要抛出异常吗? + if (index >= size() || index < 0) { + System.out.println("要抛出异常吗?"); + return null; + } else { + // 假设头节点有数据吧,节约点资源 + for (int i = 0; i < index; i++) { + ptr = ptr.next; + } + } + return ptr; + } + // 删除特定索引位置的对象 + public Object remove(int index) { + Node ptr = new Node(); + Node temp = new Node(); + ptr = head; + // 如果index越界,则抛出异常啊?要抛出异常吗? + if (index >= size() || index < 0) { + System.out.println("要抛出异常吗?"); + return null; + } else { + // 假设头节点有数据吧,节约点资源 + for (int i = 0; i < index; i++) { + ptr = ptr.next; + } + // 此时ptr指向的就是index这个节点,但是我们要的是前一个节点 + temp = ptr.next; + ptr.next = ptr.next.next; + } + size--; + return temp; + } + public int size() { + // 隐藏的成本就是多写一个函数,没别的用途就是隐藏安全用的 + return size; + } + // 因为头节点head里面也是有数据的,所以这里的插入指的是插到头前面 + public void addFirst(Object o) { + // 不能发呆了 + // 首先创建节点 + Node ptr = new Node(); + ptr.data = o; + ptr.next = head; + head = ptr; + size++; + } + // 最后一个节点指向 null嘛 + public void addLast(Object o) { + Node ptr = new Node(); + ptr.data = o; + ptr.next = null; + // 取尾节点,size正好比索引大一个 + Node lastNode = head; + for (int i = 0; i < size-1; i++) { + lastNode = lastNode.next; + } + lastNode.next = ptr; + size++; + } + public Object removeLast() { + // 取尾节点前一个,size正好比索引大一个 + Node lastNode = head; + for (int i = 0; i < size-2; i++) { + lastNode = lastNode.next; + } + lastNode.next = null; + size--; + return null; + } + public Object removeFirst() { + Node firstNode = new Node(); + firstNode.next = head.next; + head = firstNode; + size--; + return null; + } + // 节点静态内部类 + private static class Node { + Object data; // Object 是一个通用的类型 + Node next; + } +} diff --git a/group06/290149544/hw1/src/hw1/List.java b/group06/290149544/hw1/src/hw1/List.java new file mode 100644 index 0000000000..cf68bf919e --- /dev/null +++ b/group06/290149544/hw1/src/hw1/List.java @@ -0,0 +1,9 @@ +package hw1; + +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/group06/290149544/hw1/src/hw1/Queue.java b/group06/290149544/hw1/src/hw1/Queue.java new file mode 100644 index 0000000000..a8340c6a8d --- /dev/null +++ b/group06/290149544/hw1/src/hw1/Queue.java @@ -0,0 +1,20 @@ +package hw1; + +// 本质是排队,先进先出,公平嘛 +// 往队列头删除,往队列尾部插入,因为栈是严格在顶部操作,而队列就复杂的多 +public class Queue { + public void enQueue() { + + } + + public Object deQueue() { + return null; + } + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} diff --git a/group06/290149544/hw1/src/hw1/Stack.java b/group06/290149544/hw1/src/hw1/Stack.java new file mode 100644 index 0000000000..590aa3a93b --- /dev/null +++ b/group06/290149544/hw1/src/hw1/Stack.java @@ -0,0 +1,39 @@ +package hw1; + +// mini jvm 用来做函数调用,比如表达式求值啦,都会用到栈 +public class Stack { + // 动态需要的成员属性 + private int size = 0; + + // ArrayList 使用我们自己定义的数据结构,自己吃自己的狗粮 + private ArrayList elementData = new ArrayList(); // 估计先调用自己吧 + + // push对内部而言就是向数组插入一个元素,吃自己的狗粮了,狗粮是之前做的独立的 + public void push(Object o) { + elementData.add(o); + size++; + } + // 弹出一个元素,内部实现就是删除一个元素 + public Object pop() { + // + Object temp; + temp = elementData.remove(size); + size--; // 单独写,保证代码可读性 + return temp; + } + public boolean isEmpty() { + if (size == 0) { + return true; + } else { + return false; + } + } + + public Object peek() { + return elementData.get(size-1); + } + + public int size() { + return size; + } +} diff --git a/group06/309953838/.classpath b/group06/309953838/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/309953838/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/309953838/.gitignore b/group06/309953838/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/309953838/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/309953838/.project b/group06/309953838/.project new file mode 100644 index 0000000000..7d9becf359 --- /dev/null +++ b/group06/309953838/.project @@ -0,0 +1,17 @@ + + + 309953838Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/309953838/src/com/team6/week1/ArrayList.java b/group06/309953838/src/com/team6/week1/ArrayList.java new file mode 100644 index 0000000000..6aba74bd93 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/ArrayList.java @@ -0,0 +1,57 @@ +package com.team6.week1; + +public class ArrayList implements List { + + int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + + if (size >= elementData.length) { + Object[] tem = new Object[elementData.length * 2]; + for (int i = 0; i < size; i++) { + tem[i] = elementData[i]; + } + elementData = tem; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (size >= elementData.length) { + Object[] tem = new Object[elementData.length * 2]; + for (int i = 0; i < size; i++) { + tem[i] = elementData[i]; + } + elementData = tem; + } + for (int i = index; i < size; i++) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + for (int i = index; i < size; i++) { + elementData[i - 1] = elementData[i]; + } + size--; + return elementData[index]; + } + + public int size() { + return size; + + } + +} diff --git a/group06/309953838/src/com/team6/week1/LinkedList.java b/group06/309953838/src/com/team6/week1/LinkedList.java new file mode 100644 index 0000000000..d3863b0b05 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/LinkedList.java @@ -0,0 +1,121 @@ +package com.team6.week1; + +public class LinkedList implements List { + + private Node root; + int index; + + public void addNode(String name) { + if (root == null) { + root = new Node(name); + } else { + root.add(name); + } + } + + class Node { + Object data; + Node next; + Node(Object data) { + this.data = data; + } + + + public void add(Object data) { + if (this.next == null) { + this.next = new Node(data); + } else { + this.next.add(data); + } + } + + + public Object del(int i) { + if (this.next != null) { + index++; + if (i == index) { + this.next = this.next.next; + return this.next.data; + } else { + this.next.del(i); + } + } + return null; + } + + public void traversal() { + if (this.next != null) { + index++; + this.next.traversal(); + } + } + + public void add(int i, Object o) { + if (this.next != null) { + if (i == index) { + Node node = new Node(data); + node.next = this.next.next; + this.next = node; + return; + } else { + this.next.add(i, o); + } + index++; + } + } + + public Object get(int i) { + if (this.next != null) { + + if (i == index) { + return this.data; + } else { + this.next.get(i); + } + index++; + } + return null; + } + + } + + @Override + public void add(Object data) { + if (root == null) { + root = new Node(data); + } else { + root.add(data); + } + } + + @Override + public void add(int index, Object o) { + if (root != null) { + root.add(index, o); + } + } + + @Override + public Object get(int index) { + if (root.next != null) { + return root.get(index); + } + return null; + } + + @Override + public Object remove(int index) { + if (root != null) { + return root.del(index); + } + return null; + } + + @Override + public int size() { + if (root != null) { + root.traversal(); + } + return index; + } +} diff --git a/group06/309953838/src/com/team6/week1/List.java b/group06/309953838/src/com/team6/week1/List.java new file mode 100644 index 0000000000..ee63e5d6f3 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/List.java @@ -0,0 +1,14 @@ +package com.team6.week1; + +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/group06/309953838/src/com/team6/week1/Queue.java b/group06/309953838/src/com/team6/week1/Queue.java new file mode 100644 index 0000000000..6bdd2604a8 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/Queue.java @@ -0,0 +1,62 @@ +package com.team6.week1; + +public class Queue { + private Node first; + private int index; + + class Node{ + Object data; + Node next; + + + Node(Object data){ + this.data = data; + } + + public void add(Object data){ + if(this.next == null){ + this.next = new Node(data); + }else{ + this.next.add(data); + } + } + + + public void traversal(){ + if(this.next != null){ + index++; + this.next.traversal(); + } + } + } + + public void enQueue(Object o){ + if(first != null){ + first.add(o); + } + } + + public Object deQueue(){ + if(first != null){ + Object obj = first.data; + first = first.next; + return obj; + } + return null; + } + + public boolean isEmpty(){ + if(first == null){ + return true; + }else{ + return false; + } + } + + public int size(){ + if(first != null){ + first.traversal(); + } + return index; + } +} diff --git a/group06/309953838/src/com/team6/week1/Stack.java b/group06/309953838/src/com/team6/week1/Stack.java new file mode 100644 index 0000000000..6568b5dea9 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/Stack.java @@ -0,0 +1,32 @@ +package com.team6.week1; + +public class Stack { + +private List elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int size = elementData.size(); + Object obj = elementData.remove(size--); + return obj; + } + + public Object peek(){ + int size = elementData.size(); + return elementData.get(size - 1); + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/547958234/src/com/coding/basic/ArrayList.java b/group06/547958234/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..30be14a906 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/ArrayList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +import java.util.*; + +public class ArrayList implements List { + private int size = 0; + private static final int INCREMENT = 10; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + growLength(); + elementData[size++] = o; + } + public void add(int index, Object o){ + growLength(); + 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) { + //如果之前分配了很多内存,多次remove后是否需要将elementData手动缩小容量 + if (index > size) throw new IndexOutOfBoundsException("index: "+index+", size: "+size); + Object retVal = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + elementData[--size] = null; + return retVal; + } + + public int size(){ + return this.size; + } + public Iterator iterator(){ + return null; + } + + private void growLength() { + if (this.size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length+INCREMENT); + } + } + +} diff --git a/group06/547958234/src/com/coding/basic/BinaryTreeNode.java b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4dff1cdb31 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + if (o < this.data) { + if (this.left != null) { + this.left.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.left = node; + } + } + if (o > this.data) { + if (this.right != null) { + this.right.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.right = node; + } + } + return this; + } + +} diff --git a/group06/547958234/src/com/coding/basic/Iterator.java b/group06/547958234/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/547958234/src/com/coding/basic/LinkedList.java b/group06/547958234/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..bc4dd44b5b --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedList.java @@ -0,0 +1,116 @@ +package com.coding.basic; + +import sun.jvm.hotspot.debugger.win32.coff.AuxBfEfRecord; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + if (this.size == 0) { + addFirst(o); + } else { + Node node = findNode(size - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + node.next = newNode; + this.size++; + } + } + + public void add(int index, Object o) { + ensureNoOverStep(index); + if (index == 0) { + addFirst(o); + } else if (index == this.size) { + addLast(o); + } else { + Node beforeNode = findNode(index - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = beforeNode.next; + beforeNode.next = newNode; + this.size++; + } + + } + + public Object get(int index) { + ensureNoOverStep(index); + Node node = findNode(index); + return node.data; + } + + public Object remove(int index) { + //只需要把对应节点从链表中摘出来就行了? + ensureNoOverStep(index); + if (index == 0) { + return removeFirst(); + } else if (index == this.size - 1) { + return removeLast(); + } else { + Node beforeNode = findNode(index - 1); + Node selectNode = beforeNode.next; + beforeNode.next = selectNode.next; + this.size--; + return selectNode.data; + } + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + node.next = this.head; + this.head = node; + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = new Node(); + node = head; + head = head.next; + this.size--; + return node.data; + } + + public Object removeLast() { + Node beforeNode = findNode(this.size - 2); + Node node = beforeNode.next; + beforeNode.next = null; + this.size--; + return node.data; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + } + + private Node findNode(int index) { + ensureNoOverStep(index); + Node node = this.head; + while (index > 0) { + node = node.next; + index--; + } + return node; + } + + private void ensureNoOverStep(int index) { + if (index > this.size) throw new IndexOutOfBoundsException("Index: " + index + ", size: " + this.size); + } +} diff --git a/group06/547958234/src/com/coding/basic/LinkedListTest.java b/group06/547958234/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..afe137351f --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,20 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList; +/** + * Created by mac on 2017/2/21. + */ +public class LinkedListTest { + public static void main(String[] args) { + LinkedList l = new LinkedList(); + l.add(0); + l.add(1); + l.add(2); + l.add(3,3); + Object ret = l.remove(1); + + for(int i=0;i 0) + + System.arraycopy(elementData, index+1, elementData, index ,size - index-1); + elementData[--size] = null; + + + return o; + } + public int size(){ + return size; + + } + public int Capacity(){ + return capacity; + } + private void rangCheck(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +// private class MyIndexOutOfBoundsException extends RuntimeException{ +// @SuppressWarnings("unused") +// public MyIndexOutOfBoundsException(String e) { +// super(); +// } +// } + private void ensureCapacityInternal(int minCapacity) { + + modCount++; + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(10, minCapacity); + capacity=minCapacity; + } + if (minCapacity - elementData.length > 0) + + grow(minCapacity); + } + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + capacity=newCapacity; + + elementData = Arrays.copyOf(elementData, newCapacity); + } + public Iterator iterator() { + return new Itr(); + } + private class Itr implements Iterator { + int cursor; + int lastRet = -1; + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData[lastRet = i]; + + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + + } + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + +} diff --git a/group06/736464448/data_structure/MyBinaryTree.java b/group06/736464448/data_structure/MyBinaryTree.java new file mode 100644 index 0000000000..f7ba05f9cd --- /dev/null +++ b/group06/736464448/data_structure/MyBinaryTree.java @@ -0,0 +1,198 @@ +package data_structure; + + + +public class MyBinaryTree { + + private BinaryTreeNode root; + private int size; + + + + public void add(int key,Object o) { + size++; + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); + if(parent==null) + root=newnode; + else{ + target=compareKey(key,parent); + + if (key < target.key) { + target.left = newnode; + newnode.top = target; + } else if(key > target.key){ + target.right = newnode; + newnode.top = target; + } + else{ + target.data=o; + size--; + } + + } + + } + public Object get(int key){ + BinaryTreeNode target=null; + target=search( key); + if(target==null) + return null; + else + return target.data; + } + private BinaryTreeNode search(int key){ + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + if(parent==null) + return null; + + else + target=compareKey(key,parent); + if (key == target.key) { + + return target; + } + return null; + } + public Object remove(int key){ + BinaryTreeNode replace=null; + BinaryTreeNode target=null; + BinaryTreeNode oldnode=null; + + target=search( key); + if(target==null) + return null; + else + { + oldnode=target; + if(target.left==null&&target.right==null){ + + changeParent( target,null); + target=null; + } + else if(target.left!=null&&target.right==null){ + // replace=next(target.left); + // target=replace; + // changeParent(target,replace); + // changeChild(target, replace); + // changeParent(replace,null); + // target=null; + + replace=target.left; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left==null&&target.right!=null){ +// replace=prev(target.right); +// target=replace; +// changeParent(target,replace); +// changeChild(target, replace); +// changeParent(replace,null); +// replace=null; + + replace=target.right; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left!=null&&target.right!=null){ + int prev=prev(target.right).key; + int next=next(target.left).key; + if((next-key)>(key-prev)) + replace=prev(target.right); + else + replace=next(target.left); + target=replace; + changeParent(target,replace); + changeChild(target, replace); + changeParent(replace,null); + replace=null; + } + } + size--; + return oldnode.data; + } + private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ + BinaryTreeNode targetparent=null; + targetparent=target.top; + if(targetparent.key>target.key) + targetparent.left=child; + else + targetparent.right=child; + + } + private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ + BinaryTreeNode targetleftchild=null; + BinaryTreeNode targetrightchild=null; + targetleftchild=target.left; + targetrightchild=target.right; + if(targetleftchild!=null) + targetleftchild.top=parent; + if(targetrightchild!=null) + targetrightchild.top=parent; + + } + //找到前驱节点 + private BinaryTreeNode prev(BinaryTreeNode target){ + // BinaryTreeNode prev=null; + while(target.left!=null){ + target=target.left; + } + return target; + + } + //找到后驱节点 + private BinaryTreeNode next(BinaryTreeNode target){ +// BinaryTreeNode next=null; + while(target.right!=null){ + target=target.right; + } + return target; + + } + public int size(){ + + return size; + } + private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { + BinaryTreeNode parent=node; + while (parent != null) { + + if (key < parent.key&&parent.left!=null) { + parent = parent.left; + } else if (key > parent.key&&parent.right!=null) { + parent = parent.right; + } else { + return parent; + + } + } + return parent; + + + } + + + + + private static class BinaryTreeNode{ + Object data; + int key; + BinaryTreeNode left; + BinaryTreeNode right; + BinaryTreeNode top; + public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ + this.key=key; + this.data=o; + this.left=left; + this.right=right; + this.top=top; + + } + + + } +} diff --git a/group06/736464448/data_structure/MyLinkedList.java b/group06/736464448/data_structure/MyLinkedList.java new file mode 100644 index 0000000000..32097115e7 --- /dev/null +++ b/group06/736464448/data_structure/MyLinkedList.java @@ -0,0 +1,209 @@ +package data_structure; + + +import java.util.Iterator; +import java.util.NoSuchElementException; + + + +public class MyLinkedList { + private int size; + private Node head; + private Node last; + + public void add(Object o){ + + linkLast(o); + + } + + public Object get(int index){ + checkPositionIndex(index); + Node node=node(index); + return node.item; + } + public Object remove(int index){ + checkPositionIndex(index); + Node node=node(index); + isnull(node); + Object o=null; + Node before=null; + if(index==0){ + o=node.item; + node.next=head; + node=null; + } + else + { + before=node(index-1); + before.next=node.next; + o=node.item; + node=null; + + } + + return o; + } + public int size(){ + + return size; + } + public void addFirst(Object o){ + + linkFirst(o); + + + } + public void addLast(Object o){ + linkLast(o); + + } + public Object removeFirst(){ + Node f=head; + isnull(f); + final Node next=head.next; + Object o=f.item; + f=null; + head=next; + if(next==null) + last=null; + size--; + + return o; + } + //这个方法用多了也爆炸 + public Object removeLast(){ + Node l=last; + isnull(l); + Object o=null; + if(size>=2){ + final Node before=node(size-1); + o=l.item; + l=null; + last=before; + + if(before==null) + head=null; + } + else{ + o=l.item; + l=null; + last=null; + head=null; + } + + + size--; + return o; + + } + public Iterator iterator(){ + + + return new ListItr(); + } + + + private static class Node{ + Object item; + Node next; + Node(Object o, Node next){ + this.item=o; + this.next=next; + } + } + public void add(int index, Object o) { + checkPositionIndex(index); + + if (index == 0) + linkFirst(o); + else + linkNext(o, node(index-1)); + } + private void linkNext(Object o,Node node){ + final Node next=node.next; + final Node newnode=new Node(o,next); + node.next=newnode; + size++; + } + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + private void linkLast(Object o){ + final Node l=last; + final Node node=new Node(o,null); + last=node; + if(head==null) + head=node; + else + l.next=node; + size++; + } + private void linkFirst(Object o){ + final Node f=head; + final Node node=new Node(o,null); + head=node; + if(last==null) + last=node; + else + head.next=f; + size++; + } + private void isnull(Node node){ + if (node == null) + throw new NoSuchElementException(); + } + Node node(int index) { + // assert isElementIndex(index); + + + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + + + } + + private class ListItr implements Iterator { + private Node next=head; + private int nextIndex; + private Node lastReturned = null; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + //加上死循环了 +// if(nextIndex==size){ +// next=head; +// nextIndex=0; +// } + return lastReturned.item; + } + + @Override + public void remove() { + // TODO Auto-generated method stub + + } + } + + + +} diff --git a/group06/736464448/data_structure/MyQueue.java b/group06/736464448/data_structure/MyQueue.java new file mode 100644 index 0000000000..8e3f50e0bb --- /dev/null +++ b/group06/736464448/data_structure/MyQueue.java @@ -0,0 +1,21 @@ +package data_structure; + +public class MyQueue { + private MyLinkedList elementData =new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/736464448/data_structure/MyStack.java b/group06/736464448/data_structure/MyStack.java new file mode 100644 index 0000000000..f442468fb2 --- /dev/null +++ b/group06/736464448/data_structure/MyStack.java @@ -0,0 +1,22 @@ +package data_structure; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/736464448/test_data_structure/TestMyArrayList.java b/group06/736464448/test_data_structure/TestMyArrayList.java new file mode 100644 index 0000000000..6ce24c90a8 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyArrayList.java @@ -0,0 +1,93 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyArrayList; + +public class TestMyArrayList { + MyArrayList list; + + @Before + public void setUp() throws Exception { + list=new MyArrayList(); + System.out.println("begin"); + } + + @After + public void tearDown() throws Exception { + System.out.println("end"); + } + + @Test + public void testMyArrayList() { + + Assert.assertEquals(0, list.Capacity()); + } + + @Test + public void testAddObject() { + list.add(new Integer(10)); + Assert.assertEquals(10, list.get(0)); + } + + @Test + public void testAddIntObject() { + list.add(1); + list.add(1); + list.add(1); + list.add(1,2); + Assert.assertEquals(2, list.get(1)); + + + } + + @Test + public void testGet() { + list.add(1); + list.add(2); + Assert.assertEquals(2, list.get(1)); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(2, list.remove(1)); + + } + + @Test + public void testSize() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testCapacity() { + list=new MyArrayList(5); + Assert.assertEquals(5, list.Capacity()); + + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + Iterator itr=list.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + +} diff --git a/group06/736464448/test_data_structure/TestMyBinaryTree.java b/group06/736464448/test_data_structure/TestMyBinaryTree.java new file mode 100644 index 0000000000..f56696d4f7 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyBinaryTree.java @@ -0,0 +1,59 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyBinaryTree; + + +public class TestMyBinaryTree { + MyBinaryTree bt; + + @Before + public void setUp() throws Exception { + System.out.println("开始测试"); + bt=new MyBinaryTree(); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testAdd() { + bt.add(1, "c"); + + } + + @Test + public void testGet() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.get(2)); + } + + @Test + public void testRemove() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.remove(2)); + + Assert.assertEquals(2, bt.size()); + } + + @Test + public void testSize() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(3, bt.size()); + } + +} diff --git a/group06/736464448/test_data_structure/TestMyLinkedList.java b/group06/736464448/test_data_structure/TestMyLinkedList.java new file mode 100644 index 0000000000..9280b0f65c --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyLinkedList.java @@ -0,0 +1,111 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyLinkedList; + +public class TestMyLinkedList { + MyLinkedList link=null; + + @Before + public void setUp() throws Exception { + link=new MyLinkedList(); + System.out.println("测试开始"); + } + + @After + public void tearDown() throws Exception { + System.out.println("测试结束"); + } + + @Test + public void testAddObject() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testGet() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testRemove() { + link.add(1); + Assert.assertEquals(1,link.remove(0)); + } + + @Test + public void testSize() { + link.add(1); + Assert.assertEquals(1,link.size()); + } + + @Test + public void testAddFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.get(0)); + } + + @Test + public void testAddLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.get(link.size()-1)); + } + + @Test + public void testRemoveFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.removeFirst()); + } + + @Test + public void testRemoveLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.removeLast()); + } + + @Test + public void testIterator() { + link.add(1); + link.add(2); + link.add(3); + Iterator itr=link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + + @Test + public void testAddIntObject() { + link.add(1); + link.add(1); + link.add(1); + link.add(2, 3); + Assert.assertEquals(3,link.get(2)); + } + + + +} diff --git a/group06/736464448/test_data_structure/TestMyQueue.java b/group06/736464448/test_data_structure/TestMyQueue.java new file mode 100644 index 0000000000..dd0e96e699 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyQueue.java @@ -0,0 +1,49 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyQueue; + +public class TestMyQueue { + MyQueue queue; + + @Before + public void setUp() throws Exception { + queue =new MyQueue(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testEnQueue() { + queue.enQueue("hello"); + } + + @Test + public void testDeQueue() { + queue.enQueue("hello"); + queue.enQueue("world"); + Assert.assertEquals("hello",queue.deQueue()); + } + + @Test + public void testIsEmpty() { + queue.enQueue("hello"); + Assert.assertEquals(false,queue.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0,queue.size()); + } + +} diff --git a/group06/736464448/test_data_structure/TestMyStack.java b/group06/736464448/test_data_structure/TestMyStack.java new file mode 100644 index 0000000000..7ddaba1e62 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyStack.java @@ -0,0 +1,60 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyStack; + +public class TestMyStack { + MyStack mystack; + + @Before + public void setUp() throws Exception { + mystack=new MyStack(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testPush() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + } + + @Test + public void testPop() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + Assert.assertEquals("World", (String)mystack.pop()); + Assert.assertEquals(",", (String)mystack.pop()); + + } + + @Test + public void testPeek() { + mystack.push("Hello"); + Assert.assertEquals("Hello", (String)mystack.peek()); + } + + @Test + public void testIsEmpty() { + + Assert.assertEquals(true, mystack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, mystack.size()); + } + +} diff --git a/group06/799237637/src/com/firsthomework/MyArrayList.java b/group06/799237637/src/com/firsthomework/MyArrayList.java new file mode 100644 index 0000000000..cf1a3e04a2 --- /dev/null +++ b/group06/799237637/src/com/firsthomework/MyArrayList.java @@ -0,0 +1,107 @@ +/* + * �ó���Ϊ�Լ�ʵ��ArrayList + * ArrayList�ײ�Ϊ����ʵ�֣��Ƕ�̬���� + */ +package com.firsthomework; + + +public class MyArrayList { + private int size=0; + private int initialcapcity=10; + private Object[] elements= null; + public MyArrayList(){ + this.elements=new Object[initialcapcity]; + } + + public MyArrayList(int capcity){ + + this.elements = new Object[capcity]; + + } + public void enlarge(){ + initialcapcity*=2; + Object[] tmpelements= new Object[initialcapcity]; + System.arraycopy(elements, 0, tmpelements, 0,size); + elements=tmpelements; + } + public void add(Object o){ + if(o==null){ + throw new RuntimeException("����Ϊ�գ�"); + } + if(size>initialcapcity){ + enlarge(); + } + elements[size]=o; + size++; + + } + public Object get(int index){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("����Խ�磺"); + } + return elements[index]; + + } +//ʵ��remove()������ɾ��λ�ã������Ԫ��Ҫǰ�ƣ�����ɾ��λ�õ�Ԫ�� + public Object remove(int index){ + Object oldValue=elements[index]; + int eleMoved=size-index-1; //�ƶ���Ԫ�صĸ��� + if(eleMoved>0){ + System.arraycopy(elements, //ԭ���� + index+1, //ԭ�������ʼλ�ã�ɾ��Ԫ�صĺ���һ��λ�ã� + elements, //Ŀ������ + index, //Ŀ���������ʼλ�� + eleMoved);//�����ij��� + } + elements[size-1]=null; + size--; + + return oldValue; + } + + public boolean set(int index,Object o){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("����Խ��"); + } + elements[index]=o; + return true; + } + + //��дtoString()���� + public String toString(){ + StringBuffer sb=new StringBuffer(); + sb.append("["); + for(int i=0;i + + + + + + diff --git a/group06/949319266/.project b/group06/949319266/.project new file mode 100644 index 0000000000..d3a5c7c636 --- /dev/null +++ b/group06/949319266/.project @@ -0,0 +1,17 @@ + + + algorithm + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/949319266/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..500f6b26f4 --- /dev/null +++ b/group06/949319266/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Fri Feb 24 09:38:47 CST 2017 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/group06/949319266/949319266learn/.classpath b/group06/949319266/949319266learn/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group06/949319266/949319266learn/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group06/949319266/src/com/coding/basic/ArrayList.java b/group06/949319266/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9d2dca03c2 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/ArrayList.java @@ -0,0 +1,93 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class ArrayList implements List { + + private int size; + private int current ; + private int point; + private Object[] elementData = null; + private static final int DEFAULT_SIZE=100; + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public ArrayList(int size) { + if(size < 0) { + System.out.println("��С����С��0"); + } else { + this.elementData = new Object[size]; + this.current = 0; + point = size; + } + } + + public void add(E e){ + judgeSize(); + this.elementData[current] = e; + this.current++; + } + public void add(int index, E e){ + judgeIndex(index); + for(int i=0 ; i= index && i+2 < elementData.length) { + elementData[i] = e; + elementData[i+1] = elementData[i+2]; + } + } + current++; + } + + public E get(int index){ + judgeIndex(index); + return (E)this.elementData[index]; + } + + public void remove(int index) { + judgeIndex(index); + for(int i=0;i0) { + return true; + } + return false; + } + public void clear() { + elementData = new Object[DEFAULT_SIZE]; + } + private void judgeSize() { + Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; + System.arraycopy(elementData, 0, newarr, 0, elementData.length); + this.elementData = newarr; + } + public void judgeIndex(int index) { + if(index < 0 || index > point) { + System.out.println("�±�Խ��"); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/BinaryTreeNode.java b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..941c31bc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode getRoot() { + return root; + } + + public Object getData() { + return data; + } + public void setData(int data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public void insert(int value){ + BinaryTreeNode newNode = new BinaryTreeNode(); + if(root == null) { + root=newNode; + root.left = null; + root.right = null; + } else { + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true) { + parentNode = currentNode; + if(newNode.data > currentNode.data) { + currentNode = currentNode.getRight(); + if(currentNode == null) { + parentNode.setRight(newNode); + return; + } + } else { + currentNode = currentNode.getLeft(); + if(currentNode == null) { + parentNode.setLeft(newNode); + return; + } + } + } + + } + } + public boolean find(int key) { + BinaryTreeNode cNode = root; + if(cNode != null) { + while(cNode.data != key) { + if(cNode.data > key) { + cNode = cNode.getLeft(); + } else { + cNode = cNode.getRight(); + } + return true; + } + return true; + } + return false; + } + //������� + public void inOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + inOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + inOrder(treeNode.getRight()); + } + } + //������� + public void leftOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + leftOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + leftOrder(treeNode.getRight()); + } + } + //������� + public void rightOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + rightOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + rightOrder(treeNode.getRight()); + } + } +} diff --git a/group06/949319266/src/com/coding/basic/LinkedList.java b/group06/949319266/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ee95a118e0 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/LinkedList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class LinkedList implements List { + + private Node last; + private Node head; + private int xsize = 0; + + public LinkedList() { + init(); + } + + private void init() { + head = new Node(null,null,null); + last = new Node(null,head,null); + head.next = last; + xsize = 0; + } + + //���������һ�����������ڲ�ʹ��;��index֮ǰ����һ���ڵ� + private void add(Node node,int index) { + if(index < 0 || index > xsize) { + throw new IndexOutOfBoundsException("�±�Խ��"); + } + node.pre = getNode(index-1); + getNode(index-1).next = node; + node.next = getNode(index); + getNode(index).pre = node; + xsize++; + } + public void add(E e){ + add(new Node(e,null,null),xsize); + } + + public void add(int index,E e){ + add(new Node(e,null,null),index); + } + private Node getNode(int index){ + Node newNode; + int current; + if(index == -1) { + return head; + } else if (index == xsize ){ + return last; + } else { + current = 0; + newNode = head.next; + while (current < index) { + newNode = newNode.next; + current++; + } + } + return newNode; + } + + public E get(int index) { + return getNode(index).e; + } + + public void remove(int index){ + Node node = getNode(index); + node.pre.next = node.next; + node.next.pre=node.pre; + xsize--; + } + + public int size(){ + return xsize; + } + + public void addFirst(E e){ + add(new Node(e,null,null),0); + } + public void addLast(E e){ + add(new Node(e,null,null),xsize); + } + public void removeFirst(){ + remove(0); + } + public void removeLast(){ + remove(xsize); + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + public E e; + Node next; + Node pre; + public Node (E e,Node pre,Node next) { + this.pre = pre; + this.next = next; + this.e = e; + } + + } + + + public boolean contains(Node node) { + Node mnode = head.next; + while(mnode !=null) { + if(mnode.equals(node)) { + return true; + } + mnode = mnode.next; + } + return false; + } + public boolean isEmpty() { + return xsize == 0 ? true:false; + } + public void clear() { + init(); + } + + public boolean contains(E e) { + // TODO Auto-generated method stub + Node node = null; + while((node=(head.next)) != null) { + if(e.equals(node)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + +} diff --git a/group06/949319266/src/com/coding/basic/List.java b/group06/949319266/src/com/coding/basic/List.java new file mode 100644 index 0000000000..970c0cfc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + + +public interface List { + public void add(E e); + public void add(int index, E e); + //public E get(int index); + public void remove(int index); + public boolean contains(E e); + public int size(); + public boolean isEmpty(); + public void clear(); +} diff --git a/group06/949319266/src/com/coding/basic/Queue.java b/group06/949319266/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b090a3626e --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Queue.java @@ -0,0 +1,52 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Queue { + E[] element; + //Ϊ�˲��Է�����ʱ����������Ϊ5 + private static final int DEFAULT_SIZE=5; + int front,rear;//�ֱ�Ϊ���׺Ͷ�β�±� + + public Queue() { + this(DEFAULT_SIZE); + } + public Queue(int size) { + element = (E[])(new Object[size]); + front = 0; + rear = 0; + } + + public void enQueue(E e){ + if(((rear+1)%element.length) == front) { + System.out.println("���������޷����"); + } else { + element[rear] = e; + rear=(rear+1)%element.length; + } + } + + public E deQueue(){ + if(rear == front) { + return null; + } else { + E e = element[front]; + front = (front + 1)%element.length; + return e; + } + + } + + public boolean isEmpty(){ + return rear == front; + } + + public int size(){ + if(rear > front){ + return rear-front; + } else { + return (element.length+1)-(front-rear); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/Stack.java b/group06/949319266/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2a309fde95 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Stack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Stack { + private ArrayList element; + int top; + public Stack() { + element = new ArrayList(); + top = 0; + } + + public void push(Object o){ + element.add(o); + top++; + } + + public void pop(){ + if(isEmpty()) { + System.out.println("ջ�ǿյ�"); + System.exit(0); + } + element.remove(top-1); + top--; + } + + public Object peek(){ + return element.get(top-1); + } + public boolean isEmpty(){ + return top == 0?true:false; + } + public int size(){ + return top; + } +} diff --git a/group06/949319266/src/com/coding/basic/test/ArrayListTest.java b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..5abd16725f --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test() { + ArrayList list = new ArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + System.out.println("�±�Ϊ3��Ԫ��Ϊ��"+list.get(3)); + System.out.println("���鳤��"+list.size()); + list.remove(2); + System.out.println("remove������鳤��"+list.size()); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + list.add(3, "g"); + System.out.println(""); + System.out.println("����������Ϊ��"); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/LinkedListTest.java b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..2b4c390721 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java @@ -0,0 +1,24 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Test; + +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + @Test + public void test() { + LinkedList list = new LinkedList(); + list.add("First"); + list.add("Second"); + list.add("Thrid"); + for(int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+ " "); + } + + } +} diff --git a/group06/949319266/src/com/coding/basic/test/QueueTest.java b/group06/949319266/src/com/coding/basic/test/QueueTest.java new file mode 100644 index 0000000000..6ec8036c35 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/QueueTest.java @@ -0,0 +1,28 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + @Test + public void test() { + Queue qu = new Queue(); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.enQueue("jie"); + System.out.println(qu.size()); + qu.deQueue(); + System.out.println(qu.size()); + System.out.println(qu.isEmpty()); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.deQueue(); + qu.enQueue("jie"); + System.out.println(qu.size()); + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/StackTest.java b/group06/949319266/src/com/coding/basic/test/StackTest.java new file mode 100644 index 0000000000..d76742e8ff --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/StackTest.java @@ -0,0 +1,26 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + @Test + public void test() { + Stack s = new Stack(); + s.push("gong"); + s.push("bo"); + s.push("jie"); + s.push("hao"); + s.push("ren"); + System.out.println(s.size()); + System.out.println(s.peek()); + s.pop(); + System.out.println(s.size()); + System.out.println(s.peek()); + } + +} diff --git "a/group06/949319266/\346\226\207\347\253\240.txt" "b/group06/949319266/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..f00eb98185 --- /dev/null +++ "b/group06/949319266/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x31y.html \ No newline at end of file diff --git a/group06/group06.md b/group06/group06.md index d3f5a12faa..8b13789179 100644 --- a/group06/group06.md +++ b/group06/group06.md @@ -1 +1 @@ - + diff --git "a/group07/1058267830/git\345\221\275\344\273\244.txt" "b/group07/1058267830/git\345\221\275\344\273\244.txt" deleted file mode 100644 index 8bcf2ffa0f..0000000000 --- "a/group07/1058267830/git\345\221\275\344\273\244.txt" +++ /dev/null @@ -1,11 +0,0 @@ -��װgit gui��������git bash���� -1.��¡ -git clone git@github.com:leijing1992/coding2017.git -2.�����޸ĵ��ļ� -git add -A -3.�ύ���ݴ��� -git commit -m "�ύ***����" -4.����master -git pull origin master -5.�ύ��master -git push origin master \ No newline at end of file diff --git a/group07/1058267830/week1/.classpath b/group07/1058267830/week1/.classpath new file mode 100644 index 0000000000..91c6d8f6c5 --- /dev/null +++ b/group07/1058267830/week1/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/1058267830/week1/.project b/group07/1058267830/week1/.project new file mode 100644 index 0000000000..d3e8bd8c59 --- /dev/null +++ b/group07/1058267830/week1/.project @@ -0,0 +1,17 @@ + + + week1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs b/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group07/1058267830/week1/src/com/coding/basic/ArrayList.java b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8d05511d8a --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private Object[] obj ; + private int length; // 数组总长度 + private int size; // 元素个数 + private int currentIndex; // 当前索引位置,默认为-1 + + public int getLength() { + return length; + } + + public ArrayList(){ + this.obj = new Object[10]; + this.length = 10; + this.size = 0; + this.currentIndex = -1; + } + + public ArrayList(int initSize){ + this.obj = new Object[initSize]; + this.length = initSize; + this.size = 0; + this.currentIndex = -1; + } + + @Override + public void add(Object o) { + if(this.size < length){ + obj[size] = o; + this.size++; + + }else{ + // 扩容,add数据 + Object[] obj1 = new Object[length * 2]; + System.arraycopy(obj, 0, obj1, 0, length); + this.length = this.length * 2; + this.obj = obj1; + obj[this.size] = o; + this.size++; + } + } + + @Override + public void add(int index, Object o) { + if(index < length){ + // 容量扩1,add数据 + Object[] obj1 = new Object[length + 1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index, obj1, index+1, length-index); + obj1[index] = o; + this.obj = obj1; + this.length++; + this.size++; + }else{ + // 容量扩到index+1, add数据 + Object[] obj1 = new Object[index + 1]; + System.arraycopy(obj, 0, obj1, 0, length); + obj1[index] = o; + this.obj = obj1; + this.length = index + 1; + this.size++; + } + } + + @Override + public Object get(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + return this.obj[index]; + } + + @Override + public Object remove(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + Object tmp = obj[index];// 取值,最后返回 + Object[] obj1 = new Object[length -1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index+1, obj1, index, length-index-1); + this.obj = obj1; + this.length--; + this.size--; + return tmp; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean hasNext() { + if(currentIndex == length-1){ + return false; + }else{ + currentIndex++; + return true; + } + } + + @Override + public Object next() { + return this.get(currentIndex); + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..e326a21f75 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; // 根节点 + public BinaryTree( ){ + BinaryTreeNode node = new BinaryTreeNode(); + this.root = node; + } + + public boolean isEmpty(){ + return this.root == null; + } + + public void insert(Object o){ + // 如果是第一次添加节点,就是root节点 + if(root.data == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + root = bnode; + }else{ + insert(o, root); + } + } + + // 递归添加非root节点 + private BinaryTreeNode insert(Object o, BinaryTreeNode node) { + if(node == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + return bnode; + } + if((int)o <= (int)node.data){ + node.left = insert(o, node.left); + }else{ + node.right = insert(o, node.right); + } + + return node; + } + + // 中序遍历 + public void middlePrint(){ + middleOrder(this.root); + } + + private void middleOrder(BinaryTreeNode node) { + if(node != null){ + middleOrder(node.left); + System.out.print(node.data + " "); + middleOrder(node.right); + } + } + + // 前序遍历 + public void prePrint(){ + preOrder(this.root); + } + + private void preOrder(BinaryTreeNode node) { + if(node != null){ + System.out.print(node.data + " "); + preOrder(node.left); + preOrder(node.right); + } + } + + // 后序遍历 + public void postPrint(){ + postOrder(this.root); + } + + private void postOrder(BinaryTreeNode node) { + if(node != null){ + postOrder(node.right); + System.out.print(node.data + " "); + postOrder(node.left); + } + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a9e6593160 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + protected Object data; + protected BinaryTreeNode left; + protected BinaryTreeNode right; + + public BinaryTreeNode(){} + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/Iterator.java b/group07/1058267830/week1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d369bbc50c --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); +} diff --git a/group07/1058267830/week1/src/com/coding/basic/LinkedList.java b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..df65b13601 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterator{ + private Node head; + private Node tail; + private Node currentNode; + private int size; + + public LinkedList(){ + this.head = new Node(null); + this.tail = head; + this.currentNode = head; + this.size = 0; + } + + @Override + public void add(Object o) { + Node node = new Node(o, null); + tail.setNext(node); + tail = node; + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0 || index > size+1){ + throw new RuntimeException("插入的位置错误..."); + } + Node pre = head; // 得到待插入位置的前一个节点 + for(int i=0; i= size){ + throw new RuntimeException("index参数错误..."); + } + Node node = head; // 得到待插入位置的前一个节点 + for(int i=0; i<=index; i++){ + node = node.getNext(); + } + return node; + } + + @Override + public Object remove(int index) { + if(index < 0 || index >= size){ + throw new RuntimeException("index参数错误..."); + } + Node pre = head; // 得到待删除位置的前一个节点 + for(int i=0; i + + + + + diff --git a/group07/1280157271/20170224-01-ArrayList/.gitignore b/group07/1280157271/20170224-01-ArrayList/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1280157271/20170224-01-ArrayList/.project b/group07/1280157271/20170224-01-ArrayList/.project new file mode 100644 index 0000000000..4c0107dd15 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.project @@ -0,0 +1,17 @@ + + + 20170224-01-ArrayList + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs b/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java new file mode 100644 index 0000000000..6eb3636be5 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java @@ -0,0 +1,9 @@ +package firstHomework.fan; + +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/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java new file mode 100644 index 0000000000..bd39bfca4e --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java @@ -0,0 +1,60 @@ +package firstHomework.fan; + +public class myArrayList implements List { + + private int size = 0; + private int initLength=10; + private Object[] elementData = new Object[initLength]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + ensureCapacity(size+1); + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + }else{ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + } + + public Object get(int index){ + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + }else{ + return elementData[index]; + } + return null; + } + + public Object remove(int index){ + if(index<0||index>size){ + System.out.println("indexӦ����0-size֮�䣡����"); + return null; + } + Object obj = get(index); + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return obj; + } + + public int size(){ + return this.size; + } + + + + public void ensureCapacity(int x){ + int oldCapacity = elementData.length; + if(x>oldCapacity){ + Object newEle[] = new Object[oldCapacity+initLength]; + System.arraycopy(elementData, 0, newEle, 0, size); + elementData = newEle; + } + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java new file mode 100644 index 0000000000..f523215a87 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java @@ -0,0 +1,68 @@ +package firstHomework.fan; +import java.util.Comparator; + +public class myBinaryTreeNode { + + private int data; + private myBinaryTreeNode left; + private myBinaryTreeNode right; + + //���캯�� + public myBinaryTreeNode(){ + } + public myBinaryTreeNode(int value){ + this.data = value; + this.left = null; + this.right = null; + } + public myBinaryTreeNode(int value,myBinaryTreeNode left,myBinaryTreeNode right){ + this.data = value; + this.left = left; + this.right = right; + } + private myBinaryTreeNode root;//������ڵ� + + //get/set���� + public Object getData() { + return data; + } + public void setData(int data) { + this.data = data; + } + + + public myBinaryTreeNode getLeft() { + return left; + } + public void setLeft(myBinaryTreeNode left) { + this.left = left; + } + + + public myBinaryTreeNode getRight() { + return right; + } + public void setRight(myBinaryTreeNode right) { + this.right = right; + } + + + public void insert(int o){ + this.root = insert(o,this.root); + + } + public myBinaryTreeNode insert(int value,myBinaryTreeNode t){ + if(t == null){//���ڵ�Ϊ�� + return new myBinaryTreeNode(value); + } + //����ֵ�뵱ǰ�ڵ�Ƚϣ�С�ڲ��뵽�����������ڲ��뵽������ + if(valuet.data){ + t.right = insert(value,t.right); + } + return t; + } + +} \ No newline at end of file diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java new file mode 100644 index 0000000000..da481e0232 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java @@ -0,0 +1,129 @@ +package firstHomework.fan; + + + +public class myLinkedList implements List { + private static class Node{//�����Ľڵ�ṹ�� + Object data;//������ + Node next; //Node���ã��൱��ָ�룬ָ����һ���ڵ� + + Node(Object e, Node next) { + this.data = e; + this.next = next; + } + } + + private Node head,last=null;//�ֱ�ָ���һ�������һ���ڵ� + private int size; + + + public void add(Object o){//��β�����ӣ����൱������β�ڵ� + creatLastNode(o); + } + public void add(int index , Object o){//��indexǰ����� + if(index == 0){//˵����������λ��Ϊ0����ô������ͷ��� + createFirstNode(o); + }else{//��ȥ�ҵ�ָ��λ�� + Node indexBeforeNode = getNode(index-1);//���ﷵ�ص���index��ǰһ���ڵ� + Node newIndex =new Node(o,indexBeforeNode.next) ;//x�½ڵ㱣��indexBefore��ָ�� + indexBeforeNode.next = newIndex; + size++; + } + } + public Object get(int index){ + return getNode(index).data;//���ص��ǽڵ��е����ݶ��� + } + + public Object remove(int index){ + if(index==0){//�Ƴ�ͷ��� + removeFirst(); + }else{//�ҵ�ָ���ڵ��ǰһ���ڵ� + Node removeNode = getNode(index-1); + removeNode.next = removeNode.next.next;//�Ƴ���index + size--; + return removeNode.next.data;//�����Ƴ��ڵ�����ݶ��� + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + createFirstNode(o); + } + public void addLast(Object o){ + creatLastNode(o); + } + public Object removeFirst(){ + if(size>0){//�б���Ϊ�գ���һ����ͷ + Node removeHead = head; + head = head.next; + size--; + return removeHead.data;//����ͷ�������ݶ��� + }else{ + System.out.println("����Ϊ�գ�"); + } + return null; + } + public Object removeLast(){ + if(size>0){ + Node removeLastBefore = getNode(size-2);//�ҵ�last�ڵ����һ���ڵ� + Object returnObj = removeLastBefore.next.data; + removeLastBefore.next = null; + last = removeLastBefore; + size--; + return returnObj; + }else{ + System.out.println("����Ϊ�գ�"); + } + return null; + } + + /* + * ����ͷ��� + * */ + private void createFirstNode(Object e){ + Node oldHead = head; + Node newHead = new Node(e,oldHead);//�������Ľڵ���Ϊhead�ڵ��ǰһ�ڵ� + head = newHead;//���ܿղ��գ�headҪָ���µ�ͷ�ڵ� + if(head == null){//�������Ϊ�գ�head��last��ָ���½ڵ㣨��Ϊ��last�Ͳ����Ҹ�ֵ����Ϊ��ȷ������������ģ� + last = newHead; + }else{//ͷ����Ѿ�����,�½ڵ���Ϊͷ��㣬ԭhead��ڶ���last����ָ������last + newHead.next = head; + } + size++; + } + /* + * ����β��� + * */ + private void creatLastNode(Object e){ + Node oldLast = last; + Node newLast = new Node(e,null);//�µ�β�ڵ���һ���ڵ�Ϊ�� + last = newLast;//���ܿղ��գ�last��Ҫָ���µ�β�ڵ� + if(head == null){//����Ϊ�� + head = newLast; + }else{ + oldLast.next = newLast; + } + size++; + } + /* + * Ѱ��ָ����� + * */ + private Node getNode(int index){ + if(index<0||index>=size){ + System.out.println("indexԽ�磡����"); + }else{ + Node node=head; + while(index != 0){ + node = node.next; + index--; + } + return node; + } + return null; + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java new file mode 100644 index 0000000000..041da7e29b --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java @@ -0,0 +1,51 @@ +package firstHomework.fan; + +public class myQueue { + private int iniLength = 10; + private Object[] array = new Object[iniLength]; + private int size = 0; + + public void enQueue(Object o){ + if(size>=array.length){//��Ҫ���� + Object[] newArray = new Object[iniLength+array.length];//array����ԭ���ټ���10 + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size++] = o; + } + + public Object deQueue(){//�Ƴ���һ��Ԫ�أ������Ԫ��������ǰλ�� + Object deQueue = array[0]; + System.arraycopy(array, 1, array, 0, size-1); + size--; + return deQueue; + + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return this.size; + } + public static void main(String[] args) { + myQueue queue = new myQueue(); + queue.enQueue("A"); + queue.enQueue("B"); + queue.enQueue("C"); + + queue.enQueue("D"); + queue.enQueue("E"); + queue.enQueue("F"); + queue.enQueue("G"); + + while(!queue.isEmpty()){ + System.out.println(queue.deQueue());//���� + } + + } + + + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java new file mode 100644 index 0000000000..623bbe787f --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java @@ -0,0 +1,31 @@ +package firstHomework.fan; + + + +public class myStack { + private myArrayList array = new myArrayList();//myArrayList�������и���̬���� + + public void push(Object o){ //��ջ + //���Ȳ���ʱmyArrayList���Լ����� + array.add(o);//�¶������������� + } + + public Object pop(){//��ջ��������β�ϵ�Ԫ�أ� ��Ҫɾ���� + Object pop = array.get(array.size()-1);//�±�Ҫ��sizeС1 + array.remove(array.size()-1); + return pop; + } + + public Object peek(){//ֻ�ǵ���ջ����ֵ����ɾ�� + return array.get(array.size()-1); + } + public boolean isEmpty(){ + return array.size()==0; + } + public int size(){ + return array.size(); + } + + +} + diff --git "a/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" "b/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" new file mode 100644 index 0000000000..1e19f6adda --- /dev/null +++ "b/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" @@ -0,0 +1 @@ +http://blog.csdn.net/stromcloud/article/details/56678442 \ No newline at end of file diff --git a/group07/1448276993/JavaStudy/.classpath b/group07/1448276993/JavaStudy/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group07/1448276993/JavaStudy/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1448276993/JavaStudy/.gitignore b/group07/1448276993/JavaStudy/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1448276993/JavaStudy/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1448276993/JavaStudy/.project b/group07/1448276993/JavaStudy/.project new file mode 100644 index 0000000000..cab7f7f249 --- /dev/null +++ b/group07/1448276993/JavaStudy/.project @@ -0,0 +1,17 @@ + + + JavaStudy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1448276993/JavaStudy/.settings/org.eclipse.jdt.core.prefs b/group07/1448276993/JavaStudy/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/1448276993/JavaStudy/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group07/1448276993/JavaStudy/src/java1/ArrayList.java b/group07/1448276993/JavaStudy/src/java1/ArrayList.java new file mode 100644 index 0000000000..8623c3ae75 --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/ArrayList.java @@ -0,0 +1,57 @@ +package java1; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++]=o; + + } + public void add(int index, Object o){ + ensureCapacity(size+1); + if(index<0&&index>size){ + System.out.println("Wrong Input��"); + }else{ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index]=o; + size++; + } + + } + + public Object get(int index){ + if(index<0&&index>size){ + System.out.println("Wrong Input��"); + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0&&index>size){ + System.out.println("Wrong Input��"); + return null; + } + Object a=elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + return a; + } + + public int size(){ + return this.size; + } + + public void ensureCapacity(int size){ + int oldCapacity=elementData.length; + if(size>oldCapacity){ + Object[] newelementData=new Object[size+oldCapacity]; + elementData=newelementData; + } + } + +} + diff --git a/group07/1448276993/JavaStudy/src/java1/LinkedList.java b/group07/1448276993/JavaStudy/src/java1/LinkedList.java new file mode 100644 index 0000000000..45df3db8bc --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/LinkedList.java @@ -0,0 +1,129 @@ +package java1; + +public class LinkedList implements List { + + private Node head=null; + private Node last=null; + private int size=0; + + public void add(Object o){ + Node newNode = new Node(o,null); + if(head==null){ + head = newNode; + last = newNode; + }else{ + addLast(o); + } + size++; + } + public void add(int index , Object o){ + if(index<0&&index>size+1){ + System.out.println("Wrong Input!"); + } + Node newNode = new Node(o,null); + if(index==0){ + addFirst(o); + }else if(index==size){ + addLast(o); + }else{ + Node a=head; + for(int i=0;ioldCapacity){ + Object[] newelementData=new Object[size+oldCapacity]; + str=newelementData; + } + } +} diff --git a/group07/1448276993/JavaStudy/src/java1/Stack.java b/group07/1448276993/JavaStudy/src/java1/Stack.java new file mode 100644 index 0000000000..9b785e48d9 --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/Stack.java @@ -0,0 +1,34 @@ +package java1; + +public class Stack { + private ArrayList array = new ArrayList(); + + public void push(Object o){ + array.add(o); + } + + public Object pop(){ + if(array.size()>0){ + Object pop = array.get(array.size()-1); + array.remove(array.size()-1); + return pop; + } + return null; + } + + public Object peek(){ + if(array.size()>0){ + return array.get(array.size()-1); + } + return null; + } + public boolean isEmpty(){ + if(array.size()==0){ + return true; + } + return false; + } + public int size(){ + return array.size(); + } +} diff --git a/group07/1519504320/.classpath b/group07/1519504320/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group07/1519504320/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1519504320/.gitignore b/group07/1519504320/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1519504320/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1519504320/.idea/.name b/group07/1519504320/.idea/.name new file mode 100644 index 0000000000..5ad528003b --- /dev/null +++ b/group07/1519504320/.idea/.name @@ -0,0 +1 @@ +1519504320Learning \ No newline at end of file diff --git a/group07/1519504320/.idea/compiler.xml b/group07/1519504320/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/group07/1519504320/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/copyright/profiles_settings.xml b/group07/1519504320/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group07/1519504320/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/misc.xml b/group07/1519504320/.idea/misc.xml new file mode 100644 index 0000000000..6a48f33378 --- /dev/null +++ b/group07/1519504320/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/modules.xml b/group07/1519504320/.idea/modules.xml new file mode 100644 index 0000000000..c8fa84b43f --- /dev/null +++ b/group07/1519504320/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/workspace.xml b/group07/1519504320/.idea/workspace.xml new file mode 100644 index 0000000000..7dbdbd8302 --- /dev/null +++ b/group07/1519504320/.idea/workspace.xml @@ -0,0 +1,746 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488014668818 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.project b/group07/1519504320/.project new file mode 100644 index 0000000000..d6fb07be83 --- /dev/null +++ b/group07/1519504320/.project @@ -0,0 +1,17 @@ + + + 1519504320Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1519504320/1519504320Learning.iml b/group07/1519504320/1519504320Learning.iml new file mode 100644 index 0000000000..5034ed029c --- /dev/null +++ b/group07/1519504320/1519504320Learning.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/compiler.xml b/group07/1519504320/src/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/group07/1519504320/src/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/copyright/profiles_settings.xml b/group07/1519504320/src/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group07/1519504320/src/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/misc.xml b/group07/1519504320/src/.idea/misc.xml new file mode 100644 index 0000000000..0dca27c4c9 --- /dev/null +++ b/group07/1519504320/src/.idea/misc.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/modules.xml b/group07/1519504320/src/.idea/modules.xml new file mode 100644 index 0000000000..f669a0e594 --- /dev/null +++ b/group07/1519504320/src/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/src.iml b/group07/1519504320/src/.idea/src.iml new file mode 100644 index 0000000000..d6ebd48059 --- /dev/null +++ b/group07/1519504320/src/.idea/src.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/vcs.xml b/group07/1519504320/src/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group07/1519504320/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/workspace.xml b/group07/1519504320/src/.idea/workspace.xml new file mode 100644 index 0000000000..030203141a --- /dev/null +++ b/group07/1519504320/src/.idea/workspace.xml @@ -0,0 +1,759 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Android Lint + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488003825215 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.7 + + + + + + + + src + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/com/coding/basic/ArrayList.java b/group07/1519504320/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..44935deaa3 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/ArrayList.java @@ -0,0 +1,96 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (elementData.length == 101 && elementData[100] == null) { + for (int i = 0; i < elementData.length; i++) { + if (elementData[i] == null) { + elementData[i] = o; + } + } + } else { + Object[] elementData2 = new Object[elementData.length + 1]; + for (int i = 0; i < elementData.length; i++) { + elementData2[i] = elementData[i]; + } + elementData2[elementData2.length - 1] = o; + elementData = elementData2; + } + + } + + public void add(int index, Object o) { + if (index < 0 || index > elementData.length - 1) { + return; + } + if (index <= elementData.length - 1) { + Object[] elementData2 = new Object[elementData.length + 1]; + elementData2[index] = o; + for (int i = 0; i < elementData2.length; i++) { + if (i < index) { + elementData2[i] = elementData[i]; + } + if (i > index) { + elementData2[i + 1] = elementData[i]; + } + } + elementData = elementData2; + } + + } + + public Object get(int index) { + if (elementData.length - 1 < index || index < 0) { + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index) { + if (index > elementData.length - 1 || index < 0) { + return null; + } else { + Object result = elementData[index]; + for (int i = index; i < elementData.length - 1; i++) { + elementData[index] = elementData[index + 1]; + } + elementData[elementData.length - 1] = null; + return result; + } + } + + public int size() { + return elementData.length; + } + + public Iterator iterator() { + + return new Iterator() { + int cursor = -1; + + + @Override + public boolean hasNext() { + if (cursor < elementData.length - 1) { + return true; + } + return false; + } + + @Override + public Object next() { + + cursor++; + return elementData[cursor]; + } + }; + } + + +} diff --git a/group07/1519504320/src/com/coding/basic/BinaryTreeNode.java b/group07/1519504320/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group07/1519504320/src/com/coding/basic/Iterator.java b/group07/1519504320/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group07/1519504320/src/com/coding/basic/LinkedList.java b/group07/1519504320/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..80288820a1 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/LinkedList.java @@ -0,0 +1,157 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + Node n1 = new Node(); + n1.data = o; + n.next = n1; + } + + public void add(int index, Object o) { + if (index == 0) { + Object o1 = head.data; + head.data = o; + Node next = new Node(); + next.data = o1; + head.next = next; + } + if (index < 0) { + return; + } + if (size() - 1 < index) { + return; + } else { + int flag = 0; + Node pre = head; + while (flag != index - 1) { + pre = pre.next; + flag++; + } + Node current = new Node(); + current.data = o; + Node next = pre.next; + pre.next = current; + current.next = next; + + } + + + } + + public Object get(int index) { + if (index < 0) { + return null; + } + if (size() - 1 < index) { + return null; + } else { + int flag = 0; + Node n = head; + while (flag != index) { + n = n.next; + flag++; + } + return n.data; + } + } + + public Object remove(int index) { + if (index < 0) { + return null; + } + if (size() - 1 < index) { + return null; + } else { + int flag = 0; + Node pre = head; + while (flag != index - 1) { + pre = pre.next; + flag++; + } + Node current = pre.next; + Object c = current.data; + pre.next = current.next; + return c; + } + } + + public int size() { + int size = -1; + if (head.data != null) { + size = 0; + Node n = head.next; + while (n.next != null) { + size++; + n = n.next; + } + } + return size; + } + + public void addFirst(Object o) { + if (head.data != null) { + Node n = new Node(); + n.data = o; + n.next = head; + } else { + head.data = o; + } + + } + + public void addLast(Object o) { + if (head.data == null) { + head.data = o; + } else { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + n.next.data = o; + } + } + + public Object removeFirst() { + if (head.data == null) { + return null; + } else { + Object o = head.data; + head = head.next; + return o; + } + } + + public Object removeLast() { + if (head.data == null) { + return null; + } else { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + Object o = n.data; + n.data = null; + return o; + } + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + } + + +} diff --git a/group07/1519504320/src/com/coding/basic/List.java b/group07/1519504320/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group07/1519504320/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(); +} diff --git a/group07/1519504320/src/com/coding/basic/Queue.java b/group07/1519504320/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..db969b0139 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + LinkedList a = new LinkedList(); + public void enQueue(Object o){ + a.add(o); + } + + public Object deQueue(){ + return a.removeFirst(); + } + + public boolean isEmpty(){ + if(a.size()==0){ + return true; + } + return false; + } + + public int size(){ + return a.size(); + } +} diff --git a/group07/1519504320/src/com/coding/basic/Stack.java b/group07/1519504320/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6198465bf3 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object result = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return result; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if (elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group07/178007127/001DataStructure/.classpath b/group07/178007127/001DataStructure/.classpath new file mode 100644 index 0000000000..84b630a879 --- /dev/null +++ b/group07/178007127/001DataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/178007127/001DataStructure/.gitignore b/group07/178007127/001DataStructure/.gitignore new file mode 100644 index 0000000000..4a95481e61 --- /dev/null +++ b/group07/178007127/001DataStructure/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/build/ diff --git a/group07/178007127/001DataStructure/.project b/group07/178007127/001DataStructure/.project new file mode 100644 index 0000000000..430da90497 --- /dev/null +++ b/group07/178007127/001DataStructure/.project @@ -0,0 +1,18 @@ + + + 001DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.springsource.ide.eclipse.gradle.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs new file mode 100644 index 0000000000..67ed2b404e --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs @@ -0,0 +1,9 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleImportPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +enableDependendencyManagement=true +projects=; diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs new file mode 100644 index 0000000000..f846d71532 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs @@ -0,0 +1,5 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences +#Thu Feb 23 15:58:56 CST 2017 +build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=; +org.springsource.ide.eclipse.gradle.linkedresources= +org.springsource.ide.eclipse.gradle.rootprojectloc= diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs new file mode 100644 index 0000000000..13198da612 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs @@ -0,0 +1,8 @@ +#org.springsource.ide.eclipse.gradle.core.actions.GradleRefreshPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +useHierarchicalNames=false diff --git a/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group07/178007127/001DataStructure/README.md b/group07/178007127/001DataStructure/README.md new file mode 100644 index 0000000000..f0a705d37a --- /dev/null +++ b/group07/178007127/001DataStructure/README.md @@ -0,0 +1,2 @@ +# 001DataStructure +001DataStructure diff --git a/group07/178007127/001DataStructure/build.gradle b/group07/178007127/001DataStructure/build.gradle new file mode 100644 index 0000000000..670cff0568 --- /dev/null +++ b/group07/178007127/001DataStructure/build.gradle @@ -0,0 +1,9 @@ +apply plugin:'java' + +repositories{ + mavenCentral() +} + +dependencies{ + compile group: 'junit', name: 'junit', version: '4.12' +} \ No newline at end of file diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java new file mode 100644 index 0000000000..1ab6bdd881 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java @@ -0,0 +1,19 @@ +package com.easy.util.junit.app; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import com.easy.util.myarraylist.TestArrayList; +import com.easy.util.mylinkedlist.TestLinkedList; +import com.easy.util.myqueue.TestQueue; +import com.easy.util.mystack.TestStack; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestArrayList.class, + TestLinkedList.class, + TestQueue.class, + TestStack.class +}) +public class TestApp { + +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java new file mode 100644 index 0000000000..1bab864a11 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java @@ -0,0 +1,175 @@ +package com.easy.util.myarraylist; + +import com.easy.util.myiterator.Iterator; + +public class ArrayList{ + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private Object[] elementData; + + private int size; + + + // region 构造函数 + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) { + throw new IllegalArgumentException("Illegal Capacity:" + initialCapacity); + } + this.elementData = new Object[initialCapacity]; + } + // endregion + + // region add方法 + public boolean add(Object o) { + if (elementData.length <= size) { + grow(1); + } + elementData[size++] = o; + return true; + } + + public void add(int index, Object o) { + rangeCheckForAdd(index); + grow(1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + // endregion + + // region get方法 + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + // endregion + + // region remove方法 + public Object remove(int index) { + rangeCheck(index); + + Object oldValue = elementData[index]; + /* + * int numMoved=size-index-1; if(numMoved>0){ + * System.arraycopy(elementData, index+1, elementData, index, numMoved); + * } elementData[--size]=null; + */ + fastRemove(index); + return oldValue; + } + + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) { + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int index = 0; index < size; index++) { + if (elementData[index].equals(o)) { + fastRemove(index); + return true; + } + } + } + return false; + } + // endregion + + // region size方法 + public int size() { + return size; + } + // endregion + + // region toString方法 + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < size(); i++) { + sb.append(elementData[i] + ","); + } + /* + * for (Object object : elementData) { sb.append(object + ","); } + */ + String temp = sb.toString(); + temp = temp.substring(0, temp.length() - 1); + return "[" + temp + "]"; + } + // endregion + + // region 私有方法 + private void grow(int minCapacity) { + Object[] dest = new Object[elementData.length + minCapacity]; + System.arraycopy(elementData, 0, dest, 0, elementData.length); + elementData = dest; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ",Size:" + size; + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private void fastRemove(int index) { + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + } + + // endregion + + // region 下一版本迭代的功能 + private static final int DEFAULT_CAPACITY = 10; + + private void ensureCapacityInternal(int minCapacity) { + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + ensureCapacityInternal(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + //endregion + + int currentIndex ; + public Iterator iterator(){ + currentIndex=-1; + return new Iterator() { + + @Override + public Object next() { + return elementData[++currentIndex]; + } + + @Override + public boolean hasNext() { + return currentIndex0&&index<=size; + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + //endregion +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java new file mode 100644 index 0000000000..0a57f448b6 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java @@ -0,0 +1,37 @@ +package com.easy.util.myqueue; + +import com.easy.util.mylinkedlist.LinkedList; + +public class Queue { + + LinkedList linkedList; + public Queue(){ + linkedList=new LinkedList(); + } + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return size()==0; + } + + public int size(){ + return linkedList.size(); + } + + @Override + public String toString() { + StringBuilder sb=new StringBuilder(); + for(int i=0;i implements List { + + private Object[] elements; + + // 列表长度,默认10个元素 + private int size = 10; + + // 最后一个元素的位置 + private int position = -1; + + public ArrayList() { + elements = new Object[size]; + } + + public ArrayList(final int capacity) { + if (capacity <= 0) + throw new RuntimeException("列表长度不可小于等于0!"); + elements = new Object[capacity]; + } + + /** + * 添加元素 + * + * @param e 要添加的元素 + * @return + */ + @Override + public boolean add(E e) { + + if (++position > size - 1) { + grow(); + } else { + elements[position] = e; + } + return true; + } + + /** + * 删除指定索引的元素 + * + * @param index 索引下标 + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + + if (index < 0 || index > position || this.isEmpty()) + throw new IndexOutOfBoundsException("要删除的索引不正确!"); + + E returnElement = (E) elements[index]; + elements[index] = null; + System.arraycopy(elements, index + 1, elements, index, position + 1 - index); + position--; + return returnElement; + } + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + @Override + public boolean remove(E e) { + + if (!this.contains(e)) return false; + int removeIndex = 0; + for(int i = 0; i < this.position; i++) { + if(elements[i].equals(e)) { + removeIndex = i; + break; + } + } + remove(removeIndex); + return true; + } + + /** + * 返回列表长度 + * + * @return + */ + @Override + public int size() { + return position + 1; + } + + /** + * 判断列表是否为空 + * + * @return + */ + @Override + public boolean isEmpty() { + return position == -1; + } + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + if (index > position) return null; + return (E)elements[index]; + } + + /** + * 重置某个索引的元素 + * + * @param index + * @param o + * @return + */ + @Override + public boolean set(int index, Object o) { + if (index > position) return false; + elements[index] = null; + elements[index] = o; + return true; + } + + /** + * 判断是否包含某个元素 + * + * @param o + * @return + */ + @Override + public boolean contains(Object o) { + + if(this.isEmpty()) return false; + for(int i = 0; i <= position; i++) { + if (elements[i].equals(o)) + return true; + } + return false; + } + + /** + * 清空列表 + */ + @Override + public void clear() { + for(int i = 0; i <= this.size(); i++) { + elements[i] = null; + } + position = -1; + } + + /** + * 取得迭代器 + * + * @return + */ + @Override + public Iterator iterator() { + return new Itr(); + } + + /** + * 数组增长 + */ + private void grow() { + Object[] newElements = new Object[size << 2]; + System.arraycopy(elements, 0, elements, 0, this.size); + elements = null; + elements = newElements; + } + + private class Itr implements Iterator { + + private int itrIndex = 0; + + @Override + public boolean hasNext() { + return get(itrIndex) != null; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + return (E) elements[itrIndex++]; + } + + @Override + public void remove() { + ArrayList.this.remove(itrIndex); + } + } + + @Override + public String toString() { + + if(this.isEmpty()) { + return "[]"; + } + StringBuilder strList = new StringBuilder("["); + for(int i = 0; i < this.size(); i++) { + strList.append(elements[i].toString()).append(","); + } + strList.deleteCharAt(strList.length() - 1); + strList.append("]"); + return strList.toString(); + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Hello.java b/group07/20409287/src/main/java/xdx/homework/first/Hello.java new file mode 100644 index 0000000000..1abb23045e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Hello.java @@ -0,0 +1,15 @@ +package xdx.homework.first; + +import java.util.List; + +/** + * @Description: Hello World! + * @author: xdx + * @date: 2017年2月25日 上午11:37:58 + */ +public class Hello { + + public static void main(String[] args) { + System.out.println("Hello WOrl"); + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Iterator.java b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java new file mode 100644 index 0000000000..b90db66d5d --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java @@ -0,0 +1,11 @@ +package xdx.homework.first; + + +public interface Iterator { + + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java new file mode 100644 index 0000000000..1c31589204 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java @@ -0,0 +1,246 @@ +package xdx.homework.first; + +/** + * @Description: 链式列表 + */ +public class LinkedList implements List { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + + } + + // 链表大小 + private int size = 0; + + private Node head; + + private Node tail; + + /** + * 添加元素 + * + * @param data + * @return + */ + @Override + public boolean add(E data) { + + if(this.head != null) { + Node newNode = new Node(data); + this.tail.next = newNode; + this.tail = newNode; + } else { + this.head = new Node(data); + this.tail = this.head; + } + size++; + return true; + } + + /** + * 删除指定索引的元素 + * + * @param index@return + */ + @Override + public E remove(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + if (isEmpty()) { + throw new RuntimeException("链表为空!"); + } + Node currentNode = this.head; + Node preNode = currentNode; + for (int i = 0; i < index; i++) { + preNode = currentNode; + currentNode = currentNode.next; + } + preNode.next = currentNode.next; + size--; + return currentNode.data; + } + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + @Override + public boolean remove(E e) { + + if (!this.contains(e)) return false; + + if (this.head.data.equals(e)) { + this.head = this.head.next; + size--; + return true; + } + Node currentNode = this.head; + Node preNode = currentNode; + boolean isFind = false; + for (int i = 0; i < size; i++) { + if(currentNode.data.equals(e)) { + isFind = true; + break; + } + preNode = currentNode; + currentNode = currentNode.next; + } + if (!isFind) return false; + preNode.next = currentNode.next; + size--; + return true; + } + + /** + * 返回列表长度 + * + * @return + */ + @Override + public int size() { + return size; + } + + /** + * 判断列表是否为空 + * + * @return + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + @Override + public E get(int index) { + + if (index < 0 || index > size || isEmpty()) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + Node currentNode = this.head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + /** + * 重置某个索引的元素 + * + * @param index + * @param e + * @return + */ + @Override + public boolean set(int index, E e) { + + if (index < 0 || index > size || isEmpty()) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + Node currentNode = this.head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + currentNode.data = e; + return false; + } + + /** + * 判断是否包含某个元素 + * + * @param e + * @return + */ + @Override + public boolean contains(E e) { + + if (isEmpty()) return false; + Node currentNode = this.head; + boolean isFind = false; + for (int i = 0; i < size(); i++) { + if(currentNode.data.equals(e)) { + isFind = true; + } + currentNode = currentNode.next; + } + return isFind; + } + + /** + * 清空列表 + */ + @Override + public void clear() { + this.head = this.tail = null; + size = 0; + } + + @Override + public String toString() { + + if (isEmpty()) return "[]"; + StringBuilder strLinkedList = new StringBuilder("["); + Node currentNode = this.head; + while (currentNode.next != null) { + strLinkedList.append(currentNode.data.toString()).append(","); + currentNode = currentNode.next; + } + strLinkedList.append(currentNode.data.toString()).append("]"); + return strLinkedList.toString(); + } + + /** + * 取得迭代器 + * + * @return + */ + @Override + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + Node curNode = LinkedList.this.head; + + @Override + public boolean hasNext() { + return curNode != null; + } + + @Override + public E next() { + Node thisNode = curNode; + curNode = curNode.next; + return thisNode.data; + } + + @Override + public void remove() { + LinkedList.this.remove(curNode.data); + curNode = curNode.next; + } + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/List.java b/group07/20409287/src/main/java/xdx/homework/first/List.java new file mode 100644 index 0000000000..7c6f1c4e52 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/List.java @@ -0,0 +1,86 @@ +package xdx.homework.first; + +/** + * @param + * @Description: 定义一组操作有序列表的接口 + * @author: xdx + * @date: 2017年2月21日 下午8:53:52 + */ +public interface List { + + /** + * 添加元素 + * + * @param e + * @return + */ + boolean add(E e); + + /** + * 删除指定索引的元素 + * + * @param int 索引下标 + * @return + */ + E remove(int index); + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + boolean remove(E e); + + /** + * 返回列表长度 + * + * @return + */ + int size(); + + /** + * 判断列表是否为空 + * + * @return + */ + boolean isEmpty(); + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 重置某个索引的元素 + * + * @param index + * @param e + * @return + */ + boolean set(int index, E e); + + /** + * 判断是否包含某个元素 + * + * @param e + * @return + */ + boolean contains(E e); + + /** + * 清空列表 + */ + void clear(); + + /** + * 取得迭代器 + * + * @return + */ + Iterator iterator(); + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Queue.java b/group07/20409287/src/main/java/xdx/homework/first/Queue.java new file mode 100644 index 0000000000..5d98cff312 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Queue.java @@ -0,0 +1,97 @@ +package xdx.homework.first; + +/** + * @Description: 链式队列 + */ +public class Queue { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + + } + + private Node front; // 队头 + + private Node rear; // 队尾 + + private int length; // 队长 + + public void enQueue(E data) { + + if(this.front == null) { + this.front = new Node(data); + this.rear = this.front; + } else { + Node newNode = new Node(data); + this.rear.next = newNode; + this.rear = newNode; + } + length++; + } + + public E deQueue() { + + if (isEmpty()) { + return null; + } + Node oldFront = this.front; + this.front = this.front.next; + length--; + return oldFront.data; + } + + public int length() { + return length; + } + + public boolean isEmpty() { + return this.front == this.rear; + } + + public void clear() { + this.length = 0; + this.front = null; + this.rear = null; + } + + public E getFront() { + + if (this.isEmpty()) { + return null; + } + return this.front.data; + } + + public E getRear() { + + if(this.isEmpty()) return null; + return this.rear.data; + } + + public String toString() { + + if (this.length == 0) return "[]"; + + Node node = this.front; + StringBuilder queueToString = new StringBuilder("["); + while (node.next != null) { + queueToString.append(node.data.toString()).append(","); + node = node.next; + } + queueToString.append(node.data.toString()).append("]"); + return queueToString.toString(); + } + + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Stack.java b/group07/20409287/src/main/java/xdx/homework/first/Stack.java new file mode 100644 index 0000000000..d898ffeb6e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Stack.java @@ -0,0 +1,81 @@ +package xdx.homework.first; + +/** + * @Description: + * @author: {User} + * @date: {Date} + */ +public class Stack { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + } + + private int size; + + private Node top; + + public void push(E e) { + if(!isEmpty()) { + Node newNode = new Node(e); + newNode.next = top; + top = newNode; + } else { + top = new Node(e); + } + size++; + } + + public E pop() { + if(isEmpty()) throw new RuntimeException("空栈!"); + Node popNode = top; + top = top.next; + return popNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public E peek() { + if(isEmpty()) throw new RuntimeException("空栈!"); + return top.data; + } + + public int size() { + return size; + } + + public void clear() { + top = null; + size = 0; + } + + @Override + public String toString() { + + if(isEmpty()) return "[]"; + StringBuilder stackToString = new StringBuilder("["); + Node itr = this.top; + while(itr != null) { + stackToString.append(itr.data.toString()).append(","); + itr = itr.next; + } + stackToString.deleteCharAt(stackToString.length() - 1).append("]"); + + return stackToString.toString(); + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java new file mode 100644 index 0000000000..2c71401873 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java @@ -0,0 +1,177 @@ +package xdx.homework.first; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ArrayList Tester. + * + * @author xdx + * @version 1.0 + * @since
2月25日, 2017
+ */ +public class ArrayListTest { + + private List defaultTestList; + + @Before + public void before() throws Exception { + + defaultTestList = new ArrayList<>(); + Assert.assertTrue(defaultTestList.add("《三国演义》")); + Assert.assertTrue(defaultTestList.add("《红楼梦》")); + Assert.assertTrue(defaultTestList.add("《西游记》")); + Assert.assertTrue(defaultTestList.add("《水浒传》")); + } + + @After + public void after() throws Exception { + } + + /** + * Method: add(E e) + */ + @Test + public void testAdd() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(1)); + Assert.assertTrue(testList.add(2)); + Assert.assertTrue(testList.add(3)); + System.out.println(testList.toString()); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemoveIndex() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(4)); + Assert.assertTrue(testReomoveList.add(5)); + Assert.assertTrue(testReomoveList.add(6)); + System.out.println("删除前: " + testReomoveList); + Integer delElement = testReomoveList.get(0); + Assert.assertTrue(testReomoveList.remove(0).equals(delElement)); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: remove(E e) + */ + @Test + public void testRemoveE() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(7)); + Assert.assertTrue(testReomoveList.add(8)); + Assert.assertTrue(testReomoveList.add(9)); + System.out.println("删除前: " + testReomoveList); + Assert.assertTrue(testReomoveList.remove(new Integer(8))); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + int size = defaultTestList.size(); + Assert.assertEquals(4, size); + System.out.println("defaultTest内容:" + defaultTestList); + System.out.println("defaultTest长度:" + size); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertFalse(defaultTestList.isEmpty()); + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.isEmpty()); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + Assert.assertTrue("《三国演义》".equals(defaultTestList.get(0))); + Assert.assertFalse("《西游记》".equals(defaultTestList.get(0))); + } + + /** + * Method: set(int index, Object o) + */ + @Test + public void testSet() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("设置前: " + testList); + Assert.assertTrue(testList.set(0, 10)); + System.out.println("设置后: " + testList); + } + + /** + * Method: contains(Object o) + */ + @Test + public void testContains() throws Exception { + Assert.assertTrue(defaultTestList.contains("《红楼梦》")); + Assert.assertFalse(defaultTestList.contains("《聊斋》")); + } + + /** + * Method: clear() + */ + @Test + public void testClear() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("清除前: " + testList); + testList.clear(); + Assert.assertTrue(testList.isEmpty()); + System.out.println("清除后: " + testList); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + Iterator iterator = testList.iterator(); + Assert.assertNotNull(iterator); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + List testListByDel = new ArrayList<>(); + Assert.assertTrue(testListByDel.add("张三")); + Assert.assertTrue(testListByDel.add("李四")); + Assert.assertTrue(testListByDel.add("王五")); + Iterator iteratorByDel = testListByDel.iterator(); + while(iteratorByDel.hasNext()) { + iteratorByDel.remove(); + } + Assert.assertTrue(testListByDel.isEmpty()); + + } + + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java b/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java new file mode 100644 index 0000000000..dabbc04a96 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java @@ -0,0 +1,222 @@ +package xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * LinkedList Tester. + * + */ +public class LinkedListTest { + + private LinkedList defaultLinkList; + + @Before + public void before() throws Exception { + + defaultLinkList = new LinkedList<>(); + defaultLinkList.add("1.苹果"); + defaultLinkList.add("2.香蕉"); + defaultLinkList.add("3.菠萝"); + defaultLinkList.add("4.橙子"); + defaultLinkList.add("5.葡萄"); + } + + @After + public void after() throws Exception { + } + + /** + * Method: add(E data) + */ + @Test + public void testAdd() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (Integer i = 0; i < 1000; i++) { + testLinkedList.add(i); + } + System.out.println(testLinkedList); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemoveIndex() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 100; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("删除前:" + testLinkedList); + for (int i = 0; i < 50; i++) { + testLinkedList.remove(i); + } + System.out.println("删除后:" + testLinkedList); + } + + /** + * Method: remove(E e) + */ + @Test + public void testRemoveE() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("删除前:" + testLinkedList); + testLinkedList.remove("1号"); + testLinkedList.remove("10号"); + System.out.println("删除后:" + testLinkedList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + int i = 0; + for (; i < 100; i++) { + testLinkedList.add(i + "号"); + } + Assert.assertEquals(i, testLinkedList.size()); + System.out.println("testLinkedList的内容:" + testLinkedList); + System.out.println("testLinkedList的长度:" + testLinkedList.size()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + System.out.println("testLikedList是否为空:" + testLinkedList.isEmpty()); + Assert.assertTrue(testLinkedList.isEmpty()); + String insertElement = "Hello World!"; + System.out.println("插入元素: " + insertElement); + testLinkedList.add(insertElement); + System.out.println("testLikedList是否为空:" + testLinkedList.isEmpty()); + Assert.assertFalse(testLinkedList.isEmpty()); + + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + for (int i = 0; i < 10; i++) { + System.out.println("索引为" + i + "处的元素为:" + testLinkedList.get(i)); + Assert.assertEquals((i + "号"), testLinkedList.get(i)); + } + } + + /** + * Method: set(int index, E e) + */ + @Test + public void testSet() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + for (int i = 0; i < 10; i++) { + String nodeValue = i + "号替补"; + testLinkedList.set(i, nodeValue); + Assert.assertEquals(nodeValue, testLinkedList.get(i)); + } + System.out.println("替换后:" + testLinkedList); + } + + /** + * Method: contains(E e) + */ + @Test + public void testContains() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + for (int i = 0; i < 10; i++) { + String containTestValue = i + "号"; + System.out.println("是否包含【" + containTestValue + "】:" + + testLinkedList.contains(containTestValue)); + Assert.assertTrue(testLinkedList.contains(containTestValue)); + } + + } + + /** + * Method: clear() + */ + @Test + public void testClear() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + testLinkedList.clear(); + System.out.println("清空后的链表:" + testLinkedList); + Assert.assertTrue(testLinkedList.isEmpty()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + Iterator iterator = testLinkedList.iterator(); + Assert.assertNotNull(iterator); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + + + + /** + * Method: remove() + */ + @Test + public void testRemove() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + Iterator iterator = testLinkedList.iterator(); + while (iterator.hasNext()) { + iterator.remove(); + } + System.out.println("删除所有元素后:" + testLinkedList); + Assert.assertTrue(testLinkedList.isEmpty()); + + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/QueueTest.java b/group07/20409287/src/test/xdx/homework/first/QueueTest.java new file mode 100644 index 0000000000..6e56e3b8b1 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/QueueTest.java @@ -0,0 +1,141 @@ +package xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Queue; + +/** + * Queue Tester. + * + * @author + * @since
二月 25, 2017
+ * @version 1.0 + */ +public class QueueTest { + + private Queue defaultQueue; + + @Before + public void before() throws Exception { + + defaultQueue = new Queue<>(); + defaultQueue.enQueue("赵"); + defaultQueue.enQueue("钱"); + defaultQueue.enQueue("孙"); + defaultQueue.enQueue("李"); + defaultQueue.enQueue("周"); + defaultQueue.enQueue("吴"); + defaultQueue.enQueue("郑"); + defaultQueue.enQueue("王"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: enQueue(E data) + * + */ + @Test + public void testEnQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println(testQueue); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println("删除前:" + testQueue); + System.out.println("删除:" + testQueue.deQueue()); + System.out.println("删除后:" + testQueue); + } + + /** + * + * Method: length() + * + */ + @Test + public void testLength() throws Exception { + Assert.assertEquals(8, defaultQueue.length()); + System.out.println("defaultQueue长度:" + defaultQueue.length()); + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertTrue(!defaultQueue.isEmpty()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + + testQueue.clear(); + Assert.assertTrue(testQueue.isEmpty()); + } + + /** + * + * Method: getFront() + * + */ + @Test + public void testGetFront() throws Exception { + Assert.assertEquals("赵", defaultQueue.getFront()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: getRear() + * + */ + @Test + public void testGetRear() throws Exception { + Assert.assertEquals("王", defaultQueue.getRear()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: toString() + * + */ + @Test + public void testToString() throws Exception { + System.out.println("defaultQueue内容:" + defaultQueue.toString()); + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/StackTest.java b/group07/20409287/src/test/xdx/homework/first/StackTest.java new file mode 100644 index 0000000000..bfc6b2c8f7 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/StackTest.java @@ -0,0 +1,126 @@ +package test.xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Stack; + +/** + * Stack Tester. + * + * @version 1.0 + */ +public class StackTest { + + private Stack defaultStack; + + @Before + public void before() throws Exception { + + defaultStack = new Stack<>(); + defaultStack.push("孙悟空"); + defaultStack.push("唐僧"); + defaultStack.push("猪八戒"); + defaultStack.push("沙僧"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(E e) + * + */ + @Test + public void testPush() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + Assert.assertTrue(testStack.isEmpty()); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + Assert.assertEquals("沙僧", defaultStack.peek()); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + Assert.assertEquals(4, defaultStack.size()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println("清空前:" + testStack); + testStack.clear(); + System.out.println("清空后:" + testStack); + Assert.assertTrue(testStack.isEmpty()); + + + } + + +} diff --git a/group07/396868934/DataStructure/.classpath b/group07/396868934/DataStructure/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group07/396868934/DataStructure/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group07/396868934/DataStructure/.project b/group07/396868934/DataStructure/.project new file mode 100644 index 0000000000..72a951f7c1 --- /dev/null +++ b/group07/396868934/DataStructure/.project @@ -0,0 +1,17 @@ + + + DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/396868934/DataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/396868934/DataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/396868934/DataStructure/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group07/396868934/DataStructure/bin/.gitignore b/group07/396868934/DataStructure/bin/.gitignore new file mode 100644 index 0000000000..c2d9872a16 --- /dev/null +++ b/group07/396868934/DataStructure/bin/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java new file mode 100644 index 0000000000..450a1ccff1 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java @@ -0,0 +1,93 @@ +package com.louisly.java; + +public class LYArrayLink { + + private int currentCount = 0; + private LYNode header = null; + private LYNode lastNode = null; + public void addObject(Object obj) { + if (obj == null) return; + + currentCount++; + LYNode node = new LYNode(); + node.data = obj; + + if (lastNode != null) { + lastNode.next = node; + lastNode = node; + } else { + lastNode = node; + } + + if (header == null) { + header = node; + } + } + + public void removeObject(Object obj) { + LYNode lastNode = null; + LYNode node = header; + Object data = null; + while (node != null) { + data = node.data; + if (data == obj) { + if (lastNode != null) { + lastNode.next = node.next; + } else { + // �Ƴ���һ��Ԫ�� + header = node.next; + } + + currentCount--; + } else { + lastNode = node; + } + node = node.next; + } + } + + public void removeAtIndex(int index) { + if (header == null) return; // error: out of bounces + + LYNode lastNode = null; + LYNode node = header; + + for (int i = 0; i < index; i++) { + if (node != null) { + lastNode = node; + node = node.next; + } else { + return; // error: out of bounces + } + } + + if (index == 0) { + header = node.next; + } else { + lastNode.next = node.next; + } + currentCount--; + } + + public Object get(int index) { + if (header == null) return null; // error: out of bounces + LYNode node = header; + for (int i = 0; i < index; i++) { + node = node.next; + if (node == null) { + return null; + } + } + return node.data; + } + + public int size() { + return currentCount; + } + + private static class LYNode { + Object data; + LYNode next; + + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java new file mode 100644 index 0000000000..0076cddad3 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java @@ -0,0 +1,96 @@ +package com.louisly.java; +import com.louisly.java.LYIterator; + +public class LYArrayList { + private Object[] elementData = new Object[10]; + private int currentCount = 0; + public void addObject(Object obj) { + if (currentCount >= elementData.length) { + grow(); + } + elementData[currentCount] = obj; + currentCount++; + } + + public boolean removeObject(Object obj) { + boolean existObj = false; + int removeIndex = -1; + int index = currentCount; + for (int i = 0; i < index; i++) { + Object element = elementData[i]; + boolean remove = false; + if (element != null && element.equals(obj)) { + elementData[i] = null; + existObj = true; + remove = true; + // �Է�����һ���ĵڶ���Ԫ�� + if (removeIndex == -1) { + removeIndex = i; + } + currentCount--; + } + // ��Ԫ����ǰ�ƶ� + if (!remove) { + elementData[removeIndex] = element; + elementData[i] = null; + removeIndex++; + } + } + return existObj; + } + + public boolean removeAtIndex(int index) { + if (index > currentCount) { + return false; + } + elementData[index] = null; + + for (int i = index+1; i < currentCount; i++) { + elementData[i-1] = elementData[i]; + elementData[i] = null; + } + currentCount--; + return true; + } + + public void grow() { + Object[] target = new Object[elementData.length*2]; + System.arraycopy(elementData, 0, target, 0, elementData.length); + elementData = target; + } + + public Object get(int index) { + if (index > currentCount) { + return null; + } + return elementData[index]; + } + + public int size() { + return currentCount; + } + + public LYIterator iterator() { + return new LYArrayListIterator(this); + } + private class LYArrayListIterator implements LYIterator { + LYArrayList arrayList = null; + int position = 0; + public LYArrayListIterator(LYArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + + return false; + } + @Override + public Object next() { + return elementData[position]; + } + public void remove() { + + } + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java new file mode 100644 index 0000000000..a457211867 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java @@ -0,0 +1,50 @@ +package com.louisly.java; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import junit.framework.Assert; + +public class LYArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + LYArrayList list = new LYArrayList(); + list.addObject(new Integer(10)); + Assert.assertEquals(10, ((Integer)list.get(0)).intValue()); + } + + @Test + public void testRemoveObject() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveAtIndex() { + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java b/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java new file mode 100644 index 0000000000..4ea3141135 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java @@ -0,0 +1,45 @@ +package com.louisly.java; +import com.louisly.java.LYObject; + +public class LYBinaryTree { + + private LYBinaryTreeNode headerNode; + + public void addObject(LYObject obj) { + + if (headerNode == null) { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + headerNode = node; + return; + } + + this.appendObject(headerNode, obj); + } + + private void appendObject(LYBinaryTreeNode toNode, LYObject obj) { + if (obj.i > toNode.data.i) { + if (toNode.right != null) { + this.appendObject(toNode.right, obj); + } else { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + toNode.right = node; + } + } else { + if (toNode.left != null) { + this.appendObject(toNode.left, obj); + } else { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + toNode.left = node; + } + } + } + + public static class LYBinaryTreeNode { + private LYObject data; + private LYBinaryTreeNode left; + private LYBinaryTreeNode right; + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java b/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java new file mode 100644 index 0000000000..c2ae34c871 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java @@ -0,0 +1,6 @@ +package com.louisly.java; + +public interface LYIterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java b/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java new file mode 100644 index 0000000000..f0f5aa5d81 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java @@ -0,0 +1,8 @@ +package com.louisly.java; + +public class LYObject extends Object { + int i = 0; + public LYObject(int i) { + this.i = i; + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java b/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java new file mode 100644 index 0000000000..5f573ae0ac --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java @@ -0,0 +1,28 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; + +public class LYQueue { + LYArrayList list = null; + public LYQueue() { + list = new LYArrayList(); + } + + public void enQueue(Object obj) { + list.addObject(obj); + } + + public Object deQueue() { + if (list.size() != 0) { + return list.get(0); + } + return null; + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java b/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java new file mode 100644 index 0000000000..c615208041 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java @@ -0,0 +1,34 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; + +public class LYStack { + private LYArrayList list = new LYArrayList(); +// public LYStack() { +// list = new LYArrayList(); +// } + + public void push(Object obj) { + list.addObject(obj); + } + + public Object pop() { + if (list.size() == 0) return null; + Object obj = list.get(list.size()-1); + list.removeObject(obj); + return obj; + } + + public Object peak() { + if (list.size() == 0) return null; + Object obj = list.get(list.size()-1); + return obj; + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/main.java b/group07/396868934/DataStructure/src/com/louisly/java/main.java new file mode 100644 index 0000000000..c613c2f36c --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/main.java @@ -0,0 +1,84 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; +import com.louisly.java.LYObject; +import com.louisly.java.LYArrayLink; +import com.louisly.java.LYBinaryTree; + +public class main { + + public static void main(String[] args) { + +// testBinaryTree(); + testArrayLink(); +// testArrayList(); + } + + public static void testBinaryTree() { + LYBinaryTree tree = new LYBinaryTree(); + tree.addObject(new LYObject(5)); + tree.addObject(new LYObject(7)); + tree.addObject(new LYObject(2)); + tree.addObject(new LYObject(1)); + tree.addObject(new LYObject(6)); + tree.addObject(new LYObject(4)); + tree.addObject(new LYObject(8)); + } + + public static void testArrayLink() { + // ����20��Ԫ�� + LYArrayLink list = new LYArrayLink(); + for (int i = 0; i < 20; i++) { + LYObject object = new LYObject(i); + list.addObject(object); + } + + System.out.print("��ǰ�����й���Ԫ�ظ�����" + list.size() + "\n"); + + // ��ӡĿǰ���ڵ�ÿ��Ԫ�� + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + + System.out.print("======\n"); + + // �Ƴ����߸�Ԫ�� + list.removeAtIndex(7); + + System.out.print("��ǰ�����й���Ԫ�ظ�����" + list.size() + "\n"); + + // �ٴ�ӡĿǰ��ʣ����ЩԪ�� + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + } + + public static void testArrayList() { + // ����20��Ԫ�� + LYArrayList list = new LYArrayList(); + for (int i = 0; i < 20; i++) { + LYObject object = new LYObject(i); + list.addObject(object); + } + + System.out.print("��ǰ�����й���Ԫ�ظ�����" + list.size() + "\n"); + + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + + System.out.print("======\n"); + + + list.removeAtIndex(7); + System.out.print("��ǰ�����й���Ԫ�ظ�����" + list.size() + "\n"); + + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + } + +} diff --git a/group07/43819473/homework/pom.xml b/group07/43819473/homework/pom.xml new file mode 100644 index 0000000000..b4c8775d15 --- /dev/null +++ b/group07/43819473/homework/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + homework1 + homework + 1.0-SNAPSHOT + + + + + junit + junit + 4.12 + + + + com.alibaba + fastjson + 1.2.7 + + + + \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java new file mode 100644 index 0000000000..40ccb13a06 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java @@ -0,0 +1,87 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + elementData[size] = o; + size++; + } + + private Object[] grow(Object[] datas) { + Object[] elementDataNew = new Object[elementData.length + elementData.length / 4]; + System.arraycopy(datas, 0, elementDataNew, 0, size); + return elementDataNew; + } + + public void add(int index, Object o) { + + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + for (int i = size - 1; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + for (int i = index; i <= size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return null; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListInterator(); + } + + private class ArrayListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return elementData[nowIndex++]; + } + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java b/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java new file mode 100644 index 0000000000..a93c25253e --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java @@ -0,0 +1,73 @@ +package dataStructure; + +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode insert(int data) { + BinaryTreeNode node = new BinaryTreeNode(data); + root = insert(root, node); + return root; + } + + private BinaryTreeNode insert(BinaryTreeNode root, BinaryTreeNode newNode) { + if (root == null) { + root = newNode; + } else if (newNode.data > root.data) { + root.right = insert(root.right, newNode); + } else { + root.left = insert(root.left, newNode); + } + return root; + } + + /** + * binary tree node + */ + private class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data) { + this.left = null; + this.right = null; + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + } +} + diff --git a/group07/43819473/homework/src/main/java/dataStructure/Iterator.java b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java new file mode 100644 index 0000000000..06ad99316f --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java @@ -0,0 +1,9 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java new file mode 100644 index 0000000000..f6a960698a --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java @@ -0,0 +1,116 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/21. + */ +public class LinkedList implements List { + + private Node head=new Node(); + private int size = 0; + + public void add(Object o) { + addToNode(head,o); + size++; + } + + public void add(int index, Object o) { + if (index <0 || index > size) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + addToNode(getLastNode(index),o); + size++; + } + + private Node getLastNode(int index){ + + Node nowNode = head; + for (int pos = 1; pos <= index; pos++) { + nowNode = nowNode.next; + } + return nowNode; + } + private void addToNode(Node node,Object o){ + if (node.next == null) { + Node newNode = new Node(); + newNode.data = o; + node.next = newNode; + } else { + Node newNode = new Node(); + newNode.data = o; + newNode.next = node.next; + node.next = newNode; + } + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + return node.next==null?null:node.next.data; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + Node nowNode=node.next; + if(nowNode.next!=null){ + node.next=node.next.next; + }else{ + node.next=null; + } + size--; + return nowNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0,o); + } + + public void addLast(Object o) { + add(size,o); + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size-1); + } + + public Iterator iterator() { + return new LinkedListInterator(); + } + + private class LinkedListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return get(nowIndex++); + } + } + + + private static class Node { + Object data; + Node next; + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/List.java b/group07/43819473/homework/src/main/java/dataStructure/List.java new file mode 100644 index 0000000000..0b1b43fc26 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/List.java @@ -0,0 +1,16 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +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/group07/43819473/homework/src/main/java/dataStructure/Queue.java b/group07/43819473/homework/src/main/java/dataStructure/Queue.java new file mode 100644 index 0000000000..943e0e64f6 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Queue.java @@ -0,0 +1,25 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Queue { + + private LinkedList list=new LinkedList(); + + public void enQueue(Object o) { + list.addFirst(o); + } + + public Object deQueue() { + return list.removeLast(); + } + + public boolean isEmpty() { + return list.size()==0?true:false; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/Stack.java b/group07/43819473/homework/src/main/java/dataStructure/Stack.java new file mode 100644 index 0000000000..e1a7161317 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Stack.java @@ -0,0 +1,29 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Stack { + + private LinkedList list = new LinkedList(); + + public void push(Object o) { + list.addFirst(o); + } + + public Object pop() { + return list.removeFirst(); + } + + public Object peek() { + return list.get(0); + } + + public boolean isEmpty() { + return list.size() == 0 ? true : false; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java b/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java new file mode 100644 index 0000000000..02ea9fbb6e --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java @@ -0,0 +1,79 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zj on 2017/2/20. + */ +public class ArrayListTest { + ArrayList list =null; + + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + for (int i = 0; i < 200; i++) { + list.add(i); + } + + System.out.println("=============================before=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + + } + } + + @After + public void tearDown() throws Exception { + System.out.println("=============================after=============================="); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void add() throws Exception { + } + + @Test + public void add1() throws Exception { + list.add(5, 555); + list.add(12, 1255); + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + list.remove(3); + list.remove(90); + } + + @Test + public void size() throws Exception { + } + + @Test + public void iterator() throws Exception { + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + ","); + } + + System.out.println(); + System.out.println("---------------------------"); + } + +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java b/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java new file mode 100644 index 0000000000..90698681e7 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java @@ -0,0 +1,34 @@ +package dataStructure; + +import com.alibaba.fastjson.JSON; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zj on 2017/2/26. + */ +public class BinaryTreeTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void insert() throws Exception { + BinaryTree tree=new BinaryTree(); + tree.insert(6); + tree.insert(5); + tree.insert(8); + tree.insert(3); + tree.insert(4); + System.out.println(JSON.toJSONString(tree.getRoot())); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java b/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java new file mode 100644 index 0000000000..cc7e2bebcb --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java @@ -0,0 +1,102 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class LinkedListTest { + + LinkedList list = null; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + for (int i = 0; i < 6; i++) { + list.add(i); + } + System.out.println("=============================before=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + } + } + + @After + public void tearDown() throws Exception { + System.out.println("=============================after=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + + } + } + + @Test + public void testAdd() throws Exception { + list.add(300); + } + + @Test + public void testAdd1() throws Exception { + list.add(2, 100); + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + list.remove(3); + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + list.addFirst(66); + } + + @Test + public void testAddLast() throws Exception { + list.addLast(77); + } + + @Test + public void testRemoveFirst() throws Exception { + list.removeFirst(); + } + + @Test + public void testRemoveLast() throws Exception { + list.removeLast(); + } + + @Test + public void testIterator() throws Exception { + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + ","); + } + + System.out.println(); + System.out.println("---------------------------"); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java b/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java new file mode 100644 index 0000000000..2d11213d42 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java @@ -0,0 +1,51 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class QueueTest { + + Queue list = null; + + @Before + public void setUp() throws Exception { + list = new Queue(); + for (int i = 0; i < 5; i++) { + list.enQueue(i); + } + } + + @After + public void tearDown() throws Exception { + System.out.println("----------------tearDown------------------"); + int count=list.size(); + for (int i = 0; i < count; i++) { + System.out.println("list.deQueue():"+list.deQueue()); + } + } + + @Test + public void testEnQueue() throws Exception { + list.enQueue(11); + } + + @Test + public void testDeQueue() throws Exception { + System.out.println("list.deQueue():"+list.deQueue()); + System.out.println("list.deQueue():"+list.deQueue()); + } + + @Test + public void testIsEmpty() throws Exception { + System.out.println("testIsEmpty:"+list.isEmpty()); + } + + @Test + public void testSize() throws Exception { + System.out.println("testSize:"+list.size()); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/StackTest.java b/group07/43819473/homework/src/test/java/dataStructure/StackTest.java new file mode 100644 index 0000000000..016bdb5811 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/StackTest.java @@ -0,0 +1,56 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zj on 2017/2/24. + */ +public class StackTest { + Stack list = null; + + @Before + public void setUp() throws Exception { + list = new Stack(); + for (int i = 0; i < 5; i++) { + list.push(i); + } + } + + @After + public void tearDown() throws Exception { + System.out.println("----------------tearDown------------------"); + int count=list.size(); + for (int i = 0; i < count; i++) { + System.out.println("list.pop():"+list.pop()); + } + } + + @Test + public void push() throws Exception { + list.push(11); + } + + @Test + public void pop() throws Exception { + System.out.println("list.pop():"+list.pop()); + System.out.println("list.pop():"+list.pop()); + } + + @Test + public void peek() throws Exception { + System.out.println("list.peek():"+list.peek()); + } + + @Test + public void testIsEmpty() throws Exception { + System.out.println("testIsEmpty:"+list.isEmpty()); + } + + @Test + public void testSize() throws Exception { + System.out.println("testSize:"+list.size()); + } + +} \ No newline at end of file diff --git a/group07/476770768/MyDataStructure/.classpath b/group07/476770768/MyDataStructure/.classpath index 63b7e892d1..b387714202 100644 --- a/group07/476770768/MyDataStructure/.classpath +++ b/group07/476770768/MyDataStructure/.classpath @@ -2,5 +2,6 @@ + diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java index f0c1b3608c..5c8c3bb858 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java @@ -89,7 +89,7 @@ public boolean isFull(){ } public void checkBounds(int index){ - if(index >= size || index < 0){ + if(index > size || index < 0){ //System.out.println("From MyArrayList: Index out of bounds"); throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java new file mode 100644 index 0000000000..c7d9299934 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyArrayListTest { + + + @Test + public void testAddObject() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + mal.add(new Integer(1)); + assertEquals(1, mal.size()); + } + + @Test + public void testAddIntObject() { + MyArrayList mal = new MyArrayList(); + mal.add(0, new Integer(1)); + assertEquals(1, mal.size()); + int tmp = 0; + try { + mal.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + + } + + @Test + public void testGet() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + int tmp = 0; + try { + mal.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + assertEquals(mal.size(),1); + } + + @Test + public void testSize() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + } + + @Test + public void testIsEmpty() { + MyArrayList mal = new MyArrayList(); + assertTrue(mal.isEmpty()); + mal.add(new Integer(1)); + assertFalse(mal.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java index 297c97a3ed..3fe3693b19 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java @@ -38,9 +38,13 @@ public void add(int index, Object o) { */ public void addFirst(Object o) { Node tmp = new Node(o); - tmp.next = head; - head.prov = tmp; - head = tmp; + if(head == null){ + head = tmp; + }else{ + tmp.next = head; + head.prov = tmp; + head = tmp; + } } /** @@ -97,7 +101,10 @@ public Object remove(int index) { public Node removeFirst() { Node tmp = head; head = head.next; - head.prov = null; + if(head != null){ + head.prov = null; + } + return tmp; } @@ -199,22 +206,22 @@ public MyIterator iterator() { private class LinkedListIterator implements MyIterator{ private MyLinkedList eleIterator; - private Node pos; + private int pos; private LinkedListIterator(MyLinkedList mll){ this.eleIterator = mll; - this.pos = eleIterator.get(0); + this.pos = 0; } @Override public boolean hasNext() { - return pos != null; + return pos <= size; } @Override public Object next() { - Node res = pos; - pos = pos.next; + Node res = eleIterator.get(pos); + pos++; return res; } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..1da080a16a --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void testAddObject() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + mll.add(new Integer(1)); + assertEquals(1, mll.size()); + } + + @Test + public void testAddIntObject() { + MyLinkedList mll = new MyLinkedList(); + mll.add(0, new Integer(1)); + assertEquals(1, mll.size()); + int tmp = 0; + try { + mll.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testGet() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + assertNotNull(mll.get(0)); + int tmp = 0; + try { + mll.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + mll.remove(0); + assertEquals(mll.size(),0); + } + + @Test + public void testSize() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + } + + @Test + public void testIsEmpty() { + MyLinkedList mll = new MyLinkedList(); + assertTrue(mll.isEmpty()); + mll.add(new Object()); + assertFalse(mll.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java new file mode 100644 index 0000000000..3d80a95ee4 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyQueueTest { + + @Test + public void testEnQueue() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + mq.enQueue(new Object()); + assertEquals(mq.size(), 1); + } + + @Test + public void testDeQueue() { + MyQueue mq = new MyQueue(); + int tmp = 0; + try { + mq.deQueue(); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + mq.enQueue(new Object()); + assertNotNull(mq.deQueue()); + } + + @Test + public void testIsEmpty() { + MyQueue mq = new MyQueue(); + assertTrue(mq.isEmpty()); + mq.enQueue(new Object()); + assertFalse(mq.isEmpty()); + } + + @Test + public void testSize() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java index 3d9e1ef9a0..3c5b5a6b67 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java @@ -31,7 +31,7 @@ public Object peek(){ } public boolean isEmpty(){ - return top >= 0; + return top < 0; } public int size(){ diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java new file mode 100644 index 0000000000..0722e2d4fa --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import java.util.EmptyStackException; + +import org.junit.Test; + +public class MyStackTest { + + @Test + public void testPush() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + ms.push(new Object()); + assertEquals(1, ms.size()); + } + + @Test + public void testPop() { + MyStack ms = new MyStack(); + ms.push(new Object()); + assertNotNull(ms.pop()); + assertEquals(0, ms.size()); + } + + @Test + public void testPeek() { + MyStack ms = new MyStack(); + int tmp = 0; + try { + ms.peek(); + } catch (EmptyStackException e) { + tmp = 1; + assertEquals(1, tmp); + } + ms.push(new Object()); + assertNotNull(ms.peek()); + assertEquals(1, ms.size()); + } + + @Test + public void testIsEmpty() { + MyStack ms = new MyStack(); + assertTrue(ms.isEmpty()); + ms.push(new Object()); + assertFalse(ms.isEmpty()); + } + + @Test + public void testSize() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java index 1ccabfc977..e75137744b 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java @@ -3,21 +3,6 @@ public class testFile { public static void main(String[] args) { - MyLinkedList mll = new MyLinkedList(); - mll.add(new Integer(5)); - mll.add(new Integer(2)); - mll.add(new Integer(3)); - mll.add(new Integer(4)); - System.out.println(mll); - MyIterator mIt = mll.iterator(); - while(mIt.hasNext()){ - System.out.println(mIt.next()); - } - mll.remove(3); - System.out.println(mll); - - - } } diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.classpath b/group07/476770768/Week2_HOMEWORK/Coderising/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore b/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.project b/group07/476770768/Week2_HOMEWORK/Coderising/.project new file mode 100644 index 0000000000..9b11b75eff --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.project @@ -0,0 +1,17 @@ + + + Coderising + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.settings/org.eclipse.jdt.core.prefs b/group07/476770768/Week2_HOMEWORK/Coderising/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java new file mode 100644 index 0000000000..521dbad798 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java @@ -0,0 +1,227 @@ +package com.Array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * ����һ����������a , �Ը������ֵ�����û� ���磺 a = [7, 9 , 30, 3] , �û���Ϊ [3, 30, 9,7] ��� a = + * [7, 9, 30, 3, 4] , �û���Ϊ [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (origin == null) + return; + int head = 0; + int tail = origin.length - 1; + int tmp; + while (head < tail) { + tmp = origin[tail]; + origin[tail] = origin[head]; + origin[head] = tmp; + head++; + tail--; + } + } + + /** + * ���������µ�һ�����飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * Ҫ������������ֵΪ0����ȥ��������Ϊ0��ֵ����һ���µ����飬���ɵ�������Ϊ�� {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int nonZeroNum = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + nonZeroNum++; + } + } + int cnt = 0; + int[] newArray = new int[nonZeroNum]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[cnt++] = oldArray[i]; + } + } + return newArray; + } + + /** + * ���������Ѿ�����õ��������飬 a1��a2 , ����һ���µ�����a3, ʹ��a3 ����a1��a2 ������Ԫ�أ� ������Ȼ������� + * ���� a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] �� a3 Ϊ[3,4,5,6,7,8] , ע�⣺ �Ѿ��������ظ� + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] resultArray = new int[array1.length + array2.length]; + int cnt1 = 0; + int cnt2 = 0; + int cntResult = 0; + resultArray[cntResult++] = array1[0] array2[cnt2]){ + if(resultArray[cntResult-1] != array2[cnt2]){ + //array2[cnt2]�е���С����û���ظ� + //����resultArray�� + resultArray[cntResult++] = array2[cnt2++]; + }else{ + //array2[cnt2]�е���С�������ظ� + cnt2++; + } + }else{ + if(resultArray[cntResult-1] != array1[cnt1]){ + //array1[cnt1]�е���С����û���ظ� + //����resultArray�� + resultArray[cntResult++] = array1[cnt1++]; + }else{ + //array1[cnt1]�е���С�������ظ� + cnt1++; + } + } + } + + //��Ϊ������������ʣ�ಿ�ַ���resultArray + if(cnt1 == array1.length){ + //array2��ʣ�� + while(cnt2 < array2.length){ + //��array2��ʣ����������ظ��ؼ���resultArray�� + if(resultArray[cntResult-1] != array2[cnt2]){ + resultArray[cntResult++] = array2[cnt2++]; + }else{ + cnt2++; + } + } + }else{ + while(cnt1 < array1.length){ + //��array1��ʣ����������ظ��ؼ���resultArray�� + if(resultArray[cntResult-1] != array1[cnt1]){ + resultArray[cntResult++] = array1[cnt1++]; + }else{ + cnt1++; + } + } + } + return Arrays.copyOf(resultArray, cntResult); + } + + /** + * ��һ���Ѿ��������ݵ����� oldArray������������չ�� ��չ��������ݴ�СΪoldArray.length + size + * ע�⣬�������Ԫ��������������Ҫ���� ���� oldArray = [2,3,6] , size = 3,�򷵻ص�������Ϊ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for(int i=0; i index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + + } + + // 取值 + public Object get(int index) { + + if (index > size) { + return null; + } else { + return elementData[index]; + } + + } + + // 按索引删除该值 + public Object remove(int index) { + Object o = elementData[index]; + // 删除的位置小于数组大小,将index位置后面的值依次向前挪一位 + for (int i = index; i < elementData.length - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return o; + } + + // 返回数组大小 + public int size() { + return size; + } + + //迭代器 + class MyIterator implements Iterator { + int current = 0; + + public boolean hasNext() { + return current < size; + } + + public Object next() { + if (hasNext()) + return elementData[current++]; + else + throw new java.util.NoSuchElementException(); + } + + } + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.add(0, 1); + a.add(2); + a.add(3); + a.add(4); + a.add(1, 0); + a.remove(3); + ArrayList.MyIterator m = a.new MyIterator(); + System.err.println(a.elementData.length); + while(m.hasNext()){ + System.out.print(m.next()+" "); + } + } + +} diff --git a/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java b/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java new file mode 100644 index 0000000000..5b136abccc --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package cn.fyl.first; + +public class BinaryTreeNode { + private Object data; //保存数据 + private BinaryTreeNode left; //左子树 + private BinaryTreeNode right; //右子树 + private BinaryTreeNode root; //根节点 + + public BinaryTreeNode getRoot() { + return root; + } + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + //插入功能,插入的值按比父类结点的值大,插入右子树;小,插入左子树 + public BinaryTreeNode insert(Object o,BinaryTreeNode t){ + if(t == null){ + BinaryTreeNode temp = new BinaryTreeNode(); //新建插入值的结点 + temp.setData(o); + return temp; + } + boolean temp = (int)o > (int)t.getData(); //暂存插入的值跟结点的值比较大小结果 + if(temp){ //ture时(插入的值大于结点的值大),所以插到右子树 + System.err.println(temp); + t.right =insert(o, t.right); + } + else{ + System.out.println(temp); + t.left = insert(o, t.left); + } + return t; + } + + public static void main(String[] args) { + BinaryTreeNode root = new BinaryTreeNode(); + BinaryTreeNode left1 = new BinaryTreeNode(); + BinaryTreeNode right1 = new BinaryTreeNode(); + BinaryTreeNode left2 = new BinaryTreeNode(); + BinaryTreeNode left3 = new BinaryTreeNode(); + root.setData(5); + root.setLeft(left1); left1.setData(2); + root.setRight(right1); right1.setData(7); + left1.setLeft(left2); left2.setData(1); + right1.setLeft(left3); left3.setData(6); + BinaryTreeNode temp = root.insert(4, left1); + System.out.println(left1.getRight().getData()); + } + +} diff --git a/group07/724319952/src/cn/fyl/first/Iterator.java b/group07/724319952/src/cn/fyl/first/Iterator.java new file mode 100644 index 0000000000..7b907d4137 --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Iterator.java @@ -0,0 +1,7 @@ +package cn.fyl.first; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); +} diff --git a/group07/724319952/src/cn/fyl/first/LinkedList.java b/group07/724319952/src/cn/fyl/first/LinkedList.java new file mode 100644 index 0000000000..e6e5fd71b8 --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/LinkedList.java @@ -0,0 +1,163 @@ +package cn.fyl.first; + +public class LinkedList implements List { + + private Node head,tail; //头尾结点 + private int size; //保存链表大小 + + //将值插入链表 + public void add(Object o) { + add(size(),o); + } + + //将值从index位置插入链表 + public void add(int index, Object o) { + if(index == 0){ + addFirst(o); + } + else if(index >= size){ + addLast(o); + } + else{ + Node current = head; + for (int i = 1; i < index; i++) { + current = current.next; + } + Node temp = current.next; + current.next = new Node(o); + (current.next).next = temp; + size++; + } + } + + //取出index位置的值 + public Object get(int index) { + if(index < 0 || index >=size){ + return null; + } + else if(index == 0){ + return head.data; + } + else if(index == size-1){ + return tail.data; + } + else{ + Node current = head; + for (int i = 1; i < index; i++) { + current = current.next; + } + Node temp = current.next; + return temp.data; + } + } + + //删除index位置的值 + public Object remove(int index) { + if(index < 0 || index >size) + return null; + else if(index ==0) + return head.data; + else if(index == size -1) + return tail.data; + else{ + Node previous = head; + for (int i = 1; i < index; i++) { + previous = previous.next; + } + Node current = previous.next; + previous.next = current.next; + size--; + return current.data; + } + } + + public int size() { + return size; + } + + //添加头结点 + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; //指向头引用所指结点 + head = newNode; //头引用指向新增结点 + size++; + if(tail == null){ + tail = head; + } + } + + //添加尾结点 + public void addLast(Object o) { + Node newNode = new Node(o); + if(tail == null){ + head = tail = newNode; + } + else{ + tail.next = newNode; + tail = tail.next; + } + size++; + } + + //删除头结点 + public Object removeFirst(){ + if(size == 0){ + return null; + } + else if(size == 1){ + Node temp = head; + head = tail = null; + size = 0; + return temp.data; + } + else{ + Node temp = head; + head = head.next; + size--; + return temp.data; + } + } + + //删除尾结点 + public Object removeLast() { + if(size == 0){ + return null; + } + else if(size ==1){ + Node temp = head; + head =tail =null; + size =0; + return temp.data; + } + else{ + Node current = head; + for (int i = 0; i < size - 2; i++) { + current = current.next; + } + Node temp =tail; + tail = current; + tail.next = null; + size--; + return temp.data; + } + } + + + + private static class Node { + Object data; + Node next; + public Node(Object o){ + data = o; + } + } + + public static void main(String[] arg){ + LinkedList l = new LinkedList(); + l.add(0); + l.add(2); + l.add(4); + l.add(3, 1); + System.out.println(l.removeLast()+" "+l.removeFirst()+" "+l.get(0)+" "+l.get(1)); + } +} diff --git a/group07/724319952/src/cn/fyl/first/List.java b/group07/724319952/src/cn/fyl/first/List.java new file mode 100644 index 0000000000..45a4d4652e --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/List.java @@ -0,0 +1,11 @@ +package cn.fyl.first; + +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/group07/724319952/src/cn/fyl/first/Queue.java b/group07/724319952/src/cn/fyl/first/Queue.java new file mode 100644 index 0000000000..1e9e5ef3ad --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Queue.java @@ -0,0 +1,37 @@ +package cn.fyl.first; + +public class Queue { + + LinkedList linkedlist = new LinkedList(); + + public void enQueue(Object o){ + linkedlist.addLast(o); + } + + public Object deQueue(){ + return linkedlist.removeFirst(); + } + + public boolean isEmpty(){ + if(linkedlist.size()>0) + return false; + else + return true; + } + + public int size(){ + return linkedlist.size(); + } + + public Object get(int index){ + return linkedlist.get(index); + } + + public static void main(String[] arg){ + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + System.out.println(q.get(1)); + } + +} diff --git a/group07/724319952/src/cn/fyl/first/Stack.java b/group07/724319952/src/cn/fyl/first/Stack.java new file mode 100644 index 0000000000..368178691f --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Stack.java @@ -0,0 +1,39 @@ +package cn.fyl.first; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + int last; + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(last-1); + } + + public Object peek() { + last = elementData.size()-1; + return elementData.get(last); + } + + public boolean isEmpty() { + if(elementData.size() > 0) + return false; + else + return true; + } + + public int size() { + return elementData.size(); + } + + public static void main(String[] args) { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + System.out.println(s.peek()); + } +} diff --git a/group07/752262774/.gitignore b/group07/752262774/.gitignore new file mode 100644 index 0000000000..8d9372e204 --- /dev/null +++ b/group07/752262774/.gitignore @@ -0,0 +1,23 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/bin/ diff --git a/group07/752262774/2.26/coding/src/main/java/ArrayList.java b/group07/752262774/2.26/coding/src/main/java/ArrayList.java new file mode 100644 index 0000000000..a7b0f43aed --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/ArrayList.java @@ -0,0 +1,114 @@ +package main.java; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/21. + */ +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[10]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = new Object[0]; + } else { + throw new IllegalArgumentException("Illegal Capacity: "+ + initialCapacity); + } + } + + public void add(Object o) { + judegGrow(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == size) { + add(o); + }else { + judegGrow(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object o = elementData[index]; + + int move = size - 1 -index; + if(move > 0) { + System.arraycopy(elementData, index+1, elementData, index, move); + } + elementData[--size] = null; + + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrrayListIterator(this); + } + + private class ArrrayListIterator implements Iterator { + + ArrayList arrayList; + + int pos; + + private ArrrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + return pos != arrayList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return elementData[i]; + }else { + throw new NoSuchElementException(); + } + } + } + + private void judegGrow() { + if(size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + 1); + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java b/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java new file mode 100644 index 0000000000..cf8ade9193 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode(BinaryTreeNode left, Object o, BinaryTreeNode right) { + setData(o); + setLeft(left); + setRight(right); + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/Iterator.java b/group07/752262774/2.26/coding/src/main/java/Iterator.java new file mode 100644 index 0000000000..74a0b35573 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Iterator.java @@ -0,0 +1,12 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public interface Iterator { + + public boolean hasNext(); + + public Object next(); +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/LinkedList.java b/group07/752262774/2.26/coding/src/main/java/LinkedList.java new file mode 100644 index 0000000000..4f37c1a31c --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/LinkedList.java @@ -0,0 +1,210 @@ +package main.java; + + +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/23. + */ +public class LinkedList implements List{ + + private int size; + + private Node first; + + private Node last; + + public LinkedList() { + this.first = null; + this.last =null; + } + + public void add(Object o) { + Node l = this.last; + Node newNode = new Node(l, o, null); + this.last = newNode; + if(null == l) { + this.first = newNode; + }else { + l.next = newNode; + } + this.size++; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == this.size) { + this.add(o); + }else { + Node target = targetNode(index); + Node before = target.prev; + Node newNode = new Node(before, o, target); + target.prev = newNode; + if(null == before) { + this.first = newNode; + }else { + before.next = newNode; + } + this.size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return targetNode(index).data; + } + + public Object remove(int index) { + rangeCheck(index); + Node target = targetNode(index); + Node before = target.prev; + Node after = target.next; + Object o = target.data; + + if(null == before) { + this.first = null; + }else { + before.next = after; + target.prev = null; + } + + if(null == after) { + this.last = before; + }else { + after.prev = before; + target.next = null; + } + target.data = null; + this.size--; + + return o; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = first; + Node newNode = new Node(null, o, node); + this.first = newNode; + if(null == node) { + this.last = newNode; + }else { + node.prev = newNode; + } + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = first; + if (node == null) + throw new NoSuchElementException(); + + first = node.next; + Object o = node.data; + if(null == node.next) { + this.last = null; + }else { + first.prev = null; + } + node.data = null; + node.next = null; + this.size--; + return o; + } + + public Object removeLast() { + Node node = last; + if (node == null) + throw new NoSuchElementException(); + + last = node.prev; + Object o = node.data; + if(null == node.prev) { + this.first = null; + }else { + last.next = null; + } + node.data = null; + node.prev = null; + this.size--; + return o; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + + LinkedList linkedList; + + int pos; + + private LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return pos != linkedList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return linkedList.get(i); + }else { + throw new NoSuchElementException(); + } + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } + + private Node targetNode(int index) { + //由index值在链表的前半部分还是后半部分,决定是从前向后,还是从后向前查找。 + Node target = new Node(); + if(index < (this.size >> 1)) { + target = this.first; + for(int i=0; iindex; i--) { + target = target.prev; + } + } + return target; + } + + private static class Node{ + Object data; + Node next; + Node prev; + + Node() { + } + + Node(Node prev, Object o, Node next) { + this.data = o; + this.next = next; + this.prev = prev; + } + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/List.java b/group07/752262774/2.26/coding/src/main/java/List.java new file mode 100644 index 0000000000..0f1d979332 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/List.java @@ -0,0 +1,18 @@ +package main.java; + +/** + * Created by yrs on 2017/2/23. + */ +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/group07/752262774/2.26/coding/src/main/java/Queue.java b/group07/752262774/2.26/coding/src/main/java/Queue.java new file mode 100644 index 0000000000..59a120f51c --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Queue.java @@ -0,0 +1,27 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/java/Stack.java b/group07/752262774/2.26/coding/src/main/java/Stack.java new file mode 100644 index 0000000000..efaa2498b9 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/java/Stack.java @@ -0,0 +1,39 @@ +package main.java; + +/** + * Created by yrs on 2017/2/25. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object o = elementData.remove(elementData.size()-1); + return o; + } + + public Object peek() { + Object o = elementData.get(elementData.size() - 1); + return o; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public static void main(String [] args) { + Stack stack = new Stack(); + stack.push(1); + System.out.println(stack.size() + " " + stack.peek() + " " + stack.pop() + " " + stack.isEmpty()); + + } + +} + \ No newline at end of file diff --git a/group07/752262774/2.26/coding/src/main/test/test.java b/group07/752262774/2.26/coding/src/main/test/test.java new file mode 100644 index 0000000000..b32a0b6406 --- /dev/null +++ b/group07/752262774/2.26/coding/src/main/test/test.java @@ -0,0 +1,60 @@ +package main.test; + + +import javax.swing.tree.TreeNode; +import java.util.*; + +/** + * Created by yrs on 2017/2/21. + */ +public class test { + public static void main(String [] args) { + ArrayList list = new ArrayList(4); + list.add(9); + System.out.println(list); + list.add(1,3); +// list.add(2,3); //error IndexOutOfBoundsException + list.remove(1); + System.out.println(list.size()); + + Object[] target = new Object[0]; + System.out.println(target); + Object[] EMPTY_ELEMENTDATA = {}; + System.out.println(EMPTY_ELEMENTDATA); + + + //LinkedList + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + System.out.println(linkedList.get(0)); + linkedList.add(1,3); + System.out.println(linkedList.size()); + System.out.println(3 >> 1); + + for(int i=0; i<1; i++) { + System.out.println("dd"); + } + + Stack stack = new Stack(); + + Queue queue; + TreeNode treeNode; + + List lstint = new ArrayList(); + lstint.add(1); + lstint.add(2); + lstint.add(3); + + // Iterator遍历一 + Iterator iterator = lstint.iterator(); + iterator.hasNext(); + while (iterator.hasNext()) + { + int i = (Integer) iterator.next(); + System.out.println(i); + } + + + } +} + \ No newline at end of file diff --git "a/group07/752262774/git\345\221\275\344\273\244.txt" "b/group07/752262774/git\345\221\275\344\273\244.txt" deleted file mode 100644 index 8bcf2ffa0f..0000000000 --- "a/group07/752262774/git\345\221\275\344\273\244.txt" +++ /dev/null @@ -1,11 +0,0 @@ -��װgit gui��������git bash���� -1.��¡ -git clone git@github.com:leijing1992/coding2017.git -2.�����޸ĵ��ļ� -git add -A -3.�ύ���ݴ��� -git commit -m "�ύ***����" -4.����master -git pull origin master -5.�ύ��master -git push origin master \ No newline at end of file diff --git a/group07/764189149/src/firstHomeWork/util/Stack.java b/group07/764189149/src/firstHomeWork/util/Stack.java index 00dc784170..a217fd0c74 100644 --- a/group07/764189149/src/firstHomeWork/util/Stack.java +++ b/group07/764189149/src/firstHomeWork/util/Stack.java @@ -1,23 +1,22 @@ package firstHomeWork.util; -import java.util.Queue; - -public class Stack { - private ArrayList elementData = new ArrayList(); - public void push(Object o){ +public class Stack { + private ArrayList elementData = new ArrayList(); + public void push(E e){ + elementData.add(e); } public Object pop(){ - return null; + return elementData.remove(elementData.size() - 1); } public Object peek(){ - return null; + return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ - return false; + return elementData.isEmpty(); } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group07/770164810/src/com/coding/basic/ArrayList.java b/group07/770164810/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..265f4398ed --- /dev/null +++ b/group07/770164810/src/com/coding/basic/ArrayList.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private int modifyNum = 0; + + private Object[] elementData = new Object[100]; + public void add(Object o){ + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + for(int i=size;i>index;i--) + { + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object oj=elementData[index]; + for(int i=index;i= size || index < 0) throw new IndexOutOfBoundsException(); + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node n = first; + first = new Node(o); + if(n != null){ + first.next = n; + }else{ + last = first; + } + size++; + } + + public void addLast(Object o) { + Node n = new Node(o); + if(last != null){ + last.next = n; + }else{ + first = n; + } + last = n; + size++; + } + + public Object removeFirst() { + return null; + } + + public Object removeLast() { + return null; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data){ + this.data = data; + } + + public Node(){} + + } + + public static void main(String[] args) { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + l.add(4); + l.add(2, 5); + l.remove(0); + + for(int i=0;i elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); - } - elementData[size++] = o; - } - public void add(int index, Object o){ - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException(); - } - - if (size + 1 > elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); - } - - System.arraycopy(elementData, index, elementData, index+1, size-index); - elementData[index] = o; - size++; - } - - public Object get(int index){ - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } - - return elementData[index]; - } - - public Object remove(int index){ - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } - - Object old = elementData[index]; - size--; - System.arraycopy(elementData, index+1, elementData, index, size-index); - elementData[size] = null; - - return old; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return new Itr(); - } - - private class Itr implements Iterator { - - int cursor = 0; - - @Override - public boolean hasNext() { - return (cursor < size); - } - - @Override - public Object next() { - if (cursor < 0 || cursor >= size) { - throw new NoSuchElementException(); - } - return elementData[cursor++]; - } - - } - -} +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + elementData[size++] = o; + } + public void add(int index, Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Object old = elementData[index]; + size--; + System.arraycopy(elementData, index+1, elementData, index, size-index); + elementData[size] = null; + + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator { + + int cursor = 0; + + @Override + public boolean hasNext() { + return (cursor < size); + } + + @Override + public Object next() { + if (cursor < 0 || cursor >= size) { + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java index af940807e6..9db8eec800 100644 --- a/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java +++ b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java @@ -1,47 +1,47 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - if (data == null || ((Integer)data).intValue() == ((Integer)o).intValue()) { - data = o; - return this; - } - else if (((Integer)o).intValue() < ((Integer)data).intValue()) { - if (left == null) { - left = new BinaryTreeNode(); - } - return left.insert(o); - } - else { - if (right == null) { - right = new BinaryTreeNode(); - } - return right.insert(o); - } - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if (data == null || ((Integer)data).intValue() == ((Integer)o).intValue()) { + data = o; + return this; + } + else if (((Integer)o).intValue() < ((Integer)data).intValue()) { + if (left == null) { + left = new BinaryTreeNode(); + } + return left.insert(o); + } + else { + if (right == null) { + right = new BinaryTreeNode(); + } + return right.insert(o); + } + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Iterator.java b/group08/619057560/2-26/code/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Iterator.java +++ b/group08/619057560/2-26/code/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/LinkedList.java b/group08/619057560/2-26/code/com/coding/basic/LinkedList.java index d2f1422cfb..38a62b2f66 100644 --- a/group08/619057560/2-26/code/com/coding/basic/LinkedList.java +++ b/group08/619057560/2-26/code/com/coding/basic/LinkedList.java @@ -1,137 +1,137 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - Node pNewNode = new Node(); - pNewNode.data = o; - - Node pNode = head; - - if (head == null) { - head = pNewNode; - return; - } - - while (pNode.next != null) { - pNode = pNode.next; - } - - pNode.next = pNewNode; - } - - public void add(int index , Object o){ - if (index < 0 && index > size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNewNode = new Node(); - pNewNode.data = o; - - if (index == 0) { - pNewNode.next = head; - head = pNewNode; - return; - } - - Node pNode = head; - while (--index > 0) { - pNode = pNode.next; - } - pNewNode.next = pNode.next; - pNode.next = pNewNode; - } - - public Object get(int index){ - if (index < 0 && index >= size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNode = head; - while (index-- > 0) { - pNode = pNode.next; - } - - return pNode.data; - } - - public Object remove(int index){ - if (index < 0 && index >= size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNode = head; - if (index == 0) { - head = head.next; - return pNode.data; - } - - while (--index > 0) { - pNode = pNode.next; - } - Node pTargetNode = pNode.next; - pNode.next = pTargetNode.next; - - return pTargetNode.data; - } - - public int size(){ - Node pNode = head; - int num = 0; - while (pNode != null) { - pNode = pNode.next; - num++; - } - return num; - } - - public void addFirst(Object o){ - add(0, o); - } - - public void addLast(Object o){ - add(o); - } - - public Object removeFirst(){ - if (head == null) { - throw new NoSuchElementException(); - } - return remove(0); - } - - public Object removeLast(){ - if (head == null) { - throw new NoSuchElementException(); - } - - Node pNode = head; - Node pPrevNode = null; - while (pNode.next != null) { - pPrevNode = pNode; - pNode = pNode.next; - } - if (pPrevNode != null) { - pPrevNode.next = pNode.next; - } - else { - head = null; - } - return pNode.data; - } - - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } -} +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + Node pNewNode = new Node(); + pNewNode.data = o; + + Node pNode = head; + + if (head == null) { + head = pNewNode; + return; + } + + while (pNode.next != null) { + pNode = pNode.next; + } + + pNode.next = pNewNode; + } + + public void add(int index , Object o){ + if (index < 0 && index > size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNewNode = new Node(); + pNewNode.data = o; + + if (index == 0) { + pNewNode.next = head; + head = pNewNode; + return; + } + + Node pNode = head; + while (--index > 0) { + pNode = pNode.next; + } + pNewNode.next = pNode.next; + pNode.next = pNewNode; + } + + public Object get(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + while (index-- > 0) { + pNode = pNode.next; + } + + return pNode.data; + } + + public Object remove(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + if (index == 0) { + head = head.next; + return pNode.data; + } + + while (--index > 0) { + pNode = pNode.next; + } + Node pTargetNode = pNode.next; + pNode.next = pTargetNode.next; + + return pTargetNode.data; + } + + public int size(){ + Node pNode = head; + int num = 0; + while (pNode != null) { + pNode = pNode.next; + num++; + } + return num; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + return remove(0); + } + + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + + Node pNode = head; + Node pPrevNode = null; + while (pNode.next != null) { + pPrevNode = pNode; + pNode = pNode.next; + } + if (pPrevNode != null) { + pPrevNode.next = pNode.next; + } + else { + head = null; + } + return pNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/List.java b/group08/619057560/2-26/code/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/group08/619057560/2-26/code/com/coding/basic/List.java +++ b/group08/619057560/2-26/code/com/coding/basic/List.java @@ -1,9 +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(); -} +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(); +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Queue.java b/group08/619057560/2-26/code/com/coding/basic/Queue.java index 67a080f408..1d240b6ec8 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Queue.java +++ b/group08/619057560/2-26/code/com/coding/basic/Queue.java @@ -1,22 +1,22 @@ -package com.coding.basic; - -public class Queue { - - private LinkedList queueList = new LinkedList(); - - public void enQueue(Object o){ - queueList.addFirst(o); - } - - public Object deQueue(){ - return queueList.removeLast(); - } - - public boolean isEmpty(){ - return queueList.size() == 0; - } - - public int size(){ - return queueList.size(); - } -} +package com.coding.basic; + +public class Queue { + + private LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.addFirst(o); + } + + public Object deQueue(){ + return queueList.removeLast(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Stack.java b/group08/619057560/2-26/code/com/coding/basic/Stack.java index 481c88bed7..03d5fa327c 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Stack.java +++ b/group08/619057560/2-26/code/com/coding/basic/Stack.java @@ -1,23 +1,23 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - 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(); - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + 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(); + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java index 1a7cc5a361..d5cfc1ffa3 100644 --- a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java +++ b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java @@ -1,3 +1,4 @@ +<<<<<<< HEAD package com.coding.basic; public class ArrayList implements List { @@ -109,3 +110,116 @@ public void remove() { } } } +======= +package com.coding.basic; + +public class ArrayList implements List { + + private int size; + + private E[] data; + + public void add(E e){ + add(size, e); + } + + public ArrayList() { + clear(); + } + + public ArrayList(int capacity) { + clear(); + ensureCapacity(capacity); + } + + public void add(int index, E e){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + if (data.length == size) { + ensureCapacity(size * 2 + 1); + } + for (int i = size++; i > index; --i) { + data[i] = data[i - 1]; + } + data[index] = e; + } + + public E get(int index){ + return data[index]; + } + + public E remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + E copy = data[index]; + --size; + for (int i = index; i < size; ++i) { + data[i] = data[i + 1]; + } + return copy; + } + + public boolean contains(E e) { + for (int i = 0; i < size; ++i) { + if (data[i] == e) { + return true; + } + } + return false; + } + + public void clear() { + size = 0; + data = (E[]) new Object[0]; + } + + public int size(){ + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public void ensureCapacity(int capacity) { + E[] newData = (E[]) new Object[capacity]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + public void trimToSize() { + E[] newData = (E[]) new Object[size]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + private class ArrayListIterator implements Iterator { + int current = 0; + + public boolean hasNext() { + return current < size; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return data[current++]; + } + + public void remove() { + ArrayList.this.remove(current); + } + } +} +>>>>>>> master diff --git a/group08/729770920/2-26/src/com/coding/basic/Iterator.java b/group08/729770920/2-26/src/com/coding/basic/Iterator.java index f0e2eddde8..f432b11910 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Iterator.java +++ b/group08/729770920/2-26/src/com/coding/basic/Iterator.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface Iterator { - boolean hasNext(); - - E next(); - - void remove(); -} +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/LinkedList.java b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java index 899e0be08e..6a09c84aae 100644 --- a/group08/729770920/2-26/src/com/coding/basic/LinkedList.java +++ b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java @@ -1,164 +1,164 @@ -package com.coding.basic; - -public class LinkedList implements List { - private int size = 0; - private Node head = new Node<>(); - private Node tail = new Node<>(); - - public LinkedList() { - head.next = tail; - tail.prev = head; - } - - public void add(E e) { - addLast(e); - } - - public void add(int index, E e) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head; - for (int i = 0, num = index; i < num; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index; i < num; ++i) { - cursor = cursor.prev; - } - } - cursor.next = cursor.next.prev = new Node(e, cursor, cursor.next); - ++size; - } - - public E get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head.next; - for (int i = 0; i < index; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index-1; i < num; ++i) { - cursor = cursor.prev; - } - } - return cursor.data; - } - - public E remove(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head.next; - for (int i = 0; i < index; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index-1; i < num; ++i) { - cursor = cursor.prev; - } - } - cursor.prev.next = cursor.next; - cursor.next.prev = cursor.prev; - --size; - return cursor.data; - } - - public int size() { - return size; - } - - public boolean isEmpty() { - return size == 0; - } - - public void addFirst(E e) { - add(0, e); - } - - public void addLast(E e) { - add(size, e); - } - - public E removeFirst() { - return remove(0); - } - - public E removeLast() { - return remove(size-1); - } - - public void clear() { - while (!isEmpty()) { - removeFirst(); - } - } - - public boolean contains(E e) { - Iterator it = this.iterator(); - while (it.hasNext()) { - if (it.next() == e) { - return true; - } - } - return false; - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private static class Node { - E data = null; - Node prev = null; - Node next = null; - - public Node() { - } - - public Node(E e, Node p, Node n) { - data = e; - prev = p; - next = n; - } - } - - private class LinkedListIterator implements Iterator { - Node currentNode = head.next; - - public boolean hasNext() { - return currentNode != tail; - } - - public E next() { - if (!hasNext()) { - throw new java.util.NoSuchElementException(); - } - E data = currentNode.data; - currentNode = currentNode.next; - return data; - } - - public void remove() { - if (!hasNext()) { - throw new java.util.NoSuchElementException(); - } - Node nextNode = currentNode.next; - currentNode.next.prev = currentNode.prev; - currentNode.prev.next = currentNode.next; - currentNode = nextNode; - --size; - } - } -} +package com.coding.basic; + +public class LinkedList implements List { + private int size = 0; + private Node head = new Node<>(); + private Node tail = new Node<>(); + + public LinkedList() { + head.next = tail; + tail.prev = head; + } + + public void add(E e) { + addLast(e); + } + + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head; + for (int i = 0, num = index; i < num; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.next = cursor.next.prev = new Node(e, cursor, cursor.next); + ++size; + } + + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + return cursor.data; + } + + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.prev.next = cursor.next; + cursor.next.prev = cursor.prev; + --size; + return cursor.data; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + add(0, e); + } + + public void addLast(E e) { + add(size, e); + } + + public E removeFirst() { + return remove(0); + } + + public E removeLast() { + return remove(size-1); + } + + public void clear() { + while (!isEmpty()) { + removeFirst(); + } + } + + public boolean contains(E e) { + Iterator it = this.iterator(); + while (it.hasNext()) { + if (it.next() == e) { + return true; + } + } + return false; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + E data = null; + Node prev = null; + Node next = null; + + public Node() { + } + + public Node(E e, Node p, Node n) { + data = e; + prev = p; + next = n; + } + } + + private class LinkedListIterator implements Iterator { + Node currentNode = head.next; + + public boolean hasNext() { + return currentNode != tail; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + E data = currentNode.data; + currentNode = currentNode.next; + return data; + } + + public void remove() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Node nextNode = currentNode.next; + currentNode.next.prev = currentNode.prev; + currentNode.prev.next = currentNode.next; + currentNode = nextNode; + --size; + } + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/List.java b/group08/729770920/2-26/src/com/coding/basic/List.java index babed5a71f..de1390d243 100644 --- a/group08/729770920/2-26/src/com/coding/basic/List.java +++ b/group08/729770920/2-26/src/com/coding/basic/List.java @@ -1,19 +1,19 @@ -package com.coding.basic; - -public interface List { - void add(E e); - - void add(int index, E e); - - void clear(); - - E get(int index); - - E remove(int index); - - int size(); - - boolean contains(E e); - - Iterator iterator(); -} +package com.coding.basic; + +public interface List { + void add(E e); + + void add(int index, E e); + + void clear(); + + E get(int index); + + E remove(int index); + + int size(); + + boolean contains(E e); + + Iterator iterator(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Queue.java b/group08/729770920/2-26/src/com/coding/basic/Queue.java index a660ad6729..c53fc8ed0a 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Queue.java +++ b/group08/729770920/2-26/src/com/coding/basic/Queue.java @@ -1,21 +1,21 @@ -package com.coding.basic; - -public class Queue { - private LinkedList data = new LinkedList<>(); - - public void enQueue(E e){ - data.addFirst(e); - } - - public Object deQueue() { - return data.removeLast(); - } - - public boolean isEmpty() { - return data.size() == 0; - } - - public int size(){ - return data.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList data = new LinkedList<>(); + + public void enQueue(E e){ + data.addFirst(e); + } + + public Object deQueue() { + return data.removeLast(); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size(){ + return data.size(); + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Stack.java b/group08/729770920/2-26/src/com/coding/basic/Stack.java index c7e7773de5..950b09e039 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Stack.java +++ b/group08/729770920/2-26/src/com/coding/basic/Stack.java @@ -1,25 +1,25 @@ -package com.coding.basic; - -public class Stack { - private LinkedList data = new LinkedList<>(); - - public void push(E e) { - data.addFirst(e); - } - - public Object pop() { - return data.removeFirst(); - } - - public Object peek() { - return data.get(0); - } - - public boolean isEmpty() { - return data.size() == 0; - } - - public int size() { - return data.size(); - } -} +package com.coding.basic; + +public class Stack { + private LinkedList data = new LinkedList<>(); + + public void push(E e) { + data.addFirst(e); + } + + public Object pop() { + return data.removeFirst(); + } + + public Object peek() { + return data.get(0); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size() { + return data.size(); + } +} diff --git a/group08/group08.md b/group08/group08.md index d3f5a12faa..8b13789179 100644 --- a/group08/group08.md +++ b/group08/group08.md @@ -1 +1 @@ - + 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 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"); + } +} 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 new file mode 100644 index 0000000000..c256ab61aa --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java @@ -0,0 +1,174 @@ +/** +* @Title: ArrayList.java +* @Description: ArrayList的实现 +* @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..062894ec95 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java @@ -0,0 +1,96 @@ +/** +* @Title: BinaryTree.java +* @Description: 二叉排序树的实现 +* @author glorychou +* @date 2017年2月25日 下午10:22:03 +*/ +package per.zyf.bds; + +/** + * @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; + + size++; + 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); + } + + /** + * @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; + 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..07eb7d7d46 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java @@ -0,0 +1,252 @@ +/** +* @Title: LinkedList.java +* @Description: 双向链表的实现 +* @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); + E e = p.item; + + // 所需删除节点的前面有节点,则改变前一节点的下一跳 + if(p.prev != null) + p.prev.next = p.next; + // 所需删除节点的后面有节点,则改变后一节点的上一跳 + if(p.next != null) + p.next.prev = p.prev; + + // 清空数据 + p.prev = null; + p.item = null; + p.next = null; + + size--; + + return e; + } + + /** + * @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..d8edbaabce --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/List.java @@ -0,0 +1,55 @@ +/** +* @Title: List.java +* @Description: List接口的实现 +* @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..39c66972a0 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Queue.java @@ -0,0 +1,59 @@ +/** +* @Title: Queue.java +* @Description: 队列的实现 +* @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..8e3f70eb51 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Stack.java @@ -0,0 +1,71 @@ +/** +* @Title: Stack.java +* @Description: 栈的实现 +* @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; + } +} 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/group07/562247675/homework/homework-0226/src/test/resources/readme.md b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep similarity index 100% rename from group07/562247675/homework/homework-0226/src/test/resources/readme.md rename to group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep 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 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 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)); + } +} 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 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 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/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 { + + } +} 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 diff --git a/group09/group09.md b/group09/group09.md index d3f5a12faa..8b13789179 100644 --- a/group09/group09.md +++ b/group09/group09.md @@ -1 +1 @@ - + diff --git a/group10/group10.md b/group10/group10.md index d3f5a12faa..8b13789179 100644 --- a/group10/group10.md +++ b/group10/group10.md @@ -1 +1 @@ - + diff --git a/group11/group11.md b/group11/group11.md index d3f5a12faa..8b13789179 100644 --- a/group11/group11.md +++ b/group11/group11.md @@ -1 +1 @@ - + diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java index 5de89da950..a30b209281 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -1,94 +1,94 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - /*扩容因子*/ - private static final int GENE = 10; - - private Object[] elementData = new Object[10]; - /*扩容引用*/ - private Object[] newElementData; - - public void add(Object o){ - grow(); - elementData[size] = o; - size ++; - } - public void add(int index, Object o){ - - if(index>size){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - grow(); - if(indexsize){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - return elementData[index]; - } - - public Object remove(int index){ - - Object o = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); - size --; - return o; - } - - public int size(){ - return size; - } - - /** - * 扩容,扩容因子为10 - */ - private void grow(){ - - if(size>=elementData.length){//长度不够需要扩容 - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; - } - } - - - public Iterator iterator(){ - - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != ArrayList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= ArrayList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return ArrayList.this.elementData[i]; - } - - } -} +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + /*扩容因子*/ + private static final int GENE = 10; + + private Object[] elementData = new Object[10]; + /*扩容引用*/ + private Object[] newElementData; + + public void add(Object o){ + grow(); + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + grow(); + if(indexsize){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + if(size>=elementData.length){//长度不够需要扩容 + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java index e5fae50203..3449517197 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java @@ -1,64 +1,64 @@ -package com.coding.basic; - -public class BinaryTree { - - //根节点 - private BinaryTreeNode root; - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public > BinaryTreeNode insert(T o){ - - BinaryTreeNode treeNode = new BinaryTreeNode(); - treeNode.setData(o); - if(root == null){ - root = treeNode; - }else{ - BinaryTreeNode currentNode = root; - BinaryTreeNode parent; - while(true){ - parent = currentNode; - if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 - currentNode = currentNode.getLeft(); - if(currentNode == null){ - parent.setLeft(treeNode); - treeNode.setParent(parent); - break; - } - }else{//向右放 - currentNode = currentNode.getRight(); - if(currentNode == null){ - parent.setRight(treeNode); - treeNode.setParent(parent); - break; - } - } - } - } - return treeNode; - } - - /** - * 先序遍历 - * @param node - * @return - */ - public List traversalBefore(BinaryTreeNode node){ - //所有数据集合 - List datas = new ArrayList(); - return traversal(node,datas); - } - private List traversal(BinaryTreeNode node,List datas){ - - if(node !=null){ - datas.add(node.getData()); - traversal(node.getLeft(),datas); - traversal(node.getRight(),datas); - } - return datas; - } - - public BinaryTreeNode getRoot() { - return root; - } - -} +package com.coding.basic; + +public class BinaryTree { + + //根节点 + private BinaryTreeNode root; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public > BinaryTreeNode insert(T o){ + + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.setData(o); + if(root == null){ + root = treeNode; + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parent; + while(true){ + parent = currentNode; + if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parent.setLeft(treeNode); + treeNode.setParent(parent); + break; + } + }else{//向右放 + currentNode = currentNode.getRight(); + if(currentNode == null){ + parent.setRight(treeNode); + treeNode.setParent(parent); + break; + } + } + } + } + return treeNode; + } + + /** + * 先序遍历 + * @param node + * @return + */ + public List traversalBefore(BinaryTreeNode node){ + //所有数据集合 + List datas = new ArrayList(); + return traversal(node,datas); + } + private List traversal(BinaryTreeNode node,List datas){ + + if(node !=null){ + datas.add(node.getData()); + traversal(node.getLeft(),datas); + traversal(node.getRight(),datas); + } + return datas; + } + + public BinaryTreeNode getRoot() { + return root; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java index 557728a02a..a8e6b66edd 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java @@ -1,37 +1,37 @@ -package com.coding.basic; -public class BinaryTreeNode { - - private Object data; - //父节点 - private BinaryTreeNode parent; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode getParent() { - return parent; - } - public void setParent(BinaryTreeNode parent) { - this.parent = parent; - } -} +package com.coding.basic; +public class BinaryTreeNode { + + private Object data; + //父节点 + private BinaryTreeNode parent; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode getParent() { + return parent; + } + public void setParent(BinaryTreeNode parent) { + this.parent = parent; + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java index 0f343ed895..1357cf17cf 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java @@ -1,58 +1,58 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.BinaryTree; -import com.coding.basic.BinaryTreeNode; -import com.coding.basic.List; - -public class BinaryTreeTest { - - BinaryTree tree ; - - @Before - public void setup() { - - tree = new BinaryTree(); - Assert.assertEquals(tree.getRoot(), null); - tree.insert(5); - tree.insert(2); - tree.insert(7); - tree.insert(1); - tree.insert(6); - } - @Test - public void insert(){ - - BinaryTreeNode node = tree.insert(4); - Assert.assertEquals(node.getParent().getData(), 2); - Assert.assertEquals(node.getParent().getLeft().getData(), 1); - - BinaryTreeNode node2 = tree.insert(8); - Assert.assertEquals(node2.getParent().getData(), 7); - Assert.assertEquals(node2.getParent().getLeft().getData(), 6); - } - - @Test - public void traversal(){ - - insert(); - //以根节点为起点先序遍历 - List treeList = tree.traversalBefore(tree.getRoot()); - //expected value - int[] exValue = {5,2,1,4,7,6,8}; - for (int i = 0; i < exValue.length; i++) { - Assert.assertEquals(treeList.get(i),exValue[i]); - } - - //以数据2位起点先序遍历 - List treeList2 = tree.traversalBefore(tree.getRoot().getLeft()); - //expected value - int[] exValue2 = {2,1,4}; - for (int i = 0; i < exValue2.length; i++) { - Assert.assertEquals(treeList2.get(i),exValue2[i]); - } - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.List; + +public class BinaryTreeTest { + + BinaryTree tree ; + + @Before + public void setup() { + + tree = new BinaryTree(); + Assert.assertEquals(tree.getRoot(), null); + tree.insert(5); + tree.insert(2); + tree.insert(7); + tree.insert(1); + tree.insert(6); + } + @Test + public void insert(){ + + BinaryTreeNode node = tree.insert(4); + Assert.assertEquals(node.getParent().getData(), 2); + Assert.assertEquals(node.getParent().getLeft().getData(), 1); + + BinaryTreeNode node2 = tree.insert(8); + Assert.assertEquals(node2.getParent().getData(), 7); + Assert.assertEquals(node2.getParent().getLeft().getData(), 6); + } + + @Test + public void traversal(){ + + insert(); + //以根节点为起点先序遍历 + List treeList = tree.traversalBefore(tree.getRoot()); + //expected value + int[] exValue = {5,2,1,4,7,6,8}; + for (int i = 0; i < exValue.length; i++) { + Assert.assertEquals(treeList.get(i),exValue[i]); + } + + //以数据2位起点先序遍历 + List treeList2 = tree.traversalBefore(tree.getRoot().getLeft()); + //expected value + int[] exValue2 = {2,1,4}; + for (int i = 0; i < exValue2.length; i++) { + Assert.assertEquals(treeList2.get(i),exValue2[i]); + } + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java index a55b2d5a3f..3a5ff822ad 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java @@ -1,109 +1,109 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Iterator; -import com.coding.basic.LinkedList; - -public class LinkedListTest { - - LinkedList ls ; - @Before - public void setup() { - ls = new LinkedList(); - } - - /** - * 测试一个参数的add方法 - * ArrayList当数据超过10时进行第一次扩容 - */ - @Test - public void add(){ - - ls.add(3); - ls.add("a"); - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.size(), 12); - Assert.assertEquals(ls.get(1), "a"); - } - - /** - * 两个参数的add方法 - */ - @Test//(expected = IndexOutOfBoundsException.class) - public void add4ToPramter(){ - - ls.add(0, 0); - ls.add(1,1); - ls.add(2, 2); - ls.add(3,3); - for (int i = 0; i < 10; i++) { - ls.add(3,i); - } - Assert.assertEquals(ls.size(), 14); - Assert.assertEquals(ls.get(3), 9); - Assert.assertEquals(ls.get(13), 3); - //打开下面操作抛出异常 - //ls.add(15, "a"); - } - - /** - * get(i) - */ - @Test//(expected = IndexOutOfBoundsException.class) - public void get(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - - Assert.assertEquals(ls.get(9), 9); - //打开下面操作抛出异常 - //ls.get(12); - } - - @Test - public void remove(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.remove(5),5); - Assert.assertEquals(ls.size(),9); - Assert.assertEquals(ls.remove(8),9); - Assert.assertEquals(ls.size(),8); - } - - @Test - public void size(){ - - Assert.assertEquals(ls.size(),0); - ls.add("a"); - Assert.assertEquals(ls.size(),1); - ls.add(0,0); - Assert.assertEquals(ls.size(),2); - ls.remove(0); - Assert.assertEquals(ls.size(),1); - - } - - @Test//(expected = NoSuchElementException.class) - public void iterator(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Iterator it = ls.iterator(); - Assert.assertEquals(it.hasNext(),true); - for (int i = 0; i < 10; i++) { - it.next(); - } - Assert.assertEquals(it.hasNext(),false); - //打开下面操作抛出异常 - //it.next(); - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + LinkedList ls ; + @Before + public void setup() { + ls = new LinkedList(); + } + + /** + * 测试一个参数的add方法 + * ArrayList当数据超过10时进行第一次扩容 + */ + @Test + public void add(){ + + ls.add(3); + ls.add("a"); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.size(), 12); + Assert.assertEquals(ls.get(1), "a"); + } + + /** + * 两个参数的add方法 + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void add4ToPramter(){ + + ls.add(0, 0); + ls.add(1,1); + ls.add(2, 2); + ls.add(3,3); + for (int i = 0; i < 10; i++) { + ls.add(3,i); + } + Assert.assertEquals(ls.size(), 14); + Assert.assertEquals(ls.get(3), 9); + Assert.assertEquals(ls.get(13), 3); + //打开下面操作抛出异常 + //ls.add(15, "a"); + } + + /** + * get(i) + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void get(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + + Assert.assertEquals(ls.get(9), 9); + //打开下面操作抛出异常 + //ls.get(12); + } + + @Test + public void remove(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.remove(5),5); + Assert.assertEquals(ls.size(),9); + Assert.assertEquals(ls.remove(8),9); + Assert.assertEquals(ls.size(),8); + } + + @Test + public void size(){ + + Assert.assertEquals(ls.size(),0); + ls.add("a"); + Assert.assertEquals(ls.size(),1); + ls.add(0,0); + Assert.assertEquals(ls.size(),2); + ls.remove(0); + Assert.assertEquals(ls.size(),1); + + } + + @Test//(expected = NoSuchElementException.class) + public void iterator(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Iterator it = ls.iterator(); + Assert.assertEquals(it.hasNext(),true); + for (int i = 0; i < 10; i++) { + it.next(); + } + Assert.assertEquals(it.hasNext(),false); + //打开下面操作抛出异常 + //it.next(); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java index e688d9b41f..75af57b371 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java @@ -1,64 +1,64 @@ -package test.com.coding.basic; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Queue; - -public class QueueTest { - - Queue qe ; - - @Before - public void setup() { - qe = new Queue(); - for (int i = 0; i < 10; i++) { - qe.enQueue(i); - } - } - - @Test - public void enQueue(){ - - Assert.assertEquals(qe.size(), 10); - qe.enQueue("abcd"); - Assert.assertEquals(qe.size(), 11); - } - - @Test//(expected = IndexOutOfBoundsException.class) - public void deQueue(){ - - Assert.assertEquals(qe.size(), 10); - for (int i = 0; i < 10; i++) { - Assert.assertEquals(qe.deQueue(), i); - } - Assert.assertEquals(qe.size(), 0); - //打开下列语句与期望异常测试 - //qe.deQueue(); - } - - public void isEmpty(){ - - Assert.assertEquals(qe.isEmpty(),false); - for (int i = 0; i < 10; i++) { - qe.deQueue(); - } - Assert.assertEquals(qe.isEmpty(),true); - Queue qe1 = new Queue(); - Assert.assertEquals(qe1.isEmpty(), true); - } - - public void size(){ - - Assert.assertEquals(qe.size(),10); - qe.enQueue("lk"); - qe.enQueue('h'); - Assert.assertEquals(qe.size(),12); - for (int i = 0; i < 12; i++) { - qe.deQueue(); - } - Assert.assertEquals(qe.size(),0); - Queue qe1 = new Queue(); - Assert.assertEquals(qe1.size(), 0); - } -} +package test.com.coding.basic; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + Queue qe ; + + @Before + public void setup() { + qe = new Queue(); + for (int i = 0; i < 10; i++) { + qe.enQueue(i); + } + } + + @Test + public void enQueue(){ + + Assert.assertEquals(qe.size(), 10); + qe.enQueue("abcd"); + Assert.assertEquals(qe.size(), 11); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void deQueue(){ + + Assert.assertEquals(qe.size(), 10); + for (int i = 0; i < 10; i++) { + Assert.assertEquals(qe.deQueue(), i); + } + Assert.assertEquals(qe.size(), 0); + //打开下列语句与期望异常测试 + //qe.deQueue(); + } + + public void isEmpty(){ + + Assert.assertEquals(qe.isEmpty(),false); + for (int i = 0; i < 10; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.isEmpty(),true); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(qe.size(),10); + qe.enQueue("lk"); + qe.enQueue('h'); + Assert.assertEquals(qe.size(),12); + for (int i = 0; i < 12; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.size(),0); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.size(), 0); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java index a0875c8f3c..5d9fcd0f16 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java @@ -1,76 +1,76 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Stack; - -public class StackTest { - - Stack st ; - - @Before - public void setup() { - st = new Stack(); - for (int i = 0; i < 10; i++) { - st.push(i); - } - } - - @Test - public void push(){ - - Assert.assertEquals(st.size(), 10); - st.push(10); - st.push('a'); - Assert.assertEquals(st.size(), 12); - } - - @Test//(expected = IndexOutOfBoundsException.class) - public void pop(){ - - Assert.assertEquals(st.size(), 10); - for (int i = 9; i >= 0; i--) { - Assert.assertEquals(st.pop(), i); - } - //打开下列语句抛出期望异常 - //st.pop(); - } - - @Test - public void peek(){ - - Assert.assertEquals(st.size(), 10); - Assert.assertEquals(st.peek(), 9); - Assert.assertEquals(st.size(), 10); - } - - @Test - public void isEmpty(){ - - Assert.assertEquals(st.isEmpty(), false); - for (int i = 0; i < 10; i++) { - st.pop(); - } - Assert.assertEquals(st.isEmpty(), true); - Stack st1 = new Stack(); - Assert.assertEquals(st1.isEmpty(), true); - } - - public void size(){ - - Assert.assertEquals(st.size(),10); - st.push("lk"); - st.push('h'); - Assert.assertEquals(st.size(),12); - for (int i = 0; i < 12; i++) { - st.pop(); - } - Assert.assertEquals(st.size(),0); - st.peek(); - Assert.assertEquals(st.size(),0); - Stack st1 = new Stack(); - Assert.assertEquals(st1.size(), 0); - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + Stack st ; + + @Before + public void setup() { + st = new Stack(); + for (int i = 0; i < 10; i++) { + st.push(i); + } + } + + @Test + public void push(){ + + Assert.assertEquals(st.size(), 10); + st.push(10); + st.push('a'); + Assert.assertEquals(st.size(), 12); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void pop(){ + + Assert.assertEquals(st.size(), 10); + for (int i = 9; i >= 0; i--) { + Assert.assertEquals(st.pop(), i); + } + //打开下列语句抛出期望异常 + //st.pop(); + } + + @Test + public void peek(){ + + Assert.assertEquals(st.size(), 10); + Assert.assertEquals(st.peek(), 9); + Assert.assertEquals(st.size(), 10); + } + + @Test + public void isEmpty(){ + + Assert.assertEquals(st.isEmpty(), false); + for (int i = 0; i < 10; i++) { + st.pop(); + } + Assert.assertEquals(st.isEmpty(), true); + Stack st1 = new Stack(); + Assert.assertEquals(st1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(st.size(),10); + st.push("lk"); + st.push('h'); + Assert.assertEquals(st.size(),12); + for (int i = 0; i < 12; i++) { + st.pop(); + } + Assert.assertEquals(st.size(),0); + st.peek(); + Assert.assertEquals(st.size(),0); + Stack st1 = new Stack(); + Assert.assertEquals(st1.size(), 0); + } +} diff --git a/group12/251822722/ArrayList.java b/group12/251822722/ArrayList.java old mode 100755 new mode 100644 diff --git a/group12/251822722/BinaryTreeNode.java b/group12/251822722/BinaryTreeNode.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Iterator.java b/group12/251822722/Iterator.java old mode 100755 new mode 100644 diff --git a/group12/251822722/LinkedList.java b/group12/251822722/LinkedList.java old mode 100755 new mode 100644 diff --git a/group12/251822722/List.java b/group12/251822722/List.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Queue.java b/group12/251822722/Queue.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Stack.java b/group12/251822722/Stack.java old mode 100755 new mode 100644 diff --git a/group12/377401843/learning_1/.gitignore b/group12/377401843/learning_1/.gitignore index 9c47ca7e58..b12088665a 100644 --- a/group12/377401843/learning_1/.gitignore +++ b/group12/377401843/learning_1/.gitignore @@ -1,3 +1,3 @@ -/bin/ +/bin/ /.project /.classpath diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java index a1ee0ee339..880af45da8 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java @@ -1,171 +1,171 @@ -package com.guodong.datastructure; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - /** - * 按下标顺序新增元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - ensureCapacityInternal(size + 1); - elementData[size] = o; - size++; - } - - /** - * 在指定下标位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkRangeForAdd(index); - - ensureCapacityInternal(size + 1); - - System.arraycopy(elementData, index, elementData, index + 1, size - index); - - elementData[index] = o; - size++; - } - - /** - * 根据下标获取列表数据 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkRangeForGetOrRemove(index); - - return elementData[index]; - } - - /** - * 根据下标移除元素,并返回移除的元素值 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkRangeForGetOrRemove(index); - - Object oldValue = elementData[index]; - - System.arraycopy(elementData, index + 1, elementData, index, size - index -1); - - size--; - elementData[size] = null; - - return oldValue; - } - - /** - * 获得列表长度 - * - * @Method size - * @return - * @see com.guodong.datastructure.List#size() - */ - public int size() { - return size; - } - - /** - * 获取ArrayList的迭代器 - * - * @MethodName iterator - * @author zhaogd - * @date 2017年2月21日 下午8:19:28 - * @return - */ - public Iterator iterator() { - return new ArrayListIterator(); - } - - /** - * 确保数组容量足够,如果不够则扩充数组 - * - * @MethodName ensureCapacityInternal - * @author zhaogd - * @date 2017年2月21日 下午5:06:46 - * @param minCapacity - */ - private void ensureCapacityInternal(int minCapacity) { - if(minCapacity > elementData.length){ - grow(minCapacity); - } - } - - /** - * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 - * - * @MethodName grow - * @author zhaogd - * @date 2017年2月21日 下午5:20:55 - * @param minCapacity - */ - private void grow(int minCapacity) { - minCapacity = elementData.length + elementData.length / 2; - elementData = Arrays.copyOf(elementData, minCapacity); - } - - /** - * 检查Add方法的下标范围是否合法 - * - * @MethodName checkRangeForAdd - * @author zhaogd - * @date 2017年2月21日 下午7:32:55 - * @param index - */ - private void checkRangeForAdd(int index) { - if(index > size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查Get或者Remove方法的下标范围是否合法 - * - * @MethodName checkRangeForGetOrRemove - * @author zhaogd - * @date 2017年2月21日 下午7:33:21 - * @param index - */ - private void checkRangeForGetOrRemove(int index) { - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - - private class ArrayListIterator implements Iterator{ - - private int lastIndex = 0; - - @Override - public boolean hasNext() { - return lastIndex < size; - } - - @Override - public Object next() { - return elementData[lastIndex++]; - } - } -} +package com.guodong.datastructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 按下标顺序新增元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size] = o; + size++; + } + + /** + * 在指定下标位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkRangeForAdd(index); + + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + /** + * 根据下标获取列表数据 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkRangeForGetOrRemove(index); + + return elementData[index]; + } + + /** + * 根据下标移除元素,并返回移除的元素值 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkRangeForGetOrRemove(index); + + Object oldValue = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index -1); + + size--; + elementData[size] = null; + + return oldValue; + } + + /** + * 获得列表长度 + * + * @Method size + * @return + * @see com.guodong.datastructure.List#size() + */ + public int size() { + return size; + } + + /** + * 获取ArrayList的迭代器 + * + * @MethodName iterator + * @author zhaogd + * @date 2017年2月21日 下午8:19:28 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + /** + * 确保数组容量足够,如果不够则扩充数组 + * + * @MethodName ensureCapacityInternal + * @author zhaogd + * @date 2017年2月21日 下午5:06:46 + * @param minCapacity + */ + private void ensureCapacityInternal(int minCapacity) { + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + /** + * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 + * + * @MethodName grow + * @author zhaogd + * @date 2017年2月21日 下午5:20:55 + * @param minCapacity + */ + private void grow(int minCapacity) { + minCapacity = elementData.length + elementData.length / 2; + elementData = Arrays.copyOf(elementData, minCapacity); + } + + /** + * 检查Add方法的下标范围是否合法 + * + * @MethodName checkRangeForAdd + * @author zhaogd + * @date 2017年2月21日 下午7:32:55 + * @param index + */ + private void checkRangeForAdd(int index) { + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查Get或者Remove方法的下标范围是否合法 + * + * @MethodName checkRangeForGetOrRemove + * @author zhaogd + * @date 2017年2月21日 下午7:33:21 + * @param index + */ + private void checkRangeForGetOrRemove(int index) { + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + + private class ArrayListIterator implements Iterator{ + + private int lastIndex = 0; + + @Override + public boolean hasNext() { + return lastIndex < size; + } + + @Override + public Object next() { + return elementData[lastIndex++]; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java index 677eff3dab..2ced039d20 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java @@ -1,58 +1,58 @@ -package com.guodong.datastructure; - -public class BinaryTreeNode { - - private int data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(int data) { - this.data = data; - } - - public int getData() { - return data; - } - - public void setData(int data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(int o) { - - if (o < data) { - if (left != null) { - left.insert(o); - } else { - left = new BinaryTreeNode(o); - return left; - } - } else { - if (right != null) { - right.insert(o); - } else { - right = new BinaryTreeNode(o); - return right; - } - } - - return null; - } - -} +package com.guodong.datastructure; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(int o) { + + if (o < data) { + if (left != null) { + left.insert(o); + } else { + left = new BinaryTreeNode(o); + return left; + } + } else { + if (right != null) { + right.insert(o); + } else { + right = new BinaryTreeNode(o); + return right; + } + } + + return null; + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java index 1a9bc5ad8a..8d486220b0 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java @@ -1,7 +1,7 @@ -package com.guodong.datastructure; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.guodong.datastructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index a7dc12694e..976129cc84 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -1,285 +1,285 @@ -package com.guodong.datastructure; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private int size; - - private Node head; - - private Node last; - - /** - * 向 链表尾端插入元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - linkLast(o); - } - - /** - * 向链表指定位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkIndexForAdd(index); - - if (index == size) { - linkLast(o); - } else { - Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 - Node currentNode = getNodeByIndex(index); // 取到当前下标节点 - Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 - - if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 - head = newNode; - } else { - prevNode.next = newNode; - } - size++; - } - } - - /** - * 根据下标获取链表中元素 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkIndexForGet(index); - return getNodeByIndex(index).data; - } - - public Object getLast() { - return last.data; - } - - /** - * 根据下标移除链表元素 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkIndexForGet(index); - - Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 - Node currentNode = null; - if (prevNode == null) { - currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 - head = currentNode.next; - } else { - currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 - prevNode.next = currentNode.next; - } - Node nextNode = currentNode.next; - - if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 - last = prevNode; - } else { - currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 - } - Object data = currentNode.data; - currentNode.data = null; // 清空当前节点的值,等待垃圾回收 - - size--; - - return data; - } - - /** - * 返回List长度 - */ - public int size() { - return size; - } - - /** - * 向列表头部添加元素 - * - * @param o - */ - public void addFirst(Object o) { - Node n = head; - Node newNode = new Node(o, n); - - head = newNode; - if (n == null) { - last = newNode; - } - size++; - } - - /** - * 向列表尾部添加元素 - * - * @param o - */ - public void addLast(Object o) { - linkLast(o); - } - - /** - * 移除链表第一个元素 - * - * @return - */ - public Object removeFirst() { - Node n = head; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node nextNode = n.next; - - n.data = null; - n.next = null; - - head = nextNode; - if (nextNode == null) { - last = null; - } - - size--; - return data; - } - - public Object removeLast() { - Node n = last; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node prevNode = getNodeByIndex(size - 2); - n.data = null; - if (prevNode == null) { - head = null; - } else { - prevNode.next = null; - } - last = prevNode; - - size--; - return data; - } - - /** - * 根据下标获取对应的节点 - * - * @MethodName getNodeByIndex - * @author zhaogd - * @date 2017年2月23日 下午3:32:48 - * @param index - * @return - */ - private Node getNodeByIndex(int index) { - if (index < 0) { - return null; - } - Node n = head; - for (int i = 0; i < index; i++) { - n = n.next; - } - return n; - } - - /** - * 在链表尾端插入节点 - * - * @MethodName linkLast - * @author zhaogd - * @date 2017年2月23日 下午3:14:28 - * @param o - */ - private void linkLast(Object o) { - Node n = last; // 取出原尾端数据 - Node newNode = new Node(o, null); // 创建新节点 - last = newNode; // 把新节点放入链表尾端 - // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 - // 如果不为空,把原尾端节点指向新节点 - if (n == null) { - head = newNode; - } else { - n.next = newNode; - } - - size++; - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForAdd - * @author zhaogd - * @date 2017年2月23日 下午3:05:07 - * @param index - */ - private void checkIndexForAdd(int index) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForGet - * @author zhaogd - * @date 2017年2月23日 下午4:21:35 - * @param index - */ - private void checkIndexForGet(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { - private Node current; - - private int index; - - @Override - public boolean hasNext() { - return index < size; - } - - @Override - public Object next() { - if (current == null) { - current = getNodeByIndex(index); - } - Object data = current.data; - current = current.next; - index++; - return data; - } - } -} +package com.guodong.datastructure; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size; + + private Node head; + + private Node last; + + /** + * 向 链表尾端插入元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + linkLast(o); + } + + /** + * 向链表指定位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkIndexForAdd(index); + + if (index == size) { + linkLast(o); + } else { + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 + head = newNode; + } else { + prevNode.next = newNode; + } + size++; + } + } + + /** + * 根据下标获取链表中元素 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkIndexForGet(index); + return getNodeByIndex(index).data; + } + + public Object getLast() { + return last.data; + } + + /** + * 根据下标移除链表元素 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkIndexForGet(index); + + Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 + Node currentNode = null; + if (prevNode == null) { + currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 + head = currentNode.next; + } else { + currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 + prevNode.next = currentNode.next; + } + Node nextNode = currentNode.next; + + if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 + last = prevNode; + } else { + currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 + } + Object data = currentNode.data; + currentNode.data = null; // 清空当前节点的值,等待垃圾回收 + + size--; + + return data; + } + + /** + * 返回List长度 + */ + public int size() { + return size; + } + + /** + * 向列表头部添加元素 + * + * @param o + */ + public void addFirst(Object o) { + Node n = head; + Node newNode = new Node(o, n); + + head = newNode; + if (n == null) { + last = newNode; + } + size++; + } + + /** + * 向列表尾部添加元素 + * + * @param o + */ + public void addLast(Object o) { + linkLast(o); + } + + /** + * 移除链表第一个元素 + * + * @return + */ + public Object removeFirst() { + Node n = head; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node nextNode = n.next; + + n.data = null; + n.next = null; + + head = nextNode; + if (nextNode == null) { + last = null; + } + + size--; + return data; + } + + public Object removeLast() { + Node n = last; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node prevNode = getNodeByIndex(size - 2); + n.data = null; + if (prevNode == null) { + head = null; + } else { + prevNode.next = null; + } + last = prevNode; + + size--; + return data; + } + + /** + * 根据下标获取对应的节点 + * + * @MethodName getNodeByIndex + * @author zhaogd + * @date 2017年2月23日 下午3:32:48 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + if (index < 0) { + return null; + } + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + /** + * 在链表尾端插入节点 + * + * @MethodName linkLast + * @author zhaogd + * @date 2017年2月23日 下午3:14:28 + * @param o + */ + private void linkLast(Object o) { + Node n = last; // 取出原尾端数据 + Node newNode = new Node(o, null); // 创建新节点 + last = newNode; // 把新节点放入链表尾端 + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 + // 如果不为空,把原尾端节点指向新节点 + if (n == null) { + head = newNode; + } else { + n.next = newNode; + } + + size++; + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForAdd + * @author zhaogd + * @date 2017年2月23日 下午3:05:07 + * @param index + */ + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForGet + * @author zhaogd + * @date 2017年2月23日 下午4:21:35 + * @param index + */ + private void checkIndexForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current; + + private int index; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (current == null) { + current = getNodeByIndex(index); + } + Object data = current.data; + current = current.next; + index++; + return data; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java index 2471c15d21..1a6f12da52 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java @@ -1,14 +1,14 @@ -package com.guodong.datastructure; - -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(); -} +package com.guodong.datastructure; + +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/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java index b14751aab7..6dc85d8ec0 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java @@ -1,21 +1,21 @@ -package com.guodong.datastructure; - -public class Queue { - private LinkedList element = new LinkedList(); - - public void enQueue(Object o) { - element.addLast(o); - } - - public Object deQueue() { - return element.removeFirst(); - } - - public boolean isEmpty() { - return element.size() == 0; - } - - public int size() { - return element.size(); - } -} +package com.guodong.datastructure; + +public class Queue { + private LinkedList element = new LinkedList(); + + public void enQueue(Object o) { + element.addLast(o); + } + + public Object deQueue() { + return element.removeFirst(); + } + + public boolean isEmpty() { + return element.size() == 0; + } + + public int size() { + return element.size(); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java index f743d0dd3b..c2b5049e73 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java @@ -1,25 +1,25 @@ -package com.guodong.datastructure; - -public class Stack { - private LinkedList elementData = new LinkedList(); - - public void push(Object o) { - elementData.addLast(o); - } - - public Object pop() { - return elementData.removeLast(); - } - - public Object peek() { - return elementData.getLast(); - } - - public boolean isEmpty() { - return elementData.size() == 0; - } - - public int size() { - return elementData.size(); - } -} +package com.guodong.datastructure; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o) { + elementData.addLast(o); + } + + public Object pop() { + return elementData.removeLast(); + } + + public Object peek() { + return elementData.getLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java index ec3a7600a4..f38f58614d 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java @@ -1,135 +1,135 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.ArrayList; -import com.guodong.datastructure.Iterator; - -public class ArrayListTest { - - ArrayList arrayList; - - @Before - public void setUp() throws Exception { - arrayList = new ArrayList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - // 测试新增 - arrayList.add(0); - assertEquals(0, arrayList.get(0)); - assertEquals(1, arrayList.size()); - - // 测试扩充 - for (int i = 1; i < 101; i++) { - arrayList.add(i); - } - assertEquals(101, arrayList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(-1, 2); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(1, 3); - } - - @Test - public void testAddIntObject() { - // 测试下标新增 - arrayList.add(0, 1); - arrayList.add(1, 2); - arrayList.add(2, 3); - arrayList.add(3, 4); - assertEquals(4, arrayList.size()); - - // 测试中间插入 - arrayList.add(2, 5); - assertEquals(5, arrayList.size()); // 测试插入之后长度 - assertEquals(5, arrayList.get(2)); - assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 - assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 - - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(0); - } - - @Test - public void testGet() { - arrayList.add(1); - arrayList.add(2); - arrayList.add(3); - assertEquals(1, arrayList.get(0)); - assertEquals(2, arrayList.get(1)); - assertEquals(3, arrayList.get(2)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove1() { - arrayList.remove(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove2() { - arrayList.remove(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove3() { - arrayList.remove(1); - } - - @Test - public void testRemove() { - arrayList.add(1); - arrayList.remove(0); - assertEquals(0, arrayList.size()); - - arrayList.add(1); - arrayList.add(2); - arrayList.remove(0); - assertEquals(1, arrayList.size()); - assertEquals(2, arrayList.get(0)); - } - - @Test - public void testSize() { - arrayList.add(1); - assertEquals(1, arrayList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = arrayList.iterator(); - assertFalse(iterator.hasNext()); - - arrayList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.ArrayList; +import com.guodong.datastructure.Iterator; + +public class ArrayListTest { + + ArrayList arrayList; + + @Before + public void setUp() throws Exception { + arrayList = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + // 测试新增 + arrayList.add(0); + assertEquals(0, arrayList.get(0)); + assertEquals(1, arrayList.size()); + + // 测试扩充 + for (int i = 1; i < 101; i++) { + arrayList.add(i); + } + assertEquals(101, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(-1, 2); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(1, 3); + } + + @Test + public void testAddIntObject() { + // 测试下标新增 + arrayList.add(0, 1); + arrayList.add(1, 2); + arrayList.add(2, 3); + arrayList.add(3, 4); + assertEquals(4, arrayList.size()); + + // 测试中间插入 + arrayList.add(2, 5); + assertEquals(5, arrayList.size()); // 测试插入之后长度 + assertEquals(5, arrayList.get(2)); + assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 + assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 + + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(0); + } + + @Test + public void testGet() { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + assertEquals(1, arrayList.get(0)); + assertEquals(2, arrayList.get(1)); + assertEquals(3, arrayList.get(2)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove1() { + arrayList.remove(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove2() { + arrayList.remove(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove3() { + arrayList.remove(1); + } + + @Test + public void testRemove() { + arrayList.add(1); + arrayList.remove(0); + assertEquals(0, arrayList.size()); + + arrayList.add(1); + arrayList.add(2); + arrayList.remove(0); + assertEquals(1, arrayList.size()); + assertEquals(2, arrayList.get(0)); + } + + @Test + public void testSize() { + arrayList.add(1); + assertEquals(1, arrayList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + assertFalse(iterator.hasNext()); + + arrayList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java index 83972b7776..537cd5961f 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java @@ -1,37 +1,37 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.BinaryTreeNode; - -public class BinaryTreeNodeTest { - - private BinaryTreeNode binaryTreeNode; - - @Before - public void setUp() throws Exception { - binaryTreeNode = new BinaryTreeNode(50); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testInsert() { - binaryTreeNode.insert(20); - binaryTreeNode.insert(30); - binaryTreeNode.insert(60); - binaryTreeNode.insert(80); - - assertEquals(20, binaryTreeNode.getLeft().getData()); - assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); - assertEquals(60, binaryTreeNode.getRight().getData()); - assertEquals(80, binaryTreeNode.getRight().getRight().getData()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUp() throws Exception { + binaryTreeNode = new BinaryTreeNode(50); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + binaryTreeNode.insert(20); + binaryTreeNode.insert(30); + binaryTreeNode.insert(60); + binaryTreeNode.insert(80); + + assertEquals(20, binaryTreeNode.getLeft().getData()); + assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(60, binaryTreeNode.getRight().getData()); + assertEquals(80, binaryTreeNode.getRight().getRight().getData()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java index 054e8f81b4..52d42b5aa7 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java @@ -1,143 +1,143 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Iterator; -import com.guodong.datastructure.LinkedList; - -public class LinkedListTest { - - private LinkedList linkedList; - - @Before - public void setUp() throws Exception { - linkedList = new LinkedList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - linkedList.add(1); - assertEquals(1, linkedList.size()); - assertEquals(1, linkedList.get(0)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - linkedList.add(-1, 1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - linkedList.add(1, 1); - } - - @Test - public void testAddIntObject() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(1, linkedList.get(0)); - - linkedList.add(1,3); - assertEquals(2, linkedList.get(2)); - assertEquals(3, linkedList.get(1)); - assertEquals(3, linkedList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - linkedList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - linkedList.get(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet3() { - linkedList.get(1); - } - - @Test - public void testGet() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(2, linkedList.get(1)); - } - - @Test - public void testGetLast() { - linkedList.add(1); - assertEquals(1, linkedList.getLast()); - - linkedList.add(2); - assertEquals(2, linkedList.getLast()); - } - - @Test - public void testRemove() { - linkedList.add(1); - assertEquals(1, linkedList.remove(0)); - assertEquals(0, linkedList.size()); - } - - @Test - public void testSize() { - linkedList.add(1); - linkedList.add(1); - linkedList.add(1); - assertEquals(3, linkedList.size()); - } - - @Test - public void testAddFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.get(0)); - - linkedList.addFirst(2); - linkedList.addFirst(3); - assertEquals(3, linkedList.get(0)); - assertEquals(1, linkedList.getLast()); - } - - @Test - public void testAddLast() { - linkedList.addLast(1); - assertEquals(1, linkedList.getLast()); - assertEquals(1, linkedList.get(0)); - } - - @Test - public void testRemoveFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.removeFirst()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testRemoveLast() { - linkedList.addLast(2); - assertEquals(2, linkedList.removeLast()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = linkedList.iterator(); - assertFalse(iterator.hasNext()); - - linkedList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Iterator; +import com.guodong.datastructure.LinkedList; + +public class LinkedListTest { + + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add(1); + assertEquals(1, linkedList.size()); + assertEquals(1, linkedList.get(0)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + linkedList.add(-1, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + linkedList.add(1, 1); + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(1, linkedList.get(0)); + + linkedList.add(1,3); + assertEquals(2, linkedList.get(2)); + assertEquals(3, linkedList.get(1)); + assertEquals(3, linkedList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + linkedList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + linkedList.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet3() { + linkedList.get(1); + } + + @Test + public void testGet() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(2, linkedList.get(1)); + } + + @Test + public void testGetLast() { + linkedList.add(1); + assertEquals(1, linkedList.getLast()); + + linkedList.add(2); + assertEquals(2, linkedList.getLast()); + } + + @Test + public void testRemove() { + linkedList.add(1); + assertEquals(1, linkedList.remove(0)); + assertEquals(0, linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add(1); + linkedList.add(1); + linkedList.add(1); + assertEquals(3, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.get(0)); + + linkedList.addFirst(2); + linkedList.addFirst(3); + assertEquals(3, linkedList.get(0)); + assertEquals(1, linkedList.getLast()); + } + + @Test + public void testAddLast() { + linkedList.addLast(1); + assertEquals(1, linkedList.getLast()); + assertEquals(1, linkedList.get(0)); + } + + @Test + public void testRemoveFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testRemoveLast() { + linkedList.addLast(2); + assertEquals(2, linkedList.removeLast()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + assertFalse(iterator.hasNext()); + + linkedList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java index 86a4ebdd68..1773b5027c 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java @@ -1,62 +1,62 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import java.util.NoSuchElementException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Queue; - -public class QueueTest { - - private Queue queue; - - @Before - public void setUp() throws Exception { - queue = new Queue(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEnQueue() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - } - - @Test(expected = NoSuchElementException.class) - public void testDeQueueExecption() { - queue.deQueue(); - } - - @Test - public void testDeQueue() { - queue.enQueue(1); - assertEquals(1, queue.deQueue()); - assertTrue(queue.isEmpty()); - } - - @Test - public void testIsEmpty() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - - queue.deQueue(); - assertTrue(queue.isEmpty()); - } - - @Test - public void testSize() { - queue.enQueue(1); - queue.enQueue(1); - queue.enQueue(1); - - assertEquals(3, queue.size()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Queue; + +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + } + + @Test(expected = NoSuchElementException.class) + public void testDeQueueExecption() { + queue.deQueue(); + } + + @Test + public void testDeQueue() { + queue.enQueue(1); + assertEquals(1, queue.deQueue()); + assertTrue(queue.isEmpty()); + } + + @Test + public void testIsEmpty() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + + queue.deQueue(); + assertTrue(queue.isEmpty()); + } + + @Test + public void testSize() { + queue.enQueue(1); + queue.enQueue(1); + queue.enQueue(1); + + assertEquals(3, queue.size()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java index 36781c863f..74ac8e7cc7 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java @@ -1,46 +1,46 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Stack; - -public class StackTest { - - private Stack stack; - - @Before - public void setUp() throws Exception { - stack = new Stack(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPush() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPop() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPeek() { - stack.push(11); - assertEquals(11, stack.peek()); - assertFalse(stack.isEmpty()); - assertEquals(1, stack.size()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPush() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPeek() { + stack.push(11); + assertEquals(11, stack.peek()); + assertFalse(stack.isEmpty()); + assertEquals(1, stack.size()); + } + +} diff --git a/group12/441908378/ArrayList.java b/group12/441908378/ArrayList.java old mode 100755 new mode 100644 index 45e495867c..74f49a39d1 --- a/group12/441908378/ArrayList.java +++ b/group12/441908378/ArrayList.java @@ -1,50 +1,50 @@ -import java.util.Arrays; - -public class ArrayList { - -private int size = 0; - -private Object[] elementData = new Object[100]; - -public void enlargeCapacity(int minCapacity){ - int oldCapacity=elementData.length; - if(oldCapacityb){ - return left; - }else{ - return right; - } - } - - -} + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node; + do{ + node=compare(o); + }while(node==null); + node.data=o; + return node; + } + + public BinaryTreeNode compare(Object o){ + int a=(Integer)data; + int b=(Integer)o; + if(a>b){ + return left; + }else{ + return right; + } + } + + +} diff --git a/group12/441908378/LinkedList.java b/group12/441908378/LinkedList.java old mode 100755 new mode 100644 index 0d0339bc01..456160f154 --- a/group12/441908378/LinkedList.java +++ b/group12/441908378/LinkedList.java @@ -1,121 +1,121 @@ -public class LinkedList { - -private Node head; - -private static class Node{ - Object data; - Node next; -} - -public boolean hasNext(Node a){ - if(a.next!=null){ - return true; - } - return false; -} - -public Node getIndex(int index){ - Node a=head.next; - for(int i=0;i= 0 && index <= size() Ϊ true + * @param index ���ӵ�λ�� + * @param o ���ӵ�Ԫ�� + */ + public void add(int index, Object o){ + if(index > size || index < 0){ + //size�������������Ԫ�ص��±꣬����index����sizeҲ���ԡ� + throw new ArrayIndexOutOfBoundsException("Index:" + index); + } + checkSize(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + /** + * ��ȡ�����indexλ�ô�Ԫ�� + * indexӦ���� index >= 0 && index < size() Ϊ true + */ + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + /** + * ɾ�������indexλ�ô���Ԫ�� + * indexӦ���� index >= 0 && index < size() Ϊ true + * @return ��ɾ����Ԫ�� + */ + public Object remove(int index){ + checkIndex(index); + Object obj = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return obj; + } + + /** + * ��ȡ��ǰArrayList���Ѿ��洢��Ԫ�صĸ��� + */ + public int size(){ + return this.size; + } + + /** + * ��ȡ��ǰArrayList�ĵ����� + * @return ��ǰArrayList�ĵ����� + */ + public Iterator iterator(){ + return new Iterator(){ + private int count = 0; + + @Override + public boolean hasNext() { + return size > count; + } + + @Override + public Object next() { + if(count == size){ + throw new RuntimeErrorException(null, "û�и����Ԫ�أ�"); + } + return get(count++); + } + + @Override + public void remove() { + ArrayList.this.remove(count - 1); + } + + }; + } + + /** + * �������get��remove������������±��Ƿ�Խ�� + * @param index ������±� + */ + private void checkIndex(int index){ + if(index >= size || index < 0){ + //size�������������Ԫ�ص��±꣬����index�±��ϻ�û�д洢���ݣ��޷�����get��remove���� + throw new ArrayIndexOutOfBoundsException("Index:" + index); + } + } + + /** + * ���ArrayList�Ƿ�Ҫ������ + * ��ArrayList��Ҫ����ʱ����������ݣ�ÿ�����ݽ�ʹ����������20�� + */ + private void checkSize(){ + if(size < elementData.length) return; + elementData = Arrays.copyOf(elementData, elementData.length + 20/*(int)(elementData.length * 1.2)*/); + } + +} diff --git a/group13/1274639949/lesson01/src/com/hans/BinaryTree.java b/group13/1274639949/lesson01/src/com/hans/BinaryTree.java new file mode 100644 index 0000000000..a4bf39dd3a --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/BinaryTree.java @@ -0,0 +1,10 @@ +package com.hans; + +public class BinaryTree { + BinaryTreeNode root = new BinaryTreeNode(); + + public boolean add(Object obj){ + + return true; + } +} diff --git a/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java b/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java new file mode 100644 index 0000000000..683868be1d --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.hans; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group13/1274639949/lesson01/src/com/hans/Iterator.java b/group13/1274639949/lesson01/src/com/hans/Iterator.java new file mode 100644 index 0000000000..4ec870ed01 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Iterator.java @@ -0,0 +1,8 @@ +package com.hans; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); +} + diff --git a/group13/1274639949/lesson01/src/com/hans/LinkedList.java b/group13/1274639949/lesson01/src/com/hans/LinkedList.java new file mode 100644 index 0000000000..8e2d421501 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/LinkedList.java @@ -0,0 +1,148 @@ +package com.hans; + +public class LinkedList implements List { + + private int size; + + private Node head; + + public LinkedList() { + head = new Node(); +// head.data = head; + } + + /** + * ����һ��Ԫ�� + */ + public void add(Object o){ + Node tail = getTail(); + + Node node = new Node(); + node.data = o; + + tail.next = node; + size++; + return; + } + + /** + * ��ָ��λ�ü���һ��Ԫ�� + * @param index ָ����λ�ã�Ӧ������ index > 0 && index <= size() Ϊ true�� + */ + public void add(int index , Object o){ + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("Index:" + index); + + Node pos = head; + for(int i = 0; i < index; i++){ + //Ҫ��indexλ������һ��Ԫ�أ�ֻҪ��ȡ�� index - 1 λ�õ�Ԫ�ؼ��� + pos = pos.next; + } + Node node = new Node(); + node.data = o; + node.next = pos.next; + pos.next = node; + size++; + return; + } + + /** + * ��ȡָ��λ�ô���Ԫ�� + * @param index Ҫ��ȡ��Ԫ�ص�λ�ã�Ӧ������ index > 0 && index < size() Ϊ true�� + */ + public Object get(int index){ + checkIndex(index); + Node pos = head; + for(int i = 0; i <= index; i++){ + //��������Ϊ <= ,������Ϊ��Ҫ��ȡ��indexλ�� + pos = pos.next; + } + return pos.data; + } + + /** + * �Ƴ�������ָ��λ�ô���Ԫ�� + * @param index Ҫ�Ƴ���Ԫ�ص�λ�ã�Ӧ������ index > 0 && index < size() Ϊ true�� + */ + public Object remove(int index){ + checkIndex(index); + Node pos = head; + for(int i = 0; i < index; i++){ + pos = pos.next; + } + Node temp = pos.next; + pos.next = temp.next; + size--; + return temp.data; + } + + /** + * ��ȡ�洢��Ԫ�صĸ��� + */ + public int size(){ + return this.size; + } + + /** + * �ڵ�һ��Ԫ�ص�ǰ�������һ��Ԫ�� + * @param o + */ + public void addFirst(Object o){ + add(0, o); + } + + /** + * �����һ��Ԫ�صĺ�������һ��Ԫ�� + * @param o + */ + public void addLast(Object o){ + add(o); + } + + /** + * �Ƴ���һ��Ԫ�� + * @return ���Ƴ���Ԫ�� + */ + public Object removeFirst(){ + return remove(0); + } + + /** + * �Ƴ����һ��Ԫ�� + * @return ���Ƴ���Ԫ�� + */ + public Object removeLast(){ + return remove(size - 1); + } + + public Iterator iterator(){ + return null; + } + + + private static final class Node{ + Object data; + Node next; + } + + /** + * ��ȡ���һ���ڵ� + * @return + */ + private Node getTail(){ + Node tail = head; + while(tail.next != null){ + tail = tail.next; + } + return tail; + } + /** + * ���get��remove�������������Ԫ���Ƿ���Ч + * @param index + */ + private void checkIndex(int index) { + if(index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index:" + index); + } +} + diff --git a/group13/1274639949/lesson01/src/com/hans/List.java b/group13/1274639949/lesson01/src/com/hans/List.java new file mode 100644 index 0000000000..f8ffaa8f5e --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/List.java @@ -0,0 +1,10 @@ +package com.hans; + +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(); + public Iterator iterator(); +} diff --git a/group13/1274639949/lesson01/src/com/hans/Queue.java b/group13/1274639949/lesson01/src/com/hans/Queue.java new file mode 100644 index 0000000000..2c696cca10 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Queue.java @@ -0,0 +1,22 @@ +package com.hans; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group13/1274639949/lesson01/src/com/hans/Stack.java b/group13/1274639949/lesson01/src/com/hans/Stack.java new file mode 100644 index 0000000000..d370221f1a --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Stack.java @@ -0,0 +1,30 @@ +package com.hans; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size() <= 0){ + return null; + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if(elementData.size() <= 0){ + return null; + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} + diff --git "a/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" "b/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" new file mode 100644 index 0000000000..a89412cc01 --- /dev/null +++ "b/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" @@ -0,0 +1 @@ +如今计算机已经深入走进我们生活的方方面面,购物、娱乐、餐饮······,可以说我们无时无刻不在利用计算机给我们的 diff --git a/group13/1641296572/lesson1/.gitignore b/group13/1641296572/lesson1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/1641296572/lesson1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java b/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c9d4661987 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,107 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List +{ + + private int size = 0; + + private Object[] elementData = new Object[3]; + + public void add(Object o) + { + if (elementData.length == size) + { + expand(); + } + elementData[size++] = o; + } + + private void expand() + { + Object[] newDatas = new Object[size * 3 / 2 + 1]; + System.arraycopy(elementData, 0, newDatas, 0, size); + elementData = newDatas; + System.out.println("expand: from :" + size + " to " + size * 2); + + } + + public void add(int index, Object o) + { + if (index > size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + if (elementData.length == size) + { + expand(); + } + + Object[] tmps = new Object[size - index]; + System.arraycopy(elementData, index, tmps, 0, size - index); + elementData[index] = o; + System.arraycopy(tmps, 0, elementData, index + 1, size - index); + + size++; + } + + public Object get(int index) + { + if (index >= size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + + return elementData[index]; + } + + public Object remove(int index) + { + if (index >= size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + Object rt = elementData[index]; + + Object[] tmps = new Object[size - index - 1]; + System.arraycopy(elementData, index + 1, tmps, 0, size - index - 1); + System.arraycopy(tmps, 0, elementData, index, size - index - 1); + elementData[--size] = null; + + return rt; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return new Iterator(){ + int pos =0; + @Override + public boolean hasNext() + { + if(pos < size) + { + return true; + } + return false; + } + + @Override + public Object next() + { + if(pos < size) + { + throw new NoSuchElementException(); + } + return elementData[pos++]; + }}; + } + +} + + diff --git a/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java b/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java new file mode 100644 index 0000000000..1aa36825c4 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class BinarySortTree +{ + BinaryTreeNode root = null; + int size =0; + + public void insert(Object o) + { + BinaryTreeNode node = new BinaryTreeNode(); + node.setData(o); + node.setLeft(null); + node.setRight(null); + + if(null == root) + { + root = node; + size++; + } + + BinaryTreeNode p = root; + BinaryTreeNode pre = p; + while(p!= null) + { + if(objectCompare(o, p.getData())<0) + { + pre = p; + p=p.getLeft(); + } + else + { + pre = p; + p=p.getRight(); + } + } + + if(objectCompare(o, pre.getData())<0) + { + pre.setLeft(node); + } + else + { + pre.setRight(node); + } + + size++; + } + + private int objectCompare(Object o1, Object o2) + { +// return o.toString().compareTo(o2.toString()); + return (Integer) o1 - (Integer)o2; + } + + public void print(BinaryTreeNode r) + { + if(r==null) + { + return; + } + System.out.print(r.getData() + "->"); + print(r.getLeft()); + print(r.getRight()); + } + +} diff --git a/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java b/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d0ba079f40 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode +{ + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() + { + return data; + } + + public void setData(Object data) + { + this.data = data; + } + + public BinaryTreeNode getLeft() + { + return left; + } + + public void setLeft(BinaryTreeNode left) + { + this.left = left; + } + + public BinaryTreeNode getRight() + { + return right; + } + + public void setRight(BinaryTreeNode right) + { + this.right = right; + } + + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java b/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..4175d7d2dd --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator +{ + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java b/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9bd71ed325 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,220 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List +{ + + private Node head; + private int size = 0; + + public void add(Object o) + { + if (0 == size) + { + head = new Node(); + head.data = o; + head.next = null; + } else + { + Node pNode = head; + while (pNode.next != null) + { + pNode = pNode.next; + } + Node node = new Node(); + node.data = o; + pNode.next = node; + node.next = null; + } + size++; + } + + public void add(int index, Object o) + { + if (index < 0 || index > size) + { + throw new IndexOutOfBoundsException(); + } + Node node = new Node(); + node.data = o; + + if (index != 0) + { + Node nowNode = head; + Node preNode = head; + for (int i = 0; i < index; i++) + { + preNode = nowNode; + nowNode = nowNode.next; + } + preNode.next = node; + node.next = nowNode; + } else + { + node.next = head; + head = node; + + } + size++; + } + + public Object get(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int i = 0; i < index; i++) + { + node = node.next; + } + return node.data; + } + + public Object remove(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + Object rt = null; + if (index == 0) + { + rt = head.data; + Node node = head; + head = head.next; + node.next = null; + node.data = null; + } else + { + Node preNode = head; + Node nowNode = head; + for (int i = 0; i < index; i++) + { + preNode = nowNode; + nowNode = nowNode.next; + } + rt = nowNode.data; + preNode.next = nowNode.next; + nowNode.next = null; + nowNode.data = null; + } + size--; + return rt; + } + + public int size() + { + return size; + } + + public void addFirst(Object o) + { + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + + size++; + } + + public void addLast(Object o) + { + Node node = new Node(); + node.data = o; + node.next = null; + + Node nowNode = head; + + if (size == 0) + { + head = node; + size++; + return; + } + + while (nowNode != null && nowNode.next != null) + { + nowNode = nowNode.next; + } + + nowNode.next = node; + size++; + } + + public Object removeFirst() + { + if (size == 0) + { + throw new NoSuchElementException(); + } + Node node = head; + head = head.next; + Object rt = node.data; + node.next = null; + node.data = null; + size--; + return rt; + } + + public Object removeLast() + { + if (size == 0) + { + throw new NoSuchElementException(); + } + + Node nowNode = head; + Node preNode = head; + while (nowNode.next != null) + { + preNode = nowNode; + nowNode = nowNode.next; + } + + preNode.next = null; + Object rt = nowNode.data; + nowNode.data = null; + size--; + return rt; + } + + public Iterator iterator() + { + return new Iterator() + { + Node node = head; + + @Override + public boolean hasNext() + { + if (null != node) + { + return true; + } + return false; + } + + @Override + public Object next() + { + if (null == node) + { + throw new NoSuchElementException(); + } + Object o = node.data; + node = node.next; + return o; + } + }; + } + + private static class Node + { + Object data; + Node next; + + } +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/List.java b/group13/1641296572/lesson1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..cf5d612814 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/List.java @@ -0,0 +1,14 @@ +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/group13/1641296572/lesson1/src/com/coding/basic/Queue.java b/group13/1641296572/lesson1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..695c953324 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue +{ + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.add(o); + } + + public Object deQueue() + { + Object rt = list.removeFirst(); + return rt; + } + + public boolean isEmpty() + { + return 0==list.size(); + } + + public int size() + { + return list.size(); + } + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/Stack.java b/group13/1641296572/lesson1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..424263f0b7 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Stack.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(elementData.size(), o); + } + + public Object pop() + { + int size = elementData.size(); + if(size==0) + { + throw new EmptyStackException(); + } + + Object rt = elementData.get(size-1); + elementData.remove(size-1); + return rt; + } + + public Object peek() + { + if(elementData.size()==0) + { + throw new EmptyStackException(); + } + + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty() + { + return 0==elementData.size(); + } + + public int size() + { + return elementData.size(); + } + + public static void main(String []args) + { + Stack st = new Stack(); + System.out.println("is Empty:"+ st.isEmpty()); + for(int i=0;i<10;i++) + { + st.push("s="+ i); + } + + System.out.println("is Empty:"+ st.isEmpty()); + System.out.println(st.peek()); + for(int i=0;i<10;i++) + { + System.out.println("pop->" + st.pop()); + } + System.out.println("is Empty:"+ st.isEmpty()); + } + + +} \ No newline at end of file diff --git a/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java b/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java new file mode 100644 index 0000000000..39feef2cbd --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java @@ -0,0 +1,135 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 22/02/2017. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[10]; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean add(Object o) { + int minCapacity = size + 1; + if (minCapacity - elementData.length > 0) + grow(minCapacity); + elementData[size++] = o; + return true; + } + + public boolean add(int index, Object o) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + int minCapacity = size + 1; + if (minCapacity - elementData.length > 0) + grow(minCapacity); + elementData[index] = o; + size++; + return true; + } + + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) + if (elementData[index] == null) { + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + } else { + for (int index = 0; index < size; index++) + if (o.equals(elementData[index])) { + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + } + return false; + } + + public boolean remove(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + + public Object get(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + return elementData[index]; + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + elementData = copy(elementData, newCapacity); + + } + + public Object[] copy(Object[] src, int newCapacity) { + Object[] arr = new Object[newCapacity]; + for (int i = 0; i < src.length; i++) { + arr[i] = elementData[i]; + } + return arr; + } + + public void copy(Object[] src, int srcPost, Object[] dest, int destPost, int length) { + for (int i = 0; i < length; i++) { + dest[destPost + i] = src[srcPost + i]; + } + } + + public Iterator iterator() { + return new ListIterator(this); + } + + public class ListIterator implements Iterator { + + private List list; + private int endIndex = 0; + private int index = 0; + + public ListIterator(ArrayList list) { + this.list = list; + this.endIndex = list.size(); + } + + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public Object next() { + if (!this.hasNext()) { + throw new RuntimeException("EmptyElementException"); + } else { + return list.get(index++); + } + } + } +} + diff --git a/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java b/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java new file mode 100644 index 0000000000..9b6a9d7031 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java @@ -0,0 +1,68 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 26/02/2017. + */ +public class BinaryTree { + + private TreeNode root; + + public boolean isEmpty() { + return root == null; + } + + //节点个数 + public int size() { + return size(root); + } + + private int size(TreeNode subTree) { + if (subTree == null) { + return 0; + } else { + return 1 + size(subTree.leftChild) + + size(subTree.rightChild); + } + } + + public void insert(int o) { + TreeNode newNode = new TreeNode(o, null, null); + if (root == null) + root = newNode; + else { + TreeNode current = root; + while (true) { + if (o < current.data) { + current = current.leftChild; + if (current == null) { + current.leftChild = newNode; + return; + } + } else { + current = current.rightChild; + if (current == null) { + current.rightChild = newNode; + return; + } + } + } + + } + + } + + private class TreeNode { + private int data; + private TreeNode leftChild; + private TreeNode rightChild; + + public TreeNode() { + } + + public TreeNode(int o, TreeNode l, TreeNode r) { + this.data = o; + this.leftChild = l; + this.rightChild = r; + } + } +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/Iterator.java b/group13/2729382520/L1/src/io/github/vxzh/Iterator.java new file mode 100644 index 0000000000..9db09eb9d4 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/Iterator.java @@ -0,0 +1,9 @@ +package io.github.vxzh; + +public interface Iterator { + + boolean hasNext(); + + Object next(); + +} \ No newline at end of file diff --git a/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java b/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java new file mode 100644 index 0000000000..9f065c1879 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java @@ -0,0 +1,57 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 24/02/2017. + */ +public class LinkQueue implements Queue { + + private int size = 0; + + private Node front; + private Node rear; + + /** + * 入队 + */ + public void enQueue(Object o) { + Node node = rear; + Node newNode = new Node(o, null); + rear = newNode; + if (node == null) + front = newNode; + else + node.next = newNode; + size++; + } + + /** + * 出队 + */ + public Object deQueue() { + if (isEmpty()) + throw new RuntimeException("EmptyQueueException"); + Node node = front; + front = node.next; + size--; + return node.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + + Node(Object element, Node next) { + this.data = element; + this.next = next; + } + + } +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java b/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java new file mode 100644 index 0000000000..e599bf15e7 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java @@ -0,0 +1,138 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 23/02/2017. + */ +public class LinkedList implements List { + + private int size = 0; + + private Node head; + private Node tail; + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + private void linkLast(Object o) { + Node node = tail; + Node newNode = new Node(tail, o, null); + tail = newNode; + if (node == null) + head = newNode; + else + node.next = newNode; + size++; + } + + private void linkBefore(Object o, Node node) { + Node prev = node.prev; + Node newNode = new Node(prev, o, node); + node.prev = newNode; + if (prev == null) + head = newNode; + else + prev.next = newNode; + size++; + } + + public boolean add(Object o) { + linkLast(o); + return true; + } + + public boolean add(int index, Object o) { + if (index > size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + if (index == size) + linkLast(o); + else + linkBefore(o, node(index)); + return true; + } + + public boolean remove(Object o) { + if (o == null) { + return false; + } else { + for (Node x = head; x != null; x = x.next) { + if (o.equals(x.data)) { + unlink(x); + return true; + } + } + } + return false; + } + + public boolean remove(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + unlink(node(index)); + return true; + } + + public Object get(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + return node(index).data; + } + + private void unlink(Node x) { + Node prev = x.prev; + Node next = x.next; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + tail = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.data = null; + size--; + } + + private Node node(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = tail; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + + private static class Node { + Object data; + Node prev; + Node next; + + Node(Node prev, Object element, Node next) { + this.data = element; + this.next = next; + this.prev = prev; + } + + } + +} + + + diff --git a/group13/2729382520/L1/src/io/github/vxzh/List.java b/group13/2729382520/L1/src/io/github/vxzh/List.java new file mode 100644 index 0000000000..6e52bd58f6 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/List.java @@ -0,0 +1,22 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 22/02/2017. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean add(Object o); + + boolean add(int index, Object o); + + boolean remove(Object o); + + boolean remove(int index); + + Object get(int index); + +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/Queue.java b/group13/2729382520/L1/src/io/github/vxzh/Queue.java new file mode 100644 index 0000000000..258229c17b --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/Queue.java @@ -0,0 +1,15 @@ +package io.github.vxzh; + +/** + * Created by xuxiaoqing on 26/02/2017. + */ +public interface Queue { + + void enQueue(Object o); + + Object deQueue(); + + boolean isEmpty(); + + int size(); +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/Stack.java b/group13/2729382520/L1/src/io/github/vxzh/Stack.java new file mode 100644 index 0000000000..de7c8ede4d --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/Stack.java @@ -0,0 +1,41 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 24/02/2017. + */ +public class Stack { + + private ArrayList elementData; + + public Stack() { + this.elementData = new ArrayList(); + } + + public int size() { + return elementData.size(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object obj; + int len = elementData.size(); + obj = peek(); + elementData.remove(len - 1); + return obj; + } + + public Object peek() { + int len = elementData.size(); + if (len == 0) + throw new RuntimeException("EmptyStackException"); + return elementData.get(len - 1); + } + +} diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties new file mode 100644 index 0000000000..e23811b654 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Sun Feb 19 10:59:39 CST 2017 diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock new file mode 100644 index 0000000000..c5daff8bbe Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin new file mode 100644 index 0000000000..6512ad40b8 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin new file mode 100644 index 0000000000..357f65c480 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin new file mode 100644 index 0000000000..1dcc7e5225 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin new file mode 100644 index 0000000000..561100c562 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin new file mode 100644 index 0000000000..a2d12a13b5 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin new file mode 100644 index 0000000000..18c54c8bd5 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock new file mode 100644 index 0000000000..bed9c81dee Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock differ diff --git a/group13/2931408816/lesson1/data-structure/build.gradle b/group13/2931408816/lesson1/data-structure/build.gradle new file mode 100644 index 0000000000..0049406393 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/build.gradle @@ -0,0 +1,28 @@ +group 'cn.net.pikachu' +version '1.0-SNAPSHOT' + +buildscript { + ext.kotlin_version = '1.0.6' + + repositories { + mavenCentral() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + testCompile group: 'junit', name: 'junit', version: '4.11' + +} diff --git a/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..6ffa237849 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar differ diff --git a/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..84514df01f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Feb 19 11:20:26 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/group13/2931408816/lesson1/data-structure/gradlew b/group13/2931408816/lesson1/data-structure/gradlew new file mode 100644 index 0000000000..9aa616c273 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradlew @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/group13/2931408816/lesson1/data-structure/gradlew.bat b/group13/2931408816/lesson1/data-structure/gradlew.bat new file mode 100644 index 0000000000..f9553162f1 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/group13/2931408816/lesson1/data-structure/settings.gradle b/group13/2931408816/lesson1/data-structure/settings.gradle new file mode 100644 index 0000000000..941deed614 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'data-structure' + diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java new file mode 100644 index 0000000000..8cc192feeb --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java @@ -0,0 +1,97 @@ +package cn.net.pikachu.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private class Itr implements Iterator { + private int curIndex = 0; + + @Override + public boolean hasNext() { + return curIndex < size; + } + + @Override + public Object next() { + return elementData[curIndex++]; + } + } + + public void add(Object o) { + if (size == elementData.length) { + grow(); + } + elementData[size++] = o; + } + + private void grow() { + if (elementData.length < 2048) { + Arrays.copyOf(elementData, elementData.length * 2); + } else { + Arrays.copyOf(elementData, elementData.length + 1024); + } + } + + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } else if (index == size) { + add(o); + } else { + if (size == elementData.length) { + grow(); + } + for (int i = size; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + } + size++; + + } + + public Object get(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return o; + } + + public int size() { + return size; + } + + // 迭代器留在后面写 + public Iterator iterator() { + return new Itr(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("["); + for (int i = 0; i < size - 1; i++) { + builder.append(elementData[i]).append(","); + } + if (size > 0) { + builder.append(elementData[size - 1]); + } + builder.append("]"); + return builder.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..0c4d545e8a --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java @@ -0,0 +1,60 @@ +package cn.net.pikachu.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if (!(o instanceof Comparable)){ + throw new ClassCastException(o.toString()); + } + // 根据节点大小放置元素 小的在左边 大的在右边 + Comparable c = (Comparable) o; + if (data==null){ + data=o; + return this; + }else if (c.compareTo(o)>0){ + if (left==null){ + left=new BinaryTreeNode(); + } + return left.insert(o); + }else { + if (right==null){ + right=new BinaryTreeNode(); + } + return right.insert(o); + } + } + public void inOrderTraversal(Visit visit,BinaryTreeNode node){ + if (node.left!=null) + inOrderTraversal(visit,node.left); + if (node!=null) + visit.visit(node.data); + if (node.right!=null) + inOrderTraversal(visit,node.right); + } + public static interface Visit{ + public void visit(Object o); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java new file mode 100644 index 0000000000..05e8486430 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java new file mode 100644 index 0000000000..2c29d0f085 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java @@ -0,0 +1,161 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int curSize = 0; + + private class Itr implements Iterator { + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode != null; + } + + @Override + public Object next() { + Node node = curNode; + curNode = curNode.next; + return node.data; + } + } + + public void add(Object o) { + if (head == null) { + head = new Node(o, null); + } else { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o, null); + } + curSize++; + } + + public void add(int index, Object o) { + // 这里可以等于 + if (index < 0 || index > curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + if (index == 0) { + addFirst(o); + } else { + + Node node = head; + for (int i = 1; i < index; i++) { + node = node.next; + } + node.next = new Node(o, node.next); + curSize++; + } + + } + + public Object get(int index) { + if (index < 0 || index >= curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + if (index < 0 || index >= curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + if (index == 0) { + return removeFirst(); + } + + Node node = head; + for (int i = 1; i < index; i++) { + node = node.next; + } + Node t = node.next; + node.next=t.next; + curSize--; + return t.data; + } + + public int size() { + return curSize; + } + + public void addFirst(Object o) { + Node node = new Node(o, head); + head = node; + curSize++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node = head; + head = head.next; + curSize--; + return node.data; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node; + if (head.next == null) { + node = head; + head = null; + } else { + node = head; + while (node.next.next != null) { + node = node.next; + } + Node t = node.next; + node.next = null; + node = t; + } + curSize--; + return node.data; + } + + // 后面再实现 + public Iterator iterator() { + return new Itr(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("["); + Node node = head; + while (node != null) { + builder.append(node.data).append(","); + node = node.next; + } + if (curSize>0) + builder.deleteCharAt(builder.length() - 1); + builder.append("]"); + return builder.toString(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java new file mode 100644 index 0000000000..4272bde587 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java @@ -0,0 +1,9 @@ +package cn.net.pikachu.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java new file mode 100644 index 0000000000..89eda11fbe --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java @@ -0,0 +1,17 @@ +package cn.net.pikachu.basic; + +/** + * Created by pikachu on 17-2-21. + */ +public class Main { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + for (int i = 0; i < 3; i++) { + list.add(i); + } + Iterator iterator = list.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java new file mode 100644 index 0000000000..7e033d0885 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java @@ -0,0 +1,30 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +public class Queue { + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()){ + throw new NoSuchElementException(); + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size()==0; + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java new file mode 100644 index 0000000000..492d782b26 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java @@ -0,0 +1,33 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +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 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() { + return elementData.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java new file mode 100644 index 0000000000..ea3998b7c4 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java @@ -0,0 +1,212 @@ +package cn.net.pikachu.other; + +import java.util.AbstractList; +import java.util.Objects; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyArrayList extends AbstractList{ + // 当前能容纳的最大元素个数 + private int maxSize = 8; + // 当前数组已经有了的元素个数 + private int curSize = 0; + // 存放元素的数组 + private Object[] arr= new Object[curSize]; + /** + * {@inheritDoc} + * + * @param index + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public E get(int index) { + if (index<0 || index > curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + return (E) arr[index]; + } + + public int size() { + return curSize; + } + + /** + * Appends the specified element to the end of this list (optional + * operation). + *

+ *

Lists that support this operation may place limitations on what + * elements may be added to this list. In particular, some + * lists will refuse to add null elements, and others will impose + * restrictions on the type of elements that may be added. List + * classes should clearly specify in their documentation any restrictions + * on what elements may be added. + *

+ *

This implementation calls {@code add(size(), e)}. + *

+ *

Note that this implementation throws an + * {@code UnsupportedOperationException} unless + * {@link #add(int, Object) add(int, E)} is overridden. + * + * @param e element to be appended to this list + * @return {@code true} + * @throws UnsupportedOperationException if the {@code add} operation + * is not supported by this list + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws NullPointerException if the specified element is null and this + * list does not permit null elements + * @throws IllegalArgumentException if some property of this element + * prevents it from being added to this list + */ + @Override + public boolean add(E e) { + if (curSize>=maxSize){ + int t = maxSize; + int limit = 1024; + if(maxSizeset operation + * is not supported by this list + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws NullPointerException if the specified element is null and + * this list does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this list + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + @SuppressWarnings("unchecked") + public E set(int index, E element) { + if (index>curSize){ + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + E t = (E) arr[index]; + arr[index]=element; + return t; + } + + /** + * {@inheritDoc} + *

+ *

This implementation always throws an + * {@code UnsupportedOperationException}. + * + * @param index + * @throws UnsupportedOperationException {@inheritDoc} + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + if (index>curSize){ + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + E t = (E) arr[index]; + if (index + *

This implementation calls {@code removeRange(0, size())}. + *

+ *

Note that this implementation throws an + * {@code UnsupportedOperationException} unless {@code remove(int + * index)} or {@code removeRange(int fromIndex, int toIndex)} is + * overridden. + * + * @throws UnsupportedOperationException if the {@code clear} operation + * is not supported by this list + */ + @Override + public void clear() { + // 父类使用了迭代器 +// super.clear(); + curSize=0; + } + + /** + * {@inheritDoc} + *

+ *

This implementation first gets a list iterator (with + * {@code listIterator()}). Then, it iterates over the list until the + * specified element is found or the end of the list is reached. + * + * @param o + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException {@inheritDoc} + */ + @Override + public int indexOf(Object o) { + // 父类使用了迭代器 +// return super.indexOf(o); + for (int i = 0; i < curSize; i++) { + if (Objects.equals(o,arr[i])){ + return i; + } + } + return -1; + } + + /** + * {@inheritDoc} + *

+ *

This implementation returns size() == 0. + */ + @Override + public boolean isEmpty() { +// return super.isEmpty(); + return curSize==0; + } + + /** + * {@inheritDoc} + *

+ *

This implementation first gets a list iterator that points to the end + * of the list (with {@code listIterator(size())}). Then, it iterates + * backwards over the list until the specified element is found, or the + * beginning of the list is reached. + * + * @param o + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException {@inheritDoc} + */ + @Override + public int lastIndexOf(Object o) { + // 父类使用了迭代器 +// return super.lastIndexOf(o); + for (int i = curSize-1; i >=0; i--) { + if (Objects.equals(o,arr[i])){ + return i; + } + } + return -1; + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java new file mode 100644 index 0000000000..9fd8341981 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java @@ -0,0 +1,449 @@ +package cn.net.pikachu.other; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyLinkedList extends AbstractSequentialList implements Deque { + private Node head = new Node(); + private Node tail = head; + private int curSize = 0; + /** + * Returns a list iterator over the elements in this list (in proper + * sequence). + * + * @param index index of first element to be returned from the list + * iterator (by a call to the next method) + * @return a list iterator over the elements in this list (in proper + * sequence) + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @Override + public ListIterator listIterator(int index) { + // TODO + throw new UnsupportedOperationException(); + } + + /** + * Inserts the specified element at the front of this deque if it is + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerFirst}. + * + * @param e the element to add + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void addFirst(E e) { + head.next= new Node(e,head.next,head); + curSize++; + } + + /** + * Inserts the specified element at the end of this deque if it is + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerLast}. + *

+ *

This method is equivalent to {@link #add}. + * + * @param e the element to add + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void addLast(E e) { + Node t = new Node(e,null,tail); + tail.next=t; + tail=t; + curSize++; + } + + /** + * Inserts the specified element at the front of this deque unless it would + * violate capacity restrictions. When using a capacity-restricted deque, + * this method is generally preferable to the {@link #addFirst} method, + * which can fail to insert an element only by throwing an exception. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offerFirst(E e) { + throw new UnsupportedOperationException(); + } + + /** + * Inserts the specified element at the end of this deque unless it would + * violate capacity restrictions. When using a capacity-restricted deque, + * this method is generally preferable to the {@link #addLast} method, + * which can fail to insert an element only by throwing an exception. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offerLast(E e) { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves and removes the first element of this deque. This method + * differs from {@link #pollFirst pollFirst} only in that it throws an + * exception if this deque is empty. + * + * @return the head of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E removeFirst() { + if (curSize==0){ + throw new NoSuchElementException(); + } + E e = head.e; + head=head.next; + curSize--; + return e; + } + + /** + * Retrieves and removes the last element of this deque. This method + * differs from {@link #pollLast pollLast} only in that it throws an + * exception if this deque is empty. + * + * @return the tail of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E removeLast() { + if (curSize==0){ + throw new NoSuchElementException(); + } + // 怎么去去掉最后一个呢? + Node t=head; + while (t.next!=tail){ + t=t.next; + } + tail=t; + curSize--; + return t.next.e; + } + + /** + * Retrieves and removes the first element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the head of this deque, or {@code null} if this deque is empty + */ + @Override + public E pollFirst() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves and removes the last element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the tail of this deque, or {@code null} if this deque is empty + */ + @Override + public E pollLast() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves, but does not remove, the first element of this deque. + *

+ * This method differs from {@link #peekFirst peekFirst} only in that it + * throws an exception if this deque is empty. + * + * @return the head of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E getFirst() { + return head.e; + } + + /** + * Retrieves, but does not remove, the last element of this deque. + * This method differs from {@link #peekLast peekLast} only in that it + * throws an exception if this deque is empty. + * + * @return the tail of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E getLast() { + return tail.e; + } + + /** + * Retrieves, but does not remove, the first element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the head of this deque, or {@code null} if this deque is empty + */ + @Override + public E peekFirst() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves, but does not remove, the last element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the tail of this deque, or {@code null} if this deque is empty + */ + @Override + public E peekLast() { + throw new UnsupportedOperationException(); + } + + /** + * Removes the first occurrence of the specified element from this deque. + * If the deque does not contain the element, it is unchanged. + * More formally, removes the first element {@code e} such that + * (o==null ? e==null : o.equals(e)) + * (if such an element exists). + * Returns {@code true} if this deque contained the specified element + * (or equivalently, if this deque changed as a result of the call). + * + * @param o element to be removed from this deque, if present + * @return {@code true} if an element was removed as a result of this call + * @throws ClassCastException if the class of the specified element + * is incompatible with this deque + * (optional) + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * (optional) + */ + @Override + public boolean removeFirstOccurrence(Object o) { + Node t = head; + while (t.next!=null){ + if (Objects.equals(t.next.e,o)){ + t.next=t.next.next; + return true; + } + } + return false; + } + + /** + * Removes the last occurrence of the specified element from this deque. + * If the deque does not contain the element, it is unchanged. + * More formally, removes the last element {@code e} such that + * (o==null ? e==null : o.equals(e)) + * (if such an element exists). + * Returns {@code true} if this deque contained the specified element + * (or equivalently, if this deque changed as a result of the call). + * + * @param o element to be removed from this deque, if present + * @return {@code true} if an element was removed as a result of this call + * @throws ClassCastException if the class of the specified element + * is incompatible with this deque + * (optional) + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * (optional) + */ + @Override + public boolean removeLastOccurrence(Object o) { + return false; + } + + /** + * Inserts the specified element into the queue represented by this deque + * (in other words, at the tail of this deque) if it is possible to do so + * immediately without violating capacity restrictions, returning + * {@code true} upon success and {@code false} if no space is currently + * available. When using a capacity-restricted deque, this method is + * generally preferable to the {@link #add} method, which can fail to + * insert an element only by throwing an exception. + *

+ *

This method is equivalent to {@link #offerLast}. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offer(E e) { + return false; + } + + /** + * Retrieves and removes the head of the queue represented by this deque + * (in other words, the first element of this deque). + * This method differs from {@link #poll poll} only in that it throws an + * exception if this deque is empty. + *

+ *

This method is equivalent to {@link #removeFirst()}. + * + * @return the head of the queue represented by this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E remove() { + return null; + } + + /** + * Retrieves and removes the head of the queue represented by this deque + * (in other words, the first element of this deque), or returns + * {@code null} if this deque is empty. + *

+ *

This method is equivalent to {@link #pollFirst()}. + * + * @return the first element of this deque, or {@code null} if + * this deque is empty + */ + @Override + public E poll() { + return null; + } + + /** + * Retrieves, but does not remove, the head of the queue represented by + * this deque (in other words, the first element of this deque). + * This method differs from {@link #peek peek} only in that it throws an + * exception if this deque is empty. + *

+ *

This method is equivalent to {@link #getFirst()}. + * + * @return the head of the queue represented by this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E element() { + return null; + } + + /** + * Retrieves, but does not remove, the head of the queue represented by + * this deque (in other words, the first element of this deque), or + * returns {@code null} if this deque is empty. + *

+ *

This method is equivalent to {@link #peekFirst()}. + * + * @return the head of the queue represented by this deque, or + * {@code null} if this deque is empty + */ + @Override + public E peek() { + return null; + } + + /** + * Pushes an element onto the stack represented by this deque (in other + * words, at the head of this deque) if it is possible to do so + * immediately without violating capacity restrictions, throwing an + * {@code IllegalStateException} if no space is currently available. + *

+ *

This method is equivalent to {@link #addFirst}. + * + * @param e the element to push + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void push(E e) { + + } + + /** + * Pops an element from the stack represented by this deque. In other + * words, removes and returns the first element of this deque. + *

+ *

This method is equivalent to {@link #removeFirst()}. + * + * @return the element at the front of this deque (which is the top + * of the stack represented by this deque) + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E pop() { + return null; + } + + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over the elements in this deque in reverse + * sequential order. The elements will be returned in order from + * last (tail) to first (head). + * + * @return an iterator over the elements in this deque in reverse + * sequence + */ + @NotNull + @Override + public Iterator descendingIterator() { + return null; + } +} +class Node{ + public E e; + public Node next; + public Node prev; + public Node() { + } + + public Node(E e) { + this.e = e; + } + + public Node(E e, Node next, Node prev) { + this.e = e; + this.next = next; + this.prev = prev; + } +} \ No newline at end of file diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java new file mode 100644 index 0000000000..0179d5ef63 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyQueue { +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java new file mode 100644 index 0000000000..714c43e2a3 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyStack { +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java new file mode 100644 index 0000000000..42e226aa5f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyTree { +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java new file mode 100644 index 0000000000..8519eee121 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java @@ -0,0 +1,92 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by pikachu on 17-2-20. + */ +public class ArrayListTest { + ArrayList list; + @Before + public void before(){ + list=new ArrayList(); + } + @Test + public void testAdd(){ + list.add(1); + Assert.assertEquals("[1]",list.toString()); + Assert.assertEquals(1,list.size()); + list.add(0,0); + Assert.assertEquals("[0,1]",list.toString()); + Assert.assertEquals(2,list.size()); + list.add(1,2); + Assert.assertEquals("[0,2,1]",list.toString()); + Assert.assertEquals(3,list.size()); + + } + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove(){ + list.add(1); + list.remove(0); + Assert.assertEquals("[]",list.toString()); + Assert.assertEquals(0,list.size()); + list.add(2); + list.add(3); + list.add(4); + Object o =list.remove(1); + Assert.assertEquals(3,o); + Assert.assertEquals("[2,4]",list.toString()); + Assert.assertEquals(2,list.size()); + list.remove(4); + } + @Test(expected = IndexOutOfBoundsException.class) + public void testGet(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(1,list.get(0)); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals(4,list.size()); + list.get(4); + } + @Test + public void testSize(){ + Assert.assertEquals(0,list.size()); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(4,list.size()); + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Iterator iterator = list.iterator(); + StringBuilder builder = new StringBuilder(); + builder.append("["); + while (iterator.hasNext()){ + builder.append(iterator.next()).append(","); + } + builder.deleteCharAt(builder.length()-1); + builder.append("]"); + Assert.assertEquals(builder.toString(),list.toString()); + } + /* + // 抛出异常的测试 + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveWithException(){ + list.remove(2); + } + // 超时测试 + @Test(timeout = 2000) + public void test(){ + while (true); + } + */ +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..fcf666412f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java @@ -0,0 +1,56 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** +* BinaryTreeNode Tester. +* +* @author pikachu +* @since

二月 25, 2017
+* @version 1.0 +*/ +public class BinaryTreeNodeTest { + + BinaryTreeNode tree; + @Before + public void before() throws Exception { + tree = new BinaryTreeNode(); + } + + @After + public void after() throws Exception { + + } + + /** + * + * Method: insert(Object o) + * + */ + @Test + public void testInsert() { + for (int i = 0; i < 4; i++) { + tree.insert(i); + } + final StringBuilder builder = new StringBuilder(); + BinaryTreeNode.Visit visit = new BinaryTreeNode.Visit() { + @Override + public void visit(Object o) { + builder.append(o).append(","); + } + }; + builder.append("["); + tree.inOrderTraversal(visit,tree); + if (builder.length()>2){ + builder.deleteCharAt(builder.length()-1); + } + builder.append("]"); + Assert.assertEquals("[0,1,2,3]",builder.toString()); + + } + + +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java new file mode 100644 index 0000000000..361007228a --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java @@ -0,0 +1,116 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by pikachu on 17-2-25. + */ +public class LinkedListTest { + + LinkedList list; + @Before + public void before(){ + list = new LinkedList(); + } + @Test + public void testAdd(){ + list.add(1); + Assert.assertEquals("[1]",list.toString()); + Assert.assertEquals(1,list.size()); + list.add(0,0); + Assert.assertEquals("[0,1]",list.toString()); + Assert.assertEquals(2,list.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove(){ + list.add(1); + list.remove(0); + Assert.assertEquals("[]",list.toString()); + Assert.assertEquals(0,list.size()); + list.add(2); + list.add(3); + list.add(4); + Object o =list.remove(1); + Assert.assertEquals(3,o); + Assert.assertEquals("[2,4]",list.toString()); + Assert.assertEquals(2,list.size()); + list.remove(4); + } + @Test(expected = IndexOutOfBoundsException.class) + public void testGet(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(1,list.get(0)); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals(4,list.size()); + list.get(4); + } + @Test + public void testAddFirst(){ + list.add(1); + list.add(2); + list.add(3); + list.addFirst(0); + Assert.assertEquals(4,list.size()); + Assert.assertEquals(0,list.get(0)); + Assert.assertEquals("[0,1,2,3]",list.toString()); + } + @Test + public void testAddLast(){ + list.add(1); + list.add(2); + list.add(3); + list.addLast(4); + Assert.assertEquals(4,list.size()); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals("[1,2,3,4]",list.toString()); + } + @Test + public void testRemoveFirst(){ + list.add(1); + list.add(2); + list.add(3); + list.removeFirst(); + Assert.assertEquals(2,list.size()); + Assert.assertEquals("[2,3]",list.toString()); + } + @Test + public void testRemoveLast(){ + list.add(1); + list.add(2); + list.add(3); + list.removeLast(); + Assert.assertEquals(2,list.size()); + Assert.assertEquals("[1,2]",list.toString()); + } + @Test + public void testSize(){ + Assert.assertEquals(0,list.size()); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(4,list.size()); + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Iterator iterator = list.iterator(); + StringBuilder builder = new StringBuilder(); + builder.append("["); + while (iterator.hasNext()){ + builder.append(iterator.next()).append(","); + } + builder.deleteCharAt(builder.length()-1); + builder.append("]"); + Assert.assertEquals(builder.toString(),list.toString()); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java new file mode 100644 index 0000000000..ef115315d8 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java @@ -0,0 +1,57 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.NoSuchElementException; + +/** + * Created by pikachu on 17-2-25. + */ +public class QueueTest { + Queue queue; + @Before + public void before(){ + queue = new Queue(); + } + @Test + public void testEnQueue(){ + for (int i = 0; i < 4; i++) { + queue.enQueue(i); + } + Assert.assertEquals("[0,1,2,3]",queue.toString()); + Assert.assertEquals(4,queue.size()); + } + @Test(expected= NoSuchElementException.class) + public void testDeQueue(){ + for (int i = 0; i < 4; i++) { + queue.enQueue(i); + } + for (int i=0;i<4;i++) { + Assert.assertEquals(i,queue.deQueue()); + } + queue.deQueue(); + + } + @Test + public void testIsEmpty(){ + Assert.assertEquals(true,queue.isEmpty()); + queue.enQueue(1); + Assert.assertEquals(false,queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true,queue.isEmpty()); + } + @Test + public void testSize(){ + for (int i = 0; i < 4; i++) { + Assert.assertEquals(i,queue.size()); + queue.enQueue(i); + } + for (int i = 4; i > 0; i--) { + Assert.assertEquals(i,queue.size()); + queue.deQueue(); + } + Assert.assertEquals(0,queue.size()); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java new file mode 100644 index 0000000000..deea464a23 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java @@ -0,0 +1,105 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.util.*; + +/** +* Stack Tester. +* +* @author pikachu +* @since
二月 25, 2017
+* @version 1.0 +*/ +public class StackTest { + + Stack stack; + @Before + public void before() throws Exception { + stack = new Stack(); + } + + @After + public void after() throws Exception { + + } + + /** + * + * Method: push(Object o) + * + */ + @Test + public void testPush() { + Assert.assertEquals("[]",stack.toString()); + for (int i = 0; i < 4; i++) { + stack.push(i); + } + Assert.assertEquals("[0,1,2,3]",stack.toString()); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() { + for (int i = 0; i < 4; i++) { + stack.push(i); + } + for (int i = 3; i >= 0; i--) { + Assert.assertEquals(i,stack.pop()); + } + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() { + for (int i = 0; i < 4; i++) { + stack.push(i); + Assert.assertEquals(i,stack.peek()); + } + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() { + Assert.assertEquals(true,stack.isEmpty()); + for (int i = 0; i < 4; i++) { + stack.push(i); + } + Assert.assertEquals(false,stack.isEmpty()); + for (int i = 0; i < 4; i++) { + stack.pop(); + } + Assert.assertEquals(true,stack.isEmpty()); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() { + for (int i = 0; i < 4; i++) { + Assert.assertEquals(i,stack.size()); + stack.push(i); + } + Assert.assertEquals(4,stack.size()); + } + + +} diff --git a/group13/413007522/.gitignore b/group13/413007522/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/413007522/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/413007522/lesson01/.gitignore b/group13/413007522/lesson01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/413007522/lesson01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/413007522/lesson01/pom.xml b/group13/413007522/lesson01/pom.xml new file mode 100644 index 0000000000..b90f06b534 --- /dev/null +++ b/group13/413007522/lesson01/pom.xml @@ -0,0 +1,7 @@ + + 4.0.0 + cn.xl + lesson01_code + 0.0.1-SNAPSHOT + lesson01_code + \ No newline at end of file diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java new file mode 100644 index 0000000000..80464455e4 --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java @@ -0,0 +1,238 @@ +package cn.xl; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; + + +/** + * 1. ArrayList ��һ��������У��൱�ڶ�̬���顣��Java����Ա������ܶ�̬���ӣ� + * 2. ӵ�����ӡ�ɾ�����޸ġ������ȹ��ܡ� + * @author XIAOLONG + * + * @param + */ + +public class MyArrayList implements MyList { + + + private int size = 0; + + private final int initialCapacity = 3; + + private Object[] elementData = new Object[100]; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int modCount = 0; + + /** + * ����һ��Ĭ�ϳ�ʼ����Ϊ3�Ŀ��б� + * + */ + public MyArrayList() { + elementData = new Object[initialCapacity]; + } + + /** + * ����һ��ָ����ʼ�����Ŀ��б� + * @param initialCapacity + */ + public MyArrayList(int initialCapacity) { + + if (initialCapacity < 0){ + throw new IllegalArgumentException("Illegal initialCapacity: "+ initialCapacity); + }else if(initialCapacity == 0){ + elementData = EMPTY_ELEMENTDATA; + }else{ + elementData = new Object[this.initialCapacity]; + } + } + + /** + * + * ����һ������ָ��collection��Ԫ�ص��б�����ЩԪ�ذ��ո�collection�ĵ������������ǵ�˳������ + * ��MySubClass��MyClass�����ࡣ + * Collection myCollection; + * Collection mySubCollection; + * ArrayList myList = new ArrayList(myCollection); + * Ҳ���ԣ�ArrayList myList = new ArrayList(mySubCollection); + * ����MyClass����MyClass�������Collection�����Թ����ArrayList + * @param c + */ + public MyArrayList(Collection c) { + elementData = c.toArray(); + if((size = elementData.length) != 0){ + //c.toArray might (incorrectly) not return Object[] (see 6260652) + //�ٷ�bug������ת�Ͳ���ȫ + //����������Object[]������Arrays.copyOf��������תΪObject[] + if (elementData.getClass() != Object[].class) + elementData = Arrays.copyOf(elementData, size, Object[].class); + }else{ + elementData = EMPTY_ELEMENTDATA; + } + } + + + /** + * ��ָ����Ԫ��������б���ָ��λ���ϵ�Ԫ�� + * @param index + * @param element + * @return Object(��ǰλ�ڸ�λ���ϵľ�Ԫ��) + */ + public Object set(int index, Object element) { + if (index >= size()) + throw new RuntimeException("The Index: "+index+" is out of band."); + + Object oldValue = elementData[index]; + elementData[index] = element; + return oldValue; + } + + /** + * ����Ԫ�����б�β�� + * @param e + */ + public void add(Object e) { + if (e == null) { + throw new RuntimeException("The value should not be null."); + } + if (size() >= initialCapacity) { + ensureCapacity(size() + 1); + } + elementData [size] = e; + size++; + } + + /** + * ��Ԫ�����ӵ��б�ָ��λ�� + * @param index + * @param element + */ + public void add(int index, Object o) { + if (index >= size || index < 0) + throw new RuntimeException("The Index: "+index+" is out of band."); + // ������鳤�Ȳ��㣬���������ݡ� + ensureCapacity(size+1); + // �� elementData�д�Indexλ�ÿ�ʼ������Ϊsize-index��Ԫ�أ� + // ���������±�Ϊindex+1λ�ÿ�ʼ���µ�elementData�����С� + // ������ǰλ�ڸ�λ�õ�Ԫ���Լ����к���Ԫ������һ��λ�á� + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + + /** + * ���ش��б���ָ��λ���ϵ�Ԫ�� + * @param index + * @return + */ + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + return elementData [index]; + } + + /** + * ɾ��ָ��λ��Ԫ�� + * @param ɾ��Ԫ������λ�ã���0��ʼ + * @return Object(��ɾ��ָ��λ���ϵľ�Ԫ��) + */ + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + modCount++; + Object oldElement = elementData[index]; + //�˴���ȻҲ����System.arraycopy ʵ�� + for (int i = index; i < size - 1; i++) { + elementData [i] = elementData [i + 1]; + } + elementData [size - 1] = null; + size--; + return oldElement; + } + + /** + * �������ݣ�ÿ�������ռ�Ϊ 50%+1 + * @param ��ǰ����������С����ֵ + */ + private void ensureCapacity(int minCapacity) { + modCount++; + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + //λ�����㣬�൱�ڳ���2��������Щ�� int newCapacity = (oldCapacity * 3)/2 + 1; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + /** + * ��List�е�Ԫ�ظ���. + * @return ��List�е�Ԫ�ظ��� + */ + public int size() { + return size; + } + + /** + * �����ListԪ�����е�һ�������� + * @return ��ListԪ�����еĵ����� + */ + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor; // ��һ��Ԫ�ص�����λ�� + int lastRet = -1; // ��һ��Ԫ�ص�����λ�� + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + int i = cursor; + if (i >= size){ + throw new RuntimeException("No such element."); + } + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length){ + throw new RuntimeException("This list is being modified."); + } + + cursor = i + 1; + return elementData[lastRet = i]; + } + + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new RuntimeException("This list is being modified."); + } + } + } + +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java new file mode 100644 index 0000000000..ee4c4e461e --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java @@ -0,0 +1,218 @@ +package cn.xl; + +import java.util.Iterator; + +/** + * + * @author XIAOLONG + * @param + * + */ +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node first; + + private Node last; + + + /** + * һ���޲ι��캯��������һ����List + * + */ + public MyLinkedList(){ + + } + + + + /** + * ����Ԫ�����б�β�� + * @param Object(���ӵ�Ԫ��) + */ + public void add(Object o) { + + addLast(o); + } + + + /** + * ���б�ָ��λ������Ԫ��,���Ǿ�Ԫ�� + * @param index ����λ�ã�Object ����Ԫ�� + */ + public void add(int index, Object o) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + Node x = node(index); + x.data = o; + + } + + /** + * ��ȡָ��λ�õ�Ԫ��data + * @param index ��Ҫ��ȡԪ������λ�� + */ + public Object get(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + return node(index).data; + } + + + /** + * ɾ��ָ��λ��Ԫ�� + * @param index ɾ���б�Ԫ������λ�� + */ + public Object remove(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + final Node node = node(index); + final Object o = node.data; + if(first.equals(node)){ + removeFirst(); + }else if(last.equals(node)){ + removeLast(); + }else{ + final Node prev = node.prev; + final Node next = node.next; + + prev.next = next; + next.prev = prev; + node.prev = null; + node.next = null; + } + node.data = null; + size --; + return o; + } + + + /** + * ��ȡ�б���ǰsize + */ + public int size() { + return size; + } + + /** + * ����ͷԪ��,���ͷԪ��Ϊ�գ������ø�Ԫ��ͬʱΪβԪ�� + * @param Object�� ���ӵ�ͷԪ�أ� + */ + public void addFirst(Object o){ + + final Node f = first; + final Node newNode = new Node(null,o,f); + if(f == null){ + last = newNode; + }else{ + f.prev = newNode; + } + size ++; + } + + /** + * ����βԪ�أ����βԪ��Ϊ�գ�ͬʱ���ø�Ԫ��ΪͷԪ�� + * @param Object(���ӵ�βԪ��) + */ + public void addLast(Object o){ + + final Node l = last; + final Node newNode = new Node(l,o,null); + if(l == null){ + first = newNode; + }else{ + l.next = newNode; + } + size ++; + } + + /** + * �Ƴ���һ��Ԫ�أ�����Ƴ��Ժ��б�Ϊ���� first = next = null + * @return �����Ƴ�Ԫ�� + */ + public Object removeFirst(){ + + final Node f = first; + final Object o = f.data; + final Node next = f.next ; + f.next = null; + f.data = null; + first = next; + if(next == null){ + last = next; + }else{ + next.prev = null; + } + size --; + return o; + } + + /** + * �Ƴ����һ��Ԫ�� + * @return �����Ƴ�Ԫ�� + */ + public Object removeLast(){ + + final Node l = last; + final Object o = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if(prev == null){ + last = null; + }else{ + prev.next = null; + } + size --; + return o; + } + public Iterator iterator(){ + return null; + } + + /** + * ����Node�ڲ��� + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + + Node(Node prev,Object o,Node next){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + /** + * ����һ����ȡ�±�λ��node�ķ��� + * �����ǰ�±�С��list���ȵ�һ�䣬���ͷ��ʼ���������� ��β����ʼ���� + * @param index ����Ԫ�ص�λ�� + */ + private Node node(int 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; + } + } +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java new file mode 100644 index 0000000000..f9769ba33a --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java @@ -0,0 +1,9 @@ +package cn.xl; + +public interface MyList { + 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/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java new file mode 100644 index 0000000000..cd61cfd8c8 --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java @@ -0,0 +1,110 @@ +package cn.xl; + +/** + * Queue一个先进先出(first in first out,FIFO)得队列 + * 所谓的队列,也是一个含有至少两个基本操作的抽象数据类型:插入新的元素;删除最久时间插入的元素。 + * 遵循FIFO(First in,first out,先进先出)的原则。 + * MyQueue采用环形数组实现 + * @author XIAOLONG + * + */ +public class MyQueue { + + private int size,head,tail; + + private Object[] elementData; + + private final int initialCapacity = 4; + + public MyQueue(){ + + head = tail = -1; + elementData = new Object[initialCapacity]; + + } + + /** + * 向队列中添加元素 + * @param o + */ + public void enQueue(Object o){ + + ensureCapacity(); + + if( head == -1) { + tail = head = 0; + } + size ++; + elementData[tail] = o; + tail ++; + + + } + + /** + * 删除栈顶元素,并返回旧栈顶元素 + * @return 旧栈顶元素 + */ + public Object deQueue(){ + Object element = elementData[head]; + if(head == tail){ + head = tail = -1; + }else if(head == elementData.length-1){ + head = 0; + }else{ + head ++; + } + size --; + return element; + } + + /** + * 判断队列是否为空 + * @return + */ + public boolean isEmpty(){ + return head == -1; + } + + /** + * 返回自身长度 + * @return + */ + public int size(){ + return size; + } + + /** + * 判断队列是否已满 + * @return + */ + public boolean isFull() { + return (head == 0 && tail == elementData.length); + } + + /** + * 扩展容量,如果队列有效数据已经占满空间则增加2,否则覆盖无效数据,重新分配数据空间 + * @param 当前队列所需最小容量size + */ + private void ensureCapacity(){ + + if(isFull()){ + Object [] oldData = elementData; + elementData = new Object[elementData.length + 2]; + System.arraycopy(oldData, head,elementData , 0, oldData.length); + }else if(head > 0){ + Object [] oldData = elementData; + System.arraycopy(oldData, head,elementData , 0, oldData.length-head); + tail = tail - head ; + head = 0; + } + } + + + + public static void main(String[] args) + { + + + } +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java new file mode 100644 index 0000000000..ac78828f5c --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java @@ -0,0 +1,124 @@ +package cn.xl; + +import java.util.Arrays; +import java.util.EmptyStackException; + +/** + * Stack��һ������ȳ���last in first out��LIFO���Ķ�ջ�� + * ��Vector��Ļ�������չ5���������� + * @author XIAOLONG + * + */ +public class MyStack { + + private int elementCount; + + private Object[] elementData; + + /** + * �޲ι��췽��������һ����ջ + * + */ + public MyStack(){ + + } + + + /** + * Ԫ����ջ + * @param item + * @return ��ջԪ�� + */ + public synchronized Object push(Object item){ + + ensureCapacity(elementCount+1); + elementData[elementCount] = item; + elementCount ++; + return item; + } + + /** + * ��ջ��Ԫ���Ƴ��������ظ�Ԫ�� + * @return ջ��Ԫ�� + */ + public synchronized Object pop(){ + Object obj; + + obj = peek(); + elementCount --; + elementData[elementCount] = null; + + return obj; + } + + /** + * �鿴ջ��Ԫ�� + * + * @return ջ��Ԫ�� + * @throws ���ջΪ�� �� �׳� EmptyStackException�쳣 . + */ + public synchronized Object peek(){ + int len = elementCount; + + if(len == 0) + throw new EmptyStackException(); + + return elementData[len - 1]; + + } + + /** + * ���ջ�Ƿ�Ϊ�� + * + * @return True or false + */ + public boolean isEmpty(){ + + return elementCount == 0; + } + + /** + * ��ѯռջ�Ƿ����ijԪ�� + * @param ��ѯԪ�� + * @return ���Ԫ�ش��ڷ���Ԫ������λ�ã�ջ��Ԫ��λ��Ϊ1���� + * �����Ԫ����ջ�����ظ����򷵻ؾ���ջ�������Ԫ��λ�ã� + * �����Ԫ����ջ�в����ڣ��򷵻� -1 �� + */ + public synchronized int search(Object o){ + + if(o == null){ + for(int i = elementCount -1;i >= 0; i--){ + if(elementData[i] == null){ + return elementCount - i; + } + } + }else{ + for(int i = elementCount -1;i >= 0; i-- ){ + if(o.equals(elementData[i])){ + return elementCount - i; + } + } + } + + return -1; + } + + /** + * ��չ��������������������һ�� + * @param ��ǰջ������С����size + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + int newCapacity = oldCapacity << 1; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public static void main(String[] args){ + + + + } + +} diff --git a/group13/453070251/lessones01/BinaryTreeNode.java b/group13/453070251/lessones01/BinaryTreeNode.java new file mode 100644 index 0000000000..f5d8c5dacd --- /dev/null +++ b/group13/453070251/lessones01/BinaryTreeNode.java @@ -0,0 +1,23 @@ +public class BinaryTreeNode{ + public BinaryTreeNode left; + public BinaryTreeNode right; + public int value; + public BinaryTreeNode(int arg_value){ + value = arg_value; + } + public void add(int arg_value){ + if(arg_valuevalue){ + if(right == null){ + right = new BinaryTreeNode(arg_value); + }else{ + right.add(arg_value); + } + } + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/LinkedList.java b/group13/453070251/lessones01/LinkedList.java new file mode 100644 index 0000000000..c1cefc463f --- /dev/null +++ b/group13/453070251/lessones01/LinkedList.java @@ -0,0 +1,116 @@ +public class LinkedList{ + public static void main(String[] args){ + LinkedList arr = new LinkedList(); + arr.add("0"); + System.out.println(arr.get(0)); + + System.out.println(arr.get(1)); + arr.add("1"); + System.out.println(arr.get(1)); + + + System.out.println(arr.get(3)); + arr.add("3",3); + System.out.println(arr.get(3)); + arr.add("before 2",2); + System.out.println(arr.get(2)); + System.out.println(arr.get(4)); + arr.set(4,"4"); + System.out.println(arr.get(4)); + arr.set(8,"8"); + System.out.println(arr.get(8)); + + } + protected Node first; + protected Node lastest; + private int size; + public LinkedList(){ + lastest = first = new Node(null); + size = 0; + } + public T get(int arg_num){ + //throw Error if arg_num<0,unfinished + return _get(arg_num); + } + public void set(int arg_num,T arg_value){ + //throw Error if arg_num<0,unfinished + _set(arg_num,arg_value); + } + public T remove(int arg_num){ + //throw Error if arg_num<0,unfinished + return _remove(arg_num).value; + } + public void add(T arg_value,int arg_num){ + //throw Error if arg_num<0,unfinished + _add(arg_value,arg_num); + } + public void add(T arg_value){ + _add(arg_value,size); + } + public int size(){ + return size; + } + protected Node _remove(int arg_num){ + if(arg_num node = _getNode(arg_num); + node.remove(); + size--; + if(arg_num == size){ + lastest = node.left; + }else if(arg_num == 0 && size != 0){ + first = node.right; + } + return node; + }else{ + return null; + } + } + protected Node _getNode(int arg_num){ + Node node = first; + for(int num = 0;num=size){return null;} + return _getNode(arg_num).value; + } + protected void _set(int arg_num,T arg_value){ + if(arg_num>=size){ + _add(arg_value,arg_num); + }else{ + _getNode(arg_num).value = arg_value; + } + } + protected void _add(T arg_value,int arg_num){ + if(arg_num > size){ + Node n = new Node(null); + lastest.setRight(n); + for(int num=size+1;num(null)); + n = n.right; + } + n.setRight(new Node(arg_value)); + lastest = n.right; + size = arg_num+1; + }else if(arg_num == size){ + if(size == 0){ + lastest.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + }else if(arg_num == 0){ + first.setLeft(new Node(arg_value)); + first = first.left; + size++; + }else{ + Node node = _getNode(arg_num); + node.addLeft(new Node(arg_value)); + size++; + } + + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/LinkedListWithFloat.java b/group13/453070251/lessones01/LinkedListWithFloat.java new file mode 100644 index 0000000000..803faa43d3 --- /dev/null +++ b/group13/453070251/lessones01/LinkedListWithFloat.java @@ -0,0 +1,86 @@ +public class LinkedListWithFloat extends LinkedList{ + public static void main(String[] args){ + LinkedListWithFloat arr = new LinkedListWithFloat(); + arr.add("0"); + System.out.println(arr.get(0)); + arr.add("1"); + System.out.println(arr.get(1)); + + System.out.println(arr.get(3)); + arr.add("3",3); + System.out.println(arr.get(3)); + arr.add("before 2",2); + System.out.println(arr.get(2)); + System.out.println(arr.get(4)); + arr.set(4,"4"); + System.out.println(arr.get(4)); + arr.set(8,"8"); + System.out.println(arr.get(8)); + System.out.println(arr.remove(8)); + System.out.println(arr.get(8)); + System.out.println(arr.remove(2)); + System.out.println(arr.get(2)); + System.out.println(arr.remove(0)); + System.out.println(arr.get(0)); + System.out.println(arr.size()); + } + + private Node floatNode; + private int floatNum; + public LinkedListWithFloat(){ + super(); + floatNode = first; + floatNum = 0; + } + protected Node _getNode(int arg_num){ + int position = arg_num - floatNum; + if(position == 0){return floatNode;} + //arg_num = (int leftPosition = arg_num); + int rightPosition = size() - 1 - arg_num; + boolean floatNear = position * position <= arg_num * rightPosition; + if(floatNear){ + if(position>0){ + return findFloatRight(arg_num); + }else/*if(position<0)*/{ + return findFloatLeft(arg_num); + } + }else if(position<0){ + floatNode = first; + floatNum = 0; + return findFloatRight(arg_num); + }else/*if(position>0)*/{ + floatNode = lastest; + floatNum = rightPosition + arg_num;//size()-1 + return findFloatLeft(arg_num); + } + } + protected void _add(T arg_value,int arg_num){ + super._add(arg_value,arg_num); + if(arg_num <= floatNum){floatNum++;} + } + protected Node _remove(int arg_num){ + Node temp = super._remove(arg_num); + if(temp == null){return temp;} + if(temp.right != null){ + floatNode = temp.right; + }else{ + floatNode = lastest; + floatNum = size()-1; + } + return temp; + } + private Node findFloatLeft(int arg_num){ + for(;floatNum>arg_num;){ + floatNode = floatNode.left; + floatNum--; + } + return floatNode; + } + private Node findFloatRight(int arg_num){ + for(;floatNum{ + public Node left; + public Node right; + public T value; + public Node(T arg_value){ + value = arg_value; + } + public void setLeft(Node arg_another){ + _connect(arg_another,this); + } + public void setRight(Node arg_another){ + _connect(this,arg_another); + } + public void addLeft(Node arg_another){ + _connect(left,arg_another,this); + } + public void addRight(Node arg_another){ + _connect(this,arg_another,right); + } + public Node remove(){ + _connect(left,right); + return this; + } + private void _connect(Node arg_left,Node arg_right){ + if(arg_left != null){arg_left.right = arg_right;} + if(arg_right != null){arg_right.left = arg_left;} + } + private void _connect(Node arg_left,Node node,Node arg_right){ + _connect(arg_left,node); + _connect(node,arg_right); + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/Queue.java b/group13/453070251/lessones01/Queue.java new file mode 100644 index 0000000000..6e54c1add2 --- /dev/null +++ b/group13/453070251/lessones01/Queue.java @@ -0,0 +1,40 @@ +public class Queue{ + Node first; + Node lastest; + private int size; + public Queue(){ + first = lastest = new Node(null); + size = 0; + } + public int size(){ + return size; + } + public Queue push_back(T arg_value){ + if(size == 0){ + first.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + return this; + } + public T pop_front(){ + if(size > 0){size--;}; + if(size == 0){ + T temp; + temp = first.value; + first.value = null; + return temp; + }else{ + first = first.right; + return first.left.remove().value; + } + } + public T front(){ + return first.value; + } + public T back(){ + return lastest.value; + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/Stack.java b/group13/453070251/lessones01/Stack.java new file mode 100644 index 0000000000..a87cb59268 --- /dev/null +++ b/group13/453070251/lessones01/Stack.java @@ -0,0 +1,40 @@ +public class Stack{ + Node first; + Node lastest; + private int size; + public Stack(){ + first = lastest = new Node(null); + size = 0; + } + public int size(){ + return size; + } + public Stack push(T arg_value){ + if(size == 0){ + lastest.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + return this; + } + public T pop(){ + if(size > 0){size--;}; + if(size == 0){ + T temp; + temp = lastest.value; + lastest.value = null; + return temp; + }else{ + lastest = lastest.left; + return lastest.right.remove().value; + } + } + public T peek(){ + return lastest.value; + } + public boolean empty(){ + return size == 0; + } +} \ No newline at end of file diff --git a/group13/568334413/0226/src/Main.java b/group13/568334413/0226/src/Main.java new file mode 100644 index 0000000000..3d3750475a --- /dev/null +++ b/group13/568334413/0226/src/Main.java @@ -0,0 +1,123 @@ +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; + +import java.util.ArrayList; +import java.util.Stack; + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + + com.coding.basic.TreeSet treeSet = new com.coding.basic.TreeSet(); + treeSet.add(1); + treeSet.add(2); + treeSet.add(3); + treeSet.add(4); + System.out.println("treeSet = " + treeSet.size); + + + } + + public static void testQueueIterator() { + Queue queue = new Queue(); + queue.enQueue("0"); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + while (queue.iterator.hasNext()) { + System.out.println("next === " + queue.iterator.next()); + } + + } + + public static void testLinkedListIterator() { + com.coding.basic.LinkedList arrayList = new com.coding.basic.LinkedList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + arrayList.add("4"); + arrayList.add("5"); + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println("next === " + iterator.next()); + } + } + + + public static void testArrayListIterator() { + com.coding.basic.ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + arrayList.add("4"); + arrayList.add("5"); + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println("next === " + iterator.next()); + System.out.println("next =2== " + iterator.next()); + } + } + + public static void linkedList() { + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + stack.push("4"); + String a = stack.peek(); + System.out.println("a = " + a); + +// LinkedList linkedList = new LinkedList(); +// linkedList.add("0"); +// linkedList.add("1"); +// linkedList.add("2"); +// linkedList.add("3"); +// linkedList.get(1); +// linkedList.size(); + LinkedList linkedList = new LinkedList(); + linkedList.add("0"); + linkedList.add("1"); + linkedList.add(1, "2"); + linkedList.toString(); + } + + public static void arrayList() { + ArrayList arrayList = new ArrayList(); + arrayList.add("1"); + arrayList.add(null); + arrayList.add(null); + arrayList.add(null); + arrayList.add(null); + arrayList.add("2"); + arrayList.remove(1); + +// arrayList.add(7, "100"); + + System.out.println("arrayList = " + arrayList.size()); + java.util.List list = new ArrayList(); + + com.coding.basic.ArrayList myList = new com.coding.basic.ArrayList(); + myList.add("0"); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + myList.add("5"); + myList.add("6"); + myList.add("7"); + myList.add(8, "8"); + System.out.println("myList = " + myList.get(9)); + + System.out.println("myList = " + myList.toString()); + myList.remove(1); +// System.out.println("myList = " + myList.size()); + + System.out.println("myList = " + myList.toString()); + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/ArrayList.java b/group13/568334413/0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9592af4050 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + dilatation(); + size = size + 1; + elementData[size - 1] = o; + } + + + public void add(int index, Object o) { + //是否超出目前大小+1; + //是否需要先扩容 + //移动数组 + checkRange(index); + + dilatation(); + //手动移动素组 + for (int i = size; i >= index; i--) { + Object o1 = elementData[i]; + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size = size + 1; + + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + public Object remove(int index) { + checkRange(index); + + Object object = elementData[index]; + + //native 方法,未知细节 + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size = size - 1; + elementData[size] = null; + return object; + } + + @Override + public int size() { + return this.size; + } + + public void checkRange(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + public void dilatation() { + if (elementData.length == size + 1) { + Object[] tempElementData = elementData; + elementData = new Object[elementData.length * 2]; + for (int i = 0; i < tempElementData.length; i++) { + elementData[i] = tempElementData[i]; + } + } + } + + @Override + public String toString() { + StringBuilder value = new StringBuilder(); + for (int i = 0; i < size; i++) { + Object object = elementData[i]; + value.append(object.toString() + ","); + } + return value.toString(); + } + + Iterator mIterator = new MIterator(); + + public Iterator iterator() { + return mIterator; + } + + class MIterator implements Iterator { + int index = 0; + + @Override + public boolean hasNext() { + if (index < size) { + + return true; + } + index = 0; + return false; + } + + @Override + public Object next() { + Object object = elementData[index]; + index++; + return object; + } + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java b/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e1f8840552 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Integer getData() { + return data; + } + + public void setData(Integer data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} diff --git a/group13/568334413/0226/src/com/coding/basic/Iterator.java b/group13/568334413/0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..96adcd6d3a --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group13/568334413/0226/src/com/coding/basic/LinkedList.java b/group13/568334413/0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d3ff3fd907 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.coding.basic; + +public class LinkedList implements List { + private int size = 0; + private Node headNode; + private Node lastNode; + + public LinkedList() { + headNode = new Node(); + lastNode = new Node(); + headNode.next = lastNode; + lastNode.prev = headNode; + } + + public void add(Object o) { + Node node = new Node(); + node.data = o; + if (size == 0) { + headNode.next = node; + lastNode.prev = node; + node.next = lastNode; + node.prev = headNode; + + } else { + Node tempLastNode = lastNode.prev; + tempLastNode.next = node; + + node.next = lastNode; + node.prev = tempLastNode; + lastNode.prev = node; + } + size = size + 1; + + } + + public void add(int index, Object o) { + checkRange(index); + Node preNode = null; + Node nextNode = null; + for (int i = 0; i <= index; i++) { + preNode = headNode.next; + } + nextNode = preNode.next; + Node newNode = new Node(); + newNode.data = o; + + preNode.next = newNode; + nextNode.prev = newNode; + + newNode.next = nextNode; + newNode.prev = preNode; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + public void checkRange(int index) { + if (index > size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + public Object get(int index) { + checkRange(index); + Node node = null; + for (int i = 0; i <= index; i++) { + node = headNode.next; + } + return node; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node oldFirstnode = headNode.next; + Node node = new Node(); + node.data = o; + headNode.next = node; + node.next = oldFirstnode; + node.prev = headNode; + oldFirstnode.prev = node; + } + + public void addLast(Object o) { + Node oldLastNode = lastNode.prev; + + Node node = new Node(); + node.data = o; + node.prev = oldLastNode; + node.next = lastNode; + + oldLastNode.next = node; + lastNode.prev = node; + + } + + public Object removeFirst() { + Node firstNode = headNode.next; + Node newFirstNode = firstNode.next; + newFirstNode.prev = headNode; + headNode.next = newFirstNode; + return firstNode; + } + + public Object removeLast() { + Node lastOldNode = lastNode.prev; + lastNode.prev = lastOldNode.prev; + lastOldNode.prev.next = lastNode; + + return lastOldNode; + } + + Iterator iterator = new MIterator(); + + public Iterator iterator() { + return iterator; + } + + @Override + public String toString() { + return super.toString(); + + } + + private static class Node { + Object data; + Node next; + Node prev; + } + + class MIterator implements Iterator { + Node node; + + @Override + public boolean hasNext() { + if (node == null) { + node = headNode.next; + } + if (!node.equals(lastNode)) { + System.out.println("ture"); + + return true; + } + node = null; + return false; + } + + @Override + public Object next() { + if (node.equals(headNode)) { + node = headNode.next; + return node; + } + Node tempNode = node; + node = node.next; + if (node == null) { + throw new IndexOutOfBoundsException("out of "); + } + return tempNode.data; + } + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/List.java b/group13/568334413/0226/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group13/568334413/0226/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(); +} diff --git a/group13/568334413/0226/src/com/coding/basic/Queue.java b/group13/568334413/0226/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3113e1b91a --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Queue { + LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.addLast(o); + } + + public Object deQueue() { + Object o = linkedList.removeLast(); + return o; + } + + public boolean isEmpty() { + if (linkedList.size() == 0) { + return true; + } + return false; + } + + public int size() { + return linkedList.size(); + } + + public Iterator iterator = linkedList.iterator(); +} diff --git a/group13/568334413/0226/src/com/coding/basic/Stack.java b/group13/568334413/0226/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d53ba4b075 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object object = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return object; + } + + public Object peek() { + Object object = elementData.get(elementData.size() - 1); + return object; + } + + public boolean isEmpty() { + if (elementData.size() > 0) { + return false; + } + return true; + + } + + public int size() { + return elementData.size(); + } + + Iterator iterator = elementData.iterator(); +} diff --git a/group13/568334413/0226/src/com/coding/basic/TreeSet.java b/group13/568334413/0226/src/com/coding/basic/TreeSet.java new file mode 100644 index 0000000000..47334ba54f --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/TreeSet.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +/** + * Created by laibin on 2017/2/25. + */ +public class TreeSet { + BinaryTreeNode root = null; + public int size = 0; + + public void add(Integer integer) { + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.setData(integer); + if (root == null) { + root = binaryTreeNode; + size++; + return; + } + insert(root, binaryTreeNode); + } + + void insert(BinaryTreeNode node, BinaryTreeNode tempNode) { + if (tempNode.getData() < node.getData()) { + if (node.getLeft() == null) { + node.setLeft(tempNode); + size++; + return; + } + insert(node.getLeft(), tempNode); + } else if (tempNode.getData() > node.getData()) { + if (node.getRight() == null) { + node.setRight(tempNode); + size++; + return; + } + insert(node.getRight(), tempNode); + } + } + +} diff --git a/group13/6023757/lesson2/mytest/.settings/org.eclipse.jdt.core.prefs b/group13/6023757/lesson2/mytest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group13/6023757/lesson2/mytest/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java b/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java new file mode 100644 index 0000000000..99f2c43a6d --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java @@ -0,0 +1,86 @@ +package mytest; + +import java.util.Arrays; + +public class ArrayList { + private int size = 0; + private Object[] elementData; + + public ArrayList(){ + elementData = new Object[10]; + } + + public ArrayList(int InitSize){ + elementData = new Object[InitSize]; + } + public void add(Object o){ + if(this.size == this.elementData.length){ + this.enlarge(); + + } + this.elementData[size] = o; + this.size++; + } + + public void add(int index, Object o){ + if(this.size == this.elementData.length){ + this.enlarge(); + } + if(index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + else{ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 0, newArray, 0, index); + newArray[index] = o; + System.arraycopy(this.elementData, index, newArray, index + 1, this.size - index); + this.elementData = newArray; + this.size++; + } + } + + public Object get(int index){ + if(index > -1 && index < this.size()){ + return elementData[index]; + } + else{ + return null; + } + } + + public void remove(int index){ + if(index >= this.size){ + System.out.println("Index is out of scope"); + return; + } + + if(index == this.size - 1){ + this.elementData[index] = null; + } + else if(index == 0){ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 1, newArray, 0, this.size -1); + this.elementData = newArray; + } + else{ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 0, newArray, 0, index-1); + System.arraycopy(this.elementData, index, newArray, index-1, this.size - index); + this.elementData = newArray; + } + + this.size--; + } + + public int size(){ + return this.size; + } + + public void enlarge(){ + Object[] newArray = new Object[this.elementData.length+5]; + System.arraycopy(this.elementData, 0, newArray, 0, this.elementData.length); + this.elementData = newArray; + } + +} + diff --git a/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java b/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java new file mode 100644 index 0000000000..8cfb0d6d2d --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java @@ -0,0 +1,116 @@ +package mytest; + +public class LinkedList { + private int size = 0; + private Node firstNode = null; + private Node lastNode = null; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + if(lastNode == null){ + firstNode = newNode; + lastNode = newNode; + } + else{ + lastNode.next = newNode; + lastNode = newNode; + } + this.size++; + } + public void add(int index , Object o){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + if(index == 0){ + addFirst(o); + } + else if(index == this.size-1){ + addLast(o); + } + else{ + Node pointer = this.firstNode; + for(int i = 0;i < index - 1;i++){ + pointer = pointer.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = pointer.next; + pointer.next = newNode; + this.size++; + } + + } + public Object get(int index){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + Node pointer = this.firstNode; + for(int i = 0;i < index;i++){ + pointer = pointer.next; + } + return pointer.data; + } + public void remove(int index){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + if(index == 0){ + removeFirst(); + } + else if(index == this.size-1){ + removeLast(); + } + else{ + Node pointer = this.firstNode; + for(int i = 0;i < index - 1;i++){ + pointer = pointer.next; + } + Node toBeDelete = pointer.next; + pointer.next = toBeDelete.next; + toBeDelete.next = null; + this.size--; + } + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = firstNode; + firstNode = newNode; + this.size++; + } + public void addLast(Object o){ + add(o); + } + public void removeFirst(){ + Node NextNode = firstNode.next; + firstNode.next = null; + firstNode = NextNode; + this.size--; + } + public void removeLast(){ + Node pointer = this.firstNode; + for(int i = 0;i < this.size - 2;i++){ + pointer = pointer.next; + } + Node toBeDelete = pointer.next; + pointer.next = null; + lastNode = pointer; + toBeDelete.next = null; + this.size--; + } + + private static class Node{ + Object data; + Node next; + + } + +} diff --git a/group13/6023757/lesson2/mytest/src/mytest/myMain.java b/group13/6023757/lesson2/mytest/src/mytest/myMain.java new file mode 100644 index 0000000000..f66910c724 --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/myMain.java @@ -0,0 +1,50 @@ +package mytest; + +public class myMain { + + public static void main(String[] args) { + // TODO Auto-generated method stub + myMain myTest = new myMain(); +// myTest.testArrayList(); + myTest.testLinkedList(); + } + + public void testLinkedList(){ + LinkedList myList = new LinkedList(); + myList.add("aa"); + myList.add("bb"); + myList.add("cc"); + myList.add("dd"); + myList.add("ee"); + myList.add("ff"); + myList.add("gg"); + myList.removeLast(); + for(int i = 0;i < myList.size();i++){ + System.out.println(myList.get(i)); + } + + } + + public void testArrayList(){ + ArrayList myArray = new ArrayList(5); + myArray.add("xx1"); + myArray.add("xx2"); + myArray.add("xx3"); + myArray.add("xx4"); + myArray.add("xx5"); + myArray.add("xx6"); + myArray.add("xx7"); + myArray.add("xx8"); + myArray.add("xx9"); + myArray.add("xx10"); + myArray.add("xx11"); + myArray.add("xx12"); + myArray.add("xx13"); + myArray.remove(8); + int i; + for(i=0;i size){ + System.out.println("输入有误"); + return; + } + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size) { + System.out.println("出界"); + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index > size){ + System.out.println("输入有误"); + return null; + } + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[size--] = null; + return o; + } + + public int size(){ + return elementData.length; + } + //判断是否越界 如果越界添加长度 + public void checkLength(int l){ + int oldSize = elementData.length; + if(l > oldSize){ //大于原来的长度创建新的数组 + elementData = Arrays.copyOf(elementData, l+100); + } + } + + public Iterator iterator(){ + return new Iterator(){ + int cursor; + public boolean hasNext() { + + return cursor != size ; + } + + public Object next() { + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData_ = elementData; + if (i >= elementData_.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData_[i]; + } + + }; + + } + + + + + +} diff --git a/group13/771992096/src/com/java/gsl/Iterator.java b/group13/771992096/src/com/java/gsl/Iterator.java new file mode 100644 index 0000000000..b2f2a258d8 --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Iterator.java @@ -0,0 +1,7 @@ +package com.java.gsl; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group13/771992096/src/com/java/gsl/LinkedList.java b/group13/771992096/src/com/java/gsl/LinkedList.java new file mode 100644 index 0000000000..2a4b90cbbf --- /dev/null +++ b/group13/771992096/src/com/java/gsl/LinkedList.java @@ -0,0 +1,96 @@ +package com.java.gsl; + + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + + } + public void add(int index , Object o){ + if(index < 0 || index > size ){ + return; + } + Node node = (Node) (index==size ? head : get(index)); + Node prenode = node.pre; +// Node l = last; +// Node newNode = new Node(l, o, null); +// last = newNode; +// if (l == null) +// head = newNode; +// else +// l.next = newNode; +// size++; + + } + public Object get(int index){ + if (index < 0 || index >= size){ + System.out.println("错误"); + return null; + } + // 获取index处的节点。 + // 若index < 双向链表长度的1/2,则从前先后查找; + // 否则,从后向前查找。 + if (index < (size/2 )) { + for (int i = 0; i <= index; i++) + head = head.next; + } else { + for (int i = size; i > index; i--) + head = head.pre; + } + return head; + + } + public Object remove(int index){ + Node x = (Node) get(index); + Node next = x.next; + Node prev = x.pre; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.pre = null; + } + + if (next == null) { + x.last = prev; + } else { + next.pre = prev; + x.next = null; + } + + size--; + return get(index); + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node pre; + Node last; + } +} diff --git a/group13/771992096/src/com/java/gsl/List.java b/group13/771992096/src/com/java/gsl/List.java new file mode 100644 index 0000000000..26807394e6 --- /dev/null +++ b/group13/771992096/src/com/java/gsl/List.java @@ -0,0 +1,9 @@ +package com.java.gsl; + +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/group13/771992096/src/com/java/gsl/Queue.java b/group13/771992096/src/com/java/gsl/Queue.java new file mode 100644 index 0000000000..64645156ec --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Queue.java @@ -0,0 +1,20 @@ +package com.java.gsl; + +public class Queue { + LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.removeLast(); + } + + public boolean isEmpty(){ + return queue.size() > 0; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group13/771992096/src/com/java/gsl/Stack.java b/group13/771992096/src/com/java/gsl/Stack.java new file mode 100644 index 0000000000..438e07a8ee --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Stack.java @@ -0,0 +1,27 @@ +package com.java.gsl; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int i = elementData.size(); + elementData.remove(i); + return elementData.get(i); + } + + public Object peek(){ + int i = elementData.size(); + return elementData.get(i); + } + public boolean isEmpty(){ + + return elementData.size()>0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group13/group13.md b/group13/group13.md index d3f5a12faa..8b13789179 100644 --- a/group13/group13.md +++ b/group13/group13.md @@ -1 +1 @@ - + diff --git a/group14/group14.md b/group14/group14.md index d3f5a12faa..8b13789179 100644 --- a/group14/group14.md +++ b/group14/group14.md @@ -1 +1 @@ - + diff --git a/group15/group15.md b/group15/group15.md index d3f5a12faa..8b13789179 100644 --- a/group15/group15.md +++ b/group15/group15.md @@ -1 +1 @@ - + diff --git a/group16/group16.md b/group16/group16.md index d3f5a12faa..8b13789179 100644 --- a/group16/group16.md +++ b/group16/group16.md @@ -1 +1 @@ - + diff --git a/group17/102228177/work2_19/.classpath b/group17/102228177/work2_19/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group17/102228177/work2_19/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/102228177/work2_19/.gitignore b/group17/102228177/work2_19/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/102228177/work2_19/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/102228177/.project b/group17/102228177/work2_19/.project similarity index 100% rename from group17/102228177/.project rename to group17/102228177/work2_19/.project diff --git a/group17/102228177/src/data2_19/ArrayList.java b/group17/102228177/work2_19/src/data2_19/ArrayList.java similarity index 100% rename from group17/102228177/src/data2_19/ArrayList.java rename to group17/102228177/work2_19/src/data2_19/ArrayList.java diff --git a/group17/102228177/src/data2_19/BinaryTreeNode.java b/group17/102228177/work2_19/src/data2_19/BinaryTreeNode.java similarity index 100% rename from group17/102228177/src/data2_19/BinaryTreeNode.java rename to group17/102228177/work2_19/src/data2_19/BinaryTreeNode.java diff --git a/group17/102228177/src/data2_19/Iterator.java b/group17/102228177/work2_19/src/data2_19/Iterator.java similarity index 100% rename from group17/102228177/src/data2_19/Iterator.java rename to group17/102228177/work2_19/src/data2_19/Iterator.java diff --git a/group17/102228177/src/data2_19/LinkedList.java b/group17/102228177/work2_19/src/data2_19/LinkedList.java similarity index 100% rename from group17/102228177/src/data2_19/LinkedList.java rename to group17/102228177/work2_19/src/data2_19/LinkedList.java diff --git a/group17/102228177/src/data2_19/List.java b/group17/102228177/work2_19/src/data2_19/List.java similarity index 100% rename from group17/102228177/src/data2_19/List.java rename to group17/102228177/work2_19/src/data2_19/List.java diff --git a/group17/102228177/src/data2_19/Queue.java b/group17/102228177/work2_19/src/data2_19/Queue.java similarity index 100% rename from group17/102228177/src/data2_19/Queue.java rename to group17/102228177/work2_19/src/data2_19/Queue.java diff --git a/group17/102228177/src/data2_19/Stack.java b/group17/102228177/work2_19/src/data2_19/Stack.java similarity index 100% rename from group17/102228177/src/data2_19/Stack.java rename to group17/102228177/work2_19/src/data2_19/Stack.java diff --git a/group17/102228177/work2_26/.classpath b/group17/102228177/work2_26/.classpath new file mode 100644 index 0000000000..dfa83d7793 --- /dev/null +++ b/group17/102228177/work2_26/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group17/102228177/work2_26/.project b/group17/102228177/work2_26/.project new file mode 100644 index 0000000000..2ae5966b60 --- /dev/null +++ b/group17/102228177/work2_26/.project @@ -0,0 +1,17 @@ + + + work2_26 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/102228177/work2_26/.settings/org.eclipse.jdt.core.prefs b/group17/102228177/work2_26/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group17/102228177/work2_26/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0434776317 --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,146 @@ +package com.coderising.array; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length/2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = temp; + } + } + + public static void main(String[] args) { + ArrayUtil util = new ArrayUtil(); + int[] origin = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// System.out.println(Arrays.toString(util.removeZero(origin))); + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + System.out.println(Arrays.toString(util.merge(a1, a2))); + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] newArray = new int[oldArray.length]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + return Arrays.copyOf(newArray, j); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + //Set是不允许重复的,所以将数组的值全部放在Set对象中 + Set set = new HashSet(); + + for(int i = 0; i < array1.length ; i++){ + set.add(array1[i]); + } + + for(int i = 0; i < array2.length ; i++){ + set.add(array2[i]); + } + + Iterator i = set.iterator(); + int[] arrays = new int[set.size()]; + int num=0; + while(i.hasNext()){ + int a = (Integer)i.next(); + arrays[num] = a; + num = num + 1; + } + + //对结果进行排序 + Arrays.sort(arrays); + return arrays; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java b/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java new file mode 100644 index 0000000000..96e6a9f867 --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java @@ -0,0 +1,27 @@ +package com.coderising.litestruts; + +/** + * javaBean 工具 + * @author ren + * + */ +public class BeanUtil { + /** + * 返回set方法名 + * @param filedName 属性名 + * @return set方法名 + */ + public static String setter(String filedName){ + return "set"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1); + } + + /** + * 返回get方法名 + * @param filedName 属性名 + * @return get方法名 + */ + public static String getter(String filedName){ + return "get"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1); + } + +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java b/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java new file mode 100644 index 0000000000..3eefec6d3d --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java @@ -0,0 +1,101 @@ +package com.coderising.litestruts; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * Dom4J 解析XML文件 + * @author ren + * + */ +public class Dom4jUtil { + + /** + * 传入文件路径解析xml文件获取根节点 + * @param path xml文件的绝对路径 + * @return Element 根节点 + */ + public static Element parseXml(String path){ + InputStream is = null; + Map map = new HashMap(); + try { + is = new FileInputStream(path); + //创建SAXReader读取XML + SAXReader reader = new SAXReader(); + //根据saxReader的read重写方法可知,既可以通过inputStream输入流来读取,也可以通过file对象来读取 + Document document = reader.read(is); + //获取根节点对象 + Element rootElement = document.getRootElement(); + return rootElement; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + finally{ + if( is!=null ){ + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + } + + /** + * 获取子节点的属性值并添加到map中并返回 + * @param element xml文件根节点对象 + * @return map 封装的属性值map对象 + */ + public static Map getAttribute(Element element){ + Map map = new HashMap(); + List elements = element.elements(); + for (Element ele : elements) { + String name = ele.attributeValue("name"); + String clas = ele.attributeValue("class"); + map.put(name, clas); + } + return map; + } + + /** + * 根据传入的Action名返回结果JSP + * @param element 根节点 + * @param actionName 标签name属性的value + * @return map 封装返回结果jsp的map对象 + */ + public static Map getJspMap(Element element,String actionName){ + Map map = new HashMap(); + List actions = element.elements(); + for (Element action : actions) { + if(actionName.equals(action.attributeValue("name"))){ + List results = action.elements(); + for (Element result : results) { + String name = result.attributeValue("name"); + String text = result.getText(); + map.put(name, text); + } + } + } + return map; + } + + public static void main(String[] args) { + String path = Dom4jUtil.class.getResource("").getPath()+"struts.xml"; + System.out.println(path); + Element element = parseXml(path); + Map attribute = getAttribute(element); + System.out.println(getJspMap(element,"login").get("success")); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/LoginAction.java b/group17/102228177/work2_26/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..efbc56607a --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,79 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.Element; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + String path = Struts.class.getResource("").getPath()+"struts.xml"; + Element element = Dom4jUtil.parseXml(path); + Map attribute = Dom4jUtil.getAttribute(element); + String className = attribute.get(actionName); + try { + Class clazz = Class.forName(className); + Object o = clazz.newInstance(); + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + Field field = clazz.getDeclaredField(key); + Method method = clazz.getDeclaredMethod(BeanUtil.setter(field.getName()),String.class); + method.invoke(o, value); + } + Method method = clazz.getDeclaredMethod("execute",null); + String str = (String) method.invoke(o); + Field[] fields = clazz.getDeclaredFields(); + Map map = new HashMap(); + for (Field field : fields) { + String fieldName = field.getName(); + Method method2 = clazz.getDeclaredMethod(BeanUtil.getter(fieldName),null); + String ret = (String) method2.invoke(o, null); + map.put(fieldName, ret); + } + View view = new View(); + view.setParameters(map); + Map result = Dom4jUtil.getJspMap(element, actionName); + view.setJsp(result.get(str)); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + runAction(actionName, params); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java b/group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/View.java b/group17/102228177/work2_26/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/struts.xml b/group17/102228177/work2_26/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0141537ddb --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group17/1158154002/.classpath b/group17/1158154002/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group17/1158154002/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group17/1158154002/.gitignore b/group17/1158154002/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/1158154002/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/1158154002/.project b/group17/1158154002/.project new file mode 100644 index 0000000000..95ef0b2be8 --- /dev/null +++ b/group17/1158154002/.project @@ -0,0 +1,17 @@ + + + 1158154002 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/1158154002/src/test01/arrayList/ArrayListTest.java b/group17/1158154002/src/test01/arrayList/ArrayListTest.java new file mode 100644 index 0000000000..ae61880ccc --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/ArrayListTest.java @@ -0,0 +1,34 @@ +package test01.arrayList; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void cetrinWArrayListTest(){ + CetrinwList list = new CetrinwArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + System.out.println("下标为3的元素为"+list.get(3)); + System.out.println("数组size:"+list.size()); + list.remove(2); + System.out.print("remove后的数组size:"+list.size()); + + System.out.print("remove后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + list.insert(3,"gg"); + + System.out.println(""); + System.out.print("insert后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + } +} diff --git a/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java b/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java new file mode 100644 index 0000000000..a69e617454 --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java @@ -0,0 +1,124 @@ +package test01.arrayList; + +public class CetrinwArrayList implements CetrinwList { + + /** + * 数组默认长度 + */ + private static final int DEFAULT_SIZE=10; + + /** + * 存储队列中的元素 + */ + private Object[] elements=null; + + /** + * 数组大小指针 + */ + private int capacity; + + /** + * 当前游标 + */ + private int current; + + public CetrinwArrayList() { + this(DEFAULT_SIZE); + } + + public CetrinwArrayList(int size) { + if (size<0) { + throw new RuntimeException("数组大小不能小于0"); + } else { + this.elements=new Object[size]; + this.current=0; + this.capacity=size; + } + } + + @Override + public E get(int index) { + confirmIndex(index); + return (E)elements[index]; + } + + @Override + public void add(E e) { + confirmSize(); + this.elements[current]=e; + this.current++; + } + + @Override + public void remove(int index) { + confirmIndex(index); + for (int i = index; i < elements.length; i++) { + if (i+1=index; i--) { + elements[i+1]=elements[i]; + } + elements[index]=e; + current++; + } + + @Override + public boolean contains(Object o) { + for (Object object : elements) { + if (object.equals(o)) { + return true; + } + } + return false; + } + + @Override + public int size() { + return this.current; + } + + @Override + public boolean isEmpty() { + if (current>0) { + return false; + } + return true; + } + + @Override + public void clearList() { + for (int i = 0; i < current; i++) { + elements[i]=null; + } + } + + /** + * 确认当前数组的容量,如果满足,则不操作,如果不满足,则增加空间 + */ + private void confirmSize(){ + if (this.current==this.capacity) { + this.capacity=this.capacity*3/2; + Object[] newElements=new Object[this.capacity]; + System.arraycopy(elements, 0, newElements, 0, elements.length); + this.elements=newElements; + } + } + + + /** + * 判断下标是否越界 + */ + private void confirmIndex(int index){ + if (index>capacity||index<0) { + throw new RuntimeException("下标越界"); + } + } +} diff --git a/group17/1158154002/src/test01/arrayList/CetrinwList.java b/group17/1158154002/src/test01/arrayList/CetrinwList.java new file mode 100644 index 0000000000..7badabf807 --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/CetrinwList.java @@ -0,0 +1,43 @@ +package test01.arrayList; + +public interface CetrinwList { + /** + * 取得数据 + */ + E get(int index); + + /** + *新增数据 + */ + void add(E e); + + /** + * 移除数据 + */ + void remove(int index); + + /** + * 插入数据 + */ + void insert(int index,E e); + + /** + * 是否存在数据 + */ + boolean contains(Object o); + + /** + * 获得List长度 + */ + int size(); + + /** + * 是否为空 + */ + boolean isEmpty(); + + /** + * 清空 + */ + void clearList(); +} diff --git a/group17/1158154002/src/test01/linkedList/MyLinkedList.java b/group17/1158154002/src/test01/linkedList/MyLinkedList.java new file mode 100644 index 0000000000..9a39ab16c3 --- /dev/null +++ b/group17/1158154002/src/test01/linkedList/MyLinkedList.java @@ -0,0 +1,172 @@ +package test01.linkedList; + +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class MyLinkedList implements Iterable { + private int theSize; + private int modCount = 0; + private Node beginMarker; + private Node endMarker; + + private static class Node { + public T data; + public Node prev; + public Node next; + + public Node(T d, Node p, Node n) { + data = d; + prev = p; + next = n; + } + } + + public MyLinkedList() { + clear(); + } + + public void clear() { + beginMarker = new Node(null, null, null); + endMarker = new Node(null, beginMarker, null); + beginMarker.next = endMarker; + theSize = 0; + modCount++; + } + + public int size() { + return theSize; + } + + public boolean isEmpty() { + return size() == 0; + } + + public void add(T x) { + add(size(), x); + } + + public void add(int idx, T x) { + addBefore(getNode(idx), x); + } + + public T get(int idx) { + return getNode(idx).data; + } + + public T set(int idx, T newVal) { + Node p = getNode(idx); + T oldVal = p.data; + p.data = newVal; + return oldVal; + } + + public T remove(int idx) { + return remove(getNode(idx)); + } + + private void addBefore(Node p, T x) { + Node newNode = new Node(x, p.prev, p); + newNode.prev.next = newNode; + p.prev = newNode; + theSize++; + modCount++; + } + + private T remove(Node p) { + + final T element = p.data; + final Node next = p.next; + final Node prev = p.prev; + + if (prev == null) { + beginMarker = next; + } else { + prev.next = next; + p.prev = null; + } + + if (next == null) { + endMarker = prev; + } else { + next.prev = prev; + p.next = null; + } + + p.data = null; + theSize--; + modCount++; + return p.data; + } + + private Node getNode(int idx) { +// Node p; +// if (idx < 0 || idx > size()) { +// throw new IndexOutOfBoundsException(); +// } +// if (idx < size() / 2) { +// p = beginMarker.next; +// for (int i = 0; i < idx; i++) { +// p = p.next; +// } +// } else { +// p = endMarker; +// for (int i = size(); i < idx; i--) { +// p = p.prev; +// } +// } +// + if (idx < (size() >> 1)) { + Node p = beginMarker; + for (int i = 0; i < idx; i++) + p = p.next; + return p; + } else { + Node p = endMarker; + for (int i = size() - 1; i > idx; i--) + p = p.prev; + return p; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current = beginMarker.next; + private int expectedModCount = modCount; + private boolean okToRemove = false; + + @Override + public boolean hasNext() { + return current != endMarker; + } + + @Override + public T next() { + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + if (!hasNext()) { + throw new NoSuchElementException(); + } + T nextItem = current.data; + current = current.next; + okToRemove = true; + return nextItem; + } + + public void remove() { + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + if (!okToRemove) { + throw new IllegalStateException(); + } + MyLinkedList.this.remove(current.prev); + okToRemove = false; + expectedModCount++; + } + } +} diff --git a/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java b/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java new file mode 100644 index 0000000000..9903188289 --- /dev/null +++ b/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java @@ -0,0 +1,37 @@ +package test01.linkedList; + +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void MyLinkedListTest(){ + MyLinkedList list = new MyLinkedList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + System.out.println("下标为3的元素为"+list.get(3)); + System.out.println("数组size:"+list.size()); + list.remove(2); + System.out.print("remove后的数组size:"+list.size()); + + System.out.print("remove后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + list.add(3,"gg"); + + System.out.println(""); + System.out.print("insert后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + } +} diff --git a/group17/1158154002/src/test01/queue/Queue.java b/group17/1158154002/src/test01/queue/Queue.java new file mode 100644 index 0000000000..002f05e3e8 --- /dev/null +++ b/group17/1158154002/src/test01/queue/Queue.java @@ -0,0 +1,105 @@ +package test01.queue; + +import java.util.Arrays; +import java.util.Collection; + +/** + *

+ * structure for queue + *

+ *

+ * 用1个数组存数据,数组 小index 是 head,大index是tail,
+ * 插入在 tail 处,删除在 head 处, + *

+ * + * @author eric + * @param + */ +public class Queue { + /** 初始容量 */ + public static final int DEFAULT_SIZE = 10; + /** 容量不足时翻倍数 */ + public static final float DEFAULT_INCREMENT = 1.5f; + /** 数据 */ + private Object[] elementData; + /** 元素个数 */ + private int elementCount; + /** 数组的头部,即 下次删除数据的 index */ + private int head; + /** 数组的尾部,即 下次插入数据的 index */ + private int tail; + + public Queue() { + this(DEFAULT_SIZE); + } + + public Queue(int size) { + this.elementData = new Object[size]; + this.elementCount = 0; + this.head = 0; + this.tail = 0; + } + + public Queue(Object[] data) { + this.elementData = data; + this.elementCount = data.length; + this.head = 0; + this.tail = 0; + } + + public Queue(Collection c) { + this(c.toArray()); + } + + /** + * 添加数据 到尾部 + * + * @param ele + * @return + */ + public synchronized E add(E ele) { + if (tail >= elementData.length) { + adjustData(); + } + elementData[tail] = ele; + elementCount++; + tail++; + return ele; + }; + + /** + * 删除数据 从头部 + * + * @return + */ + @SuppressWarnings("unchecked") + public synchronized E remove() { + E e = (E) elementData[head]; + elementData[head] = null; + elementCount--; + head++; + return e; + }; + + /** + * 获得当前的数据 + * + * @return + */ + public synchronized Object[] getData() { + return Arrays.copyOfRange(this.elementData, this.head, this.tail); + } + + public synchronized void adjustData() { + if (tail >= elementData.length) { // tail 处空间不足时调用 + // head 的空位去掉 + int newSize = (elementData.length == elementCount) ? (int) Math.ceil(elementCount * DEFAULT_INCREMENT) + : elementData.length; + elementData = Arrays.copyOfRange(elementData, head, elementData.length); + // 调整空间 + elementData = Arrays.copyOf(elementData, newSize); + tail = elementCount; + head = 0; + } + } +} diff --git a/group17/1158154002/src/test01/queue/QueueTest.java b/group17/1158154002/src/test01/queue/QueueTest.java new file mode 100644 index 0000000000..6abb1aa5c6 --- /dev/null +++ b/group17/1158154002/src/test01/queue/QueueTest.java @@ -0,0 +1,41 @@ +package test01.queue; + +import org.junit.Test; + +public class QueueTest{ + @Test + public void test() { + Queue queueOne = new Queue(); + // 第1次 加入个数 + int addCountOne = 30; + // 第1次 删除个数 + int removeCountOne = 20; + // 第2次 加入个数 + int addCountTwo = 10; + + for (int i = 0; i < addCountOne; i++) { + queueOne.add(i); + } + Object[] data = queueOne.getData(); + for (int i = 0; i < data.length; i++) { + System.out.println((Integer) data[i] == i); + } + + for (int i = 0; i < removeCountOne; i++) { + System.out.println(queueOne.remove() == i); + } + + for (int i = 0; i < addCountTwo; i++) { + queueOne.add(i * 10); + } + Object[] data2 = queueOne.getData(); + int baseCount = addCountOne - removeCountOne; + for (int i = 0; i < addCountTwo; i++) { + System.out.println((Integer) data2[baseCount + i] == i * 10); + } + } + + public static void main(String[] args) { + new QueueTest().test(); + } +} diff --git a/group17/1158154002/src/test01/stack/MyStack.java b/group17/1158154002/src/test01/stack/MyStack.java new file mode 100644 index 0000000000..766a8083b2 --- /dev/null +++ b/group17/1158154002/src/test01/stack/MyStack.java @@ -0,0 +1,111 @@ +package test01.stack; + +import java.util.Iterator; + +import com.sun.org.apache.bcel.internal.generic.POP; +import com.sun.org.apache.bcel.internal.generic.PUSH; + +public class MyStack implements Iterable { + private static final int DEFAULT_SIZE=10; + private int size; + private T[] item; + private int top; + + public MyStack() { + clear(); + } + + public void clear(){ + size=0; + top=-1; + ensureCapacity(DEFAULT_SIZE); + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size()==0; + } + + public void trumToSize(){ + ensureCapacity(size()); + } + + public void ensureCapacity(int capacity){ + if (capacity iterator() { + // TODO Auto-generated method stub + return new StackIterator(); + } + + private class StackIterator implements Iterator{ + private int current=0; + + @Override + public boolean hasNext() { + return current<=top; + } + + @Override + public T next() { + if (!hasNext()) { + throw new NullPointerException(); + } + return item[current++]; + } + + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.pop(); + stack.push(4); + stack.push(5); + Iterator iterator = stack.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + } +} diff --git a/group17/1158154002/src/test01/tree/Test.java b/group17/1158154002/src/test01/tree/Test.java new file mode 100644 index 0000000000..489862534c --- /dev/null +++ b/group17/1158154002/src/test01/tree/Test.java @@ -0,0 +1,20 @@ +package test01.tree; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Tree tree = new Tree(); + tree.addNode(null, "string"); + tree.addNode(tree.getNode("string"), "hello"); + tree.addNode(tree.getNode("string"), "world"); + tree.addNode(tree.getNode("hello"), "sinny"); + tree.addNode(tree.getNode("hello"), "fredric"); + tree.addNode(tree.getNode("world"), "Hi"); + tree.addNode(tree.getNode("world"), "York"); + tree.showNode(tree.root); + + System.out.println("end of the test"); + } + +} diff --git a/group17/1158154002/src/test01/tree/Tree.java b/group17/1158154002/src/test01/tree/Tree.java new file mode 100644 index 0000000000..2c4f1ea6b4 --- /dev/null +++ b/group17/1158154002/src/test01/tree/Tree.java @@ -0,0 +1,56 @@ +package test01.tree; + +public class Tree { + + public TreeNode root; + + public Tree(){} + + public void addNode(TreeNode node, T newNode){ + //增加根节点 + if(null == node){ + if(null == root){ + root = new TreeNode(newNode); + } + }else{ + TreeNode temp = new TreeNode(newNode); + node.nodelist.add(temp); + } + } + + /* 查找newNode这个节点 */ + public TreeNode search(TreeNode input, T newNode){ + + TreeNode temp = null; + + if(input.t.equals(newNode)){ + return input; + } + + for(int i = 0; i < input.nodelist.size(); i++){ + + temp = search(input.nodelist.get(i), newNode); + + if(null != temp){ + break; + } + } + + return temp; + } + + public TreeNode getNode(T newNode){ + return search(root, newNode); + } + + public void showNode(TreeNode node){ + if(null != node){ + //循环遍历node的节点 + System.out.println(node.t.toString()); + + for(int i = 0; i < node.nodelist.size(); i++){ + showNode(node.nodelist.get(i)); + } + } + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test01/tree/TreeNode.java b/group17/1158154002/src/test01/tree/TreeNode.java new file mode 100644 index 0000000000..7114095d45 --- /dev/null +++ b/group17/1158154002/src/test01/tree/TreeNode.java @@ -0,0 +1,21 @@ +package test01.tree; + +import java.util.ArrayList; +import java.util.List; + +public class TreeNode { + public T t; + private TreeNode parent; + + public List> nodelist; + + public TreeNode(T stype){ + t = stype; + parent = null; + nodelist = new ArrayList>(); + } + + public TreeNode getParent() { + return parent; + } +} \ No newline at end of file diff --git a/group17/1264835468/src/assignment2_26/ArrayUtil.java b/group17/1264835468/src/assignment2_26/ArrayUtil.java new file mode 100644 index 0000000000..826b76a677 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/ArrayUtil.java @@ -0,0 +1,183 @@ +package assignment2_26; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int mid = origin.length / 2; + for (int i = 0; i < mid; i++) { + int temp = origin[i]; + int reversePosition = origin.length - 1; + origin[i] = origin[reversePosition]; + origin[reversePosition] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int count = 0; + for (int i : oldArray) { + if (i != 0) + count++; + } + int[] newArray = new int[count]; + int currentPos = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + newArray[currentPos++] = oldArray[i]; + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + TreeSet set = new TreeSet<>(); + for (Integer integer : array1) { + set.add(integer); + } + for (Integer integer : array2) { + set.add(integer); + } + int[] result = new int[set.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = set.pollFirst(); + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + return Arrays.copyOf(oldArray, size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) + return new int[0]; + List fList = new ArrayList<>(); + fList.add(1); + fList.add(1); + int last = fList.size() - 1; + while (fList.get(last) < max) { + fList.add(fList.get(last) + fList.get(last - 1)); + last++; + } + int[] result = new int[fList.size() - 1]; + for (int i = 0; i < result.length; i++) { + result[i] = fList.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + boolean[] isPrime = new boolean[max]; + List primes = new ArrayList<>(); + for (int i = 0; i < isPrime.length; i++) { + isPrime[i] = true; + } + for (int i = 2; i * i < max; i++) { + for (int j = i; i * j < max; j++) + isPrime[i * j] = false; + } + for (int i = 2; i < isPrime.length; i++) { + if (isPrime[i]) + primes.add(i); + } + int[] result = new int[primes.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = primes.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int sum = 0; + ArrayList perfectNumbers = new ArrayList<>(); + for (int i = 1; i < max; i++) { + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) + perfectNumbers.add(i); + sum = 0; + } + + int[] result = new int[perfectNumbers.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = perfectNumbers.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i : array) { + stringBuilder.append(i + seperator); + } + stringBuilder.delete(stringBuilder.length() - seperator.length(), stringBuilder.length()); + + return stringBuilder.toString(); + } + +} diff --git a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java new file mode 100644 index 0000000000..c5ff251592 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java @@ -0,0 +1,100 @@ +package assignment2_26; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArrayUtilTest { + ArrayUtil arrayUtil = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] array = new int[] {}; + arrayUtil.reverseArray(array); + assertArrayEquals(new int[] {}, array); + + array = new int[] { 1 }; + arrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 1 }, array); + + array = new int[] { 1, 2, 3 }; + arrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 3, 2, 1 }, array); + } + + @Test + public void testRemoveZero() { + int[] array = new int[] {}; + assertArrayEquals(new int[] {}, arrayUtil.removeZero(array)); + + array = new int[] { 0 }; + assertArrayEquals(new int[] {}, arrayUtil.removeZero(array)); + + array = new int[] { 1 }; + assertArrayEquals(new int[] { 1 }, arrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 0, 0, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array)); + } + + @Test + public void testMerge() { + int[] array1 = { 3, 5, 7, 8 }; + int[] array2 = { 4, 5, 6, 7 }; + assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, arrayUtil.merge(array1, array2)); + } + + @Test + public void testGrow() { + int[] array = { 3, 5, 7 }; + assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, arrayUtil.grow(array, 5)); + assertArrayEquals(new int[] { 3, 5, 7 }, arrayUtil.grow(array, 3)); + } + + @Test + public void testFibonacci() { + assertArrayEquals(new int[] {}, arrayUtil.fibonacci(1)); + + assertArrayEquals(new int[] { 1, 1 }, arrayUtil.fibonacci(2)); + + assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, arrayUtil.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + assertArrayEquals(new int[] {}, arrayUtil.getPrimes(1)); + + assertArrayEquals(new int[] {}, arrayUtil.getPrimes(2)); + + assertArrayEquals(new int[] { 2 }, arrayUtil.getPrimes(3)); + + assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, arrayUtil.getPrimes(20)); + } + + @Test + public void testGetPerfectNumbers() { + assertArrayEquals(new int[] { 6 }, arrayUtil.getPerfectNumbers(10)); + + assertArrayEquals(new int[] { 6, 28 }, arrayUtil.getPerfectNumbers(100)); + + assertArrayEquals(new int[] { 6, 28, 496 }, arrayUtil.getPerfectNumbers(1000)); + + assertArrayEquals(new int[] { 6, 28, 496, 8128 }, arrayUtil.getPerfectNumbers(10000)); + } + + @Test + public void testJoin() { + assertEquals("3-4-5", arrayUtil.join(new int[] { 3, 4, 5 }, "-")); + + assertEquals("345", arrayUtil.join(new int[] { 3, 4, 5 }, "")); + + assertEquals("3", arrayUtil.join(new int[] { 3 }, "")); + + assertEquals("3--4--5", arrayUtil.join(new int[] { 3, 4, 5 }, "--")); + } + +} diff --git a/group17/1264835468/src/assignment2_26/LoginAction.java b/group17/1264835468/src/assignment2_26/LoginAction.java new file mode 100644 index 0000000000..3101d322b8 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/LoginAction.java @@ -0,0 +1,42 @@ +package assignment2_26; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group17/1264835468/src/assignment2_26/Struts.java b/group17/1264835468/src/assignment2_26/Struts.java new file mode 100644 index 0000000000..a6b56ce26b --- /dev/null +++ b/group17/1264835468/src/assignment2_26/Struts.java @@ -0,0 +1,99 @@ +package assignment2_26; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static File configFile = new File("./src/struts.xml");; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + // 0 + XmlPhraser phraser = new XmlPhraser(configFile); + String className = phraser.getClassNameByActionName(actionName); + View view = new View(); + try { + // 1 + Class actionClass = Class.forName(className); + Constructor constructor = actionClass.getConstructor(); + Object instance = constructor.newInstance(); + invokeSetters(actionClass, instance, parameters); + + // 2 + String result = invokeExecute(actionClass, instance); + + // 3 + Map getterResult = invokeGetters(actionClass, instance); + view.setParameters(getterResult); + + // 4 + String resultJsp = phraser.getResultJsp(actionName, result); + view.setJsp(resultJsp); + + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException + | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + private static Map invokeGetters(Class actionClass, Object instance) + throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Map result = new HashMap<>(); + for (Method method : actionClass.getDeclaredMethods()) { + if (method.getName().matches("get.*")) { + String fieldName = method.getName().substring(3).toLowerCase(); + String value = (String) (method.invoke(instance)); + result.put(fieldName, value); + } + } + return result; + } + + private static String invokeExecute(Class actionClass, Object instance) throws NoSuchMethodException, + SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method method = actionClass.getDeclaredMethod("execute"); + return (String) method.invoke(instance); + } + + private static void invokeSetters(Class actionClass, Object instance, Map parameters) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + for (String fieldName : parameters.keySet()) { + invokeSetter(actionClass, instance, fieldName, parameters.get(fieldName)); + } + } + + private static void invokeSetter(Class actionClass, Object instance, String fieldName, String value) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + Method setter = actionClass.getDeclaredMethod(setterName, String.class); + setter.invoke(instance, value); + } +} diff --git a/group17/1264835468/src/assignment2_26/StrutsTest.java b/group17/1264835468/src/assignment2_26/StrutsTest.java new file mode 100644 index 0000000000..f47410209b --- /dev/null +++ b/group17/1264835468/src/assignment2_26/StrutsTest.java @@ -0,0 +1,38 @@ +package assignment2_26; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/1264835468/src/assignment2_26/View.java b/group17/1264835468/src/assignment2_26/View.java new file mode 100644 index 0000000000..e96197fad6 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/View.java @@ -0,0 +1,26 @@ +package assignment2_26; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/1264835468/src/assignment2_26/XmlPhraser.java b/group17/1264835468/src/assignment2_26/XmlPhraser.java new file mode 100644 index 0000000000..198a9b573e --- /dev/null +++ b/group17/1264835468/src/assignment2_26/XmlPhraser.java @@ -0,0 +1,72 @@ +package assignment2_26; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class XmlPhraser { + private File file; + List actionElements; + + public XmlPhraser(File file) { + this.file = file; + actionElements = new ArrayList<>(); + getActionsFromFile(); + } + + public String getClassNameByActionName(String actionName) { + Element action = getElementByActionName(actionName); + return action.getAttribute("class"); + } + + public String getResultJsp(String actionName, String resultName) { + Element action = getElementByActionName(actionName); + NodeList results = action.getChildNodes(); + for (int i = 0; i < results.getLength(); i++) { + Node child = results.item(i); + if (child instanceof Element) { + Element result = (Element) child; + if (result.getAttribute("name").equals(resultName)) + return result.getTextContent(); + } + } + throw new RuntimeException("not found result named:" + resultName); + } + + private Element getElementByActionName(String actionName) { + for (Element element : actionElements) { + if (element.getAttribute("name").equals(actionName)) { + return element; + } + } + throw new RuntimeException("no such element named " + actionName); + } + + private void getActionsFromFile() { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(file); + Element root = document.getDocumentElement(); + NodeList children = root.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child instanceof Element) + actionElements.add((Element) child); + } + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group17/1264835468/src/struts.xml b/group17/1264835468/src/struts.xml new file mode 100644 index 0000000000..ad43b47967 --- /dev/null +++ b/group17/1264835468/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" index 36b44b4aa8..4e6e0b6b1b 100644 --- "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" +++ "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" @@ -44,7 +44,7 @@ 51075907 http://m.blog.csdn.net/article/details?id=57083764 -1158154002 +1158154002 http://blog.csdn.net/shengren12/article/details/57400858 345450234 diff --git a/group17/group17.md b/group17/group17.md index d3f5a12faa..8b13789179 100644 --- a/group17/group17.md +++ b/group17/group17.md @@ -1 +1 @@ - + diff --git a/group18/1049843090/.classpath b/group18/1049843090/.classpath new file mode 100644 index 0000000000..68a547a96e --- /dev/null +++ b/group18/1049843090/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/group18/1049843090/.gitignore b/group18/1049843090/.gitignore new file mode 100644 index 0000000000..117a0b15d5 --- /dev/null +++ b/group18/1049843090/.gitignore @@ -0,0 +1,7 @@ +*.idea +*.iml +*.eml +.settings/ +target/ +build/ +out/ \ No newline at end of file diff --git a/group18/1049843090/.project b/group18/1049843090/.project new file mode 100644 index 0000000000..978233664c --- /dev/null +++ b/group18/1049843090/.project @@ -0,0 +1,15 @@ + + + 1049843090 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1049843090/src/com/coderising/array/ArrayUtil.java b/group18/1049843090/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..23e5bd2215 --- /dev/null +++ b/group18/1049843090/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.coderising.array; + + +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (isEmptyOrNull(origin)) { + return; + } + //solution 1 move element + //for (int i = 0; i <= origin.length >> 2; i++) { + // int temp = origin[i]; + // origin[i] = origin[origin.length - 1 - i]; + // origin[origin.length - 1 - i] = temp; + //} + + //solution 2 use Stack + Stack stack = new Stack<>(); + for (int i : origin) { + stack.push(i); + } + for (int i = 0; i < origin.length; i++) { + origin[i]=stack.pop(); + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + if (isEmptyOrNull(oldArray)) { + return null; + } + //solution 1 use Queue OR Stack + //Queue queue = new Queue<>(); + //for (int i : oldArray) { + // if (i != 0) { + // queue.enQueue(i); + // } + //} + //int[] newArray = new int[queue.size()]; + //for (int i = 0; i < newArray.length; i++) { + // newArray[i] = queue.deQueue(); + //} + //return newArray; + + + //solution 2 use Array + int[] tempArray = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + tempArray[index++] = oldArray[i]; + } + } + int[] newArray = new int[index]; + System.arraycopy(tempArray,0,newArray,0,index); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + + return null; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + if (isEmptyOrNull(oldArray)) { + return null; + } + int[] newArray = new int[oldArray.length + size]; + //solution 1 use System.arraycopy + //System.arraycopy(oldArray,0,newArray,0, oldArray.length); + + //solution 2 use loop + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator 分隔符 + * @return + */ + public String join(int[] array, String seperator) { + if (isEmptyOrNull(array)) { + return ""; + } + if (array.length < 2) { + return array[0] + ""; + } + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length - 1; i++) { + stringBuffer.append(array[i] + seperator); + } + stringBuffer.append(array[array.length - 1]); + return stringBuffer.toString(); + } + + /** + * 检查数组是否为空或者为null + * + * @param array + * @return + */ + private boolean isEmptyOrNull(int[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + public static void main(String[] args) { + int[] a = {7,9,5}; + ArrayUtil arrayUtil = new ArrayUtil(); + arrayUtil.reverseArray(a); + for (int i : a) { + System.out.println(i); + } + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/ArrayList.java b/group18/1049843090/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f6b3007a7d --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/ArrayList.java @@ -0,0 +1,148 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * A Simple ArrayList + */ +public class ArrayList implements List { + + /** + * 当前list的元素个数 + */ + private int size; + + /** + * 默认数组大小 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 存储元素的数组 + */ + private Object[] elementData; + + /** + * 追加一个元素 + * + * @param e + */ + public void add(E e) { + grow(); + elementData[size++] = e; + } + + private void grow() { + if (elementData.length == size) { + elementData = Arrays.copyOf(elementData, size + 10); + } + } + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + grow(); + int movedNum = size - index; + if (movedNum > 0) { + System.arraycopy(elementData, index, elementData, index + 1, movedNum); + } + elementData[index] = e; + size++; + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return getElement(index); + } + + + /** + * 删除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + E delEle = getElement(index); + int movedNum = size - index - 1;//是不是最后一个元素 + if (movedNum > 0) { + System.arraycopy(elementData, index + 1, elementData, index, movedNum); + } + elementData[--size] = null; + return delEle; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + private E getElement(int index) { + return (E) elementData[index]; + } + + /** + * list中元素的个数 + * + * @return + */ + public int size() { + return this.size; + } + + + public ArrayList() { + this.elementData = new Object[DEFAULT_CAPACITY]; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor;//游标 + + private int lastRet = -1;//可被删除元素下标 + + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public E next() { + int i = cursor; + cursor++; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + cursor = lastRet;//游标等于当前删除元素的下标 或者 cursor--; + ArrayList.this.remove(lastRet); + lastRet = -1;//重置可删元素下标 + + } + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/BinaryTree.java b/group18/1049843090/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..7f476b1d74 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,95 @@ +package com.coding.basic; + + +/** + * + */ +public class BinaryTree { + + private Node root; + + public boolean insert(int data) { + Node newNode = new Node(data); + if (root == null) { + root = newNode; + return true; + } + + return add(data); + //return add(root,new Node(data)); + } + + private boolean add(Node currentNode,Node newNode) { + if (currentNode.data == newNode.data) { + return false; + } + if (currentNode.data < newNode.data) { + if (currentNode.right == null) { + currentNode.right = newNode; + return true; + } else { + return add(currentNode.right, newNode); + } + } + if (currentNode.left == null) { + currentNode.left = newNode; + return true; + } else { + return add(currentNode.left, newNode); + } + } + + private boolean add(int data) { + Node newNode = new Node(data); + boolean result = false; + Node cursorNode = root; + Node parentNode = null; + while (true) { + parentNode = cursorNode; + if (cursorNode.data == data) { + break; + } + if (cursorNode.data < data) { + cursorNode = cursorNode.right; + if (cursorNode == null) { + parentNode.right = newNode; + result = true; + break; + } + } else { + cursorNode = cursorNode.left; + if (cursorNode == null) { + parentNode.left = newNode; + result = true; + break; + } + } + } + return result; + } + + + private static class Node { + int data; + Node left, right; + + public Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + public static void main(String[] args) { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(6); + binaryTree.insert(4); + binaryTree.insert(8); + binaryTree.insert(7); + binaryTree.insert(3); + + System.out.println("finsh"); + } + +} diff --git a/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..20f90e9239 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +/** + * Binary Tree + */ +public class BinaryTreeNode { + + /** + * 节点数据 + */ + private Object data; + /** + * 左节点 + */ + private BinaryTreeNode left; + /** + * 右节点 + */ + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + + public BinaryTreeNode(Object o){ + this.setData(o); + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Iterator.java b/group18/1049843090/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f4938a46c7 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Iterator.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Iterator + */ +public interface Iterator { + + /** + * 可迭代对象是否还有值 + * + * @return + */ + boolean hasNext(); + + /** + * 返回迭代对象的下一个元素 + * + * @return + */ + E next(); + + /** + * 移除当前元素 + */ + void remove(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/LinkedList.java b/group18/1049843090/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..7b4a378301 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/LinkedList.java @@ -0,0 +1,238 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * A Simple LinkedList + * + * @param element + */ +public class LinkedList implements List { + /** + * 链表head + */ + private Node head; + + + /** + * 链表中元素的个数 + */ + private int size; + + + /** + * 追加一个元素到链表尾 + * + * @param e + */ + public void add(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 插入一个元素到链表指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + if (index == 0) { + addFirst(e); + } else if (index == size) { + addLast(e); + } else { + Node indexNode = index(index); + Node next = indexNode.next; + Node newNode = new Node(e, next); + index(index - 1).next = newNode; + indexNode = null; + size++; + } + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return index(index).data; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + /** + * 移除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == (size - 1)) { + return removeLast(); + } else { + Node delNode = index(index); + E e = delNode.data; + Node prev = index(index - 1); + prev.next = index(index + 1); + delNode = null; + size--; + return e; + } + } + + /** + * 当前链表的元素个数 + * + * @return + */ + public int size() { + return this.size; + } + + /** + * 添加到链表的头 + * + * @param e + */ + public void addFirst(E e) { + Node newNode = new Node(e, null); + if (this.head != null) { + newNode.next = this.head; + } + this.head = newNode; + size++; + } + + /** + * 添加到链表的尾 + * + * @param e + */ + public void addLast(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 获取指定位置的节点 + * + * @param index + * @return + */ + private Node index(int index) { + Node node = this.head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + /** + * 删除链表第一个元素 + * + * @return + */ + public E removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + E e = head.data; + head = head.next; + size--; + return e; + } + + /** + * 删除链表最后一个元素 + * + * @return + */ + public E removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node end = index(size - 1); + E e = end.data; + end = null; + end = index(size - 2); + end.next = null; + size--; + return e; + } + + /** + * 节点数据 + * + * @param + */ + private static class Node { + //当前节点存储的数据 + E data; + //下一个节点 + Node next; + + public Node(E data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator(){ + return new LinkedListIterator<>(); + } + + private class LinkedListIterator implements Iterator{ + + private int cursor;//游标 + + private int lastRet = -1;//可被删除元素下标 + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public E next() { + int i = cursor; + cursor++; + return (E) LinkedList.this.get(lastRet=i); + } + + @Override + public void remove() { + if(lastRet<0){ + throw new IllegalStateException(); + } + cursor = lastRet; + LinkedList.this.remove(lastRet); + lastRet = -1; + } + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/List.java b/group18/1049843090/src/com/coding/basic/List.java new file mode 100644 index 0000000000..962fcf4b77 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/List.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +/** + * A Simple List Interface + */ +public interface List { + + /** + * 追加一个元素 + * + * @param e + */ + void add(E e); + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + void add(int index, E e); + + /** + * 获取指定位置元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 移除指定位置元素 + * + * @param index + * @return + */ + E remove(int index); + + + /** + * 当前List中元素的个数 + * + * @return + */ + int size(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Queue.java b/group18/1049843090/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..94759f0b5b --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Queue.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +/** + * A Simple Queue + */ +public class Queue { + + private LinkedList elementData; + + /** + * 入列 + * + * @param e + */ + public void enQueue(E e) { + elementData.addLast(e); + } + + /** + * 出列 + * + * @return + */ + public E deQueue() { + return elementData.removeFirst(); + } + + /** + * 查看第一个元素 + * + * @return + */ + public E peek() { + return elementData.size()==0?null:elementData.get(0); + } + + /** + * 队列是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 队列中元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Queue() { + elementData = new LinkedList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Stack.java b/group18/1049843090/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..61561f1450 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Stack.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * A Simple Stack + */ +public class Stack { + private ArrayList elementData; + + /** + * 压入栈顶 + * + * @param e + */ + public void push(E e) { + elementData.add(e); + } + + /** + * 取出栈顶元素 + * + * @return + */ + public E pop() { + return elementData.remove(elementData.size() - 1); + } + + /** + * 查看栈顶元素 + * + * @return + */ + public E peek() { + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + /** + * 栈内是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 栈顶内元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Stack() { + elementData = new ArrayList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/ArrayListTest.java b/group18/1049843090/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..25b2a018df --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * ArrayList Test + */ +public class ArrayListTest { + + ArrayList list; + + @Before + public void setUp() throws Exception { + list = new ArrayList<>(); + + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void add() throws Exception { + list.add("first"); + assertEquals("first", list.get(0)); + } + + @Test + public void add1() throws Exception { + list.add(0, "first"); + assertEquals("插入第一条", "first", list.get(0)); + list.add(0, "insert"); + assertEquals("插入第二条", "insert", list.get(0)); + list.add(2, "position_2"); + assertEquals("position_2", list.get(2)); + assertEquals(3, list.size()); + } + + @Test + public void get() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + assertEquals("first", list.get(0)); + assertEquals("second", list.get(1)); + assertEquals("third", list.get(2)); + + } + + @Test + public void remove() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + list.add("fourth"); + assertEquals("first", list.remove(0)); + assertEquals(3, list.size()); + assertEquals("third", list.remove(1)); + assertEquals("fourth", list.remove(1)); + assertEquals(1, list.size()); + + } + + @Test + public void size() throws Exception { + list.add("first"); + assertEquals(1,list.size()); + list.add("second"); + assertEquals( 2,list.size()); + } + + + @Test + public void iterator() throws Exception { + Iterator iterator = list.iterator(); + assertEquals(false,iterator.hasNext()); + list.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,list.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/LinkedListTest.java b/group18/1049843090/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..fb962287d3 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * LinkedList Test + */ +public class LinkedListTest { + + LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList<>(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void add() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + assertEquals(2, linkedList.size()); + + } + + @Test + public void add1() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals(2, linkedList.size()); + } + + @Test + public void get() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals("third", linkedList.get(2)); + } + + @Test + public void remove() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals("first", linkedList.remove(0)); + assertEquals("third", linkedList.remove(1)); + assertEquals("fourth", linkedList.remove(1)); + assertEquals(1, linkedList.size()); + + } + + @Test + public void size() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals(4, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addFirst("first first"); + assertEquals("first first", linkedList.get(0)); + + } + + @Test + public void addLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addLast("last"); + assertEquals("last", linkedList.get(2)); + } + + @Test + public void removeFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("first", linkedList.removeFirst()); + assertEquals("second", linkedList.removeFirst()); + assertEquals(1, linkedList.size()); + assertEquals("third", linkedList.get(0)); + assertEquals("third", linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("third", linkedList.removeLast()); + assertEquals("second", linkedList.removeLast()); + assertEquals("first", linkedList.removeLast()); + assertEquals(0, linkedList.size()); + + } + + @Test + public void iterator() throws Exception { + Iterator iterator = linkedList.iterator(); + assertEquals(false,iterator.hasNext()); + linkedList.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,linkedList.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/QueueTest.java b/group18/1049843090/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..2652d1e214 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/QueueTest.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Queue Test + */ +public class QueueTest { + + Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue<>(); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("A"); + assertEquals("A",queue.deQueue()); + } + + @Test + public void peek() throws Exception { + assertEquals(null,queue.peek()); + queue.enQueue("A"); + assertEquals("A",queue.peek()); + } + + @Test + public void deQueue() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals("A",queue.deQueue()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,queue.isEmpty()); + queue.enQueue("A"); + assertEquals(false,queue.isEmpty()); + } + + @Test + public void size() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals(2,queue.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/StackTest.java b/group18/1049843090/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..e2587ba7f2 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/StackTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Stack Test + */ +public class StackTest { + Stack stack; + @Before + public void setUp() throws Exception { + stack = new Stack<>(); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("A"); + assertEquals("A",stack.pop()); + } + + @Test + public void pop() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.pop()); + assertEquals("B",stack.pop()); + assertEquals("A",stack.pop()); + assertEquals(0,stack.size()); + + + } + + @Test + public void peek() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.peek()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,stack.isEmpty()); + stack.push("A"); + assertEquals(false,stack.isEmpty()); + } + + @Test + public void size() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals(3,stack.size()); + } + +} \ No newline at end of file diff --git a/group18/1057617027/.classpath b/group18/1057617027/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group18/1057617027/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/1057617027/.gitignore b/group18/1057617027/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group18/1057617027/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group18/1057617027/.project b/group18/1057617027/.project new file mode 100644 index 0000000000..3189c0c10d --- /dev/null +++ b/group18/1057617027/.project @@ -0,0 +1,17 @@ + + + 1057617027Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1057617027/src/com/coding/basic/ArrayList.java b/group18/1057617027/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2c11c0a3fa --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + + +@SuppressWarnings("rawtypes") +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + public void kuorong(int size){ + if(size>elementData.length){ + Object[] elementDataTemp = new Object[size*2]; + System.arraycopy(elementData, 0, elementDataTemp, 0, elementData.length); + elementData = elementDataTemp; + } + } + public void add(Object o){ + kuorong(size); + elementData[size] = o; + ++size; + } + public void add(int index, Object o){ + if(index>size||index<0) + throw new IndexOutOfBoundsException("��ȷ�����indexֵ������"+size+"�Ҳ�С��0"); + kuorong(++size); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + } + + public Object get(int index){ + + return elementData[index]; + } + + public Object remove(int index){ + if(index>size||index<0) + throw new IndexOutOfBoundsException("��ȷ�����indexֵ������"+size+"�Ҳ�С��0"); + + System.arraycopy(elementData, index+1, elementData, index, size-index); + size--; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + + return new myiterator(); + } + public class myiterator implements Iterator{ + private int nextIndex; + public boolean hasNext(){ + return nextIndex!=size; + } + public Object next(){ + return elementData[nextIndex++]; + } + + } + public static void main(String[] args) { + ArrayList al = new ArrayList(); + + al.add(1); + al.add(2); + al.add(3); + al.add(4); + al.add(2,5); + al.remove(2); + for(int i= 0;i<5;i++){ + System.out.println(al.get(i));} + System.out.println(al.size()); + + } + +} diff --git a/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java b/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..bcb0a4af65 --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode head; + private BinaryTreeNode node; + BinaryTreeNode(Object data,BinaryTreeNode left,BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(node==null){ + node = new BinaryTreeNode(o, null, null); + }else{ + if(Integer.parseInt(String.valueOf(o))<=Integer.parseInt(String.valueOf(node.data))){ + node.left = insert(node.left,o ); + node = node.left; + }else{ + node.right = insert(node.right,o); + node = node.right; + } + } + return node; + } + public BinaryTreeNode insert(BinaryTreeNode node,Object o){ + if(node==null){ + node = new BinaryTreeNode(o, null, null); + }else{ + if(Integer.parseInt(String.valueOf(o))<=Integer.parseInt(String.valueOf(node.data))){ + node.left = insert(node.left,o ); + node.left =node; + }else{ + node.right = insert(node.right,o ); + node.right =node; + } + } + return node; + } +public static void main(String[] args){ + BinaryTreeNode node = new BinaryTreeNode(null, null, null); + + System.out.println(node.insert(6).data); + System.out.println(node.insert(5).data); + System.out.println(node.insert(11).data); + System.out.println(node.insert(7).data); + System.out.println(node.insert(2).data); + System.out.println(node); + +} +} \ No newline at end of file diff --git a/group18/1057617027/src/com/coding/basic/Iterator.java b/group18/1057617027/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..40c0906495 --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); +} diff --git a/group18/1057617027/src/com/coding/basic/LinkedList.java b/group18/1057617027/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..b4c3325814 --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/LinkedList.java @@ -0,0 +1,145 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = new Node(null,null); + private Node last; + private int size; + private static class Node{ + Object data; + Node next; + Node(Object data,Node next){ + this.data = data; + this.next = next; + } + } + public void add(Object o){ + if(size==0){ + Node node = new Node(o,null); + head = node; + last = node; + size++; + }else{ + Node node = new Node(o,null); + last.next = node; + last = node; + size++; + } + + } + public void add(int index , Object o){ + if(index>size){ + System.out.println("�����"+index+"���ڵ�ǰ��"+size); + } + Node n = head; + Node n1 = head; + for(int i=0;i diff --git a/group18/1159828430/20170219/.project b/group18/1159828430/20170219/.project index da66c3a498..93c92379ce 100644 --- a/group18/1159828430/20170219/.project +++ b/group18/1159828430/20170219/.project @@ -1,17 +1,17 @@ - - - 20170219 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + 20170219 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java index c8db730110..44f6b3402d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java @@ -1,3 +1,4 @@ + package com.coding.basic; import java.util.Arrays; @@ -99,8 +100,9 @@ public boolean isEmpty(){ return size == 0; } + //迭代器 public Iterator iterator(){ - return null; + return new ArrayListIterator(this); } //动态增加ArrayList大小 @@ -128,6 +130,35 @@ private void fastRemove(int index) { } elementData[--size] = null; } + + private class ArrayListIterator implements Iterator{ + private ArrayList list = null; + private int cursor = 0; + private int lastRet = -1; + + public ArrayListIterator(ArrayList list){ + this.list = list; + } + @Override + public boolean hasNext() { + return cursor != list.size; + } + + @Override + public Object next() { + lastRet = cursor; + Object o = list.get(lastRet); + cursor ++; + return o; + } + @Override + public void remove() { + list.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } + + } } class testArrayList{ @@ -138,8 +169,16 @@ public static void main(String[] args) { } arrayList.add(5,15); arrayList.remove(11); + Iterator it = arrayList.iterator(); + while(it.hasNext()) { + Integer o = (Integer)it.next(); + if(o == 8){ + it.remove(); + } + } for (int i = 0; i < arrayList.size(); i++) { System.out.println("value is "+arrayList.get(i)); } + } } diff --git a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java index 0ce3f762ba..58064fdfae 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java @@ -1,3 +1,4 @@ + package com.coding.basic; /** * @author Hipple @@ -7,5 +8,5 @@ public interface Iterator { public boolean hasNext(); public Object next(); + public void remove(); -} \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java index 433715d5ad..4586f0549d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java @@ -1,5 +1,6 @@ package com.coding.basic; +import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** @@ -88,7 +89,7 @@ public Object getFirst() { } public Iterator iterator(){ - return null; + return new LinkedListIterator(); } //头部增加节点 @@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } + //迭代器 + private class LinkedListIterator implements Iterator{ + private Node lastReturned = null; + private Node next; + private int nextIndex; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + public void remove() { + if (lastReturned == null) + throw new IllegalStateException(); + + Node lastNext = lastReturned.next; + unlink(lastReturned); + if (next == lastReturned) + next = lastNext; + else + nextIndex--; + lastReturned = null; + } + } + + //节点对象 private static class Node{ Object data; Node next; diff --git a/group18/1159828430/20170219/src/com/coding/basic/List.java b/group18/1159828430/20170219/src/com/coding/basic/List.java index 08e0f9e2d5..78674d202d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/List.java +++ b/group18/1159828430/20170219/src/com/coding/basic/List.java @@ -1,13 +1,13 @@ -package com.coding.basic; -/** - * @author Hipple - * @Time:2017年2月20日 下午8:52:08 - * @version 1.0 - */ -public interface List { - public boolean add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); +package com.coding.basic; +/** + * @author Hipple + * @Time:2017年2月20日 下午8:52:08 + * @version 1.0 + */ +public interface List { + public boolean add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); } \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/Queue.java b/group18/1159828430/20170219/src/com/coding/basic/Queue.java index e91779fdb2..a5de938d6d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Queue.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Queue.java @@ -1,31 +1,31 @@ -package com.coding.basic; -/** - * @author Hipple - * @Time:2017年2月23日 下午11:00:00 - * @version 1.0 - */ - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public Queue(){ - - } - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - elementData.getFirst(); - return null; - } - - public boolean isEmpty(){ - return elementData.isEmpty(); - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; +/** + * @author Hipple + * @Time:2017年2月23日 下午11:00:00 + * @version 1.0 + */ + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public Queue(){ + + } + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + elementData.getFirst(); + return null; + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group18/1159828430/20170219/src/com/coding/basic/Stack.java b/group18/1159828430/20170219/src/com/coding/basic/Stack.java index ef5b637050..eae2f6637d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Stack.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Stack.java @@ -1,57 +1,57 @@ -package com.coding.basic; - -import java.util.EmptyStackException; - -/** - * @author Hipple - * @Time:2017年2月23日 下午10:59:39 - * @version 1.0 - */ -public class Stack { - private ArrayList elementData = new ArrayList(); - - public Stack(){ - - } - - //入栈 - public void push(Object o){ - elementData.add(o); - } - - //出栈 - public Object pop(){ - if (elementData.isEmpty()) { - throw new EmptyStackException(); - } - final Object o = peek(); - elementData.remove(o);//重新写根据对象remove - return o; - } - - public Object peek(){ - if (elementData.isEmpty()) { - throw new EmptyStackException(); - } - final Object o = elementData.get(elementData.size()-1); - return o; - } - public boolean isEmpty(){ - return size() == 0; - } - public int size(){ - return elementData.size(); - } -} -class TestStack { - public static void main(String[] args){ - Stack myStack=new Stack(); - myStack.push("a"); - myStack.push(2); - myStack.push("123"); - myStack.push("ahu"); - while(!myStack.isEmpty()){ - System.out.println(myStack.pop()); - } - } -} +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * @author Hipple + * @Time:2017年2月23日 下午10:59:39 + * @version 1.0 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack(){ + + } + + //入栈 + public void push(Object o){ + elementData.add(o); + } + + //出栈 + public Object pop(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = peek(); + elementData.remove(o);//重新写根据对象remove + return o; + } + + public Object peek(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} +class TestStack { + public static void main(String[] args){ + Stack myStack=new Stack(); + myStack.push("a"); + myStack.push(2); + myStack.push("123"); + myStack.push("ahu"); + while(!myStack.isEmpty()){ + System.out.println(myStack.pop()); + } + } +} diff --git a/group18/1159828430/README.md b/group18/1159828430/README.md index 387ebebf6d..8ff2ffc95e 100644 --- a/group18/1159828430/README.md +++ b/group18/1159828430/README.md @@ -1,2 +1,2 @@ -# 2017编程提高群 -这里是1159828430 大连—书生 的代码提交区 +# 2017编程提高群 +这里是1159828430 大连—书生 的代码提交区 diff --git a/group18/1787597051/.classpath b/group18/1787597051/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group18/1787597051/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/1787597051/.gitignore b/group18/1787597051/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/1787597051/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/1787597051/.project b/group18/1787597051/.project new file mode 100644 index 0000000000..2abc06efd7 --- /dev/null +++ b/group18/1787597051/.project @@ -0,0 +1,17 @@ + + + 1787597051 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1787597051/.settings/org.eclipse.jdt.core.prefs b/group18/1787597051/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group18/1787597051/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java b/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group18/1787597051/src/com/coding/basic/MyArrayList.java b/group18/1787597051/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..77b14150c1 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + private final int GROW = 4; + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o) { + if (size > elementData.length - 1) { + elementData = Arrays.copyOf(elementData, elementData.length + GROW); + elementData[size] = o; + } else { + elementData[size] = o; + } + size++; + } + + public void add(int index, Object o) { + Object[] target = new Object[elementData.length - index]; + for (int x = index, y = 0; x < elementData.length; x++, y++) { + target[y] = elementData[x]; + } + elementData = Arrays.copyOf(elementData, elementData.length + 1); + size = index; + // elementData[index] = o; + elementData[size] = o; + size++; + for (int y = 0; y < target.length; y++) { + // add(target[y]); + elementData[size] = target[y]; + size++; + } + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + Object removeData = elementData[index]; + elementData[index] = null; + Object[] target = Arrays.copyOfRange(elementData, index + 1, elementData.length); + for (int x = index, y = 0; y < target.length; y++, x++) { + elementData[x] = target[y]; + } + size--; + return removeData; + } + + public int size() { + return size; + } + + public MyIteratorImpl iterator() { + return new MyIteratorImpl(); + } + + private class MyIteratorImpl implements MyIterator { + int index; + + public boolean hasNext() { + return index != size; + } + + public Object next() { + int i = index; + index = i + 1; + return elementData[i]; + } + + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyIterator.java b/group18/1787597051/src/com/coding/basic/MyIterator.java new file mode 100644 index 0000000000..59af236aab --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyIterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface MyIterator { + public abstract boolean hasNext(); + public abstract Object next(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyLinkedList.java b/group18/1787597051/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..862a0b9f38 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +public class MyLinkedList implements MyList { + private int size; + private Node head; + + public MyLinkedList() { + head = new Node(); + head.data = "ͷ���"; + head.next = null; + } + + public void add(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p3 = new Node(); + p3.data = o; + p.next = p3; + size++; + } + + public void add(int index, Object o) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = new Node(); + p2.data = o; + p2.next = p.next; + p.next = p2; + size++; + } + p = p.next; + num++; + } + } + + public Object get(int index) { + int num = 0; + Node p = head.next; + while (p != null) { + if (num == index) { + return p.data; + } + p = p.next; + num++; + } + return null; + } + + public Object remove(int index) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = p.next; + p.next = p.next.next; + size--; + return p2.data; + } + p = p.next; + num++; + } + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node p = new Node(); + p.data = o; + p.next = head.next; + head.next = p; + size++; + } + + public void addLast(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2.data = o; + p.next = p2; + size++; + } + + public Object removeFirst() { + Node p = head; + if (p.next != null) { + Node p2 = head.next; + p.next = p.next.next; + size--; + return p2.data; + } + return null; + } + + public Object removeLast() { + Node p = head; + if (p.next != null) { + while (p.next.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2 = p.next; + p.next = null; + size--; + return p2.data; + } + return null; + } + /* + * public Iterator iterator(){ return null; } + */ + + private static class Node { + Object data; + Node next; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyList.java b/group18/1787597051/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..afb20940ea --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyList.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface MyList { + public abstract void add(Object o); + public abstract void add(int index, Object o); + public abstract Object get(int index); + public abstract Object remove(int index); + public abstract int size(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyQueue.java b/group18/1787597051/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..3161f6b4e9 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyQueue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class MyQueue { + private int size; + MyLinkedList mll = new MyLinkedList(); + public MyQueue() { + } + public void enQueue(Object o){ + mll.add(o); + size++; + } + + public Object deQueue(){ + size--; + return mll.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyStack.java b/group18/1787597051/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..36c9aaffa5 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyStack.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() > 0) { + Object data = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return data; + } + return null; + } + + public Object peek() { + if (elementData.size() > 0) { + return elementData.get(elementData.size() - 1); + } + return null; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group18/542330964/.classpath b/group18/542330964/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group18/542330964/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group18/542330964/.gitignore b/group18/542330964/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/542330964/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/542330964/.project b/group18/542330964/.project new file mode 100644 index 0000000000..3a99e1d0ad --- /dev/null +++ b/group18/542330964/.project @@ -0,0 +1,17 @@ + + + 542330964learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/542330964/src/basicstruct/ArrayList.java b/group18/542330964/src/basicstruct/ArrayList.java new file mode 100644 index 0000000000..b56c61c4d1 --- /dev/null +++ b/group18/542330964/src/basicstruct/ArrayList.java @@ -0,0 +1,80 @@ +package basicstruct; + + + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData ; + + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList() { + elementData=new Object [DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if(initialCapacity>=0){ + elementData=new Object[initialCapacity]; + }else { + throw new IllegalArgumentException("initialCapacity"+ + initialCapacity+"不能为负数"); + } + } + + public void add(Object o){ + ensureCapacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + if(index<0||index>size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index + 1,size - index); + elementData[index] = o; + size++; + } + + private void rangeCheck(int index) { + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + } + private void ensureCapacity() { + if(size == elementData.length) { + Object[] newArray = new Object[size * 2 + 1]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object movedValue = elementData[index]; + //被删除元素后的元素数目 + int numMoved = size - index - 1; + //后面有元素 + if (numMoved > 0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + //恰为最后一个元素 + size--; + elementData[size] = null; //垃圾回收 + return movedValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group18/542330964/src/basicstruct/BinaryTreeNode.java b/group18/542330964/src/basicstruct/BinaryTreeNode.java new file mode 100644 index 0000000000..5fba822d2f --- /dev/null +++ b/group18/542330964/src/basicstruct/BinaryTreeNode.java @@ -0,0 +1,31 @@ +package basicstruct; +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Iterator.java b/group18/542330964/src/basicstruct/Iterator.java new file mode 100644 index 0000000000..f7a094dd14 --- /dev/null +++ b/group18/542330964/src/basicstruct/Iterator.java @@ -0,0 +1,8 @@ +package basicstruct; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/LinkedList.java b/group18/542330964/src/basicstruct/LinkedList.java new file mode 100644 index 0000000000..b124e9f8b9 --- /dev/null +++ b/group18/542330964/src/basicstruct/LinkedList.java @@ -0,0 +1,175 @@ +package basicstruct; + +import java.util.NoSuchElementException; + + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size=0; + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index: "+index); + } + if (index == size) { + addLast(o); + } else { + Node temp = node(index); + final Node pred = temp.previous; + final Node newNode = new Node(o, temp, pred); + temp.previous = newNode; + if (pred == null){ + head = newNode; + } + else{ + pred.next = newNode; + } + size++; + } + } + + public Node node(int index) { + //二分法查找 + if (index < (size >> 1)) { + Node temp = head; + for (int i = 0; i < index; i++){ + temp = temp.next; + } + return temp; + } else { + Node temp = tail; + for (int i = size - 1; i > index; i--){ + temp = temp.previous; + } + return temp; + } + } + + public Object get(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return node(index).data; + } + + public Object remove(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return deleteElement(node(index)); + } + + private Object deleteElement(Node node) { + Object element = node.data; + Node next = node.next; + Node prev = node.previous; + if (prev == null) { + head = next; + }else{ + prev.next = next; + node.previous = null; + } + if(next == null) { + tail = prev; + }else { + next.previous = prev; + node.next = null; + } + node.data = null; + size--; + return element; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node h = head; + Node newNode = new Node(o, h, null); + head = newNode; + if (h == null){ + tail = newNode; + }else{ + h.previous = newNode; + } + size++; + } + + public void addLast(Object o){ + Node t = tail; + Node node = new Node(o, null, t); + tail = node; + if (t == null) { + head = node; + } else { + t.next = node; + } + size++; + } + + public Object removeFirst(){ + final Node h = head; + if (h == null) { + throw new NoSuchElementException("No such element"); + } + final Object element = h.data; + final Node next = h.next; + h.data = null; + h.next = null; + head = next; + if (next == null) { + tail = null; + } else { + next.previous = null; + } + size--; + return element; + } + + public Object removeLast(){ + Node t = tail; + if (t == null){ + throw new NoSuchElementException("No such element"); + } + final Object element = t.data; + final Node prev = t.previous; + t.data = null; + t.previous = null; + tail = prev; + if (prev == null) { + head = null; + } else { + prev.next = null; + } + size--; + return element; + } + public boolean isEmpty(){ + return size==0; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + Node previous; + public Node() { + super(); + } + public Node(Object data, Node next, Node previous) { + super(); + this.data = data; + this.next = next; + this.previous = previous; + } + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/List.java b/group18/542330964/src/basicstruct/List.java new file mode 100644 index 0000000000..abe8ec3613 --- /dev/null +++ b/group18/542330964/src/basicstruct/List.java @@ -0,0 +1,13 @@ +package basicstruct; + +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/group18/542330964/src/basicstruct/Queue.java b/group18/542330964/src/basicstruct/Queue.java new file mode 100644 index 0000000000..5cb8ff6f12 --- /dev/null +++ b/group18/542330964/src/basicstruct/Queue.java @@ -0,0 +1,22 @@ +package basicstruct; + +public class Queue { + + private LinkedList queue = new LinkedList(); + //进队列 + public void enQueue(Object o) { + queue.addLast(o); + } + //出队列 + public Object deQueue() { + return queue.removeFirst(); + } + + public int size() { + return queue.size(); + } + + public boolean isEmpty() { + return queue.isEmpty(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Stack.java b/group18/542330964/src/basicstruct/Stack.java new file mode 100644 index 0000000000..41f32af82f --- /dev/null +++ b/group18/542330964/src/basicstruct/Stack.java @@ -0,0 +1,24 @@ +package basicstruct; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + //删除栈顶的值 + public Object pop(){ + 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(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/junittest/ArraylistTest.java b/group18/542330964/src/junittest/ArraylistTest.java new file mode 100644 index 0000000000..bfaa41a4b0 --- /dev/null +++ b/group18/542330964/src/junittest/ArraylistTest.java @@ -0,0 +1,52 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.ArrayList; + +public class ArraylistTest { + + + ArrayList list = new ArrayList(); + @Before + public void setUp() throws Exception { + list.add(1); + list.add(1,"boy next door"); + list.add(222); + list.add("333"); + list.add(4,"444"); + list.add(1232); + list.add("555"); + } + + + @Test + public void testAdd() { + System.out.println(list.get(0)); + System.out.println(list.get(3)); + System.out.println(list.get(5)); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.remove(0); + list.remove(5); + System.out.println(list.get(0)); + System.out.println(list.size()); +// System.out.println(list.get(5)); + } + + @Test + public void testSize() { + System.out.println(list.size()); + } + +} diff --git a/group18/542330964/src/junittest/LinkedListTest.java b/group18/542330964/src/junittest/LinkedListTest.java new file mode 100644 index 0000000000..8572b7a524 --- /dev/null +++ b/group18/542330964/src/junittest/LinkedListTest.java @@ -0,0 +1,65 @@ +package junittest; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.LinkedList; + +public class LinkedListTest { + + LinkedList l = new LinkedList(); + @Before + public void setUp() throws Exception { + l.add(1); + l.add("2"); + l.add(new Date()); + l.add(22); + l.add(33); + l.add(1, 3); + } + + @Test + public void testAddObject() { + System.out.println(l.get(1)); + System.out.println(l.get(3)); + System.out.println(l.get(200)); + } + + @Test + public void testRemove() { + l.remove(1); + System.out.println(l.get(1)); + } + + @Test + public void testSize() { + System.out.println(l.size()); + } + + @Test + public void testAddFirst() { + l.addFirst(0); + System.out.println(l.get(0)); + } + + @Test + public void testAddLast() { + l.addLast(999); + System.out.println(l.get(l.size()-1)); + } + + @Test + public void testRemoveFirst() { + l.removeFirst(); + System.out.println(l.get(0)); + } + + @Test + public void testRemoveLast() { + l.removeLast(); + System.out.println(l.get(l.size()-1)); + } + +} diff --git a/group18/542330964/src/junittest/QueueTest.java b/group18/542330964/src/junittest/QueueTest.java new file mode 100644 index 0000000000..a3488563e1 --- /dev/null +++ b/group18/542330964/src/junittest/QueueTest.java @@ -0,0 +1,33 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Queue; + +public class QueueTest { + + Queue q = new Queue(); + @Before + public void setUp() throws Exception { + q.enQueue(11); + q.enQueue(22); + q.enQueue(33); + q.enQueue(44); + q.enQueue(55); + } + + @Test + public void testDeQueue() { + q.deQueue(); + System.out.println(q.size()); + } + + @Test + public void testIsEmpty() { + System.out.println(q.isEmpty()); + } + +} diff --git a/group18/542330964/src/junittest/StackTest.java b/group18/542330964/src/junittest/StackTest.java new file mode 100644 index 0000000000..96372bb957 --- /dev/null +++ b/group18/542330964/src/junittest/StackTest.java @@ -0,0 +1,39 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Stack; + +public class StackTest { + + Stack s= new Stack(); + @Before + public void setUp() throws Exception { + s.push(11); + s.push(22); + s.push(33); + s.push("44"); + s.push(55); + } + + @Test + public void testPop() { + System.out.println(s.peek()); + s.pop(); + System.out.println(s.peek()); + } + + @Test + public void testIsEmpty() { + System.out.println(s.isEmpty()); + } + + @Test + public void testSize() { + System.out.println(s.size()); + } + +} diff --git a/group18/564673292/com/coding/basic/ArrayList.java b/group18/564673292/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4bb16148b7 --- /dev/null +++ b/group18/564673292/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterable{ + private E[] array; + private int lastIndex; + private int length; + //Constructor + @SuppressWarnings("unchecked") + public ArrayList(){ + length = 10; + array = (E[])new Object[length]; + lastIndex = 0; + } + + public void add(E object){ + if(lastIndex == length){ + this.grow(); + } + array[lastIndex] = object; + lastIndex++; + } + + @SuppressWarnings("unchecked") + private void grow(){ + E[] tempArray = (E[])new Object[length + 10]; + System.arraycopy(array, 0, tempArray, 0, length); + array = tempArray; + length = length + 10; + } + + public void insert(int index, E o) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + for (int i = lastIndex; i > index; i--) { + array[i] = array[i - 1]; + } + array[index] = o; + length++; + } + + public E get(int index) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + return array[index]; + } + + public E remove(int index){ + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + E removed = array[index]; + for (int i = index; i < lastIndex - 1; i++) { + array[i] = array[i + 1]; + } + lastIndex--; + array[lastIndex] = null; + return removed; + } + + public int size(){ + return lastIndex; + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private ArrayList arrayList; + // constructor + public Itr(ArrayList arrayList){ + this.arrayList = arrayList; + itrCurIndex = -1; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > lastIndex - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.arrayList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.arrayList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/BinaryTreeNode.java b/group18/564673292/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..713e8e4409 --- /dev/null +++ b/group18/564673292/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +// This is a node in a customized binaryTree. The tree have 2 extra features comparing to general binary trees. +// 1. The data of each node are in number class. +// 2. The left child node has a smaller number data than root node, and the right child node has a larger number data that root node. + +package com.coding.basic; + +public class BinaryTreeNode { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + // constructor + public BinaryTreeNode(E data){ + this.data = data; + } + + public E getData() { + return this.data; + } + + public void setData(E data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return this.left; + } + + public boolean setLeft(BinaryTreeNode left) { + if(this.compareWithRoot(left.data) >= 0 || this.left != null){ + System.err.println("The left node data should be smaller than root node."); + return false; + }else{ + this.left = left; + return true; + } + } + + public BinaryTreeNode getRight() { + return this.right; + } + + public boolean setRight(BinaryTreeNode right) { + if(this.compareWithRoot(right.data) <= 0 || this.right != null) { + System.err.println("The right node data should be larger than root node."); + + return false; + }else{ + this.right = right; + return true; + } + } + + private int compareWithRoot(E o){ + return (Integer)o - (Integer)this.getData(); + } + + @SuppressWarnings("unchecked") + public void insert(E o){ + BinaryTreeNode newNode = new BinaryTreeNode(o); + if(!this.setLeft(newNode)){ + if(!this.setRight(newNode)){ + if(this.left.getData() == o){ + this.right.insert(o); + }else{ + this.left.insert(o); + } + } + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterable.java b/group18/564673292/com/coding/basic/Iterable.java new file mode 100644 index 0000000000..e80308012a --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterable.java @@ -0,0 +1,5 @@ +package com.coding.basic; + +public interface Iterable{ + public Iterator iterator(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterator.java b/group18/564673292/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..27d475265e --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public E remove(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/LinkedList.java b/group18/564673292/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..19b12d12da --- /dev/null +++ b/group18/564673292/com/coding/basic/LinkedList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterable { + + private Node head; + private Node last; + private int length; + + private class Node{ + public E data; + public Node next; + + // constructor + private Node(E o, Node n){ + data = o; + next = n; + } + } + + // constructor + public LinkedList(){ + head = new Node(null, null); + last = head; + length = 0; + } + + public void add(E o){ + Node newNode = new Node(o, null); + last.next = newNode; + last = newNode; + length++; + } + + public void insert(int index , E o){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index); + Node nodeToInsert = new Node(o,nextNode); + prevNode.next = nodeToInsert; + length++; + } + + private Node getNode(int index){ + int count = 0; + Node currentNode = head; + while(currentNode.next != null && count <= index){ + currentNode = currentNode.next; + count++; + } + return currentNode; + } + + public E get(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeAtIndex = this.getNode(index); + return nodeAtIndex.data; + } + + public E remove(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeToRemove = this.getNode(index); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index + 1); + prevNode.next = nextNode; + E removedData = nodeToRemove.data; + nodeToRemove = null; + length--; + return removedData; + } + + public int size(){ + return length; + } + + public void addFirst(E o){ + this.insert(0, o); + } + public void addLast(E o){ + this.add(o); + + } + public E removeFirst(){ + return this.remove(0); + } + public E removeLast(){ + return this.remove(length - 1); + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private Node currentNode; + private LinkedList linkedList; + + public Itr(LinkedList linkedList){ + itrCurIndex = -1; + currentNode = head; + this.linkedList = linkedList; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > length - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.linkedList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.linkedList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/List.java b/group18/564673292/com/coding/basic/List.java new file mode 100644 index 0000000000..04a7ac992e --- /dev/null +++ b/group18/564673292/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void insert(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Queue.java b/group18/564673292/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b40f06afc8 --- /dev/null +++ b/group18/564673292/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedList; + + // constructor + public Queue(){ + linkedList = new LinkedList(); + } + + public void enQueue(E o){ + linkedList.addLast(o); + } + + public E deQueue(){ + return linkedList.removeFirst(); + } + + public E peek(){ + return linkedList.get(0); + } + + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + public int size(){ + return linkedList.size(); + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Stack.java b/group18/564673292/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b1b129fc40 --- /dev/null +++ b/group18/564673292/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack{ + private ArrayList arrayList; + + // constructor + public Stack(){ + arrayList = new ArrayList(); + } + + public void push(E o){ + arrayList.add(o); + } + + public E pop(){ + return arrayList.remove(arrayList.size() - 1); + } + + public E peek(){ + return arrayList.get(arrayList.size() - 1); + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true: false; + } + + public int size(){ + return arrayList.size(); + } + + // public Iterator iterator(){ + // return new Itr(); + // } + + // private class Itr implements Iterator{ + // Iterator arrayListItr = arrayList.iterator(); + // public boolean hasNext(){ + // return arrayListItr.hasNext(); + // } + + // public E next(){ + // return arrayListItr.next(); + // } + + // @Override // Stack iterator can only remove the last element + // public E remove(){ + // return arrayList.pop(); + // } + // } +} \ No newline at end of file diff --git "a/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..d2a21619bf --- /dev/null +++ "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,41 @@ +## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.25) + +原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-detailed/) + +> 本人对于CPU、内存、硬盘以及指令等一系列计算机核心组件理解甚浅。并且对其也不是很来感,不过身为一名软件专业学生以及未来的程序猿,还是硬着头皮研究研究。以下是对其学习的一个总结吧算是,有什么不正确的地方还请指出,不喜勿喷。 + +相信大家都知道计算机是由控制器、运算器、存储器、输入设备、输出设备五大部分组成。控制器 + 运算器组成了CPU,存储器即内存,当然硬盘也属于存储器,不过硬盘和内存存储形式有所不同,详细见下。 + +说明:点击以下标题查看维基百科详解 + +### [CPU](https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8) + +CPU即中央处理器(Central Processing Unit)。 + +它是计算机的主要设备之一,可以是说是计算机最重要的组成部分。常听到有人说CPU是"计算机的大脑",但我觉得这句话是不完全正确的。为什么这么说呢,CPU没有存储能力,只有为数不多的寄存器能临时存储一点东西,人可是有存储能力的,像最强大脑上有些人更拥有超强的记忆力。所以在存储能力这方面来讲,我觉得把CPU比作”大脑“的说法不太正确。就像刘欣前辈在其公号上讲解[CPU](http://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665513017&idx=1&sn=5550ee714abd36d0b580713f673e670b&scene=21#wechat_redirect)说的那样,"上帝为你关闭了一扇门,就一定会为你打开一扇窗",CPU虽然“脑容量”很小,但是它拥有超强的运算力。拿内存和硬盘来说,CPU比内存要快100倍,比硬盘快1000多万倍。这种运算速度可是人望尘莫及的了。所以在运算力这方面讲,又可以把CPU比作“超强的大脑”。 + +它负责处理、运算计算机内部的所有数据。 + +### [内存](https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E5%AD%98%E5%8F%96%E5%AD%98%E5%82%A8%E5%99%A8) + +内存即RAM,随机存取存储器(Random Access Memory)。 + +内存是计算机的主存,主存(Main memory)即电脑内部最重要的存储器,是与CPU直接交换数据的内部存储器。它用来加载各种各样的数据和程序以供CPU直接运行与运用。它是可以随时读写的,并且速度也很快,仅仅比CPU慢100倍。它通常作为操作系统或其他正在运行中的程序的临时数据存储媒介。通俗点说,内存就是用来临时存储数据,用来给CPU提供CPU要处理的东西。但是内存不会长期保存数据,只是临时存储,程序和数据处理完后就释放空间。 + +### [硬盘](https://zh.wikipedia.org/wiki/%E7%A1%AC%E7%9B%98) + +硬盘即HDD(Hard Disk Drive)。 + +硬盘是计算机上使用坚硬的旋转盘片为基础的非挥发性存储设备,它在平整的磁性表面存储和检索数字数据,信息通过离磁性表面很近的磁头,由电磁流来改变极性方式被电磁流写到磁盘上,信息可以通过相反的方式读取,例如读头经过纪录数据的上方时磁场导致线圈中电气信号的改变。硬盘的读写是采用随机存取的方式,因此可以以任意顺序读取硬盘中的数据。以上来自维基百科。各种专业名词,我相信你已经看厌倦了快。说白了,硬盘就是存东西的,长期存,也就是具有记忆力。不像CPU和内存,“一觉醒来就忘了以前的事情”,所有的数据全都清空。硬盘会长期存储数据,只要是不人为删除,不出现硬件故障,东西就不会丢。 + +### [指令](https://zh.wikipedia.org/wiki/%E6%8C%87%E4%BB%A4) + +指令,怎么说呢,指令就是任何可执行程序的元素的表述。指令一般会包含一个操作码和零到多个操作数。操作码指定了要进行什么样的操作。操作数可能指定了参与操作的寄存器、内存地址或立即数,它可能还会包含寻址方式,寻址方式确定操作数的含义。说白了,指令就是CPU的命令,CPU通过寄存器中的指令来进行数据的操作。 + +另外,指令一般有四种:加载、存储、操作和跳转。 + +### 它们之间的关系 + +CPU从内存或缓存中取出指令,放入指令寄存器,并对指令译码进行分解,进而对数据进行处理。这么说吧,计算机中所有的程序运行都是在内存中进行的,因此内存对计算机性能的影响非常大。数据由传输速度较慢的硬盘通过内存传送到CPU进行处理。不过内存是带电存储的(断电数据就会消失),而且容量十分有限,所以要长时间储存程序或数据就需要使用硬盘。 + +所以计算机处理数据大概就通过以上几个部分:数据(硬盘)——>内存——>CPU——>CPU通过指令处理数据 \ No newline at end of file diff --git a/group18/935542673/Blog/README.md b/group18/935542673/Blog/README.md new file mode 100644 index 0000000000..0533a45f85 --- /dev/null +++ b/group18/935542673/Blog/README.md @@ -0,0 +1,4 @@ +## 2017编程提高社群博客 + +2017.2.25 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/CPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB.md) + diff --git a/group18/935542673/Coding/.classpath b/group18/935542673/Coding/20170219/.classpath similarity index 100% rename from group18/935542673/Coding/.classpath rename to group18/935542673/Coding/20170219/.classpath diff --git a/group18/935542673/Coding/20170219/.gitignore b/group18/935542673/Coding/20170219/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/935542673/Coding/20170219/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/935542673/Coding/.project b/group18/935542673/Coding/20170219/.project similarity index 100% rename from group18/935542673/Coding/.project rename to group18/935542673/Coding/20170219/.project diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md b/group18/935542673/Coding/20170219/README.md similarity index 82% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md rename to group18/935542673/Coding/20170219/README.md index a565fb7148..a001a83d24 100644 --- a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md +++ b/group18/935542673/Coding/20170219/README.md @@ -1,8 +1,10 @@ ## 2017编程提高社群作业:实现基本的数据结构(2017.2.19) +#### [所有基本数据结构实现类及接口](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure) + 1. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) - 1.1 ArrayList实现了一下方法: + 1.1 ArrayList实现了以下方法: ``` (1)add(Object): 添加元素到集合; @@ -16,7 +18,7 @@ (9)iterator(): 返回一个迭代器的实现类。 ``` - 1.2 LinkedList实现了一下方法: + 1.2 LinkedList实现以下方法: ``` (1)addFirst(Object): 在链表头部插入新的元素; @@ -36,7 +38,7 @@ (15)iterator(): 返回一个迭代器的实现类。 ``` - 1.3 Queue实现了一下方法: + 1.3 Queue实现了以下方法: ``` (1)enQueue(Object): 入队操作; @@ -45,7 +47,7 @@ (4)isEmpty(): 判断队列是否为空。 ``` - 1.4 Stack实现了一下方法: + 1.4 Stack实现了以下方法: ``` (1)push(Object):入栈操作; @@ -58,7 +60,7 @@ 2. 实现了BinarySearchTree、Iterator接口 - 2.1 BinarySearchTree实现了一下方法: + 2.1 BinarySearchTree实现了以下方法: ``` (1)insert(int):插入操作; @@ -67,7 +69,7 @@ (4)inorderTraverse(Node):遍历操作,采用中序遍历。 ``` - 2.2 Iterator定义了一下方法: + 2.2 Iterator定义了以下方法: ``` (1)hasNext():判断是否有元素没有被遍历; @@ -76,8 +78,6 @@ ``` ​ -3. 对应以上类做了单元测试 + #### [所有基本数据结构测试类](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure) 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 - - 点击查看: [unit test](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/junit/com/ikook/basic_data_structure) \ No newline at end of file diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java similarity index 95% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java index 6891a76879..79ec2865c3 100644 --- a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java +++ b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java @@ -51,7 +51,9 @@ public void add(Object obj) { * @param obj */ public void add(int index, Object obj) { - rangeCheck(index); + if (index > size || index < 0) + throw new IndexOutOfBoundsException("索引越界异常"); + ensureCapacity(); System.arraycopy(elementData, index, elementData, index + 1, size-index); @@ -151,11 +153,7 @@ private void ensureCapacity() { */ private void rangeCheck(int index) { if(index < 0 || index >= size) { - try { - throw new Exception("索引异常"); - } catch (Exception e) { - e.printStackTrace(); - } + throw new IndexOutOfBoundsException("索引越界异常"); } } diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java diff --git a/group18/935542673/Coding/README.md b/group18/935542673/Coding/README.md new file mode 100644 index 0000000000..77e4706a07 --- /dev/null +++ b/group18/935542673/Coding/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群Coding + +2017.2.19 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219) \ No newline at end of file diff --git "a/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" "b/group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" similarity index 100% rename from "group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" rename to "group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" diff --git a/group18/935542673/README.md b/group18/935542673/README.md new file mode 100644 index 0000000000..d98a632c03 --- /dev/null +++ b/group18/935542673/README.md @@ -0,0 +1,15 @@ +## 2017编程提高社群 + +群昵称:青岛-ikook + +QQ号码:935542673 + +------------------------------------------------------------------------------------------------------------------------------ + +**代码库结构说明:** + +[Blog](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Blog) 目录为本人在社群中所编写的所有博客,可在 README.md 中直接点击某篇查看 + +[Coding](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding) 目录为本人在社群中所编写的所有代码,可在 README.md 中直接点击查看 + +[Datum](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Datum) 目录为本人在社群中产出的所有资料 \ No newline at end of file diff --git a/group19/group19.md b/group19/group19.md index d3f5a12faa..8b13789179 100644 --- a/group19/group19.md +++ b/group19/group19.md @@ -1 +1 @@ - + diff --git a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java index 0d960ee3d8..590fd3209e 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java +++ b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java @@ -1,122 +1,122 @@ -package com.coding.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - int n = elementData.length; - int i = 0; - while (elementData[i] != null) { - i++; - } - if (i < n) { - elementData[i] = o; - } else { - Object[] temp = Arrays.copyOf(elementData, n + 1); - temp[n] = o; - elementData = temp; - } - size++; - } - public void add(int index, Object o){ - int n = elementData.length; - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index > i) { - System.out.println(index + " is invalid index!"); - return; - } - if (i < n) { - for (int j = i; j > index; j--) { - elementData[j] = elementData[j - 1]; - } - elementData[index] = o; - } else { - Object[] temp = Arrays.copyOf(elementData, n + 1); - for (int j = i; j > index; j--) { - temp[j] = temp[j - 1]; - } - temp[index] = o; - elementData = temp; - } - size++; - } - - public Object get(int index){ - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index >= i) { - System.out.println(index + " is invalid index!"); - return null; - } else { - return elementData[index]; - } - } - - public Object remove(int index){ - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index >= i) { - System.out.println(index + " is invalid index!"); - return null; - } - Object result = elementData[index]; - for (int j = index; j < i; j++) { - elementData[j] = elementData[j + 1]; - } - size--; - return result; - } - - public int size(){ - return size; - } - - public String toString() { - int i = 0; - while (elementData[i] != null) { - i++; - } - String result = ""; - for (int j = 0; j < i - 1; j++) { - result += elementData[j].toString() + ", "; - } - result += elementData[size - 1].toString(); - return result; - } - - public Iterator iterator(){ - return null; - } - - public static void main(String args[]){ - ArrayList list1 = new ArrayList(); - list1.add("a"); - list1.add("b"); - list1.add("c"); - System.out.println(list1.toString()); - list1.add(5, "d"); - list1.add(1, "d"); - System.out.println(list1.toString()); - list1.add(4, "e"); - System.out.println(list1.toString()); - list1.get(5); - System.out.println(list1.get(0)); - list1.remove(2); - System.out.println(list1.toString()); - System.out.println(list1.size()); - } - -} +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (i < n) { + elementData[i] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + temp[n] = o; + elementData = temp; + } + size++; + } + public void add(int index, Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index > i) { + System.out.println(index + " is invalid index!"); + return; + } + if (i < n) { + for (int j = i; j > index; j--) { + elementData[j] = elementData[j - 1]; + } + elementData[index] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + for (int j = i; j > index; j--) { + temp[j] = temp[j - 1]; + } + temp[index] = o; + elementData = temp; + } + size++; + } + + public Object get(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } + Object result = elementData[index]; + for (int j = index; j < i; j++) { + elementData[j] = elementData[j + 1]; + } + size--; + return result; + } + + public int size(){ + return size; + } + + public String toString() { + int i = 0; + while (elementData[i] != null) { + i++; + } + String result = ""; + for (int j = 0; j < i - 1; j++) { + result += elementData[j].toString() + ", "; + } + result += elementData[size - 1].toString(); + return result; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String args[]){ + ArrayList list1 = new ArrayList(); + list1.add("a"); + list1.add("b"); + list1.add("c"); + System.out.println(list1.toString()); + list1.add(5, "d"); + list1.add(1, "d"); + System.out.println(list1.toString()); + list1.add(4, "e"); + System.out.println(list1.toString()); + list1.get(5); + System.out.println(list1.get(0)); + list1.remove(2); + System.out.println(list1.toString()); + System.out.println(list1.size()); + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java index 266eff3d56..d7ac820192 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java +++ b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Iterator.java b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Iterator.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java index e029beaeb4..13f423cc24 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java +++ b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java @@ -1,148 +1,148 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private int size; - - public void add(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node node = new Node(o); - Node curr = head; - while (curr.next != null) { - curr = curr.next; - } - curr.next = node; - size++; - } - public void add(int index , Object o){ - - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return; - } - if (head.data == null) { - if (index == 0) { - head.data = o; - head.next = null; - size++; - return; - } else { - System.out.println("invalid index!"); - return; - } - } - Node node = new Node(o); - Node curr = head; - for (int i = 0; i < index - 1; i++) { - curr = curr.next; - } - Node temp = curr.next; - curr.next = node; - node.next = temp; - size++; - } - public Object get(int index){ - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return null; - } - Node result = head; - for (int i = 0; i < index; i++) { - result = result.next; - } - return result; - } - public Object remove(int index){ - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return null; - } - Node curr = head; - for (int i = 0; i < index - 1; i++) { - curr = curr.next; - } - Node result = curr.next; - curr.next = curr.next.next; - size--; - return result; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node temp = head; - head = new Node(o); - head.next = temp; - size++; - } - - public void addLast(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node node = new Node(o); - Node curr = head; - while (curr.next != null) { - curr = curr.next; - } - curr.next = node; - size++; - } - - public Object removeFirst(){ - if (head.data == null) { - return null; - } - Node result = head; - head = head.next; - size--; - return result; - } - - public Object removeLast(){ - if (head.data == null) { - return null; - } - Node curr = head; - for (int i = 0; i < size - 1; i++) { - curr = curr.next; - } - Node result = curr.next; - curr.next = null; - size--; - return result; - } - - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - Node(Object o) { - data = o; - next = null; - } - - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public void add(int index , Object o){ + + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return; + } + if (head.data == null) { + if (index == 0) { + head.data = o; + head.next = null; + size++; + return; + } else { + System.out.println("invalid index!"); + return; + } + } + Node node = new Node(o); + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node temp = curr.next; + curr.next = node; + node.next = temp; + size++; + } + public Object get(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node result = head; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + public Object remove(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = curr.next.next; + size--; + return result; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node temp = head; + head = new Node(o); + head.next = temp; + size++; + } + + public void addLast(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + + public Object removeFirst(){ + if (head.data == null) { + return null; + } + Node result = head; + head = head.next; + size--; + return result; + } + + public Object removeLast(){ + if (head.data == null) { + return null; + } + Node curr = head; + for (int i = 0; i < size - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = null; + size--; + return result; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + Node(Object o) { + data = o; + next = null; + } + + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/List.java b/group20/331798361/assignment1/src/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/List.java +++ b/group20/331798361/assignment1/src/com/coding/basic/List.java @@ -1,9 +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(); -} +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(); +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Queue.java b/group20/331798361/assignment1/src/com/coding/basic/Queue.java index c4991a07f5..102fc025f5 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Queue.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Queue.java @@ -1,30 +1,30 @@ -package com.coding.basic; - - -public class Queue { - private LinkedList list = new LinkedList(); - - public void enQueue(Object o){ - list.add(o); - } - - public Object deQueue(){ - int length = list.size(); - if (length == 0) { - return null; - } - return list.removeFirst(); - } - - public boolean isEmpty(){ - if (list.size() == 0) { - return true; - } else { - return false; - } - } - - public int size(){ - return list.size(); - } -} +package com.coding.basic; + + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + int length = list.size(); + if (length == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + if (list.size() == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return list.size(); + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Stack.java b/group20/331798361/assignment1/src/com/coding/basic/Stack.java index 0a48545cea..3a62e3817e 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Stack.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Stack.java @@ -1,37 +1,37 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - elementData.add(o); - } - - public Object pop(){ - int length = elementData.size(); - if (length == 0) { - return null; - } - return elementData.remove(length - 1); - } - - public Object peek(){ - int length = elementData.size(); - if (length == 0) { - return null; - } - return elementData.get(length - 1); - } - - public boolean isEmpty(){ - if (elementData.size() != 0) { - return false; - } else { - return true; - } - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.remove(length - 1); + } + + public Object peek(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.get(length - 1); + } + + public boolean isEmpty(){ + if (elementData.size() != 0) { + return false; + } else { + return true; + } + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/ArrayList.java b/group20/404130810/src/com/basic/datastructure/ArrayList.java index 86c001b2c1..e91eb9ce5e 100644 --- a/group20/404130810/src/com/basic/datastructure/ArrayList.java +++ b/group20/404130810/src/com/basic/datastructure/ArrayList.java @@ -1,110 +1,110 @@ -package com.basic.datastructure; - -public class ArrayList implements List { - - private Object[] elementData; - private int size; - - private int enableCapacity; - - public ArrayList() { - this.enableCapacity = 10; - this.elementData = new Object[enableCapacity]; - } - - @Override - public void add(Object o) { - growIfNeeded(); - elementData[size] = o; - this.size++; - } - - @Override - public void add(int index, Object o) { - rangeCheckForAdd(index); - growIfNeeded(); - - Object[] tmpObjects = new Object[elementData.length]; - System.arraycopy(elementData, 0, tmpObjects, 0, index); - tmpObjects[index] = o; - System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); - - elementData = tmpObjects; - - this.size++; - } - - @Override - public Object get(int index) { - if (index > size) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - return elementData[index]; - } - - @Override - public Object remove(int index) { - Object removedObj = this.get(index); - rangeCheck(index); - Object[] tmpObjects = new Object[elementData.length]; - - System.arraycopy(elementData, 0, tmpObjects, 0, index); - System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); - - elementData = tmpObjects; - - return removedObj; - } - - @Override - public int size() { - return size; - } - - @Override - public String toString() { - for (int i = 0; i < elementData.length; i++) { - System.out.print(elementData[i] + ","); - } - return ""; - } - - private void rangeCheck(int paramInt) { - if ((paramInt < 0) || (paramInt >= size)) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); - } - } - - private void rangeCheckForAdd(int paramInt) { - if ((paramInt < 0) || (paramInt > size)) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); - } - } - - private String outOfBoundsMsg(int paramInt) { - return "Index: " + paramInt + ", Size: " + size; - } - - private Object[] growIfNeeded() { - if (enableCapacity <= this.size) { - enableCapacity = enableCapacity * 2; - Object[] largerElementData = new Object[enableCapacity]; - System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); - elementData = largerElementData; - } - return elementData; - } - - public static void main(String[] args) { - ArrayList list = new ArrayList(); - for (int i = 0; i <= 11; i++) { - list.add(String.valueOf(i)); - } - System.out.println(list.get(11)); - //list.add(10,"test"); - //list.get(10); - //list.remove(10); - //System.out.println(list); - } - -} +package com.basic.datastructure; + +public class ArrayList implements List { + + private Object[] elementData; + private int size; + + private int enableCapacity; + + public ArrayList() { + this.enableCapacity = 10; + this.elementData = new Object[enableCapacity]; + } + + @Override + public void add(Object o) { + growIfNeeded(); + elementData[size] = o; + this.size++; + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + growIfNeeded(); + + Object[] tmpObjects = new Object[elementData.length]; + System.arraycopy(elementData, 0, tmpObjects, 0, index); + tmpObjects[index] = o; + System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); + + elementData = tmpObjects; + + this.size++; + } + + @Override + public Object get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObj = this.get(index); + rangeCheck(index); + Object[] tmpObjects = new Object[elementData.length]; + + System.arraycopy(elementData, 0, tmpObjects, 0, index); + System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); + + elementData = tmpObjects; + + return removedObj; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + for (int i = 0; i < elementData.length; i++) { + System.out.print(elementData[i] + ","); + } + return ""; + } + + private void rangeCheck(int paramInt) { + if ((paramInt < 0) || (paramInt >= size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private void rangeCheckForAdd(int paramInt) { + if ((paramInt < 0) || (paramInt > size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private String outOfBoundsMsg(int paramInt) { + return "Index: " + paramInt + ", Size: " + size; + } + + private Object[] growIfNeeded() { + if (enableCapacity <= this.size) { + enableCapacity = enableCapacity * 2; + Object[] largerElementData = new Object[enableCapacity]; + System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); + elementData = largerElementData; + } + return elementData; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for (int i = 0; i <= 11; i++) { + list.add(String.valueOf(i)); + } + System.out.println(list.get(11)); + //list.add(10,"test"); + //list.get(10); + //list.remove(10); + //System.out.println(list); + } + +} diff --git a/group20/404130810/src/com/basic/datastructure/LinkedList.java b/group20/404130810/src/com/basic/datastructure/LinkedList.java index 43ba7ddd59..4fc92fddec 100644 --- a/group20/404130810/src/com/basic/datastructure/LinkedList.java +++ b/group20/404130810/src/com/basic/datastructure/LinkedList.java @@ -1,176 +1,176 @@ -package com.basic.datastructure; - -public class LinkedList implements List { - private Node first; - private Node last; - - private int size; - - public void add(Object item) { - addLast(item); - } - - public void add(int index, Object item) { - checkRange(index); - if (index == 0) { - addFirst(item); - } else if (index == size) { - addLast(item); - } else { - Node tmpNode = new Node(item); - Node preNode = get(index - 1); - Node nextNode = get(index + 1); - preNode.next = tmpNode; - tmpNode.next = nextNode; - size ++; - } - } - - public Node get(int index) { - checkRange(index); - if(size > 0){ - int loopTimes = 0; - Node p = first; - while(index > loopTimes){ - p = p.next; - loopTimes ++; - } - return p; - } - - return null; - } - - public Object remove(int index) { - checkRange(index); - Node tmpNode = null; - if(index == 0){ - removeFirst(); - }else if(index == size -1){ - removeLast(); - }else{ - tmpNode = get(index); - Node preNode = get(index-1); - Node nextNode = get(index + 1); - preNode.next = nextNode; - size --; - } - - return tmpNode; - } - - public int size() { - return size; - } - - public void addFirst(Object item) { - if (size == 0) { - first = new Node(item); - last = first; - } else { - Node tmpNode = new Node(item); - tmpNode.next = first; - first = tmpNode; - } - size++; - } - - public void addLast(Object item) { - if (size == 0) { - first = new Node(item); - last = first; - } else { - last.next = new Node(item); - last = last.next; - } - size++; - } - - public Object removeFirst() { - Node tmpNode = first; - if(tmpNode == null){ - last = null; - }else if(size == 2){ - first = last; - last = null; - size --; - }else{ - first = first.next; - size--; - } - return tmpNode; - } - - public Object removeLast() { - Node tmpNode = null; - if(size == 1){ - this.removeFirst(); - }else if(size >0){ - int index = size - 1; - tmpNode = last; - get(index - 1).next = null; - last = get(index - 1); - size --; - } - return tmpNode; - } - - private void checkRange(int index) { - if (index > size || index < 0) { - throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); - } - } - - public String toString() { - StringBuilder sb = new StringBuilder(); - Node p = first; - while (p != null) { - sb.append(p.item + "\n"); - p = p.next; - } - return sb.toString(); - } - - private static class Node { - private Object item; - private Node next; - Node(Object item) { - this.item = item; - } - } - - public static void main(String[] args) { - - /*Test add - LinkedList list = new LinkedList(); - for (int i = 0; i <= 5; i++) { - list.add(i); - } - list.add(3, "test"); - System.out.println(list); - */ - - /*Test remove - list.remove(3); - System.out.println(list); - */ - - /*Test removeLast and removeFirst - System.out.println(list); - list.removeLast(); - System.out.println(list); - list.removeLast(); - System.out.println(list); - list.removeLast(); - System.out.println(list); - */ - - /*Test from Java API - java.util.LinkedList linkedList = new java.util.LinkedList(); - linkedList.add("test"); - linkedList.removeFirst(); - System.out.println(linkedList); - */ - - } -} +package com.basic.datastructure; + +public class LinkedList implements List { + private Node first; + private Node last; + + private int size; + + public void add(Object item) { + addLast(item); + } + + public void add(int index, Object item) { + checkRange(index); + if (index == 0) { + addFirst(item); + } else if (index == size) { + addLast(item); + } else { + Node tmpNode = new Node(item); + Node preNode = get(index - 1); + Node nextNode = get(index + 1); + preNode.next = tmpNode; + tmpNode.next = nextNode; + size ++; + } + } + + public Node get(int index) { + checkRange(index); + if(size > 0){ + int loopTimes = 0; + Node p = first; + while(index > loopTimes){ + p = p.next; + loopTimes ++; + } + return p; + } + + return null; + } + + public Object remove(int index) { + checkRange(index); + Node tmpNode = null; + if(index == 0){ + removeFirst(); + }else if(index == size -1){ + removeLast(); + }else{ + tmpNode = get(index); + Node preNode = get(index-1); + Node nextNode = get(index + 1); + preNode.next = nextNode; + size --; + } + + return tmpNode; + } + + public int size() { + return size; + } + + public void addFirst(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + Node tmpNode = new Node(item); + tmpNode.next = first; + first = tmpNode; + } + size++; + } + + public void addLast(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + last.next = new Node(item); + last = last.next; + } + size++; + } + + public Object removeFirst() { + Node tmpNode = first; + if(tmpNode == null){ + last = null; + }else if(size == 2){ + first = last; + last = null; + size --; + }else{ + first = first.next; + size--; + } + return tmpNode; + } + + public Object removeLast() { + Node tmpNode = null; + if(size == 1){ + this.removeFirst(); + }else if(size >0){ + int index = size - 1; + tmpNode = last; + get(index - 1).next = null; + last = get(index - 1); + size --; + } + return tmpNode; + } + + private void checkRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + Node p = first; + while (p != null) { + sb.append(p.item + "\n"); + p = p.next; + } + return sb.toString(); + } + + private static class Node { + private Object item; + private Node next; + Node(Object item) { + this.item = item; + } + } + + public static void main(String[] args) { + + /*Test add + LinkedList list = new LinkedList(); + for (int i = 0; i <= 5; i++) { + list.add(i); + } + list.add(3, "test"); + System.out.println(list); + */ + + /*Test remove + list.remove(3); + System.out.println(list); + */ + + /*Test removeLast and removeFirst + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + */ + + /*Test from Java API + java.util.LinkedList linkedList = new java.util.LinkedList(); + linkedList.add("test"); + linkedList.removeFirst(); + System.out.println(linkedList); + */ + + } +} diff --git a/group20/404130810/src/com/basic/datastructure/List.java b/group20/404130810/src/com/basic/datastructure/List.java index 13fc25acfd..dcc0c18e18 100644 --- a/group20/404130810/src/com/basic/datastructure/List.java +++ b/group20/404130810/src/com/basic/datastructure/List.java @@ -1,10 +1,10 @@ -package com.basic.datastructure; - -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(); - -} +package com.basic.datastructure; + +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/group20/404130810/src/com/basic/datastructure/Queue.java b/group20/404130810/src/com/basic/datastructure/Queue.java index f83d937e5c..8e2cb7f4d9 100644 --- a/group20/404130810/src/com/basic/datastructure/Queue.java +++ b/group20/404130810/src/com/basic/datastructure/Queue.java @@ -1,33 +1,33 @@ -package com.basic.datastructure; - -public class Queue { - LinkedList list = new LinkedList(); - private int size; - - public void enQueue(Object o){ - list.add(o); - size ++; - } - - public Object deQueue(){ - size --; - return list.removeLast(); - } - - public boolean isEmpty(){ - return list.size() == 0; - } - - public int size(){ - return size; - } - - public static void main(String[] args) { - Queue queue = new Queue(); - for (int i = 0; i < 10; i++) { - queue.enQueue(i); - } - queue.deQueue(); - System.out.println("Finished"); - } -} +package com.basic.datastructure; + +public class Queue { + LinkedList list = new LinkedList(); + private int size; + + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + size --; + return list.removeLast(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return size; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + queue.deQueue(); + System.out.println("Finished"); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/Stack.java b/group20/404130810/src/com/basic/datastructure/Stack.java index 7a58fd49e6..bfff4d0633 100644 --- a/group20/404130810/src/com/basic/datastructure/Stack.java +++ b/group20/404130810/src/com/basic/datastructure/Stack.java @@ -1,41 +1,41 @@ -package com.basic.datastructure; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size; - - public void push(Object o){ - elementData.add(o); - size++; - } - public Object pop(){ - size --; - return elementData.remove(elementData.size() - 1); - } - - /** - * Looks at the object at the top of this stack without removing it from the stack. - * @return Object - */ - public Object peek(){ - return elementData.get(elementData.size() - 1); - } - public boolean isEmpty(){ - return size == 0; - } - public int size(){ - return size; - } - - public static void main(String[] args) { - Stack stack = new Stack(); - for (int i = 0; i < 10; i++) { - stack.push(i); - } - System.out.println(stack.peek()); - - stack.pop(); - System.out.println("Finished"); - } - -} +package com.basic.datastructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + public Object pop(){ + size --; + return elementData.remove(elementData.size() - 1); + } + + /** + * Looks at the object at the top of this stack without removing it from the stack. + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + stack.push(i); + } + System.out.println(stack.peek()); + + stack.pop(); + System.out.println("Finished"); + } + +} diff --git a/group20/404130810/src/com/basic/practice/PolymorphicInJava.java b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java index 6bf9996da3..c8da98e61f 100644 --- a/group20/404130810/src/com/basic/practice/PolymorphicInJava.java +++ b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java @@ -1,41 +1,41 @@ -package com.basic.practice; - -/** - * - * @author Wu Alvin - * Java Polymorphic Only represent in method level - * - */ - -class Fruit{ - String name = "Fruit"; - public void print(int i){ - System.out.println("Fruit" + i); - } -} - - -class Apple extends Fruit{ - String name = "Apple"; - public void print(int i){ - System.out.println("Apple" + i); - } -} - - -public class PolymorphicInJava { - - public static void main(String[] args) { - Apple apple = new Apple(); - apple.print(100); - //Apple100 - System.out.println(apple.name); - //Apple - Fruit fruit = apple; - fruit.print(100); - //Apple100 - System.out.println(fruit.name); - //Fruit - } - -} +package com.basic.practice; + +/** + * + * @author Wu Alvin + * Java Polymorphic Only represent in method level + * + */ + +class Fruit{ + String name = "Fruit"; + public void print(int i){ + System.out.println("Fruit" + i); + } +} + + +class Apple extends Fruit{ + String name = "Apple"; + public void print(int i){ + System.out.println("Apple" + i); + } +} + + +public class PolymorphicInJava { + + public static void main(String[] args) { + Apple apple = new Apple(); + apple.print(100); + //Apple100 + System.out.println(apple.name); + //Apple + Fruit fruit = apple; + fruit.print(100); + //Apple100 + System.out.println(fruit.name); + //Fruit + } + +} diff --git a/group20/404130810/src/com/basic/practice/ValuePassInJava.java b/group20/404130810/src/com/basic/practice/ValuePassInJava.java index 6187b5e0fc..c162ba4fc8 100644 --- a/group20/404130810/src/com/basic/practice/ValuePassInJava.java +++ b/group20/404130810/src/com/basic/practice/ValuePassInJava.java @@ -1,44 +1,44 @@ -package com.basic.practice; - -public class ValuePassInJava { - - /* - * Pass the Simple value, etc int String... - * Change will NOT happened - * - */ - public static void main(String[] args) { - String s = new String("123"); - int i = 1; - changeVal(i); - System.out.println(i); - - } - - private static void changeVal(int i){ - i = 2; - } - - /* - * Pass whole OBJECT, but change the Member variable - * Change will happened - */ - - /* - public static void main(String[] args) { - Person p = new Person(); - p.age = 10; - changeAge(p); - System.out.println(p.age); - } - - private static void changeAge(Person p){ - p.age = 20; - } - */ - -} - -class Person{ - int age; -} +package com.basic.practice; + +public class ValuePassInJava { + + /* + * Pass the Simple value, etc int String... + * Change will NOT happened + * + */ + public static void main(String[] args) { + String s = new String("123"); + int i = 1; + changeVal(i); + System.out.println(i); + + } + + private static void changeVal(int i){ + i = 2; + } + + /* + * Pass whole OBJECT, but change the Member variable + * Change will happened + */ + + /* + public static void main(String[] args) { + Person p = new Person(); + p.age = 10; + changeAge(p); + System.out.println(p.age); + } + + private static void changeAge(Person p){ + p.age = 20; + } + */ + +} + +class Person{ + int age; +} diff --git a/group20/group20.md b/group20/group20.md index d3f5a12faa..8b13789179 100644 --- a/group20/group20.md +++ b/group20/group20.md @@ -1 +1 @@ - + diff --git a/group21/group21.md b/group21/group21.md index d3f5a12faa..8b13789179 100644 --- a/group21/group21.md +++ b/group21/group21.md @@ -1 +1 @@ - + diff --git a/liuxin/.classpath b/liuxin/.classpath index 373dce4005..3e0fb272a8 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -1,7 +1,7 @@ - + diff --git a/liuxin/.gitignore b/liuxin/.gitignore index 3e2fcc7171..ae3c172604 100644 --- a/liuxin/.gitignore +++ b/liuxin/.gitignore @@ -1 +1 @@ -/bin/ +/bin/ diff --git a/liuxin/.project b/liuxin/.project index 2858b5b710..fab8d7f04c 100644 --- a/liuxin/.project +++ b/liuxin/.project @@ -1,17 +1,17 @@ - - - 2017Learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/src/com/coderising/array/ArrayUtil.java index 78845d06d0..e5ddb476a6 100644 --- a/liuxin/src/com/coderising/array/ArrayUtil.java +++ b/liuxin/src/com/coderising/array/ArrayUtil.java @@ -1,96 +1,96 @@ -package com.coderising.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/src/com/coderising/litestruts/LoginAction.java index 1005f35a29..dcdbe226ed 100644 --- a/liuxin/src/com/coderising/litestruts/LoginAction.java +++ b/liuxin/src/com/coderising/litestruts/LoginAction.java @@ -1,39 +1,39 @@ -package com.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/src/com/coderising/litestruts/Struts.java index 44cc35bf01..85e2e22de3 100644 --- a/liuxin/src/com/coderising/litestruts/Struts.java +++ b/liuxin/src/com/coderising/litestruts/Struts.java @@ -1,34 +1,34 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - -} +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/src/com/coderising/litestruts/StrutsTest.java index a44c1878ac..b8c81faf3c 100644 --- a/liuxin/src/com/coderising/litestruts/StrutsTest.java +++ b/liuxin/src/com/coderising/litestruts/StrutsTest.java @@ -1,43 +1,43 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/liuxin/src/com/coderising/litestruts/View.java b/liuxin/src/com/coderising/litestruts/View.java index 0194c681f6..07df2a5dab 100644 --- a/liuxin/src/com/coderising/litestruts/View.java +++ b/liuxin/src/com/coderising/litestruts/View.java @@ -1,23 +1,23 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml index 99063bcb0c..dd598a3664 100644 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ b/liuxin/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + \ No newline at end of file diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/src/com/coding/basic/ArrayList.java index 57412dcf7f..1f185736f9 100644 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ b/liuxin/src/com/coding/basic/ArrayList.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index 266eff3d56..d7ac820192 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/liuxin/src/com/coding/basic/Iterator.java b/liuxin/src/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/liuxin/src/com/coding/basic/Iterator.java +++ b/liuxin/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java index 1fd99bf26b..e2c4e5e795 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/src/com/coding/basic/LinkedList.java @@ -1,46 +1,46 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/liuxin/src/com/coding/basic/List.java b/liuxin/src/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/liuxin/src/com/coding/basic/List.java +++ b/liuxin/src/com/coding/basic/List.java @@ -1,9 +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(); -} +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(); +} diff --git a/liuxin/src/com/coding/basic/Queue.java b/liuxin/src/com/coding/basic/Queue.java index 08d2d86b14..36e516e266 100644 --- a/liuxin/src/com/coding/basic/Queue.java +++ b/liuxin/src/com/coding/basic/Queue.java @@ -1,19 +1,19 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java index 4bfe28057f..a5a04de76d 100644 --- a/liuxin/src/com/coding/basic/Stack.java +++ b/liuxin/src/com/coding/basic/Stack.java @@ -1,22 +1,22 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/shell.sh b/shell.sh deleted file mode 100644 index fbf69995d1..0000000000 --- a/shell.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -git add -A . -git commit -m "bobi" -git push origin master