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/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/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/group06/1378560653/.classpath b/group06/1378560653/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/1378560653/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1378560653/.gitignore b/group06/1378560653/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1378560653/.gitignore @@ -0,0 +1 @@ +/bin/ 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){ + //ﵽޣinitialSize50% + 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(); + } + + //ƳͷԪأΪʱnullqueueһ㲻ò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){ + //ﵽޣinitialSize100% + 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;//ʱΪleftright + 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/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 guigit 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){//Ϊգheadlastָ½ڵ㣨Ϊ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; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * 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 guigit 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/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 - - - - - - + + + + + + + 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..f8298d7a48 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java @@ -1,145 +1,145 @@ -package com.coding.basic; - -import java.util.Arrays; - -/** - * @author Hipple - * @Time:2017年2月20日 下午8:53:31 - * @version 1.0 - */ -public class ArrayList implements List { - - //元素数量 - private int size = 0; - - //默认容量 - private final int defaultCapacity = 10; - - //存储元素的容器 - private static Object[] elementData; - - //无参构造器 - public ArrayList(){ - elementData = new Object[defaultCapacity]; - } - - //指定容量的构造器 - public ArrayList(int capacity){ - if (capacity < 0) { - //非法参数 - throw new IllegalArgumentException("Illegal Capacity: "+ capacity); - } - elementData = new Object[capacity]; - } - - //添加元素 - public boolean add(Object o){ - ensureCapacityInternal(size + 1); - elementData[size++] = o; - return true; - } - - //添加元素到指定位置 - public void add(int index, Object o){ - rangeCheck(index); - //将当前位置及后续元素后移一位 - ensureCapacityInternal(size + 1); - 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 oldValue = elementData[index]; - int numMoved = size - index - 1; - if (numMoved > 0) { - //要删除的元素不是最后一个时,将当前元素及后续元素左移一位 - System.arraycopy(elementData, index+1, elementData, index, numMoved); - } - elementData[--size] = null;//自动回收 - return oldValue; - } - - //删除元素 - public boolean remove(Object o) { - // 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。 - 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 (o.equals(elementData[index])) { - fastRemove(index); - return true; - } - } - } - return false; - } - - //返回现有元素数量 - public int size(){ - return size; - } - - //是否为空 - public boolean isEmpty(){ - return size == 0; - } - - public Iterator iterator(){ - return null; - } - - //动态增加ArrayList大小 - private void ensureCapacityInternal(int minCapacity) { - //当前数组无法再存放时将数组长度增加至原长度的1.5倍 - if (minCapacity - elementData.length > 0) { - int newCapacity = (elementData.length * 3)/2; - elementData = Arrays.copyOf(elementData, newCapacity); - } - - } - - //检查是否下标越界 - private void rangeCheck(int index){ - if (index < 0 || index > this.size) { - throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); - } - } - - //删除元素,与remove的 差别就是没有下标检查 - private void fastRemove(int index) { - int numMoved = size - index - 1; - if (numMoved > 0){ - System.arraycopy(elementData, index + 1, elementData, index, numMoved); - } - elementData[--size] = null; - } - -} -class testArrayList{ - public static void main(String[] args) { - ArrayList arrayList = new ArrayList(); - for (int i = 0; i < 10; i++) { - arrayList.add(i+1); - } - arrayList.add(5,15); - arrayList.remove(11); - for (int i = 0; i < arrayList.size(); i++) { - System.out.println("value is "+arrayList.get(i)); - } - } -} +package com.coding.basic; + +import java.util.Arrays; + +/** + * @author Hipple + * @Time:2017年2月20日 下午8:53:31 + * @version 1.0 + */ +public class ArrayList implements List { + + //元素数量 + private int size = 0; + + //默认容量 + private final int defaultCapacity = 10; + + //存储元素的容器 + private static Object[] elementData; + + //无参构造器 + public ArrayList(){ + elementData = new Object[defaultCapacity]; + } + + //指定容量的构造器 + public ArrayList(int capacity){ + if (capacity < 0) { + //非法参数 + throw new IllegalArgumentException("Illegal Capacity: "+ capacity); + } + elementData = new Object[capacity]; + } + + //添加元素 + public boolean add(Object o){ + ensureCapacityInternal(size + 1); + elementData[size++] = o; + return true; + } + + //添加元素到指定位置 + public void add(int index, Object o){ + rangeCheck(index); + //将当前位置及后续元素后移一位 + ensureCapacityInternal(size + 1); + 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 oldValue = elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) { + //要删除的元素不是最后一个时,将当前元素及后续元素左移一位 + System.arraycopy(elementData, index+1, elementData, index, numMoved); + } + elementData[--size] = null;//自动回收 + return oldValue; + } + + //删除元素 + public boolean remove(Object o) { + // 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。 + 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 (o.equals(elementData[index])) { + fastRemove(index); + return true; + } + } + } + return false; + } + + //返回现有元素数量 + public int size(){ + return size; + } + + //是否为空 + public boolean isEmpty(){ + return size == 0; + } + + public Iterator iterator(){ + return null; + } + + //动态增加ArrayList大小 + private void ensureCapacityInternal(int minCapacity) { + //当前数组无法再存放时将数组长度增加至原长度的1.5倍 + if (minCapacity - elementData.length > 0) { + int newCapacity = (elementData.length * 3)/2; + elementData = Arrays.copyOf(elementData, newCapacity); + } + + } + + //检查是否下标越界 + private void rangeCheck(int index){ + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + } + + //删除元素,与remove的 差别就是没有下标检查 + private void fastRemove(int index) { + int numMoved = size - index - 1; + if (numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + } + +} +class testArrayList{ + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + for (int i = 0; i < 10; i++) { + arrayList.add(i+1); + } + arrayList.add(5,15); + arrayList.remove(11); + 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..01b8f2f62c 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java @@ -1,11 +1,11 @@ -package com.coding.basic; -/** - * @author Hipple - * @Time:2017年2月20日 下午8:56:05 - * @version 1.0 - */ -public interface Iterator { - public boolean hasNext(); - public Object next(); - +package com.coding.basic; +/** + * @author Hipple + * @Time:2017年2月20日 下午8:56:05 + * @version 1.0 + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + } \ No newline at end of file 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/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/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