diff --git a/.gitignore b/.gitignore index a7f8909d44..e88b72bb43 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,10 @@ *.war *.ear +*.iml +*.idea + + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* @@ -14,12 +18,24 @@ hs_err_pid* .metadata .recommenders <<<<<<< HEAD +<<<<<<< HEAD .idea ======= +======= + + +#macOS +.DS_Store + +>>>>>>> refs/remotes/onlyliuxin/master .idea/ *.iml rebel.* .rebel.* target +<<<<<<< HEAD +>>>>>>> refs/remotes/onlyliuxin/master +======= + >>>>>>> refs/remotes/onlyliuxin/master diff --git a/group12/2258659044/readme.txt b/group12/2258659044/readme.txt new file mode 100644 index 0000000000..59c627dd7e --- /dev/null +++ b/group12/2258659044/readme.txt @@ -0,0 +1 @@ +this is my projectWorkcpace! \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..5de89da950 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/ArrayList.java @@ -0,0 +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]; + } + + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..e5fae50203 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java @@ -0,0 +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; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..557728a02a --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +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; + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java b/group12/2258659044/zj-2017/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..c854120212 --- /dev/null +++ b/group12/2258659044/zj-2017/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/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java b/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..460298ff56 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/LinkedList.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + + Node addNode = new Node(); + addNode.data = o; + if(size==0){ + head = addNode; + }else{ + //获取最后一个节点 + Node lastNode = getPointNode(size-1); + lastNode.next = addNode; + } + size++; + } + public void add(int index , Object o){ + + Node addNode = new Node(); + addNode.data = o; + if(index == 0){ + addFirst(o); + return; + } + if(index == size){ + Node lastNode = getPointNode(size-1); + lastNode.next = addNode; + }else{ + Node pointNode = getPointNode(index); + Node prePointNode = getPointNode(index-1); + prePointNode.next = addNode; + addNode.next = pointNode; + } + size ++; + } + public Object get(int index){ + + Node node = getPointNode(index); + return node.data; + } + + public Object remove(int index){ + + Node pointNode = getPointNode(index); + Node nextPointNode = getPointNode(index+1); + if(index ==0){ + head = nextPointNode; + }else{ + Node prePointNode = getPointNode(index-1); + prePointNode.next = nextPointNode; + } + size --; + return pointNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + Node secondNode = head; + head = new Node(); + head.data = o; + if(size>0){ + head.next = secondNode; + } + size ++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + + return remove(0); + } + + public Object removeLast(){ + + return remove(size-1); + } + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != LinkedList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= LinkedList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return LinkedList.this.get(i); + } + + } + + /** + * 获取指定的节点 + * @return + */ + private Node getPointNode(int index){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size+""); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node{ + Object data; + Node next; + + } +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/List.java b/group12/2258659044/zj-2017/src/com/coding/basic/List.java new file mode 100644 index 0000000000..a5a3688eb6 --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java b/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..e29ff65ddf --- /dev/null +++ b/group12/2258659044/zj-2017/src/com/coding/basic/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList element = new LinkedList(); + + public void enQueue(Object o){ + + element.add(o); + } + + public Object deQueue(){ + + return element.removeFirst(); + } + + public boolean isEmpty(){ + + return element.size()==0; + } + + public int size(){ + + return element.size(); + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/Stack.java b/group12/2258659044/zj-2017/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..03709097e5 --- /dev/null +++ b/group12/2258659044/zj-2017/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(){ + + return elementData.remove(size()-1); + } + + public Object peek(){ + + return elementData.get(size()-1); + } + public boolean isEmpty(){ + + return size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..badcb2968f --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,109 @@ +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayListTest { + + ArrayList ls ; + @Before + public void setup() { + ls = new ArrayList(); + } + + /** + * 测试一个参数的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/BinaryTreeTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java new file mode 100644 index 0000000000..0f343ed895 --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java @@ -0,0 +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]); + } + } +} 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 new file mode 100644 index 0000000000..a55b2d5a3f --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java @@ -0,0 +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(); + } +} 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 new file mode 100644 index 0000000000..e688d9b41f --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java @@ -0,0 +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); + } +} 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 new file mode 100644 index 0000000000..a0875c8f3c --- /dev/null +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java @@ -0,0 +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); + } +} diff --git a/group12/247565311/week1/ArrayList.java b/group12/247565311/week1/ArrayList.java new file mode 100644 index 0000000000..c2643af683 --- /dev/null +++ b/group12/247565311/week1/ArrayList.java @@ -0,0 +1,249 @@ +package week1; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + + +public class ArrayList implements List { + private int size=0,offset=10; + private Object[] data = null; + public ArrayList(){ + data = new Object[offset]; + } + public ArrayList(int arg0){ + if(arg0<0) arg0=0; + size = arg0; + data = new Object[size]; + } + @Override + public boolean add(E arg0) { + if(arg0 == null) return false; + size += 1; + int leng = data.length; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;isize || 0leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg0) { + if (arg0 == null) return false; + int leng = data.length,newobjnum = arg0.size(),lastsize=size; + size += newobjnum; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg1) { + int newobjnum = arg1.size(),lastsize = size; + if(arg1 == null || arg0>size+1 || 0>arg0 || newobjnum==0) return false; + size += newobjnum; + int leng = data.length; + if(size>leng){ + Object[] newdata = new Object[size + offset]; + for(int i=0;i arg0) { + for(Object o:arg0){ + if(!this.contains(o)) return false; + } + return true; + } + + @Override + public E get(int arg0) { + if(arg0 >-1 && arg0-1;i--){ + if(this.data[i].equals(arg0)) return i; + } + return -1; + } + + @Override + public Iterator iterator() { + + return null; + } + + @Override + public ListIterator listIterator() { + + return null; + } + + @Override + public ListIterator listIterator(int arg0) { + + return null; + } + + @Override + public boolean remove(Object arg0) { + for(int i=0;ithis.size-1) return null; + E res = (E)data[arg0]; + for(int i=arg0;i arg0) { + int toberemovednums = arg0.size(); + if(!this.containsAll(arg0)) return false; + int index=0; + for(int i=0;i arg0) { + // what does this mean? + return false; + } + + @Override + public E set(int arg0, E arg1) { + if(arg0<0||arg0>this.size-1) return null; + this.data[arg0] = arg1; + return arg1; + } + + @Override + public int size() { + return this.size; + } + + @Override + public List subList(int arg0, int arg1) { + if(arg0>=arg1 || arg0<0 || arg1>this.size-1) return null; + List res = new ArrayList(); + for(int i=arg0;i T[] toArray(T[] arg0) { + T[] res = (T[])(new Object[this.size]); + for(int i=0;i { + private LinkedList data = new LinkedList(); + private int size = 0; + + + public Deque(){ + + } + public Deque(int arg0){ + data = new LinkedList(arg0); + } + public boolean push(E arg0){ + data.add(data.size(),arg0); + size += 1; + return true; + } + public E pop(){ + size -= 1; + E res = data.get(0); + data.remove(0); + return res; + } + public E peek(){ + return data.get(0); + } + public int size(){ + return this.size; + } + public boolean isEmpty(){ + return this.size==0; + } + +} diff --git a/group12/247565311/week1/Link.java b/group12/247565311/week1/Link.java new file mode 100644 index 0000000000..e2910e53d3 --- /dev/null +++ b/group12/247565311/week1/Link.java @@ -0,0 +1,5 @@ +package week1; + +public class Link { + +} diff --git a/group12/247565311/week1/LinkedList.java b/group12/247565311/week1/LinkedList.java new file mode 100644 index 0000000000..c3f0ca2eb8 --- /dev/null +++ b/group12/247565311/week1/LinkedList.java @@ -0,0 +1,266 @@ +package week1; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class LinkedList implements List,Cloneable { + private Node head = null; + private Node tail = null; + private int size = 0; + + public LinkedList(){ + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + public LinkedList(int arg0){ + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + public Object clone(){ + LinkedList clone = null; + try { + clone = (LinkedList)(super.clone()); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + clone.head = new Node(null); + clone.tail = new Node(null); + clone.size = 0; + for(Node x = head.next;x!=null;x = x.next){ + clone.add(x.val); + } + return clone; + } + @Override + public boolean add(Object val) { + Node n = new Node(val); + n.next = tail; + n.ahead = tail.ahead; + tail.ahead.next = n; + tail.ahead = n; + size += 1; + return true; + } + + @Override + public void add(int arg0, E arg1) { + if(arg0<0 || arg0>size) arg0=0; + Node n=new Node(arg1),p=head; + for(int i=0;i arg0) { + for(E o:arg0){ + this.add(o); + } + return true; + } + + @Override + public boolean addAll(int arg0, Collection arg1) { + for(E e:arg1){ + this.add(arg0,e); + arg0+=1; + } + return true; + } + + @Override + public void clear() { + head = new Node(null); + tail = new Node(null); + head.next = tail; + tail.ahead = head; + size = 0; + } + + @Override + public boolean contains(Object arg0) { + boolean flag = arg0==null; + Node n = head; + for(int i=0;i arg0) { + for(Object e:arg0){ + if(!this.contains(e)) return false; + } + return true; + } + + @Override + public E get(int arg0) { + E res = null; + if(arg0>-1 && arg0 < size){ + Node n = head; + for(int i=0;i iterator() { + + return null; + } + + @Override + public int lastIndexOf(Object arg0) { + boolean flag = arg0==null; + Node n = tail; + for(int i=size-1;i>-1;i--){ + n = n.ahead; + if(flag){ + if(n.val == null) return i; + }else{ + if(arg0.equals(n.val)) return i; + } + } + return -1; + } + + @Override + public ListIterator listIterator() { + + return null; + } + + @Override + public ListIterator listIterator(int arg0) { + + return null; + } + + @Override + public boolean remove(Object arg0) { + Node n = head; + int index = this.indexOf(arg0); + if(index == -1) return false; + for(int i=0;isize-1) return null; + for(int i=0;i arg0) { + for(Object o:arg0){ + if(!this.remove(o)) return false; + } + return true; + } + + @Override + public boolean retainAll(Collection arg0) { + // ? + return false; + } + + @Override + public E set(int arg0, E arg1) { + if(arg0<0 || arg0>size-1) return null; + Node n=head; + for(int i=0;i subList(int arg0, int arg1) { + + return null; + } + + @Override + public Object[] toArray() { + Object[]res = new Object[size]; + Node n = head; + for(int i=0;i T[] toArray(T[] arg0) { + + return null; + } + private static class Node{ + Object val = null; + Node next = null,ahead=null; + public Node(Object arg0){val = arg0;} + } +} diff --git a/group12/247565311/week1/Stack.java b/group12/247565311/week1/Stack.java new file mode 100644 index 0000000000..5672274d81 --- /dev/null +++ b/group12/247565311/week1/Stack.java @@ -0,0 +1,39 @@ +package week1; + +import java.util.List; + +public class Stack { + private List data = new ArrayList(); + private int size = 0; + + public Stack(){ + + } + + public Stack(int arg0){ + if(arg0 < 0) arg0 = 0; + size = arg0; + data = new ArrayList(size); + + } + public boolean isEmpty(){ + return size==0; + } + public boolean push(E arg0){ + size += 1; + data.add(arg0); + return true; + } + public E pop(){ + if(this.isEmpty()) return null; + size -= 1; + E res = data.get(size); + data.remove(size); + return res; + } + public E peek(){ + if(this.isEmpty()) return null; + E res = data.get(size-1); + return res; + } +} diff --git a/group12/251822722/ArrayList.java b/group12/251822722/ArrayList.java new file mode 100755 index 0000000000..77b8052cc7 --- /dev/null +++ b/group12/251822722/ArrayList.java @@ -0,0 +1,87 @@ +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int index =0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + elementData[size] = o; + size = size+1; + + } + + public void add(int index, Object o) { + + Object[] elementDataNew =null; + if(size= 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 new file mode 100644 index 0000000000..677eff3dab --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java @@ -0,0 +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; + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java new file mode 100644 index 0000000000..1a9bc5ad8a --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000000..a7dc12694e --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -0,0 +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; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java new file mode 100644 index 0000000000..2471c15d21 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java @@ -0,0 +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(); +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java new file mode 100644 index 0000000000..b14751aab7 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java @@ -0,0 +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(); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java new file mode 100644 index 0000000000..f743d0dd3b --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java @@ -0,0 +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(); + } +} 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 new file mode 100644 index 0000000000..ec3a7600a4 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java @@ -0,0 +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()); + } +} 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 new file mode 100644 index 0000000000..83972b7776 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java @@ -0,0 +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()); + } + +} 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 new file mode 100644 index 0000000000..054e8f81b4 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java @@ -0,0 +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()); + } + +} 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 new file mode 100644 index 0000000000..86a4ebdd68 --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java @@ -0,0 +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()); + } + +} 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 new file mode 100644 index 0000000000..36781c863f --- /dev/null +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java @@ -0,0 +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()); + } + +} diff --git a/group12/382266293/.classpath b/group12/382266293/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group12/382266293/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group12/382266293/.gitignore b/group12/382266293/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/382266293/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/382266293/.project b/group12/382266293/.project new file mode 100644 index 0000000000..1282023911 --- /dev/null +++ b/group12/382266293/.project @@ -0,0 +1,17 @@ + + + 382266293Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group12/382266293/src/Collection/AbstractList.java b/group12/382266293/src/Collection/AbstractList.java new file mode 100644 index 0000000000..a81e76b587 --- /dev/null +++ b/group12/382266293/src/Collection/AbstractList.java @@ -0,0 +1,43 @@ +package Collection; + +public abstract class AbstractList implements List { + + protected static final String PREFIX = "["; + protected static final String SUFFIX = "]"; + protected static final String SEPERATOR = ", "; + protected static final int MAX_SIZE = Integer.MAX_VALUE/3; + + protected void checkIndex(int i) { + if( i < 0 || i > Math.min(size(), MAX_SIZE)) + throw new IndexOutOfBoundsException("Size :" + size()+", Index: " + i); + } + + public boolean isEmpty() { + return size() == 0; + } + + + public int indexOf(E e) { + for (int i = 0; i < size()-1; i++) { + if (get(i).equals(e)) + return i; + } + return -1; + } + + protected abstract Iterator iterator(); + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(PREFIX); + for (int i = 0; i < size(); i++) { + sb.append(get(i)); + if (i < size()-1) + sb.append(SEPERATOR); + } + sb.append(SUFFIX); + return sb.toString(); + } + +} diff --git a/group12/382266293/src/Collection/Concrete/ArrayList.java b/group12/382266293/src/Collection/Concrete/ArrayList.java new file mode 100644 index 0000000000..3db5ab47e6 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/ArrayList.java @@ -0,0 +1,148 @@ +package Collection.Concrete; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class ArrayList extends AbstractList { + + private E[] elements; + private int size; + private static final int INITIAL_SIZE = 16; + + + public ArrayList() { + elements = (E[]) new Object[INITIAL_SIZE]; + size = 0; + } + + @Override + public void add(E e) { + checkCapacity(); + elements[size++] = e; + } + + private void checkCapacity() { + if (size >= MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max size"); + + if (elements.length - size < INITIAL_SIZE) + grow(); + } + + synchronized private void grow() { + + int newCapacity = size * 2; + newCapacity = (newCapacity < 0 || newCapacity - MAX_SIZE > 0) ? MAX_SIZE : newCapacity; + E[] target = (E[]) new Object[newCapacity]; + System.arraycopy(elements, 0, target, 0, size); + elements = target; + + } + + public void add(int index, E e) { + checkCapacity(); + if (index == size) { + add(e); + return; + } + checkIndex(index); + synchronized (this) { + System.arraycopy(elements, index, elements, index+1, size-index+1); + elements[index] = e; + size++; + } + } + + @Override + public E get(int index) { + checkIndex(index); + return elements[index]; + } + + + public E getLast() { + return get(size-1); + } + + public void addLast(E e) { + add(e); + } + + public E removeLast() { + return elements[--size]; + } + + public E remove(int index) { + checkIndex(index); + E result = elements[index]; + synchronized (this) { + System.arraycopy(elements, index+1, elements, index, size-index-1); + elements[--size] = null; + } + return result; + } + + @Override + public int size() { + return size; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(elements); + result = prime * result + size; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ArrayList other = (ArrayList) obj; + if (!Arrays.equals(elements, other.elements)) + return false; + if (size != other.size) + return false; + return true; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public ArrayListIterator(ArrayList arrayList) { + myArrayList = arrayList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) elements[pos++]; + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java new file mode 100644 index 0000000000..34a9d4253e --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java @@ -0,0 +1,126 @@ +package Collection.Concrete; + +public class BinaryTreeNode> { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + private ArrayList myList; + + + + public BinaryTreeNode() { + this.data = null; + this.left = null; + this.right = null; + } + + public BinaryTreeNode(E data) { + this.data = data; + this.left = null; + this.right = null; + } + + public boolean isEmpty() { + return data == null; + } + + public int size() { + return size; + } + + public BinaryTreeNode insert(E o) { + BinaryTreeNode res; + if (isEmpty()) { + data = o; + size++; + return this; + } else { + BinaryTreeNode p = this; + res = new BinaryTreeNode(o); + while (true) { + if (res.getData().compareTo(p.getData()) < 0) { + if (p.left == null) { + p.setLeft(res); + break; + } + p = p.left; + } else if (res.getData().compareTo(p.getData()) > 0) { + if (p.right == null) { + p.setRight(res); + break; + } + p = p.right; + } else { + return null; + } + } + size++; + } + return res; + } + + + + public ArrayList preOrderTraversal(BinaryTreeNode node) { + + if (node != null) { + preOrderTraversal(node.left); + myList.add(node.data); + preOrderTraversal(node.right); + } + return myList; + } + + @Override + public String toString() { + myList = new ArrayList(); + return preOrderTraversal(this).toString(); + } + + public E getData() { + return data; + } + public void setData(E 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; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BinaryTreeNode other = (BinaryTreeNode) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + +} \ No newline at end of file diff --git a/group12/382266293/src/Collection/Concrete/LinkedList.java b/group12/382266293/src/Collection/Concrete/LinkedList.java new file mode 100644 index 0000000000..66dbd9d8d0 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/LinkedList.java @@ -0,0 +1,196 @@ +package Collection.Concrete; + +import java.util.NoSuchElementException; +import Collection.AbstractList; +import Collection.Iterator; + + +public class LinkedList extends AbstractList { + + private Node head; + private int size; + + public LinkedList() { + this.head = new Node(null); + this.size = 0; + } + + @Override + public void add(E e) { + addLast(e); + } + + + @Override + public E get(int index) { + checkIndex(index); + return getNode(index).data; + } + + public E getFirst() { + return get(0); + } + + public E getLast() { + return get(size-1); + } + + + public void add(int index, E e) { + if (index == size) { + addLast(e); + return; + } + + if (index == 0) { + addFirst(e); + return; + } + + checkIndex(index); + Node pNode = new Node(e); + Node p = getNode(index); + synchronized (this) { + getNode(index-1).next = pNode; + pNode.next = p; + size++; + } + } + + public void addFirst(E e){ + checkCapacity(); + Node pNode = new Node(e); + Node oldHead = head; + head = pNode; + pNode.next = oldHead; + size++; + return; + } + + public void addLast(E e){ + if (size == 0) { + addFirst(e); + return; + } + + checkCapacity(); + Node res = new Node(e); + setLastNode(res); + size++; + return; + } + + public E removeFirst(){ + return remove(0); + } + public E removeLast(){ + return remove(size-1); + } + + public E remove(int index) { + checkIndex(index); + Node pNode = getNode(index); + if (index == 0) { + head = head.next; + } else if (index == size-1 ) { + getNode(index-1).next = null; + } else { + getNode(index-1).next = getNode(index+1); + } + size--; + return pNode.data; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private void checkCapacity() { + if (size > MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max capacity: "+ MAX_SIZE); + } + + private Node getNode(int index) { + if (size == 0) + return head; + + Node pNode = head; + for ( int i = 0; i < index ; i++) { + pNode = pNode.next; + } + return pNode; + } + + private void setLastNode(Node res) { + getNode(size-1).next = res; + } + + private static class Node { + E data; + Node next; + + public Node(E data) { + this.data = data; + this.next = null; + } + + @Override + public String toString() { + return data.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Node other = (Node) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + } + + @SuppressWarnings("hiding") + private class LinkedListIterator implements Iterator { + + private LinkedList myLinkedList; + private int pos; + + public LinkedListIterator(LinkedList linkedList) { + myLinkedList = linkedList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos++); + throw new NoSuchElementException(); + } + } +} diff --git a/group12/382266293/src/Collection/Concrete/Queue.java b/group12/382266293/src/Collection/Concrete/Queue.java new file mode 100644 index 0000000000..300fb633af --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Queue.java @@ -0,0 +1,84 @@ +package Collection.Concrete; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Queue extends AbstractList { + + private LinkedList myList; + + public Queue() { + this.myList = new LinkedList(); + } + + public void enQueue(E e){ + myList.addLast(e); + } + + public E deQueue(){ + if (0 == size()) + return null; + return myList.removeFirst(); + } + + @Override + public void add(E e) { + enQueue(e); + } + + @Override + public E get(int index) { + if (0 == size()) + return null; + return myList.get(index); + } + + public E element() { + if (0 == size()) + return null; + return myList.getFirst(); + } + + + @Override + public int size() { + return myList.size(); + } + + @Override + protected Iterator iterator() { + return myList.iterator(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Queue other = (Queue) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + + + + + +} diff --git a/group12/382266293/src/Collection/Concrete/Stack.java b/group12/382266293/src/Collection/Concrete/Stack.java new file mode 100644 index 0000000000..17ec4364fa --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Stack.java @@ -0,0 +1,106 @@ +package Collection.Concrete; + +import java.util.EmptyStackException; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Stack extends AbstractList { + + private ArrayList myList; + + public Stack() { + this.myList = new ArrayList(); + } + + public void push(E e){ + myList.addLast(e); + } + + public E pop(){ + checkEmpty(); + return myList.removeLast(); + } + + private void checkEmpty() { + if (0 == size()) + throw new EmptyStackException(); + } + + public E peek(){ + checkEmpty(); + return myList.getLast(); + } + + public int size(){ + return myList.size(); + } + + @Override + public void add(E e) { + push(e); + } + + @Override + public E get(int index) { + checkEmpty(); + return myList.get(size() - index - 1); + } + + @Override + protected Iterator iterator() { + return new StackIterator(myList); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Stack other = (Stack) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + private class StackIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public StackIterator(ArrayList myList) { + myArrayList = myList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos); + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Iterator.java b/group12/382266293/src/Collection/Iterator.java new file mode 100644 index 0000000000..d51656a3a8 --- /dev/null +++ b/group12/382266293/src/Collection/Iterator.java @@ -0,0 +1,7 @@ +package Collection; + +public interface Iterator { + + public boolean hasNext(); + public E next(); +} diff --git a/group12/382266293/src/Collection/List.java b/group12/382266293/src/Collection/List.java new file mode 100644 index 0000000000..7ddb685cac --- /dev/null +++ b/group12/382266293/src/Collection/List.java @@ -0,0 +1,16 @@ +package Collection; + +public interface List { + + public void add(E e); + + public int size(); + + public E get(int index); + + public boolean isEmpty(); + + public int indexOf(E e); + + +} diff --git a/group12/382266293/src/TestCollection/AllTests.java b/group12/382266293/src/TestCollection/AllTests.java new file mode 100644 index 0000000000..cd4eeadb58 --- /dev/null +++ b/group12/382266293/src/TestCollection/AllTests.java @@ -0,0 +1,11 @@ +package TestCollection; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayListTest.class, BinaryTreeNodeTest.class, LinkedListTest.class, QueueTest.class, StackTest.class }) +public class AllTests { + +} diff --git a/group12/382266293/src/TestCollection/ArrayListTest.java b/group12/382266293/src/TestCollection/ArrayListTest.java new file mode 100644 index 0000000000..f29580616f --- /dev/null +++ b/group12/382266293/src/TestCollection/ArrayListTest.java @@ -0,0 +1,173 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.ArrayList; +import junit.framework.TestCase; + +public class ArrayListTest extends TestCase { + + + private ArrayList myAL; + private static Random rnd = new Random(); + + @Before + public void setUp() throws Exception { + + myAL = new ArrayList(); + assertEquals(true,myAL.isEmpty()); + + } + + @After + public void tearDown() throws Exception { + myAL = null; + } + + @Test + public void testRawTypeArrayList() { + + List rawList = new ArrayList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + } + + @Test + public void testEmpty() { + + assertEquals(true,myAL.isEmpty()); + + myAL.add(5); + assertEquals(false,myAL.isEmpty()); + + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(false,myAL.isEmpty()); + + } + + @Test + public void testAddIntAutoBoxing() { + + myAL.add(5); + myAL.add(5); + myAL.add(5); + myAL.add(1,10); + int c = myAL.get(1); + assertEquals(10,c); + + assertEquals(4,myAL.size()); + myAL.add(4,15); + int a = myAL.get(0); + Integer b = myAL.get(1); + c = myAL.get(2); + int d = myAL.get(3); + int e = myAL.get(4); + assertEquals(5,a); + assertEquals(new Integer(10),b); + assertEquals(5,c); + assertEquals(5,d); + assertEquals(15,e); + } + + @Test + public void testGet() { + + int[] result = addRandomInt(myAL, getRandomNumber()); + + int actual,expected; + + for (int i = 0; i < result.length; i++) { + actual = myAL.get(i); + expected = result[i]; + assertEquals(expected, actual); + } + + } + + @Test + public void testRemove() { + + addIntWithNatureOrder(myAL, 100); + + testRemoveAndGetFromTail(myAL); + try { + myAL.remove(10); + fail("no exception thrown"); + } catch (Exception e) { + assertEquals(IndexOutOfBoundsException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + + assertEquals(0,myAL.size()); + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(num,myAL.size()); + } + + + + @Test + public void testGrow() { + + int actualSize = 12345; + + addIntWithNatureOrder(myAL, actualSize); + + assertEquals(actualSize,myAL.size()); + } + + + @Test + public void testIterator() { + + addIntWithNatureOrder(myAL,100); + + Iterator it = myAL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + int actual = it.next(); + assertEquals(i,actual); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + @Test + public void testIndexOf() { + + int num = 200; + addIntWithNatureOrder(myAL,num); + + int expected,actual; + for (int i = 0; i < num-1; i++) { + expected = i; + actual = myAL.indexOf(i); + assertEquals(expected, actual); + } + + assertEquals(-1, myAL.indexOf(-1*getRandomNumber())); + + } + +} diff --git a/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..275ef59484 --- /dev/null +++ b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java @@ -0,0 +1,54 @@ +package TestCollection; + +import static util.Print.*; +import java.util.Random; +import java.util.Set; +import java.util.TreeSet; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.BinaryTreeNode; +import junit.framework.TestCase; + +public class BinaryTreeNodeTest extends TestCase { + + private BinaryTreeNode myTree; + + @Before + public void setUp() throws Exception { + myTree = new BinaryTreeNode(); + assertEquals(0, myTree.size()); + } + + @After + public void tearDown() throws Exception { + myTree = null; + } + + @Test + public void testInsert() { + Set expected = new TreeSet(); + int size = getRandomNumber(); + int j = 0 ; + while (expected.size() != size) { + j = getRandomNumber(); + expected.add(j); + myTree.insert(j); + } + + assertEquals(size,myTree.size()); + assertEquals(expected.toString(),myTree.toString()); + } + + public void testSize() { + + for (int i = 0; i < getRandomNumber(); i++) { + myTree.insert(18); + myTree.insert(-19); + myTree.insert(1); + assertEquals(3,myTree.size()); + } + } +} \ No newline at end of file diff --git a/group12/382266293/src/TestCollection/LinkedListTest.java b/group12/382266293/src/TestCollection/LinkedListTest.java new file mode 100644 index 0000000000..8b9fad15eb --- /dev/null +++ b/group12/382266293/src/TestCollection/LinkedListTest.java @@ -0,0 +1,212 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.LinkedList; +import junit.framework.TestCase; + + +public class LinkedListTest extends TestCase { + + LinkedList myLL; + + @Before + public void setUp() throws Exception { + myLL = new LinkedList(); + assertEquals(0,myLL.size()); + } + + @After + public void tearDown() throws Exception { + myLL = null; + } + + @Test + public void testLinkedList() { + List rawList = new LinkedList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + + } + + @Test + public void testAddE() { + myLL.add("s"); + assertEquals(1,myLL.size()); + assertEquals(false,myLL.isEmpty()); + } + + @Test + public void testAddStringE() { + String a; + + addString(myLL,30); + println(myLL.get(0)); + +// for (int i = 0 ; i < 29; i++) { +// a = "" + i; +// assertEquals(myLL.get(i),a); +// } + } + + @Test + public void testAddIndex() { + String a; + for (int i = 0 ; i < 30; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "ss"; + myLL.add(3,ss); + assertEquals(myLL.get(3), ss); + assertEquals(myLL.get(2), "2"); + assertEquals(myLL.get(4), "3"); + + } + + public void testAddFirst() { + String a; + for (int i = 0 ; i < 20; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "bba"; + myLL.addFirst(ss); + assertEquals(ss,myLL.get(0)); + assertEquals(21, myLL.size()); + + ; + for (int i = 1 ; i < myLL.size(); i++) { + a = (i-1) + ""; + assertEquals(a, myLL.get(i)); + } + } + + public void testAddLast() { + String a; + for (int i = 0 ; i < 25; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "25"; + myLL.addLast(ss); + int size = myLL.size(); + assertEquals(26, size); + + for (int i = 0 ; i < size; i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + } + + @Test + public void testRemoveFirst() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeFirst(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i+1 + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = i+1 +""; + result = myLL.removeFirst(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + } + + @Test + public void testRemoveLast() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeLast(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = myLL.size()-1 +""; + result = myLL.removeLast(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + + } + + + @Test + public void testRemove() { + + String res = ""; + String a = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + for(int i = myLL.size()-1; i >= 0; i--) { + a = myLL.get(i); + res = myLL.remove(i); + assertEquals(i, myLL.size()); + assertEquals(a,res); + } + } + + @Test + public void testSize() { + assertEquals(0,myLL.size()); + } + + @Test + public void testIterator() { + for(int i = 0; i<10; i++) { + myLL.add(i+""); + } + Iterator it = myLL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + String res = it.next(); + assertEquals(i+"",res); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/QueueTest.java b/group12/382266293/src/TestCollection/QueueTest.java new file mode 100644 index 0000000000..01a9aa31f2 --- /dev/null +++ b/group12/382266293/src/TestCollection/QueueTest.java @@ -0,0 +1,98 @@ +package TestCollection; + +import static util.Print.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static util.TestUtil.*; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + +public class QueueTest extends TestCase { + + private Queue myQueue; + + @Before + public void setUp() throws Exception { + myQueue= new Queue(); + } + + @After + public void tearDown() throws Exception { + myQueue = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myQueue.isEmpty()); + myQueue.enQueue(getRandomNumber()); + assertEquals(false, myQueue.isEmpty()); + } + + @Test + public void testEnQueue() { + + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + + } + + @Test + public void testDeQueue() { + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + int size = myQueue.size(); + for (int i = 0; i < size ; i++) { + assertEquals(size-i, myQueue.size()); + int expect = i; + int actual = myQueue.deQueue(); + assertEquals(expect, actual); + } + + assertEquals(null, myQueue.deQueue()); + assertEquals(null, myQueue.element()); + assertEquals(null, myQueue.get(0)); + + } + + @Test + public void testelement() { + + int expected = 0; + int element1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myQueue.enQueue(i); + expected = i; + + element1 = myQueue.element(); + assertEquals(expected, element1); + + for (int j = 0; j < i; j++) { + repeated = myQueue.element(); + assertEquals(expected, repeated); + } + myQueue.deQueue(); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myQueue.size()); + myQueue.enQueue(i); + } + } + + @Test + public void testAdd() { + for (int i = 0; i < 10; i++) { + myQueue.add(i); + Integer actual = new Integer(myQueue.get(i)); + Integer expected = new Integer(i); + assertEquals(expected, actual); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/src/TestCollection/StackTest.java new file mode 100644 index 0000000000..3784a9b972 --- /dev/null +++ b/group12/382266293/src/TestCollection/StackTest.java @@ -0,0 +1,129 @@ +package TestCollection; + +import static util.Print.*; + +import java.util.EmptyStackException; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.Stack; +import junit.framework.TestCase; + +public class StackTest extends TestCase { + + Stack myStack; + + @Before + public void setUp() throws Exception { + myStack= new Stack(); + } + + @After + public void tearDown() throws Exception { + myStack = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myStack.isEmpty()); + myStack.push(getRandomNumber()); + assertEquals(false, myStack.isEmpty()); + } + + @Test + public void testPush() { + for (int i = 0; i < 10; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testPop() { + testPush(); + int size = myStack.size(); + for (int i = size; i > 0; i--) { + assertEquals(i, myStack.size()); + int expect = i-1; + assertEquals(i, myStack.size()); + int actual = myStack.pop(); + assertEquals(expect, actual); + } + + try { + myStack.pop(); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + } + + @Test + public void testPeek() { + + int expected = 0; + int peek1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myStack.push(i); + expected = i; + + peek1 = myStack.peek(); + assertEquals(expected, peek1); + + for (int j = 0; j < i; j++) { + repeated = myStack.peek(); + assertEquals(expected, repeated); + } + } + + } + + + public void testGet() { + + try { + myStack.get(getRandomNumber()); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testAdd() { + myStack.push(5); + myStack.push(10); + myStack.push(15); + println(myStack.get(0)); + println(myStack.get(1)); + println(myStack.get(2)); + + } + + @Test + public void testPopPushAndPeek() { + for (int i = 0; i < 10; i++) { + int expected = i; + myStack.push(i); + int a = 0; + myStack.push(a); + myStack.pop(); + int actual = myStack.peek(); + assertEquals(expected, actual); + } + } + +} diff --git a/group12/382266293/src/test.java b/group12/382266293/src/test.java new file mode 100644 index 0000000000..e4e7fead2d --- /dev/null +++ b/group12/382266293/src/test.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; +import java.util.Queue; +import java.util.Stack; +import java.util.concurrent.PriorityBlockingQueue; +import static util.Print.*; + +public class test { + + public static void main(String[] args) { + Queue queue = new PriorityBlockingQueue(); + println(queue.poll()); + + } + +} diff --git a/group12/382266293/src/util/Print.java b/group12/382266293/src/util/Print.java new file mode 100644 index 0000000000..b2ae48552b --- /dev/null +++ b/group12/382266293/src/util/Print.java @@ -0,0 +1,14 @@ +package util; + + +public class Print { + + public static void print(Object o){ + System.out.print(o); + } + + public static void println(Object o){ + System.out.println(o); + } + +} diff --git a/group12/382266293/src/util/TestUtil.java b/group12/382266293/src/util/TestUtil.java new file mode 100644 index 0000000000..2dfeeade7f --- /dev/null +++ b/group12/382266293/src/util/TestUtil.java @@ -0,0 +1,78 @@ +package util; + +import java.util.Random; + +import Collection.List; +import Collection.Concrete.ArrayList; +import Collection.Concrete.LinkedList; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + + +public class TestUtil extends TestCase { + + private static Random random = new Random(); + private static final int RANDOM_BOUND = 2<<15; + private static final int RANDOM_SIZE = 500; + + + public static int getRandomNumber() { + return random.nextInt(RANDOM_SIZE); + } + + public static void addIntWithNatureOrder(List myList, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myList.add(acutal); + } + } + + public static int[] addRandomInt(List myList, int numbers) { + + int actual = 0; + int[] result = new int[numbers]; + for (int i = 0; i < numbers ; i++) { + actual = random.nextInt(RANDOM_BOUND); + result[i] = actual; + myList.add(actual); + } + return result; + } + + public static void addString(List myList, int num) { + + String actual; + for (int index = 0; index < num ; index++) { + actual = index + ""; + myList.add(actual); + } + } + + public static void testRemoveAndGetFromTail(ArrayList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void testRemoveAndGetFromTail(LinkedList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void enQueueIntWithNatureOrder(Queue myQueue, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myQueue.enQueue(acutal); + } + } + +} diff --git a/group12/441908378/ArrayList.java b/group12/441908378/ArrayList.java new file mode 100755 index 0000000000..45e495867c --- /dev/null +++ b/group12/441908378/ArrayList.java @@ -0,0 +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; + } + } + + +} diff --git a/group12/441908378/LinkedList.java b/group12/441908378/LinkedList.java new file mode 100755 index 0000000000..0d0339bc01 --- /dev/null +++ b/group12/441908378/LinkedList.java @@ -0,0 +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 elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} diff --git a/group12/446031103/src/com/coding/basic/BinaryTreeNode.java b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..ed26d946bb --- /dev/null +++ b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,44 @@ +package com.coding.basic; + + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode( Object data,BinaryTreeNode left,BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(BinaryTreeNode tree,Object o){ + if(null== tree){ + tree = new BinaryTreeNode(o, null, null); + } + if(Integer.valueOf(o.toString())>Integer.valueOf(tree.data.toString())){ + tree.right =insert(tree.right,o); + }else{ + tree.left = insert(tree.left,o); + } + return tree; + } + +} diff --git a/group12/446031103/src/com/coding/basic/Iterator.java b/group12/446031103/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/LinkedList.java b/group12/446031103/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de508694a0 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/LinkedList.java @@ -0,0 +1,225 @@ +package com.coding.basic; + +/** + * + * LinkedList集合-链 + * + * @ClassName LinkedList + * @author msh + * @date 2017年2月21日 下午4:08:01 + */ +public class LinkedList implements List { + //链头 + private Node head; + //集合大小 + private int size=0; + /** + * + * 向链中添加元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + Node newNode = new Node(o, null); + if (null == head) { + head = newNode; + } else { + Node lastNode = null; + for (int i = 0; i < size; i++) { + lastNode = (Node) get(i); + } + lastNode.next = newNode; + } + size++; + } + /** + * + * 向链中添加元素 + * + * @Method add 增加 + * @param index 下标 + * @param o 元素 + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + validate(index); + Node newNode = null; + Node perNode = null; + Node nextNode = null; + // 当为最后插入时 + if (index == size - 1) { + newNode = new Node(o, null); + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + perNode.next = newNode; + } else if (0 == index) { + nextNode = head.next; + newNode = new Node(o, nextNode); + head = newNode; + } else { + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + nextNode = perNode.next.next; + newNode = new Node(o, nextNode); + perNode.next = newNode; + } + size++; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + Node tempNode = head; + for (int i = 0; i <= index; i++) { + tempNode = tempNode.next; + } + return tempNode; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index){ + Node removeNode = (Node) get(index); + validate(index); + if (index == size - 1) { + Node tempNode = head; + for (int i = 0; i < index; i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + } else if (index == 0) { + Node tempNode = head.next; + head.next = null; + head = tempNode; + } else { + } + size--; + return removeNode; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.coding.basic.List#size() + */ + public int size(){ + return size; + } + /** + * + * 想链头中插入元素 + * + * @MethodName addFirst + * @author msh + * @date 2017年2月21日 下午4:10:56 + * @param o + */ + public void addFirst(Object o){ + Node newNode = new Node(o, head); + head = newNode; + } + /** + * + * 向链后加入元素 + * + * @MethodName addLast + * @author msh + * @date 2017年2月21日 下午4:11:43 + * @param o + */ + public void addLast(Object o){ + add(o); + } + /** + * + * 删除链头 + * + * @MethodName removeFirst + * @author msh + * @date 2017年2月21日 下午4:12:14 + * @return + */ + public Object removeFirst(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node orgHead = head; + Node tempNode = head.next; + head.next = null; + head = tempNode; + return orgHead; + } + /** + * + * 删除链尾 + * + * @MethodName removeLast + * @author zhaogd + * @date 2017年2月21日 下午4:12:44 + * @return + */ + public Object removeLast(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node lastNode = (Node) get(size); + Node tempNode = head; + for (int i = 0; i < (size - 1); i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + return lastNode; + } + public Iterator iterator(){ + return null; + } + + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + /** + * + * 链中元素 + * + * @ClassName Node + * @author zhaogd + * @date 2017年2月21日 下午4:13:10 + */ + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group12/446031103/src/com/coding/basic/List.java b/group12/446031103/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/Queue.java b/group12/446031103/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3844d9dd24 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +/** + * + * 队列-先进先出 + * + * @ClassName Queue + * @author msh + * @date 2017年2月21日 下午9:29:03 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + /** + * + * 入队列 + * + * @MethodName enQueue + * @author msh + * @date 2017年2月21日 下午9:45:15 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + /** + * + * 离开队列 + * + * @MethodName deQueue + * @author msh + * @date 2017年2月21日 下午9:56:06 + * @return + */ + public Object deQueue(){ + if(isEmpty()) + throw new IndexOutOfBoundsException("size:"+size()); + Object o=elementData.get(0); + elementData.removeFirst(); + return o; + } + /** + * + * 是否为空 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:57:14 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==elementData.size()) + temp= true; + return temp; + } + /** + * + * 队列中元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:57:28 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group12/446031103/src/com/coding/basic/Stack.java b/group12/446031103/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d1c58b671 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Stack.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +/** + * + * 栈-先进后出 + * + * @ClassName Stack + * @author msh + * @date 2017年2月21日 下午9:05:39 + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + /** + * + * 向栈中加入元素 + * + * @MethodName push + * @author msh + * @date 2017年2月21日 下午9:12:03 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + /** + * + * 从栈中取出元素 + * + * @MethodName pop + * @author msh + * @date 2017年2月21日 下午9:12:51 + * @return + */ + public Object pop(){ + Object o= peek(); + elementData.remove(size()-1); + return o; + } + /** + * + * 取出栈顶元素 + * + * @MethodName peek + * @author msh + * @date 2017年2月21日 下午9:13:08 + * @return + */ + public Object peek(){ + Object o=elementData.get(size()-1); + return o; + } + /** + * + * 判断栈中是否有元素 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:14:26 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==size()) + temp = true; + return temp; + } + /** + * + * 栈中有多少元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:16:42 + * @return + */ + public int size(){ + return elementData.size(); + } + +} diff --git a/group12/495473393/Coding/.gitignore b/group12/495473393/Coding/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/495473393/Coding/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/495473393/Coding/src/Base/ArrayList.java b/group12/495473393/Coding/src/Base/ArrayList.java new file mode 100644 index 0000000000..0b99f87873 --- /dev/null +++ b/group12/495473393/Coding/src/Base/ArrayList.java @@ -0,0 +1,32 @@ +package Base; + +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/group12/495473393/Coding/src/Base/BinaryTreeNode.java b/group12/495473393/Coding/src/Base/BinaryTreeNode.java new file mode 100644 index 0000000000..875abe8c80 --- /dev/null +++ b/group12/495473393/Coding/src/Base/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package Base; + +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/group12/495473393/Coding/src/Base/Iterator.java b/group12/495473393/Coding/src/Base/Iterator.java new file mode 100644 index 0000000000..19b65c3637 --- /dev/null +++ b/group12/495473393/Coding/src/Base/Iterator.java @@ -0,0 +1,7 @@ +package Base; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/495473393/Coding/src/Base/LinkedList.java b/group12/495473393/Coding/src/Base/LinkedList.java new file mode 100644 index 0000000000..0d4ce20167 --- /dev/null +++ b/group12/495473393/Coding/src/Base/LinkedList.java @@ -0,0 +1,46 @@ +package Base; + +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/group12/495473393/Coding/src/Base/List.java b/group12/495473393/Coding/src/Base/List.java new file mode 100644 index 0000000000..6c910af600 --- /dev/null +++ b/group12/495473393/Coding/src/Base/List.java @@ -0,0 +1,9 @@ +package Base; + +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/495473393/Coding/src/Base/Queue.java b/group12/495473393/Coding/src/Base/Queue.java new file mode 100644 index 0000000000..9c2948d7e3 --- /dev/null +++ b/group12/495473393/Coding/src/Base/Queue.java @@ -0,0 +1,19 @@ +package Base; + +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/group12/495473393/Coding/src/Base/Stack.java b/group12/495473393/Coding/src/Base/Stack.java new file mode 100644 index 0000000000..8f59343d4b --- /dev/null +++ b/group12/495473393/Coding/src/Base/Stack.java @@ -0,0 +1,22 @@ +package Base; + +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/group12/563253496/datastructure/.classpath b/group12/563253496/datastructure/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group12/563253496/datastructure/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group12/563253496/datastructure/.gitignore b/group12/563253496/datastructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/563253496/datastructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/563253496/datastructure/.project b/group12/563253496/datastructure/.project new file mode 100644 index 0000000000..4ae7fd9359 --- /dev/null +++ b/group12/563253496/datastructure/.project @@ -0,0 +1,17 @@ + + + datastructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group12/563253496/datastructure/src/Collection/ArrayList.java b/group12/563253496/datastructure/src/Collection/ArrayList.java new file mode 100644 index 0000000000..597a9b1a3a --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/ArrayList.java @@ -0,0 +1,173 @@ +package Collection; + +import com.coding.basic.List; + +import java.util.NoSuchElementException; + +import com.coding.basic.Iterator; + +public class ArrayList implements List { + + private int size; + private Object[] elementData; + + public ArrayList() { + size = 0; + elementData = new Object[10]; + } + + public ArrayList(Object o) { + size = 0; + elementData = new Object[10]; + this.add(o); + } + + public ArrayList(int initialCapacity) { + size = 0; + elementData = new Object[initialCapacity]; + } + + @Override + public void add(Object o) { + if (size <= elementData.length - 1) { + elementData[size] = o; + size++; + } else { + this.extendCapacity(); + elementData[size] = o; + size++; + + } + } + + @Override + public void add(int index, Object o) { + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + if (index > elementData.length - 1) { + while (index > elementData.length - 1) { + this.extendCapacity(); + } + elementData[index] = o; + size = index + 1; + return; + } + + if (index >= size) { + size = index + 1; + elementData[index] = o; + return; + } + if (index >= 0 && index < size) { + this.moveRearward(index); + elementData[index] = o; + size++; + return; + } + } + + @Override + public Object get(int index) { + checkCapacity(index); + if (index < size) { + return elementData[index]; + } + return null; + } + + @Override + public Object remove(int index) { + checkCapacity(index); + + if (index == size - 1) { + size--; + return elementData[size - 1]; + } + if (index < size - 1) { + Object tmp = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return tmp; + } + return null; + } + + private void checkCapacity(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void extendCapacity() { + Object[] elements = new Object[elementData.length + 10]; + for (int i = 0; i < size; i++) { + elements[i] = elementData[i]; + } + elementData = elements; + + } + + @Override + public int size() { + return size; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]); + sb.append(","); + } + sb.deleteCharAt(sb.length() - 1); + sb.append("]"); + return sb.toString(); + } + + private void moveRearward(int index) { + size++; + + if (size >= elementData.length - 1) + this.extendCapacity(); + + for (int i = size - 1; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int pos; + + public ArrayListIterator() { + + pos = 0; + } + + @Override + public boolean hasNext() { + if (pos < size) { + return true; + } + return false; + } + + @Override + public Object next() { + if (hasNext()) { + return elementData[pos++]; + } else + throw new NoSuchElementException(); + + } + + } + +} diff --git a/group12/563253496/datastructure/src/Collection/LinkedList.java b/group12/563253496/datastructure/src/Collection/LinkedList.java new file mode 100644 index 0000000000..94aefb188e --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/LinkedList.java @@ -0,0 +1,227 @@ +package Collection; + +import com.coding.basic.List; +import com.coding.basic.Iterator; + +public class LinkedList implements List { + + public Node head; + public int size; + + public LinkedList() { + head = new Node(); + size = 0; + } + + public LinkedList(Object o) { + head = new Node(o); + size = 1; + } + + public void add(Object o) { + if (size == 0) { + addfirst(o); + return; + } + addlast(o); + } + + public void add(int index, Object o) { + this.checkCapacity(index); + if (index == 0) { + addfirst(o); + return; + } + if (index == size) { + addlast(o); + return; + } + addmid(index, o); + } + + public void checkCapacity(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + public void addfirst(Object o) { + Node tmp = new Node(head); + head.data = o; + head.next = tmp; + size++; + } + + public void addlast(Object o) { + Node tmp = new Node(head); + //Node last = new Node(o); + //last.data=o; + for (int i = 0; i < size-1; i++) { + tmp = tmp.next; + } + + tmp.next = new Node(o); + size++; + + } + + public void addmid(int index, Object o) { + Node tmp = new Node(head); + Node add = new Node(o); + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + add.next = tmp.next; + tmp.next = add; + size++; + } + + public Object get(int index) { + checkCapacity(index); + Node tmp = new Node(head); + if (index == 0) { + return head; + } + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + return tmp.next; + } + + public Object remove(int index) { + checkCapacity(index); + Node tmp = new Node(head); + if (index == 0) { + return removeFirst(); + } + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + Node result = new Node(tmp.next); + tmp.next = result.next; + return result; + } + + public int size() { + return this.size; + } + + public Object removeFirst() { + Node tmp = new Node(head); + head = head.next; + return tmp; + } + + public Object removeLast() { + if (size == 0) { + return null; + } + if (size == 1) { + Node tmp = new Node(head); + head = null; + return tmp; + } + Node tmp = new Node(head); + for (int i = 0; i < size - 2; i++) { + tmp = tmp.next; + } + Node result = new Node(tmp.next); + tmp.next = result.next; + return result; + } + + public String toString(){ + StringBuilder sb= new StringBuilder(); + sb.append("["); + Node tmp=new Node(head); + for(int i=0;ielementData.size()){ + throw new IndexOutOfBoundsException(); + } + } + public Object pop(){ + checkCapacity(); + Object o = elementData.remove(size-1); + size--; + return o; + } + + public Object peek() { + checkCapacity(); + Object o = elementData.get(size-1); + return o; + } + + public boolean isEmpty() { + if(size!=0){ + return true; + } + return false; + } + + public String toString(){ + return super.toString(); + } + + public int size() { + return size; + } +} diff --git a/group12/563253496/datastructure/src/Collection/TestArrayList.java b/group12/563253496/datastructure/src/Collection/TestArrayList.java new file mode 100644 index 0000000000..eef55fae6b --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/TestArrayList.java @@ -0,0 +1,33 @@ +package Collection; + +import java.lang.reflect.Array; + +/** + * Created by bdl19 on 2017/2/23. + */ +public class TestArrayList { + /*public static void main(String[] args){ + /* + ArrayList al= new ArrayList("test1"); + System.out.println(al); + al.add("test2"); + System.out.println(al); + al.add(1,2); + System.out.println(al); + System.out.println(al.get(2)); + System.out.println(al.get(1)); + System.out.println(al.get(0)); + // System.out.println(al.get(3)); + System.out.println(al.size()); + System.out.println(al.remove(2)); + System.out.println(al); + + ArrayList al =new ArrayList(); + al.add(3,2); + al.add(0,0); + System.out.println(al.size()); + System.out.println(al); + + } + **/ +} diff --git a/group12/563253496/datastructure/src/Collection/TestStack.java b/group12/563253496/datastructure/src/Collection/TestStack.java new file mode 100644 index 0000000000..4702e49079 --- /dev/null +++ b/group12/563253496/datastructure/src/Collection/TestStack.java @@ -0,0 +1,15 @@ +package Collection; + +/** + * Created by bdl19 on 2017/2/25. + */ +public class TestStack { + public static void main(String[] args) { + Stack s=new Stack(); + s.push("a"); + s.push("b"); + System.out.println(s.pop()); + System.out.println(s.pop()); + + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java b/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/ArrayList.java @@ -0,0 +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; + } + +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/BinaryTreeNode.java b/group12/563253496/datastructure/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/Iterator.java b/group12/563253496/datastructure/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java b/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/LinkedList.java @@ -0,0 +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; + + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/List.java b/group12/563253496/datastructure/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/563253496/datastructure/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/group12/563253496/datastructure/src/com/coding/basic/Queue.java b/group12/563253496/datastructure/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/Queue.java @@ -0,0 +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; + } +} diff --git a/group12/563253496/datastructure/src/com/coding/basic/Stack.java b/group12/563253496/datastructure/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group12/563253496/datastructure/src/com/coding/basic/Stack.java @@ -0,0 +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; + } +} diff --git a/group15/1500_369516660/src/com/arrayList/basic/List.java b/group15/1500_369516660/src/com/arrayList/basic/List.java new file mode 100644 index 0000000000..698e06eb42 --- /dev/null +++ b/group15/1500_369516660/src/com/arrayList/basic/List.java @@ -0,0 +1,16 @@ +package com.arrayList.basic; +/** + * ɾ鷽 + * @author Jodie + * + */ +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 String remove(Object o); + public int size(); + +} diff --git a/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java b/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java new file mode 100644 index 0000000000..d2f3af6329 --- /dev/null +++ b/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java @@ -0,0 +1,155 @@ +package com.arrayList.basic; + +import java.util.Arrays; + +/** + * + * @author Jodie + * + */ +public class SimpleArrayList implements List { + + private final static int default_num = 10;//ʱûжСĬΪ10 + private Object[] elementData; + private int size = 0;//ĴС,ʵĴС + + public SimpleArrayList(){ + this(default_num); + } + + public SimpleArrayList(int size) { + if(size<0){//ж϶ĴСǷС0 + throw new IllegalArgumentException(); + }else{ + elementData = new Object[size]; + } + } + +/** + * дӺķ1 + */ + @Override + public void add(Object o) { + //жǷҪ + ifSpaceEnougn(size+1); + elementData[size++]=o; + } + +/** + * дӺķ2 + */ + @Override + public void add(int index, Object o) { + checkIfOut(index);//ǷԽ + ifSpaceEnougn(size+1);//ǷҪ + System.arraycopy(elementData, index, elementData, index + 1, size-index);//indexԪؼԺԪһλ + } +/** + * õָ± + */ + @Override + public Object get(int index) { + checkIfOut(index); + return elementData[index]; + } +/** + * ɾָ± + */ + @Override + public Object remove(int index) { + Object value = get(index); + int numRemove = size - index - 1; + if(numRemove > 0){ + System.arraycopy(elementData, index+1, elementData, index, size - index);//ǰһλ + } + elementData[--size] = null; + return value; + } +/** + * ɾָ + */ + @Override + public String remove(Object o) { + if(contains(o)){ + remove(indexOf(o)); + return "ɾɹ"; + }else{ + return "ҪɾݲУɾʧ"; + } + } +/** + * жǷҪ + * @param size + */ + private void ifSpaceEnougn(int size) { + if(size>default_num){ + exlicitSpace(size); + } + if(size<0){//sizeInteger.MAX_VALUEʱΪ + throw new OutOfMemoryError(); + } + } +/** + * ݷ + * @param + */ + private void exlicitSpace(int size) { + final int max_arrayLength = Integer.MAX_VALUE-8; + int newLength = elementData.length*2;//һΪԭƵ + if(newLength - size<0){ + newLength = size; + } + if(newLength > max_arrayLength){//ݺĴСֵ + newLength = (size > max_arrayLength ? Integer.MAX_VALUE : max_arrayLength); + } + elementData = Arrays.copyOf(elementData, newLength); + } + +/** + * жǷԽ + * @param index + */ + private void checkIfOut(int index) { + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } + } + +/** + * ҵָ± + * @param o + * @return + */ +private int indexOf(Object o) { + if(o!=null){ + for(int i=0;i= 0; + } + + @Override + public int size() { + return size; + } + + +} diff --git a/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java b/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java new file mode 100644 index 0000000000..646c2ab656 --- /dev/null +++ b/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java @@ -0,0 +1,38 @@ +package com.arrayList.basic; + +public class SimpleLinkedList implements List { + + private int size;//ĴС + private Node head; + + @Override + public void add(Object o) { + + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int index) { + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public String remove(Object o) { + return null; + } + + @Override + public int size() { + return 0; + } + +} diff --git a/group16/1012075117/DataStructure219/.classpath b/group16/1012075117/DataStructure219/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group16/1012075117/DataStructure219/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1012075117/DataStructure219/.project b/group16/1012075117/DataStructure219/.project new file mode 100644 index 0000000000..567baae65f --- /dev/null +++ b/group16/1012075117/DataStructure219/.project @@ -0,0 +1,17 @@ + + + DataStructure219 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs b/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/1012075117/DataStructure219/.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/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java new file mode 100644 index 0000000000..a1d46a21d8 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java @@ -0,0 +1,94 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class ArrayList implements List { + + private int flag = -1; + private static final int DEFAULT_CAPACITY = 1; + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + @Override + public void add(Object element) { + // 当要添加数据的位置已经超过数组长度时,增长数组长度 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[flag + 1] = element; + flag++; + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > getFlag() + 1) { + System.out.println("在--" + index + "--添加的--" + element + "--无效,因为越界了!"); + return; + } + // 数组长度永远比已存数据大一个。 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[index] = element; + if (index > getFlag()) { + flag++; + } + } + + @Override + public Object get(int index) { + if (index < 0 || index > getFlag()) { + System.out.print("在--" + index + "--的get无效,因为越界了!"); + return null; + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > getFlag()) { + System.out.println("在--" + index + "--的remove无效,因为越界了!"); + return null; + } + Object oldValue = elementData[index]; + elementData[index] = null; + // 将删除处后面的数据往前移一格。 + Object[] data2 = new Object[elementData.length - 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag()); + elementData = data2; + flag--; + return oldValue; + } + + @Override + public int size() { + return getFlag() + 1; + } + + public int getFlag() { + return flag; + } + + private void grow() { + Object[] data2 = new Object[elementData.length + 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag() + 2);// 最后一个参数是需要复制的数据的数量。 + elementData = data2; + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + ArrayList al = new ArrayList(); + al.add(0, 99); + al.add(1, 100); + System.out.println(al.get(1)); + al.remove(1); + System.out.println(al.get(1)); + System.out.println(al.size()); + } +} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java new file mode 100644 index 0000000000..a1c728f0a1 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java @@ -0,0 +1,194 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class LinkedList implements List { + + private Node head = null; + private Node last = null; + private int size = 0; + + private static class Node { + Object item; + Node prev; + Node next; + + public Node(Node prev, Object item, Node next) { + this.prev = prev; + this.item = item; + this.next = next; + } + } + + @Override + public void add(Object element) { + addLast(element); + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > size) { + System.out.println("操作无效,越界了"); + return; + } + if (index == 0) { + addFirst(element); + return; + } + if (index == size) { + addLast(element); + return; + } + Node indexNode = node(index); + Node newNode = new Node(indexNode.prev, element, indexNode); + indexNode.prev.next = newNode; + indexNode.prev = newNode; + size++; + } + + @Override + public Object get(int index) { + if (index < 0 || index >= size) { + System.out.println("查询无效,越界了"); + return null; + } + if (index == 0) { + return head.item; + } + return node(index).item; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > size) { + System.out.println("是空的,无法删除"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node x = node(index); + final Object element = x.item; + final Node next = x.next; + final Node prev = x.prev; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.item = null; + size--; + return element; + } + + @Override + public int size() { + return size; + } + + private void addFirst(Object element) { + final Node f = head; + Node newNode = new Node(null, element, f); + head = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object element) { + if (head == null) { + addFirst(element); + } else { + Node newNode = new Node(last, element, null); + last.next = newNode; + last = newNode; + size++; + } + } + + public Object removeFirst() { + if (head == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + Node x = head; + Node next = head.next; + Object element = x.item; + x.item = null; + x.next = null; + head = next; + if (next == null) + last = null; + else + x.prev = null; + size--; + return element; + } + } + + public Object removeLast() { + if (last == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + final Node l = last; + final Object element = l.item; + final Node p = l.prev; + l.item = null; + l.prev = null; + last = p; + if (p == null) + head = null; + else + p.next = null; + size--; + return element; + } + } + + Node node(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.add(0, "xxx"); + ll.add(1, 111); + System.out.println(ll.size()); + System.out.println(ll.get(2)); + + } +} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java new file mode 100644 index 0000000000..5226796141 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java @@ -0,0 +1,13 @@ +package com.stackwei.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/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java new file mode 100644 index 0000000000..4a227495e9 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java @@ -0,0 +1,52 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class Queue { + + private LinkedList ll = new LinkedList(); + + /** + * 在队尾添加数据 + * @param element + */ + public void enQueue(Object element) { + ll.addLast(element); + } + + /** + * 删除队头数据 + * @return + */ + public Object deQueue() { + return ll.removeFirst(); + } + + /** + * 队列是否为空 + * @return + */ + public boolean isEmpty() { + if (ll.size() > 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Queue q = new Queue(); + q.enQueue(97); + q.enQueue(98); + q.enQueue(99); + System.out.println(q.isEmpty()); + System.out.println(q.deQueue()); + } + +} diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java new file mode 100644 index 0000000000..1b047ffafd --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java @@ -0,0 +1,59 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class Stack { + + private ArrayList al = new ArrayList(); + + /** + * 进栈 + * @param item + */ + public void push(Object item) { + al.add(item); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + return al.remove(al.getFlag()); + } + + /** + * 获取栈顶元素 + * @return + */ + public Object peek() { + return al.get(al.getFlag()); + } + + /** + * 栈是否为空 + * @return + */ + public boolean isEmpty() { + if (al.getFlag() >= 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Stack s = new Stack(); + s.push(98); + s.push(99); + s.pop(); + System.out.println(s.peek()); + } + +} diff --git a/group16/1154151360/.classpath b/group16/1154151360/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/1154151360/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1154151360/.gitignore b/group16/1154151360/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/1154151360/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/1154151360/.project b/group16/1154151360/.project new file mode 100644 index 0000000000..f88388f6d7 --- /dev/null +++ b/group16/1154151360/.project @@ -0,0 +1,17 @@ + + + DataStructure2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1154151360/.settings/org.eclipse.core.resources.prefs b/group16/1154151360/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group16/1154151360/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group16/1154151360/src/com/list/ArrayList.java b/group16/1154151360/src/com/list/ArrayList.java new file mode 100644 index 0000000000..733dc0f341 --- /dev/null +++ b/group16/1154151360/src/com/list/ArrayList.java @@ -0,0 +1,93 @@ +package com.list; + + +//ArrayList +public class ArrayList { + + private int size; + + private Object [] elementData = new Object[10]; + + public boolean add(Object data){ + + getRow(size+1); + elementData[size++] = data; + return true; + } + + public boolean add(int index, Object data){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("�±�Խ��"); + } + getRow(size + 1); + Object object = elementData [index]; + System.arraycopy(elementData, index,elementData , index + 1,size - index ); + elementData[index] = data; + size++; + return true; + } + + public Object get(int index){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("下标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("下标越界"); + } + Object object = elementData [index]; + System.arraycopy(elementData, index + 1 , elementData, index, size - 1 - index); + elementData[size--]= null; + return object; + } + + + private void getRow(int num){ + if (num > elementData.length ){ + int oldLength = elementData.length; + int newLength = ((num + elementData.length) * 3) >> 2; + Object [] oldelements = elementData; + elementData = new Object[newLength]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + public int size(){ + return size; + } + + public int length(){ + return elementData.length; + } + public static void main(String[] args) { + ArrayList list = new ArrayList(); + + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add("E"); + list.add("F"); + list.add("G"); + list.add("H"); + list.add("I"); + list.add("J"); + list.add("K"); + list.add("L"); + list.add(2, 1); + System.out.println("elementsData.Length: "+list.length()); + System.out.println("elementsData.size: "+list.size()); + for (int i = 0; i < list.size; i++){ + System.out.print(list.get(i)+ " "); + } + System.out.println(" "); + list.remove(2); + + for (int i = 0; i < list.size; i++){ + System.out.print(list.get(i)+ " "); + } + } +} diff --git a/group16/1154151360/src/com/list/LinkedList.java b/group16/1154151360/src/com/list/LinkedList.java new file mode 100644 index 0000000000..46f61fb667 --- /dev/null +++ b/group16/1154151360/src/com/list/LinkedList.java @@ -0,0 +1,158 @@ +package com.list; + +public class LinkedList { + + private int size; + + private Node head;//头节点 + + Node current; //当前节点 + + public LinkedList(){ + this.head = current = new Node(null); + this.size = 0; + } + + + private boolean add(Object object){ + addAfter(object); + size++; + return true; + } + + + private boolean add(int index,Object object) throws Exception{ + index(index - 1); + current.nextNode = new Node(object,current.nextNode.nextNode); + size++; + return true; + } + + + private boolean addFirst(Object object){ + Node node = new Node(object,null); + current = head.nextNode; + head.nextNode = node; + node.nextNode = current; + size++; + return true; + } + + + private boolean addLast(Object object){ + add(object); + return true; + } + + + private Object get(int index) throws Exception{ + index(index); + return current.object; + } + + + private Object remove(int index) throws Exception{ + + if (index == size - 1){ + Object object = removeLast(); + return object; + } + index(index - 1); + Object object = current.nextNode.object; + + current.nextNode = current.nextNode.nextNode; + size--; + return object; + } + + private Object removeFirst(){ + Object object = null; + if (size > 0){ + current = head.nextNode; + object = current.object; + head.nextNode = head.nextNode.nextNode; + size--; + } + return object; + } + + private Object removeLast() throws Exception{ + Object object = null; + if (size > 0){ + int j = 0; + current = head.nextNode; + + while (current != null){ + current = current.nextNode; + j++; + } + index(j - 1); + object = current.nextNode.object; + current.nextNode = null; + size--; + } + return object; + } + private void index (int index) throws Exception{ + + if (index < -1 || index > size){ + + throw new Exception(" "); + } + + if (index == -1){ + return; + } + current = head.nextNode; + int j = 0; + while (current != null && j < index){ + current = current.nextNode; + j++; + } + } + + + private void addAfter(Object object){ + + if (head.nextNode == null){ + + Node newNode = new Node(object,null); + }else{ + current = head.nextNode; + while (current.nextNode == null){ + current = current.nextNode; + } + current.setNode(new Node(object,null)); + } + + + } + + + + + private static class Node{ + + Object object; + + Node nextNode; + + + Node (Node nextNode){ + this.nextNode = nextNode; + } + + + Node (Object object, Node nextNode){ + this.nextNode = nextNode; + this.object = object; + } + + private void setNode(Node node){ + this.nextNode = node; + } + + } + + +} diff --git a/group16/1154151360/src/com/list/Queue.java b/group16/1154151360/src/com/list/Queue.java new file mode 100644 index 0000000000..faa3e87381 --- /dev/null +++ b/group16/1154151360/src/com/list/Queue.java @@ -0,0 +1,51 @@ +package com.list; +//队列 +public class Queue { + + Object [] element; + + private static int DEFAULT_SIZE = 10; + + int front;//头指针 + + int rear;//尾指针 + + public Queue(){ + this(DEFAULT_SIZE); + } + public Queue(int size){ + element = new Object[size]; + this.front = 0; + this.rear = 0; + } + + public boolean enQueue(Object object){ + + if ((rear + 1) % element.length == front){ + return false; + }else{ + element[rear] = object; + rear = (rear + 1) % element.length; + return true; + } + } + + public Object deQueue(){ + if (front == rear){ + return null; + }else{ + Object object = element[front]; + front = (front + 1) % element.length; + return object; + } + + } + + public int size(){ + return (rear -front) & (element.length - 1); + } + + public boolean isEmpty(){ + return rear == front; + } +} diff --git a/group16/1154151360/src/com/list/Stack.java b/group16/1154151360/src/com/list/Stack.java new file mode 100644 index 0000000000..f92a9e731c --- /dev/null +++ b/group16/1154151360/src/com/list/Stack.java @@ -0,0 +1,35 @@ +package com.list; + +import java.util.ArrayList; +import java.util.EmptyStackException; + +public class Stack { + + ArrayList elelmentData = new ArrayList(); + + //压入栈 + public void push(Object object){ + elelmentData.add(object); + } + + //弹出栈 + public Object pop(){ + if (isEmpty()){ throw new EmptyStackException();} + return elelmentData.remove(elelmentData.size() - 1); + } + + //取栈顶元素 + public Object peek(){ + if (isEmpty()){return null;} + return elelmentData.get(elelmentData.size() - 1); + } + + public boolean isEmpty(){ + return elelmentData.isEmpty(); + } + + public int size(){ + if (isEmpty()){throw new EmptyStackException();} + return elelmentData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e287419dc0 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,63 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ǰ鳤 + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ǰ鳤ȵ1.5 + if (newCapacity - num < 0) { + newCapacity = num; // Dz,ֱΪֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // ±Խж + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group16/1287642108/0226/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/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..99c92fb9f1 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,101 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //ȡڵ + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..95dee3d81b --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..c0b7da89f8 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1325756593/.classpath b/group16/1325756593/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group16/1325756593/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/1325756593/.gitignore b/group16/1325756593/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/1325756593/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/1325756593/.project b/group16/1325756593/.project new file mode 100644 index 0000000000..e3d07b6899 --- /dev/null +++ b/group16/1325756593/.project @@ -0,0 +1,17 @@ + + + DongqiHomeWork + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1325756593/dongqihust.readme b/group16/1325756593/dongqihust.readme new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group16/1325756593/src/com/dong/week1/ArrayList.java b/group16/1325756593/src/com/dong/week1/ArrayList.java new file mode 100644 index 0000000000..a700aecfb5 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/ArrayList.java @@ -0,0 +1,109 @@ +package com.dong.week1; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + /** + * жǷѾˣѾݴ + */ + if(elementData.length==size){ + elementData = Arrays.copyOf(elementData, elementData.length*2+1); + } + elementData[size++]=o; + + } + public void add(int index, Object o){ + if(index >size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("Խ,ǰ鳤"+size+",Ԫص:"+index); + } + /** + * Ѿˣ + */ + if(size==elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length*2+1); + } + Object[] elementDataClone = elementData.clone(); + System.arraycopy(elementData, index, elementDataClone, index+1, size-index); + elementDataClone[index++]=o; + size++; + elementData = elementDataClone; + } + + + public Object get(int index){ + if(index >=size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("Խ,ǰ鳤"+size+",Ԫص:"+index); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >=size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("Խ,ǰ鳤"+size+",ɾԪص:"+index); + } + elementData[index]=null; + size--; + Object[] elementDataClone = elementData.clone(); + System.arraycopy(elementData, index+1, elementDataClone, index, size-index-1); + elementData = elementDataClone; + return elementData[index]; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new IteratorArrayList(this); + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + + private class IteratorArrayList implements Iterator{ + + private ArrayList arrayList; + private int index=0; + + + public IteratorArrayList(ArrayList arrayList) { + super(); + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.arrayList.size() >index; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(hasNext()){ + return this.arrayList.get(index++); + } + return null; + } + + } + + public static void main(String[] args) { + ArrayList arrayList= new ArrayList(); + + Iterator iterator= arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + +} diff --git a/group16/1325756593/src/com/dong/week1/ArrayListTest.java b/group16/1325756593/src/com/dong/week1/ArrayListTest.java new file mode 100644 index 0000000000..128130cfb2 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/ArrayListTest.java @@ -0,0 +1,63 @@ +package com.dong.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + //@Test + public void testAddObject() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=200;i++){ + arrayList.add(i); + } + System.out.println(arrayList); + } + + //@Test + public void testAddIntObject() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=2;i++){ + arrayList.add(i); + } + arrayList.add(1,100); + arrayList.add(1, 1000); + System.out.println(arrayList); + } + +// @Test + public void testGet() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=200;i++){ + arrayList.add(i); + } + //System.out.println(arrayList.get(-1)); + //System.out.println(arrayList.get(50)); + System.out.println(arrayList.get(200)); + //System.out.println(arrayList.get(300)); + + + } + + @Test + public void testRemove() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=10;i++){ + arrayList.add(i); + } + arrayList.remove(1); + arrayList.remove(1); + System.out.println(arrayList); + } + +// @Test + public void testSize() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=10;i++){ + arrayList.add(i); + } + System.out.println(arrayList.size()); + } + +} diff --git a/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java b/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..ef41ced8df --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java @@ -0,0 +1,39 @@ +package com.dong.week1; + +public class BinaryTreeNode { + private TreeNode node; + + private static class TreeNode{ + private int key=0; + private TreeNode leftChild=null; + private TreeNode rightChild=null; + + public TreeNode(){} + + /** + * @param key + * @param data + */ + public TreeNode(int key){ + this.key=key; + this.leftChild=null; + this.rightChild=null; + } + + + } + + + + public TreeNode insert(TreeNode o){ + if(node == null){ + return o; + } + if(node.key > o.key){ + return insert(o.leftChild); + }else{ + return insert(node.leftChild); + } + } + +} diff --git a/group16/1325756593/src/com/dong/week1/Iterator.java b/group16/1325756593/src/com/dong/week1/Iterator.java new file mode 100644 index 0000000000..9147ab8264 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Iterator.java @@ -0,0 +1,7 @@ +package com.dong.week1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/1325756593/src/com/dong/week1/LinkedList.java b/group16/1325756593/src/com/dong/week1/LinkedList.java new file mode 100644 index 0000000000..339b5862ee --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/LinkedList.java @@ -0,0 +1,208 @@ +package com.dong.week1; + +public class LinkedList implements List { + + private int size = 0; + private Node head; + private Node tail; + + + public void add(Object o){ + //һԪص߼dz򵥣ֻҪжheadǷΪաΪգֱӼӼ + Node node = new Node(o,null); + if(head ==null){ + head =node; + }else{ + tail.next=node; + } + tail=node; + size++; + + } + public void add(int index , Object o){ + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("indexΪ"); + } + if(index > size){ + throw new ArrayIndexOutOfBoundsException("ǰlistΪ"+size+",ȡǣ"+index); + } + if(size==0){ + head=new Node(o, null); + tail=head; + return; + } + if(index==0){ + Node curNode =head; + Node newNode =new Node(o, curNode); + head=newNode; + return; + } + Node curNode =head; + Object retVal = null; + for(int i=0;i= size){ + throw new ArrayIndexOutOfBoundsException("ǰlistΪ"+size+",Ƴǣ"+index); + } + Node curNode = head; + for(int i=0;i= size){ + throw new ArrayIndexOutOfBoundsException("ǰlistΪ"+size+",ȡǣ"+index); + } + Object retVal = null; + if(index==0){ + retVal =head.data; + head=head.next; + return retVal; + } + Node curNode =head; + + for(int i=0;iindex; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(hasNext()){ + return this.list.get(index++); + } + return null; + } +} + public static void main(String[] args) { + + LinkedList arrayList= new LinkedList(); + + Iterator iterator= arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + +} + +} diff --git a/group16/1325756593/src/com/dong/week1/List.java b/group16/1325756593/src/com/dong/week1/List.java new file mode 100644 index 0000000000..adc694241a --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/List.java @@ -0,0 +1,9 @@ +package com.dong.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/group16/1325756593/src/com/dong/week1/Queue.java b/group16/1325756593/src/com/dong/week1/Queue.java new file mode 100644 index 0000000000..445390a168 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Queue.java @@ -0,0 +1,24 @@ +package com.dong.week1; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(elementData.size(), o); + } + + public Object deQueue(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("Ϊ"); + } + return elementData.remove(0); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1325756593/src/com/dong/week1/Stack.java b/group16/1325756593/src/com/dong/week1/Stack.java new file mode 100644 index 0000000000..1dea6cdfd9 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Stack.java @@ -0,0 +1,31 @@ +package com.dong.week1; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + + public Object pop(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("ջΪ"); + } + return elementData.remove(elementData.size()-1); + + } + + public Object peek(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("ջΪ"); + } + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1924332561/.classpath b/group16/1924332561/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/1924332561/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1924332561/.project b/group16/1924332561/.project new file mode 100644 index 0000000000..5f960262bc --- /dev/null +++ b/group16/1924332561/.project @@ -0,0 +1,17 @@ + + + 1924332561Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1924332561/.settings/org.eclipse.core.resources.prefs b/group16/1924332561/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group16/1924332561/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group16/1924332561/src/com/coding/basic/ArrayList.java b/group16/1924332561/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3fc0960e08 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/ArrayList.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class ArrayList implements List{ + private int size = 0; + + private Object[] elementDate = new Object[100]; + + + @Override + public void add(Object o) { + + this.elementDate[this.size]=o; + this.size++; + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int dex) { + + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java b/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e7d9e43e24 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class BinaryTreeNode { + private Object date; + private BinaryTreeNode left; + private BinaryTreeNode right; + public Object getDate() { + return date; + } + public void setDate(Object date) { + this.date = date; + } + 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/group16/1924332561/src/com/coding/basic/Iterator.java b/group16/1924332561/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group16/1924332561/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/group16/1924332561/src/com/coding/basic/LinkedList.java b/group16/1924332561/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d39557be29 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/LinkedList.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private static class Node{ + Object date; + Node next; + } + + private Node head; + + + @Override + public void add(Object o) { + + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int dex) { + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + + return 0; + } + + 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; + } + +} diff --git a/group16/1924332561/src/com/coding/basic/List.java b/group16/1924332561/src/com/coding/basic/List.java new file mode 100644 index 0000000000..d48b1f4827 --- /dev/null +++ b/group16/1924332561/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/group16/1924332561/src/com/coding/basic/Queue.java b/group16/1924332561/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..7dc7b4820a --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(){ + + + } + public Object deQueue(){ + + return null; + } + + public boolean isEmpty(){ + + return false; + } + + public int size(){ + + return -1; + } +} diff --git a/group16/1924332561/src/com/coding/basic/Stack.java b/group16/1924332561/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7a49c48279 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementDate = 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/group16/214074094/readme.txt b/group16/214074094/readme.txt new file mode 100644 index 0000000000..c1b06ddcc2 --- /dev/null +++ b/group16/214074094/readme.txt @@ -0,0 +1 @@ +I am 北京-Shane diff --git a/group16/214074094/src/com/coding/basic/ArrayList.java b/group16/214074094/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..158c866d45 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/ArrayList.java @@ -0,0 +1,165 @@ +package coding.basic; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * @Author shane + * @Time 2017/2/25 13:06 + * @Email stevenchenguang@gmail.com + * @Desc OwnArrayList + */ +public class ArrayList implements List { + + private int size = 0; + + private final static Object[] EMPTY_ELEMENTDATA = {}; + + /** + * 默认容量 + */ + private static int DEFAULT_CAPACITY = 10; + + private Object[] elementData; + + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + @Override + public void add(Object o) { + if (elementData == EMPTY_ELEMENTDATA) { + elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY); + elementData[0] = o; + } else if (size < elementData.length) { + elementData[size] = o; + } else { + _grow(); + elementData[size] = o; + } + size++; + _analyze(); + } + + @Override + public void add(int index, Object o) { + if (index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + if (elementData == EMPTY_ELEMENTDATA) { + if (index != 0) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } else { + elementData = new Object[DEFAULT_CAPACITY]; + elementData[0] = o; + } + } else if (index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } else if (index == size) { + _grow(); + elementData[size] = o; + size++; + } else { + if (elementData.length == size) { + _grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + _analyze(); + } + + @Override + public Object get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + Object oldValue = elementData[index]; + //需要复制的长度 + int needMoveLength = size - index - 1; + //如果该长度小于0, 说明只有一个元素, 直接置空即可 + if (needMoveLength > 0) { + System.arraycopy(elementData, index + 1, elementData, index, needMoveLength); + } + elementData[--size] = null; + _analyze(); + return oldValue; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListItrator(); + } + + /** + * @Author: shane + * @Time: 2017/2/25 20:18 + * @Email: stevenchenguang@gmail.com + * @param: + * @Return: + * @Throw: + * @Desc: 返回真实长度的数组数据 + */ + private void _analyze() { + if (size < elementData.length) { + elementData = Arrays.copyOf(elementData, size); + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 20:19 + * @Email: stevenchenguang@gmail.com + * @param: + * @Return: + * @Throw: + * @Desc: 将数组的长度扩容至2倍 + */ + private void _grow() { + elementData = Arrays.copyOf(elementData, elementData.length << 1); + } + + @Override + public String toString() { + return Arrays.toString(elementData); + } + + public boolean isEmpty() { + return size == 0; + } + + private class ArrayListItrator implements Iterator { + + private int position = 0; + + @Override + public boolean hasNext() { + return position != size; + } + + @Override + public Object next() { + int i = position; + if (i >= size) { + throw new NoSuchElementException(); + } + position = i + 1; + return elementData[i]; + } + } +} diff --git a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b40066ebe1 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,39 @@ +package 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/group16/214074094/src/com/coding/basic/Iterator.java b/group16/214074094/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..1acc5349a4 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package coding.basic; + +public interface Iterator { + + boolean hasNext(); + + Object next(); + +} diff --git a/group16/214074094/src/com/coding/basic/LinkedList.java b/group16/214074094/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9108c2b6fe --- /dev/null +++ b/group16/214074094/src/com/coding/basic/LinkedList.java @@ -0,0 +1,222 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/25 21:01 + * @Email stevenchenguang@gmail.com + * @Desc OwnLinkedList + */ +public class LinkedList implements List { + + private int size = 0; + + private Node first; + + private Node last; + + public void add(Object o) { + if (size == 0) { + first = new Node(null, o, null); + last = first; + size++; + } else { + addLast(o); + } + } + + public void add(int index, Object o) { + _checkIndex(index); + if (index == size - 1) { + addLast(o); + } else { + Node prev = _node(index); + Node next = _node(index + 1); + Node newNode = new Node(prev, o, next); + prev.next = newNode; + next.prev = newNode; + size++; + } + } + + public Object get(int index) { + _checkIndex(index); + return node(index); + } + + public Object remove(int index) { + _checkIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } + Node curr = _node(index); + Object data = curr.data; + final Node prev = curr.prev; + final Node next = curr.next; + + prev.next = next; + next.prev = prev; + curr = null; + size--; + + return data; + } + + private Object removeFirst() { + Node oldFirst = first; + Object data = first.data; + final Node oldSecond = oldFirst.next; + if (null == oldSecond) { + first = null; + last = null; + } else { + oldSecond.prev = null; + first = oldSecond; + oldFirst = null; + } + size--; + return data; + } + + private Object removeLast() { + Node oldLast = last; + Object data = last.data; + final Node oldLastButOne = last.prev; + if (null == oldLastButOne) { + first = null; + last = null; + } else { + oldLastButOne.next = null; + last = oldLastButOne; + oldLast = null; + } + size--; + return data; + } + + public void addFirst(Object o) { + final Node oldFirst = first; + final Node param = new Node(null, o, null); + if (null == oldFirst) { + first = param; + } else { + oldFirst.prev = param; + param.next = oldFirst; + first = param; + } + size++; + } + + public void addLast(Object o) { + final Node n = last; + final Node newNode = new Node(n, o, null); + last = newNode; + n.next = newNode; + size++; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + public boolean isEmpty() { + return size == 0; + } + + private static class Node { + Node prev; + Object data; + Node next; + + public Node(Node prev, Object data, Node next) { + this.prev = prev; + this.data = data; + this.next = next; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:44 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: Node + * @Throw: + * @Desc: 根据下标获取节点元素上的数据 + */ + private Object node(int index) { + //如果下标在左一半, 从左往右取 + if (index < size >> 1) { + Node tmp = first; + for (int i = 0; i < index; i++) { + tmp = tmp.next; + } + return tmp.data; + } else { + Node tmp = last; + for (int i = size - 1; i > index; i--) { + tmp = tmp.prev; + } + return tmp.data; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:44 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: Node + * @Throw: + * @Desc: 根据下标获取节点元素 + */ + private Node _node(int index) { + //如果下标在左一半, 从左往右取 + if (index < size >> 1) { + Node tmp = first; + for (int i = 0; i < index; i++) { + tmp = tmp.next; + } + return tmp; + } else { + Node tmp = last; + for (int i = size - 1; i > index; i--) { + tmp = tmp.prev; + } + return tmp; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:43 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: + * @Throw: IndexOutOfBoundsException + * @Desc: 校验下标是否合法 + */ + private void _checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public String toString() { + if (0 == size) { + return "[]"; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < this.size; i++) { + sb.append(get(i)).append(", "); + } + String tmp = sb.substring(0, sb.length() - 2); + return "[" + tmp + "]"; + } +} \ No newline at end of file diff --git a/group16/214074094/src/com/coding/basic/List.java b/group16/214074094/src/com/coding/basic/List.java new file mode 100644 index 0000000000..5da9b0d4c6 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/List.java @@ -0,0 +1,15 @@ +package coding.basic; + +public interface List { + + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); + +} diff --git a/group16/214074094/src/com/coding/basic/Queue.java b/group16/214074094/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..869d0f7333 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Queue.java @@ -0,0 +1,36 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 17:19 + * @Email stevenchenguang@gmail.com + * @Desc Own Queue + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + return elementData.remove(0); + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group16/214074094/src/com/coding/basic/Stack.java b/group16/214074094/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7ef1c9ad06 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 16:55 + * @Email stevenchenguang@gmail.com + * @Desc Own Stack + */ +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.isEmpty(); + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group16/214074094/src/com/reading/blog_test.txt b/group16/214074094/src/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/214074094/src/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/214074094/src/test/coding/basic/AbstractTest.java b/group16/214074094/src/test/coding/basic/AbstractTest.java new file mode 100644 index 0000000000..80eaaa6fe5 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/AbstractTest.java @@ -0,0 +1,19 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/25 13:06 + * @Email stevenchenguang@gmail.com + * @Desc 测试基类 + */ +public class AbstractTest { + + protected void printStar() { + System.out.println("********************************************"); + } + + protected void printHyphen() { + System.out.println("--------------------------------------------"); + } + +} diff --git a/group16/214074094/src/test/coding/basic/ArrayListTest.java b/group16/214074094/src/test/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..2f03342d61 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/ArrayListTest.java @@ -0,0 +1,81 @@ +package coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/25 13:02 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ + +public class ArrayListTest extends AbstractTest { + + private static ArrayList list; + + @Before + public void before() { + + list = new ArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + list.add("e"); + + printStar(); + System.out.println("Before Test data :" + list); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + list); + printStar(); + } + + @Test + public void testAddI() { + int index = list.size(); + list.add(index, "test add i"); + Assert.assertEquals(list.get(index), "test add i"); + } + + @Test + public void test() { + java.util.ArrayList list = new java.util.ArrayList(); + list.add("a"); + list.add("b"); + java.util.Iterator it = list.iterator(); + while (it.hasNext()) { + + } + System.out.println(it.next()); + System.out.println(it.next()); + System.out.println(it.next()); + } + + @Test + public void testSize() { + Assert.assertEquals(5, list.size()); + } + + @Test + public void testRemove() { + list.remove(5); + Assert.assertEquals(list.get(3), "d"); + } + + @Test + public void testIterator() { + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + +} diff --git a/group16/214074094/src/test/coding/basic/LinkedListTest.java b/group16/214074094/src/test/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..bc78728a25 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/LinkedListTest.java @@ -0,0 +1,63 @@ +package coding.basic; + +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/25 23:32 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class LinkedListTest extends AbstractTest { + + private static LinkedList list; + + @Before + public void before() { + list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + list.add("e"); + + printStar(); + System.out.println("Before Test data :" + list); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + list); + printStar(); + } + + @Test + public void testAddIndex() { + list.add(0, "after a"); + Assert.assertEquals("after a", list.get(1)); + + list.add(3, "after c"); + Assert.assertEquals("after c", list.get(4)); + + list.add(6, "after e"); + Assert.assertEquals("after e", list.get(7)); + } + + @Test + public void testRemove() { + list.remove(0); + Assert.assertEquals("b", list.get(0)); + + list.remove(list.size() - 1); + Assert.assertEquals("d", list.get(list.size() - 1)); + + Object obj = list.remove(1); + Assert.assertEquals("c", obj); + Assert.assertEquals(2, list.size()); + } +} diff --git a/group16/214074094/src/test/coding/basic/QueueTest.java b/group16/214074094/src/test/coding/basic/QueueTest.java new file mode 100644 index 0000000000..12302783b3 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/QueueTest.java @@ -0,0 +1,62 @@ +package coding.basic; + +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/26 17:24 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class QueueTest extends AbstractTest { + + private static Queue queue; + + @Before + public void before() { + queue = new Queue(); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + printStar(); + System.out.println("Before Test data :" + queue); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + queue); + printStar(); + } + + @Test + public void testDeQueueAndIsEmpty() { + Assert.assertEquals("a", queue.deQueue()); + + queue.deQueue(); + queue.deQueue(); + queue.deQueue(); + queue.deQueue(); + + Assert.assertEquals(true, queue.isEmpty()); + + try { + queue.deQueue(); + } catch (RuntimeException e) { + Assert.assertEquals("Queue is empty", e.getMessage()); + } + } + + @Test + public void testSize() { + Assert.assertEquals(5, queue.size()); + } +} diff --git a/group16/214074094/src/test/coding/basic/StackTest.java b/group16/214074094/src/test/coding/basic/StackTest.java new file mode 100644 index 0000000000..f289744a67 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/StackTest.java @@ -0,0 +1,68 @@ +package coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/26 16:58 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class StackTest extends AbstractTest { + + private static Stack stack; + + @Before + public void before() { + stack = new Stack(); + + stack.push("a"); + stack.push("b"); + stack.push("c"); + stack.push("d"); + stack.push("e"); + + printStar(); + System.out.println("Before Test data :" + stack); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + stack); + printStar(); + } + + @Test + public void testPop() { + Assert.assertEquals("e", stack.pop()); + } + + @Test + public void testPeek() { + Assert.assertEquals("e", stack.peek()); + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(false, stack.isEmpty()); + + stack.pop(); + stack.pop(); + stack.pop(); + stack.pop(); + stack.pop(); + + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(5, stack.size()); + } + +} diff --git a/group16/214074094/target/production/214074094/reading/blog_test.txt b/group16/214074094/target/production/214074094/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/214074094/target/production/214074094/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/2562124714/.idea/dictionaries/zhangwj.xml b/group16/2562124714/.idea/dictionaries/zhangwj.xml new file mode 100644 index 0000000000..d07fd80ae1 --- /dev/null +++ b/group16/2562124714/.idea/dictionaries/zhangwj.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/misc.xml b/group16/2562124714/.idea/misc.xml new file mode 100644 index 0000000000..e97ef03f44 --- /dev/null +++ b/group16/2562124714/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/modules.xml b/group16/2562124714/.idea/modules.xml new file mode 100644 index 0000000000..c3fdba38f0 --- /dev/null +++ b/group16/2562124714/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/workspace.xml b/group16/2562124714/.idea/workspace.xml new file mode 100644 index 0000000000..d357c0f9a1 --- /dev/null +++ b/group16/2562124714/.idea/workspace.xml @@ -0,0 +1,780 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + binaryTree.PriOder + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487640652721 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/2562124714.iml b/group16/2562124714/2562124714.iml new file mode 100644 index 0000000000..3a8ffcf1f5 --- /dev/null +++ b/group16/2562124714/2562124714.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/src/Test/ArrayListTest.java b/group16/2562124714/src/Test/ArrayListTest.java new file mode 100644 index 0000000000..9bd8f2aeca --- /dev/null +++ b/group16/2562124714/src/Test/ArrayListTest.java @@ -0,0 +1,70 @@ +package Test; + +import com.coding.basic.ArrayList; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class ArrayListTest { + private static ArrayList arraylist = new ArrayList(); + @BeforeClass + public static void setUp() throws Exception { + + System.out.println("初始化变量"); + for (Integer i = 0; i < 5; i++) + { + arraylist.add(i); + } + + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void add() throws Exception { + Integer i = arraylist.size(); + Integer AddElement = 999; + arraylist.add(AddElement); + assertEquals(i + 1, arraylist.size()); + assertEquals(AddElement, arraylist.get(arraylist.size())); + arraylist.remove(arraylist.size()); + } + + @Test + public void add1() throws Exception { + Integer AddElement = 999; + arraylist.add(1, AddElement); + assertEquals(AddElement, arraylist.get(1)); + arraylist.remove(1); + } + + @Test + public void get() throws Exception { + assertEquals(null, arraylist.get(9999)); + } + + @Test + public void remove() throws Exception { + Integer i = (Integer)arraylist.get(1); + assertEquals(i, arraylist.remove(1)); + arraylist.add(1, i); + + } + + @Test + public void size() throws Exception { + assertEquals(5, arraylist.size()); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/BinaryTreeNodeTest.java b/group16/2562124714/src/Test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..7d8f4fdd01 --- /dev/null +++ b/group16/2562124714/src/Test/BinaryTreeNodeTest.java @@ -0,0 +1,100 @@ +package Test; + +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.TreeData; +import com.sun.org.apache.bcel.internal.generic.NEW; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class BinaryTreeNodeTest { + private BinaryTreeNode binaryTree = new BinaryTreeNode(); + @Before + public void setUp() throws Exception { + //System.out.println("初始化二叉树,5, 4, 7"); + TreeData element = new TreeData(); + element.setT((Integer)5); + binaryTree.insert(element); + TreeData element2 = new TreeData(); + element2.setT((Integer)4); + binaryTree.insert(element2); + TreeData element3 = new TreeData(); + element3.setT((Integer)7); + binaryTree.insert(element3); +// binaryTree.PriOder(this.binaryTree); + } + + @Test + public void getData() throws Exception { + assertEquals(5, binaryTree.getData().getT()); + + } + + @Test + public void setData() throws Exception { + TreeData element = new TreeData(); + element.setT(6); + binaryTree.setData(element); + assertEquals(6, binaryTree.getData().getT()); +// binaryTree.PriOder(this.binaryTree); + + } + + @Test + public void getLeft() throws Exception { + assertEquals(4, binaryTree.getLeft().getData().getT()); + + } + + @Test + public void setLeft() throws Exception { + TreeData element = new TreeData(); + element.setT(2); + BinaryTreeNode NewTreeNode = new BinaryTreeNode(); + NewTreeNode.setData(element); + binaryTree.setLeft(NewTreeNode); + assertEquals(2, binaryTree.getLeft().getData().getT()); +// binaryTree.PriOder(this.binaryTree); + } + + @Test + public void getRight() throws Exception { + assertEquals(7, binaryTree.getRight().getData().getT()); + + } + + @Test + public void setRight() throws Exception { + TreeData element = new TreeData(); + element.setT(9); + BinaryTreeNode NewTreeNode = new BinaryTreeNode(); + NewTreeNode.setData(element); + binaryTree.setRight(NewTreeNode); + assertEquals(9, binaryTree.getRight().getData().getT()); + } + + @Test + public void priOder() throws Exception { + + + } + + @Test + public void insert() throws Exception { + TreeData element = new TreeData(); + element.setT(2); + binaryTree.insert(element); + binaryTree.PriOder(this.binaryTree); + element.setT(9); + binaryTree.insert(element); + binaryTree.PriOder(this.binaryTree); + element.setT(8); +// binaryTree.PriOder(this.binaryTree); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/LinkedListTest.java b/group16/2562124714/src/Test/LinkedListTest.java new file mode 100644 index 0000000000..276b389f15 --- /dev/null +++ b/group16/2562124714/src/Test/LinkedListTest.java @@ -0,0 +1,81 @@ +package Test; + +import com.coding.basic.LinkedList; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class LinkedListTest { + private LinkedList linkedlist = new LinkedList(); + + @Before + public void Init() + { + System.out.println("初始化"); + linkedlist.add(9.9); + linkedlist.add(9.99); + } + + + @Test + public void add() throws Exception { + linkedlist.add(8.8); + assertEquals(3, linkedlist.size()); + System.out.println("after add size is " + linkedlist.size()); + System.out.println("after add last element is " + linkedlist.get(linkedlist.size())); + + } + + @Test + public void add1() throws Exception { + linkedlist.add(2, 7.7); + assertEquals(3, linkedlist.size()); + System.out.println("after add in 2th size is " + linkedlist.size()); + System.out.println("after add 2th element is " + linkedlist.get(2)); + } + + @Test + public void get() throws Exception { + assertEquals(9.9, linkedlist.get(1)); + } + + @Test + public void remove() throws Exception { + assertEquals(9.9, linkedlist.remove(1)); + } + + @Test + public void size() throws Exception { + assertEquals(2, linkedlist.size()); + + } + + @Test + public void addFirst() throws Exception { + linkedlist.addFirst(3.3); + assertEquals(3.3, linkedlist.get(1)); +// System.out.println(); + } + + @Test + public void addLast() throws Exception { + linkedlist.addLast(3.3); + assertEquals(3.3, linkedlist.get(linkedlist.size())); + + } + + @Test + public void removeFirst() throws Exception { + assertEquals(9.9, linkedlist.removeFirst()); + } + + @Test + public void removeLast() throws Exception { + assertEquals(9.99, linkedlist.removeLast()); + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/QueueTest.java b/group16/2562124714/src/Test/QueueTest.java new file mode 100644 index 0000000000..3f0557f262 --- /dev/null +++ b/group16/2562124714/src/Test/QueueTest.java @@ -0,0 +1,58 @@ +package Test; + +import com.coding.basic.Queue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class QueueTest { + private Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + System.out.println("初始化队列,元素为a,b,c,d"); + + String[] s = {"a", "b","c","d"}; + for (String a:s + ) { + queue.enQueue(a); + } + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("dasdas"); + assertEquals(5, queue.size()); +// assertEquals("dasdas", queue.); + } + + @Test + public void deQueue() throws Exception { + assertEquals("a",queue.deQueue()); + assertEquals(3, queue.size()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(false, queue.isEmpty()); + + } + + @Test + public void size() throws Exception { + assertEquals(4, queue.size()); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/StackTest.java b/group16/2562124714/src/Test/StackTest.java new file mode 100644 index 0000000000..0a36d4dc0f --- /dev/null +++ b/group16/2562124714/src/Test/StackTest.java @@ -0,0 +1,57 @@ +package Test; + +import com.coding.basic.Stack; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class StackTest { + private Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + System.out.println("初始化栈,元素为a,b,c,d"); + + String[] s = {"a", "b","c","d"}; + for (String a:s + ) { + stack.push(a); + } + + } + + @Test + public void push() throws Exception { + stack.push("aaa"); + assertEquals(5, stack.size()); + assertEquals("aaa", stack.peek()); + + } + + @Test + public void pop() throws Exception { + assertEquals("d", stack.pop()); + assertEquals(3, stack.size()); + + } + + @Test + public void peek() throws Exception { + assertEquals("d", stack.peek()); + assertEquals(4, stack.size()); + } + + @Test + public void isEmpty() throws Exception { + assertEquals(false, stack.isEmpty()); + } + + @Test + public void size() throws Exception { + assertEquals(4, stack.size()); + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/TestRunner.java b/group16/2562124714/src/Test/TestRunner.java new file mode 100644 index 0000000000..2bf465f832 --- /dev/null +++ b/group16/2562124714/src/Test/TestRunner.java @@ -0,0 +1,19 @@ +package Test; + +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.Failure; + +import javax.xml.transform.Result; + +/** + * Created by zhangwj on 2017/2/23. 调用测试类 可重复使用 + */ +public class TestRunner { + public static void main(String[] args) { + org.junit.runner.Result result = JUnitCore.runClasses(BinaryTreeNodeTest.class); + for (Failure failure:result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f1d5a9fdd9 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private int Scale; //每次扩展大小 + + private Object[] elementData = new Object[100]; + + public ArrayList() + { + this.Scale = 10; + } + + public ArrayList(int i) + { + this.Scale = i; + } + + public void add(Object o){ + if (this.size == elementData.length) + { + DoEnlage(); + } + elementData[size] = o; + this.size++; + } + + private void DoEnlage() + { + if (this.Scale >= 1 && this.Scale <= 10000) + { + Object[] NewElementData = new Object[this.elementData.length + this.Scale]; + System.arraycopy(this.elementData,0,NewElementData,0,this.elementData.length); + + this.elementData = NewElementData; + } + + } + + //index从1开始 位置1,2,3,4,5,6 + public void add(int index, Object o){ + if (this.size == elementData.length) + { + DoEnlage(); + } + int i = 0; + //遍历赋值 + for(i = this.size; i >= index;i--) + { + this.elementData[i] = this.elementData[i - 1]; + } + + this.elementData[i] = o; + this.size++; + + } + + public Object get(int index){ + if (index >= 1 && index <= this.size) + { + return this.elementData[index - 1]; + } + else { + return null; + } + + + } + + public Object remove(int index){ + if (index >= 1 && index <= this.size) + { + int i = 0; + Object DelElement = this.elementData[index - 1]; + for(i = index; i <= this.size; i++) + { + this.elementData[i - 1] = this.elementData[i]; + } + this.elementData[i] = null; + this.size--; + + return DelElement; + + } + else { + return null; + } + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java b/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a703ad3165 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +import java.util.Properties; + +public class BinaryTreeNode { + + private TreeData treeData; + //private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public TreeData getData() { + return treeData; + } + public void setData(TreeData data) { + this.treeData = 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 PriOder(BinaryTreeNode Node) + { + if (Node.treeData != null) + { + System.out.println(Node.treeData.getT()); + if (Node.left != null) + { + PriOder(Node.left); + } + if (Node.right != null) + { + PriOder(Node.right); + } + } + + } + + + public BinaryTreeNode insert(TreeData o){ + if (this.treeData == null && this.left == null && this.right == null) + { + this.treeData = o; + this.left = null; + this.right = null; + return null; + } + + //遍历寻找元素应该插入的位置 + if (o.compareTo(this.treeData) <= 0) + { + if (this.left != null) + { + this.left.insert(o); + } + else + { + BinaryTreeNode NewNode = new BinaryTreeNode(); + NewNode.setData(o); + NewNode.setLeft(null); + NewNode.setRight(null); + this.left = NewNode; + } + } + else + { + if (this.right != null) + { + this.right.insert(o); + } + else + { + BinaryTreeNode NewNode = new BinaryTreeNode(); + NewNode.setData(o); + NewNode.setLeft(null); + NewNode.setRight(null); + this.right = NewNode; + } + } + + + return null; + } + +} diff --git a/group16/2562124714/src/com/coding/basic/Iterator.java b/group16/2562124714/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/2562124714/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/group16/2562124714/src/com/coding/basic/LinkedList.java b/group16/2562124714/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..f1de0a6839 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/LinkedList.java @@ -0,0 +1,200 @@ +package com.coding.basic; + +import com.sun.org.apache.bcel.internal.generic.NEW; + +import java.awt.*; + +public class LinkedList implements List { + public LinkedList() + { + head = null; + this.Size = 0; + } + + private Node head; + private int Size; + + public void add(Object o){ + Node NewNode = new Node(o); + + if (this.head == null) + { + head = NewNode; + } + else + { + Node node; + for (node = head; node.next != null; node = node.next) + { + } + node.next = NewNode; + } + this.Size++; + + } + //index为位置1,2,3,4,5,6,7 + public void add(int index , Object o){ + Node NewNode = new Node(o); + + if (1 == index) + { + NewNode.next = head; + head = NewNode; + } + else { + Node node; + int i = 0; + for (i = 1, node = head; i < index - 1; i++, node = node.next) { + } + NewNode.next = node.next; + node.next = NewNode; + } + this.Size++; + + } + public Object get(int index){ + Node node; + int i = 0; + + for (i = 1, node = head; i < index ; i++, node = node.next) { + } + + return node.data; +// return null; + } + public Object remove(int index){ + Node node; + int i = 0; + + if (1 == index) + { + if (head.next == null) + { + Object DelData = head.data; + head = null; + this.Size--; + return DelData; + } + else + { + Node DelNode = head; + head = DelNode.next; + DelNode.next = null; + this.Size--; + return DelNode.data; + + } + } + else { + + for (i = 1, node = head; i < index - 1; i++, node = node.next) { + } + Node DelNode = node.next; + node.next = DelNode.next; + DelNode.next = null; + this.Size--; + return DelNode.data; + } + } + + public int size(){ + + return this.Size; + } + + public void addFirst(Object o){ + Node NewNode = new Node(o); + + if (null == this.head) + { + NewNode.next = null; + head = NewNode; + } + else + { + NewNode.next = head; + head = NewNode; + } + + this.Size++; + + } + public void addLast(Object o){ + Node NewNode = new Node(o); + + if (this.Size == 0) + { + NewNode.next = null; + head = NewNode; + } + else + { +// int i = 0; + Node node; + for (node = head; node.next != null; node = node.next) { + + } + node.next = NewNode; + } + + this.Size++; + + } + public Object removeFirst(){ + Node DelFirst; + + if (1 == this.Size) + { + DelFirst = this.head; + DelFirst.next = null; + head = null; + } + else + { + DelFirst = this.head; + head = head.next; + DelFirst.next = null; + } + this.Size--; + + return DelFirst.data; + } + public Object removeLast(){ + Node DelLast; + + if (1 == this.Size) + { + DelLast = head; + DelLast.next = null; + head = null; + } + else + { + Node node; + for (node = head; node.next.next != null; node = node.next) { + + } + DelLast = node.next; + node.next = null; + } + this.Size--; + + return DelLast.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object o) + { + this.data = o; + this.next = null; + } + + } +} diff --git a/group16/2562124714/src/com/coding/basic/List.java b/group16/2562124714/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/2562124714/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/group16/2562124714/src/com/coding/basic/Queue.java b/group16/2562124714/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..40b8f22607 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + + return elementData.get(1); + } + + public boolean isEmpty(){ + + return elementData.size() == 0 ? true : false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/2562124714/src/com/coding/basic/Stack.java b/group16/2562124714/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..9dba3befa5 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + +// public Stack() +// { +// elementData +// } + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.remove(elementData.size()); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()); + return o; + } + public boolean isEmpty(){ + if (elementData.size() == 0) + { + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group16/2562124714/src/com/coding/basic/TestJunit.java b/group16/2562124714/src/com/coding/basic/TestJunit.java new file mode 100644 index 0000000000..59ef43a992 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TestJunit.java @@ -0,0 +1,17 @@ +package com.coding.basic; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class TestJunit { + @Test + + public void testAdd() + { + String str = "Junit is working fine"; + assertEquals("Junit is working fine", str); + } + +} diff --git a/group16/2562124714/src/com/coding/basic/TestRunner.java b/group16/2562124714/src/com/coding/basic/TestRunner.java new file mode 100644 index 0000000000..625ace52c6 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TestRunner.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import com.sun.net.httpserver.Authenticator; +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.Failure; +import org.junit.runners.model.TestClass; + +import javax.xml.transform.Result; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class TestRunner { + public static void main(String[] args) + { + org.junit.runner.Result result = JUnitCore.runClasses(TestJunit.class); + + for (Failure failure : result.getFailures()) + { + System.out.println(failure.toString()); + } + + System.out.println(result.wasSuccessful()); + } +} diff --git a/group16/2562124714/src/com/coding/basic/TreeData.java b/group16/2562124714/src/com/coding/basic/TreeData.java new file mode 100644 index 0000000000..e79b7bd450 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TreeData.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +/** + * Created by zhangwj on 2017/2/22. + */ +public class TreeData> implements Comparable>{ + private int s; + private T t; + + public T getT() + { + return t; + } + + public void setT(T o) { t = o;} + + @Override + public int compareTo(TreeData o) { + return getT().compareTo(o.getT()); + } + +// public int compareTo(TreeData o) +// { +// +// } + + +} diff --git a/group16/313001956/.classpath b/group16/313001956/.classpath new file mode 100644 index 0000000000..b42037dde2 --- /dev/null +++ b/group16/313001956/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group16/313001956/.gitignore b/group16/313001956/.gitignore new file mode 100644 index 0000000000..84c048a73c --- /dev/null +++ b/group16/313001956/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/group16/313001956/.project b/group16/313001956/.project new file mode 100644 index 0000000000..16d7526efa --- /dev/null +++ b/group16/313001956/.project @@ -0,0 +1,36 @@ + + + assignment + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/group16/313001956/.settings/.jsdtscope b/group16/313001956/.settings/.jsdtscope new file mode 100644 index 0000000000..92e666d77d --- /dev/null +++ b/group16/313001956/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.jdt.core.prefs b/group16/313001956/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..c537b63063 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +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.compliance=1.6 +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/group16/313001956/.settings/org.eclipse.wst.common.component b/group16/313001956/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000000..2a267dc19d --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml b/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..611d2dfa73 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000000..3bd5d0a480 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000000..05bd71b6ec --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/group16/313001956/WebContent/META-INF/MANIFEST.MF b/group16/313001956/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..254272e1c0 --- /dev/null +++ b/group16/313001956/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..eaaa690fa6 Binary files /dev/null and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/502059278/.classpath b/group16/502059278/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/502059278/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/502059278/.gitignore b/group16/502059278/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/502059278/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/502059278/.project b/group16/502059278/.project new file mode 100644 index 0000000000..72a951f7c1 --- /dev/null +++ b/group16/502059278/.project @@ -0,0 +1,17 @@ + + + DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" new file mode 100644 index 0000000000..31dfe4c14b Binary files /dev/null and "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" differ diff --git a/group16/502059278/src/cn/mark/MyArrayList.java b/group16/502059278/src/cn/mark/MyArrayList.java new file mode 100644 index 0000000000..9e0e406274 --- /dev/null +++ b/group16/502059278/src/cn/mark/MyArrayList.java @@ -0,0 +1,144 @@ +package cn.mark; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 自定义实现ArrayList的数据结构 + * @author hilih + * + */ +public class MyArrayList implements MyList{ + + private int size = 0; + + private Object[] elementData; + + public MyArrayList(){ + //默认容量初始化为10 + this(10); + } + + /** + * 初始即指定大小的构造方法 + * @param size 集合容量 + */ + public MyArrayList(int size){ + if ( size < 0 ){ + System.out.println("不合法的容量输入"); + return; + } + elementData = new Object[size]; + } + + /** + * 集合增容 + * @param minSize + */ + private void ensureSize(int minSize){ + int oldSize = elementData.length; + if(minSize > oldSize){ + int newSize = 3 * oldSize / 2 + 1; + if(minSize > newSize){ + newSize = minSize; + } + elementData = Arrays.copyOf(elementData, newSize); + } + } + + /** + * 下标范围判断 + * @param index + */ + private boolean rangeCheck(int index){ + if ( index >= size || index < 0 ){ + System.out.println("索引不合法!"); + return false; + } + return true; + } + + @Override + public boolean add(Object o) { + ensureSize(size+1); + elementData[size++] = o; + return true; + } + + @Override + public boolean add(int index, Object o) { + if (!rangeCheck(index)){ + return false; + } + ensureSize(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + return true; + } + + @Override + public Object get(int index) { + if (!rangeCheck(index)){ + return null; + } + Object o = elementData[index]; + return o; + } + + @Override + public Object remove(int index) { + if (!rangeCheck(index)){ + return null; + } + Object oldValue = elementData[index]; + int numMoved = size - index - 1; + if( numMoved > 0 ){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + return oldValue; + } + + @Override + public int size() { + return size; + } + + + + @Override + public String toString() { + StringBuilder s = new StringBuilder(); + s.append("["); + for (int i = 0; i < size; i++){ + Object o = elementData[i]; + s.append(o.toString()); + if( i < size-1 ){ + s.append(","); + } + } + s.append("]"); + return s.toString(); + } + + /** + * 判断当前集合是否为空 + * @return + */ + public boolean isEmpty(){ + return size == 0; + } + + public static void main(String[] args) { + MyList list = new MyArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add(2,"d"); + Object o = list.get(5); + System.out.println(o); + System.out.println(list.size()); + System.out.println(list); + } +} diff --git a/group16/502059278/src/cn/mark/MyLinkedList.java b/group16/502059278/src/cn/mark/MyLinkedList.java new file mode 100644 index 0000000000..7f9c3856a2 --- /dev/null +++ b/group16/502059278/src/cn/mark/MyLinkedList.java @@ -0,0 +1,67 @@ +package cn.mark; +/** + * 自定义实现LinkedList数据结构 + * @author hilih + * + */ +public class MyLinkedList implements MyList{ + + private Node head; + private int size;//集合的长度 + + /** + * 添加元素 + */ + @Override + public boolean add(Object o) { + //为空判断 + if ( o == null ){ + System.out.println("不允许null的元素插入!"); + return false; + } + if(head == null){ + head = new Node(); + head.data = o; + }else{ + + } + + return false; + } + + @Override + public boolean add(int index, Object o) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object get(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object remove(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return 0; + } + + private static class Node{ + Object data; + Node next; + } + + + public static void main(String[] args) { + + + } + +} diff --git a/group16/502059278/src/cn/mark/MyList.java b/group16/502059278/src/cn/mark/MyList.java new file mode 100644 index 0000000000..19bc3f92fe --- /dev/null +++ b/group16/502059278/src/cn/mark/MyList.java @@ -0,0 +1,32 @@ +package cn.mark; + +public interface MyList { + /** + * 向集合中增加元素 + * @param o + */ + public boolean add(Object o); + /** + * 向集合指定的位置中增加元素 + * @param index 下标 + * @param o 元素 + */ + public boolean 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(); +} \ No newline at end of file diff --git a/group16/542087872/src/com/coding/basic/ArrayList.java b/group16/542087872/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1b10b441cf --- /dev/null +++ b/group16/542087872/src/com/coding/basic/ArrayList.java @@ -0,0 +1,88 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + // 每次乘2增长 + private void grow() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + + public void add(Object o){ + if (size >= elementData.length) { + this.grow(); + } + + elementData[size++] = o; + } + public void add(int index, Object o){ + if (size >= elementData.length) { + this.grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + + Object el = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + size--; + return el; + } + + public int size(){ + return size; + } + + private class ArrIter implements Iterator { + int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < size; + } + + @Override + public Object next() { + return elementData[cursor++]; + } + } + + public Iterator iterator(){ + return new ArrIter(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]); + if (i < size - 1) { + sb.append(","); + } + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java b/group16/542087872/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..df167343a0 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + 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(int data) { + this.data = data; + } + + private BinaryTreeNode insertAt(BinaryTreeNode node, int o) { + if (o < node.getData()) { + if (node.getLeft() != null) { + return insertAt(node.getLeft(), o); + } else { + BinaryTreeNode nowNode = new BinaryTreeNode(o); + node.setLeft(nowNode); + + return nowNode; + } + } else { + if (node.getRight() != null) { + return insertAt(node.getRight(), o); + } else { + BinaryTreeNode nowNode = new BinaryTreeNode(o); + node.setRight(nowNode); + return nowNode; + } + } + } + + public BinaryTreeNode insert(int o){ + return insertAt(this, o); + } + + @Override + public String toString() { + return "data: " + data; + } +} diff --git a/group16/542087872/src/com/coding/basic/Iterator.java b/group16/542087872/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/542087872/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/group16/542087872/src/com/coding/basic/LinkedList.java b/group16/542087872/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..144af4ec8d --- /dev/null +++ b/group16/542087872/src/com/coding/basic/LinkedList.java @@ -0,0 +1,192 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + + public void add(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + } else { + tail.next = nowNode; + } + tail = nowNode; + } + public void add(int index , Object o){ + int count = 0; + Node lastOne = null; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + lastOne = tpHead; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + + Node nowNode = new Node(o); + if (lastOne == null) { + head = nowNode; + head.next = tpHead; + } else { + lastOne.next = nowNode; + nowNode.next = tpHead; + } + } + public Object get(int index){ + int count = 0; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + return tpHead.data; + } + public Object remove(int index){ + int count = 0; + Node lastOne = null; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + lastOne = tpHead; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + if (lastOne == null) { + head = tpHead.next; + } else { + lastOne.next = tpHead.next; + } + + if (tpHead.next == null) { + tail = lastOne; + } + + return tpHead.data; + } + + public int size(){ + int count = 0; + Node tpHead = head; + while (tpHead != null) { + count ++; + tpHead = tpHead.next; + } + + return count; + } + + public void addFirst(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + tail = nowNode; + } else { + nowNode.next = head; + head = nowNode; + } + } + public void addLast(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + tail = nowNode; + } else { + tail.next = nowNode; + tail = nowNode; + } + } + public Object removeFirst(){ + if (head == null) { + throw new IndexOutOfBoundsException(); + } + + Node nowValue = head; + + Node nextNode = head.next; + if (nextNode == null) { + tail = null; + } + head = nextNode; + + return nowValue.data; + } + public Object removeLast(){ + if (head == null) { + throw new IndexOutOfBoundsException(); + } + + Node nowValue = tail; + + Node lastOne = null; + Node tpHead = head; + while (tpHead != tail) { + lastOne = tpHead; + tpHead = tpHead.next; + } + if (lastOne == null) { + head = null; + } else { + lastOne.next = null; + } + tail = lastOne; + + return nowValue.data; + } + + private class LinkIter implements Iterator { + + Node cursor = head; + + @Override + public boolean hasNext() { + return cursor != null; + } + + @Override + public Object next() { + Node ret = cursor; + cursor = cursor.next; + return ret.data; + } + } + + public Iterator iterator(){ + return new LinkIter(); + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } + + @Override + public String toString() { + Node tpHead = head; + StringBuilder sb = new StringBuilder("["); + while (tpHead != null) { + sb.append(tpHead.data); + sb.append(","); + tpHead = tpHead.next; + } + sb.append("]"); + return sb.toString(); + } + +} diff --git a/group16/542087872/src/com/coding/basic/List.java b/group16/542087872/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/542087872/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/group16/542087872/src/com/coding/basic/Queue.java b/group16/542087872/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..8e4285464b --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group16/542087872/src/com/coding/basic/Stack.java b/group16/542087872/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..bfe98dd8b7 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return o; + } + + 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/group16/542087872/src/com/coding/basic/Test.java b/group16/542087872/src/com/coding/basic/Test.java new file mode 100644 index 0000000000..2db5b2f9ab --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Test.java @@ -0,0 +1,166 @@ +package com.coding.basic; + + +/** + * Created by xiaoyuan on 25/02/2017. + */ +public class Test { + public static void main(String[] args) { + + testArrayList(); + testLinkedList(); + + testQueue(); + testStack(); + + + testBinaryTreeNode(); + } + + private static void testBinaryTreeNode() { + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(10); + binaryTreeNode.insert(5); + binaryTreeNode.insert(4); + binaryTreeNode.insert(6); + binaryTreeNode.insert(11); + + traverse(binaryTreeNode); + + } + + private static void traverse(BinaryTreeNode node) { + if (node.getLeft() != null) { + traverse(node.getLeft()); + } + + System.out.println("-- " + node.getData() + " --"); + + if (node.getRight() != null) { + traverse(node.getRight()); + } + + } + + + static void testStack() { + + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + System.out.println(stack.size()); + System.out.println(stack.isEmpty()); + + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + System.out.println(stack.isEmpty()); + + } + + static void testQueue() { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + + System.out.println(queue.size()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.size()); + } + static void testLinkedList() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + + System.out.println(linkedList.size()); + System.out.println(linkedList); + + linkedList.add(4); + linkedList.add(5); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.add(0, 10); + linkedList.add(0, 9); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + System.out.println(linkedList.get(3)); + + linkedList.remove(0); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.addFirst(100); + linkedList.addLast(8888); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.removeFirst(); + linkedList.removeLast(); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + + } + + static void testArrayList() { + ArrayList arrayList = new ArrayList(); + arrayList.add("1"); + arrayList.add("2"); + // test size and add + System.out.println(arrayList.size()); + System.out.println(arrayList); + + + arrayList.add("3"); + arrayList.add("4"); + arrayList.add("5"); + arrayList.add("6"); + arrayList.add("7"); + arrayList.add("8"); + arrayList.add("9"); + arrayList.add("10"); + arrayList.add("11"); + arrayList.add("12"); + arrayList.add("13"); + + // test size + // test grow + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test add at index + arrayList.add(2, 100); + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test remove + arrayList.remove(0); + System.out.println(arrayList.size()); + System.out.println(arrayList); + arrayList.remove(2); + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test iterator + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + + } +} diff --git a/group16/out/production/214074094/com/reading/blog_test.txt b/group16/out/production/214074094/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/out/production/214074094/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/out/production/214074094/readme.txt b/group16/out/production/214074094/readme.txt new file mode 100644 index 0000000000..c1b06ddcc2 --- /dev/null +++ b/group16/out/production/214074094/readme.txt @@ -0,0 +1 @@ +I am 北京-Shane diff --git a/group16/out/production/214074094/src/com/reading/blog_test.txt b/group16/out/production/214074094/src/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/out/production/214074094/src/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/liuxin/.classpath b/liuxin/.classpath index fceb4801b5..b387714202 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -1,6 +1,7 @@ - - - - - - + + + + + + + diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..78845d06d0 --- /dev/null +++ b/liuxin/src/com/coderising/array/ArrayUtil.java @@ -0,0 +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; + } + + +} diff --git a/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/Struts.java @@ -0,0 +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; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/liuxin/src/com/coderising/litestruts/View.java b/liuxin/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file