diff --git a/.gitignore b/.gitignore index 2a5296f902..dbf44ab662 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.jar *.war *.ear +target # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* @@ -13,9 +14,13 @@ hs_err_pid* #ide config .metadata .recommenders + + +#macOS +.DS_Store + .idea/ *.iml rebel.* .rebel.* -target diff --git a/group01/group01.md b/group01/group01.md index 8b13789179..d3f5a12faa 100644 --- a/group01/group01.md +++ b/group01/group01.md @@ -1 +1 @@ - + diff --git a/group02/group02.md b/group02/group02.md index 8b13789179..d3f5a12faa 100644 --- a/group02/group02.md +++ b/group02/group02.md @@ -1 +1 @@ - + diff --git a/group03/group03.md b/group03/group03.md index 8b13789179..d3f5a12faa 100644 --- a/group03/group03.md +++ b/group03/group03.md @@ -1 +1 @@ - + diff --git a/group04/group04.md b/group04/group04.md index 8b13789179..d3f5a12faa 100644 --- a/group04/group04.md +++ b/group04/group04.md @@ -1 +1 @@ - + diff --git a/group05/group05.md b/group05/group05.md index 8b13789179..d3f5a12faa 100644 --- a/group05/group05.md +++ b/group05/group05.md @@ -1 +1 @@ - + diff --git a/group06/group06.md b/group06/group06.md index 8b13789179..d3f5a12faa 100644 --- a/group06/group06.md +++ b/group06/group06.md @@ -1 +1 @@ - + diff --git "a/group07/1046545622/git\345\221\275\344\273\244.txt" "b/group07/1046545622/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1046545622/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1058267830/git\345\221\275\344\273\244.txt" "b/group07/1058267830/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1058267830/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1070440331/git\345\221\275\344\273\244.txt" "b/group07/1070440331/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1070440331/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1280157271/git\345\221\275\344\273\244.txt" "b/group07/1280157271/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1280157271/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1448276993/git\345\221\275\344\273\244.txt" "b/group07/1448276993/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1448276993/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1519504320/git\345\221\275\344\273\244.txt" "b/group07/1519504320/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1519504320/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/1520332119/git\345\221\275\344\273\244.txt" "b/group07/1520332119/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1520332119/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/1536161030/.classpath b/group07/1536161030/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group07/1536161030/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1536161030/.gitignore b/group07/1536161030/.gitignore new file mode 100644 index 0000000000..be00f4a4de --- /dev/null +++ b/group07/1536161030/.gitignore @@ -0,0 +1,22 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* \ No newline at end of file diff --git a/group07/1536161030/.project b/group07/1536161030/.project new file mode 100644 index 0000000000..11a1957bfe --- /dev/null +++ b/group07/1536161030/.project @@ -0,0 +1,17 @@ + + + homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1536161030/.settings/org.eclipse.core.resources.prefs b/group07/1536161030/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..4824b80263 --- /dev/null +++ b/group07/1536161030/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git "a/group07/1536161030/CPU,\345\206\205\345\255\230,\347\241\254\347\233\230,\346\214\207\344\273\244.txt" "b/group07/1536161030/CPU,\345\206\205\345\255\230,\347\241\254\347\233\230,\346\214\207\344\273\244.txt" new file mode 100644 index 0000000000..32d83cd08b --- /dev/null +++ "b/group07/1536161030/CPU,\345\206\205\345\255\230,\347\241\254\347\233\230,\346\214\207\344\273\244.txt" @@ -0,0 +1,12 @@ +计算机内部主要参与计算有CPU,内存,硬盘,指令。 +CPU,也叫处理器,人靠大脑思考,电脑靠CPU运算。CPU主要负责计算和控制的部分,顾名思义,就是控制单元和运算单元两部分,正如人的大脑进行记忆和运算一样。 +但人脑的记忆和运算是一体的,而计算机的每个功能单元都是分工明确的。其中控制单元根据程序,通过指令来控制运算单元来进行运算得出结果,然后控制存储单元来存储中间的结果。 +内存是存储CPU需要的数据资料,主要功能有:缓存系统中的临时数据;保存从硬盘读取的数据,提供给CPU使用。 +硬盘是负责存储所有需要永久存储的数据,比如我们的程序和数据,优点是非常固定,快速且准确,不像人脑存储的信息,会随着时间而遗忘。 +内存存取数据的速度比硬盘的存取速度快了10倍, 在某些环境里可能会更大。 +CPU的速度比内存不知要快多少倍。当我们把程序从硬盘放到内存以后,CPU就直接在内存运行程序,这样比CPU直接在硬盘运行程序就要快很多。 +所以内存解决了一部分CPU运行过快,而硬盘数据存取太慢的问题。 提高了我们的电脑的运行速度。 +当我们运行程序的时候,CPU首先接受到我们的命令,之后CPU是告诉硬盘,我要运行你存储的程序A,你把程序A送到内存去。CPU对内存说,我让硬盘把程序A送到你这里来了,你保存一下。 等程序A被完整的送到内存之后。CPU就开始执行程序A。而CPU如何运算,如何取数,如何存储数据,三者如何对上面的问题进行沟通,这就需要指令了。 +指令也就是机器语言的一个个语句,相当于CPU,内存,硬盘之间的语言。用来指挥计算机工作,计算的命令,比如数据的运算,逻辑运算,数据传送,程序的控制,输入输出指令等。 + + diff --git "a/group07/1536161030/git\345\221\275\344\273\244.txt" "b/group07/1536161030/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/1536161030/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/1536161030/src/com/coding/basic/ArrayList.java b/group07/1536161030/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..dfad793392 --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/ArrayList.java @@ -0,0 +1,95 @@ +package com.coding.basic; + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList(int size) { + this.elementData = new Object[size]; + } + + public ArrayList() { + this.elementData = new Object[10]; + } + + public void add(Object o) { + if(isFull()) + resize(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + rangeCheckForAdd(index); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + rangeCheckForAdd(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheckForAdd(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + elementData[--size] = null; + return o; + } + + public int size() { + return elementData.length; + } + + public com.coding.basic.Iterator iterator() { + return new Itr(); + } + + private class Itr implements com.coding.basic.Iterator { + int cursor; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i < elementData.length) { + cursor = i + 1; + return elementData[i]; + } + return null; + } + } + + //妾㈡煡涓嬭〃瓒婄晫 + public void rangeCheckForAdd(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + } + + //鏁扮粍鏄惁婊 + public boolean isFull(){ + return size == elementData.length; + } + + //鎵╁ + public void resize(){ + Object[] newElementData = new Object[elementData.length * 2]; + System.arraycopy(elementData, 0, newElementData, 0, size); + this.elementData = newElementData; + newElementData = null; + } + + // + public boolean isEmpty() { + return size == 0; + } + + +} diff --git a/group07/1536161030/src/com/coding/basic/BinaryTreeNode.java b/group07/1536161030/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b1143e1a74 --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class BinaryTreeNode > { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.data = data; + this.left=null; + this.right =null; + } + + public BinaryTreeNode root; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + /* + * 浜屽弶鏍 b 鎻掑叆瀵硅薄 o 鐖惰妭鐐 pnode + * 鑻涓虹┖鏍戯紝鎻掑叆鑺傜偣浣滀负鏍 + * o 绛変簬鏍硅妭鐐 鐩存帴杩斿洖 + * o 灏忎簬鏍硅妭鐐 pnode = 宸﹀瓙鏍 + * else pnode = 鍙冲瓙鏍 + * + */ + public BinaryTreeNode insert(Object o) { + BinaryTreeNode current = root; + + if(current == null){ + return new BinaryTreeNode(o); + } + if (o.compareTo((Object) current.data)<0){ + if(current.left !=null){ + current = current.left; + }else{ + current.left = new BinaryTreeNode(o); + return current; + } + }else if(o.compareTo((Object) current.data)==0){ + return current; + }else{ + if(current.right !=null){ + current = current.right; + }else{ + current.right = new BinaryTreeNode(o); + return current; + } + } + return current; + } +} diff --git a/group07/1536161030/src/com/coding/basic/Iterator.java b/group07/1536161030/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..2ab5f039d5 --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + + Object next(); + +} diff --git a/group07/1536161030/src/com/coding/basic/LinkedList.java b/group07/1536161030/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..fc3ed5afaf --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/LinkedList.java @@ -0,0 +1,139 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + this.head = null; + this.size = 0; + } + + public void add(Object o) { + Node newNode = new Node(o); + if(isEmpty()){ + head = newNode; + } + else{ + newNode.next = head; + head = newNode; + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + + Node indexNode = node(index); + + Node newNode = new Node(o); + if(isEmpty()){ + head = newNode; + }else { + newNode.next = indexNode; + indexNode = newNode; + } + size++; + } + + public Object get(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + return node(index).data; + } + + public Object remove(int index) { + Node indexNode = node(index); + Node preNode = node(index); + preNode.next = indexNode.next; + indexNode.next = null; + size--; + return indexNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if(o == null){ + throw new RuntimeException("涓嶈兘鍔犲叆null鍏冪礌"); + } + Node newNode = new Node(o); + newNode.next = head; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o); + Node last = node(size); + + newNode.next = last.next; + last.next = newNode; + size++; + } + + public Object removeFirst() { + Node oldNode = head; + head = head.next; + head.next = null; + size--; + return oldNode.data; + } + + public Object removeLast() { + Node oldNode = node(size); + Node preNode = node(size - 1); + preNode.next = null; + size--; + return oldNode.data; + } + + public Iterator iterator(int index) { + return new Itr(index); + } + + // TODO: 2017/2/24 + private class Itr implements com.coding.basic.Iterator { + private int nextIndex; + + public Itr(int index) { + this.nextIndex = index; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + return null; + } + } + + + private static class Node { + public Object data; + public Node next; + + public Node(Object data) { + this.data = data; + this.next = null; + } + } + + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + // + public boolean isEmpty() { + return head ==null; + } +} diff --git a/group07/1536161030/src/com/coding/basic/List.java b/group07/1536161030/src/com/coding/basic/List.java new file mode 100644 index 0000000000..df30cc6e07 --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group07/1536161030/src/com/coding/basic/Queue.java b/group07/1536161030/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..70f99dfd3e --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/Queue.java @@ -0,0 +1,72 @@ +package com.coding.basic; + +public class Queue { + private int size; + + //澶存爣蹇椾綅 + private int head; + + //灏炬爣蹇椾綅 + private int tail; + + private Object[] queue; + + public Queue() { + this.queue = new Object[10]; + this.size = 0 ; + this.head = 0; + this.tail = 0; + } + + public void enQueue(Object o) { + if(isFull()){ + resize(); + head=0; + } + int newtail = (head + size )% queue.length; + queue[newtail]=o; + size ++; + } + + public Object deQueue() { + if(isEmpty()) + throw new IndexOutOfBoundsException("闃熷垪涓虹┖!"); + Object old = queue[head]; + queue[head] =null; + head = (head + 1)% queue.length; + size --; + return old; + + } + + public Object getHead(){ + return head; + } + + public Object getTail(){ + return tail; + } + + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + int diff = tail - head; + return diff; + } + + // + public boolean isFull(){ + return size == queue.length; + } + + //鎵╁ + public void resize(){ + Object[] newQueue = new Object[queue.length * 2]; + System.arraycopy(queue, 0, newQueue, 0, size); + this.queue = newQueue; + newQueue = null; + } +} diff --git a/group07/1536161030/src/com/coding/basic/Stack.java b/group07/1536161030/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..233723755b --- /dev/null +++ b/group07/1536161030/src/com/coding/basic/Stack.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + + +public class Stack { + private Object[] elementData; + private int size; + + public Stack() { + this.size = 0; + this.elementData = new Object[10]; + } + + public void push(Object o) { + if (o == null) + throw new RuntimeException("鍏冪礌涓嶅彲涓篘ULL"); + size++; + elementData[size -1]= o ; + } + + public Object pop() { + if (isEmpty()) + throw new EmptyStackException(); + Object old = elementData[size - 1]; + elementData[size] = null; + size--; + return old; + } + + public Object peek() { + if (isEmpty()) + throw new EmptyStackException(); + return elementData[size -1]; + } + + public boolean isEmpty() { + return size < 1; + } + + // + public int size() { + return size; + } +} + + diff --git a/group07/1536161030/src/com/coding/test/ArrayListTest.java b/group07/1536161030/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..482fe53cd8 --- /dev/null +++ b/group07/1536161030/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,70 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +// +public class ArrayListTest { + + @Test + public void testAddObject() { + ArrayList al = new ArrayList(); + assertEquals(10, al.size()); + al.add(new Integer(1)); + System.out.print(al.get(0)); + } + + @Test + public void testAddIntObject() { + ArrayList al = new ArrayList(); + al.add(0, new Integer(1)); + assertEquals(10, al.size()); + int tmp = 0; + try { + al.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + + } + + @Test + public void testGet() { + ArrayList al = new ArrayList(); + al.add(new Integer(1)); + assertEquals((Integer)al.get(0),new Integer(1)); + int tmp = 0; + try { + al.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + ArrayList al = new ArrayList(); + al.add(new Integer(1)); + assertEquals((Integer)al.get(0),new Integer(1)); + assertEquals(al.size(),10); + } + + @Test + public void testSize() { + ArrayList al = new ArrayList(); + assertEquals(10, al.size()); + } + + @Test + public void testIsEmpty() { + ArrayList al = new ArrayList(); + assertTrue(al.isEmpty()); + al.add(new Integer(1)); + assertFalse(al.isEmpty()); + } +} diff --git a/group07/1536161030/src/com/coding/test/BinaryTreeNodeTest.java b/group07/1536161030/src/com/coding/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..dd2cde39b7 --- /dev/null +++ b/group07/1536161030/src/com/coding/test/BinaryTreeNodeTest.java @@ -0,0 +1,19 @@ +package com.coding.test; + +// +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.LinkedList; + +public class BinaryTreeNodeTest { + + @Test + public void testAddObject() { + BinaryTreeNode bt = new BinaryTreeNode(null); + bt.insert(new Integer(1)); + assertNotNull(bt); + } +} diff --git a/group07/1536161030/src/com/coding/test/LinkedListTest.java b/group07/1536161030/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..fb0ed303ed --- /dev/null +++ b/group07/1536161030/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,68 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.LinkedList; + +// +public class LinkedListTest{ + @Test + public void testAddObject() { + LinkedList list = new LinkedList(); + assertEquals(0, list.size()); + list.add(new Integer(1)); + assertEquals(1, list.size()); + } + + @Test + public void testAddIntObject() { + LinkedList list = new LinkedList(); + list.add(0, new Integer(1)); + assertEquals(1, list.size()); + int tmp = 0; + try { + list.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testGet() { + LinkedList list = new LinkedList(); + list.add(new Object()); + assertNotNull(list.get(0)); + int tmp = 0; + try { + list.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + LinkedList list = new LinkedList(); + list.add(new Object()); + list.remove(0); + assertEquals(list.size(),0); + } + + @Test + public void testSize() { + LinkedList list = new LinkedList(); + assertEquals(0, list.size()); + } + + @Test + public void testIsEmpty() { + LinkedList list = new LinkedList(); + assertTrue(list.isEmpty()); + list.add(new Object()); + assertFalse(list.isEmpty()); +} +} diff --git a/group07/1536161030/src/com/coding/test/QueueTest.java b/group07/1536161030/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..3130dbc808 --- /dev/null +++ b/group07/1536161030/src/com/coding/test/QueueTest.java @@ -0,0 +1,47 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Queue; + +// +public class QueueTest { + + @Test + public void testEnQueue() { + Queue queue = new Queue(); + assertEquals(queue.size(), 0); + System.out.print(queue.getHead()); + } + + @Test + public void testDeQueue() { + Queue queue = new Queue(); + int tmp = 0; + try { + queue.deQueue(); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + queue.enQueue(new Object()); + assertNotNull(queue.deQueue()); + } + + @Test + public void testIsEmpty() { + Queue queue = new Queue(); + assertTrue(queue.isEmpty()); + queue.enQueue(new Object()); + assertFalse(queue.isEmpty()); + } + + @Test + public void testSize() { + Queue queue = new Queue(); + assertEquals(queue.size(), 0); + } + +} diff --git a/group07/1536161030/src/com/coding/test/StackTest.java b/group07/1536161030/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..3622e0cb68 --- /dev/null +++ b/group07/1536161030/src/com/coding/test/StackTest.java @@ -0,0 +1,58 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import java.util.EmptyStackException; + +import org.junit.Test; + +import com.coding.basic.Stack; + +// +public class StackTest { + + @Test + public void testPush() { + Stack stack = new Stack(); + assertEquals(0, stack.size()); + stack.push(new Object()); + assertEquals(1, stack.size()); + } + + @Test + public void testPop() { + Stack stack = new Stack(); + stack.push(new Object()); + assertNotNull(stack.pop()); + assertEquals(0, stack.size()); + } + + @Test + public void testPeek() { + Stack stack = new Stack(); + int tmp = 0; + try { + stack.peek(); + } catch (EmptyStackException e) { + tmp = 1; + assertEquals(1, tmp); + } + stack.push(new Object()); + assertNotNull(stack.peek()); + assertEquals(1, stack.size()); + } + + @Test + public void testIsEmpty() { + Stack stack = new Stack(); + assertTrue(stack.isEmpty()); + stack.push(new Object()); + assertFalse(stack.isEmpty()); + } + + @Test + public void testSize() { + Stack stack = new Stack(); + assertEquals(0, stack.size()); + } +} diff --git "a/group07/178007127/git\345\221\275\344\273\244.txt" "b/group07/178007127/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/178007127/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/20409287/git\345\221\275\344\273\244.txt" "b/group07/20409287/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/20409287/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/2306826375/git\345\221\275\344\273\244.txt" "b/group07/2306826375/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/2306826375/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/2306826375/txt b/group07/2306826375/txt new file mode 100644 index 0000000000..d6459e0054 --- /dev/null +++ b/group07/2306826375/txt @@ -0,0 +1 @@ +xxx diff --git "a/group07/2708094737/git\345\221\275\344\273\244.txt" "b/group07/2708094737/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/2708094737/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/2915553181/git\345\221\275\344\273\244.txt" "b/group07/2915553181/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/2915553181/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/328536398/git\345\221\275\344\273\244.txt" "b/group07/328536398/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/328536398/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/396868934/git\345\221\275\344\273\244.txt" "b/group07/396868934/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/396868934/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/43819473/git\345\221\275\344\273\244.txt" "b/group07/43819473/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/43819473/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/466199956/git\345\221\275\344\273\244.txt" "b/group07/466199956/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/466199956/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/476770768/MyDataStructure/.classpath b/group07/476770768/MyDataStructure/.classpath new file mode 100644 index 0000000000..63b7e892d1 --- /dev/null +++ b/group07/476770768/MyDataStructure/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/476770768/MyDataStructure/.gitignore b/group07/476770768/MyDataStructure/.gitignore new file mode 100644 index 0000000000..c910559f7f --- /dev/null +++ b/group07/476770768/MyDataStructure/.gitignore @@ -0,0 +1,17 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ diff --git a/group07/476770768/MyDataStructure/.project b/group07/476770768/MyDataStructure/.project new file mode 100644 index 0000000000..b2d0b8054f --- /dev/null +++ b/group07/476770768/MyDataStructure/.project @@ -0,0 +1,17 @@ + + + MyDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/476770768/MyDataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/476770768/MyDataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group07/476770768/MyDataStructure/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/BinaryTreeNode.java b/group07/476770768/MyDataStructure/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6cc4ecb4df --- /dev/null +++ b/group07/476770768/MyDataStructure/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; + + public void insert(BinaryTreeNode node) { + if (this.data == null) { + // empty binary tree + this.data = node.data; + this.left = node.left; + this.right = node.right; + } else if (((Integer) this.data).intValue() >= ((Integer) node.data).intValue()) { + this.left.insert(node); + }else{ + this.right.insert(node); + } + } + + 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; + } +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..f0c1b3608c --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,149 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class MyArrayList implements MyList{ + + private int size = 0; + + private Object[] elementData = new Object[5]; + + @Override + /** + * add an element to the end + */ + public void add(Object o) { + int index = size; + if(isFull()){ + extendLength(); + } + elementData[index] = o; + size++; + } + + @Override + /** + * add an element to certain index + */ + public void add(int index, Object o) { + checkBounds(index); + if(isFull()){ + extendLength(); + } + for(int i=size; i>=index; i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + @Override + /** + * get an element + */ + public Object get(int index) { + checkBoundsForGet(index); + if(index >= size){ + return null; + } + return elementData[index]; + } + + @Override + /** + * remove an element + */ + public Object remove(int index) { + checkBounds(index); + Object res = elementData[index]; + for(int i=index+1; i<=size; i++){ + elementData[i-1] = elementData[i]; + } + size--; + return res; + } + + @Override + public int size() { + return size; + } + + /** + * extends the length + */ + public void extendLength(){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + //System.out.println("add extend "+elementData.length); + } + + public boolean isEmpty(){ + return size == 0; + } + + public boolean isFull(){ + int len = elementData.length; + if(size >= len-1){ + return true; + } + return false; + } + + public void checkBounds(int index){ + if(index >= size || index < 0){ + //System.out.println("From MyArrayList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + /** + * for get() + * @param index + */ + public void checkBoundsForGet(int index){ + if(index >= elementData.length || index < 0){ + //System.out.println("From MyArrayList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + public String OutOfBoundsMsg(int index){ + return "Index: "+index+", Size: "+size; + } + + @Override + public String toString() { + String s = ""; + for(int i=0; i size - 1) { + // System.out.println("From MyLinkedList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + /** + * the index should be within 0~size + * + * @param index + */ + public void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + // System.out.println("From MyLinkedList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + public String OutOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * find the position of index + * + * @param index + * @return + */ + public Node findIndexPosition(int index) { + Node pos = head; + for (int i = 0; i < index; i++) { + pos = pos.next; + } + return pos; + } + + @Override + public String toString() { + String s = ""; + Node tmp = head; + while (tmp != null) { + s += tmp.data + " "; + tmp = tmp.next; + } + return s; + } + + private static class Node { + public Object data; + public Node prov; + public Node next; + + public Node() { + } + + public Node(Object o) { + this.data = o; + this.prov = null; + this.next = null; + } + } + + public MyIterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements MyIterator{ + + private MyLinkedList eleIterator; + private Node pos; + + private LinkedListIterator(MyLinkedList mll){ + this.eleIterator = mll; + this.pos = eleIterator.get(0); + } + + @Override + public boolean hasNext() { + return pos != null; + } + + @Override + public Object next() { + Node res = pos; + pos = pos.next; + return res; + } + + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..9089e106db --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyList.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueue.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..717c67bd4b --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + //if queue is empty, element.size()-1 = -1 + //MyLinkedList will throw exception + Object tmp = elementData.remove(elementData.size()-1); + return tmp; + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } + + public MyIterator iterator() { + return elementData.iterator(); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..3d9e1ef9a0 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + int top = -1;//always points to top element + + public void push(Object o){ + elementData.add(o); + top++; + } + + public Object pop(){ + if(isEmpty()){ + throw new EmptyStackException(); + }else{ + Object tmp = elementData.remove(top); + top--; + return tmp; + } + } + + public Object peek(){ + if(isEmpty()){ + throw new EmptyStackException(); + }else{ + Object tmp = elementData.get(top); + return tmp; + } + } + + public boolean isEmpty(){ + return top >= 0; + } + + public int size(){ + return top + 1; + } + + public MyIterator iterator() { + return elementData.iterator(); + } +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java new file mode 100644 index 0000000000..1ccabfc977 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class testFile { + + public static void main(String[] args) { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Integer(5)); + mll.add(new Integer(2)); + mll.add(new Integer(3)); + mll.add(new Integer(4)); + System.out.println(mll); + MyIterator mIt = mll.iterator(); + while(mIt.hasNext()){ + System.out.println(mIt.next()); + } + mll.remove(3); + System.out.println(mll); + + + + } + +} diff --git "a/group07/476770768/git\345\221\275\344\273\244.txt" "b/group07/476770768/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/476770768/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/515372252/git\345\221\275\344\273\244.txt" "b/group07/515372252/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/515372252/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/562247675/git\345\221\275\344\273\244.txt" "b/group07/562247675/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/562247675/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/562247675/homework/.gitignore b/group07/562247675/homework/.gitignore new file mode 100644 index 0000000000..df3ade545c --- /dev/null +++ b/group07/562247675/homework/.gitignore @@ -0,0 +1,22 @@ +rebel.xml +.idea/ +*.iml +target/ +*.class +*.jar +*.war +*.ear +hs_err_pid* +*.DS_Store +._* +.Trashes +.TemporaryItems +desktop.ini +Thumbs.db +$RECYCLE.BIN/ +*.lnk +.metadata +.settings +.classpath +.mymetadata +.project \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/pom.xml b/group07/562247675/homework/homework-0226/pom.xml new file mode 100644 index 0000000000..f6a6002f6d --- /dev/null +++ b/group07/562247675/homework/homework-0226/pom.xml @@ -0,0 +1,22 @@ + + + + com.coding2017.group7 + homework + 1.0-SNAPSHOT + + 4.0.0 + + com.coding2017.group7 + homework-0226 + + + junit + junit + + + + \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayList.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayList.java new file mode 100644 index 0000000000..05d3d80d49 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayList.java @@ -0,0 +1,98 @@ +package com.coding2017.group7.homework.c0226; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + @Override + public void add(Object o) { + if (isFull()) { + increase(); + } + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + checkRangeAdd(index); + if (isFull()) { + increase(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkRangeGet(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + checkRangeGet(index); + Object element = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - (index + 1)); + elementData[--size] = null; + return element; + } + + @Override + public int size() { + return size; + } + + public MyIterator iterator() { + return new MyArrayListIterator(); + } + + private boolean isFull() { + return size >= elementData.length; + } + + private void checkRangeGet(int index) { + boolean outOfRange = index < 0 || index >= size; + if (outOfRange) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private void checkRangeAdd(int index) { + boolean outOfRange = index < 0 || index > size; + if (outOfRange) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private void increase() { + Object[] target = new Object[elementData.length * 2]; + System.arraycopy(elementData, 0, target, 0, elementData.length); + elementData = target; + } + + @Override + public String toString() { + return Arrays.toString(elementData); + } + + private class MyArrayListIterator implements MyIterator { + + private int index = 0; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + checkRangeGet(index); + return elementData[index++]; + } + } +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayListTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayListTest.java new file mode 100644 index 0000000000..7523092985 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyArrayListTest.java @@ -0,0 +1,71 @@ +package com.coding2017.group7.homework.c0226; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class MyArrayListTest { + + private MyArrayList myList = new MyArrayList(); + private Object[] elements = new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + private final int mySize = elements.length; + + @Before + public void setUp() throws Exception { + for (int i = 0; i < mySize; i++) { + myList.add(i, i + 1); + } + } + + @After + public void tearDown() throws Exception { + for (int i = myList.size(); i > 0; i--) { + myList.remove(i - 1); + } + myList = null; + } + + @Test + public void addLast() throws Exception { + int element = -1; + myList.add(element); + Assert.assertEquals(myList.size(), mySize + 1); + Assert.assertTrue(myList.get(myList.size() - 1).equals(element)); + } + + @Test + public void addIndex() throws Exception { + int index = mySize / 2; + int element = -1; + myList.add(index, element); + Assert.assertTrue(myList.get(index).equals(element)); + Assert.assertEquals(myList.size(), mySize + 1); + } + + @Test + public void remove() throws Exception { + int index = mySize / 2; + Object before = myList.get(index + 1); + Object element = myList.remove(index); + Object after = myList.get(index); + Assert.assertTrue(before.equals(after)); + Assert.assertEquals(myList.size(), mySize - 1); + + } + + @Test + public void iterator() throws Exception { + MyIterator iterator = myList.iterator(); + int count = 0; + while (iterator.hasNext()) { + iterator.next(); + count++; + } + Assert.assertEquals(mySize, count); + } + +} \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNode.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNode.java new file mode 100644 index 0000000000..334dcd06de --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNode.java @@ -0,0 +1,52 @@ +package com.coding2017.group7.homework.c0226; + +public class MyBinaryTreeNode { + + private Comparable data; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Comparable getData() { + return data; + } + + public void setData(Comparable data) { + this.data = data; + } + + public MyBinaryTreeNode getLeft() { + return left; + } + + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + + public MyBinaryTreeNode getRight() { + return right; + } + + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Comparable o) { + if (data == null) { + data = o; + } + int compare = o.compareTo(data); + if (compare < 0) { + if (left == null) { + left = new MyBinaryTreeNode(); + } + left.insert(o); + } else if (compare > 0) { + if (right == null) { + right = new MyBinaryTreeNode(); + } + right.insert(o); + } + return this; + } + +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java new file mode 100644 index 0000000000..9766b03947 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java @@ -0,0 +1,34 @@ +package com.coding2017.group7.homework.c0226; + +import org.junit.Assert; +import org.junit.Test; + +public class MyBinaryTreeNodeTest { + + + @Test + public void insert() throws Exception { + MyBinaryTreeNode node = new MyBinaryTreeNode(); + node.insert(5) + .insert(2) + .insert(7) + .insert(1) + .insert(6) + .insert(4) + .insert(8) + .insert(3); + Comparable data1 = node.getLeft().getLeft().getData(); + Comparable data4 = node.getLeft().getRight().getData(); + Comparable data6 = node.getRight().getLeft().getData(); + Comparable data8 = node.getRight().getRight().getData(); + Comparable data3 = node.getLeft().getRight().getLeft().getData(); + Assert.assertEquals(1, data1); + Assert.assertEquals(4, data4); + Assert.assertEquals(6, data6); + Assert.assertEquals(8, data8); + Assert.assertEquals(3, data3); + + } + + +} \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyIterator.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyIterator.java new file mode 100644 index 0000000000..9ecc4d3a7f --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyIterator.java @@ -0,0 +1,8 @@ +package com.coding2017.group7.homework.c0226; + +public interface MyIterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java new file mode 100644 index 0000000000..6b8a9ed197 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java @@ -0,0 +1,150 @@ +package com.coding2017.group7.homework.c0226; + +public class MyLinkedList implements MyList { + + private Node head = new Node(); + private int size = 0; + + @Override + public void add(Object o) { + Node node = new Node(); + node.data = o; + Node last = find(size - 1); + last.next = node; + size++; + } + + @Override + public void add(int index, Object o) { + checkRangeAdd(index); + Node insert = find(index); + Node before = find(index - 1); + Node node = new Node(); + node.data = o; + node.next = insert; + before.next = node; + size++; + } + + @Override + public Object get(int index) { + checkRangeGet(index); + return find(index).data; + } + + @Override + public Object remove(int index) { + checkRangeGet(index); + Node before = find(index - 1); + Node remove = find(index); + Node after = find(index + 1); + before.next = after; + size--; + return remove.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + node.next = find(0); + head.next = node; + size++; + } + + public void addLast(Object o) { + Node last = find(size - 1); + Node node = new Node(); + node.data = o; + last.next = node; + size++; + } + + public Object removeFirst() { + checkRangeGet(size - 1); + Node remove = find(0); + head.next = find(1); + size--; + return remove.data; + } + + public Object removeLast() { + checkRangeGet(size - 1); + Node remove = find(size - 1); + Node before = find(size - 2); + before.next = null; + size--; + return remove.data; + } + + public MyIterator iterator() { + return new MyLinkedListIterator(); + } + + private void checkRangeGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + } + } + + private void checkRangeAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + } + } + private Node find(int index) { + Node node = head; + int pos = -1; + while (pos < index) { + node = node.next; + pos++; + } + return node; + } + + @Override + public String toString() { + Node node = head; + StringBuilder sBuilder = new StringBuilder(); + while (node.next != null) { + node = node.next; + sBuilder.append(node.data).append(", "); + } + int length = sBuilder.length(); + if (length > 0) { + sBuilder.delete(length - 2, length); + } + return "[" + sBuilder.toString() + "]"; + } + + private static class Node { + Object data; + Node next; + + } + + + private class MyLinkedListIterator implements MyIterator { + + + private Node node = head; + private int size = 0; + + @Override + public boolean hasNext() { + return node.next != null; + } + + @Override + public Object next() { + checkRangeGet(size); + node = node.next; + size++; + return node.data; + } + } +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java new file mode 100644 index 0000000000..fe7d9ba28b --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java @@ -0,0 +1,108 @@ +package com.coding2017.group7.homework.c0226; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class MyLinkedListTest { + + private MyLinkedList myList = new MyLinkedList(); + private Object[] elements = new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + private final int mySize = elements.length; + + @Before + public void setUp() throws Exception { + for (int i = 0; i < mySize; i++) { + myList.add(i, i + 1); + } + } + + @After + public void tearDown() throws Exception { + for (int i = myList.size(); i > 0; i--) { + myList.remove(i - 1); + } + myList = null; + } + + @Test + public void add() throws Exception { + myList.add(-1); + Assert.assertEquals(myList.size(), mySize + 1); + } + + @Test + public void addIndex() throws Exception { + int pos = mySize / 2; + Object before = myList.get(pos); + myList.add(pos, -1); + Object after = myList.get(pos + 1); + Assert.assertEquals(myList.size(), mySize + 1); + Assert.assertEquals(before, after); + } + + @Test + public void remove() throws Exception { + myList.remove(0); + myList.remove(myList.size() - 1); + myList.remove(myList.size() / 2); + Assert.assertEquals(myList.size(), mySize - 3); + } + + @Test + public void addFirst() throws Exception { + myList.addFirst(-1); + myList.addFirst(-1); + myList.addFirst(-1); + Object o1 = myList.get(0); + Object o2 = myList.get(1); + Object o3 = myList.get(2); + Assert.assertTrue(o1.equals(o2)); + Assert.assertTrue(o2.equals(o3)); + Assert.assertTrue(o3.equals(-1)); + Assert.assertEquals(myList.size(), mySize + 3); + } + + @Test + public void addLast() throws Exception { + myList.addLast(-1); + myList.addLast(-1); + myList.addLast(-1); + Object o1 = myList.get(myList.size() - 1); + Object o2 = myList.get(myList.size() - 2); + Object o3 = myList.get(myList.size() - 3); + Assert.assertTrue(o1.equals(o2)); + Assert.assertTrue(o2.equals(o3)); + Assert.assertTrue(o3.equals(-1)); + Assert.assertEquals(myList.size(), mySize + 3); + } + + @Test + public void removeFirst() throws Exception { + myList.addFirst(-1); + Object o = myList.removeFirst(); + Assert.assertTrue(o.equals(-1)); + Assert.assertEquals(myList.size(), mySize); + } + + @Test + public void removeLast() throws Exception { + myList.addLast(-1); + Object o = myList.removeLast(); + Assert.assertTrue(o.equals(-1)); + Assert.assertEquals(myList.size(), mySize); + } + + @Test + public void iterator() throws Exception { + MyIterator iterator = myList.iterator(); + int count = 0; + while (iterator.hasNext()) { + System.out.println(iterator.next()); + count++; + } + Assert.assertEquals(mySize, count); + } + +} \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyList.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyList.java new file mode 100644 index 0000000000..7532313b89 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyList.java @@ -0,0 +1,13 @@ +package com.coding2017.group7.homework.c0226; + +public interface MyList { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueue.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueue.java new file mode 100644 index 0000000000..c701c032b8 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueue.java @@ -0,0 +1,35 @@ +package com.coding2017.group7.homework.c0226; + +import java.util.EmptyStackException; + +public class MyQueue { + + private MyLinkedList myLinkedList = new MyLinkedList(); + + public void enQueue(Object o) { + myLinkedList.add(0, o); + } + + public Object deQueue() { + if (isEmpty()) { + throw new EmptyQueueException(); + } + return myLinkedList.removeLast(); + } + + public boolean isEmpty() { + return myLinkedList.size() <= 0; + } + + public int size() { + return myLinkedList.size(); + } + + private static class EmptyQueueException extends EmptyStackException { + } + + @Override + public String toString() { + return myLinkedList.toString(); + } +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueueTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueueTest.java new file mode 100644 index 0000000000..6bcae88530 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyQueueTest.java @@ -0,0 +1,48 @@ +package com.coding2017.group7.homework.c0226; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class MyQueueTest { + + private MyQueue myQueue = new MyQueue(); + private final Object[] elements = {1, 2, 3}; + private final int mySize = elements.length; + + @Before + public void setUp() throws Exception { + for (int i = 0; i < mySize; i++) { + myQueue.enQueue(i + 1); + } + } + + @After + public void tearDown() throws Exception { + for (int i = myQueue.size(); i > 0; i--) { + myQueue.deQueue(); + } + } + + @Test + public void enQueue() throws Exception { + myQueue.enQueue(-1); + Object o = 0; + for (int i = myQueue.size(); i > 0; i--) { + o = myQueue.deQueue(); + } + Assert.assertTrue(o.equals(-1)); + } + + @Test + public void deQueue() throws Exception { + myQueue.enQueue(-1); + Object o = myQueue.deQueue(); + Assert.assertTrue(o.equals(elements[0])); + } + +} \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStack.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStack.java new file mode 100644 index 0000000000..18c10704a7 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStack.java @@ -0,0 +1,40 @@ +package com.coding2017.group7.homework.c0226; + +import java.util.EmptyStackException; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + private final int first = 0; + + public void push(Object o) { + + elementData.add(first, o); + } + + public Object pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(first); + } + + public Object peek() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.get(first); + } + + public boolean isEmpty() { + return elementData.size() <= 0; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java new file mode 100644 index 0000000000..d29031be60 --- /dev/null +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java @@ -0,0 +1,50 @@ +package com.coding2017.group7.homework.c0226; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class MyStackTest { + private MyStack myStack = new MyStack(); + private Object[] elements = new Object[]{1}; + private final int mySize = elements.length; + + @Before + public void setUp() throws Exception { + for (int i = 0; i < mySize; i++) { + myStack.push(i + 1); + } + } + + @After + public void tearDown() throws Exception { + for (int i = myStack.size(); i > 0; i--) { + myStack.pop(); + } + } + + @Test + public void push() throws Exception { + myStack.push(-1); + Assert.assertTrue(myStack.pop().equals(-1)); + } + + @Test + public void pop() throws Exception { + for (int i = myStack.size(); i > 0; i--) { + myStack.pop(); + } + Assert.assertEquals(myStack.size(), 0); + } + + @Test + public void peek() throws Exception { + for (int i = 0; i < myStack.size(); i++) { + Object peek = myStack.peek(); + Assert.assertTrue(peek.equals(elements[i])); + } + Assert.assertEquals(myStack.size(), mySize); + } + +} \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/resources/readme.md b/group07/562247675/homework/homework-0226/src/main/resources/readme.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group07/562247675/homework/homework-0226/src/test/resources/readme.md b/group07/562247675/homework/homework-0226/src/test/resources/readme.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group07/562247675/homework/pom.xml b/group07/562247675/homework/pom.xml new file mode 100644 index 0000000000..17a2c5f0fd --- /dev/null +++ b/group07/562247675/homework/pom.xml @@ -0,0 +1,53 @@ + + + + 4.0.0 + + com.coding2017.group7 + homework + 1.0-SNAPSHOT + + + homework-0226 + + + pom + + UTF-8 + 1.6 + 1.6 + + + + + junit + junit + 4.12 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + ${project.build.sourceEncoding} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + diff --git a/group07/562247675/homework/readme.md b/group07/562247675/homework/readme.md new file mode 100644 index 0000000000..2a54b7ef9a --- /dev/null +++ b/group07/562247675/homework/readme.md @@ -0,0 +1 @@ +# Homework \ No newline at end of file diff --git a/group07/562247675/notebook/readme.md b/group07/562247675/notebook/readme.md new file mode 100644 index 0000000000..d36f4b9c53 --- /dev/null +++ b/group07/562247675/notebook/readme.md @@ -0,0 +1 @@ +# Notebook \ No newline at end of file diff --git "a/group07/598812995/git\345\221\275\344\273\244.txt" "b/group07/598812995/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/598812995/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/603622009/git\345\221\275\344\273\244.txt" "b/group07/603622009/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/603622009/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/724319952/git\345\221\275\344\273\244.txt" "b/group07/724319952/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/724319952/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git "a/group07/752262774/git\345\221\275\344\273\244.txt" "b/group07/752262774/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/752262774/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/764189149/.classpath b/group07/764189149/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group07/764189149/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/764189149/.gitignore b/group07/764189149/.gitignore new file mode 100644 index 0000000000..8d9372e204 --- /dev/null +++ b/group07/764189149/.gitignore @@ -0,0 +1,23 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/bin/ diff --git a/group07/764189149/.project b/group07/764189149/.project new file mode 100644 index 0000000000..2076c6b51c --- /dev/null +++ b/group07/764189149/.project @@ -0,0 +1,17 @@ + + + 764189149Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/764189149/src/firstHomeWork/util/ArrayList.java b/group07/764189149/src/firstHomeWork/util/ArrayList.java new file mode 100644 index 0000000000..88b97d828f --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/ArrayList.java @@ -0,0 +1,197 @@ +package firstHomeWork.util; + +import java.util.NoSuchElementException; + +/** + * @Description: 鍩轰簬鏁扮粍鐨勫垪琛 + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍9:03:17 + * @param + */ +public class ArrayList implements List { + private static int initialCapacity = 10 ;//鏁扮粍榛樿鍒濆瀹归噺 + Object[] elements;//鍏冪礌鐨勬暟缁 + private int size;//鍏冪礌鐨勪釜鏁 + + public ArrayList(){ + this(initialCapacity); + } + public ArrayList(int capacity){ + elements = new Object[capacity]; + } + private void ensureCapacity(int minCapacity){ + if(minCapacity > 0){ + + } + } + @Override + public boolean add(E e) { + ensureCapacity(size + 1); + elements[size++] = e; + return true; + } + + @Override + public E remove(int index) { + rangeCheck(index); + E oldElement = (E) elements[index]; + //灏嗗叾鍚庣殑鍏冪礌鍓嶇Щ + int needMovedNum = size - index - 1; + move(elements, index+1, elements,index, needMovedNum); + size--; + return oldElement; + } + + /** + * @Description: 绉诲姩鏁扮粍涓殑鍏冪礌 + * @param src 鍘熸暟缁 + * @param from 澶嶅埗鍏冪礌璧峰涓嬫爣 + * @param dest 鐩爣鍏冪礌鏁扮粍 + * @param num 瑕佸鍒剁殑鍏冪礌涓暟 + * @return: void + * @author: leijing + * @date: 2017骞2鏈22鏃 涓嬪崍7:54:08 + */ + private void move(Object[] src , int srcPosition , Object[] dest , int destPosition, int num){ + for(int i = 0 ; i < num ; i ++){ + dest[destPosition++] = src[srcPosition++]; + } + } + + /** + * @Description: 妫鏌ヤ笅鏍囨槸鍚︽纭紝濡傛灉瓒婄晫鎶涘嚭寮傚父 + * @param index + * @return: void + * @author: leijing + * @date: 2017骞2鏈22鏃 涓嬪崍7:52:59 + */ + private void rangeCheck(int index){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + } + + @Override + public boolean remove(Object o) { + if(o == null){ + for (int index = 0; index < size; index++) { + if(elements[index] == null){ + remove(index); + return true; + } + } + }else{ + for (int index = 0; index < size; index++) { + if(o.equals(elements[index])){ + remove(index); + return true; + } + } + } + return false; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public E get(int index) { + rangeCheck(index); + return (E) elements[index]; + } + + @Override + public E set(int index, E e) { + rangeCheck(index); + E oldElement = (E) elements[index]; + elements[index] = e; + return oldElement; + } + + @Override + public boolean contains(Object o) { + return indexOf(o) >= 0; + } + + private int indexOf(Object o){ + if(o == null){ + for (int index = 0; index < size; index++) { + if(elements[index] == null){ + return index; + } + } + }else{ + for (int index = 0; index < size; index++) { + if(o.equals(elements[index])){ + return index; + } + } + } + return -1; + } + + @Override + public void clear() { + for (int index = 0; index < size; index++) { + elements[index] = null; + } + size = 0; + } + + @Override + public Iterator iterator() { + return new ArraylistIterator(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int index = 0; index < size; index++) { + if(index == size -1){ + sb.append(elements[index]); + }else{ + sb.append(elements[index]).append(","); + } + + } + return sb.toString(); + } + private class ArraylistIterator implements Iterator{ + private int position; + + @Override + public boolean hasNext() { + return position != size; + } + + @Override + public E next() { + Object[] elements = ArrayList.this.elements; + int i = position; + if(i >= size){ + throw new NoSuchElementException(); + } + position = i + 1; + return (E) elements[i+1]; + } + + @Override + public void remove() { + if(position > size){ + throw new NoSuchElementException(); + } + ArrayList.this.remove(position); + } + + } + + + +} diff --git a/group07/764189149/src/firstHomeWork/util/BinaryTreeNode.java b/group07/764189149/src/firstHomeWork/util/BinaryTreeNode.java new file mode 100644 index 0000000000..c7e85d4915 --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/BinaryTreeNode.java @@ -0,0 +1,31 @@ +package firstHomeWork.util; +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group07/764189149/src/firstHomeWork/util/DoubleLinkedList.java b/group07/764189149/src/firstHomeWork/util/DoubleLinkedList.java new file mode 100644 index 0000000000..9e83297a46 --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/DoubleLinkedList.java @@ -0,0 +1,335 @@ +package firstHomeWork.util; + +import java.util.NoSuchElementException; + +/** + * @Description: 鍙屽悜閾捐〃 + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:37:58 + * @param + */ +public class DoubleLinkedList { + + private int size;//鑺傜偣涓暟 + private Node head;//澶磋妭鐐 + private Node tail;//灏捐妭鐐 + + public Node getHead() { + return head; + } + public void setHead(Node head) { + this.head = head; + } + public Node getTail() { + return tail; + } + public void setTail(Node tail) { + this.tail = tail; + } + /** + * @Description: 娣诲姞鍏冪礌鍒板ご閮 + * @param e + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:38:20 + */ + public boolean addFirst(E e) { + Node node = new Node(e); + if(null == head){ + node.prev = null; + head = node; + tail = head; + }else{ + node.next = head; + head.prev = node; + head = node; + } + size++; + return true; + } + /** + * @Description: 娣诲姞鍏冪礌鍒板熬閮 + * @param e + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:38:20 + */ + public boolean addLast(E e) { + Node node = new Node(e); + if(null == tail){ + tail.next = null; + tail = node; + head = tail; + }else{ + tail.next = node; + node.prev = tail; + tail = node; + } + size++; + return true; + } + + public boolean remove(E o) throws Exception { + if(isEmpty()){ + throw new Exception("閾捐〃涓虹┖锛屾病鏈夊厓绱犲彲浠ュ垹闄"); + } + Node current = head;//浠庡ご鑺傜偣寮濮嬪垹 + if(o == null){ + while(current != null){ + if(current.data == null){ + current.prev.next = current.next;//灏嗗綋鍓嶈妭鐐圭殑鍓嶉┍鑺傜偣鐨勫悗缁ц妭鐐规敼涓哄綋鍓嶈妭鐐圭殑鍚庣户 + current.next.prev = current.prev;//灏嗗綋鍓嶈妭鐐瑰悗缁ц妭鐐圭殑鍓嶉┍鑺傜偣鏀逛负褰撳墠鑺傜偣鐨勫墠椹辫妭鐐 + current.next = null;//褰撳墠鑺傜偣鐨勫墠椹辨敼涓簄ull + current.prev = null;//褰撳墠鑺傜偣鐨勫悗缁ф敼涓簄ull + size--; + return true; + } + current = current.next; + } + }else{ + while(current != null){ + if(o.equals(current.data)){ + current.prev.next = current.next;//灏嗗綋鍓嶈妭鐐圭殑鍓嶉┍鑺傜偣鐨勫悗缁ц妭鐐规敼涓哄綋鍓嶈妭鐐圭殑鍚庣户 + current.next.prev = current.prev;//灏嗗綋鍓嶈妭鐐瑰悗缁ц妭鐐圭殑鍓嶉┍鑺傜偣鏀逛负褰撳墠鑺傜偣鐨勫墠椹辫妭鐐 + current.next = null;//褰撳墠鑺傜偣鐨勫墠椹辨敼涓簄ull + current.prev = null;//褰撳墠鑺傜偣鐨勫悗缁ф敼涓簄ull + size--; + return true; + } + current = current.next; + } + } + + return false; + } + + /** + * @Description: 杩斿洖鍏冪礌涓暟 + * @return: int + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:38:20 + */ + public int size() { + return size; + } + /** + * @Description: 鏄惁绌洪摼琛 + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:38:20 + */ + public boolean isEmpty() { + return size == 0; + } + /** + * @Description: 妫鏌ヤ笅鏍囨湁鏁堟 + * @param index + * @return: void + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:40:15 + */ + private void rangeCheck(int index){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + } + public E get(int index) { + rangeCheck(index); + Node current = head;//浠庡ご鑺傜偣寮濮 + int i = 0; + while(current != null){ + if(i == index){ + return current.data; + } + current = current.next; + i++; + } + return null; + } + public Node node(int index) { + rangeCheck(index); + if(index < size >> 1){//灏忎簬鍏冪礌澶у皬鐨勪簩鍒嗕箣涓锛屼粠澶磋妭鐐瑰紑濮嬮亶鍘 + Node current = head;//浠庡ご鑺傜偣寮濮 + for(int i = 0 ; i < index ; i++){ + current = current.next; + } + return (Node)current; + }else{//浠庡熬鑺傜偣寮濮嬮亶鍘 + Node current = tail;//浠庡熬鑺傜偣寮濮 + for(int i = 0 ; i < index ; i++){ + current = current.prev; + } + return (Node)current; + } + } + /** + * @Description: 璁剧疆鏌愪釜浣嶇疆鐨勫厓绱 + * @param index + * @param data + * @return: E + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:58:32 + */ + public E set(int index, E data) { + rangeCheck(index); + Node current = head;//浠庡ご鑺傜偣寮濮 + int i = 0; + while(current != null){ + if(i == index){ + Node node = new Node(data); + Node prev = current.prev; + prev.next = node; + node.prev = prev; + node.next = current; + current.prev = node; + size++; + return data; + } + current = current.next; + i++; + } + return null; + } + /** + * @Description: 鍒ゆ柇鏄惁鍖呭惈鏌愪釜鍏冪礌 + * @param o + * @throws Exception + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈24鏃 涓婂崍11:57:35 + */ + public boolean contains(Object o) throws Exception { + if(isEmpty()){ + throw new Exception("閾捐〃涓虹┖锛屾病鏈変换浣曞厓绱"); + } + Node current = head;//浠庡ご鑺傜偣寮濮嬫壘 + if(o == null){ + while(current != null){ + if(current.data == null){ + return true; + } + current = current.next; + } + }else{ + while(current != null){ + if(o.equals(current.data)){ + return true; + } + current = current.next; + } + } + return false; + } + /** + * @Description: 娓呯┖閾捐〃锛屽垹闄ゆ墍鏈夊厓绱 + * @return: void + * @author: leijing + * @date: 2017骞2鏈24鏃 涓嬪崍4:41:56 + */ + public void clear() { + Node current = head;//浠庡ご鑺傜偣寮濮嬮亶鍘 + while(current != null){ + Node tmp = current; + current = current.next; + tmp.prev = null; + tmp.next = null; + } + size = 0; + } + + public Iterator iterator() { + return new ListItr(0); + } + private class ListItr implements Iterator{ + private Node lastReturned = null;//褰撳墠鐨勮妭鐐 + private Node next;//涓嬩竴涓妭鐐 + private int nextIndex;//褰撳墠绱㈠紩鐨勪笅鏍 + + public ListItr(int nextIndex){ + next = (nextIndex == size) ? null : node(nextIndex); + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public E next() { + if (!hasNext()){ + throw new NoSuchElementException(); + } + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + @Override + public void remove() { + if (lastReturned == null){ + throw new IllegalStateException(); + } + + if(lastReturned == next){//tail node + lastReturned.prev.next = null; + lastReturned.prev = null; + }else{ + lastReturned.prev.next = lastReturned.next; + lastReturned.next.prev = lastReturned.prev; + lastReturned.next = null; + lastReturned.prev = null; + } + nextIndex--; + } + + public boolean hasPrev(){ + return nextIndex > 0; + } + + public E prev(){ + if(!hasPrev()){ + throw new NoSuchElementException(); + } + next = lastReturned = (next == null ) ? tail : next.prev;//濡傛灉鏄ご鑺傜偣锛屽墠涓涓寚鍚戝熬鑺傜偣 + nextIndex--; + return lastReturned.data; + } + + } + + static class Node{ + private E data; + private Node prev;//鍓嶉┍鑺傜偣 + private Node next;//鍚庣户鑺傜偣 + + public Node(E data){ + this.data = data; + } + + public E getData() { + return data; + } + + public void setData(E data) { + this.data = data; + } + + public Node getPrev() { + return prev; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + } +} diff --git a/group07/764189149/src/firstHomeWork/util/Iterator.java b/group07/764189149/src/firstHomeWork/util/Iterator.java new file mode 100644 index 0000000000..ecbf3015e0 --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/Iterator.java @@ -0,0 +1,30 @@ +package firstHomeWork.util; + +/** + * @Description: 杩唬鍣 + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:49:10 + * @param + */ +public interface Iterator { + /** + * @Description: 杩斿洖杩唬鍣ㄤ腑鏄惁鏈変笅涓涓厓绱 + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:49:52 + */ + boolean hasNext(); + /** + * @Description: 杩斿洖杩唬鍣ㄤ腑鐨勪笅涓涓厓绱 + * @return: E + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:50:35 + */ + E next(); + /** + * @Description: 鍒犻櫎杩唬鍣ㄤ腑鐨勫綋鍓嶅厓绱 + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:51:07 + */ + void remove(); +} diff --git a/group07/764189149/src/firstHomeWork/util/List.java b/group07/764189149/src/firstHomeWork/util/List.java new file mode 100644 index 0000000000..fac6efa0cc --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/List.java @@ -0,0 +1,87 @@ +package firstHomeWork.util; + +/** + * @Description: 瀹氫箟涓缁勬搷浣滄湁搴忓垪琛ㄧ殑鎺ュ彛 + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:53:52 + * @param + */ +public interface List { + /** + * @Description: 娣诲姞鍏冪礌 + * @param e + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:55:32 + */ + boolean add(E e); + /** + * @Description: 鍒犻櫎鎸囧畾绱㈠紩鐨勫厓绱 + * @param index + * @return: E + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:56:08 + */ + E remove(int index); + /** + * @Description: 鍒犻櫎鍏冪礌 + * @param o + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:56:28 + */ + boolean remove(Object o); + /** + * @Description: 杩斿洖鍏冪礌涓暟 + * @return: int + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:57:25 + */ + int size(); + /** + * @Description: 鍒ゆ柇闆嗗悎鏄惁涓虹┖ + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:57:51 + */ + boolean isEmpty(); + /** + * @Description: 鑾峰彇鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @return: E + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:58:27 + */ + E get(int index); + /** + * @Description: 璁剧疆鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @param e + * @return: E + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:58:58 + */ + E set(int index , E e); + /** + * @Description: 鍒ゆ柇闆嗗悎鏄惁鍖呭惈鏌愪釜鍏冪礌 + * @param o + * @return: boolean + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍8:59:32 + */ + boolean contains(Object o); + /** + * @Description: 娓呯┖闆嗗悎 + * @return: void + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍9:00:12 + */ + void clear(); + /** + * @Description: 鑾峰彇闆嗗悎鐨勮凯浠e櫒 + * @return: Iterator + * @author: leijing + * @date: 2017骞2鏈21鏃 涓嬪崍9:00:47 + */ + Iterator iterator(); +} diff --git a/group07/764189149/src/firstHomeWork/util/Queue.java b/group07/764189149/src/firstHomeWork/util/Queue.java new file mode 100644 index 0000000000..78185c3ae8 --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/Queue.java @@ -0,0 +1,59 @@ +package firstHomeWork.util; + +/** + * @Description: 寰幆闃熷垪 + * @author: leijing + * @date: 2017骞2鏈24鏃 涓嬪崍9:00:30 + * @param + */ +public class Queue { + private Object[] queue; + private int capacity; + private static int INITIAL_CAPACITY = 10; + private int front; //闃熷ご + private int rear; //闃熷熬 + + public Queue(int capacity){ + this.capacity = capacity; + this.front = 0; + this.rear = 0; + queue = new Object[capacity]; + } + public Queue(){ + this(INITIAL_CAPACITY); + } + + public boolean enQueue(E e) throws Exception{ + if(isFull()){ + return false; + } + if(rear == capacity - 1){//寰幆鍒╃敤 + rear = 0; + } + queue[rear++] = e; + return true; + } + + public E deQueue(){ + if(isEmpty()){ + return null; + } + if(front == capacity - 1){ + front = 0; + } + + return (E) queue[front++]; + } + + public boolean isEmpty(){ + return front == rear; + } + + public int size(){ + return Math.abs(rear - front) + 1; + } + + public boolean isFull(){ + return (rear + 1) % capacity == front; + } +} diff --git a/group07/764189149/src/firstHomeWork/util/Stack.java b/group07/764189149/src/firstHomeWork/util/Stack.java new file mode 100644 index 0000000000..00dc784170 --- /dev/null +++ b/group07/764189149/src/firstHomeWork/util/Stack.java @@ -0,0 +1,23 @@ +package firstHomeWork.util; + +import java.util.Queue; + +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/group07/770164810/git\345\221\275\344\273\244.txt" "b/group07/770164810/git\345\221\275\344\273\244.txt" new file mode 100644 index 0000000000..8bcf2ffa0f --- /dev/null +++ "b/group07/770164810/git\345\221\275\344\273\244.txt" @@ -0,0 +1,11 @@ +安装git gui软件,打开git bash操作 +1.克隆 +git clone git@github.com:leijing1992/coding2017.git +2.添加修改的文件 +git add -A +3.提交到暂存区 +git commit -m "提交***代码" +4.更新master +git pull origin master +5.提交到master +git push origin master \ No newline at end of file diff --git a/group07/770164810/test.txt b/group07/770164810/test.txt new file mode 100644 index 0000000000..7c4a013e52 --- /dev/null +++ b/group07/770164810/test.txt @@ -0,0 +1 @@ +aaa \ No newline at end of file diff --git a/group07/group07.md b/group07/group07.md index 8b13789179..d3f5a12faa 100644 --- a/group07/group07.md +++ b/group07/group07.md @@ -1 +1 @@ - + diff --git a/group08/108621969/2-26/com/coding/basic/ArrayList.java b/group08/108621969/2-26/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a06488acb9 --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/ArrayList.java @@ -0,0 +1,119 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public class ArrayList implements List { + private int size = 0; + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + if(isFull()) { + elementData = expandArray(elementData); + } + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + ifOutOfBounds(index); + if(isFull()) { + elementData = expandArray(elementData); + } + System.arraycopy(elementData,index,elementData,index+1,size++); + elementData[index] = o; + } + + @Override + public Object get(int index) { + ifOutOfBounds(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + ifOutOfBounds(index); + Object delData = elementData[index]; + System.arraycopy(elementData,index+1,elementData,index,size-index); + size--; + return index; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + String str = ""; + for(int i=0; i<=size-1; i++){ + str += elementData[i] + " "; + } + return str; + } + + private boolean isFull() { + if(size >= elementData.length-1){ + return true; + } + return false; + } + + public boolean isEmpty() { + if(size == 0) { + return true; + } + return false; + } + + private void ifOutOfBounds(int index) { + if(index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + private Object[] expandArray(Object[] arr) { + return Arrays.copyOf(arr, arr.length+10); + } + + public Iterator iterator = new ArrayListIterator(this); + + private class ArrayListIterator implements Iterator{ + + private ArrayList arrList; + int position = 0,length; + + private ArrayListIterator(ArrayList arrList){ + this.arrList = arrList; + this.length = arrList.size(); + } + + @Override + public boolean hasNext() { + return position < length; + } + + @Override + public Object next() { + return arrList.get(position++); + } + + } + + public static void main(String[] args) { + ArrayList arr = new ArrayList(); + arr.add(1); + arr.add(2); + arr.add(3); + arr.add(1,4); + arr.remove(2); + System.out.println(arr.toString()); + System.out.println(arr.size()); + System.out.println(arr.iterator.next()); + System.out.println(arr.iterator.next()); + } +} diff --git a/group08/108621969/2-26/com/coding/basic/BinaryTreeNode.java b/group08/108621969/2-26/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..f86efd62f5 --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + this.data = o; + this.left = null; + this.right = null; + } + + public int getData() { + return (int) data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode newNode = new BinaryTreeNode(o); + insertInto(this, newNode); + return this; + } + + private void insertInto(BinaryTreeNode tree, BinaryTreeNode o) { + if (o.getData() <= tree.getData()) { + if (tree.getLeft() != null) insertInto(tree.getLeft(), o); + else tree.setLeft(o); + } else { + if (tree.getRight() != null) insertInto(tree.getRight(), o); + else tree.setRight(o); + } + } +} diff --git a/group08/108621969/2-26/com/coding/basic/Iterator.java b/group08/108621969/2-26/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ae5e36f464 --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group08/108621969/2-26/com/coding/basic/LinkedList.java b/group08/108621969/2-26/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..dadf8caeaf --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/LinkedList.java @@ -0,0 +1,174 @@ +package com.coding.basic; + +import java.util.Objects; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public class LinkedList implements List { + private Node head = null,tail = null; + private int size=0; + private LinkedListIterator iterator = null; + + @Override + public void add(Object o) { + Node newNode = new Node(o); + if(tail == null) { + head = tail = newNode; + }else { + tail.next = newNode; + tail = newNode; + } + size++; + } + + @Override + public void add(int index, Object o) { + ifOutOfBounds(index); + Node temp = head,previousNode = head; + Node newNode = new Node(o); + if(index == 0) { + newNode.next = head; + head = newNode; + return ; + } + while(index-- > 0) { + previousNode = temp; + temp = temp.next; + } + previousNode.next = newNode; + newNode.next = temp; + size++; + } + + @Override + public Object get(int index) { + ifOutOfBounds(index); + Node temp = head; + while(index-- > 0) { + temp = temp.next; + } + return temp; + } + + @Override + public Object remove(int index) { + ifOutOfBounds(index); + Node temp = head,previousNode = head; + if(head == tail) { + head = tail = null; + return temp; + } + while(index-- > 0) { + previousNode = temp; + temp = temp.next; + } + if(tail == temp) { + tail = previousNode; + } + previousNode.next = temp.next; + size--; + return temp; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0,o); + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size()-1); + } + + public LinkedListIterator Iterator() { + if(iterator ==null) { + iterator = new LinkedListIterator(this); + } + return iterator; + } + + private void ifOutOfBounds(int index) { + if(index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + private boolean isEmpty() { + return size==0; + } + + public String toString() { + String str = ""; + Node temp = head; + while(temp!=tail) { + str = str + temp.data + " "; + temp = temp.next; + } + return str + tail.data; + } + + private static class Node { + Object data; + Node next; + public Node(Object o) { + this.data = o; + this.next = null; + } + } + + private class LinkedListIterator implements Iterator { + private LinkedList linkedList = null; + private Node currentNode = null; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + this.currentNode = this.linkedList.head; + } + + @Override + public boolean hasNext() { + if(linkedList.isEmpty()) return false; + return currentNode.next != null; + } + + @Override + public Object next() { + Object data = currentNode.data; + if(hasNext()) { + currentNode = currentNode.next; + } + return data; + } + } + + public static void main(String[] args) { + LinkedList arr = new LinkedList(); + arr.add(1); + arr.add(2); + arr.add(3); + arr.add(5); + arr.add(1,4); + arr.remove(2); + arr.removeLast(); + arr.addFirst(5); + System.out.println(arr.toString()); + System.out.println(arr.size()); + System.out.println(arr.Iterator().hasNext()); + System.out.println(arr.Iterator().next()); + System.out.println(arr.Iterator().next()); + System.out.println(arr.Iterator().next()); + System.out.println(arr.Iterator().next()); + } +} diff --git a/group08/108621969/2-26/com/coding/basic/List.java b/group08/108621969/2-26/com/coding/basic/List.java new file mode 100644 index 0000000000..30a4b4e7cb --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/List.java @@ -0,0 +1,12 @@ +package com.coding.basic; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/108621969/2-26/com/coding/basic/Queue.java b/group08/108621969/2-26/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fd087cbd9b --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/Queue.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o) { + elementData.add(0, o); + } + + public Object deQueue() { + return elementData.remove(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } + + public static void main(String[] args) { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + System.out.println(queue.deQueue()); + System.out.println(queue.toString()); + } +} diff --git a/group08/108621969/2-26/com/coding/basic/Stack.java b/group08/108621969/2-26/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d600d86fe5 --- /dev/null +++ b/group08/108621969/2-26/com/coding/basic/Stack.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import java.util.ArrayList; + + +/** + * Created by zhangjiatao on 2017/2/25. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(elementData.size(), 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(); + } + + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.pop()); + System.out.println(stack.toString()); + } +} diff --git a/group08/108621969/README.md b/group08/108621969/README.md new file mode 100644 index 0000000000..413f53e93a --- /dev/null +++ b/group08/108621969/README.md @@ -0,0 +1,7 @@ +# About Me +1. 濮撳悕: 寮犲姞娑 +2. 鎬у埆: 鐢 +3. 鐘舵: 瀹炰範 +4. 骞撮緞: 22 +5. 鑱屼笟: web鍓嶇 +6. 鍧愭爣: 涓婃捣娴︿笢 diff --git a/group08/108621969/article/Comp-Units-Relations(2-16).md b/group08/108621969/article/Comp-Units-Relations(2-16).md new file mode 100644 index 0000000000..b3fdcbb61b --- /dev/null +++ b/group08/108621969/article/Comp-Units-Relations(2-16).md @@ -0,0 +1,14 @@ +# CPU +鐢卞瘎瀛樺櫒銆佽繍绠楀櫒銆佹帶鍒跺櫒缁勬垚 +杩樻湁涓绾х紦瀛樸佷簩绾х紦瀛樼瓑 +# 鍐呭瓨 +鎴戜滑鎵璇寸殑鍐呭瓨閮芥槸绯荤粺鍐呭瓨锛屾槸SDRAM锛屽姩鎬侀殢鏈哄瓨鍌ㄥ櫒 +閫熷害蹇閲忓皬 +# 纭洏 +瀹归噺澶ч熷害鎱 +# 鎸囦护 +绋嬪簭鍦ㄨ绠楁満涓鍒嗚В涓轰竴鏉℃潯鐨勬寚浠 +# 鍏崇郴 +绋嬪簭鏂囦欢鍦ㄧ‖鐩樹腑锛屽綋璋冪敤鏃讹紝浠庣‖鐩樿浇鍏ュ唴瀛橈紝 +鎺у埗鍣ㄤ粠鍐呭瓨涓鍙栫▼搴忕殑绗竴鏉℃寚浠わ紝鎸囦护閫氱煡杩愮畻鍣ㄨ鍙栧唴瀛樹腑鐨勬暟鎹殑鍦板潃锛 +杩愮畻鍣ㄧ粨鍚堝瘎瀛樺櫒璁$畻鍑虹粨鏋滐紝璇诲彇涓嬩竴鏉℃寚浠わ紝鐩村埌鎸囦护缁撴潫銆 diff --git a/group08/1144989424/Readme.md b/group08/1144989424/Readme.md new file mode 100644 index 0000000000..3a515bb88b --- /dev/null +++ b/group08/1144989424/Readme.md @@ -0,0 +1,3 @@ +### 鐩綍璇存槑 + +涓汉鐩綍锛屼粬浜哄嬁鍔 diff --git a/group08/1144989424/firstPractice/readme.md b/group08/1144989424/firstPractice/readme.md new file mode 100644 index 0000000000..ac4feb57b8 --- /dev/null +++ b/group08/1144989424/firstPractice/readme.md @@ -0,0 +1,4 @@ +### 绗竴娆′綔涓氾細 +1. 瀹炵幇ArrayList, LinkedList, Queue, Stack +2. 鏈変綑鍔涚殑鍚屽鍙互瀹炵幇 BinaryTree 鍜孖terator +3. 鍐欎竴绡囨枃绔狅紝寤鸿: 鎻忚堪CPU锛屽唴瀛橈紝 纭洏锛屾寚浠や箣闂寸殑鍏崇郴銆 \ No newline at end of file diff --git a/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java b/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6300e92259 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package basic; + +/** + * 浜屽弶鏍戠殑鏁版嵁缁撴瀯 + * @author Wayss + * 2017-02-25 + */ + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(Integer val){ + data = val; + left = null; + right = null; + } + + + 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/group08/1144989424/firstPractice/src/basic/MyArrayList.java b/group08/1144989424/firstPractice/src/basic/MyArrayList.java new file mode 100644 index 0000000000..f9cf5b1b80 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyArrayList.java @@ -0,0 +1,97 @@ +package basic; + +/** + * 鎴戠殑ArrayList瀹炵幇 + * @author Wayss + * 2017-02-22 + */ + +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + /** + * 寰闆嗗悎涓坊鍔犱竴涓厓绱 + */ + public void add(Object o){ + int length = elementData.length; + //1.姣旇緝鍏冪礌涓暟鍜屾暟缁勫ぇ灏忥紝鍒ゆ柇鏄惁闇瑕佹墿澶ф暟缁 + if(size == length){ + expandArray(); + } + //2.鐩存帴璧嬪肩粰size锛屽嵆鏈鍚庝竴涓厓绱 + elementData[size] = o; + size++; + } + + /** + * 寰闆嗗悎鎸囧畾浣嶇疆娣诲姞涓涓厓绱狅紝璇ヤ綅缃拰鍏朵箣鍚庣殑鍏冪礌鍚戝悗绉诲姩涓浣嶃 + * 浣嶇疆涓嶅悎娉曟椂锛屾姏鍑哄紓甯搞 + */ + public void add(int index, Object o){ + int length = elementData.length; + //0.鍏堝index鐨勫艰繘琛屽垽鏂紝灏忎簬0锛屾垨鑰咃紝澶т簬size锛岃秺鐣 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //1.姣旇緝鍏冪礌涓暟鍜屾暟缁勫ぇ灏忥紝鍒ゆ柇鏄惁闇瑕佹墿澶ф暟缁 + if(size == length){ + expandArray(); + } + //2.绉诲姩index涔嬪悗鐨勬暟缁勫厓绱 + for(int i = size; i > index; i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + int length = elementData.length; + //0.鍏堝index鐨勫艰繘琛屽垽鏂紝灏忎簬0锛屾垨鑰咃紝澶т簬绛変簬size锛屼究瓒婄晫 + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //2.鎷垮嚭index浣嶇疆鐨勫硷紝 + Object o = elementData[index]; + + return o; + } + + public Object remove(int index){ + //remove 鍓嶄袱姝ョ殑閫昏緫鍜実et鏂规硶鐩稿悓 + int length = elementData.length; + //0.鍏堝index鐨勫艰繘琛屽垽鏂紝灏忎簬0锛屾垨鑰咃紝澶т簬绛変簬size锛屼究瓒婄晫 + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //2.鎷垮嚭index浣嶇疆鐨勫硷紝 + Object o = elementData[index]; + + //3.鍒犻櫎index浣嶇疆鐨勫硷紝涔嬪悗鐨勫硷紝鍚戝墠绉诲姩銆 + for(int i = index; i < size-1; i++){ + elementData[i] = elementData[i+1]; + } + size--; + return null; + } + + public int size(){ + return size; + } + + public MyIterator iterator(){ + return null; + } + + private void expandArray(){ + int length = elementData.length; + Object [] newArr = new Object[length * 2]; + for(int i = 0; i < length; i++){ + newArr[i] = elementData[i]; + } + elementData = newArr; + } +} diff --git a/group08/1144989424/firstPractice/src/basic/MyIterator.java b/group08/1144989424/firstPractice/src/basic/MyIterator.java new file mode 100644 index 0000000000..2aa1de62c4 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyIterator.java @@ -0,0 +1,7 @@ +package basic; + +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group08/1144989424/firstPractice/src/basic/MyLinkedList.java b/group08/1144989424/firstPractice/src/basic/MyLinkedList.java new file mode 100644 index 0000000000..3894884373 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyLinkedList.java @@ -0,0 +1,113 @@ +package basic; + +/** + * 瀹炵幇LinkedList鍩烘湰鍔熻兘 + * @author Wayss + * 2017-02-23 + */ + +public class MyLinkedList implements MyList { + + private Node head; + private int size = 0; + + public void add(Object o){ + Node n = new Node(o); + head.next = n; + size++; + } + public void add(int index , Object o){ + //1.index鏍¢獙 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //2. 鏌ユ壘index浣嶇疆鐨勫墠涓涓妭鐐 + //tempNode涓哄綋鍓嶉摼琛ㄧ殑绗竴涓妭鐐 + Node tempNode = head.next; + for(int i = 0; i < index - 1 ; i++){ + tempNode = tempNode.next; + } + Node behindNode = tempNode.next; + Node insertNode = new Node(o); + tempNode.next = insertNode; + insertNode.next = behindNode; + size++; + } + public Object get(int index){ + //1.index鏍¢獙 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //2. 鏌ユ壘褰撳墠鑺傜偣 + Node tempNode = head.next; + for(int i = 0; i < index; i++){ + tempNode = tempNode.next; + } + return tempNode.data; + } + public Object remove(int index){ + //1.index鏍¢獙 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("鎻掑叆鐨勪笅鏍囪秺鐣屼簡:"+"鎻掑叆鐨勪笅鏍囦负锛"+index+"闆嗗悎澶у皬涓猴細"+size); + } + //2. 鏌ユ壘褰撳墠鑺傜偣鐨勪笂涓涓妭鐐 + Node tempNode = head.next; + for(int i = 0; i < index - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + Node behideNode = tempNode.next.next; + tempNode.next = behideNode; + size--; + return deleteNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node insertNode = new Node(o); + insertNode.next = head.next; + head.next = insertNode; + size++; + } + public void addLast(Object o){ + Node insertNode = new Node(o); + Node tempNode = head.next; + for(int i = 0; i < size; i++){ + tempNode = tempNode.next; + } + tempNode.next = insertNode; + size++; + } + public Object removeFirst(){ + Node firstNode = head.next; + head = firstNode.next; + size--; + return firstNode; + } + public Object removeLast(){ + Node tempNode = head.next; + //1.绉婚櫎闇瑕佹壘鍒版渶鍚庝竴涓偣鐨勫墠涓涓偣 + for(int i = 0; i < size - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + tempNode.next = null; + size--; + return deleteNode; + } + public MyIterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object data){ + this.data = data; + } + } +} diff --git a/group08/1144989424/firstPractice/src/basic/MyList.java b/group08/1144989424/firstPractice/src/basic/MyList.java new file mode 100644 index 0000000000..c017431b22 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyList.java @@ -0,0 +1,9 @@ +package basic; + +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/1144989424/firstPractice/src/basic/MyQueue.java b/group08/1144989424/firstPractice/src/basic/MyQueue.java new file mode 100644 index 0000000000..31fab53105 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyQueue.java @@ -0,0 +1,31 @@ +package basic; + +/** + * 瀹炵幇闃熷垪 + * @author Wayss + * 2017-02-25 + */ + +public class MyQueue { + + MyLinkedList linkList = new MyLinkedList(); + + public void enQueue(Object o){ + linkList.addLast(o); + } + + public Object deQueue(){ + return linkList.removeFirst(); + } + + public boolean isEmpty(){ + if(linkList.size() == 0){ + return true; + } + return false; + } + + public int size(){ + return linkList.size(); + } +} diff --git a/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java b/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java new file mode 100644 index 0000000000..a4579217e5 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java @@ -0,0 +1,81 @@ +package basic; + +import java.util.Comparator; + +/** + * 瀹炵幇浜屽弶鎺掑簭鏍戯紝浣垮乏鑺傜偣鐨勫硷紝濮嬬粓鐖惰妭鐐瑰皬锛屽彸鑺傜偣鐨勫硷紝濮嬬粓姣旂埗鑺傜偣澶 + * @author Wayss + * 2017-02-25 + */ + +public class MySortBinaryTree { + + public BinaryTreeNode root = new BinaryTreeNode(); + + /** + * 1. 娣诲姞鏃讹紝鍏堝垽鏂笌root鑺傜偣鍊肩殑澶у皬 + * 2. insert鐨勫煎皬浜巖oot鐨勫硷紝鍒欐彃鍏ュ埌root鐨勫乏杈广 + * 3. insert鐨勫煎ぇ浜巖oot鐨勫硷紝鍒欐彃鍏ュ埌root鐨勫彸杈广 + * PS:鐩墠鍙敮鎸両nteger绫诲瀷鐨勫璞℃彃鍏 + * @param val + */ + public void add(Integer val){ + BinaryTreeNode treeNode = new BinaryTreeNode(val); + insert(root, treeNode); + } + + private void insert(BinaryTreeNode node, BinaryTreeNode insertNode){ + + int result = compare((Integer)insertNode.getData(),(Integer)node.getData()); + + if(0 == result){ + //鐩哥瓑鐨勮瘽锛屽綋閲嶅鏁版嵁锛屼笉鎻掍簡.鍛靛懙 + } + //insert鐨勫煎皬浜巖oot鐨勫硷紝鍒欐彃鍏ュ埌root鐨勫乏杈广 + //濡傛灉宸﹁妭鐐规湁鍊硷紝鍒欓掑綊 + if(-1 == result){ + if(node.getLeft() != null){ + insert(node.getLeft(), insertNode); + }else{ + node.setLeft(insertNode); + } + } + //insert鐨勫煎ぇ浜巖oot鐨勫硷紝鍒欐彃鍏ュ埌root鐨勫彸杈广 + if(1 == result){ + if(node.getRight() != null){ + insert(node.getRight(), insertNode); + }else{ + node.setRight(insertNode); + } + } + } + + public MyArrayList getValue(){ + MyArrayList malist = new MyArrayList(); + getTreeValue(root,malist); + return malist; + } + + private void getTreeValue(BinaryTreeNode node,MyArrayList list){ + //閬嶅巻宸﹀瓙鏁 + if(node.getLeft() != null){ + getTreeValue(node.getLeft(),list); + } + list.add(node.getData()); + //閬嶅巻鍙冲瓙鏁 + if(node.getRight() != null){ + getTreeValue(node.getRight(),list); + } + } + + public static int compare(Integer i1, Integer i2){ + if(i1 < i2){ + return -1; + }else if(i1 == i2){ + return 0; + }else { + return 1; + } + } + +} diff --git a/group08/1144989424/firstPractice/src/basic/MyStack.java b/group08/1144989424/firstPractice/src/basic/MyStack.java new file mode 100644 index 0000000000..c8e8e0d2fd --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MyStack.java @@ -0,0 +1,33 @@ +package basic; + +/** + * 鏍堝疄鐜 + * @author Wayss + * 2017-02-25 + */ + +public class MyStack { + private MyArrayList arrList = new MyArrayList(); + + public void push(Object o){ + arrList.add(o); + } + + public Object pop(){ + //elementData.size()-1鏄綋鍓嶆暟缁勭殑鏈鍚庝竴涓厓绱犵殑涓嬫爣 + return arrList.remove(arrList.size() - 1); + } + + public Object peek(){ + return arrList.get(arrList.size() - 1); + } + public boolean isEmpty(){ + if(arrList.size() == 0){ + return true; + } + return false; + } + public int size(){ + return arrList.size(); + } +} diff --git a/group08/121027265/README.md b/group08/121027265/README.md new file mode 100644 index 0000000000..488916d101 --- /dev/null +++ b/group08/121027265/README.md @@ -0,0 +1,3 @@ +# 鏄爣棰 +* aaa +* bbb diff --git a/group08/1226637491/2_26/ArrayList.java b/group08/1226637491/2_26/ArrayList.java new file mode 100644 index 0000000000..1d4aaf7bc4 --- /dev/null +++ b/group08/1226637491/2_26/ArrayList.java @@ -0,0 +1,95 @@ +interface MyIterator{ + public boolean hasNext(); + public Object next(); + +} + + +public class ArrayList { +/* public static void main(String[] d){ + ArrayList p = new ArrayList(); + p.add("asd");p.add("123");p.add("123");p.add("234");p.add("456"); + p.remove(1); + p.add(1, 345); + MyIterator it = p.iterator(); + while(it.hasNext()){ + System.out.println(it.next().toString()); + } + } +*/ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size >= elementData.length){//扩充数组长度 + Object[] tem = new Object[elementData.length*2]; + for(int i=0;i= elementData.length){ + Object[] tem = new Object[elementData.length*2]; + for(int i=0;i=size){ + throw new IndexOutOfBoundsException(); + } + for(int i = index; i < size; i++){ + elementData[i-1] = elementData[i]; + } + size--; + return elementData[index]; + } + + public int size(){ + return size; + + } + + + class MMIt implements MyIterator{ + int loc = 0; + + public boolean hasNext() { + if(loc>=size) + return false; + else + return true; + } + + + public Object next() { + ++loc; + return elementData[loc-1]; + } + + } + + + public MyIterator iterator(){ + return new MMIt(); + } + +} \ No newline at end of file diff --git a/group08/1226637491/2_26/LinkedList.java b/group08/1226637491/2_26/LinkedList.java new file mode 100644 index 0000000000..4534272573 --- /dev/null +++ b/group08/1226637491/2_26/LinkedList.java @@ -0,0 +1,127 @@ + + +public class LinkedList { + + private Node head = new Node(); + private int size = 0; + + public void add(Object o){ + Node tem = new Node(); + tem.data = o; + tem.next = null; + Node temp = head; + int i = 0; + while(isize+1) + throw new IndexOutOfBoundsException(); + Node tem = new Node(); + tem.data = o; + tem.next = null; + ++size; + Node temp = head; + int i = 0; + while(isize+1) + throw new IndexOutOfBoundsException(); + --size; + Node temp = head; + int i = 0; + while(iindex; i++) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object removeLast() { + return elementData[size--]; + } + + public Object remove(int index){ + Object temp = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size -= 1; + return temp; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new myIterator(this); + } + +} diff --git a/group08/125980622/first_hw/BinaryTreeNode.java b/group08/125980622/first_hw/BinaryTreeNode.java new file mode 100644 index 0000000000..0de773afd1 --- /dev/null +++ b/group08/125980622/first_hw/BinaryTreeNode.java @@ -0,0 +1,52 @@ +import com.sun.xml.internal.bind.v2.runtime.AttributeAccessor; + +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 insert(int o){ + BinaryTreeNode returnNode = null; + if (data < o) { + if (right == null) { + right = new BinaryTreeNode(o, null, null); + returnNode = right; + } else { + returnNode = right.insert(o); + } + } else if (data > o) { + if (left == null) { + returnNode = left = new BinaryTreeNode(o, null, null); + } else { + returnNode = left.insert(o); + } + } + return returnNode; + } + + BinaryTreeNode (int initData, BinaryTreeNode initLeft, BinaryTreeNode initRight) { + data = initData; + left = initLeft; + right = initRight; + } +} diff --git a/group08/125980622/first_hw/Iterator.java b/group08/125980622/first_hw/Iterator.java new file mode 100644 index 0000000000..57b7ad9d11 --- /dev/null +++ b/group08/125980622/first_hw/Iterator.java @@ -0,0 +1,4 @@ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group08/125980622/first_hw/LinkedList.java b/group08/125980622/first_hw/LinkedList.java new file mode 100644 index 0000000000..f5477064bb --- /dev/null +++ b/group08/125980622/first_hw/LinkedList.java @@ -0,0 +1,90 @@ +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if (head == null) { + head = new Node(o); + } else { + Node pointer = head.next; + while (pointer.next != null) { + pointer = pointer.next; + } + pointer.next = new Node(o); + } + size++; + } + public void add(int index , Object o){ + Node pointer = head; + for (int i = 0; i < index - 1; i++) { + pointer = pointer.next; + } + Node nNode = new Node(o, pointer.next); + pointer.next = nNode; + size++; + } + public Object get(int index){ + Node pointer = head; + for (int i= 0; i < index; i++) { + pointer = pointer.next; + } + return pointer; + } + public Object remove(int index){ + Node pointer = head; + for (int i = 0; i < index - 1; i++) { + pointer = pointer.next; + } + Node temp = pointer.next; + pointer.next = temp.next; + size--; + return temp; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + head = new Node(o, head); + } + public void addLast(Object o){ + Node pointer = head; + while (pointer.next != null) { + pointer = pointer.next; + } + pointer.next = new Node(o); + } + public Object removeFirst(){ + Node temp = head; + head = head.next; + return temp; + } + public Object removeLast(){ + Node pointer = head; + while (pointer.next.next != null) { + pointer = pointer.next; + } + Node temp = pointer.next; + pointer.next = null; + return temp; + } + public myIterator iterator(){ + return new myIterator(this); + } + + + private static class Node{ + Object data; + Node next; + Node (Object o, Node n) { + data = o; + next = n; + } + Node (Object o) { + data = o; + next = null; + } + } +} diff --git a/group08/125980622/first_hw/List.java b/group08/125980622/first_hw/List.java new file mode 100644 index 0000000000..4f7bcc71a8 --- /dev/null +++ b/group08/125980622/first_hw/List.java @@ -0,0 +1,7 @@ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/125980622/first_hw/Queue.java b/group08/125980622/first_hw/Queue.java new file mode 100644 index 0000000000..f176f2a256 --- /dev/null +++ b/group08/125980622/first_hw/Queue.java @@ -0,0 +1,21 @@ + +public class Queue { + + LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.addLast(o); + } + + public Object deQueue(){ + return queueList.removeFirst(); + } + + public boolean isEmpty(){ + return queueList.size() <= 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group08/125980622/first_hw/Stack.java b/group08/125980622/first_hw/Stack.java new file mode 100644 index 0000000000..e3c15480b8 --- /dev/null +++ b/group08/125980622/first_hw/Stack.java @@ -0,0 +1,24 @@ +public class Stack { + private ArrayList elementData = new ArrayList(); + + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + return elementData.remove(size); + } + + public Object peek(){ + return elementData.get(size); + } + public boolean isEmpty(){ + return size <= 0; + } + public int size(){ + return size; + } +} diff --git a/group08/125980622/first_hw/myIterator.java b/group08/125980622/first_hw/myIterator.java new file mode 100644 index 0000000000..0a2f9bb13d --- /dev/null +++ b/group08/125980622/first_hw/myIterator.java @@ -0,0 +1,37 @@ +import java.util.Objects; + +/** + * Created by Yusen Meng on 25/02/2017. + */ +public class myIterator implements Iterator { + int index = 0; + ArrayList _array = null; + LinkedList _list = null; + myIterator (LinkedList theList) { + _list = theList; + } + myIterator (ArrayList theArray) { + _array = theArray; + } + public boolean hasNext() { + boolean result; + if (_array != null) { + result = index < _array.size(); + } else { + result = index < _list.size(); + } + return result; + } + + public Object next() { + Object returnValue; + if (_array != null) { + returnValue = _array.get(index); + index++; + } else { + returnValue = _list.get(index); + index++; + } + return returnValue; + } +} diff --git a/group08/1277959541/1277959541 b/group08/1277959541/1277959541 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group08/1277959541/1277959541 @@ -0,0 +1 @@ + diff --git a/group08/1287324781/1287324781 b/group08/1287324781/1287324781 new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group08/1287324781/1287324781 @@ -0,0 +1 @@ + diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyArrayList.java b/group08/1425809544/02-26/com/xuyangyang/util/MyArrayList.java new file mode 100644 index 0000000000..52036eb78d --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyArrayList.java @@ -0,0 +1,113 @@ +package com; + +import java.util.NoSuchElementException; + +public class MyArrayList implements MyList { + + private static final int DEFAULT_CAPACITY = 10; + private int size; + private Object[] elementData; + + public MyArrayList() { + this.size = 0; + + } + + @Override + public boolean add(Object o) { + // TODO Auto-generated method stub + add(size(), o); + return true; + } + + @Override + public void add(int index, Object o) { + + if (elementData.length == size()) { + ensureCapacity(size() * 2 + 1); + } + + for (int i = size(); i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[2] = o; + size++; + + } + + @Override + public Object get(int index) { + + if (index < 0 || index >= size()) { + throw new ArrayIndexOutOfBoundsException(); + } + + return elementData[index]; + + } + + @Override + public Object remove(int index) { + + if (index < 0 || index >= size()) { + throw new ArrayIndexOutOfBoundsException(); + } + Object value = elementData[index]; + for (int i = index; i < size() - 1; i++) { + elementData[i] = elementData[i + 1]; + } + return value; + + } + + @Override + public int size() { + // TODO Auto-generated method stub + return size; + } + + /** + * 检测数组 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月20日 + * @param newCapacity + */ + public void ensureCapacity(int newCapacity) { + if (newCapacity < size) { + return; + } + Object[] old = elementData;// + + elementData = new Object[newCapacity];// 建立新的数组 + + for (int i = 0; i < old.length; i++) { + elementData[i] = old[i]; + } + + } + + private class InnerIterator implements MyIterator { + + private int current = 0; + + public boolean hasNext() { + return current < size(); + } + + @Override + public Object next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return elementData[current++]; + } + + public void remove() { + MyArrayList.this.remove(--current); + } + + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyIterator.java b/group08/1425809544/02-26/com/xuyangyang/util/MyIterator.java new file mode 100644 index 0000000000..1b83f8988d --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyIterator.java @@ -0,0 +1,8 @@ +package com; + +public interface MyIterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyLinkedList.java b/group08/1425809544/02-26/com/xuyangyang/util/MyLinkedList.java new file mode 100644 index 0000000000..3d84ecd268 --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyLinkedList.java @@ -0,0 +1,277 @@ +package com; + +import java.util.NoSuchElementException; + +public class MyLinkedList implements MyList { + + int size = 0; + Node first; + Node last; + + private static class Node { + Object item; + Node next;// 指向下一个节点 + Node prev;// 指向上一个节点 + + Node(Node prev, Object element, Node next) { + this.item = element; + this.next = next; + this.prev = prev; + } + } + + public boolean add(Object o) { + linkLast(o); + return true; + + } + + /** + * 把元素插入指定下标 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月21日 + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linkLast(0); + } else { + LinkBefore(o, node(index)); + } + + } + + public void addFirst(Object o) { + linkFirst(o); + } + + public void addLast(Object o) { + linkLast(o); + } + + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(null, o, f); + first = newNode; + if (first == null) { + first = newNode; + } else { + f.prev = newNode; + } + size++; + + } + + void LinkBefore(Object o, Node succ) { + final Node pred = succ.prev; + final Node newNode = new Node(pred, o, succ);// 先把新建节点的前后指针赋值 + succ.prev = newNode;// 把插入的后一个节点的前指针指向新节点 + if (pred == null) { + first = newNode;// 如果插入节点前指针为空,则为头指针 + } else { + pred.next = newNode;// 把前一个节点的后指针指向新节点; + } + size++; + } + + /** + * 根据指针返回节点信息 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月21日 + * @param index + * @return + */ + Node node(int index) { + + if (index < (size >> 1)) {// 如果节点索引在前半部分, + Node x = first;// 把头指针给x; + for (int i = 0; i < index; i++) + // 遍历前半部分节点, + x = x.next;// 寻找索引index的前一个节点,它的next指针 + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** + * 插入最后 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月21日 + * @param o + */ + private void linkLast(Object o) { + final Node l = last; + final Node newNode = new Node(l, o, null); + last = newNode; + if (last == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + public Object get(int index) { + checkElementIndex(index); + return node(index).item; + + } + + /** + * 获得元素是检查 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月21日 + * @param index + */ + private void checkElementIndex(int index) { + if (index >= 0 && index < size) { + + } else { + throw new IndexOutOfBoundsException(); + } + + } + + /** + * 添加元素时检查指针 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月21日 + * @param index + */ + private void checkPositionIndex(int index) { + if (index >= 0 && index <= size) + throw new IndexOutOfBoundsException(); + } + + public boolean reomve(Object o) { + + if (o == null) { + for (Node x = first; x != null; x = x.next) { + if (x.item == null) { + unlink(x); + return true; + } + } + + } else { + for (Node x = first; x != null; x = x.next) { + if (o.equals(x.item)) { + unlink(x); + return true; + } + } + + } + + return false; + + } + + @Override + public Object remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + + } + + private Object unlink(Node x) { + + final Object element = x.item; + final Node next = x.next; + final Node prev = x.prev; + + if (prev == null) { + first = 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; + + } + + public Object removeFirst() { + + final Node f = first; + if (f == null) { + throw new NoSuchElementException(); + } + + return unlinkFirst(f); + + } + + private Object unlinkFirst(Node f) { + + final Object element = f.item; + final Node next = f.next; + f.item = null; + f.next = null; + first = next; + if (next == null) { + last = null; + } else { + next.prev = null; + } + size--; + + return element; + } + + public Object removeLast() { + Node l = last; + if (l == null) { + throw new NoSuchElementException(); + } + return unLinkLast(l); + } + + private Object unLinkLast(Node l) { + final Object element = l.item; + final Node prev = l.prev; + last = prev; + l.item = null; + l.prev = null; + if (prev == null) { + first = null; + } else { + prev.next = null; + } + size--; + + return element; + } + + public int size() { + + return size; + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyList.java b/group08/1425809544/02-26/com/xuyangyang/util/MyList.java new file mode 100644 index 0000000000..8a2c653c8b --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyList.java @@ -0,0 +1,14 @@ +package com; + +public interface MyList { + + public boolean add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public int size(); + + public Object remove(int index); +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyQueueArray.java b/group08/1425809544/02-26/com/xuyangyang/util/MyQueueArray.java new file mode 100644 index 0000000000..248edbce26 --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyQueueArray.java @@ -0,0 +1,107 @@ +package com; + +import java.util.Arrays; + +/** + * 数组实现队列 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + */ +public class MyQueueArray { + + private int size;// 大小 + private int head;// 头 + private Object[] elementData;// 存放队列的数组 + private int initCapacity = 10;// 初始容量 + + /** + * 初始化队列容量10 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + */ + public MyQueueArray() { + elementData = new Object[initCapacity]; + } + + /** + * 获取队列大小 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @return + */ + public int size() { + return size; + } + + /** + * 入队 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @param o + */ + public void enQueue(Object o) { + if (size + 1 > elementData.length) { + int oldLength = elementData.length; + int newLength = oldLength + oldLength >> 1; + elementData = Arrays.copyOf(elementData, newLength); + } + elementData[size++] = o; + } + + /** + * 出队 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @return + */ + public Object deQueue() { + if (size == 0) { + throw new NullPointerException(); + } + Object obj = elementData[head]; + elementData[head] = 0; + head++; + size--; + return obj; + } + + /** + * 测试 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @param args + */ + public static void main(String[] args) { + MyQueueArray myQueue = new MyQueueArray(); + MyQueueLinked myQueueLinked = new MyQueueLinked(); + myQueue.enQueue("第1次入队"); + myQueue.enQueue("第2次入队"); + myQueue.enQueue("第3次入队"); + myQueue.enQueue("第4次入队"); + myQueue.enQueue("第5次入队"); + myQueue.enQueue("第6次入队"); + System.out.println(myQueue.size); + + System.out.println(myQueue.deQueue()); + System.out.println(myQueue.size); + + System.out.println(myQueue.deQueue()); + System.out.println(myQueue.size); + + System.out.println(myQueue.size); + + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyQueueLinked.java b/group08/1425809544/02-26/com/xuyangyang/util/MyQueueLinked.java new file mode 100644 index 0000000000..5b4f443975 --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyQueueLinked.java @@ -0,0 +1,139 @@ +package com; + +/** + * 链表实现队列---单向 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + */ +public class MyQueueLinked { + + private Node head; + + private Node tail; + // 队列的长度 + private int size; + + /** + * 返回队列的长度 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @return + */ + public int size() { + return size; + } + + /** + * 入队 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + */ + public void enQueue(Object obj) { + + // 如果队列为空 + if (head == null) { + head = new Node(obj, null); + tail = head; + size++; + } else { + Node oldNode = tail; + Node newNode = new Node(obj, null); + tail.next = newNode; + tail = newNode; + size++; + } + + } + + /** + * 出队 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @return + */ + public Object deQueue() { + + if (head == null) { + throw new NullPointerException(); + } + Object oldNode = head.element; + head = head.next; + size--; + return oldNode; + } + + /** + * 取出不删除 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @return + */ + public Object peek() { + if (head == null) { + throw new NullPointerException(); + } + return head.element; + } + + /** + * 节点 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + */ + private class Node { + private Object element; + private Node next; + + public Node() { + + } + + public Node(Object element, Node next) { + this.element = element; + this.next = next; + } + + } + + /** + * 测试 + * + * @Author xuyangyang + * @Describe + * @date 2017年2月23日 + * @param args + */ + public static void main(String[] args) { + MyQueueLinked myQueueLinked = new MyQueueLinked(); + myQueueLinked.enQueue("第1次入队"); + myQueueLinked.enQueue("第2次入队"); + myQueueLinked.enQueue("第3次入队"); + myQueueLinked.enQueue("第4次入队"); + myQueueLinked.enQueue("第5次入队"); + myQueueLinked.enQueue("第6次入队"); + System.out.println(myQueueLinked.size); + + System.out.println(myQueueLinked.deQueue()); + System.out.println(myQueueLinked.size); + + System.out.println(myQueueLinked.deQueue()); + System.out.println(myQueueLinked.size); + + System.out.println(myQueueLinked.peek()); + System.out.println(myQueueLinked.size); + + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/MyStack.java b/group08/1425809544/02-26/com/xuyangyang/util/MyStack.java new file mode 100644 index 0000000000..9c0a9a6149 --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/MyStack.java @@ -0,0 +1,95 @@ +package com; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.EmptyStackException; + +public class MyStack { + + private ArrayList elementDataArrayList = new ArrayList<>(); + + private Object[] elementData; + private int elementCount; + private int size; + + public MyStack() { + this.elementData = new Object[10]; + + } + + public Object push(Object o) { + addElement(o); + return o; + + } + + public void addElement(Object o) { + ensureCapacity(elementCount + 1); + elementData[elementCount++] = o; + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + public synchronized Object pop() { + Object o; + int len = size(); + o = peek(); + removeElement(len - 1); + + return o; + + } + + private void removeElement(int index) { + + if (index >= elementCount) { + throw new ArrayIndexOutOfBoundsException(); + } else if (index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + int j = elementCount - index - 1; + if (j > 0) { + System.arraycopy(elementData, index + 1, elementData, index, j); + } + elementCount--; + elementData[elementCount] = null; + } + + private Object peek() { + + int len = size(); + if (len == 0) { + throw new EmptyStackException(); + } + return elementAt(len - 1); + + } + + private Object elementAt(int index) { + if (index > +elementCount) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + private int size() { + // TODO Auto-generated method stub + return elementCount; + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/Test.java b/group08/1425809544/02-26/com/xuyangyang/util/Test.java new file mode 100644 index 0000000000..0fc81acb8e --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/Test.java @@ -0,0 +1,29 @@ +package com; + +import java.util.ArrayList; + +public class Test { + + public static void main(String[] args) { + + ArrayList items = new ArrayList<>(); + items.add("1"); + items.add("2"); + items.add("3"); + items.add("4"); + items.add("5"); + items.add("6"); + items.add("7"); + items.add("8"); + items.add("9"); + items.add("10"); + items.add("11"); + items.add("12"); + + + + + + } + +} diff --git "a/group08/1425809544/02-26/cpu \345\206\205\345\255\230 \347\241\254\347\233\230 \345\222\214\346\214\207\344\273\244\347\232\204\345\205\263\347\263\273.txt" "b/group08/1425809544/02-26/cpu \345\206\205\345\255\230 \347\241\254\347\233\230 \345\222\214\346\214\207\344\273\244\347\232\204\345\205\263\347\263\273.txt" new file mode 100644 index 0000000000..036bb225b0 --- /dev/null +++ "b/group08/1425809544/02-26/cpu \345\206\205\345\255\230 \347\241\254\347\233\230 \345\222\214\346\214\207\344\273\244\347\232\204\345\205\263\347\263\273.txt" @@ -0,0 +1,16 @@ + + +CPU 鐩稿綋浜庡ぇ鑴,鏄敱PC(绋嬪簭璁℃暟鍣),杩愮畻鍣,鎺у埗鍣,瀵勫瓨鍣ㄤ笁澶ч儴鍒嗙粍鎴; + 1.cpu浠庡唴瀛樿鍙栨寚浠,鏈塸c鎸囧畾浣嶇疆,鏀惧叆鎸囦护瀵勫瓨鍣; + 2,鏍规嵁璇诲彇鍒扮殑鎸囦护,鐢辫繍绠楀櫒鎵ц鍔犲噺鍛戒护, + + 1.绋嬪簭鍦╟pu,鍐呭瓨鍜岀‖鐩樿繍琛岀殑閫熷害渚濇閫掑噺,鐢卞揩鍒版參,鍚屾牱鐨勪粬浠环鏍兼垚鏈敱楂樺埌搴;瀛樺偍瀹归噺涔熸槸鐢卞ぇ鍒板皬; + + 2,cpu鍜屽唴瀛樹腑鐨勬暟鎹柇鐢靛悗灏变細娑堝け,纭洏涓嶄細娑堝け,鎵浠ヨ鎶婇渶瑕侀暱鏈熶娇鐢ㄧ殑绋嬪簭鏀惧湪纭洏涓,绋嬪簭鏄敱鎸囦护缁勬垚鐨, + 褰撳紑鏈哄悗,鍏堝姞杞介渶瑕佺殑鏁版嵁浠庣‖鐩樹腑璇诲彇鏀惧埌鍐呭瓨涓,鏈塩pu浠庡唴瀛樹腑璇诲彇鎸囦护,瑕佹墽琛岀殑鎸囦护鍜屾墽琛屽悗鐨勭粨鏋滄斁鍏pu鐨 + 瀵勫瓨鍣ㄤ腑;灏辩浉褰撲簬鍘熸枡杩涙満鍣ㄤ箣鍓嶆帓闃熶竴鏍,鎶婂崐鎴愬搧涔熻鍦ㄨ繖閲屾斁涓涓嬶紝鍐嶈繘鍘诲姞宸ワ紝瀹冨彧鏄竴涓复鏃朵腑杞珯(瀵勫瓨鍣); + + 3.CPU浠庡唴瀛樹腑鍙栧嚭鎸囦护,鏀惧叆鎸囦护瀵勫瓨鍣,骞跺鎸囦护璇戠爜鎿嶄綔鎺у埗鍣,鎿嶄綔鎸囦护,鎸囦护鍦╟pu涓殏瀛樺湪瀵勫瓨鍣ㄤ腑; + 浣嗘槸,cpu涓嶈兘鐩存帴鎿嶄綔纭洏,鎵浠ラ渶瑕佸唴瀛,瑕佹妸纭洏涓殑鏁版嵁鍏堝姞杞藉埌鍐呭瓨;鍐呭瓨鐩稿綋浜庝腑杞珯,鍐呭瓨鐨勯熷害鍐冲畾鐢佃剳绋嬪簭杩愯鐨勯熷害; + + 鏈夌殑缃戠粶鏂囩珷褰㈣薄姣斿柣锛氣淐PU鏄伐鍘傦紝纭洏鏄ぇ浠撳簱锛屽唴瀛樻槸姝h涓浆涓績 \ No newline at end of file diff --git "a/group08/1425809544/02-26/\346\226\207\347\253\240\351\223\276\346\216\245-java\351\233\206\345\220\210 \345\256\271\345\231\250 \347\256\200\345\215\225\346\246\202\350\277\260" "b/group08/1425809544/02-26/\346\226\207\347\253\240\351\223\276\346\216\245-java\351\233\206\345\220\210 \345\256\271\345\231\250 \347\256\200\345\215\225\346\246\202\350\277\260" new file mode 100644 index 0000000000..ff19095823 --- /dev/null +++ "b/group08/1425809544/02-26/\346\226\207\347\253\240\351\223\276\346\216\245-java\351\233\206\345\220\210 \345\256\271\345\231\250 \347\256\200\345\215\225\346\246\202\350\277\260" @@ -0,0 +1 @@ +http://m.blog.csdn.net/article/details?id=56674070 diff --git a/group08/1425809544/1425809544.md b/group08/1425809544/1425809544.md new file mode 100644 index 0000000000..61de0edff4 --- /dev/null +++ b/group08/1425809544/1425809544.md @@ -0,0 +1 @@ +娴嬭瘯 \ No newline at end of file diff --git a/group08/1509102580/zzk.md b/group08/1509102580/zzk.md new file mode 100644 index 0000000000..19fffff42b --- /dev/null +++ b/group08/1509102580/zzk.md @@ -0,0 +1 @@ +I am zzk! diff --git a/group08/2420826330/2420826330.md b/group08/2420826330/2420826330.md new file mode 100644 index 0000000000..c55a78db43 --- /dev/null +++ b/group08/2420826330/2420826330.md @@ -0,0 +1 @@ +This is 2420826330. diff --git a/group08/283677872/test b/group08/283677872/test new file mode 100644 index 0000000000..3d8158f431 --- /dev/null +++ b/group08/283677872/test @@ -0,0 +1 @@ +283677872 diff --git "a/group08/286060098/2-26/blong/\345\237\272\346\234\254\347\273\223\346\236\204.md" "b/group08/286060098/2-26/blong/\345\237\272\346\234\254\347\273\223\346\236\204.md" new file mode 100644 index 0000000000..23d6a12c12 --- /dev/null +++ "b/group08/286060098/2-26/blong/\345\237\272\346\234\254\347\273\223\346\236\204.md" @@ -0,0 +1,54 @@ +#鎸囦护闆 +---- +閫氫織鐨勭悊瑙o紝鎸囦护闆嗗氨鏄疌PU鑳借璇嗙殑璇█锛屾寚浠ら泦杩愯浜庝竴瀹氱殑寰灦鏋勪箣涓.鎸囦护闆嗘湁寰堝,姣斿澶嶆潅鎸囦护闆咥MD,INTEL鐢ㄧ殑,绮剧畝鎸囦护闆咥RM鐢ㄧ殑 + +# CPU +--- +CPU浣挎暣涓绠楁満鐨勬牳蹇,杩涜鏁翠綋鐨勯昏緫鎿嶄綔,浠栫殑杩愮畻閫熷害寰堝揩,鎴戜滑鍦ㄤ富鏈轰笂鐨勬搷浣滄渶缁堥兘浼氬彉鎴愬師瀛愭寚浠ょ敱CPU鏉ユ墽琛 + +鍘嗗彶涓婄殑CPU瀵圭▼搴忕殑鎵ц瀛樺湪寰堝鐨勬墽琛屾柟妗,姣斿璇存椂闂寸墖娉,浠ょ墝鐜箣绫荤殑鐨,姣忎釜绋嬪簭渚濇浜ゆ崲鎵ц,杩欐牱灏卞瓨鍦ㄤ竴涓棶棰,绋嬪簭鐨勬崲鍏ユ崲鍑洪渶瑕佷繚鐣欏綋鍓嶆墽琛屾儏鍐电殑鐜板満,杩樿鍔犺浇鍘嗗彶绋嬪簭鐨勫巻鍙茬幇鍦 + +杩欎簺鏁版嵁瀛樺偍鍦ㄥ摢閲?鐢变簬CPU鐨勯熷害杩囧揩,鑰屾垜浠槸鐢ㄧ殑澶ц妯$殑鏁版嵁瀛樺偍涓鑸槸浠锋牸杈冧綆閫熷害杈冩參鐨勭鐩.濡傛灉璇碈PU鐩存帴浠庣鐩樺姞杞,CPU鐨勮祫婧愬埄鐢ㄧ巼浼氬彉鐨勫緢浣,浼氫弗閲嶆氮璐逛簡CPU鐨勮繍绠楄兘鍔.鍥犳鎴戜滑闇瑕佺紦瀛 + +# 缂撳瓨 +--- +缂撳瓨鐨勫嚭鏄负鍔犺浇鐩稿叧鐨勬暟鎹彁渚汣PU璺熀杩愮畻鐨勫瓨鍌,CPU涓殑瀵勫瓨鍣ㄥ氨鏄竴绉嶇紦瀛,杩欐槸CPU鏈韩瀵规搷浣滅殑浼樺寲.浣嗘槸璇磋繖鏍烽珮閫熺殑缂撳瓨鏈韩鐨勫埗閫犳垚鏈槸闈炲父楂樼殑,瀹屽叏浣跨敤杩欐牱楂樻ц兘鐨勫瓨鍌ㄨ澶囧苟涓嶆槸涓涓叿鏈夋т环姣旂殑瑙e喅鏂规 + +鍥犳鍑虹幇浜嗗绾х紦瀛,甯歌鐨勫INTEL鐨凩1,L2,L3,杩欐槸澶氱骇缂撳瓨鐨勪竴绉嶅疄鐜,闅忕潃灞傜骇鐨勫崌楂,瀛樺偍绌洪棿鍦ㄥ崌楂,閫熷害鍦ㄤ笅灏,閫氳繃璇稿LFU銆丩RU鐨勭紦瀛樼疆鎹㈢畻娉曚紭鍖栫浉鍏崇殑缂撳瓨鍛戒腑鐜囨彁楂楥PU鍒╃敤鐜 + +鐞嗚В Cache --> 浼犻侀棬 `http://www.mouseos.com/arch/cache.html` + +鍗充究鏄疞3鐨勭紦瀛樹篃浠ュ氨鏄崄鍒嗘槀璐电殑,渚濇棫鏄緢闅惧ぇ瑙勬ā浣跨敤鐨勫瓨鍌ㄨ澶,鍥犳闇瑕侀熷害鐩稿杈冨揩,浣嗘槸浠锋牸鍙堟槸鏅氱敤鎴峰彲浠ユ帴鏀剁殑涓棿璁惧,姣斿鍐呭瓨 + +# 鍐呭瓨 +--- +鍐呭瓨鐨勫瓨鍦ㄥ疄闄呬笂瑙e喅浜嗗緢澶氶棶棰,浠栫殑浠锋牸鐩稿楂橀熺紦瀛樻潵璇寸浉瀵硅緝浣,鑳藉鎻愪緵杈冨ぇ鐨勫瓨鍌,涔熷彲浠ョ湅浣滅紦瀛樼殑涓绉,灏嗙鐩樼殑鏁版嵁棰勫姞杞,渚涢珮閫熺紦瀛樺姞杞. + +鍐呭瓨鏈夊緢澶氱殑宸ヤ綔棰戠巼,鍐呭瓨涓婚瓒婇珮鍦ㄤ竴瀹氱▼搴︿笂浠h〃鐫鍐呭瓨鎵鑳借揪鍒扮殑閫熷害瓒婂揩,璁$畻鏈虹郴缁熺殑鏃堕挓閫熷害鏄互棰戠巼鏉ヨ 閲忕殑.鏅朵綋鎸崱鍣ㄦ帶鍒剁潃鏃堕挓閫熷害,鍦ㄧ煶鑻辨櫠鐗囦笂鍔犱笂鐢靛帇,鍏跺氨浠ユ寮︽尝鐨勫舰寮忛渿鍔ㄨ捣鏉,杩欎竴闇囧姩鍙互閫氳繃鏅剁墖鐨勫舰鍙樺拰澶у皬璁板綍涓嬫潵銆傛櫠浣撶殑闇囧姩浠ユ寮﹁皟鍜屽彉鍖栫殑鐢垫祦鐨勫舰寮忚〃鐜板嚭鏉,杩欎竴鍙樺寲鐨勭數娴佸氨鏄椂閽熶俊鍙. + +鍐呭瓨鏄竴绉嶆柇鐢靛悗瀛樺偍娲楁礂灏变細涓㈠け鐨勫瓨鍌ㄨ澶,杩欏氨灏村艾浜,瀵逛簬瀹曟満鎴戜滑骞舵棤娉曢娴,鎴戜滑闇瑕佸皢浼楀鐨勮祫婧愭暟鎹繘琛屽瓨鍌,杩欎釜鏃跺欐垜浠渶瑕佺‖鐩. + +# 纭洏 +--- +纭洏瀵逛簬鎴戜滑鑰岃█浠栫殑閫熷害鍙嶈屾槸鍏舵鐨,浠栨渶澶х殑鎰忎箟鏄暟鎹殑瀹夊叏鎬.鍦ㄦ暟鎹畨鍏ㄧ殑鍩虹涔嬩笂鎴戜滑鍐嶅幓杩芥眰纭洏鐨勯熷害. + +纭洏鏈夊緢澶氭瘮濡傝鏈烘纭洏,鍥烘佺‖鐩,PCI纭洏.鏈烘纭洏.鏈烘纭洏鐨勫ぇ浣撶粨鏋勫涓嬪浘,鏁版嵁鐨勮鍙栭渶瑕佺澶寸殑绉诲姩璇诲彇鎵囧尯涓婄殑淇℃伅,杩欐椂鍊欑鐩樼殑杞熷氨寰堥噸瑕佷簡 +![yingpan.jpg-19.4kB][1] + +鍥烘佺‖鐩樺彲浠ョ湅浣滃紡闂瓨鐨勫爢绉,闂瓨鐨勫埗閫犱环鏍肩殑闄嶄綆甯︽潵浜嗕粬鐨勬槬澶,浣嗘槸闂瓨鐨勬暟鎹摝闄ゆ鏁版湁闄,鍥烘佺‖鐩樼殑瀵垮懡瑕佹敞鎰 + + + + + + + + + + + + + + + + [1]: http://static.zybuluo.com/Haipop/finuq0bs9p1d90q38bccqjea/yingpan.jpg \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/AbstractCollection.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/AbstractCollection.java new file mode 100644 index 0000000000..52b095dc23 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/AbstractCollection.java @@ -0,0 +1,25 @@ +package com.pop.practice.homework.first.collection; + +/** + * @author haipop Date: 17-2-19 Time: 涓嬪崍3:40 + */ +public abstract class AbstractCollection implements Collection { + + @Override + @SuppressWarnings("unchecked") + public void addAll(Collection collection) throws IllegalAccessException { + Iterator iterator = collection.iterator(); + while (iterator.hasNext()) { + add((T) iterator.next()); + } + } + + @Override + @SuppressWarnings("unchecked") + public void removeAll(Collection collection) { + Iterator iterator = collection.iterator(); + while (iterator.hasNext()) { + remove((T) iterator.next()); + } + } +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Collection.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Collection.java new file mode 100644 index 0000000000..9355d7761f --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Collection.java @@ -0,0 +1,42 @@ +package com.pop.practice.homework.first.collection; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:35 + */ +public interface Collection extends Iterator { + + /** + * 鏄惁涓虹┖ + */ + boolean isEmpty(); + + /** + * 鑾峰彇澶у皬 + */ + int size(); + + /** + * 娣诲姞鍏冪礌 + */ + void add(T element) throws IllegalAccessException; + + /** + * 鎵归噺娣诲姞鍏冪礌 + */ + void addAll(Collection collection) throws IllegalAccessException; + + /** + * 鍒犻櫎鍏冪礌 + */ + void remove(T element); + + /** + * 鎵归噺鍒犻櫎鍏冪礌 + */ + void removeAll(Collection collection); + + /** + * 鍏冪礌鏌ユ壘,杩斿洖绱㈠紩,鎵句笉鍒拌繑鍥-1 + */ + int contain(T element); +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Iterator.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Iterator.java new file mode 100644 index 0000000000..0bbe4fa605 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/Iterator.java @@ -0,0 +1,23 @@ +package com.pop.practice.homework.first.collection; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:34 + */ +public interface Iterator { + + /** + * 瀹炰緥鍖 + */ + Iterator iterator(); + + /** + * 鏄惁瀛樺湪涓嬩竴涓 + */ + boolean hasNext(); + + /** + * 鑾峰彇涓嬩竴涓厓绱 + */ + T next(); + +} diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/ArrayList.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/ArrayList.java new file mode 100644 index 0000000000..9c355cc8b4 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/ArrayList.java @@ -0,0 +1,166 @@ +package com.pop.practice.homework.first.collection.list; + +import java.io.Serializable; +import java.util.Objects; + +import com.pop.practice.homework.first.collection.AbstractCollection; +import com.pop.practice.homework.first.collection.Iterator; +import com.pop.practice.homework.utils.Math; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:33 + */ +public class ArrayList extends AbstractCollection implements List, Serializable { + + private static final long serialVersionUID = -3408657766857424074L; + + /** + * 闃堝 + */ + private static final double THRESHOLD = 0.75F; + + /** + * 澶у皬 + */ + private int size; + + /** + * 褰撳墠鐨勫瓨鍌ㄤ綅缃 + */ + private int flag; + + /** + * 鍏冪礌闆嗗悎 + */ + private Object[] store; + + /** + * 鏄惁瀹归噺鑷 + */ + private boolean autoIncrement; + + /** + * 閬嶅巻鍣 + */ + private Iterator iterator; + + /** + * 榛樿鏃犲弬 + */ + public ArrayList() { + this.size = 8; + this.flag = 0; + this.autoIncrement = true; + this.store = new Object[this.size]; + } + + /** + * 鎸囧畾澶у皬,涓嶅彲鑷 + */ + public ArrayList(int size) { + this.size = size; + this.flag = 0; + this.autoIncrement = false; + this.store = new Object[size]; + } + + @Override + public Iterator iterator() { + this.iterator = new ArrayListIterator(); + return iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) iterator.next(); + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return flag; + } + + @Override + public void add(T element) throws IllegalAccessException { + assessStore(flag + 1, size, store, flag); + flag++; + this.store[flag] = element; + } + + private void assessStore(int left, int size, Object[] store, int flag) throws IllegalAccessException { + if (!autoIncrement) { + return; + } + double coefficient = Math.div(left, size); + if (coefficient > THRESHOLD) { + // 杈惧埌闃堝,鎷撳睍 + Object[] newStore = new Object[this.size * 2]; + System.arraycopy(store, 0, newStore, 0, flag); + this.store = newStore; + this.size = size * 2; + } + } + + @Override + public void remove(T element) { + for (int i = 0; i < flag; i++) { + if (Objects.equals(this.store[i], element)) { + System.arraycopy(store, i + 1, store, i, flag - i); + flag--; + break; + } + } + } + + @Override + public int contain(T element) { + int result = -1; + for (int i = 0; i < flag; i++) { + if (Objects.equals(element, store[i])) { + return i; + } + } + return result; + } + + @Override + @SuppressWarnings("uncheckd") + public T get(int index) throws IndexOutOfBoundsException { + return (T) this.store[index]; + } + + private class ArrayListIterator implements Iterator { + + private int index; + + ArrayListIterator() { + index = 0; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + return store[index++]; + } + } +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/LinkedList.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/LinkedList.java new file mode 100644 index 0000000000..874195596e --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/LinkedList.java @@ -0,0 +1,240 @@ +package com.pop.practice.homework.first.collection.list; + +import java.io.Serializable; +import java.util.Objects; + +import com.pop.practice.homework.first.collection.AbstractCollection; +import com.pop.practice.homework.first.collection.Iterator; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:34 + */ +public class LinkedList extends AbstractCollection implements List, Serializable { + + private static final long serialVersionUID = 365915684200725970L; + + /** + * 澶磋妭鐐 + */ + private Node head; + + /** + * 灏捐妭鐐 + */ + private Node tail; + + /** + * 鏍囪浣 + */ + private int flag; + + /** + * 閬嶅巻鍣 + */ + private Iterator iterator; + + public LinkedList() { + this.head = new Node(); + this.tail = new Node(); + this.flag = 0; + this.head.setNext(tail); + } + + @Override + public boolean isEmpty() { + return this.flag == 0; + } + + @Override + public int size() { + return this.flag; + } + + @Override + public void add(T element) throws IllegalAccessException { + if (this.head.getData() == null) { + this.head.setData(element); + flag++; + return; + } + if (this.tail.getData() == null) { + this.tail.setData(element); + this.tail.setBefore(this.head); + flag++; + return; + } + Node node = new Node(element); + node.setBefore(this.tail); + this.tail.next = node; + this.tail = node; + flag++; + } + + @Override + public void remove(T element) { + if (this.head == null) { + return; + } + Node tmp = this.head; + while (tmp.next != null) { + if (Objects.equals(tmp.getData(), element)) { + if (tmp == this.head) { + if (this.tail.getData() == null) { + this.head.setData(null); + } else { + this.head = this.head.next; + this.head.before = null; + } + flag--; + return; + } + Node next = tmp.next; + Node before = tmp.before; + if (next == null) { + before.setNext(null); + } else { + before.setNext(next); + } + next.setBefore(before); + flag--; + tmp = null; + } + tmp = tmp.next; + } + } + + @Override + public int contain(T element) { + Node tmp = this.head; + int index = 0; + while (tmp.next != null) { + if (Objects.equals(tmp.getData(), element)) { + return index; + } else { + tmp = tmp.next; + index++; + } + } + return -1; + } + + @Override + public T get(int index) throws IndexOutOfBoundsException { + Node tmp = this.head; + for (int i = 1; i < index; i++) { + if (tail == tmp) { + // 鍒版渶鍚庤繕娌″埌鎸囧畾浣嶇疆,瓒婄晫 + throw new IndexOutOfBoundsException(); + } + tmp = tmp.getNext(); + } + return tmp.getData(); + } + + @Override + public Iterator iterator() { + this.iterator = new LinkListIterator(); + return iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) iterator.next(); + } + + private class LinkListIterator implements Iterator { + + private Node cache; + + LinkListIterator() { + cache = head; + } + + @Override + public Iterator iterator() { + return new LinkListIterator(); + } + + @Override + public boolean hasNext() { + return cache.getNext() == null; + } + + @Override + public Object next() { + return cache.getNext(); + } + } + + /** + * 閾捐〃鑺傜偣绫 --> 杩欓噷鍋氫簡鍙屽悜閾捐〃锛岃繖閲屽鏋滄槸涓涓烦琛ㄧ殑璇濇煡璇㈡ц兘浼氭洿濂戒竴鐐 + */ + private class Node implements Serializable { + + private static final long serialVersionUID = 6327349429030778592L; + /** + * 鏁版嵁 + */ + private T data; + + /** + * 涓嬩竴鑺傜偣 + */ + private Node before; + + /** + * 涓嬩竴鑺傜偣 + */ + private Node next; + + Node() { + } + + Node(T data) { + this.data = data; + this.next = null; + } + + Node(T data, Node before) { + this.data = data; + this.before = before; + } + + Node(T data, Node before, Node next) { + this.data = data; + this.before = before; + this.next = next; + } + + T getData() { + return data; + } + + void setData(T data) { + this.data = data; + } + + Node getBefore() { + return before; + } + + void setBefore(Node before) { + this.before = before; + } + + Node getNext() { + return next; + } + + void setNext(Node next) { + this.next = next; + } + } + +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/List.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/List.java new file mode 100644 index 0000000000..7be9392b75 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/list/List.java @@ -0,0 +1,14 @@ +package com.pop.practice.homework.first.collection.list; + +import com.pop.practice.homework.first.collection.Collection; + +/** + * @author haipop Date: 17-2-19 Time: 涓嬪崍4:03 + */ +public interface List extends Collection { + + /** + * 鑾峰彇鎸囧畾浣嶇疆鍏冪礌 + */ + T get(int index) throws IndexOutOfBoundsException; +} diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/LinkQueue.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/LinkQueue.java new file mode 100644 index 0000000000..bb371d64ca --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/LinkQueue.java @@ -0,0 +1,202 @@ +package com.pop.practice.homework.first.collection.queue; + +import java.io.Serializable; +import java.util.Objects; + +import com.pop.practice.homework.first.collection.AbstractCollection; +import com.pop.practice.homework.first.collection.Collection; +import com.pop.practice.homework.first.collection.Iterator; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:34 + */ +public class LinkQueue extends AbstractCollection implements Queue, Serializable { + + private static final long serialVersionUID = 7942321140756962637L; + + /** + * 澶磋妭鐐 + */ + private Node head; + + /** + * 灏捐妭鐐,浼妭鐐,杩欎釜鑺傜偣鎸囩敤鏉ユ寚涓婁竴涓 + */ + private Node tail; + + /** + * 鐜版湁鐨勫厓绱犱釜鏁 + */ + private int size; + + /** + * 閬嶅巻鍣 + */ + private Iterator iterator; + + public LinkQueue() { + this.size = 0; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public void add(T element) throws IllegalAccessException { + if (this.head == null) { + this.head = new Node(element); + this.tail.setBefore(this.head); + this.size++; + return; + } + Node node = new Node(element); + node.setBefore(null); + node.setNext(this.head.getNext()); + this.head.getNext().setBefore(node); + this.head = node; + this.size++; + } + + @Override + public void remove(T element) { + if (isEmpty()) { + throw new IndexOutOfBoundsException(); + } + Node tmp = this.head; + while (tmp.getNext() == null && tmp.getNext() != this.tail) { + if (Objects.equals(tmp.getData(), element)) { + Node before = tmp.getBefore(); + Node next = tmp.getNext(); + before.setNext(next); + next.setBefore(before); + tmp = null; + break; + } + tmp = tmp.getNext(); + } + } + + @Override + public void push(T element) throws IllegalAccessException { + add(element); + } + + @Override + public void push(Collection collection) throws IllegalAccessException { + addAll(collection); + } + + @Override + public int contain(T element) { + Node tmp = this.head; + int index = 0; + while (tmp.next != null && tmp != this.tail) { + if (Objects.equals(tmp.getData(), element)) { + return index; + } else { + tmp = tmp.next; + index++; + } + } + return -1; + } + + @Override + public T pull() { + if (isEmpty()) { + throw new IndexOutOfBoundsException(); + } + Node node = this.tail.getBefore(); + this.tail.setBefore(node.getBefore()); + size--; + return node.getData(); + } + + @Override + public Iterator iterator() { + this.iterator = new LinkQueueIterator(); + return iterator; + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext(); + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) this.iterator.next(); + } + + private class LinkQueueIterator implements Iterator { + + private Node cache; + + LinkQueueIterator() { + this.cache = head; + } + + @Override + public Iterator iterator() { + return new LinkQueueIterator(); + } + + @Override + public boolean hasNext() { + return cache.getNext() == tail; + } + + @Override + public T next() { + return cache.getNext().getData(); + } + } + + private class Node implements Serializable { + + private static final long serialVersionUID = 6400564182277299061L; + + private T data; + + private Node before; + + private Node next; + + Node(T data) { + this.data = data; + } + + T getData() { + return data; + } + + void setData(T data) { + this.data = data; + } + + Node getBefore() { + return before; + } + + void setBefore(Node before) { + this.before = before; + } + + Node getNext() { + return next; + } + + void setNext(Node next) { + this.next = next; + } + } + +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/Queue.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/Queue.java new file mode 100644 index 0000000000..92f46378e0 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/queue/Queue.java @@ -0,0 +1,26 @@ +package com.pop.practice.homework.first.collection.queue; + +import com.pop.practice.homework.first.collection.Collection; + +/** + * 鏇寸鍚圦ueue璇箟鐨勬柟娉 + * + * @author haipop Date: 17-2-19 Time: 涓嬪崍3:23 + */ +public interface Queue extends Collection { + + /** + * 娣诲姞鍏冪礌 + */ + void push(T element) throws IllegalAccessException; + + /** + * 娣诲姞鍏冪礌 + */ + void push(Collection collection) throws IllegalAccessException; + + /** + * 鍙栧厓绱,鍒犻櫎鏈鍚庝竴涓苟杩斿洖 + */ + T pull(); +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/LinkStack.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/LinkStack.java new file mode 100644 index 0000000000..c9a0edd124 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/LinkStack.java @@ -0,0 +1,94 @@ +package com.pop.practice.homework.first.collection.stack; + +import java.io.Serializable; + +import com.pop.practice.homework.first.collection.AbstractCollection; +import com.pop.practice.homework.first.collection.Iterator; +import com.pop.practice.homework.first.collection.list.LinkedList; +import com.pop.practice.homework.first.collection.list.List; + +/** + * @author haipop Date: 17-2-16 Time: 涓嬪崍6:33 + */ +public class LinkStack extends AbstractCollection implements Stack, Serializable { + + private static final long serialVersionUID = -2813631170103864318L; + + /** + * 鏁版嵁瀛樺偍 + */ + private List cache; + + /** + * 鏁版嵁閲 + */ + private int size; + + /** + * 閬嶅巻鍣 + */ + private Iterator iterator; + + public LinkStack() { + this.size = 0; + this.cache = new LinkedList(); + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public void push(T element) throws IllegalAccessException { + this.cache.add(element); + size++; + } + + @Override + public void add(T element) throws IllegalAccessException { + this.cache.add(element); + size++; + } + + @Override + public T pull() throws IndexOutOfBoundsException { + T result = this.cache.get(size - 1); + size--; + return result; + } + + @Override + public void remove(T element) { + cache.remove(element); + size--; + } + + @Override + public int contain(T element) { + return cache.contain(element); + } + + @Override + public Iterator iterator() { + this.iterator = this.cache.iterator(); + return iterator; + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext(); + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) this.iterator.next(); + } + +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/Stack.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/Stack.java new file mode 100644 index 0000000000..c9ea66dd2b --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/collection/stack/Stack.java @@ -0,0 +1,22 @@ +package com.pop.practice.homework.first.collection.stack; + +import com.pop.practice.homework.first.collection.Collection; + +/** + * + * 鏇寸鍚圫tack璇箟鐨勬柟娉 + * + * @author haipop Date: 17-2-19 Time: 涓嬪崍3:50 + */ +public interface Stack extends Collection { + + /** + * 娣诲姞鍏冪礌 + */ + void push(T element) throws IllegalAccessException; + + /** + * 鍙栧厓绱 + */ + T pull(); +} diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/AbstractTree.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/AbstractTree.java new file mode 100644 index 0000000000..4e86940103 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/AbstractTree.java @@ -0,0 +1,29 @@ +package com.pop.practice.homework.first.tree; + +import com.pop.practice.homework.first.collection.Iterator; +import com.pop.practice.homework.first.collection.list.List; + +/** + * @author haipop Date: 17-2-20 Time: 涓婂崍9:53 + */ +public abstract class AbstractTree implements Tree { + + @Override + public void addNode(T[] elements) throws IllegalAccessException { + for (T ele : elements) { + addNode(ele); + } + } + + @Override + @SuppressWarnings("unchecked") + public void addNode(List elements) throws IllegalAccessException { + Iterator iterator = elements.iterator(); + while (iterator.hasNext()) { + addNode((T) iterator.next()); + } + } + + + protected abstract void addNode(T elements) throws IllegalAccessException; +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryNode.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryNode.java new file mode 100644 index 0000000000..db34c48460 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryNode.java @@ -0,0 +1,40 @@ +package com.pop.practice.homework.first.tree; + +class BinaryNode { + + private T data; + + private BinaryNode left; + + private BinaryNode right; + + public BinaryNode(T data, BinaryNode left, BinaryNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryNode getLeft() { + return left; + } + + public void setLeft(BinaryNode left) { + this.left = left; + } + + public BinaryNode getRight() { + return right; + } + + public void setRight(BinaryNode right) { + this.right = right; + } +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryTree.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryTree.java new file mode 100644 index 0000000000..eff95ec6f9 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/BinaryTree.java @@ -0,0 +1,125 @@ +package com.pop.practice.homework.first.tree; + +import java.io.Serializable; + +import com.pop.practice.homework.first.collection.queue.LinkQueue; +import com.pop.practice.homework.first.collection.queue.Queue; + +/** + * 宸﹀ぇ鍙冲皬 + * + * @author haipop Date: 17-2-20 Time: 涓婂崍9:42 + */ +public class BinaryTree extends AbstractTree implements Serializable { + + private static final long serialVersionUID = -1467347526158258388L; + + /** + * 鏍硅妭鐐 + */ + private BinaryNode root; + + @Override + @SuppressWarnings("unchecked") + protected void addNode(T elements) throws IllegalAccessException { + if (root == null) { + root = new BinaryNode(elements, null, null); + } + BinaryNode newNode = new BinaryNode<>(elements, null, null); + Queue queue = new LinkQueue<>(); + queue.add(root); + while (queue.isEmpty()) { + BinaryNode node = queue.pull(); + if (((Comparable) node.getData()).compareTo(elements) > 0) { + if (node.getRight() == null) { + node.setRight(newNode); + break; + } else { + queue.add(node.getRight()); + } + } else if (((Comparable) node.getData()).compareTo(elements) < 0) { + if (node.getLeft() == null) { + node.setLeft(newNode); + break; + } else { + queue.add(node.getLeft()); + } + } + } + } + + @Override + @SuppressWarnings("unchecked") + public void removeNode(T elements) throws IllegalAccessException { + if (root == null) { + return; + } + root = removeNode(elements, root); + } + + @SuppressWarnings("unchecked") + private BinaryNode removeNode(T element, BinaryNode node) { + BinaryNode left = node.getLeft(); + BinaryNode right = node.getRight(); + if (node.getData().compareTo(element) > 0) { + BinaryNode removeNode = removeNode(element, right); + moveNode(element, node, left, right, removeNode); + node.setRight(removeNode); + } else if (node.getData().compareTo(element) < 0) { + BinaryNode removeNode = removeNode(element, left); + moveNode(element, node, left, right, removeNode); + node.setLeft(removeNode); + } + if (left != null) { + return left; + } else if (right != null) { + return right; + } else { + return null; + } + } + + @SuppressWarnings("unchecked") + private void moveNode(T element, BinaryNode node, BinaryNode left, BinaryNode right, + BinaryNode removeNode) { + if (removeNode.getData().compareTo(right) > 0) { + BinaryNode tmp = removeNode.getRight(); + while (tmp != null) { + tmp = tmp.getRight(); + } + tmp.setRight(left.getRight()); + } else if (node.getData().compareTo(element) < 0) { + BinaryNode tmp = removeNode.getLeft(); + while (tmp != null) { + tmp = tmp.getLeft(); + } + tmp.setLeft(right.getLeft()); + } + } + + @Override + @SuppressWarnings("unchecked") + public boolean contain(T data) throws IllegalAccessException { + if (root == null) { + return false; + } + Queue queue = new LinkQueue<>(); + queue.add(root); + while (queue.isEmpty()) { + BinaryNode node = queue.pull(); + if (((Comparable) node.getData()).compareTo(data) > 0) { + if (node.getRight() != null) { + queue.add(node.getRight()); + } + } else if (((Comparable) node.getData()).compareTo(data) < 0) { + if (node.getLeft() == null) { + queue.add(node.getLeft()); + } + } else { + return true; + } + } + return false; + } + +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/Tree.java b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/Tree.java new file mode 100644 index 0000000000..174a98bc2c --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/first/tree/Tree.java @@ -0,0 +1,30 @@ +package com.pop.practice.homework.first.tree; + +import com.pop.practice.homework.first.collection.list.List; + +/** + * @author haipop Date: 17-2-19 Time: 涓嬪崍5:12 + */ +public interface Tree { + + /** + * 娣诲姞鑺傜偣 + */ + void addNode(T... elements) throws IllegalAccessException; + + /** + * 娣诲姞鑺傜偣 + */ + void addNode(List elements) throws IllegalAccessException; + + /** + * 鍒犻櫎鑺傜偣 + */ + void removeNode(T element) throws IllegalAccessException; + + /** + * 鑺傜偣鏌ユ壘,鎵惧埌杩斿洖鑺傜偣淇℃伅,鎵句笉鍒拌繑鍥瀗ull + */ + boolean contain(T data) throws IllegalAccessException; + +} \ No newline at end of file diff --git a/group08/286060098/2-26/src/com/pop/practice/homework/utils/Math.java b/group08/286060098/2-26/src/com/pop/practice/homework/utils/Math.java new file mode 100644 index 0000000000..b20c1bb486 --- /dev/null +++ b/group08/286060098/2-26/src/com/pop/practice/homework/utils/Math.java @@ -0,0 +1,29 @@ +package com.pop.practice.homework.utils; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * @author haipop Date: 17-2-18 Time: 涓嬪崍1:00 + */ +public class Math { + + public static double div(int left, int right) throws IllegalAccessException { + return div((double) left, (double) right, 2); + } + + public static double div(int left, int right, int scale) throws IllegalAccessException { + return div((double) left, (double) right, scale); + } + + @SuppressWarnings("unchecked") + public static double div(double left, double right, int scale) throws IllegalAccessException { + // 濡傛灉绮剧‘鑼冨洿灏忎簬0锛屾姏鍑哄紓甯镐俊鎭 + if (scale < 0) { + throw new IllegalAccessException("绮剧‘搴︿笉鑳藉皬浜0"); + } + BigDecimal divisor = new BigDecimal(left); + BigDecimal dividend = new BigDecimal(right); + return divisor.divide(dividend, scale, RoundingMode.HALF_EVEN).doubleValue(); + } +} \ No newline at end of file diff --git a/group08/286060098/readme.md b/group08/286060098/readme.md new file mode 100644 index 0000000000..a6325e29da --- /dev/null +++ b/group08/286060098/readme.md @@ -0,0 +1,17 @@ +# Module璇存槑 + +--- + +### 鏂囦欢澶硅鏄 +| 鏂囦欢澶 | 鎻忚堪 銆 | +| -------- | :----: | +| blong |鐩稿叧鐨勮鏄庢枃妗,鍗氬 | +| script | 鐩稿叧鐨勮剼鏈俊鎭紝濡傛湁sql鑴氭湰涔熻鍦ㄨ繖閲屾煡鎵 | +| src | 浠g爜鐩綍锝 + +### 浠g爜璇存槑 + +> 浣滀笟鍦╤omework鐩綍涓 + + + diff --git a/group08/406166841/2-26/CPU.md b/group08/406166841/2-26/CPU.md new file mode 100644 index 0000000000..0184372f60 --- /dev/null +++ b/group08/406166841/2-26/CPU.md @@ -0,0 +1,76 @@ +CPU锛氫腑澶鐞嗗崟鍏(Cntral Pocessing Uit)鐨勭缉鍐欙紝涔熷彨澶勭悊鍣紝鏄绠楁満鐨勮繍绠楁牳蹇冨拰鎺у埗鏍稿績銆備汉闈犲ぇ鑴戞濊冿紝鐢佃剳闈燙PU鏉ヨ繍绠椼佹帶鍒躲傝鐢佃剳鐨勫悇涓儴浠堕『鍒╁伐浣滐紝璧峰埌鍗忚皟鍜屾帶鍒朵綔鐢ㄣ + +鍐呭瓨锛1. 璐熻矗纭洏绛夌‖浠朵笂鐨勬暟鎹笌CPU涔嬮棿鏁版嵁浜ゆ崲澶勭悊锛2. 缂撳瓨绯荤粺涓殑涓存椂鏁版嵁銆3. 鏂數鍚庢暟鎹涪澶便 +纭洏锛氬瓨鍌ㄨ祫鏂欏拰杞欢绛夋暟鎹殑璁惧锛屾湁瀹归噺澶э紝鏂數鏁版嵁涓嶄涪澶辩殑鐗圭偣銆備篃琚汉浠О涔嬩负鈥滄暟鎹粨搴撯濄 + +鐜板湪鎴戜滑鏉ヨ涓涓婥PU銆佺‖鐩樸佸唴瀛樹笁鑰呬箣闂寸殑鍏崇郴銆 + +棣栧厛 ,鎴戜滑鍏堝洖鎯充竴涓嬩笁鑰呯殑浣滅敤锛 + +CPU锛氭槸璁$畻鏈虹殑杩愮畻鏍稿績鍜屾帶鍒舵牳蹇冿紝璁╃數鑴戠殑鍚勪釜閮ㄤ欢椤哄埄宸ヤ綔锛岃捣鍒板崗璋冨拰鎺у埗浣滅敤銆 +纭洏锛氬瓨鍌ㄨ祫鏂欏拰杞欢绛夋暟鎹殑璁惧锛屾湁瀹归噺澶э紝鏂數鏁版嵁涓嶄涪澶辩殑鐗圭偣銆備篃琚汉浠О涔嬩负鈥滄暟鎹粨搴撯濄 +鍐呭瓨锛1. 璐熻矗纭洏绛夌‖浠朵笂鐨勬暟鎹笌CPU涔嬮棿鏁版嵁浜ゆ崲澶勭悊锛2. 缂撳瓨绯荤粺涓殑涓存椂鏁版嵁銆3. 鏂數鍚庢暟鎹涪澶便 + +鐒跺悗锛 鎴戜滑鍐嶆潵鐪嬩竴涓嬬▼搴忔槸濡備綍鎵ц璧锋潵鐨勩 + +褰撴垜浠湪鐢佃剳涓婃墦寮QQ鏃讹紙鍙抽敭-鎵撳紑 鎴栬呭弻鍑籕Q鍥炬爣锛夛紝鍏跺疄鏄氳繃榧犳爣锛堣緭鍏ヨ澶囷級鍚慍PU鍙戦佷簡涓鏉″懡浠わ紝CPU鎺ユ敹鍒拌繖鏉″懡浠ゅ悗锛孮Q绋嬪簭灏变粠纭洏閲岃鍔犺浇鍒板唴瀛橈紙鍔犺浇鏃朵笉閫氳繃澶勭悊鍣紝鐩存帴浠庣‖鐩樺姞杞界▼搴忓埌鍐呭瓨閲岋級锛屽姞杞藉畬鎴愬悗锛孋PU灏卞紑濮嬫墽琛孮Q绋嬪簭銆傜▼搴忔墽琛岃捣鏉ュ悗锛孋PU鍙互璁㏎Q绋嬪簭鏄剧ず鍦ㄦ垜浠殑鍦ㄦ樉绀哄櫒涓娿備篃灏辨槸浣犵湅鍒颁簡QQ 绋嬪簭杩愯璧锋潵浜嗐傚鏋滆繖涓椂鍊欙紝浣犵敤QQ鎴彇浜嗕竴寮犲睆骞曠殑鍥剧墖锛岄偅涔堣繖寮犲浘鐗囦細棣栧厛淇濆瓨鍒板唴瀛橈紝鍦ㄦ病鏈夐鍑烘埅灞忕姸鎬佹椂锛屼綘鍙互鍦ㄨ繖寮犲浘鐗囦笂鍐欏瓧銆佺敾绾挎潯锛岀瓑浣犲彸閿繚瀛樿繖寮犲浘鐗囩殑鏃跺欙紝杩欏紶鍥剧墖灏变細淇濆瓨鍒扮‖鐩橀噷銆 + +閫氳繃浜嗚В涓涓▼搴忔槸濡備綍杩愯璧锋潵鐨勶紝鎴戜滑灏卞彲浠ヤ簡瑙d笁鑰呮槸濡備綍宸ヤ綔鐨 銆 + +鍙兘鏈変簺浜轰細涓嶆槑鐧斤紝濡傛灉绋嬪簭鏄繖鏍锋墽琛岃捣鏉ョ殑璇濓紝閭d箞涓轰粈涔圕PU涓嶇洿鎺ュ湪纭洏閲屾墽琛岀▼搴忥紝鑰岄潪瑕佹妸绋嬪簭鏀惧埌鍐呭瓨鍚庡湪鎵ц鍛紵 + +鍥犱负閫熷害宸埆澶ぇ锛 +鍐呭瓨瀛樺彇鏁版嵁鐨勯熷害姣旂‖鐩樼殑瀛樺彇閫熷害蹇簡10鍊嶏紝 鍦ㄦ煇浜涚幆澧冮噷锛岀‖鐩樺拰鍐呭瓨涔嬮棿鐨勯熷害宸窛鍙兘浼氭洿澶с + +鑰孋PU鐨勯熷害姣斿唴瀛樹笉鐭ヨ繕瑕佸揩澶氬皯鍊嶃傚綋鎴戜滑鎶婄▼搴忎粠纭洏鏀惧埌鍐呭瓨浠ュ悗锛孋PU灏辩洿鎺ュ湪鍐呭瓨杩愯绋嬪簭锛岃繖鏍锋瘮CPU鐩存帴鍦ㄧ‖鐩樿繍琛岀▼搴忓氨瑕佸揩寰堝銆 + +鍐呭瓨瑙e喅浜嗕竴閮ㄥ垎CPU杩愯杩囧揩锛岃岀‖鐩樻暟鎹瓨鍙栧お鎱㈢殑闂銆 鎻愰珮浜嗘垜浠殑鐢佃剳鐨勮繍琛岄熷害銆 + +鍐呭瓨灏卞鍚屼竴鏉♀滈珮閫熻溅閬撯濅竴鑸紝鏁版嵁鐢变紶杈撻熷害杈冩參鐨勭‖鐩橀氳繃杩欐潯楂橀熻溅閬撲紶閫佽嚦CPU杩涜澶勭悊锛 + +浣嗗唴瀛樻槸甯︾數瀛樺偍鐨(涓鏃︽柇鐢垫暟鎹氨浼氭秷澶)锛岃屼笖瀹归噺鏈夐檺锛屾墍浠ヨ闀挎椂闂村偍瀛樼▼搴忔垨鏁版嵁灏遍渶瑕佷娇鐢ㄧ‖鐩樸 + +鍏跺疄鍐呭瓨鍦ㄨ繖閲岃捣浜嗕袱涓綔鐢細 + +1. 淇濆瓨浠庣‖鐩樿鍙栫殑鏁版嵁锛屾彁渚涚粰CPU浣跨敤 + +2. 淇濆瓨CPU鐨勪竴浜涗复鏃舵墽琛岀粨鏋滐紝浠ヤ究CPU涓嬫浣跨敤鎴栦繚瀛樺埌纭洏 + + + +鏁板瓧鐢靛瓙涓湁鍚勭閫昏緫鍔熻兘,鏈绠鍗曠殑鏄笌鎴栭潪,鏁板瓧鐢靛瓙涓敤01琛ㄧず鏁版嵁姣斿浣犺〃绀 12(10杩涘埗) + +12 = 8*1+4*1+2*0+1*0琛ㄧず涓 1100浣犲彲浠ヨ瘯璇曟墍鏈夋暟瀛楅兘鍙互杩欎箞琛ㄧず鍑烘潵,鑰1鍜0鍦ㄦ暟鐢典腑鐢"楂樼數骞","浣庣數骞"琛ㄧず,璇寸櫧浜嗗氨鏄氱數琛ㄧず1鏂數琛ㄧず0. + +纭呯墖涓婄殑闆嗘垚鐢佃矾鍙互鎶婅繖浜涢棬鐢佃矾浣滃埌闈炲父灏,杩欐牱浜轰滑鐢ㄥ悇绉嶉昏緫鐢佃矾灏辫兘瀹炵幇澶嶆潅鐨勮绠楀櫒鍔熻兘, + +鎴戜滑濮戜笖璁や负鍙兘绠楀姞鍑忎箻闄ゅ惂.浣犳兂绠椾粈涔堢畻寮忛兘鍙互鐢ㄩ昏緫鐢佃矾鏉ュ疄鐜.浣嗘槸浜轰滑寰寰鎯崇畻鐨勪笢瑗块兘姣旇緝澶嶆潅姣斿杩欎釜 + +1*1+2*2+3*3+4*4...10000000*10000000 + +濡傛灉涓娆¤繍绠楅渶瑕佷竴涓昏緫鐢佃矾,閭d箞杩欏氨闇瑕2*10000000涓昏緫鐢佃矾, + +棰,鎭曟垜鐩磋█,杩欎釜鎴愭湰鏈夌偣楂, + +浜庢槸浜轰滑鎯冲埌浜嗕竴涓ソ鍔炴硶,灏辨槸鍋氫竴涓昏緫鍣ㄤ欢,鏈2涓緭鍏,鍒嗗埆鏄暟鎹緭鍏,鎸囦护杈撳叆,鏁版嵁杈撳叆灏辨槸杈撳叆瑕佽繍绠楃殑涓や釜鏁版嵁,鎸囦护杈撳叆杈撳叆璁╄繖涓櫒浠跺共浠涔,姣斿00灏辨槸鍋氬姞娉,01灏辨槸鍋氬噺娉,10鏄箻娉,11鏄櫎娉,鐒跺悗缁欎粬閰嶄釜鍌ㄥ瓨璁惧鐢ㄦ潵淇濆瓨鏁版嵁,杩欐牱鎴戜滑璁╄繖涓涓澶囦笉鏂殑杩愯,鎸夌収涓瀹氳鍒欐妸杈撳嚭鐨勬暟鎹佸洖杈撳叆灏辫兘瀹屾垚涓婇潰鎻愬埌鐨勮繍绠椾簡. + +鍚庢潵杩欎釜璁惧琚О涓篊PU, + +CPU闄や簡杩愮畻閫昏緫浠ュ,杩樿兘鎵ц: 1 绉诲姩鏁版嵁 2 閫昏緫杩愮畻 3 璺宠浆鎸囦护 杩樻湁浜涙潅涓冩潅鍏殑鎸囦护 + +绉诲姩鏁版嵁灏辨槸鎶婅緭鍑虹殑鏁版嵁閫佸洖杈撳叆鐨勬寚浠,閫氬父鏁版嵁瀛樺偍鍦ㄥ瘎瀛樺櫒鎴栬匯AM涓,鍌ㄥ瓨鍣ㄥ氨鍍忔父娉抽鐨勬煖瀛,涓婇潰鏈変釜缂栧彿,閲岄潰瀛樹簡涓暟瀛.鍚庢潵缂栧彿琚▼搴忕尶绉颁负鍦板潃. + +鏈変簡杩欎簺鎴戜滑杩樻湁涓浠朵簨娌″仛,鎯虫兂鏄粈涔? + +灏辨槸鍛婅瘔CPU,浠婂ぉ瑕佸仛鐨勬寚浠,鏈寮濮嬫寚浠ゆ槸瀛樺偍鍦ㄧ┛瀛旂焊甯︿笂鐨,鐜板湪鏄偍瀛樺湪纭洏閲岀殑 + +CPU涓涓竴涓湴璇昏繘鏉,璇讳竴涓墽琛屼竴涓搷浣.鍥犳鎵ц涓婇潰鐨勬搷浣滆鍦ㄧ焊甯︿笂鎵 2*10000000涓互涓婄殑瀛,杩欎篃鏈夌偣楹荤儲 + +骞稿ソ鎴戜滑鐨勮繍绠楁槸鏈夎寰嬬殑,鍥犳鎴戜滑鍙互璁╃焊甯﹁蛋涓璧板掑甫鍐嶈蛋涓璧,鐩村埌鏌愪釜鏉′欢婊¤冻浜嗕负姝.杩欐牱鐨勬寚浠ゅ氨鏄浆绉绘寚浠ゃ + +from锛 + +http://www.guokr.com/question/482025/ + + + diff --git a/group08/406166841/2-26/CustomArrayList.java b/group08/406166841/2-26/CustomArrayList.java new file mode 100644 index 0000000000..46dbe3d40d --- /dev/null +++ b/group08/406166841/2-26/CustomArrayList.java @@ -0,0 +1,92 @@ +package net.iyouqu.bruceretrofit.util.java; + +import java.util.Arrays; + +/** + * Created by liq on 2017/2/25. + */ + +public class CustomArrayList implements List { + + private int size = 0; + + private final static int DEFAULT_CAPACITY = 10; + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; + + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + private String desc = "index瓒呰繃鐣岄檺"; + + @Override + public void add(Object o) { + isCapacityEnough(size + 1); + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + checkRangeForAdd(index); + isCapacityEnough(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object value = get(index); + int moveSize = size - index - 1; + if (moveSize > 0){ + System.arraycopy(elementData,index + 1, elementData,index,size - index - 1); + } + elementData[--size] = null; + return value; + } + + @Override + public int size() { + return size; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(desc); + } + } + + private void checkRangeForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(desc); + } + } + + private void explicitCapacity(int capacity) { + int newLength = elementData.length * 2; + if (newLength - capacity < 0) { + newLength = capacity; + } + if (newLength > (MAX_ARRAY_LENGTH)) { + newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + elementData = Arrays.copyOf(elementData, newLength); + } + + private void isCapacityEnough(int size) { + if (size > DEFAULT_CAPACITY) { + explicitCapacity(size); + } + if (size < 0) { + throw new OutOfMemoryError(); + } + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group08/406166841/2-26/CustomLinkedList.java b/group08/406166841/2-26/CustomLinkedList.java new file mode 100644 index 0000000000..532b9b8eaf --- /dev/null +++ b/group08/406166841/2-26/CustomLinkedList.java @@ -0,0 +1,172 @@ +package net.iyouqu.bruceretrofit.util.java; + +/** + * Created by liq on 2017/2/25. + */ + +public class CustomLinkedList implements List { + + //閾捐〃闀垮害 + private int size = 0; + //閾捐〃澶存寚閽 + private Node first; + //閾捐〃灏鹃儴鎸囬拡 + private Node last; + //鎿嶄綔娆℃暟 + private int modCount; + + @Override + public void add(Object o) { + linkLast(o); + } + + @Override + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linkLast(o); + } else { + linkBefore(o, node(index)); + } + } + + @Override + public Object get(int index) { + checkPositionIndex(index); + return node(index).data; + } + + @Override + public Object remove(int index) { + checkPositionIndex(index); + return unlink(node(index)); + } + + @Override + public int size() { + return size; + } + + /** + * 娣诲姞鑺傜偣鍒伴摼琛ㄥ熬閮 + */ + public void addLast(Object e) { + linkLast(e); + } + + /** + * 瑙i櫎浼犲叆鑺傜偣鐨勫睘鎬,骞朵笖灏嗕紶鍏ヨ妭鐐圭殑涓婁竴涓拰涓嬩竴涓妭鐐 閾炬帴銆備娇浼犲叆鑺傜偣鐨勫睘鎬 鍏ㄩ儴涓 null + */ + private Object unlink(Node node) { + //鑾峰彇褰撳墠鑺傜偣node鐨勫睘鎬 + final Object element = node.data; + final Node next = node.next; + final Node prev = node.prev; + if (prev == null) { + //涓婁竴涓妭鐐逛负null灏嗛鑺傜偣璁剧疆涓轰笅涓涓妭鐐 + first = next; + } else { + //涓婁竴涓妭鐐规湁 灏嗕笂涓涓妭鐐圭殑涓嬩竴涓妭鐐 璁剧疆涓哄綋鍓嶈妭鐐圭殑涓嬩竴涓妭鐐 + prev.next = next; + //灏嗗綋鍓嶈妭鐐圭殑涓婁竴涓妭鐐硅缃负null + node.prev = null; + } + if (next == null) { + //涓嬩竴涓妭鐐逛负null灏嗘湯灏捐妭鐐硅缃负涓婁竴涓妭鐐 + last = prev; + } else { + //灏嗕笅涓涓妭鐐圭殑涓婁竴涓妭鐐 璁剧疆涓哄綋鍓嶈妭鐐圭殑涓婁竴涓妭鐐 + next.prev = prev; + node.next = null; + } + node.data = null; + size--; + modCount++; + return element; + } + + /** + * 鑾峰彇涓涓妭鐐 + * 鍒ゆ柇index 鍦ㄥ墠鍗婂尯闂磋繕鏄悗鍗婂尯闂淬傝屼笉鏄竴鐩翠粠澶村埌灏炬悳绱 + * 灏嗚妭鐐硅闂鏉傚害浠嶰(n)鍙樹负O(n/2) + */ + private Node node(int index) { + checkPositionIndex(index); + if (index < (size / 2)) { + Node x = first; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) { + x = x.prev; + } + return x; + } + } + + /** + * 鍦ㄥ弬鏁拌妭鐐逛箣鍓嶆彃鍏ヤ竴涓妭鐐 + */ + private void linkBefore(Object element, Node node) { + //鑾峰彇娣诲姞鑺傜偣鐨勪笂涓涓妭鐐 + final Node pred = node.prev; + //鍒涘缓涓涓柊鑺傜偣 + final Node newNode = new Node<>(pred, element, node); + //娣诲姞鑺傜偣鐨勪笂涓涓妭鐐逛负 鏂拌妭鐐 + node.prev = newNode; + //鍒ゆ柇涓婁竴涓妭鐐规槸鍚︿负null + if (pred == null) { + //棣栬妭鐐硅缃负鏂板垱寤虹殑鑺傜偣 + first = newNode; + } else { + //涓婁釜鑺傜偣涓嶄负null銆傚皢鍏朵笅涓妭鐐硅缃负鏂板垱寤虹殑鑺傜偣銆 + pred.next = newNode; + } + size++; + modCount++; + } + + /** + * 閾炬帴 鑺傜偣鍒 last + */ + private void linkLast(Object e) { + final Node l = last; + final Node newNode = new Node<>(l, e, null); + last = newNode; + //鍒ゆ柇閾捐〃last鏄惁涓簄ull + if (l == null) { + //閾捐〃first鎸囧悜鏂版坊鍔犵殑 鑺傜偣 + first = newNode; + } else { + //閾捐〃last涓嶄负null灏嗛摼琛╨ast鑺傜偣鐨勭殑next璁剧疆涓烘柊鑺傜偣 + l.next = newNode; + } + size++; + modCount++; + } + + /** + * 妫鏌ndex鏄惁瓒婄晫 + */ + private void checkPositionIndex(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index瓒呰繃鐣岄檺"); + } + } + + private static class Node { + Object data; + //涓嬩竴涓妭鐐 + Node next; + //涓婁竴涓妭鐐 + Node prev; + public Node(Node prev, Object item, Node next) { + this.data = item; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group08/406166841/2-26/CustomQueue.java b/group08/406166841/2-26/CustomQueue.java new file mode 100644 index 0000000000..00f3d89f6d --- /dev/null +++ b/group08/406166841/2-26/CustomQueue.java @@ -0,0 +1,51 @@ +package net.iyouqu.bruceretrofit.util.java; + +/** + * Created by liq on 2017/2/25. + */ + +public class CustomQueue { + + Object[] data = null; + //瀹归噺 + private int capacity; + //闃熷熬鎸囬拡 + private int tail; + + CustomQueue(int initSize) { + if (initSize >= 0) { + this.capacity = initSize; + data = new Object[initSize]; + tail = 0; + } else { + throw new RuntimeException("鍒濆鍖栧ぇ灏忎笉鑳藉皬浜0" + initSize); + } + } + + public void enQueue(E o){ + ensureCapacity(); + data[tail] = o; + tail++; + } + + public E deQueue(){ + return (E) data[0]; + } + + public boolean isEmpty(){ + return tail == 0; + } + + public int size(){ + return tail; + } + + private void ensureCapacity() { + if (tail == capacity) { + capacity *= 2; + Object[] newData = new Object[capacity]; + System.arraycopy(data, 0, newData, 0, tail); + data = newData; + } + } +} diff --git a/group08/406166841/2-26/CustomStack.java b/group08/406166841/2-26/CustomStack.java new file mode 100644 index 0000000000..c83c7aee78 --- /dev/null +++ b/group08/406166841/2-26/CustomStack.java @@ -0,0 +1,70 @@ +package net.iyouqu.bruceretrofit.util.java; + +import java.util.ArrayList; + +/** + * Created by liq on 2017/2/25. + */ + +public class CustomStack { + + //閲嶈浇鍥犲瓙 + private static final float LOAD_FACTOR = 0.75f; + //闇瑕佹墿鍏呭閲忔椂鐨勫ぇ灏 + private int resizeCapacity; + private Object[] data = null; + //鏍堝閲 + private int capacity; + //鏍堥《 + private int top; + + public CustomStack(int initSize) { + if (initSize >= 0) { + this.capacity = initSize; + data = new Object[initSize]; + top = 0; + this.resizeCapacity = (int) (capacity * LOAD_FACTOR); + } else { + throw new RuntimeException("鍒濆鍖栧ぇ灏忎笉鑳藉皬浜0:" + initSize); + } + } + + private ArrayList elementData = new ArrayList(); + + public void push(E o){ + checkStackCapacity(); + data[top] = o; + top++; + } + + public E pop(){ + if(top<=0) + throw new RuntimeException("娌℃湁鍏冪礌涓嶈兘寮瑰嚭"); + E e = (E) data[top - 1]; + data[top-1] = null; + --top; + return e; + } + + public E peek(){ + + return (E) data[top - 1]; + + } + public boolean isEmpty(){ + return top == 0; + } + public int size(){ + return top; + } + + private void checkStackCapacity() { + if (top == resizeCapacity) { + capacity = capacity * 2; + Object[] newData = new Object[capacity]; + System.arraycopy(data, 0, newData, 0, top); + data = newData; + } + } + +} diff --git a/group08/406166841/2-26/Iterator.java b/group08/406166841/2-26/Iterator.java new file mode 100644 index 0000000000..2a90aac57b --- /dev/null +++ b/group08/406166841/2-26/Iterator.java @@ -0,0 +1,10 @@ +package net.iyouqu.bruceretrofit.util.java; + +/** + * Created by liq on 2017/2/25. + */ + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group08/406166841/2-26/List.java b/group08/406166841/2-26/List.java new file mode 100644 index 0000000000..b302910fb0 --- /dev/null +++ b/group08/406166841/2-26/List.java @@ -0,0 +1,13 @@ +package net.iyouqu.bruceretrofit.util.java; + +/** + * Created by liq on 2017/2/25. + */ + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/406166841/406166841.md b/group08/406166841/406166841.md new file mode 100644 index 0000000000..aef5098754 --- /dev/null +++ b/group08/406166841/406166841.md @@ -0,0 +1 @@ +406166841 \ No newline at end of file diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/ArrayList.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/ArrayList.java" new file mode 100644 index 0000000000..4e31d64920 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/ArrayList.java" @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class ArrayList implements List { + // size涓嶄粎鏄疉rrayList涓疄闄呭瓨鍊煎緱浣撶幇锛岀洿鎺dd鏃讹紝涔熸槸鍦╡lementData[size]澶勮繘琛 + private int size = 0; + // 鏁扮粍鏄繛缁笖鏈夊簭鐨 + private Object[] elementData; + + private void resize(int newSize) { + Object[] newArray = new Object[newSize]; + System.arraycopy(elementData, 0, newArray, 0, size); + elementData = newArray; + } + + public ArrayList() { + elementData = new Object[10]; + } + + // 娣诲姞鎸囧畾鍏冪礌鑷冲垪琛ㄥ熬 + public void add(Object o) { + if (size == elementData.length) { + resize(size * 2); + } + elementData[size++] = o; + } + + // 灏嗘寚瀹氬厓绱犳彃鍏ュ垪琛ㄤ腑鐨勬寚瀹氫綅缃傜Щ鍔ㄥ綋鍓嶄綅缃殑鍏冪礌锛堝鏋滄湁鐨勮瘽锛夊拰鍙宠竟鐨勫悗缁厓绱狅紙鍚戠储寮曟坊鍔犱竴涓厓绱狅級 + public void add(int index, Object o) { + rangeCheck(index); + if (size == elementData.length / 4) { + resize(elementData.length / 2); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + /* + * 鑾峰彇鎸囧畾浣嶇疆鍏冪礌 + * (non-Javadoc) + * @see com.coding.basic.List#get(int) + */ + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + /* + * 鍒犻櫎鎸囧畾浣嶇疆鍏冪礌 + * (non-Javadoc) + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index) { + rangeCheck(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return oldValue; + } + + public int size() { + return this.size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + // 妫娴嬬储寮曞硷紝褰撳墠鏈塻ize涓厓绱狅紝鍗犱簡elementData[0]鑷砮lementData[size-1],add鏃讹紝鍙兘鍦ㄥ凡鏈塠0,size-1]澶勬彃鍏ワ紝鎴栬呭湪鍒楄〃灏緎ize澶刟dd + // 鎵浠ndex鍦╗0,size涔嬮棿] + private void rangeCheck(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + + class ArrayListIterator implements Iterator { + int i = 0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + return elementData[i++]; + } + + } + +} diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/BinaryTreeNode.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/BinaryTreeNode.java" new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/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/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Iterator.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Iterator.java" new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/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/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/LinkedList.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/LinkedList.java" new file mode 100644 index 0000000000..b036d5ba91 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/LinkedList.java" @@ -0,0 +1,143 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private int size = 0; + // 澶寸粨鐐 + private Node head; + // 灏剧粨鐐 + private Node tail; + + private void rangeCheck(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(Object o) { + Node node = new Node(o, null); + if (head == null) { + head = tail = node; + } + Node oldTail = tail; + tail = node; + oldTail.next = tail; + size++; + } + + public void add(int index, Object o) { + rangeCheck(index); + if (index == size) { + this.add(o); + } else { + // 淇濆瓨index澶勮妭鐐 + Node x = head; + // 淇濆瓨index-1澶勭殑鑺傜偣 + Node y = null; + for (int i = 0; i < index; i++) { + y = x; + x = x.next; + } + Node node = new Node(o, x); + y.next = node; + size++; + } + } + + public Object get(int index) { + rangeCheck(index); + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x.data; + } + + public Object remove(int index) { + rangeCheck(index); + Object removeData; + if (index == 0) { + removeData = removeFirst(); + } else if (index == size - 1) { + removeData = removeLast(); + } else { + Node x = head; + Node y = head; + for (int i = 0; i < index; i++) { + y = x; + x = x.next; + } + y.next = x.next; + size--; + removeData = x.data; + } + return removeData; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node oldHead = head; + head = new Node(o, oldHead); + size++; + } + + public void addLast(Object o) { + Node oldTail = tail; + tail = new Node(o, null); + oldTail.next = tail; + size++; + } + + public Object removeFirst() { + Node oldHead = head; + head = oldHead.next; + size--; + return oldHead.data; + } + + public Object removeLast() { + Node oldTail = tail; + Node temp = head; + for (int i = 0; i < size - 2; i++) { + temp = temp.next; + } + tail = temp; + size--; + return oldTail.data; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + } + + private class LinkedListIterator implements Iterator { + Node x = head; + + @Override + public boolean hasNext() { + return x != null; + } + + @Override + public Object next() { + Object data = x.data; + x = x.next; + return data; + } + + } +} diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/List.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/List.java" new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/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/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Queue.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Queue.java" new file mode 100644 index 0000000000..5190e1b798 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Queue.java" @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedList; + + public Queue() { + linkedList = new LinkedList(); + } + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } +} diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Stack.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Stack.java" new file mode 100644 index 0000000000..a39871d1b4 --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Stack.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + + private LinkedList linkedList; + + public Stack() { + linkedList = new LinkedList(); + } + + public void push(Object o) { + linkedList.add(o); + } + + public Object pop() { + return linkedList.removeLast(); + } + + public Object peek() { + return linkedList.get(linkedList.size()-1); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } +} diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Test.java" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Test.java" new file mode 100644 index 0000000000..b26a11be8a --- /dev/null +++ "b/group08/529757467/2017-02-26\344\275\234\344\270\232/com/coding/basic/Test.java" @@ -0,0 +1,15 @@ +package com.coding.basic; + +public class Test { + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + for (int i = 0; i < 10; i++) { + linkedList.add(i); + } + for (int i = 0; i < 10; i++) { + linkedList.removeLast(); + } + linkedList.removeLast(); + } + +} diff --git "a/group08/529757467/2017-02-26\344\275\234\344\270\232/\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.doc" "b/group08/529757467/2017-02-26\344\275\234\344\270\232/\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.doc" new file mode 100644 index 0000000000..f518f0f97f Binary files /dev/null and "b/group08/529757467/2017-02-26\344\275\234\344\270\232/\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.doc" differ diff --git "a/group08/619057560/2-26/article/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.pdf" "b/group08/619057560/2-26/article/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.pdf" new file mode 100644 index 0000000000..f310b09e2d Binary files /dev/null and "b/group08/619057560/2-26/article/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.pdf" differ diff --git a/group08/619057560/2-26/code/com/coding/basic/ArrayList.java b/group08/619057560/2-26/code/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..5fe3123515 --- /dev/null +++ b/group08/619057560/2-26/code/com/coding/basic/ArrayList.java @@ -0,0 +1,80 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + elementData[size++] = o; + } + public void add(int index, Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Object old = elementData[index]; + size--; + System.arraycopy(elementData, index+1, elementData, index, size-index); + elementData[size] = null; + + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator { + + int cursor = 0; + + @Override + public boolean hasNext() { + return (cursor < size); + } + + @Override + public Object next() { + if (cursor < 0 || cursor >= size) { + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..af940807e6 --- /dev/null +++ b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if (data == null || ((Integer)data).intValue() == ((Integer)o).intValue()) { + data = o; + return this; + } + else if (((Integer)o).intValue() < ((Integer)data).intValue()) { + if (left == null) { + left = new BinaryTreeNode(); + } + return left.insert(o); + } + else { + if (right == null) { + right = new BinaryTreeNode(); + } + return right.insert(o); + } + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Iterator.java b/group08/619057560/2-26/code/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group08/619057560/2-26/code/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/group08/619057560/2-26/code/com/coding/basic/LinkedList.java b/group08/619057560/2-26/code/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d2f1422cfb --- /dev/null +++ b/group08/619057560/2-26/code/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; + + public void add(Object o){ + Node pNewNode = new Node(); + pNewNode.data = o; + + Node pNode = head; + + if (head == null) { + head = pNewNode; + return; + } + + while (pNode.next != null) { + pNode = pNode.next; + } + + pNode.next = pNewNode; + } + + public void add(int index , Object o){ + if (index < 0 && index > size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNewNode = new Node(); + pNewNode.data = o; + + if (index == 0) { + pNewNode.next = head; + head = pNewNode; + return; + } + + Node pNode = head; + while (--index > 0) { + pNode = pNode.next; + } + pNewNode.next = pNode.next; + pNode.next = pNewNode; + } + + public Object get(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + while (index-- > 0) { + pNode = pNode.next; + } + + return pNode.data; + } + + public Object remove(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + if (index == 0) { + head = head.next; + return pNode.data; + } + + while (--index > 0) { + pNode = pNode.next; + } + Node pTargetNode = pNode.next; + pNode.next = pTargetNode.next; + + return pTargetNode.data; + } + + public int size(){ + Node pNode = head; + int num = 0; + while (pNode != null) { + pNode = pNode.next; + num++; + } + return num; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + return remove(0); + } + + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + + Node pNode = head; + Node pPrevNode = null; + while (pNode.next != null) { + pPrevNode = pNode; + pNode = pNode.next; + } + if (pPrevNode != null) { + pPrevNode.next = pNode.next; + } + else { + head = null; + } + return pNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/List.java b/group08/619057560/2-26/code/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group08/619057560/2-26/code/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/group08/619057560/2-26/code/com/coding/basic/Queue.java b/group08/619057560/2-26/code/com/coding/basic/Queue.java new file mode 100644 index 0000000000..67a080f408 --- /dev/null +++ b/group08/619057560/2-26/code/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.addFirst(o); + } + + public Object deQueue(){ + return queueList.removeLast(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Stack.java b/group08/619057560/2-26/code/com/coding/basic/Stack.java new file mode 100644 index 0000000000..481c88bed7 --- /dev/null +++ b/group08/619057560/2-26/code/com/coding/basic/Stack.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group08/619057560/2-26/code/com/coding/test/Main.java b/group08/619057560/2-26/code/com/coding/test/Main.java new file mode 100644 index 0000000000..c4b5b4b74d --- /dev/null +++ b/group08/619057560/2-26/code/com/coding/test/Main.java @@ -0,0 +1,103 @@ +package com.coding.test; + +import com.coding.basic.ArrayList; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class Main { + + private static void printArrayList(ArrayList list) { + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + Iterator itr = list.iterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + } + } + + private static void printLinkedList(LinkedList list) { + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + + private static void printBinaryTree(BinaryTreeNode node) { + if (node == null) + return; + + printBinaryTree(node.getLeft()); + if (node.getData() != null) + System.out.println(node.getData()); + printBinaryTree(node.getRight()); + } + + private static void testArrayList() { + ArrayList arrayList = new ArrayList(); + arrayList.add(new Integer(1)); + arrayList.add(new Integer(2)); + arrayList.add(new Integer(3)); + arrayList.remove(2); + arrayList.add(new Integer(4)); + arrayList.add(0,new Integer(5)); + printArrayList(arrayList); + } + + private static void testLinkedList() { + LinkedList linkedList = new LinkedList(); + linkedList.add(new Integer(1)); + linkedList.add(new Integer(2)); + linkedList.add(new Integer(3)); + linkedList.remove(2); + linkedList.add(new Integer(4)); + linkedList.add(0,new Integer(5)); + linkedList.removeFirst(); + printLinkedList(linkedList); + } + + private static void testStack() { + Stack stack = new Stack(); + stack.push(new Integer(1)); + stack.push(new Integer(2)); + stack.push(new Integer(3)); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + stack.push(new Integer(4)); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + //System.out.println(stack.pop()); + } + + private static void testQueue() { + Queue queue = new Queue(); + queue.enQueue(new Integer(1)); + queue.enQueue(new Integer(2)); + queue.enQueue(new Integer(3)); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + //System.out.println(queue.deQueue()); + } + + private static void testBinaryTree() { + BinaryTreeNode root = new BinaryTreeNode(); + root.insert(new Integer(4)); + root.insert(new Integer(3)); + root.insert(new Integer(5)); + root.insert(new Integer(1)); + root.insert(new Integer(8)); + root.insert(new Integer(2)); + root.insert(new Integer(7)); + root.insert(new Integer(6)); + printBinaryTree(root); + } + + public static void main(String[] args) { + testBinaryTree(); + } +} diff --git a/group08/619057560/CHHSAlex.md b/group08/619057560/CHHSAlex.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group08/619057560/CHHSAlex.md @@ -0,0 +1 @@ + diff --git a/group08/648354678/README.md b/group08/648354678/README.md new file mode 100644 index 0000000000..20b57abd22 --- /dev/null +++ b/group08/648354678/README.md @@ -0,0 +1,4 @@ +# 娴嬭瘯涓婁紶鏂囦欢鍒癵it +## 绗竴娆′笂浼犱互浣滃涔 +## QQ锛648354678 +### 甯屾湜璺熷ぇ瀹跺叡鍚屽涔犺繘姝 \ No newline at end of file diff --git a/group08/649859235/2-26/article.md b/group08/649859235/2-26/article.md new file mode 100644 index 0000000000..556350e58d --- /dev/null +++ b/group08/649859235/2-26/article.md @@ -0,0 +1,2 @@ +鏂囩珷鍦板潃 +http://note.youdao.com/noteshare?id=a0345eff655b1cfc5877cc267452eed0&sub=FE5CD301B3C742269D04973B0DD2393C diff --git a/group08/649859235/2-26/com/vvv/base/ArrayList.java b/group08/649859235/2-26/com/vvv/base/ArrayList.java new file mode 100644 index 0000000000..e250afe294 --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/ArrayList.java @@ -0,0 +1,68 @@ +package com.vvv.base; + +public class ArrayList implements IList { + + private static final int DEFAULT_SIZE = 100; + + private int size; + private Object[] data = new Object[DEFAULT_SIZE]; + + @Override + public void add(Object o) { + if (size > data.length) { + increase(); + } + data[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + check(index); + + if (index > data.length - 1) { + increase(); + } + System.arraycopy(data, index, data, index + 1, size - index); + data[index] = o; + size++; + } + + private void check(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index: " + index + ", size: " + + size); + } + + private void increase() { + Object[] newData = new Object[DEFAULT_SIZE * 2]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + + @Override + public Object get(int index) { + check(index); + return data[index]; + } + + @Override + public Object remove(int index) { + if (index > 0 && index < size) { + Object d = data[index]; + int diff = size - index - 1; + if (diff > 0) + System.arraycopy(data, index + 1, data, index, size - index - 1); + size--; + data[size] = null; + return d; + } + return null; + } + + @Override + public int size() { + return this.size; + } + +} diff --git a/group08/649859235/2-26/com/vvv/base/BinaryTreeNode.java b/group08/649859235/2-26/com/vvv/base/BinaryTreeNode.java new file mode 100644 index 0000000000..a5d5ee1d2a --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.vvv.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/group08/649859235/2-26/com/vvv/base/IList.java b/group08/649859235/2-26/com/vvv/base/IList.java new file mode 100644 index 0000000000..3b449fab3f --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/IList.java @@ -0,0 +1,9 @@ +package com.vvv.base; + +public interface IList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/649859235/2-26/com/vvv/base/LinkedList.java b/group08/649859235/2-26/com/vvv/base/LinkedList.java new file mode 100644 index 0000000000..fa03aba962 --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/LinkedList.java @@ -0,0 +1,117 @@ +package com.vvv.base; + +public class LinkedList implements IList { + private int size; + private Node head; + private Node tail; + + @Override + public void add(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + tail = node; + } else { + tail.next = node; + node.prev = tail; + tail = node; + } + size++; + } + + @Override + public void add(int index, Object o) { + check(index); + Node node = new Node(o); + Node temp = head; + for (int i = 0; i < index; i++) { + temp = temp.next; + } + if (temp.prev != null && temp.next != null) { + Node prevNode = temp.prev; + prevNode.next = node; + node.prev = prevNode; + node.next = temp; + temp.prev = node; + } else if (temp.prev == null) { + head = node; + temp.prev = node; + node.next = temp; + } else if (temp.next == null) { + temp.next = node; + node.prev = temp; + tail = node; + } + size++; + } + + @Override + public Object get(int index) { + check(index); + Node temp = head; + for (int i = 0; i < index; i++) { + temp = temp.next; + } + return temp.data; + } + + @Override + public Object remove(int index) { + check(index); + Node temp = head; + for (int i = 0; i < index; i++) { + temp = temp.next; + } + Object oldValue = temp.data; + if (temp.prev != null && temp.next != null) { + temp.prev.next = temp.next; + temp.next.prev = temp.prev; + temp = null; + } else if (temp.prev == null) { + head = temp.next; + temp.next.prev = null; + temp = null; + } else if (temp.next == null) { + tail = temp.prev; + temp.prev.next = null; + temp = null; + } + size--; + return oldValue; + } + + public boolean remove(Object obj) { + Node temp = head; + for (int i = 0; i < size; i++) { + if (obj.equals(temp.data)) { + remove(i); + return true; + } + temp = temp.next; + } + return false; + } + + private void check(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index: " + index + ", size: " + + size); + } + + @Override + public int size() { + return size; + } + + @SuppressWarnings("hiding") + private static class Node { + Object data; + Node next; + Node prev; + + public Node(Object data) { + this.data = data; + } + } + +} diff --git a/group08/649859235/2-26/com/vvv/base/Queue.java b/group08/649859235/2-26/com/vvv/base/Queue.java new file mode 100644 index 0000000000..f97bd9d298 --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/Queue.java @@ -0,0 +1,73 @@ +package com.vvv.base; + +public class Queue { + + QueueNode head; + QueueNode tail; + private int size; + + public Queue() { + this.head = null; + this.tail = null; + } + + public void enQueue(Object o) { + if (head == null && tail == null) { + tail = new QueueNode(o); + head = tail; + } else { + QueueNode node = new QueueNode(o); + tail.next = node; + tail = tail.next; + } + size++; + } + + public Object deQueue() { + if (head == null) { + return null; + } + + if (head == tail && tail != null) { + QueueNode node = head; + tail = null; + head = null; + size--; + return node.data; + } + + Object obj = head.data; + head = head.next; + size--; + return obj; + } + + public boolean isEmpty() { + if(head==null && tail == null) + return true; + return false; + } + + public int size() { + return size; + } + +} + +class QueueNode { + Object data; + QueueNode next; + + public QueueNode() { + this(null, null); + } + + public QueueNode(Object data) { + this(data, null); + } + + public QueueNode(Object data, QueueNode next) { + this.data = data; + this.next = next; + } +} diff --git a/group08/649859235/2-26/com/vvv/base/Stack.java b/group08/649859235/2-26/com/vvv/base/Stack.java new file mode 100644 index 0000000000..5e3ed32e14 --- /dev/null +++ b/group08/649859235/2-26/com/vvv/base/Stack.java @@ -0,0 +1,30 @@ +package com.vvv.base; + +public class Stack { + private ArrayList data = new ArrayList(); + + public void push(Object o) { + data.add(o); + } + + public Object pop() { + return data.remove(data.size() - 1); + } + + public Object peek() { + if (data.size() > 0) { + return data.get(data.size() - 1); + } + return null; + } + + public boolean isEmpty() { + if (data.size() > 0) + return false; + return true; + } + + public int size() { + return data.size(); + } +} diff --git a/group08/649859235/README.md b/group08/649859235/README.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group08/649859235/README.md @@ -0,0 +1 @@ + diff --git a/group08/729770920/.gitignore b/group08/729770920/.gitignore new file mode 100644 index 0000000000..6b468b62a9 --- /dev/null +++ b/group08/729770920/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/group08/729770920/2-26/resource/report.txt b/group08/729770920/2-26/resource/report.txt new file mode 100644 index 0000000000..f256165498 --- /dev/null +++ b/group08/729770920/2-26/resource/report.txt @@ -0,0 +1,5 @@ +CPU鏄绠楁満鐨勫ぇ鑴戯紝鐢辫繍绠楀櫒鍜屽瘎瀛樺櫒缁勬垚銆傚彲浠ュ偍瀛樻寚浠ゅ拰鏁版嵁锛屽苟鎵ц鎸囦护杩涜绠鍗曠殑璁$畻銆 +鎸囦护鏄疌PU鍙互鐩存帴鎵ц鐨勫懡浠ゃ +鍐呭瓨鏄墍璋撶殑RAM锛岃鍐欓熷害杈冨揩銆傚父鐢ㄦ潵浣滀负CPU鍜岀‖鐩樹箣闂存暟鎹氦鎹㈢殑缂撳瓨銆 +纭洏瀹归噺鏋佸ぇ锛屼絾鏄鍐欓熷害姣斿唴瀛樻參寰堝銆傜敤鏉ュ瓨鏀剧▼搴忕殑浜岃繘鍒剁爜锛岄煶棰戯紝瑙嗛绛夌瓑鏁版嵁璧勬簮銆 +绋嬪簭杩愯鏃讹紝鏁版嵁鐢辩‖鐩樻嫹璐濆埌鍐呭瓨锛孋PU鍐嶄粠鍐呭瓨閲屽姞杞藉寘鍚寚浠ょ殑鏁版嵁锛岄愪竴鎵ц銆 \ No newline at end of file diff --git a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8fe149f4ad --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java @@ -0,0 +1,111 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size; + + private E[] data; + + public void add(E e){ + add(size, e); + } + + public ArrayList() { + clear(); + } + + public ArrayList(int capacity) { + clear(); + ensureCapacity(capacity); + } + + public void add(int index, E e){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + if (data.length == size) { + ensureCapacity(size * 2 + 1); + } + for (int i = size++; i > index; --i) { + data[i] = data[i - 1]; + } + data[index] = e; + } + + public E get(int index){ + return data[index]; + } + + public E remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + E copy = data[index]; + --size; + for (int i = index; i < size; ++i) { + data[i] = data[i + 1]; + } + return copy; + } + + public boolean contains(E e) { + for (int i = 0; i < size; ++i) { + if (data[i] == e) { + return true; + } + } + return false; + } + + public void clear() { + size = 0; + data = (E[]) new Object[0]; + } + + public int size(){ + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public void ensureCapacity(int capacity) { + E[] newData = (E[]) new Object[capacity]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + public void trimToSize() { + E[] newData = (E[]) new Object[size]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + private class ArrayListIterator implements Iterator { + int current = 0; + + public boolean hasNext() { + return current < size; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return data[current++]; + } + + public void remove() { + ArrayList.this.remove(current); + } + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Iterator.java b/group08/729770920/2-26/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f0e2eddde8 --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/LinkedList.java b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..899e0be08e --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + private int size = 0; + private Node head = new Node<>(); + private Node tail = new Node<>(); + + public LinkedList() { + head.next = tail; + tail.prev = head; + } + + public void add(E e) { + addLast(e); + } + + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head; + for (int i = 0, num = index; i < num; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.next = cursor.next.prev = new Node(e, cursor, cursor.next); + ++size; + } + + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + return cursor.data; + } + + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.prev.next = cursor.next; + cursor.next.prev = cursor.prev; + --size; + return cursor.data; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + add(0, e); + } + + public void addLast(E e) { + add(size, e); + } + + public E removeFirst() { + return remove(0); + } + + public E removeLast() { + return remove(size-1); + } + + public void clear() { + while (!isEmpty()) { + removeFirst(); + } + } + + public boolean contains(E e) { + Iterator it = this.iterator(); + while (it.hasNext()) { + if (it.next() == e) { + return true; + } + } + return false; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + E data = null; + Node prev = null; + Node next = null; + + public Node() { + } + + public Node(E e, Node p, Node n) { + data = e; + prev = p; + next = n; + } + } + + private class LinkedListIterator implements Iterator { + Node currentNode = head.next; + + public boolean hasNext() { + return currentNode != tail; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + E data = currentNode.data; + currentNode = currentNode.next; + return data; + } + + public void remove() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Node nextNode = currentNode.next; + currentNode.next.prev = currentNode.prev; + currentNode.prev.next = currentNode.next; + currentNode = nextNode; + --size; + } + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/List.java b/group08/729770920/2-26/src/com/coding/basic/List.java new file mode 100644 index 0000000000..babed5a71f --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/List.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public interface List { + void add(E e); + + void add(int index, E e); + + void clear(); + + E get(int index); + + E remove(int index); + + int size(); + + boolean contains(E e); + + Iterator iterator(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Queue.java b/group08/729770920/2-26/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a660ad6729 --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList data = new LinkedList<>(); + + public void enQueue(E e){ + data.addFirst(e); + } + + public Object deQueue() { + return data.removeLast(); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size(){ + return data.size(); + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Stack.java b/group08/729770920/2-26/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..c7e7773de5 --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private LinkedList data = new LinkedList<>(); + + public void push(E e) { + data.addFirst(e); + } + + public Object pop() { + return data.removeFirst(); + } + + public Object peek() { + return data.get(0); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size() { + return data.size(); + } +} diff --git a/group08/729770920/README.md b/group08/729770920/README.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group08/769638826/769638826.md b/group08/769638826/769638826.md new file mode 100644 index 0000000000..4573496090 --- /dev/null +++ b/group08/769638826/769638826.md @@ -0,0 +1,2 @@ +###鑷垜浠嬬粛 + diff --git a/group08/782476895/20170225/ArrayList.java b/group08/782476895/20170225/ArrayList.java new file mode 100644 index 0000000000..7b94eebd36 --- /dev/null +++ b/group08/782476895/20170225/ArrayList.java @@ -0,0 +1,55 @@ +package com.sl.test20170221; + +import java.util.Arrays; + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + grow(elementData); + elementData[size] = o; + } + + @Override + public void add(int index, Object o) { + grow(elementData); + for(int i = size - 1;i >= index;i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + } + + @Override + public Object get(int index) { + return elementData[index]; + } + + @Override + public Object remove(int index) { + size--; + for(int i = index;i < size;i++){ + elementData[i] = elementData[i+1]; + } + elementData[size] = null; + return elementData; + } + + @Override + public int size() { + + return size; + } + + private void grow(Object[] elementData){ + size++; + if(size >= elementData.length){ + int newSize = elementData.length + 100; + elementData = Arrays.copyOf(elementData, newSize); + } + } + +} diff --git a/group08/782476895/20170225/LinkedList.java b/group08/782476895/20170225/LinkedList.java new file mode 100644 index 0000000000..30ba21046f --- /dev/null +++ b/group08/782476895/20170225/LinkedList.java @@ -0,0 +1,127 @@ +package com.sl.test20170221; + +public class LinkedList implements List{ + private Node root; + int index; + + public void addNode(String name){ + if(root == null){ + root = new Node(name); + }else{ + root.add(name); + } + } + + + + + class Node{ + Object data; + Node next; + + + Node(Object data){ + this.data = data; + } + //娣诲姞鑺傜偣 + public void add(Object data){ + if(this.next == null){ + this.next = new Node(data); + }else{ + this.next.add(data); + } + } + //鍒犻櫎鑺傜偣 + public Object del(int i){ + if(this.next != null){ + index++; + if(i == index){ + this.next = this.next.next; + return this.next.data; + }else{ + this.next.del(i); + } + } + return null; + } + + + //閬嶅巻 + public void traversal(){ + if(this.next != null){ + index++; + this.next.traversal(); + } + } + //鎸囧畾浣嶇疆澧炲姞 + public void add(int i, Object o){ + if(this.next != null){ + if(i == index){ + Node node = new Node(data); + node.next = this.next.next; + this.next = node; + return ; + }else{ + this.next.add(i,o); + } + index++; + } + } + + //寰楀埌鎸囧畾鐨勮妭鐐 + public Object get(int i){ + if(this.next != null){ + + if(i == index){ + return this.data; + }else{ + this.next.get(i); + } + index++; + } + return null; + } + + } + + @Override + public void add(Object data) { + if(root == null){ + root = new Node(data); + }else{ + root.add(data); + } + } + + @Override + public void add(int index, Object o) { + if(root != null){ + root.add(index, o); + } + } + + @Override + public Object get(int index) { + if(root.next != null){ + return root.get(index); + } + return null; + } + + @Override + public Object remove(int index) { + if(root != null){ + return root.del(index); + } + return null; + } + + @Override + public int size() { + if(root != null){ + root.traversal(); + } + return index; + } + +} diff --git a/group08/782476895/20170225/List.java b/group08/782476895/20170225/List.java new file mode 100644 index 0000000000..08160598b8 --- /dev/null +++ b/group08/782476895/20170225/List.java @@ -0,0 +1,9 @@ +package com.sl.test20170221; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/782476895/20170225/Queue.java b/group08/782476895/20170225/Queue.java new file mode 100644 index 0000000000..3c4a3c8256 --- /dev/null +++ b/group08/782476895/20170225/Queue.java @@ -0,0 +1,65 @@ +package com.sl.test20170221; + +import com.sl.test20170221.LinkedList.Node; + +public class Queue { + + private Node first; + private int index; + + class Node{ + Object data; + Node next; + + + Node(Object data){ + this.data = data; + } + //娣诲姞鑺傜偣 + public void add(Object data){ + if(this.next == null){ + this.next = new Node(data); + }else{ + this.next.add(data); + } + } + + //閬嶅巻 + public void traversal(){ + if(this.next != null){ + index++; + this.next.traversal(); + } + } + } + + public void enQueue(Object o){ + if(first != null){ + first.add(o); + } + } + + public Object deQueue(){ + if(first != null){ + Object obj = first.data; + first = first.next; + return obj; + } + return null; + } + + public boolean isEmpty(){ + if(first == null){ + return true; + }else{ + return false; + } + } + + public int size(){ + if(first != null){ + first.traversal(); + } + return index; + } +} diff --git a/group08/782476895/20170225/Stack.java b/group08/782476895/20170225/Stack.java new file mode 100644 index 0000000000..50739dc7f3 --- /dev/null +++ b/group08/782476895/20170225/Stack.java @@ -0,0 +1,31 @@ +package com.sl.test20170221; + +public class Stack { + private List elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int size = elementData.size(); + Object obj = elementData.remove(size--); + return obj; + } + + public Object peek(){ + int size = elementData.size(); + return elementData.get(size - 1); + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } +} diff --git "a/group08/782476895/20170225/\346\217\217\350\277\260CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group08/782476895/20170225/\346\217\217\350\277\260CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..0449ce2f36 Binary files /dev/null and "b/group08/782476895/20170225/\346\217\217\350\277\260CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group08/875325254/2-26/ArrayList.java b/group08/875325254/2-26/ArrayList.java new file mode 100644 index 0000000000..d90caa7d50 --- /dev/null +++ b/group08/875325254/2-26/ArrayList.java @@ -0,0 +1,37 @@ +import java.util.Arrays; + +/** + * Created by Great on 2017/2/25. + */ +public class ArrayList { + private final int DEFAULT_SIZE = 20; + int[] array = new int[DEFAULT_SIZE]; + int size = 0; + public void add(int e) { + if (size == array.length) { + array = Arrays.copyOf(array, array.length + DEFAULT_SIZE); + } + array[size] = e; + ++size; + } + + public Integer get(int i) { + if(i < 0 || i >= size) return null; + return array[i]; + } + + public int size() { + return size; + } + + public void remove(int i) { + if (i < 0 || i >= size) return; + for (int j = i; j < size; j++) { + array[j] = array[j + 1]; + } + --size; + } + public boolean isEmpty() { + return size == 0; + } +} diff --git a/group08/875325254/2-26/LinkList.java b/group08/875325254/2-26/LinkList.java new file mode 100644 index 0000000000..a6b785ee79 --- /dev/null +++ b/group08/875325254/2-26/LinkList.java @@ -0,0 +1,73 @@ +/** + * Created by Great on 2017/2/24. + */ +public class LinkList { + private class Node{ + Node next; + int data; + } + private Node rear; + private Node top; + private int size = 0; + public void add(int e) { + if (rear == null) { + rear = new Node(); + top = rear; + }else { + rear.next = new Node(); + rear = rear.next; + } + rear.data = e; + ++size; + } + + public Integer getReciprocal(int count) { + if (top == null || count < 1) return null; + Node node = top; + int size = 0; + while( node.next != null) { + node = node.next; + size++; + } + if (size+1 < count) return null; + return get(size+1-count); + } + + public Integer get(int i) { + if (i < 0 || i >= size) return null; + return getNode(i).data; + } + + public void remove(int i) { + if (i < 0 || i >= size) return; + if (i == 0 ) { + top = top.next; + return; + } + if (i == size - 1) { + getNode(i - 1).next = null; + } + Node front = getNode(i - 1); + Node back = getNode(i + 1); + front.next = back; + --size; + } + + private Node getNode(int i) { + if (i < 0 || i >= size) return null; + Node node = top; + for (int j = 0; j < size; j++) { + if (j == i) return node; + node = node.next; + } + return null; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } +} diff --git a/group08/875325254/2-26/Queue.java b/group08/875325254/2-26/Queue.java new file mode 100644 index 0000000000..6e3b80d7d2 --- /dev/null +++ b/group08/875325254/2-26/Queue.java @@ -0,0 +1,41 @@ +import java.util.Arrays; + +/** + * Created by Great on 2017/2/23. + */ +public class Queue { + private class Node{ + Node next; + int data; + } + private int size; + private Node front; + private Node rear; + public void add(int e) { + if (front == null) { + front = new Node(); + rear = front; + }else { + front.next = new Node(); + front = front.next; + } + front.data = e; + ++size; + } + + public Integer poll() { + if (rear == null) return null; + int data = rear.data; + rear = rear.next; + --size; + return data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group08/875325254/2-26/Stack.java b/group08/875325254/2-26/Stack.java new file mode 100644 index 0000000000..78b9884ccb --- /dev/null +++ b/group08/875325254/2-26/Stack.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +/** + * Created by Great on 2017/2/23. + */ +public class Stack { + private final int DEFAULT_SIZE = 20; + private int base; + private int top; + private int[] array = new int[DEFAULT_SIZE]; + public void push(Integer e) { + if (top == array.length){ + array = Arrays.copyOf(array, array.length + DEFAULT_SIZE); + } + array[top] = e; + ++top; + } + public Integer pop(){ + if (top == 0) return null; + top--; + return array[top]; + } + public int size() { + return top; + } + public boolean isEmpty(){ + return top == 0; + } +} diff --git a/group08/875325254/2-26/Test.java b/group08/875325254/2-26/Test.java new file mode 100644 index 0000000000..b397fd3e16 --- /dev/null +++ b/group08/875325254/2-26/Test.java @@ -0,0 +1,35 @@ +import java.util.List; + +/** + * Created by Great on 2017/2/23. + */ +public class Test { + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + LinkList linkList = new LinkList(); + Queue queue = new Queue(); + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + arrayList.add(i); + linkList.add(i); + queue.add(i); + stack.push(i); + } + System.out.println("ArrayList:"); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println(arrayList.get(i)); + } + System.out.println("LinkList:"); + for (int i = 0; i < linkList.size(); i++) { + System.out.println(linkList.get(i)); + } + System.out.println("Queue:"); + while (!queue.isEmpty()) { + System.out.println(queue.poll()); + } + System.out.println("Stack:"); + while (!stack.isEmpty()) { + System.out.println(stack.pop()); + } + } +} diff --git a/group08/875325254/875325254.md b/group08/875325254/875325254.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group08/875325254/875325254.md @@ -0,0 +1 @@ + diff --git a/group08/group08.md b/group08/group08.md index 8b13789179..d3f5a12faa 100644 --- a/group08/group08.md +++ b/group08/group08.md @@ -1 +1 @@ - + diff --git a/group09/group09.md b/group09/group09.md index 8b13789179..d3f5a12faa 100644 --- a/group09/group09.md +++ b/group09/group09.md @@ -1 +1 @@ - + diff --git a/group10/group10.md b/group10/group10.md index 8b13789179..d3f5a12faa 100644 --- a/group10/group10.md +++ b/group10/group10.md @@ -1 +1 @@ - + diff --git a/group11/group11.md b/group11/group11.md index 8b13789179..d3f5a12faa 100644 --- a/group11/group11.md +++ b/group11/group11.md @@ -1 +1 @@ - + 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"); + } + + /** + * 涓や釜鍙傛暟鐨刟dd鏂规硶 + */ + @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"); + } + + /** + * 涓や釜鍙傛暟鐨刟dd鏂规硶 + */ + @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() { + // 娴嬭瘯鏂板涓嬫爣寮傚父鏃讹紝鏄惁鍙互姝g‘鎶涘嚭寮傚父 + arrayList.add(-1, 2); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + // 娴嬭瘯鏂板涓嬫爣寮傚父鏃讹紝鏄惁鍙互姝g‘鎶涘嚭寮傚父 + 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/group12/group12.md b/group12/group12.md index 8b13789179..d3f5a12faa 100644 --- a/group12/group12.md +++ b/group12/group12.md @@ -1 +1 @@ - + diff --git a/group13/group13.md b/group13/group13.md index 8b13789179..d3f5a12faa 100644 --- a/group13/group13.md +++ b/group13/group13.md @@ -1 +1 @@ - + diff --git a/group14/group14.md b/group14/group14.md index 8b13789179..d3f5a12faa 100644 --- a/group14/group14.md +++ b/group14/group14.md @@ -1 +1 @@ - + diff --git a/group15/group15.md b/group15/group15.md index 8b13789179..d3f5a12faa 100644 --- a/group15/group15.md +++ b/group15/group15.md @@ -1 +1 @@ - + 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 + "--鐨刧et鏃犳晥,鍥犱负瓒婄晫浜嗭紒"); + return null; + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > getFlag()) { + System.out.println("鍦--" + index + "--鐨剅emove鏃犳晥,鍥犱负瓒婄晫浜嗭紒"); + 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; // 若新容量还是不够,直接扩为需求值 + } + 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){ + //添加一个元素的逻辑非常简单,只需要判断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("鍒濆鍖栭槦鍒,鍏冪礌涓篴,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("鍒濆鍖栨爤,鍏冪礌涓篴,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; + +/** + * 鑷畾涔夊疄鐜癆rrayList鐨勬暟鎹粨鏋 + * @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; +/** + * 鑷畾涔夊疄鐜癓inkedList鏁版嵁缁撴瀯 + * @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("涓嶅厑璁竛ull鐨勫厓绱犳彃鍏!"); + 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/group16.md b/group16/group16.md index 8b13789179..d3f5a12faa 100644 --- a/group16/group16.md +++ b/group16/group16.md @@ -1 +1 @@ - + 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/group17/102228177/.classpath b/group17/102228177/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group17/102228177/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/102228177/.project b/group17/102228177/.project new file mode 100644 index 0000000000..7f56ab1057 --- /dev/null +++ b/group17/102228177/.project @@ -0,0 +1,17 @@ + + + 102228177 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/102228177/src/data2_19/ArrayList.java b/group17/102228177/src/data2_19/ArrayList.java new file mode 100644 index 0000000000..35a17dcf59 --- /dev/null +++ b/group17/102228177/src/data2_19/ArrayList.java @@ -0,0 +1,148 @@ +package data2_19; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + + +public class ArrayList implements List{ + public static final int defLen = 10; + private Object[] elements; + private int size; + private int maxLen; + + public ArrayList(){ + size = 0; + maxLen = defLen; + elements = new Object[defLen]; + } + + /** + * 鍦ˋrrayList鏈熬澶勮拷鍔犲厓绱 + * @param o 娣诲姞鐨勫厓绱 + */ + public void add(Object o){ + if(size >= maxLen){ + grow(); + } + elements[size] = o; + size++; + } + + /** + * 鏁扮粍鎵╁ + */ + private void grow(){ + maxLen = maxLen + (maxLen >> 1); + Object[] newArr = new Object[maxLen]; + System.arraycopy(elements, 0, newArr, 0, size); + elements = newArr; + } + + /** + * 鍦ㄦ寚瀹氱储寮曞娣诲姞鍏冪礌 + * @param i 鎸囧畾绱㈠紩 + * @param o 娣诲姞鍏冪礌 + */ + public void add(int i,Object o){ + //鍒ゆ柇鎻掑叆浣嶇疆澶т簬鏁扮粍瀹為檯闀垮害 + if(i > size){ + size = i; + if(size >= maxLen){//鏁扮粍澶у皬澶т簬鏁扮粍鏈澶у閲忓垯闇瑕佹墿瀹 + grow(); + } + } + //鎻掑叆浣嶇疆涓嶅ぇ浜庢暟缁勫疄闄呴暱搴︽椂锛屽皢鎻掑叆浣嶇疆鐨勫厓绱犲悜鍚庣Щ銆 + for (int j = size; j > i ; j++) { + elements[j] = elements[j-1]; + } + elements[i] = o; + size++; + } + + /** + * 鑾峰彇浼犲叆绱㈠紩鐨勫厓绱 + * @param index 绱㈠紩 + * @return 杩斿洖浼犲叆绱㈠紩鐨勫厓绱 + */ + public Object get(int index){ + //绱㈠紩涓嶅湪瀹為檯鑼冨洿鍐 + if(index < 0||index >= size){ + throw new ArrayIndexOutOfBoundsException(); + } + for (int i = 0; i < size; i++) { + return elements[index]; + } + return null; + } + + /** + * 鍒犻櫎鎸囧畾绱㈠紩鍏冪礌骞惰繑鍥 + * @param index + * @return 璇ョ储寮曞鍏冪礌 + */ + public Object remove(int index){ + //绱㈠紩涓嶅湪瀹為檯鑼冨洿鍐 + if(index < 0||index >= size){ + throw new ArrayIndexOutOfBoundsException(); + }else{ + for (int j = index; j < size-1; j++) { + elements[j]=elements[j+1]; + } + size--; + return elements[index]; + } + } + + /** + * 鑾峰彇澶у皬 + * @return + */ + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + int cursor; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if(i >= size){ + throw new NoSuchElementException(); + } + if (i >= elements.length){ + throw new ConcurrentModificationException(); + } + cursor = i+1; + return elements[i]; + } + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(0); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(6, 6); + list.remove(3); + for (int i = 0; i < list.size(); i++) { + System.out.println(i+":"+list.get(i)); + } + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } +} diff --git a/group17/102228177/src/data2_19/BinaryTreeNode.java b/group17/102228177/src/data2_19/BinaryTreeNode.java new file mode 100644 index 0000000000..adb23e8467 --- /dev/null +++ b/group17/102228177/src/data2_19/BinaryTreeNode.java @@ -0,0 +1,74 @@ +package data2_19; + +public class BinaryTreeNode implements Comparable{ + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + this.data = o; + } + + 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; + } + + @Override + public int compareTo(BinaryTreeNode o) { + return (this.data.hashCode() < o.data.hashCode()) ? -1 : + ((this.data.hashCode() == o.data.hashCode()) ? 0 : 1); + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + insertNode(this,node); + return node; + } + + private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { + //鐖惰妭鐐瑰ぇ浜庢坊鍔犲厓绱 + if(parentNode.compareTo(node) == 1){ + if(parentNode.left == null){ + parentNode.left = node; + return; + } + insertNode(parentNode.left, node); + } + //鐖惰妭鐐瑰皬浜庢坊鍔犲厓绱 + else + if(parentNode.compareTo(node) == -1){ + if(parentNode.right == null){ + parentNode.right = node; + return; + } + insertNode(parentNode.right, node); + }else{ + throw new RuntimeException("No duplicate vertex allowed!"); + } + } + + public static void main(String[] args) { + BinaryTreeNode tree = new BinaryTreeNode(5); + tree.insert(2); + tree.insert(23); + tree.insert(7); + tree.insert(1); + } + +} \ No newline at end of file diff --git a/group17/102228177/src/data2_19/Iterator.java b/group17/102228177/src/data2_19/Iterator.java new file mode 100644 index 0000000000..cbb3f605c2 --- /dev/null +++ b/group17/102228177/src/data2_19/Iterator.java @@ -0,0 +1,8 @@ +package data2_19; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group17/102228177/src/data2_19/LinkedList.java b/group17/102228177/src/data2_19/LinkedList.java new file mode 100644 index 0000000000..9c53c7d99d --- /dev/null +++ b/group17/102228177/src/data2_19/LinkedList.java @@ -0,0 +1,142 @@ +package data2_19; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + size = 0; + head = null; + } + + public void add(Object o){ + Node node = new Node(o); + if(head == null){ + head = node; + }else{ + //p涓烘父鏍 浠庡ご閬嶅巻鍒板熬 + Node p = head; + while(p.next != null){ + p = p.next; + } + p.next = node; + } + size++; + } + + public void add(int index , Object o){ + //鍒ゆ柇涓嶄负绌洪摼琛 + if(head != null){ + Node p = head; + int k = 0; + //鎵弿鍗曢摼琛ㄦ煡鎵剧index-1涓妭鐐 + while(k < index-1 && p.next != null){ + k++; + p = p.next; + } + //鍒ゆ柇鏄惁鎵惧埌绗琲ndex-1涓妭鐐 + if(p != null){ + Node node = new Node(o); + node.next = p.next; + p.next = node; + } + size++; + } + } + + public Object get(int index){ + if(index <0 || index >= size){ + throw new IndexOutOfBoundsException(); + }else{ + Node p = head; + int k = 0; + while(k < index && p.next != null){ + k++; + p = p.next; + } + return p.data; + } + } + public Object remove(int index){ + if(index <0 || index >= size){ + throw new IndexOutOfBoundsException(); + }else{ + if(head != null){ + Node p = head; + int k = 0; + while(k > index-1 && p.next != null){ + k++; + p = p.next; + } + Node next = p.next; + p.next = next.next; + size--; + return next.data; + } + } + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + + public void addLast(Object o){ + Node node = new Node(o); + if(head == null){ + head = node; + }else{ + Node p = head; + while(p.next != null){ + p = p.next; + } + p.next = node; + } + size++; + } + + public Object removeFirst(){ + if(head == null){ + throw new NoSuchElementException(); + } + Node node = head; + head = node.next; + size--; + return node.data; + } + public Object removeLast(){ + if(head == null){ + throw new NoSuchElementException(); + }else{ + Node p = head; + int k = 0; + while(k < size-1 && p.next != null){ + k++; + p = p.next; + } + Node last = p.next; + p.next = null; + size--; + return last.data; + } + } + + private static class Node{ + Object data; + Node next; + private Node(Object o){ + this.data = o; + this.next = null; + } + } +} \ No newline at end of file diff --git a/group17/102228177/src/data2_19/List.java b/group17/102228177/src/data2_19/List.java new file mode 100644 index 0000000000..8a03fb9c8c --- /dev/null +++ b/group17/102228177/src/data2_19/List.java @@ -0,0 +1,10 @@ +package data2_19; + +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/group17/102228177/src/data2_19/Queue.java b/group17/102228177/src/data2_19/Queue.java new file mode 100644 index 0000000000..95bd77729d --- /dev/null +++ b/group17/102228177/src/data2_19/Queue.java @@ -0,0 +1,38 @@ +package data2_19; +public class Queue { + + private LinkedList linkedList = new LinkedList(); + private int elementCount; + + public Queue() { + this.elementCount = 0; + } + + public void enQueue(Object o){ + linkedList.addLast(o); + elementCount++; + } + + public Object deQueue(){ + Object object = linkedList.removeFirst(); + elementCount--; + return object; + } + + public boolean isEmpty(){ + return elementCount == 0; + } + + public int size(){ + return elementCount; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + queue.enQueue(2); + queue.enQueue(3); + System.out.println(queue.isEmpty()); + System.out.println(queue.size()); + System.out.println(queue.deQueue()); + } +} \ No newline at end of file diff --git a/group17/102228177/src/data2_19/Stack.java b/group17/102228177/src/data2_19/Stack.java new file mode 100644 index 0000000000..7c26967ef4 --- /dev/null +++ b/group17/102228177/src/data2_19/Stack.java @@ -0,0 +1,44 @@ +package data2_19; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData; + private int elementCount; + + public Stack() { + this.elementData = new ArrayList(); + this.elementCount = 0; + } + public void push(Object o){ + elementData.add(o); + elementCount++; + } + + public Object pop(){ + Object object = elementData.remove(elementCount-1); + elementCount--; + return object; + } + + public Object peek(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(elementCount-1); + } + public boolean isEmpty(){ + return elementCount==0; + } + public int size(){ + return elementCount; + } + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + System.out.println(stack.pop()); + System.out.println(stack.peek()); + + } +} \ No newline at end of file diff --git a/group17/116665530/homework/src/com/coding/basic/MyArrayList.java b/group17/116665530/homework/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..e9af793f47 --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class MyArrayList implements MyList { + private int size = 0; + private Object[] elementData = new Object[100]; + + public void add(Object o){ + elementData[size++] = o; + } + public void add(int index, Object o){ + for(int i = size; i > index; i--) + { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object obj = elementData[index]; + for(int i = index; i < size(); i++) + { + elementData[i] = elementData[i + 1]; + } + size--; + return elementData; + } + + public int size(){ + return size; + } + + public MyIterator myIterator(){ + return null; + } + +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyBinaryTreeNode.java b/group17/116665530/homework/src/com/coding/basic/MyBinaryTreeNode.java new file mode 100644 index 0000000000..c79328b1ed --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyBinaryTreeNode.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class MyBinaryTreeNode { + private Object data; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public MyBinaryTreeNode getLeft() { + return left; + } + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + public MyBinaryTreeNode getRight() { + return right; + } + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Object o){ + return null; + } +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyIterator.java b/group17/116665530/homework/src/com/coding/basic/MyIterator.java new file mode 100644 index 0000000000..9247878483 --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyIterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface MyIterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyLinkedList.java b/group17/116665530/homework/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..fb75a27c6b --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class MyLinkedList implements MyList{ + 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 MyIterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyList.java b/group17/116665530/homework/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..3cc9d10473 --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyList.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyQueue.java b/group17/116665530/homework/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c5e79cdf4e --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyQueue.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class MyQueue { + private Object[] elementData; + private int elementCount; + private int head; + private int next; + public void enQueue(Object o){ + elementData[next] = o; + elementCount++; + next++; + } + + public Object deQueue(){ + Object obj = elementData[head]; + elementData[head] = null; + elementCount--; + head++; + return obj; + } + + public boolean isEmpty(){ + if(elementData.length==0){ + return true; + } + return false; + } + + public int size(){ + return elementData.length; + } +} diff --git a/group17/116665530/homework/src/com/coding/basic/MyStack.java b/group17/116665530/homework/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..194a8259d3 --- /dev/null +++ b/group17/116665530/homework/src/com/coding/basic/MyStack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class MyStack { + Object[] elementData; + private int size; + + public void push(Object o){ + elementData[size++]=o; + } + + public Object pop(){ + if(size>0) + { + elementData[--size]=null; + } + return null; + } + + public Object peek(){ + if(elementData.length == 0){ + return null; + } + return elementData[size - 1]; + } + public boolean isEmpty(){ + if(elementData.length == 0){ + return true; + } + return false; + } + public int size(){ + return elementData.length; + } +} diff --git a/group17/1204187480/code/homework/basic/pom.xml b/group17/1204187480/code/homework/basic/pom.xml index 039af72d42..17b2dea49e 100644 --- a/group17/1204187480/code/homework/basic/pom.xml +++ b/group17/1204187480/code/homework/basic/pom.xml @@ -1,12 +1,12 @@ - - 4.0.0 - basic - - com.coding - parent - 1.0-SNAPSHOT - ../parent/pom.xml - - + + 4.0.0 + basic + + com.coding + parent + 1.0-SNAPSHOT + ../parent/pom.xml + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/ArrayList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/ArrayList.java index 6ddf8cef58..d09d63c2fa 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/ArrayList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/ArrayList.java @@ -1,108 +1,108 @@ -package com.coding.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - private Iterator iterator = new ArrayListIterator(); - - private int length() { - return elementData.length; - } - - private static final int ENLARGE_LENGTH = 100; - - private Object[] enlarge(Object[] origin) { - return Arrays.copyOf(origin, origin.length + ENLARGE_LENGTH); - } - - private void enLargeElementData() { - if (size == length()) { - elementData = enlarge(elementData); - } - } - - public void add(Object o) { - enLargeElementData(); - elementData[size] = o; - size++; - } - - public void add(int index, Object o) { - checkForAdd(index); - enLargeElementData(); - // 澶囦唤 index 澶勫強鍚庨潰鐨勬暟鎹 - Object[] elementsBehindIndex = backBehindElements(elementData, index); - // 缁檌ndex澶 璁惧 - elementData[index] = o; - // 杩藉姞 澶囦唤鐨勬暟鎹 - appendElement(elementData, index, elementsBehindIndex); - size++; - } - - private void appendElement(Object[] origin, int pos, Object[] append) { - System.arraycopy(append, 0, origin, pos, append.length); - } - - private Object[] backBehindElements(Object[] elementData, int index) { - int backSize = size - index; - Object[] back = new Object[backSize]; - System.arraycopy(elementData, index, back, 0, backSize); - return back; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - private void checkIndex(int index) { - if (index < 0 || index >= size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - private void checkForAdd(int index) { - if (index < 0 || index > size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - public Object remove(int index) { - checkIndex(index); - Object[] back = backBehindElements(elementData, index + 1); - System.arraycopy(back, 0, elementData, index, back.length); - Object ret = elementData[index]; - elementData[index] = null; - size--; - return ret; - } - - public int size() { - return size; - } - - public Iterator iterator() { - return iterator; - } - - private class ArrayListIterator implements Iterator { - - int next = 0; - - @Override - public boolean hasNext() { - return next < size; - } - - @Override - public Object next() { - return elementData[next++]; - } - } - -} +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private Iterator iterator = new ArrayListIterator(); + + private int length() { + return elementData.length; + } + + private static final int ENLARGE_LENGTH = 100; + + private Object[] enlarge(Object[] origin) { + return Arrays.copyOf(origin, origin.length + ENLARGE_LENGTH); + } + + private void enLargeElementData() { + if (size == length()) { + elementData = enlarge(elementData); + } + } + + public void add(Object o) { + enLargeElementData(); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkForAdd(index); + enLargeElementData(); + // 澶囦唤 index 澶勫強鍚庨潰鐨勬暟鎹 + Object[] elementsBehindIndex = backBehindElements(elementData, index); + // 缁檌ndex澶 璁惧 + elementData[index] = o; + // 杩藉姞 澶囦唤鐨勬暟鎹 + appendElement(elementData, index, elementsBehindIndex); + size++; + } + + private void appendElement(Object[] origin, int pos, Object[] append) { + System.arraycopy(append, 0, origin, pos, append.length); + } + + private Object[] backBehindElements(Object[] elementData, int index) { + int backSize = size - index; + Object[] back = new Object[backSize]; + System.arraycopy(elementData, index, back, 0, backSize); + return back; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private void checkForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + public Object remove(int index) { + checkIndex(index); + Object[] back = backBehindElements(elementData, index + 1); + System.arraycopy(back, 0, elementData, index, back.length); + Object ret = elementData[index]; + elementData[index] = null; + size--; + return ret; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return iterator; + } + + private class ArrayListIterator implements Iterator { + + int next = 0; + + @Override + public boolean hasNext() { + return next < size; + } + + @Override + public Object next() { + return elementData[next++]; + } + } + +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/BinaryTreeNode.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/BinaryTreeNode.java index d7ac820192..266eff3d56 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Iterator.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Iterator.java index 06ef6311b2..dbe8b9afb2 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Iterator.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java index 706dafe820..7174cb9cdf 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -1,125 +1,147 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private int size = 0; - - public void add(Object o) { - Node newNode = new Node(o, null); - if (head == null) { - head = newNode; - } else { - node(size - 1).next = newNode; - } - size++; - } - - public void add(int index, Object o) { - checkForAdd(index); - if (index == size) { - add(o); - }else { - Node newNode = new Node(o, null); - if (index == 0){ - addFirst(o); - } else { - Node preNode = node(index - 1); - Node now = preNode.next; - preNode.next = newNode; - newNode.next = now; - size++; - } - } - - } - - private Node node(int index) { - Node x = head; - for (int i = 0; i < index; i++) { - x = x.next; - } - return x; - } - - public Object get(int index) { - checkIndex(index); - return node(index).data; - } - - /** - * 璁╄鍒犻櫎鐨勫紩鐢ㄧ殑鎸佹湁鑰呮寚鍚戜笅涓涓妭鐐 - * @param index - * @return - */ - public Object remove(int index) { - final Object ret; - checkIndex(index); - if (index == 0) { - Node removeNode = head; - ret = head.data; - head = removeNode.next; - } else { - Node pre = node(index - 1); - Node removeNode = pre.next; - ret = removeNode.data; - pre.next = removeNode.next; - } - size--; - return ret; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - head = new Node(o, head);; - size++; - } - - public void addLast(Object o) { - add(o); - } - - public Object removeFirst() { - if (size == 0){ - return null; - }else { - return remove(0); - } - } - - public Object removeLast() { - return remove(size - 1); - } - - public Iterator iterator() { - return null; - } - - private void checkIndex(int index) { - if (index < 0 || index >= size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - private void checkForAdd(int index) { - if (index < 0 || index > size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - private static class Node { - Object data; - Node next; - - public Node() { - } - - public Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + private Iterator iterator = new LinkedListIterator(); + + + public void add(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + node(size - 1).next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkForAdd(index); + if (index == size) { + add(o); + }else { + Node newNode = new Node(o, null); + if (index == 0){ + addFirst(o); + } else { + Node preNode = node(index - 1); + Node now = preNode.next; + preNode.next = newNode; + newNode.next = now; + size++; + } + } + + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + public Object get(int index) { + checkIndex(index); + return node(index).data; + } + + /** + * 璁╄鍒犻櫎鐨勫紩鐢ㄧ殑鎸佹湁鑰呮寚鍚戜笅涓涓妭鐐 + * @param index + * @return + */ + public Object remove(int index) { + final Object ret; + checkIndex(index); + if (index == 0) { + Node removeNode = head; + ret = head.data; + head = removeNode.next; + } else { + Node pre = node(index - 1); + Node removeNode = pre.next; + ret = removeNode.data; + pre.next = removeNode.next; + } + size--; + return ret; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + head = new Node(o, head);; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + if (size == 0){ + return null; + }else { + return remove(0); + } + } + + public Object removeLast() { + return remove(size - 1); + } + + public Iterator iterator() { + return iterator; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private void checkForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private static class Node { + Object data; + Node next; + + public Node() { + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator{ + + private Node next; + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public Object next() { + if (next == null) { + throw new IndexOutOfBoundsException("there is no node in list"); + } + Node ret = next; + next = next.next; + return ret.data; + } + } +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Queue.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Queue.java index 36e516e266..10560f969e 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Queue.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Queue.java @@ -1,19 +1,24 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} +package com.coding.basic; + +import java.util.Arrays; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(0); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java index a5a04de76d..998ddf9768 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java @@ -1,22 +1,30 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new IllegalStateException("the stack is empty"); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (isEmpty()) { + throw new IllegalStateException("the stack is empty"); + } + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java index 85d3cf6901..95f0085b66 100644 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java @@ -1,25 +1,25 @@ -package com.coding.api; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Created by luoziyihao on 2/25/17. - */ -public class ArrayListTest { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Test - public void testAdd(){ - List list = new ArrayList(Arrays.asList(0, 1, 2, 3)); - logger.info("list={}", list); - list.add(5, 2); - logger.info("list={}", list); - } -} +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArrayListTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Test + public void testAdd(){ + List list = new ArrayList(Arrays.asList(0, 1, 2, 3)); + logger.info("list={}", list); + list.add(5, 2); + logger.info("list={}", list); + } +} diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArraysTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArraysTest.java index eb41a7e262..6c96193d82 100644 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArraysTest.java +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArraysTest.java @@ -1,22 +1,22 @@ -package com.coding.api; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Arrays; - -/** - * Created by luoziyihao on 2/25/17. - */ -public class ArraysTest { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - - @Test - public void testCopyOf(){ - Object[] a = new Object[]{1, 2, 3, 4}; - Object[] b = Arrays.copyOf(a, 10); - logger.info("a={}, b={}", Arrays.toString(a), Arrays.toString(b)); - } -} +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArraysTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + @Test + public void testCopyOf(){ + Object[] a = new Object[]{1, 2, 3, 4}; + Object[] b = Arrays.copyOf(a, 10); + logger.info("a={}, b={}", Arrays.toString(a), Arrays.toString(b)); + } +} diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/SystemTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/SystemTest.java index efc4022378..6a23cb125e 100644 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/SystemTest.java +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/SystemTest.java @@ -1,24 +1,24 @@ -package com.coding.api; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Arrays; - -/** - * Created by luoziyihao on 2/25/17. - */ -public class SystemTest { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Test - public void testArrayCopy() { - int[] a = new int[]{1, 2, 3, 4, 5, 6, 7}; - int[] b = new int[]{11, 22, 33, 44, 55, 66, 77}; - System.arraycopy(a, 2, b, 4, 3); - logger.info("b={}", Arrays.toString(b)); - - } -} +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class SystemTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Test + public void testArrayCopy() { + int[] a = new int[]{1, 2, 3, 4, 5, 6, 7}; + int[] b = new int[]{11, 22, 33, 44, 55, 66, 77}; + System.arraycopy(a, 2, b, 4, 3); + logger.info("b={}", Arrays.toString(b)); + + } +} diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/ArrayListTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/ArrayListTest.java index 9241fe72da..656ff54c06 100644 --- a/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/ArrayListTest.java +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/ArrayListTest.java @@ -1,35 +1,35 @@ -package com.coding.basic; - -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * Created by luoziyihao on 2/25/17. - */ -public class ArrayListTest { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - - private List list = new ArrayList(); - - @Before - public void before() { - - } - - @Test - public void add() throws Exception { - list.add(1); - } - - @Test - public void get() throws Exception { - add(); - logger.info("{}", list.get(0)); - } - +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArrayListTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + private List list = new ArrayList(); + + @Before + public void before() { + + } + + @Test + public void add() throws Exception { + list.add(1); + } + + @Test + public void get() throws Exception { + add(); + logger.info("{}", list.get(0)); + } + } \ No newline at end of file diff --git a/group17/1204187480/code/homework/parent/pom.xml b/group17/1204187480/code/homework/parent/pom.xml index 9d29a4d93e..7af48c6fed 100644 --- a/group17/1204187480/code/homework/parent/pom.xml +++ b/group17/1204187480/code/homework/parent/pom.xml @@ -1,95 +1,95 @@ - - 4.0.0 - - com.coding - parent - pom - 1.0-SNAPSHOT - https://github.com/luoziyihao/coding2017 - - - UTF-8 - UTF-8 - UTF-8 - 1.8 - 1.8 - 1.8 - 3.0 - 1.1.7 - 1.2 - 1.2.17 - 4.12 - - - - - - ch.qos.logback - logback-classic - ${logback-classic.version} - - - - commons-logging - commons-logging - ${commons-logging.version} - - - log4j - log4j - ${log4j.version} - - - - - junit - junit - ${junit.version} - - - - - - alimaven - aliyun maven - http://maven.aliyun.com/nexus/content/groups/public/ - - true - - - true - - - - - - alimaven - aliyun maven - http://maven.aliyun.com/nexus/content/groups/public/ - - true - - - true - - - - - - ${project.artifactId} - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - + + 4.0.0 + + com.coding + parent + pom + 1.0-SNAPSHOT + https://github.com/luoziyihao/coding2017 + + + UTF-8 + UTF-8 + UTF-8 + 1.8 + 1.8 + 1.8 + 3.0 + 1.1.7 + 1.2 + 1.2.17 + 4.12 + + + + + + ch.qos.logback + logback-classic + ${logback-classic.version} + + + + commons-logging + commons-logging + ${commons-logging.version} + + + log4j + log4j + ${log4j.version} + + + + + junit + junit + ${junit.version} + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + true + + + true + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + true + + + true + + + + + + ${project.artifactId} + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/pom.xml b/group17/1204187480/code/homework/pom.xml index e939b51bd0..df0626e528 100644 --- a/group17/1204187480/code/homework/pom.xml +++ b/group17/1204187480/code/homework/pom.xml @@ -1,14 +1,14 @@ - - - 4.0.0 - com.coding - coding2017 - 1.0-SNAPSHOT - pom - - parent - basic - - - + + + 4.0.0 + com.coding + coding2017 + 1.0-SNAPSHOT + pom + + parent + basic + + + diff --git a/group17/1264835468/.gitignore b/group17/1264835468/.gitignore new file mode 100644 index 0000000000..71f96b185c --- /dev/null +++ b/group17/1264835468/.gitignore @@ -0,0 +1,5 @@ +/bin/ + +.classpath +.project +.gitignore \ No newline at end of file diff --git a/group17/1264835468/src/assignment/BinaryTree.java b/group17/1264835468/src/assignment/BinaryTree.java new file mode 100644 index 0000000000..1d76a7ce2c --- /dev/null +++ b/group17/1264835468/src/assignment/BinaryTree.java @@ -0,0 +1,154 @@ +package assignment; + +// +public class BinaryTree> implements Iterable> { + private BinaryTreeNode root; + + public BinaryTree(T data) { + root = new BinaryTreeNode(data); + } + + public BinaryTree(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode insert(T data) { + BinaryTreeNode node = new BinaryTreeNode(data); + if (root == null) + root = node; + else + insert(root, node); + return node; + } + + public BinaryTreeNode insert(BinaryTreeNode node) { + return insert(node.getData()); + } + + private void insert(BinaryTreeNode current, BinaryTreeNode node) { + + if (current.getData().compareTo(node.getData()) > 0) { + if (current.getLeft() == null) + current.setLeft(node); + else + insert(current.getLeft(), node); + } + else { + if (current.getRight() == null) + current.setRight(node); + else + insert(current.getRight(), node); + } + } + + @Override + public String toString() { + return new BFSNodeQueue().toString(); + } + + /** + * 骞垮害浼樺厛閬嶅巻鑺傜偣闃熷垪 + * + * @author Administrator + * + */ + private class BFSNodeQueue { + private MyQueue> nodeQueue; + + public BFSNodeQueue() { + nodeQueue = new MyQueue<>(); + } + + public boolean isEmpty() { + return nodeQueue.isEmpty(); + } + + public void enQueue(BinaryTreeNode node) { + if (node != null) nodeQueue.enQueue(node); + } + + // 鍑洪槦鍚屾椂鎶婂瓙鑺傜偣鍏ラ槦 + public BinaryTreeNode deQueue() { + if (!isEmpty()) { + BinaryTreeNode first = nodeQueue.deQueue(); + enQueue(first.getLeft()); + enQueue(first.getRight()); + return first; + } + throw new QueueIsEmptyException(); + } + + // 鎶婃墍鏈夊嚭闃熻妭鐐规斁杩涘彟涓涓槦鍒椾腑 + public MyQueue> getResult() { + prepare(); + MyQueue> result = new MyQueue<>(); + while (!isEmpty()) { + result.enQueue(deQueue()); + } + return result; + } + + private void prepare() { + clearQueue(); + enQueue(root); + } + + public void clearQueue() { + while (!isEmpty()) { + deQueue(); + } + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + + Iterator> iterator = iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next() + "\n"); + } + return stringBuilder.toString(); + } + } + + @Override + public Iterator> iterator() { + return new BFSIterator(); + } + + private class BFSIterator implements Iterator> { + MyArrayList> list; + Iterator> iterator; + + public BFSIterator() { + MyQueue> BFSQueue = new BFSNodeQueue().getResult(); + list = new MyArrayList<>(); + while (!BFSQueue.isEmpty()) { + list.add(BFSQueue.deQueue()); + } + iterator = list.iterator(); + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public BinaryTreeNode next() { + return iterator.next(); + } + } + + public static void main(String[] args) { + BinaryTree binaryTree = new BinaryTree<>(5); + binaryTree.insert(6); + binaryTree.insert(7); + binaryTree.insert(4); + Iterator> iterator = binaryTree.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + System.out.println(binaryTree); + } +} diff --git a/group17/1264835468/src/assignment/BinaryTreeNode.java b/group17/1264835468/src/assignment/BinaryTreeNode.java new file mode 100644 index 0000000000..02a162ae10 --- /dev/null +++ b/group17/1264835468/src/assignment/BinaryTreeNode.java @@ -0,0 +1,58 @@ +package assignment; + +public class BinaryTreeNode> { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data) { + this.data = data; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("node:" + data); + // 闈炲彾鑺傜偣鍒欏姞涓婂乏鍙冲瓙鑺傜偣data + if (left != null || right != null) { + if (left != null) + stringBuilder.append(",left:" + left.data); + else + stringBuilder.append(",left:null"); + if (right != null) + stringBuilder.append(",right:" + right.data); + else + stringBuilder.append(",right:null"); + } + return stringBuilder.toString(); + } + + public static void main(String[] args) { + // BinaryTreeNode binaryTreeNode = new BinaryTreeNode<>(1); + } + +} diff --git a/group17/1264835468/src/assignment/Iterable.java b/group17/1264835468/src/assignment/Iterable.java new file mode 100644 index 0000000000..403c5e9866 --- /dev/null +++ b/group17/1264835468/src/assignment/Iterable.java @@ -0,0 +1,6 @@ +package assignment; + +// +public interface Iterable { + Iterator iterator(); +} diff --git a/group17/1264835468/src/assignment/Iterator.java b/group17/1264835468/src/assignment/Iterator.java new file mode 100644 index 0000000000..feb4e2066a --- /dev/null +++ b/group17/1264835468/src/assignment/Iterator.java @@ -0,0 +1,7 @@ +package assignment; + +public interface Iterator { + public boolean hasNext(); + + public E next(); +} diff --git a/group17/1264835468/src/assignment/List.java b/group17/1264835468/src/assignment/List.java new file mode 100644 index 0000000000..ba31dc333b --- /dev/null +++ b/group17/1264835468/src/assignment/List.java @@ -0,0 +1,14 @@ +package assignment; + +// +public interface List { + public void add(E o); + + public void add(int index, E o); + + public E get(int index); + + public E remove(int index); + + public int size(); +} diff --git a/group17/1264835468/src/assignment/MyArrayList.java b/group17/1264835468/src/assignment/MyArrayList.java new file mode 100644 index 0000000000..ea9ae87755 --- /dev/null +++ b/group17/1264835468/src/assignment/MyArrayList.java @@ -0,0 +1,110 @@ +package assignment; + +import java.util.Arrays; + +public class MyArrayList implements List, Iterable { + private Object[] elementData; + private static final int DEFAULT_SIZE = 10; + private int size; + + public MyArrayList() { + this(DEFAULT_SIZE); + } + + public MyArrayList(int initSize) { + if (initSize < 0) { + throw new IllegalArgumentException(initSize + " < 0"); + } + if (initSize == 0) { + elementData = new Object[DEFAULT_SIZE]; + } + else { + elementData = new Object[initSize]; + } + size = 0; + } + + public void add(E o) { + growIfNeed(); + elementData[size++] = o; + } + + public void add(int index, E o) { + if (index < 0 || index > size) { + throw new IllegalArgumentException("index:" + index); + } + growIfNeed(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @SuppressWarnings("unchecked") + public E get(int index) { + rangeCheck(index); + return (E) elementData[index]; + } + + public E remove(int index) { + rangeCheck(index); + E target = get(index); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return target; + } + + public int size() { + return size; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new NoSuchElementException("index:" + index); + } + } + + private void growIfNeed() { + if (size == elementData.length) + grow(); + } + + private void grow() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + @Override + public Iterator iterator() { + return new ArrayIterator<>(); + } + + private class ArrayIterator implements Iterator { + private int currentPos = 0; + + @Override + public boolean hasNext() { + return currentPos < size; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + rangeCheck(currentPos); + return (E) elementData[currentPos++]; + } + + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData, size)); + } + +} + +class NoSuchElementException extends RuntimeException { + + public NoSuchElementException(String string) { + super(string); + } + +} diff --git a/group17/1264835468/src/assignment/MyLinkedList.java b/group17/1264835468/src/assignment/MyLinkedList.java new file mode 100644 index 0000000000..b96a7e1d5b --- /dev/null +++ b/group17/1264835468/src/assignment/MyLinkedList.java @@ -0,0 +1,144 @@ +package assignment; + +public class MyLinkedList implements List, Iterable { + private Node head; + private int size; + + public MyLinkedList() { + size = 0; + } + + public void add(E o) { + if (head == null) + addFirst(o); + else + addLast(o); + } + + public void addFirst(E o) { + Node oldFirst = head; + head = new Node<>(o, oldFirst); + size++; + } + + public void addLast(E o) { + if (head == null) { + addFirst(o); + } + else { + Node oldLast = movePtrTo(size - 1); + oldLast.next = new Node<>(o, null); + size++; + } + + } + + public void add(int index, E o) { + if (index > size || index < 0) { + throw new IllegalArgumentException("index:" + index); + } + if (index == 0) { + addFirst(o); + return; + } + Node temp = movePtrTo(index - 1); + Node oldNext = temp.next; + Node newNext = new Node<>(o, oldNext); + temp.next = newNext; + size++; + } + + public E remove(int index) { + rangeCheck(index); + E data; + if (index == 0) { + data = head.data; + head = head.next; + } + else { + Node pre = movePtrTo(index - 1); + Node target = pre.next; + pre.next = target.next; + data = target.data; + } + size--; + return data; + } + + public E get(int index) { + rangeCheck(index); + return movePtrTo(index).data; + } + + public int size() { + return size; + } + + private Node movePtrTo(int index) { + Node resultNode = head; + for (int i = 0; i < index; i++) { + resultNode = resultNode.next; + } + return resultNode; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new NoSuchElementException("index:" + index); + } + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder('['); + Node temp = head; + while (temp != null) { + stringBuilder.append(String.valueOf(temp.toString()) + ","); + temp = temp.next; + } + stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length()); + stringBuilder.append(']'); + return stringBuilder.toString(); + } + + private static class Node { + private T data; + private Node next; + + public Node(T data, Node next) { + this.data = data; + this.next = next; + } + + @Override + public String toString() { + return data.toString(); + } + } + + @Override + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + Node currentNode; + + public ListIterator() { + currentNode = head; + } + + @Override + public boolean hasNext() { + return currentNode.next != null; + } + + @Override + public E next() { + Node temp = currentNode; + currentNode = currentNode.next; + return temp.data; + } + + } +} \ No newline at end of file diff --git a/group17/1264835468/src/assignment/MyQueue.java b/group17/1264835468/src/assignment/MyQueue.java new file mode 100644 index 0000000000..c5e3e8ce88 --- /dev/null +++ b/group17/1264835468/src/assignment/MyQueue.java @@ -0,0 +1,34 @@ +package assignment; + +public class MyQueue { + private MyLinkedList elementData = new MyLinkedList<>(); + + public void enQueue(T o) { + elementData.addLast(o); + } + + public T deQueue() { + if (!isEmpty()) { + return elementData.remove(0); + } + throw new QueueIsEmptyException(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} + +class QueueIsEmptyException extends RuntimeException { + public QueueIsEmptyException() { + super(); + } + + public QueueIsEmptyException(String string) { + super(string); + } +} diff --git a/group17/1264835468/src/assignment/MyStack.java b/group17/1264835468/src/assignment/MyStack.java new file mode 100644 index 0000000000..0c3d30337b --- /dev/null +++ b/group17/1264835468/src/assignment/MyStack.java @@ -0,0 +1,39 @@ +package assignment; + +public class MyStack { + private MyArrayList elementData = new MyArrayList<>(); + + public void push(T o) { + elementData.add(o); + } + + public T pop() { + if (!isEmpty()) { + T data = elementData.remove(elementData.size() - 1); + return data; + } + throw new StackIsEmptyException(); + } + + public T peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} + +class StackIsEmptyException extends RuntimeException { + public StackIsEmptyException() { + super(); + } + + public StackIsEmptyException(String string) { + super(string); + } +} diff --git a/group17/1282579502/.classpath b/group17/1282579502/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group17/1282579502/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group17/1282579502/.gitignore b/group17/1282579502/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/1282579502/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/1282579502/.project b/group17/1282579502/.project new file mode 100644 index 0000000000..8c3f97afde --- /dev/null +++ b/group17/1282579502/.project @@ -0,0 +1,37 @@ +<<<<<<< HEAD:group17/876385982/.project + + + 876385982 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + +======= + + + 1282579502Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + +>>>>>>> 907d4deeb1542a0f373ee6874add67184cd8332d:group17/1282579502/.project diff --git a/group17/1282579502/src/com/coding/basic/ArrayList.java b/group17/1282579502/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c07b58281c --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[8]; + + public int test = 10; + + public void add(Object o){ + if(size + 1 >elementData.length){ + expand(); + } + elementData[size] = o; + size++; + } + /** + * Parameters: + * index index at which the specified element is to be inserted + * element element to be inserted + * Throws: + * IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) + */ + + public void add(int index, Object o){ + if(index <0 || index > size()) throw new IndexOutOfBoundsException(index + ": Invalid Index"); + if(size()+1>elementData.length){ + expand(); + } + if(index < size()) + System.arraycopy(elementData, index, elementData, index+1, size() - index); + elementData[index] = o; + size ++; + } + + public Object get(int index){ + if(index <0 || index >= size()) throw new ArrayIndexOutOfBoundsException(index + ": Invalid Index."); + return elementData[index]; + } + + public Object remove(int index) { + if(index <0 || index >= size()) throw new ArrayIndexOutOfBoundsException(index + ": Invalid Index."); + Object item = elementData[index]; + if(size() == 1){ + size--; + }else{ + if(index o, return 1 + * this.data < o, return -1 + * this.data == o, return 0; + */ + public BinaryTreeNode insert(Object o){ + if(!( o instanceof Comparable)) throw new IllegalArgumentException(o + " is NOT comparable. "); + if(data == null) { + data = o; + return this; + } + + Comparable cdata = (Comparable) data; + if(cdata.compareTo(o)>0){ + if(left == null) { + left = new BinaryTreeNode(o); + return left; + } + else{ + return left.insert(o); + } + } + else if(cdata.compareTo(o)<0){ + if(right == null){ + right = new BinaryTreeNode(o); + return right; + }else{ + return right.insert(o); + } + } + else{ + throw new IllegalArgumentException(o + " encountered a duplication."); + } + } + +} diff --git a/group17/1282579502/src/com/coding/basic/Iterator.java b/group17/1282579502/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coding/basic/LinkedList.java b/group17/1282579502/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..55db7e97bc --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/LinkedList.java @@ -0,0 +1,148 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = null; + private Node tail = null; + private int size = 0; + + public void add(Object o){ + if(head == null){ + head = new Node(o); + tail = head; + }else{ + tail.next = new Node(o); + tail = tail.next; + } + size ++; + } + /** + * Add to index from 0 to size + * Add to index at 0 is equivalent to addFirst + * Add to index at size is equivalent to add and addLast + * + */ + public void add(int index , Object o){ + if(index < 0 || index >size+1) throw new IndexOutOfBoundsException(index + ": Invalid Index"); + Node newNode = new Node(o); + //manage head node + if(index == 0){ + if(head == null){ + this.add(o); + } + else{ + newNode.next = head; + head = newNode; + size++; + } + } + else if (index == size){ + this.add(o); + } + else{ + Node prevNode = getNodeAt(index-1); + newNode.next = prevNode.next; + prevNode.next = newNode; + size ++; + } + } + public Object get(int index){ + Node c = getNodeAt(index); + if(c == null ) return null; + return c.data; + } + + private Node getNodeAt(int index){ + if(index < 0 || index >size-1) throw new IndexOutOfBoundsException(index + ": Invalid Index"); + Node c = head; + while(index-- > 0 ){ + if(c != null) c = c.next; + else return null; + } + return c; + } + + public Object remove(int index){ + if(index<0 || index >size-1) throw new IndexOutOfBoundsException(index + ": Invalid Index"); + Node ret = null; + if(index == 0){ + ret = head; + head = head.next; + size --; + }else if(index == size -1){ + Node nodeBeforeTail = getNodeAt(index -1); + ret = tail; + nodeBeforeTail.next = null; + size --; + }else{ + Node nodeBeforeTarget = getNodeAt(index -1); + Node target = nodeBeforeTarget.next; + ret = target; + nodeBeforeTarget.next = target.next; + size --; + } + + return ret.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + this.add(0, o); + } + + public void addLast(Object o){ + this.add(o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private class Node{ + public Object data = null; + public Node next = null; + public Node(Object o){ + data = o; + } + } + + private static class LinkedListIterator implements Iterator{ + + Object[] oArray = null; + int cursor = 0; + public LinkedListIterator(LinkedList ll){ + if(ll == null) throw new NullPointerException("Linkedlist Object is NULL"); + oArray = new Object[ll.size()]; + for(int i = 0; i< ll.size(); i++){ + oArray[i] = ll.get(i); + } + } + + @Override + public boolean hasNext() { + if(cursor < oArray.length){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o = oArray[cursor]; + cursor ++; + return o; + } + + } +} diff --git a/group17/1282579502/src/com/coding/basic/List.java b/group17/1282579502/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coding/basic/Queue.java b/group17/1282579502/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b9cbd3c500 --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private LinkedList ll = null; + + public Queue(){ + ll = new LinkedList(); + } + + public void enQueue(Object o){ + ll.add(o); + } + + public Object deQueue() throws IndexOutOfBoundsException{ + try{ + return ll.remove(0); + }catch(IndexOutOfBoundsException ie){ + throw new IndexOutOfBoundsException(ie.getMessage()); + } + + } + + public boolean isEmpty(){ + return (ll.size() == 0) ? true : false; + } + + public int size(){ + return ll.size(); + } +} diff --git a/group17/1282579502/src/com/coding/basic/Stack.java b/group17/1282579502/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7bfcd75ec3 --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + private ArrayList al = null; + + public Stack(){ + al = new ArrayList(); + } + + public void push(Object o){ + al.add(o); + } + + public Object pop(){ + return al.remove(al.size()-1); + } + + public Object peek(){ + return (al.size() == 0) ? null : al.get(al.size() -1); + } + + public boolean isEmpty(){ + return (al.size() == 0) ? true : false; + } + + public int size(){ + return al.size(); + } +} diff --git a/group17/1282579502/src/com/coding/basic/ut/ArrayListTest.java b/group17/1282579502/src/com/coding/basic/ut/ArrayListTest.java new file mode 100644 index 0000000000..9bce65892b --- /dev/null +++ b/group17/1282579502/src/com/coding/basic/ut/ArrayListTest.java @@ -0,0 +1,121 @@ +package com.coding.basic.ut; + +import static org.junit.Assert.*; + +import java.lang.reflect.Field; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayListTest { + + ArrayList target; + Object[] elementDataField; + @Before + public void setUp() throws Exception { + target = new ArrayList(); + /* + * NOTE: getField only returns PUBLIC fields, getDeclaredField get all fields + Field[] allFields = target.getClass().getFields(); + System.out.println("Get all fields"); + for(Field tmpf : allFields){ + System.out.println(tmpf.getName()); + } + Field[] allDeclFields = target.getClass().getDeclaredFields(); + System.out.println("Get all declared fields"); + for(Field tmpf : allDeclFields){ + System.out.println(tmpf.getName()); + } + * + */ + try { + Field f = target.getClass().getDeclaredField("elementData"); + + f.setAccessible(true); + if(f.getType().isArray()){ + elementDataField = (Object[]) f.get(target); + } + } catch (NoSuchFieldException e) { + fail(e.getMessage()); + } catch (SecurityException e) { + fail(e.getMessage()); + } catch (IllegalArgumentException e) { + fail(e.getMessage()); + } catch (IllegalAccessException e) { + fail(e.getMessage()); + } + } + + @Test + public void testAddObject() { + Object item = new String("s1"); + target.add(item); + assertEquals(item, elementDataField[0]); + } + + @Test + public void testAddIndexObject() { + //fail("Not yet implemented"); + Object item0 = new String("s0"); + target.add(0, item0); + + Object item1 = new String("s1"); + target.add(0, item1); + + assertEquals(item1, elementDataField[0]); + assertEquals(item0, elementDataField[1]); + } + + @Test + public void testGet() { + target.add("0"); + Object o = target.get(0); + assertEquals("0", o); + assertNotEquals("2", 0); + } + + @Test + public void testRemove() { + String[] items = new String[]{"0","1","2"}; + for(int i = 0; i=0; i--){ + assertEquals(ll.get(i), items[items.length - 1 -i]); + } + + } + + @Test + public void testAddLast() { + LinkedList ll = new LinkedList(); + String[] items = new String[]{"0","1","2","3","4"}; + for(int i = 0 ; i< items.length; i++){ + ll.addLast(items[i] ); + } + //expect 0, 1, 2, 3, 4 + for(int i = 0; i= elementData.length) { + resize(2 * size); + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + if (size >= elementData.length) { + resize(2 * size); + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + return elementData[index]; + } + + public Object remove(int index) { + Object o = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private void resize(int n) { + Object[] temp = elementData; + elementData = new Object[n]; + for (int i = 0; i < temp.length; i++) { + elementData[i] = temp[i]; + } + } + +} diff --git a/group17/1540186032/First/src/BinaryTreeNode.java b/group17/1540186032/First/src/BinaryTreeNode.java new file mode 100644 index 0000000000..d9cf24d809 --- /dev/null +++ b/group17/1540186032/First/src/BinaryTreeNode.java @@ -0,0 +1,31 @@ + +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/group17/1540186032/First/src/Iterator.java b/group17/1540186032/First/src/Iterator.java new file mode 100644 index 0000000000..b7d572d9d7 --- /dev/null +++ b/group17/1540186032/First/src/Iterator.java @@ -0,0 +1,6 @@ + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/1540186032/First/src/LinkedList.java b/group17/1540186032/First/src/LinkedList.java new file mode 100644 index 0000000000..18c7978c6d --- /dev/null +++ b/group17/1540186032/First/src/LinkedList.java @@ -0,0 +1,131 @@ +public class LinkedList implements List { + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add(0); + + System.out.println(linkedList.get(0)); + + } + + private Node first; + + private Node last; + + private int size = 0; + + public void add(Object o) { + Node oldLast = last; + last = new Node(); + last.data = o; + last.next = null; + if (oldLast != null) { + oldLast.next = last; + } + if (first == null) { + first = last; + } + size++; + } + + public void add(int index, Object o) { + if (index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + if (index == 0) { + addFirst(o); + return; + } + if (index == size) { + addLast(o); + return; + } + Node curserNode = cursor(index - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = curserNode.next; + curserNode.next = newNode; + size++; + } + + public Object get(int index) { + return cursor(index).data; + } + + public Object remove(int index) { + Node node = cursor(index - 1).next; + cursor(index - 1).next = node.next; + node.next = null; + return node.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(); + newNode.data = o; + if (first == null) { + first = newNode; + } else { + newNode.next = first; + first = newNode; + } + if (last == null) { + last = first; + } + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(); + newNode.data = o; + if (last == null) { + last = newNode; + } else { + last.next = newNode; + last = newNode; + } + if (first == null) { + first = last; + } + size++; + } + + public Object removeFirst() { + Node node = first; + Node newFirst = first.next; + first.next = null; + first = newFirst; + size--; + return node.data; + } + + public Object removeLast() { + Node node = last; + Node newLast = cursor(size - 1); + newLast.next = null; + last = newLast; + size--; + return node.data; + } + + public Iterator iterator() { + return null; + } + + private Node cursor(int index) { + Node curserNode = first; + for (int i = 0; i < index; i++) { + curserNode = curserNode.next; + } + return curserNode; + } + + private static class Node { + Object data; + Node next; + + } +} diff --git a/group17/1540186032/First/src/List.java b/group17/1540186032/First/src/List.java new file mode 100644 index 0000000000..7290dd72cf --- /dev/null +++ b/group17/1540186032/First/src/List.java @@ -0,0 +1,8 @@ + +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/group17/1540186032/First/src/Queue.java b/group17/1540186032/First/src/Queue.java new file mode 100644 index 0000000000..4f6bcd41cb --- /dev/null +++ b/group17/1540186032/First/src/Queue.java @@ -0,0 +1,23 @@ + +public class Queue { + LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + if (elementData.size() == 0) { + + } + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group17/1540186032/First/src/Stack.java b/group17/1540186032/First/src/Stack.java new file mode 100644 index 0000000000..c6609c8db8 --- /dev/null +++ b/group17/1540186032/First/src/Stack.java @@ -0,0 +1,28 @@ +import java.util.EmptyStackException; + +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 EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group17/176653813/1766538130226Lesson/.classpath b/group17/176653813/1766538130226Lesson/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group17/176653813/1766538130226Lesson/.gitignore b/group17/176653813/1766538130226Lesson/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/176653813/1766538130226Lesson/.project b/group17/176653813/1766538130226Lesson/.project new file mode 100644 index 0000000000..faf83f8e14 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/.project @@ -0,0 +1,17 @@ + + + 1766538130226Lesson + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/176653813/1766538130226Lesson/.settings/org.eclipse.jdt.core.prefs b/group17/176653813/1766538130226Lesson/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/.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/group17/176653813/1766538130226Lesson/src/com/coding/basic/ArrayList.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..484b1e5340 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/src/com/coding/basic/ArrayList.java @@ -0,0 +1,80 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private Object[] obj = new Object[5]; + + @Override + public void add(Object o) { + if(this.size < 0 ) + System.out.print("Error"); + this.extend(100); + obj[this.size] = o; + this.size ++; + } + + @Override + public void add(int index, Object o) { + + if(index < 0) + System.out.println("Error"); + if(index < this.size){ + extend(100); + for(int i = this.size;i < index; i--){ + obj[i+1] = obj[i]; + } + obj[index] = o; + }else if(index >= size){ + extend(100); + obj[size] = o; + } + this.size++; + } + + @Override + public Object get(int index) { + if(index < 0 || index > size){ + System.out.println("Error"); + return null; + } + return obj[index]; + } + + @Override + public Object remove(int index) { + if(index < 0 || index > size){ + System.out.println("Error"); + return null; + } + for(int i = index;i <= size;i++){ + obj[i] = obj[i+1]; + } + size--; + return obj[index]; + } + + @Override + public int size() { + return size; + } + public int length(){ + return obj.length; + } + public void extend(int newLength){ + if (this.size >= obj.length){ + //扩展数组 + Object[] old = obj; + obj = new Object[size+newLength]; + for(int i = 0;i < size; i++){ + obj[i] = old[i]; + } + } + return; + } + public void Iteror(){ + for(int i = 0 ;i < size ; i++){ + System.out.println(obj[i]); + } + } +} diff --git a/group17/176653813/1766538130226Lesson/src/com/coding/basic/LinkList.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/LinkList.java new file mode 100644 index 0000000000..6b7ebe0f81 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/src/com/coding/basic/LinkList.java @@ -0,0 +1,96 @@ +package com.coding.basic; + +public class LinkList implements List{ + + private Node head; //头结点不计算个数 + private static class Node{ + Object data; + Node next; + } + + @Override + public void add(Object o) { + //第一步没有想到 + if(null == head){ + head = new Node(); + head.next = null; + head.data = o; + }else{ + //尾插入法 + //Node t = new Node(); + Node t; + Node ins = new Node(); + t = head; + while(t.next != null){ + t = t.next; + } + t.next = ins; + ins.next = null; + ins.data = o; + } + } + + @Override + public void add(int index, Object o) { + if(index < 0 ){ + System.out.println("Error"); + }else if(index == 0 || index == 1){ + Node t = new Node(); + t.next = head.next; + head.next = t; + t.data = o; + }else{ + Node t = new Node();//当前节点 + Node p = new Node();//前一个节点 + t = head.next; + for(int i = 1;i < index;i++){ + p = t; + t = t.next; + } + Node ins = new Node(); + p.next = ins; + ins.next = t; + ins.data = o; + } + + } + + @Override + public Object get(int index) { + if(index < 0 || head == null){ + System.out.println("Error"); + return null; + }else{ + Node t ; + t = head; + for(int i = 1;i < index;i++){ + t = t.next; + } + return t.data; + } + } + + @Override + public Object remove(int index) { + + return null; + } + + public void display(){ + if(head == null){ + System.out.println("No Data"); + }else{ + Node t ; + t = head; + while(t != null){ + System.out.println("******"+t.data); + t = t.next; + } + } + + } + @Override + public int size() { + return 0; + } +} diff --git a/group17/176653813/1766538130226Lesson/src/com/coding/basic/List.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/List.java new file mode 100644 index 0000000000..2c3e428bf8 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/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/group17/176653813/1766538130226Lesson/src/com/coding/basic/Queue.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b1f9c85511 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Queue { + + private LinkList elementData = new LinkList(); + private int front = 0; + private int rear = 0; + + + public void enQueue(Object o){ + elementData.add(o); + rear++; + } + public Object deQueue(){ + if(!isEmpty()){ + Object obj = elementData.remove(front); + front++; + return obj; + }else{ + System.out.println("Queue is empty"); + return null; + } + } + public boolean isEmpty(){ + if(front > rear){ + return true; + } + return false; + } + + public int size(){ + return rear-front+1; + } +} + diff --git a/group17/176653813/1766538130226Lesson/src/com/coding/basic/Stack.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..27e4dc7db9 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class Stack { + + // + private ArrayList elementData = new ArrayList(); + private int top = 0; + + public void push(Object o){ + elementData.add(o); + top++; + } + + public Object pop(){ + if(!isEmpty()){ + System.out.println("stack is empoty"); + return null; + } + Object obj = elementData.remove(top); + top--; + return obj; + } + + public Object peek(){ + return elementData.get(top); + } + + public boolean isEmpty(){ + if(top != 0){ + return true; + } + return false; + } + + public int size(){ + return top++; + } + + + +} diff --git a/group17/176653813/1766538130226Lesson/src/com/coding/basic/Test.java b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Test.java new file mode 100644 index 0000000000..50b9b144b7 --- /dev/null +++ b/group17/176653813/1766538130226Lesson/src/com/coding/basic/Test.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +public class Test { + + @org.junit.Test + public void test() { + +/* ArrayList al = new ArrayList(); + al.add(1); + al.add(2); + al.add(3); + al.add(4); + al.add(5); + al.add(200); + al.add(10,100); + al.Iteror(); + //System.out.println(al.length()); + //System.out.println(al.size()); + System.out.println("=================="); + al.remove(0); + al.Iteror();*/ + + LinkList ls = new LinkList(); + ls.add(100); + ls.add(300); + ls.add(500); + ls.add(1000); + ls.add(3,2000); + ls.display(); + System.out.println(ls.get(4)); + } + +} + diff --git a/group17/176653813/RemoteSystemsTempFiles/.project b/group17/176653813/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..7675629320 --- /dev/null +++ b/group17/176653813/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group17/240094626/.gitignore b/group17/240094626/.gitignore new file mode 100644 index 0000000000..cf8e1e36d1 --- /dev/null +++ b/group17/240094626/.gitignore @@ -0,0 +1,45 @@ +<<<<<<< HEAD:group17/.gitignore +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders + +.classpath +.project +. +======= +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders + +*.settings +*.project +*.classpath +*/.settings +/**/target/**/* +>>>>>>> 306b41b325671fb7420952ad0788ea6be114d674:group17/240094626/.gitignore diff --git a/group17/240094626/warm-up/.gitignore b/group17/240094626/warm-up/.gitignore new file mode 100644 index 0000000000..c7b6e44e1d --- /dev/null +++ b/group17/240094626/warm-up/.gitignore @@ -0,0 +1,4 @@ +/bin/ +*.classpath +*.project +/.settings/ diff --git a/group17/240094626/warm-up/src/com/coding/basic/Iterator.java b/group17/240094626/warm-up/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group17/240094626/warm-up/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/group17/240094626/warm-up/src/com/coding/basic/List.java b/group17/240094626/warm-up/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group17/240094626/warm-up/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/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java b/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java new file mode 100644 index 0000000000..6486868054 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java @@ -0,0 +1,169 @@ +package com.coding.basic.impl; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +/** + * + * @鎻忚堪: ArrayList绠鍗曞疄鐜 + * @浣滆:240094626 + * @鍒涘缓鏃ユ湡:2017-2-20 + */ +public class ArrayList implements List { + + /** + * @comment:鍏冪礌鏁扮粍 + */ + private Object data[] = null; + + /** + * @comment:鏁扮粍鍏冪礌涓暟 + */ + private int size = 0; + + /** + * 鏃犲弬鏋勯犲嚱鏁帮紝鍒濆鍖栧閲忎负10鐨勭┖鍒楄〃 + */ + public ArrayList(){ + this(10); + } + + /** + * @param length + * 鏋勯犲嚱鏁帮紝鍒濆鍖栧閲忎负length鐨勭┖鍒楄〃 + */ + public ArrayList(int length){ + if(length < 0){ + throw new IllegalArgumentException("鍒濆瀹归噺鍙傛暟闈炴硶锛"+length); + } + data = new Object[length]; + } + + + /** + * @createTime: 2017-2-21 涓嬪崍1:32:28 + * @param length + * @return:void + * @comment锛氬垪琛ㄧ粨鏋勬墿灞曞閲忥紝姣忔澧炲姞鍘熸潵鐨1/2瀹归噺 + */ + private void grow(int length){ + int oldLength = data.length; + if(length > oldLength){ + Object oldData[] = data; + int newLength = oldLength*3/2 + 1; + if(newLength < length){ + newLength = length; + } + data = new Object[newLength]; + System.arraycopy(oldData, 0, data, 0, oldLength); + } + } + + /** + * @createTime: 2017-2-21 涓嬪崍1:32:05 + * @param index + * @return:void + * @comment锛氭楠屼笅鏍囧弬鏁版槸鍚﹁秴闄 + */ + private void check(int index) { + if( index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); + } + } + + @Override + public void add(Object o) { + grow(size+1); + data[size++]=o; + } + + @Override + public void add(int index, Object o) { + if( index > size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); + } + grow(size+1); + System.arraycopy(data, index, data, index+1, size-index); + data[index] = o; + size++; + + } + + @Override + public Object get(int index) { + check(index); + return data[index]; + } + + + + @Override + public Object remove(int index) { + check(index); + Object remove = data[index]; + System.arraycopy(data, index+1, data, index, size-index); + data[--size] = null; + return remove; + } + + @Override + public int size() { + return size; + } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for(int i =0; i 0){ + sb.append(","); + } + sb.append(data[i]); + } + return String.format("ArrayList {data=[%s], size=%d}", sb.toString(),size); + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + /** + * @鎻忚堪: 绠鍗曞疄鐜拌凯浠e櫒 + * @浣滆:240094626 + * @鍒涘缓鏃ユ湡:2017-2-21 + */ + private class ArrayListIterator implements Iterator{ + + /** + * @column:index + * @comment:褰撳墠浣嶇疆涓嬫爣 + */ + private int index; + + /** + * 鏃犲弬鏋勯狅紝鍒濆鍖栬凯浠e櫒鐨勪笅鏍囦负0 + */ + public ArrayListIterator(){ + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o = get(index++); + return o; + } + + } + +} + + diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java b/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java new file mode 100644 index 0000000000..41ec919b27 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java @@ -0,0 +1,110 @@ +package com.coding.basic.impl; + + +/** + * 浜屽弶鏍戠畝鍗曞疄鐜(key涓篿nt绫诲瀷) + * @author 240094626 + * + */ +public class BinaryTree { + /**鏍圭粨鐐癸紝鍒濆鍖栦负绌*/ + private Node rootNode = null; + + + /** + * 鏍规嵁key鍊兼彃鍏ユ暟鎹甦ata涓虹┖鐨勬柊鑺傜偣 + * @param key + * @return + */ + public Node insert(int key){ + return insert(key,null); + } + + /** + * 鏍规嵁key鍊兼彃鍏ユ暟鎹甦ata涓簅鐨勬柊鑺傜偣 + * @param key + * @param o + * @return + */ + public Node insert(int key ,Object o){ + Node newNode = new Node(key, o); + if(rootNode == null){ + rootNode = newNode; + return rootNode; + } + Node fatherNode = rootNode; + Node currentNode = rootNode; + while(currentNode != null){ + fatherNode = currentNode; + if(key < currentNode.key){ + currentNode = currentNode.left; + }else{ + currentNode = currentNode.right; + } + } + if(key < fatherNode.key){ + fatherNode.left = newNode; + }else{ + fatherNode.right = newNode; + } + size++; + return newNode; + } + + /** + * 鏍规嵁key鍊兼煡鎵剧粨鐐 + * @param key + * @return + */ + public Node getNode(int key){ + return get(rootNode, key); + } + + /** + * 閫掑綊绠楁硶锛 鏍规嵁寮濮嬬粨鐐逛綅缃拰key鍊兼煡鎵捐妭鐐 + * @param n + * @param key + * @return + */ + private Node get(Node n,int key){ + if(n == null){ + return null; + } + if(key < n.key){ + return get(n.left, key); + }else if(key > n.key){ + return get(n.left, key); + } + return n; + } + + + + + + + private static class Node{ + + int key; + Object data; + Node left; + Node right; + + public Node(int key, Object data) { + this.key = key; + this.data = data; + this.left = null; + this.right = null; + } + + @Override + public String toString() { + return "Node [key=" + key + ", data=" + data + "]"; + } + + + } + + + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java b/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java new file mode 100644 index 0000000000..d6284f9bec --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java @@ -0,0 +1,205 @@ +package com.coding.basic.impl; + +import java.util.NoSuchElementException; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +/** + * 鍙屽悜閾捐〃绠鍗曞疄鐜 + * @author 240094626 + */ +public class LinkedList implements List { + /**澶磋妭鐐癸紙绌虹殑锛*/ + private Node header = new Node(null, null, null); + /**閾捐〃鑺傜偣闀垮害*/ + private int size = 0; + + + /** + * 鏃犲弬鏋勯犲嚱鏁帮紝鍒濆鍖杊eader鑺傜偣锛屽墠鍚庡潎鎸囧悜header鑺傜偣锛屽舰鎴愮幆褰㈤摼琛 + * 鐜舰閾捐〃锛氫负浜嗕娇閾捐〃鑺傜偣鐨勫紑澶存槸header锛岀粨灏句篃鏄痟eader锛 + * 鐢变簬瀹炵幇浜哃ist锛岄偅涔堥摼琛ㄥ氨鏄湁搴忕殑锛屾牴鎹笅鏍囨煡璇㈡椂鍙熷姪鐜舰鐗圭偣鍙屽悜鏌ユ壘锛屾彁鍗囨晥鐜囷紱 + */ + public LinkedList() { + header.next = header.pre = header; + } + + + /** + * 灏哋bject o 娣诲姞鍒 鑺傜偣n涔嬪墠 + * @param o + * @param n + */ + private void addBefore(Object o, Node n) { + Node newNode = new Node(o, n.pre, n); + newNode.next.pre = newNode; + newNode.pre.next = newNode; + size++; + } + + /** + * 鏍规嵁涓嬫爣浣嶇疆鏌ユ壘缁撶偣 + * @param index + * @return + */ + private Node getNode(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("index:"+index); + } + // 鏌ユ壘浠巋eader寮濮 + Node n = header; + if(index <= (size >> 1)){ + // 寰next鏂瑰悜鎵剧index涓妭鐐 + for(int i=0; i <=index; i++){ + n = n.next; + } + }else{ + // 寰pre鏂瑰悜鎵剧size-index涓妭鐐 + for(int i=size-index; i >0; i--){ + n = n.pre; + } + } + return n; + } + + + /** + * 绉婚櫎鑺傜偣锛屼粠褰撳墠鑺傜偣鐨勫墠鍚庤妭鐐归棿鍒犻櫎褰撳墠鑺傜偣 + * @param n + * @return + */ + private Object remove(Node n){ + if(n == header){ + throw new NoSuchElementException("鏈壘鍒拌妭鐐"); + } + Object result = n.data; + n.pre.next = n.next; + n.next.pre = n.pre; + n.data = null; + size--; + return result; + } + + @Override + public void add(Object o) { + // 榛樿寰header鍓嶆坊鍔 + addBefore(o,header); + } + + + @Override + public void add(int index, Object o) { + addBefore(o,index==size?header:getNode(index)); + } + + @Override + public Object get(int index) { + Node n = getNode(index); + return n.data; + } + + + @Override + public Object remove(int index) { + return remove(getNode(index)); + } + + + @Override + public int size() { + return size; + } + /** + * 鐜舰閾捐〃缁撴瀯锛宧eader.next灏辨槸绗竴涓妭鐐 + * @param o + */ + public void addFirst(Object o){ + addBefore(o, header.next); + } + /** + * 鐜舰閾捐〃缁撴瀯锛宧eader.pre灏辨槸鏈鍚庝竴涓妭鐐 + * @param o + */ + public void addLast(Object o){ + addBefore(o, header); + } + public Object removeFirst(){ + return remove(header.next); + } + public Object removeLast(){ + return remove(header.pre); + } + + public Iterator iterator(){ + + return new LinkedListIterator(); + } + + + @Override + public String toString() { + Iterator it = iterator(); + StringBuilder sb = new StringBuilder(); + while(it.hasNext()){ + if(sb.length() > 0){ + sb.append(","); + } + sb.append(it.next()); + } + return "LinkedList {nodes=[" + sb + "], size=" + size + "}"; + } + + + private static class Node{ + Object data; + Node pre; + Node next; + + /** + * 閾捐〃鑺傜偣锛屽甫鍙傛瀯閫犲嚱鏁 + * @param data 鑺傜偣鍐呭 + * @param pre 涓婁竴涓妭鐐 + * @param next 涓嬩竴涓妭鐐 + */ + public Node(Object data, Node pre, Node next) { + super(); + this.data = data; + this.pre = pre; + this.next = next; + } + + @Override + public String toString() { + return "Node {data=" + data + "}"; + } + + + + } + + private class LinkedListIterator implements Iterator{ + int index ; + + public LinkedListIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public Object next() { + Node n = getNode(index++); + return n.data; + } + + } + + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java b/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java new file mode 100644 index 0000000000..42fae9217c --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java @@ -0,0 +1,73 @@ +package com.coding.basic.impl; + +import com.coding.basic.Iterator; + +/** + * 闃熷垪绠鍗曞疄鐜 + * @author 240094626 + * + */ +public class Queue { + /**闃熷垪鍏冪礌瀹瑰櫒瀵硅薄*/ + LinkedList elementData = new LinkedList(); + + /** + * 鍏ラ槦鍒 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + + /** + * 鍑洪槦鍒楋細鍏堣繘鍏堝嚭锛屾晠鍙栧嚭閾捐〃棣栦釜鑺傜偣 + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + if(elementData.size() > 0 ){ + return false; + } + return true; + } + + public int size(){ + return elementData.size(); + } + + + @Override + public String toString() { + return "Queue {elementData=" + elementData + "}"; + } + + public Iterator iterator(){ + return new QueueIterator(); + } + + private class QueueIterator implements Iterator{ + + int index; + + public QueueIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < elementData.size()){ + return true; + } + return false; + } + + @Override + public Object next() { + return elementData.get(index++); + } + + } +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java b/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java new file mode 100644 index 0000000000..786071f0a1 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java @@ -0,0 +1,86 @@ +package com.coding.basic.impl; + +import com.coding.basic.Iterator; + +/** + * 鏍堢殑绠鍗曞疄鐜 + * @author 240094626 + */ +public class Stack { + /**闀垮害鍙彉鐨勫厓绱犲鍣*/ + private ArrayList elementData = new ArrayList(); + + /** + * 鍘嬪叆鏍 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 鍑烘爤锛堟湯灏惧厓绱狅級锛屽苟绉婚櫎 + * @return Object + */ + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + /** + * 鍙栧嚭鏍堬紙鏈熬鍏冪礌锛夛紝涓嶇Щ闄 + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + /** + * 鍒ゆ柇鏍堟槸鍚︿负绌 + * @return boolean + */ + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + /** + * 鏍堢殑闀垮害锛屾棦鏄鍣ˋrrayList鐨勯暱搴 + * @return int + */ + public int size(){ + return elementData.size(); + } + + + + @Override + public String toString() { + return "Stack {elementData=" + elementData + "}"; + } + + public Iterator iterator(){ + return new StackIterator(); + } + + private class StackIterator implements Iterator{ + int index; + + public StackIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < elementData.size()){ + return true; + } + return false; + } + + @Override + public Object next() { + return elementData.get(index++); + } + + + } +} \ No newline at end of file diff --git a/group17/240094626/warm-up/src/com/coding/basic/test/ArrayListTest.java b/group17/240094626/warm-up/src/com/coding/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..03419f7d0f --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/test/ArrayListTest.java @@ -0,0 +1,55 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +import com.coding.basic.impl.ArrayList; + +/** + * ArrayList 绠鍗曟祴璇 + * @author 240094626 + * + */ +public class ArrayListTest { + + + @Test + public void test() { + ArrayList list = new ArrayList(); + + System.out.println("******娴嬭瘯add(Object o ):娣诲姞绗竴涓厓绱0******"); + list.add(0); + System.out.println("ArrayList print:"+list.toString()); + + System.out.println("******娴嬭瘯add(int index,Object o):娣诲姞绗簩涓厓绱1******"); + list.add(1, 1); + System.out.println("ArrayList print:"+list.toString()); + + System.out.println("******娴嬭瘯remove(int index):鍒犻櫎绗1涓厓绱:0******"); + list.remove(0); + System.out.println("ArrayList print:"+list.toString()); + + System.out.println("******娴嬭瘯add(int Object o):娣诲姞绗笁涓厓绱2******"); + list.add(2); + System.out.println("ArrayList print:"+list.toString()); + + + System.out.println("ArrayList size:"+list.size()); + + System.out.println("******娴嬭瘯get(int index):鍒ゆ柇绗1涓厓绱犳槸鍚︿负1******"); + assertEquals(1, list.get(0)); + } + + public static void main(String[] args) { + Result result = JUnitCore.runClasses(ArrayListTest.class); + for(Failure failure : result.getFailures()){ + System.out.println(failure.toString()); + } + System.out.println("test success!:"+result.wasSuccessful()); + } + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/test/LinkedListTest.java b/group17/240094626/warm-up/src/com/coding/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..25c8ed80c8 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/test/LinkedListTest.java @@ -0,0 +1,60 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +import com.coding.basic.impl.LinkedList; + +/** + * LinkedList 绠鍗曟祴璇 + * @author 240094626 + * + */ +public class LinkedListTest { + + @Test + public void test() { + LinkedList list = new LinkedList(); + + System.out.println("******娴嬭瘯add(Object o ):娣诲姞绗竴涓厓绱0******"); + list.add(0); + System.out.println("LinkedList print:"+list.toString()); + System.out.println("******娴嬭瘯add(int index,Object o):娣诲姞绗簩涓厓绱1******"); + list.add(1); + System.out.println("******娴嬭瘯addLast(Object o):寰閾捐〃鏈鍚庢坊鍔犲厓绱3******"); + list.addLast(3); + System.out.println("******娴嬭瘯addFirst(Object o):寰閾捐〃鏈鍓嶉潰娣诲姞鍏冪礌5******"); + list.addFirst(5); + System.out.println("LinkedList print:"+list.toString()); + + System.out.println("******娴嬭瘯remove(int index):鍒犻櫎绗4涓厓绱:index=3******"); + list.remove(3); + System.out.println("LinkedList print:"+list.toString()); + + System.out.println("******娴嬭瘯addFirst(int Object o):閾捐〃鏈鍓嶉潰娣诲姞绱2******"); + list.addFirst(2); + System.out.println("LinkedList print:"+list.toString()); + + // 鏂█绗竴涓厓绱犱负0 + assertEquals(2, list.get(0)); + + list.addLast(3); + list.addFirst(5); + System.out.println("LinkedList print:"+list.toString()); + // 鏂█鏈鍚庝竴涓厓绱犱负3 + assertEquals(3,list.get(list.size()-1)); + } + + public static void main(String[] args) { + Result result = JUnitCore.runClasses(LinkedListTest.class); + for(Failure failure : result.getFailures()){ + System.out.println(failure.toString()); + } + System.out.println("test success!:"+result.wasSuccessful()); + } + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/test/QueueTest.java b/group17/240094626/warm-up/src/com/coding/basic/test/QueueTest.java new file mode 100644 index 0000000000..6d653983bd --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/test/QueueTest.java @@ -0,0 +1,57 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +import com.coding.basic.impl.Queue; + +/** + * Queue 绠鍗曟祴璇 + * @author 240094626 + * + */ +public class QueueTest { + + @Test + public void test() { + Queue queue = new Queue(); + + System.out.println("******娴嬭瘯enQueue(Object o):鍏ラ槦鍒楀厓绱燼,b,c******"); + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + System.out.println("queue:"+queue.toString()); + + // 鏂█闃熷垪涓嶄负绌 + assertEquals(false,queue.isEmpty()); + + // 鏂█鍑洪槦鍒楁槸a + System.out.println("******娴嬭瘯deQueue(Object o):鍑洪槦鍒楀厓绱燼******"); + assertEquals("a",queue.deQueue()); + System.out.println("queue:"+queue.toString()); + + // 鏂█鍑洪槦鍒楁槸b + System.out.println("******娴嬭瘯deQueue(Object o):鍑洪槦鍒楀厓绱燽******"); + assertEquals("b",queue.deQueue()); + System.out.println("queue:"+queue.toString()); + + // 鏂█鍑洪槦鍒楁槸c + assertEquals("c",queue.deQueue()); + + + + + } + + public static void main(String[] args) { + Result result = JUnitCore.runClasses(QueueTest.class); + for(Failure failure : result.getFailures()){ + System.out.println(failure.toString()); + } + System.out.println("test success!:"+result.wasSuccessful()); + } +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/test/StackTest.java b/group17/240094626/warm-up/src/com/coding/basic/test/StackTest.java new file mode 100644 index 0000000000..9b6c07b8f4 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/test/StackTest.java @@ -0,0 +1,71 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +import com.coding.basic.Iterator; +import com.coding.basic.impl.Stack; + +/** + * stack 绠鍗曟祴璇 + * @author 240094626 + * + */ +public class StackTest { + + @Test + public void test() { + Stack stack = new Stack(); + System.out.println("******娴嬭瘯push(Object o):鍘嬪叆绗竴涓厓绱0******"); + stack.push(0); + System.out.println("Stack print:"+stack.toString()); + + System.out.println("******娴嬭瘯push(Object o):鍘嬪叆绗簩涓厓绱2******"); + stack.push(2); + System.out.println("Stack print:"+stack.toString()); + + System.out.println("******娴嬭瘯peek():浠庢爤灏惧彇鍑2锛屼笉鍒犻櫎******"); + stack.peek(); + System.out.println("Stack print:"+stack.toString()); + + System.out.println("******娴嬭瘯peek():鍐嶆浠庢爤灏惧彇鍑2锛屼笉鍒犻櫎******"); + // 鏂█鍑烘爤涓2 + assertEquals(2,stack.peek()); + // 鏂█size涓2 + assertEquals(2,stack.size()); + + System.out.println("******娴嬭瘯pop():鏈熬鍏冪礌2鍑烘爤,骞剁Щ闄******"); + // 鏂█鍑烘爤涓2 + assertEquals(2,stack.pop()); + System.out.println("Stack print:"+stack.toString()); + + // 鏂█涓嶄负绌 + assertEquals(false,stack.isEmpty()); + // 鏂█size涓1 + assertEquals(1,stack.size()); + // 娣诲姞3锛5 涓や釜鍏冪礌 + stack.push(3); + stack.push(5); + System.out.println("Stack print:"+stack.toString()); + // 娴嬭瘯杩唬鍣 + Iterator it = stack.iterator(); + int i = 1; + while(it.hasNext()){ + System.out.println("绗"+i+"涓厓绱狅細"+it.next()); + i++; + } + + } + public static void main(String[] args) { + Result result = JUnitCore.runClasses(StackTest.class); + for(Failure failure : result.getFailures()){ + System.out.println(failure.toString()); + } + System.out.println("test success!:"+result.wasSuccessful()); + } + +} diff --git a/group17/51075907/HomeWork/.classpath b/group17/51075907/HomeWork/.classpath new file mode 100644 index 0000000000..d70658c4e7 --- /dev/null +++ b/group17/51075907/HomeWork/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/51075907/HomeWork/.project b/group17/51075907/HomeWork/.project new file mode 100644 index 0000000000..f0440af423 --- /dev/null +++ b/group17/51075907/HomeWork/.project @@ -0,0 +1,17 @@ + + + HomeWork + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/51075907/HomeWork/.settings/org.eclipse.jdt.core.prefs b/group17/51075907/HomeWork/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..25bebf8de8 --- /dev/null +++ b/group17/51075907/HomeWork/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.4 +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=warning +org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning +org.eclipse.jdt.core.compiler.source=1.3 diff --git a/group17/51075907/HomeWork/src/day1_HomeWork/ArrayList.java b/group17/51075907/HomeWork/src/day1_HomeWork/ArrayList.java new file mode 100644 index 0000000000..1d071a0b25 --- /dev/null +++ b/group17/51075907/HomeWork/src/day1_HomeWork/ArrayList.java @@ -0,0 +1,75 @@ +package day1_HomeWork; + + + public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + //如果数组空间超过当前大小,保证容量 + public void ensureCapacity( int newCapacity) + { + if ( newCapacity < size) + return; + + Object [] old =elementData; + elementData =(Object []) new Object[newCapacity]; + for (int i=0; i index; i--) + elementData[i] =elementData [i-1]; + elementData[index]= o; + size ++; + + } + + public Object get(int index){ + if ( index<0 || index >=size()) + throw new ArrayIndexOutOfBoundsException(); + return elementData[index]; + } + + public Object remove(int index){ + Object remove_elementData=elementData[index]; + for ( int i=index;i size()) + throw new IndexOutOfBoundsException(); + + if (index < size()/2) + { + p=beginMarker.next; + for (int i =0;iindex;i--) + p=p.prev; + } + return p; + } + public Object remove(Node p){ + p.next.prev =p.prev; + p.prev.next=p.next; + int size; + size--; + int modCount; + modCount++; + return p.data; + } + + public int size(){ + return -1; + } + + public void addFirst(Node p,Object o){ + Node newNode= new Node (o,p.prev,p); + newNode.prev.next= newNode; + p.prev =newNode; + size++; + modCount++; + } + public void addLast(Object o){ + + } + public Object removeFirst(int index){ + return remove( get(index)); + } + public Object removeLast(){ + return null; + } + public java.util.Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements java.util.Iterator{ + private Node current=beginMarker.next; + private int expectedModCount=modCount; + private boolean okTORemove=false; + + public boolean haNext() + { + return current!= endMarker; + } + public Object next() + { + if (modCount !=expectedModCount) + throw new java.util.ConcurrentModificationException(); + if (!=hasNext()) + throw new java.util.NoSuchElementException(); + + + } + } + + + private static class Node{ + public Node(Object o, Node p, Node n) + { + data =o; next=n; prev=p; + } + public Object data; + public Node next; + public Node prev; + + } +} diff --git a/group17/51075907/HomeWork/src/day1_HomeWork/List.java b/group17/51075907/HomeWork/src/day1_HomeWork/List.java new file mode 100644 index 0000000000..037b33142b --- /dev/null +++ b/group17/51075907/HomeWork/src/day1_HomeWork/List.java @@ -0,0 +1,9 @@ +package day1_HomeWork; + +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/group17/51075907/HomeWork/src/day1_HomeWork/Queue.java b/group17/51075907/HomeWork/src/day1_HomeWork/Queue.java new file mode 100644 index 0000000000..5345e30297 --- /dev/null +++ b/group17/51075907/HomeWork/src/day1_HomeWork/Queue.java @@ -0,0 +1,19 @@ +package day1_HomeWork; + +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/group17/51075907/HomeWork/src/day1_HomeWork/Stack.java b/group17/51075907/HomeWork/src/day1_HomeWork/Stack.java new file mode 100644 index 0000000000..c3f4d66bc3 --- /dev/null +++ b/group17/51075907/HomeWork/src/day1_HomeWork/Stack.java @@ -0,0 +1,23 @@ +package day1_HomeWork; + + +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/group17/785396327/binarytree/BinaryTree.java b/group17/785396327/binarytree/BinaryTree.java index bf5a6b639c..d55c65f8e4 100644 --- a/group17/785396327/binarytree/BinaryTree.java +++ b/group17/785396327/binarytree/BinaryTree.java @@ -1,55 +1,55 @@ -package binarytree; - -/** - * Created by william on 2017/2/16. - */ -public class BinaryTree { - private Node root; - - class Node { - private Node left; - private Node right; - private Comparable data; - - public Node(Node left, Node right, Comparable data) { - this.left = left; - this.right = right; - this.data = data; - } - - private void add(Comparable data) { - if (this.data.compareTo(data) >= 0) - if (this.left == null) - this.left = new Node(null, null, data); - else - left.add(data); - else if (this.data.compareTo(data) < 0) - if (this.right == null) - this.right = new Node(null, null, data); - else - this.right.add(data); - } - - public Comparable getData() { - return this.data; - } - - public Node getLeft() { - return this.left; - } - - public Node getRight() { - return this.right; - } - } - - public void add(Comparable data) { - if (this.root == null) - root = new Node(null, null, data); - else this.root.add(data); - } - - public void printByType(SearchType type) { - type.printByType(this.root); - } -} +package binarytree; + +/** + * Created by william on 2017/2/16. + */ +public class BinaryTree { + private Node root; + + class Node { + private Node left; + private Node right; + private Comparable data; + + public Node(Node left, Node right, Comparable data) { + this.left = left; + this.right = right; + this.data = data; + } + + private void add(Comparable data) { + if (this.data.compareTo(data) >= 0) + if (this.left == null) + this.left = new Node(null, null, data); + else + left.add(data); + else if (this.data.compareTo(data) < 0) + if (this.right == null) + this.right = new Node(null, null, data); + else + this.right.add(data); + } + + public Comparable getData() { + return this.data; + } + + public Node getLeft() { + return this.left; + } + + public Node getRight() { + return this.right; + } + } + + public void add(Comparable data) { + if (this.root == null) + root = new Node(null, null, data); + else this.root.add(data); + } + + public void printByType(SearchType type) { + type.printByType(this.root); + } +} diff --git a/group17/785396327/binarytree/DLRSearchType.java b/group17/785396327/binarytree/DLRSearchType.java index 3d1adbbdc7..7f9ba1c38d 100644 --- a/group17/785396327/binarytree/DLRSearchType.java +++ b/group17/785396327/binarytree/DLRSearchType.java @@ -1,16 +1,16 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class DLRSearchType implements SearchType { - - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - System.out.print(root.getData()+" "); - printByType(root.getLeft()); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class DLRSearchType implements SearchType { + + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + System.out.print(root.getData()+" "); + printByType(root.getLeft()); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LDRSearchType.java b/group17/785396327/binarytree/LDRSearchType.java index da18dbc1b9..f67b6dcb81 100644 --- a/group17/785396327/binarytree/LDRSearchType.java +++ b/group17/785396327/binarytree/LDRSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LDRSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - System.out.print(root.getData() + " "); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LDRSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + System.out.print(root.getData() + " "); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LFSearchType.java b/group17/785396327/binarytree/LFSearchType.java index d983c50ff9..e88e76b210 100644 --- a/group17/785396327/binarytree/LFSearchType.java +++ b/group17/785396327/binarytree/LFSearchType.java @@ -1,27 +1,27 @@ -package binarytree; - -import java.util.LinkedList; - -/** - * Created by william on 2017/2/18. - */ -public class LFSearchType implements SearchType { - private LinkedList queue = new LinkedList<>(); - - @Override - public void printByType(BinaryTree.Node root) { - if (root == null) - return; - queue.offer(root); - while (!queue.isEmpty()) { - BinaryTree.Node curNode = queue.poll(); - System.out.print(curNode.getData() + " "); - if (curNode.getLeft() != null) - queue.offer(curNode.getLeft()); - if (curNode.getRight() != null) - queue.offer(curNode.getRight()); - } - - } - -} +package binarytree; + +import java.util.LinkedList; + +/** + * Created by william on 2017/2/18. + */ +public class LFSearchType implements SearchType { + private LinkedList queue = new LinkedList<>(); + + @Override + public void printByType(BinaryTree.Node root) { + if (root == null) + return; + queue.offer(root); + while (!queue.isEmpty()) { + BinaryTree.Node curNode = queue.poll(); + System.out.print(curNode.getData() + " "); + if (curNode.getLeft() != null) + queue.offer(curNode.getLeft()); + if (curNode.getRight() != null) + queue.offer(curNode.getRight()); + } + + } + +} diff --git a/group17/785396327/binarytree/LRDSearchType.java b/group17/785396327/binarytree/LRDSearchType.java index 250645e6df..dbd5f1e820 100644 --- a/group17/785396327/binarytree/LRDSearchType.java +++ b/group17/785396327/binarytree/LRDSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LRDSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - printByType(root.getRight()); - System.out.print(root.getData() + " "); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LRDSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + printByType(root.getRight()); + System.out.print(root.getData() + " "); + } + } +} diff --git a/group17/785396327/binarytree/SearchType.java b/group17/785396327/binarytree/SearchType.java index 606124a781..78f43d0c2d 100644 --- a/group17/785396327/binarytree/SearchType.java +++ b/group17/785396327/binarytree/SearchType.java @@ -1,9 +1,9 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public interface SearchType { - - void printByType(T root); -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public interface SearchType { + + void printByType(T root); +} diff --git a/group17/785396327/list/ArrayList.java b/group17/785396327/list/ArrayList.java index 9d79bd2013..f4e5f26f40 100644 --- a/group17/785396327/list/ArrayList.java +++ b/group17/785396327/list/ArrayList.java @@ -1,135 +1,135 @@ -package list; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 10; - private int size; - private Object[] elementData; - - public ArrayList() { - elementData = new Object[DEFAULT_CAPACITY]; - } - - public ArrayList(int initialCapacity) { - if (initialCapacity < 0) - throw new RuntimeException("闈炴硶鍒濆鍖栧ぇ灏忓弬鏁!"); - elementData = new Object[initialCapacity]; - } - - public boolean add(T ele) { - grow(); - elementData[size] = ele; - size++; - return true; - } - - public T get(int index) { - checkBounds(index); - return (T) elementData[index]; - } - - public T remove(int index) { - checkBounds(index); - T removeEle = (T) elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index); - size--; - return removeEle; - } - - public boolean add(int index, T ele) { - checkBounds(index); - size++;//鏈夋晥鍏冪礌鍐呭鍏堝姞锛屼繚璇侀暱搴︽瀬闄愭儏鍐礸row鍦ㄦ坊鍔犲墠鐢熸晥 - grow(); - //灏嗗師鏈暟缁勪粠寰呮彃鍏ョ殑index鎴彇锛屽皢鍘熸湰index鍚庣殑鏈夋晥鍊硷紝澶嶅埗鍒板師鏈暟缁刬ndex+1涔嬪悗 - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = ele; - return true; - } - - @Override - public boolean remove(T ele) { - int index; - if ((index = indexOf(ele)) == -1) - return false; - remove(index); - return true; - } - - private void checkBounds(int index) { - if (index < 0 || index >= size) - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - public int size() { - return size; - } - - private void grow() { - if (size >= elementData.length) { - int curLen = elementData.length; - int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); - elementData = Arrays.copyOf(elementData, newLen); - } - } - - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - return indexOf(ele) != -1; - } - - public int indexOf(T ele) { - for (int i = 0; i < size; i++) { - if (ele == null) - if (null == elementData[i]) - return i; - else if (ele.equals(elementData[i])) - return i; - } - return -1; - } - - public Iterator iterator() { - return new Itr(); - } - - private class Itr implements Iterator { - int cursor;//寰呴亶鍘嗗厓绱犵殑涓嬫爣 - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - public T next() { - if (cursor >= size) - throw new NoSuchElementException(); - return (T) elementData[cursor++]; - } - - @Override - public void remove() { - if (cursor >= size) - throw new NoSuchElementException(); - ArrayList.this.remove(cursor--); - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - for (Object ele : elementData) { - sb.append(ele).append(" "); - } - return sb.append("]").toString(); - } -} +package list; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) + throw new RuntimeException("闈炴硶鍒濆鍖栧ぇ灏忓弬鏁!"); + elementData = new Object[initialCapacity]; + } + + public boolean add(T ele) { + grow(); + elementData[size] = ele; + size++; + return true; + } + + public T get(int index) { + checkBounds(index); + return (T) elementData[index]; + } + + public T remove(int index) { + checkBounds(index); + T removeEle = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return removeEle; + } + + public boolean add(int index, T ele) { + checkBounds(index); + size++;//鏈夋晥鍏冪礌鍐呭鍏堝姞锛屼繚璇侀暱搴︽瀬闄愭儏鍐礸row鍦ㄦ坊鍔犲墠鐢熸晥 + grow(); + //灏嗗師鏈暟缁勪粠寰呮彃鍏ョ殑index鎴彇锛屽皢鍘熸湰index鍚庣殑鏈夋晥鍊硷紝澶嶅埗鍒板師鏈暟缁刬ndex+1涔嬪悗 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = ele; + return true; + } + + @Override + public boolean remove(T ele) { + int index; + if ((index = indexOf(ele)) == -1) + return false; + remove(index); + return true; + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + public int size() { + return size; + } + + private void grow() { + if (size >= elementData.length) { + int curLen = elementData.length; + int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); + elementData = Arrays.copyOf(elementData, newLen); + } + } + + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + return indexOf(ele) != -1; + } + + public int indexOf(T ele) { + for (int i = 0; i < size; i++) { + if (ele == null) + if (null == elementData[i]) + return i; + else if (ele.equals(elementData[i])) + return i; + } + return -1; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//寰呴亶鍘嗗厓绱犵殑涓嬫爣 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public T next() { + if (cursor >= size) + throw new NoSuchElementException(); + return (T) elementData[cursor++]; + } + + @Override + public void remove() { + if (cursor >= size) + throw new NoSuchElementException(); + ArrayList.this.remove(cursor--); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + for (Object ele : elementData) { + sb.append(ele).append(" "); + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/Iterator.java b/group17/785396327/list/Iterator.java index 0df87c6cf1..382dbf0c84 100644 --- a/group17/785396327/list/Iterator.java +++ b/group17/785396327/list/Iterator.java @@ -1,13 +1,13 @@ -package list; - -/** - * Created by IBM on 2017/2/25. - */ -public interface Iterator { - - boolean hasNext(); - - T next(); - - void remove(); -} +package list; + +/** + * Created by IBM on 2017/2/25. + */ +public interface Iterator { + + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group17/785396327/list/LinkedList.java b/group17/785396327/list/LinkedList.java index 1b1fa0fccb..c0cbaf7670 100644 --- a/group17/785396327/list/LinkedList.java +++ b/group17/785396327/list/LinkedList.java @@ -1,163 +1,163 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public class LinkedList implements List { - private int size; - private Node first; - private Node last; - - private static class Node { - Node next; - Node prev; - T data; - - Node(Node prev, Node next, T data) { - this.prev = prev; - this.next = next; - this.data = data; - } - } - - @Override - public int size() { - return this.size; - } - - @Override - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - Node head = first; - while (head != null) { - if ((ele == null && head.data == null) || (ele.equals(head.data))) - return true; - head = head.next; - } - return false; - } - - @Override - public boolean add(T ele) { - if (first == null) - first = last = new Node(null, null, ele); - else { - //鏂版坊鍔犺妭鐐圭殑涓婁竴涓妭鐐规槸鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐 - Node addNode = new Node(last, null, ele); - //鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐圭殑涓嬩竴涓妭鐐归渶瑕佹寚鍚戞柊娣诲姞鐨勮妭鐐 - last.next = addNode; - //鏇存柊鏈鍚庝竴涓妭鐐逛负鏂版坊鍔犵殑鑺傜偣 - last = addNode; - } - size++; - return true; - } - - @Override - public boolean add(int index, T ele) { - checkBounds(index, true); - if (index == size) add(ele); - else { - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index - 1)//寰楀埌瑕佹彃鍏ヤ綅缃殑鍓嶄竴涓妭鐐 - head.next = new Node(head, head.next, ele); - else - head = head.next; - } - } - size++; - return true; - } - - @Override - public boolean remove(T ele) { - if (!contains(ele)) - return false; - Node head = first; - Node prev = head.prev; - while (head != null) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) { - prev.next = head.next; - size--; - return true; - } - prev = head; - head = head.next; - } - return false; - } - - @Override - public T remove(int index) { - checkBounds(index, false); - T removeEle = get(index); - remove(removeEle); - return removeEle; - } - - @Override - public T get(int index) { - checkBounds(index, false); - if (index > (size >> 1)) { - //绱㈠紩浣嶇疆澶т簬1/2size锛屼粠鍚庡線鍓 - Node tail = last; - for (int i = size - 1; i >= 0; i--) { - if (i == index) - return (T) tail.data; - else - tail = tail.prev; - } - } else { - //浠庡墠寰鍚 - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index) - return (T) head.data; - else - head = head.next; - } - } - return null; - } - - @Override - public int indexOf(T ele) { - if (first == null) return -1; - Node head = first; - for (int i = 0; i < size; i++) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) - return i; - head = head.next; - } - return -1; - } - - /** - * 鎸囧畾浣嶇疆鏌ユ壘鍏冪礌鍜屾彃鍏ュ厓绱犲埌鎸囧畾浣嶇疆IndexOutofBounds鐨勫垽鏂爣鍑嗕笉涓鏍 - * - * @param index - * @param isInsert - */ - private void checkBounds(int index, boolean isInsert) { - if (isInsert && (index < 0 || index > size))//鍏佽鎻掑叆鍒版渶鍚庝竴涓厓绱犱箣鍚庯紝涓嶈兘鎺掗櫎= - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - if (index < 0 || index >= size)//鏌ヨ浠0 --- size-1 - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - Node head = first; - while (head != null) { - sb.append(head.data + " "); - head = head.next; - } - return sb.append("]").toString(); - } -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + + private static class Node { + Node next; + Node prev; + T data; + + Node(Node prev, Node next, T data) { + this.prev = prev; + this.next = next; + this.data = data; + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + Node head = first; + while (head != null) { + if ((ele == null && head.data == null) || (ele.equals(head.data))) + return true; + head = head.next; + } + return false; + } + + @Override + public boolean add(T ele) { + if (first == null) + first = last = new Node(null, null, ele); + else { + //鏂版坊鍔犺妭鐐圭殑涓婁竴涓妭鐐规槸鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐 + Node addNode = new Node(last, null, ele); + //鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐圭殑涓嬩竴涓妭鐐归渶瑕佹寚鍚戞柊娣诲姞鐨勮妭鐐 + last.next = addNode; + //鏇存柊鏈鍚庝竴涓妭鐐逛负鏂版坊鍔犵殑鑺傜偣 + last = addNode; + } + size++; + return true; + } + + @Override + public boolean add(int index, T ele) { + checkBounds(index, true); + if (index == size) add(ele); + else { + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index - 1)//寰楀埌瑕佹彃鍏ヤ綅缃殑鍓嶄竴涓妭鐐 + head.next = new Node(head, head.next, ele); + else + head = head.next; + } + } + size++; + return true; + } + + @Override + public boolean remove(T ele) { + if (!contains(ele)) + return false; + Node head = first; + Node prev = head.prev; + while (head != null) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) { + prev.next = head.next; + size--; + return true; + } + prev = head; + head = head.next; + } + return false; + } + + @Override + public T remove(int index) { + checkBounds(index, false); + T removeEle = get(index); + remove(removeEle); + return removeEle; + } + + @Override + public T get(int index) { + checkBounds(index, false); + if (index > (size >> 1)) { + //绱㈠紩浣嶇疆澶т簬1/2size锛屼粠鍚庡線鍓 + Node tail = last; + for (int i = size - 1; i >= 0; i--) { + if (i == index) + return (T) tail.data; + else + tail = tail.prev; + } + } else { + //浠庡墠寰鍚 + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index) + return (T) head.data; + else + head = head.next; + } + } + return null; + } + + @Override + public int indexOf(T ele) { + if (first == null) return -1; + Node head = first; + for (int i = 0; i < size; i++) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) + return i; + head = head.next; + } + return -1; + } + + /** + * 鎸囧畾浣嶇疆鏌ユ壘鍏冪礌鍜屾彃鍏ュ厓绱犲埌鎸囧畾浣嶇疆IndexOutofBounds鐨勫垽鏂爣鍑嗕笉涓鏍 + * + * @param index + * @param isInsert + */ + private void checkBounds(int index, boolean isInsert) { + if (isInsert && (index < 0 || index > size))//鍏佽鎻掑叆鍒版渶鍚庝竴涓厓绱犱箣鍚庯紝涓嶈兘鎺掗櫎= + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + if (index < 0 || index >= size)//鏌ヨ浠0 --- size-1 + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + Node head = first; + while (head != null) { + sb.append(head.data + " "); + head = head.next; + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/List.java b/group17/785396327/list/List.java index 706f9f29bb..54fb72108d 100644 --- a/group17/785396327/list/List.java +++ b/group17/785396327/list/List.java @@ -1,25 +1,25 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public interface List { - - int size(); - - boolean isEmpty(); - - boolean contains(T ele); - - boolean add(T ele); - - boolean add(int index, T ele); - - boolean remove(T ele); - - T remove(int index); - - T get(int index); - - int indexOf(T ele); -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean contains(T ele); + + boolean add(T ele); + + boolean add(int index, T ele); + + boolean remove(T ele); + + T remove(int index); + + T get(int index); + + int indexOf(T ele); +} diff --git a/group17/785396327/queue/Queue.java b/group17/785396327/queue/Queue.java index 68bea83b77..7e399961ed 100644 --- a/group17/785396327/queue/Queue.java +++ b/group17/785396327/queue/Queue.java @@ -1,41 +1,41 @@ -package queue; - -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class Queue extends LinkedList { - - public boolean add(T ele) { - return add(ele); - } - - public T element() { - if (size() == 0) - throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); - return get(0); - } - - public boolean offer(T ele) { - return add(ele); - } - - public T peek() { - if (size() == 0) - return null; - return get(0); - } - - public T poll() { - if (size() == 0) - return null; - return remove(0); - } - - public T remove() { - if (size() == 0) - throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); - return remove(0); - } -} +package queue; + +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class Queue extends LinkedList { + + public boolean add(T ele) { + return add(ele); + } + + public T element() { + if (size() == 0) + throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); + return get(0); + } + + public boolean offer(T ele) { + return add(ele); + } + + public T peek() { + if (size() == 0) + return null; + return get(0); + } + + public T poll() { + if (size() == 0) + return null; + return remove(0); + } + + public T remove() { + if (size() == 0) + throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); + return remove(0); + } +} diff --git a/group17/785396327/stack/Stack.java b/group17/785396327/stack/Stack.java index c917809cc3..980ca2c0e6 100644 --- a/group17/785396327/stack/Stack.java +++ b/group17/785396327/stack/Stack.java @@ -1,29 +1,29 @@ -package stack; - -import java.util.EmptyStackException; - -/** - * Created by william on 2017/2/25. - */ -public class Stack extends ArrayList { - - public boolean empty() { - return isEmpty(); - } - - public T peek() { - if (size() == 0) - throw new EmptyStackException(); - return (T) get(0); - } - - public T pop() { - if (size() == 0) - throw new EmptyStackException(); - return (T) remove(0); - } - - public void push(T ele) { - add(0, ele); - } -} +package stack; + +import java.util.EmptyStackException; + +/** + * Created by william on 2017/2/25. + */ +public class Stack extends ArrayList { + + public boolean empty() { + return isEmpty(); + } + + public T peek() { + if (size() == 0) + throw new EmptyStackException(); + return (T) get(0); + } + + public T pop() { + if (size() == 0) + throw new EmptyStackException(); + return (T) remove(0); + } + + public void push(T ele) { + add(0, ele); + } +} diff --git a/group17/82427129/.gitignore b/group17/82427129/.gitignore new file mode 100644 index 0000000000..ccfa635638 --- /dev/null +++ b/group17/82427129/.gitignore @@ -0,0 +1,7 @@ +/.metadata/ +/RemoteSystemsTempFiles/ + +/JavaUtil/.settings/ + +.classpath +.project diff --git a/group17/82427129/JavaUtil/.gitignore b/group17/82427129/JavaUtil/.gitignore new file mode 100644 index 0000000000..24d64373c4 --- /dev/null +++ b/group17/82427129/JavaUtil/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/group17/82427129/JavaUtil/pom.xml b/group17/82427129/JavaUtil/pom.xml new file mode 100644 index 0000000000..6c95dbb077 --- /dev/null +++ b/group17/82427129/JavaUtil/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + com.coding.basic + JavaUtil + 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + + + + junit + junit + 4.7 + test + + + \ No newline at end of file diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..d9f3d217f5 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + private static Object[] EMPTY_ELEMENTDATA = {}; + + private static int INITIALCAPACITY = 10; + + public ArrayList(){ + elementData = EMPTY_ELEMENTDATA; + } + + public ArrayList(int initialCapacity){ + elementData = new Object[INITIALCAPACITY]; + } + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + rangeCheckForAdd(index); + + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object set(int index, Object o){ + rangeCheck(index); + + Object oldValue = elementData[index]; + elementData[index] = o; + return oldValue; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldValue = elementData[index]; + int movedLength = size - index - 1; + if(movedLength > 0)//当要删除最后一个元素时,不需要移动数组,只需要把最后一个元素置null + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size] = null; + return oldValue; + } + + private void rangeCheckForAdd(int index){ + if( index > size || index<0 ){ + throw new IndexOutOfBoundsException(outofIndex(index)); + } + } + private void rangeCheck(int index){ + if( index >= size || index < 0){ + throw new IndexOutOfBoundsException(outofIndex(index)); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private void ensureCapacity(int minCapacity){ + if(elementData == EMPTY_ELEMENTDATA){ + minCapacity = Math.max(minCapacity, INITIALCAPACITY);//针对addall首次增加的数量就比INITIALCAPACITY多 + } + if(minCapacity - elementData.length > 0){ + grow(minCapacity); + } + } + + private void grow(int minCapcity){ + int oldCapacity = elementData.length; + int newCapcity = oldCapacity + (oldCapacity>>1); + if(newCapcity - minCapcity < 0){ + newCapcity = minCapcity; + } + elementData = Arrays.copyOf(elementData, newCapcity); + } + + private String outofIndex(int index){ + return "Index: "+index+", Size: "+size; + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/BinaryTreeNode.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/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/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/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/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2a63eee0fc --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,180 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private int size = 0; + + private Node first; + + private Node last; + + public void add(Object o){ + add(size,o); + } + public void add(int index , Object o){ + rangeCheck(index); + + if(index == size){ + linkLast(o); + }else{ + linkBefore(o, indexOf(index)); + } + } + private void linkBefore(Object o ,Node succ){ + final Node prev = succ.prev; + final Node newNode = new Node(prev, o, succ); + succ.prev = newNode; + if(prev == null){ + first = newNode; + }else{ + prev.next = newNode; + } + size++; + } + private void linkLast(Object o){ + final Node succ = last; + final Node newNode = new Node(succ, o, null); + last = newNode; + if(succ == null){ + first = newNode; + }else{ + succ.next = newNode; + } + size++; + } + private void rangeCheck(int index) { + if(index > size|| index < 0 ) + throw new IndexOutOfBoundsException("Size"+size+":index"+index); + } + private void elementIndexCheck(int index){ + if(index >=size||index < 0) + throw new IndexOutOfBoundsException("Size"+size+":index"+index); + } + /** + * 获取“下标”为index的值, + * index为size时返回null + * @param index + * @return + */ + private Node indexOf(int index){ + if(index < (this.size>>1) ){ + Node x = first; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + }else{ + Node x = last; + for (int i = this.size-1; i > index; i--) { + x = x.prev; + } + return x; + } + } + + public Object get(int index){ + elementIndexCheck(index); + + return indexOf(index); + } + public Object remove(int index){ + elementIndexCheck(index); + + if(index == 0){ + return removeFirst(); + }else if(index == size) { + return removeLast(); + }else{ + return unlinkNode(indexOf(index)); + } + } + + private Object unlinkNode(Node node) { + final Node next = node.next; + final Node prev = node.prev; + final Object element = node.data; + if(next == null){ + last = node; + }else{ + next.prev = node; + node.next = next; + } + if(prev == null){ + first = node; + }else{ + prev.next = node; + node.prev = prev; + } + size--; + node.data = null; + + return element; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + linkBefore(o, first); + } + + public void addLast(Object o){ + linkLast(o); + } + + public Object removeFirst(){ + if(first == null) + throw new NoSuchElementException("first is null"); + + Object oldData = first.data; + final Node next = first.next; + first.data = null; + first.next = null;//GC + first = next; + + if(next == null){ + last = null; + }else{ + next.prev = null; + } + size--; + + return oldData; + } + + public Object removeLast(){ + if(last == null) + throw new NoSuchElementException("last is null"); + + Object oldData = last.data; + final Node prev = last.prev; + last.prev = null; + last.data = null;//GC + last = prev; + + if(prev == null){ + first = null; + }else{ + prev.next = null; + } + size--; + + return oldData; + } + + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + Node prev; + Node(Node prev,Object data,Node next){ + this.data = data; + this.next = next; + this.prev = prev; + } + } +} \ No newline at end of file diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/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/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Queue.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b54ff5f56b --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + return elementData.removeLast(); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Stack.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7c00e1950c --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int length = size(); + Object lastData = peek(); + elementData.remove(length - 1); + + return lastData; + } + + public Object peek(){ + if(size()<=0) + throw new EmptyStackException(); + + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group17/865797761/.classpath b/group17/865797761/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group17/865797761/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/865797761/.gitignore b/group17/865797761/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/865797761/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/865797761/.project b/group17/865797761/.project new file mode 100644 index 0000000000..a78673e93c --- /dev/null +++ b/group17/865797761/.project @@ -0,0 +1,17 @@ + + + 865797761 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/865797761/.settings/org.eclipse.jdt.core.prefs b/group17/865797761/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group17/865797761/.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/group17/865797761/src/Collection/ArrayList.java b/group17/865797761/src/Collection/ArrayList.java new file mode 100644 index 0000000000..2e9b68d0ea --- /dev/null +++ b/group17/865797761/src/Collection/ArrayList.java @@ -0,0 +1,135 @@ +package Collection ; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + Created by william on 2017/2/25. + */ +public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) + throw new RuntimeException("非法初始化大小参数!"); + elementData = new Object[initialCapacity]; + } + + public boolean add(T ele) { + grow(); + elementData[size] = ele; + size++; + return true; + } + + public T get(int index) { + checkBounds(index); + return (T) elementData[index]; + } + + public T remove(int index) { + checkBounds(index); + T removeEle = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return removeEle; + } + + public boolean add(int index, T ele) { + checkBounds(index); + size++;//有效元素内容先加,保证长度极限情况grow在添加前生效 + grow(); + //将原本数组从待插入的index截取,将原本index后的有效值,复制到原本数组index+1之后 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = ele; + return true; + } + + @Override + public boolean remove(T ele) { + int index; + if ((index = indexOf(ele)) == -1) + return false; + remove(index); + return true; + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + public int size() { + return size; + } + + private void grow() { + if (size >= elementData.length) { + int curLen = elementData.length; + int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); + elementData = Arrays.copyOf(elementData, newLen); + } + } + + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + return indexOf(ele) != -1; + } + + public int indexOf(T ele) { + for (int i = 0; i < size; i++) { + if (ele == null) + if (null == elementData[i]) + return i; + else if (ele.equals(elementData[i])) + return i; + } + return -1; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//待遍历元素的下标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public T next() { + if (cursor >= size) + throw new NoSuchElementException(); + return (T) elementData[cursor++]; + } + + @Override + public void remove() { + if (cursor >= size) + throw new NoSuchElementException(); + ArrayList.this.remove(cursor--); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + for (Object ele : elementData) { + sb.append(ele).append(" "); + } + return sb.append("]").toString(); + } +} \ No newline at end of file diff --git a/group17/865797761/src/Collection/Iterable.java b/group17/865797761/src/Collection/Iterable.java new file mode 100644 index 0000000000..04955e1b6e --- /dev/null +++ b/group17/865797761/src/Collection/Iterable.java @@ -0,0 +1,6 @@ +package Collection; + +// +public interface Iterable { + Iterator iterator(); +} \ No newline at end of file diff --git a/group17/865797761/src/Collection/Iterator.java b/group17/865797761/src/Collection/Iterator.java new file mode 100644 index 0000000000..5ff4bbaf79 --- /dev/null +++ b/group17/865797761/src/Collection/Iterator.java @@ -0,0 +1,7 @@ +package Collection; + +public interface Iterator { + public boolean hasNext(); + + public E next(); +} \ No newline at end of file diff --git a/group17/865797761/src/Collection/List.java b/group17/865797761/src/Collection/List.java new file mode 100644 index 0000000000..eeef249f88 --- /dev/null +++ b/group17/865797761/src/Collection/List.java @@ -0,0 +1,14 @@ +package Collection; + +// +public interface List { + public void add(E o); + + public void add(int index, E o); + + public E get(int index); + + public E remove(int index); + + public int size(); +} \ No newline at end of file diff --git a/group17/865797761/src/Collection/MyArrayList.java b/group17/865797761/src/Collection/MyArrayList.java new file mode 100644 index 0000000000..50e2c24d01 --- /dev/null +++ b/group17/865797761/src/Collection/MyArrayList.java @@ -0,0 +1,110 @@ +package Collection; + +import java.util.Arrays; + +public class MyArrayList implements List, Iterable { + private Object[] elementData; + private static final int DEFAULT_SIZE = 10; + private int size; + + public MyArrayList() { + this(DEFAULT_SIZE); + } + + public MyArrayList(int initSize) { + if (initSize < 0) { + throw new IllegalArgumentException(initSize + " < 0"); + } + if (initSize == 0) { + elementData = new Object[DEFAULT_SIZE]; + } + else { + elementData = new Object[initSize]; + } + size = 0; + } + + public void add(E o) { + growIfNeed(); + elementData[size++] = o; + } + + public void add(int index, E o) { + if (index < 0 || index > size) { + throw new IllegalArgumentException("index:" + index); + } + growIfNeed(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @SuppressWarnings("unchecked") + public E get(int index) { + rangeCheck(index); + return (E) elementData[index]; + } + + public E remove(int index) { + rangeCheck(index); + E target = get(index); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return target; + } + + public int size() { + return size; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new NoSuchElementException("index:" + index); + } + } + + private void growIfNeed() { + if (size == elementData.length) + grow(); + } + + private void grow() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + @Override + public Iterator iterator() { + return new ArrayIterator<>(); + } + + private class ArrayIterator implements Iterator { + private int currentPos = 0; + + @Override + public boolean hasNext() { + return currentPos < size; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + rangeCheck(currentPos); + return (E) elementData[currentPos++]; + } + + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData, size)); + } + +} + +class NoSuchElementException extends RuntimeException { + + public NoSuchElementException(String string) { + super(string); + } + +} \ No newline at end of file diff --git a/group17/865797761/src/Collection/MyLinkedList.java b/group17/865797761/src/Collection/MyLinkedList.java new file mode 100644 index 0000000000..cf7495ab4e --- /dev/null +++ b/group17/865797761/src/Collection/MyLinkedList.java @@ -0,0 +1,144 @@ +package Collection; + +public class MyLinkedList implements List, Iterable { + private Node head; + private int size; + + public MyLinkedList() { + size = 0; + } + + public void add(E o) { + if (head == null) + addFirst(o); + else + addLast(o); + } + + public void addFirst(E o) { + Node oldFirst = head; + head = new Node<>(o, oldFirst); + size++; + } + + public void addLast(E o) { + if (head == null) { + addFirst(o); + } + else { + Node oldLast = movePtrTo(size - 1); + oldLast.next = new Node<>(o, null); + size++; + } + + } + + public void add(int index, E o) { + if (index > size || index < 0) { + throw new IllegalArgumentException("index:" + index); + } + if (index == 0) { + addFirst(o); + return; + } + Node temp = movePtrTo(index - 1); + Node oldNext = temp.next; + Node newNext = new Node<>(o, oldNext); + temp.next = newNext; + size++; + } + + public E remove(int index) { + rangeCheck(index); + E data; + if (index == 0) { + data = head.data; + head = head.next; + } + else { + Node pre = movePtrTo(index - 1); + Node target = pre.next; + pre.next = target.next; + data = target.data; + } + size--; + return data; + } + + public E get(int index) { + rangeCheck(index); + return movePtrTo(index).data; + } + + public int size() { + return size; + } + + private Node movePtrTo(int index) { + Node resultNode = head; + for (int i = 0; i < index; i++) { + resultNode = resultNode.next; + } + return resultNode; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new NoSuchElementException("index:" + index); + } + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder('['); + Node temp = head; + while (temp != null) { + stringBuilder.append(String.valueOf(temp.toString()) + ","); + temp = temp.next; + } + stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length()); + stringBuilder.append(']'); + return stringBuilder.toString(); + } + + private static class Node { + private T data; + private Node next; + + public Node(T data, Node next) { + this.data = data; + this.next = next; + } + + @Override + public String toString() { + return data.toString(); + } + } + + @Override + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + Node currentNode; + + public ListIterator() { + currentNode = head; + } + + @Override + public boolean hasNext() { + return currentNode.next != null; + } + + @Override + public E next() { + Node temp = currentNode; + currentNode = currentNode.next; + return temp.data; + } + + } +} diff --git a/group17/865797761/src/Collection/MyQueue.java b/group17/865797761/src/Collection/MyQueue.java new file mode 100644 index 0000000000..f24535e6be --- /dev/null +++ b/group17/865797761/src/Collection/MyQueue.java @@ -0,0 +1,34 @@ +package Collection; + +public class MyQueue { + private MyLinkedList elementData = new MyLinkedList<>(); + + public void enQueue(T o) { + elementData.addLast(o); + } + + public T deQueue() { + if (!isEmpty()) { + return elementData.remove(0); + } + throw new QueueIsEmptyException(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} + +class QueueIsEmptyException extends RuntimeException { + public QueueIsEmptyException() { + super(); + } + + public QueueIsEmptyException(String string) { + super(string); + } +} \ No newline at end of file diff --git a/group17/876385982/.classpath b/group17/876385982/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group17/876385982/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/876385982/.gitignore b/group17/876385982/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/876385982/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/876385982/.project b/group17/876385982/.project new file mode 100644 index 0000000000..4062c6c28f --- /dev/null +++ b/group17/876385982/.project @@ -0,0 +1,17 @@ + + + 876385982 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/876385982/src/test/Test.java b/group17/876385982/src/test/Test.java new file mode 100644 index 0000000000..5eeddf17a3 --- /dev/null +++ b/group17/876385982/src/test/Test.java @@ -0,0 +1,10 @@ +package test; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println("Test!"); + } + +} diff --git "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" index eee6b340dd..36b44b4aa8 100644 --- "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" +++ "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" @@ -12,9 +12,9 @@ 1204187480 -102228177 +102228177 http://note.youdao.com/noteshare?id=74a51e7f93461dfb77c69a1cf4755624&sub=004F10FA5D2046ABAA060F19C0D2A18F -876385982 +876385982 http://www.totoro-fly.com/?p=59 785396327 @@ -22,21 +22,21 @@ 240094626 -82427129 +82427129 http://blog.csdn.net/walk_er/article/details/57406278 296910598 -1264835468 +1264835468 http://www.jianshu.com/p/191d731ec00a 516886559 -1282579502 +1282579502 https://www.evernote.com/shard/s413/sh/3af5f6a4-a580-4a49-b63c-90f5b178aca4/7cf052e3789c862e38d6d6b3cce1ceed 614982500 865797761 -1540186032 +1540186032 http://blog.csdn.net/mpx_xb/article/details/56679603 176653813 diff --git a/group17/group17.md b/group17/group17.md index 8b13789179..d3f5a12faa 100644 --- a/group17/group17.md +++ b/group17/group17.md @@ -1 +1 @@ - + diff --git a/group18/1078285863/javaStudy/src/basicStruct/BasicStruct.java b/group18/1078285863/javaStudy/src/basicStruct/BasicStruct.java new file mode 100644 index 0000000000..1461dedf79 --- /dev/null +++ b/group18/1078285863/javaStudy/src/basicStruct/BasicStruct.java @@ -0,0 +1,11 @@ +package basicStruct; +// + +public class BasicStruct { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/group18/1078285863/javaStudy/src/simpleArrayList/SimpleArrayList.java b/group18/1078285863/javaStudy/src/simpleArrayList/SimpleArrayList.java new file mode 100644 index 0000000000..689fdccaf5 --- /dev/null +++ b/group18/1078285863/javaStudy/src/simpleArrayList/SimpleArrayList.java @@ -0,0 +1,140 @@ +package simpleArrayList; + +import java.util.ArrayList; +import java.util.Arrays; + +public class SimpleArrayList { + //存储array的容器 + private Object[] elementData; + + //默认容量 + public static final int default_capacity = 10; + + //array中元素的个数 + private int size; + + //默认构造函数 + public SimpleArrayList(){ + //构造大小默认为10的数组 + this.elementData = new Object[default_capacity]; + } + + //带参构造函数,自己指定容量大小 + public SimpleArrayList(int initialCapacity) { + if(initialCapacity <= 0){ + //参数无效 + throw new IllegalArgumentException("IllegalArgument Error"); + } + if(initialCapacity < default_capacity) + { + initialCapacity = default_capacity; + } + + this.elementData = new Object[initialCapacity]; + } + + //在索引处添加元素 + public boolean Add(int index,Object o) + { + if(index > size || index <0) + { + return false; + } + + //查找元素 + for (int i = 0; i < size; i++) { + if(o.equals(elementData[i])) + { + //移动元素的个数 + int nMove = size -index -1; + if(nMove > 0){ + System.arraycopy(elementData, index, elementData, index+1,nMove); + elementData[index] = null; + } + else { + return false; + } + } + } + + return true; + } + + //添加元素 + //返回值:是否添加成功 + public boolean Add(Object obj) + { + if (null == obj) { + throw new IllegalArgumentException("invalid Argument!"); + } + //给array扩容 + ensureCapacityInternal(size + 1); + + //数组尾部元素 = 新值 + elementData[size++] = obj; + + return true; + } + + public void clear() + { + //将elementData中元素指向NULL,促使垃圾回收机制启动 + for (int i = 0; i < elementData.length; i++) { + elementData[i] = null; + } + + //array元素个数清零 + size = 0; + } + + //移除array中的元素 + public boolean remove(Object obj) + { + //遍历elementData,查找元素 + for (int index = 0; index < size; index++) { + if(obj.equals(elementData[index]))//是否相等 + { + fastRemove(index); + return true; + } + } + return false; + } + + private void fastRemove(int index) { + //移动元素的个数 + int numMoved = size - index - 1; + if (numMoved > 0) + //将index后数据以此向前移动 + System.arraycopy(elementData, index+1, elementData, index,numMoved); + elementData[--size] = null; + } + + //返回元素总大小 + public int size() { + return size; + } + + public Object get(int index) { + //校验参数 + if(index > size || index < 0) + throw new IllegalArgumentException(); + + return elementData[index]; + } + + //涉及到最大扩容空间问题,暂时先不考虑 + private void ensureCapacityInternal(int minCapacity) { + //扩容情况,当前容器容量无法存下新数据时,是否超过了当前数组的长度 + System.out.println("element data length is "+elementData.length); + if(minCapacity - elementData.length > 0) + { + //扩容为以前容量的1.5倍 + int oldCapacity = elementData.length; + int newCapacity = oldCapacity *3/2; + + //内部创建新数组,把elementData的元素拷贝到新数组 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } +} diff --git a/group18/1078285863/javaStudy/src/simpleLinkedList/SimpleLinkedList.java b/group18/1078285863/javaStudy/src/simpleLinkedList/SimpleLinkedList.java new file mode 100644 index 0000000000..0f11532723 --- /dev/null +++ b/group18/1078285863/javaStudy/src/simpleLinkedList/SimpleLinkedList.java @@ -0,0 +1,197 @@ +package simpleLinkedList; + +import java.util.Iterator; +import java.util.LinkedList; + +import javax.sound.sampled.Line; + +public class SimpleLinkedList { + //LinkedList + private int size = 0; + private Node head = null; + private Node tail = null; + + private static class Node{ + Object data; + Node next;//指向后一个元素 + Node prev; //指向前一个元素 + } + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + //先找到index位置的元素 + Node tmp = null; + for (int i = 0; i < index; i++) { + tmp = tmp.next; + } + + Node pre = tmp.prev; + Node next = tmp.next; + + if (null == pre) { + addFirst(o); //加入头部 + } + else if(null == next){ + addLast(o); //加入尾部 + } + else { + add(o); + } + } + public Object get(int index){ + if (index > size || index <0) { + throw new IllegalArgumentException(); + } + + Node temp = null; + for(int i=0;i queueList = new LinkedList(); + public void enQueue(Object o){ + queueList.add(o); + } + + public Object deQueue(){ + return queueList.removeFirst(); + } + + public boolean isEmpty(){ + return queueList.isEmpty(); + } + + public int size(){ + return queueList.size(); + } +} \ No newline at end of file diff --git a/group18/1078285863/javaStudy/src/simpleStack/SimpleStack.java b/group18/1078285863/javaStudy/src/simpleStack/SimpleStack.java new file mode 100644 index 0000000000..bfbea7b1db --- /dev/null +++ b/group18/1078285863/javaStudy/src/simpleStack/SimpleStack.java @@ -0,0 +1,44 @@ +package simpleStack; + +import java.util.ArrayList; + +public class SimpleStack { +private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + //判断队列是否为空 + Object obj = peek(); + if(obj != null) + { + elementData.remove(obj); + return obj; + } + else { + return null; + } + } + + public Object peek(){ + if(elementData.isEmpty()){ + return null; + } + else { + int lastIndex = elementData.size() -1; + Object obj = elementData.get(lastIndex); + return obj; + } + + } + public boolean isEmpty(){ + boolean bEmpty = false; + bEmpty = elementData.isEmpty()?true:false; + return bEmpty; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group18/1159828430/20170219/.classpath b/group18/1159828430/20170219/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group18/1159828430/20170219/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group18/1159828430/20170219/.gitignore b/group18/1159828430/20170219/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/1159828430/20170219/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/1159828430/20170219/.project b/group18/1159828430/20170219/.project new file mode 100644 index 0000000000..da66c3a498 --- /dev/null +++ b/group18/1159828430/20170219/.project @@ -0,0 +1,17 @@ + + + 20170219 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c8db730110 --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java @@ -0,0 +1,145 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * @author Hipple + * @Time锛2017骞2鏈20鏃 涓嬪崍8:53:31 + * @version 1.0 + */ +public class ArrayList implements List { + + //鍏冪礌鏁伴噺 + private int size = 0; + + //榛樿瀹归噺 + private final int defaultCapacity = 10; + + //瀛樺偍鍏冪礌鐨勫鍣 + private static Object[] elementData; + + //鏃犲弬鏋勯犲櫒 + public ArrayList(){ + elementData = new Object[defaultCapacity]; + } + + //鎸囧畾瀹归噺鐨勬瀯閫犲櫒 + public ArrayList(int capacity){ + if (capacity < 0) { + //闈炴硶鍙傛暟 + throw new IllegalArgumentException("Illegal Capacity: "+ capacity); + } + elementData = new Object[capacity]; + } + + //娣诲姞鍏冪礌 + public boolean add(Object o){ + ensureCapacityInternal(size + 1); + elementData[size++] = o; + return true; + } + + //娣诲姞鍏冪礌鍒版寚瀹氫綅缃 + public void add(int index, Object o){ + rangeCheck(index); + //灏嗗綋鍓嶄綅缃強鍚庣画鍏冪礌鍚庣Щ涓浣 + ensureCapacityInternal(size + 1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + //鏍规嵁涓嬭〃鑾峰彇鍊 + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + //鍒犻櫎鍏冪礌 + public Object remove(int index){ + rangeCheck(index); + Object oldValue = elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) { + //瑕佸垹闄ょ殑鍏冪礌涓嶆槸鏈鍚庝竴涓椂锛屽皢褰撳墠鍏冪礌鍙婂悗缁厓绱犲乏绉讳竴浣 + System.arraycopy(elementData, index+1, elementData, index, numMoved); + } + elementData[--size] = null;//鑷姩鍥炴敹 + return oldValue; + } + + //鍒犻櫎鍏冪礌 + public boolean remove(Object o) { + // 鐢变簬ArrayList涓厑璁稿瓨鏀緉ull锛屽洜姝や笅闈㈤氳繃涓ょ鎯呭喌鏉ュ垎鍒鐞嗐 + if (o == null) { + for (int index = 0; index < size; index++){ + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int index = 0; index < size; index++){ + if (o.equals(elementData[index])) { + fastRemove(index); + return true; + } + } + } + return false; + } + + //杩斿洖鐜版湁鍏冪礌鏁伴噺 + public int size(){ + return size; + } + + //鏄惁涓虹┖ + public boolean isEmpty(){ + return size == 0; + } + + public Iterator iterator(){ + return null; + } + + //鍔ㄦ佸鍔燗rrayList澶у皬 + private void ensureCapacityInternal(int minCapacity) { + //褰撳墠鏁扮粍鏃犳硶鍐嶅瓨鏀炬椂灏嗘暟缁勯暱搴﹀鍔犺嚦鍘熼暱搴︾殑1.5鍊 + if (minCapacity - elementData.length > 0) { + int newCapacity = (elementData.length * 3)/2; + elementData = Arrays.copyOf(elementData, newCapacity); + } + + } + + //妫鏌ユ槸鍚︿笅鏍囪秺鐣 + private void rangeCheck(int index){ + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + } + + //鍒犻櫎鍏冪礌锛屼笌remove鐨 宸埆灏辨槸娌℃湁涓嬫爣妫鏌 + private void fastRemove(int index) { + int numMoved = size - index - 1; + if (numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + } + +} +class testArrayList{ + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + for (int i = 0; i < 10; i++) { + arrayList.add(i+1); + } + arrayList.add(5,15); + arrayList.remove(11); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("value is "+arrayList.get(i)); + } + } +} diff --git a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..0ce3f762ba --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java @@ -0,0 +1,11 @@ +package com.coding.basic; +/** + * @author Hipple + * @Time锛2017骞2鏈20鏃 涓嬪崍8:56:05 + * @version 1.0 + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..433715d5ad --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java @@ -0,0 +1,238 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * @author Hipple + * @Time锛2017骞2鏈21鏃 涓嬪崍8:00:21 + * @version 1.0 + */ +public class LinkedList implements List { + + //澶寸粨鐐 + private Node first; + + //灏剧粨鐐 + private Node last; + + //鍏冪礌鏁伴噺 + private int size = 0; + + //鏃犲弬鏋勯犲櫒 + public LinkedList(){ + } + + public boolean add(Object o){ + linkLast(o); + return true; + } + + public void add(int index , Object o){ + checkPositionIndex(index); + if (index == size) { + linkLast(o); + } else { + linkBefore(o, node(index)); + } + } + + public Object remove(int index){ + checkElementIndex(index); + return unlink(node(index)); + } + + public Object get(int index){ + checkElementIndex(index); + return node(index).data; + } + + public void addFirst(Object o){ + linkFirst(o); + } + + public void addLast(Object o){ + linkLast(o); + } + + public Object removeFirst(){ + final Node f = first; + if (f == null) { + throw new NoSuchElementException(); + } + return unlinkFirst(f); + } + + public Object removeLast(){ + final Node l = last; + if (l == null) { + throw new NoSuchElementException(); + } + return unlinkLast(l); + } + + public int size(){ + return size; + } + + //妫鏌ユ槸鍚︿负绌 + public boolean isEmpty(){ + return size == 0; + } + + //鑾峰彇澶磋妭鐐 + public Object getFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.data; + } + + public Iterator iterator(){ + return null; + } + + //澶撮儴澧炲姞鑺傜偣 + private void linkFirst(Object data){ + final Node f = first;//f瀛樺偍鑰佺殑澶撮儴鑺傜偣寰呯敤 + final Node newNode = new Node(null, data, first);//鍚庨」鎸囬拡鎸囧悜first锛屽墠椤规寚閽坣ull + first = newNode;//灏嗘柊鑺傜偣鍙樹负澶撮儴鑺傜偣 + if (f == null) {//澶磋妭鐐逛负null鍒欎唬琛ㄩ摼琛ㄤ负绌猴紝閭d箞鏂拌妭鐐逛篃鏄棦鏄ご缁撶偣涔熸槸灏剧粨鐐 + last = newNode; + } else {//鑰佺殑澶撮儴鑺傜偣鍓嶉」鎸囬拡鎸囧悜鏂拌妭鐐 + f.previous = newNode; + } + size++; + } + + //灏鹃儴澧炲姞鑺傜偣 + private void linkLast(Object data){ + final Node l = last;//l瀛樺偍鑰佺殑灏鹃儴鑺傜偣寰呯敤 + final Node newNode = new Node(last, data, null);//鍓嶉」鎸囬拡鎸囧悜last锛屽悗椤规寚閽坣ull + last = newNode;//灏嗘柊鑺傜偣鍙樹负灏鹃儴鑺傜偣 + if (l == null) {//灏捐妭鐐逛负null鍒欎唬琛ㄩ摼琛ㄤ负绌猴紝閭d箞鏂拌妭鐐逛篃鏄棦鏄ご缁撶偣涔熸槸灏剧粨鐐 + first = newNode; + } else {//鑰佺殑灏鹃儴鑺傜偣鍚庨」鎸囬拡鎸囧悜鏂拌妭鐐 + l.next = newNode; + } + size++; + } + + //鎸囧畾index鎻掑叆鑺傜偣 + private void linkBefore(Object o, Node oldNode){ + final Node pred = oldNode.previous; + final Node newNode = new Node(pred, o, oldNode); + oldNode.previous = newNode;//鏃ц妭鐐瑰墠椤规寚閽堟寚鍚戞柊鑺傜偣 + if (pred == null) {//pred涓簄ull浠h〃oldNode涓哄ご鑺傜偣 + first = newNode; + } else { + pred.next = newNode; + } + size++; + + } + + //鍒犻櫎澶撮儴鑺傜偣骞惰繑鍥炶妭鐐瑰 + private Object unlinkFirst(Node f){ + final Object element = f.data;//淇濆瓨澶磋妭鐐圭殑鍊 + final Node next = f.next; + f.data = null;//GC鑷姩鍥炴敹 + f.next = null; + first = next;//灏嗗ご鑺傜偣鐨勪笅涓鑺傜偣鍙樹负澶磋妭鐐 + if (next == null) {//濡傛灉next涓虹┖锛屽垯浠h〃f鍚屾椂涓哄熬鑺傜偣锛屾鏃舵暣涓摼琛ㄤ负绌 + last = null; + } else { + next.previous = null; + } + size--; + return element; + } + + //鍒犻櫎灏鹃儴鑺傜偣骞惰繑鍥炶鑺傜偣鐨勫 + private Object unlinkLast(Node l){ + final Object element = l.data;//淇濆瓨灏捐妭鐐圭殑鍊 + final Node prev = l.previous; + l.previous = null; + l.data = null;//GC鑷姩鍥炴敹 + last = prev;//灏嗗熬鑺傜偣鐨勪笂涓鑺傜偣鍙樹负灏捐妭鐐 + if (prev == null) {//濡傛灉prev涓虹┖锛屽垯浠h〃l鍚屾椂涓哄ご鑺傜偣锛屾鏃舵暣涓摼琛ㄤ负绌 + first = null; + } else { + prev.next = null; + } + size--; + return element; + } + + //鍒犻櫎鎸囧畾鑺傜偣 + private Object unlink(Node x){ + final Object element = x.data; + final Node prev = x.previous; + final Node next = x.next; + if (prev == null) {//prev涓虹┖浠h〃瑕佸垹闄ょ殑鏄ご鑺傜偣 + unlinkFirst(x); + } else {//prev鍚庨」鎸囬拡鎸囧悜next + prev.next = next; + x.previous = null; + } + if (next == null) {//next涓虹┖浠h〃瑕佸垹闄ょ殑鏄熬鑺傜偣 + unlinkLast(x); + } else {//next鍓嶉」鎸囬拡鎸囧悜prev + next.previous = prev; + x.next = null; + } + x.data = null; + size--; + return element; + } + + //鏌ユ壘缁撶偣 + private Node node(int index){ + if (index < (size>>1)) {//鍒ゆ柇寰幆鏂瑰悜 + Node x = first; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) { + x = x.previous; + } + return x; + } + } + + //妫鏌ヤ笅鏍囨槸鍚﹀悎娉 + private void checkElementIndex(int index){ + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + } + + private void checkPositionIndex(int index){ + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + } + + //妫鏌ヨ鍙傛暟鏄惁涓虹幇鏈夊厓绱犵殑绱㈠紩銆 + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + //妫鏌ュ弬鏁版槸鍚︽槸杩唬鍣ㄦ垨娣诲姞鎿嶄綔鐨勬湁鏁堜綅缃殑绱㈠紩 + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private static class Node{ + Object data; + Node next; + Node previous; + Node(Node previous, Object data, Node next) { + this.data = data; + this.next = next; + this.previous = previous; + } + } +} \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/List.java b/group18/1159828430/20170219/src/com/coding/basic/List.java new file mode 100644 index 0000000000..08e0f9e2d5 --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; +/** + * @author Hipple + * @Time锛2017骞2鏈20鏃 涓嬪崍8:52:08 + * @version 1.0 + */ +public interface List { + public boolean add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/Queue.java b/group18/1159828430/20170219/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..e91779fdb2 --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/Queue.java @@ -0,0 +1,31 @@ +package com.coding.basic; +/** + * @author Hipple + * @Time锛2017骞2鏈23鏃 涓嬪崍11:00:00 + * @version 1.0 + */ + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public Queue(){ + + } + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + elementData.getFirst(); + return null; + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group18/1159828430/20170219/src/com/coding/basic/Stack.java b/group18/1159828430/20170219/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..ef5b637050 --- /dev/null +++ b/group18/1159828430/20170219/src/com/coding/basic/Stack.java @@ -0,0 +1,57 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * @author Hipple + * @Time锛2017骞2鏈23鏃 涓嬪崍10:59:39 + * @version 1.0 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack(){ + + } + + //鍏ユ爤 + public void push(Object o){ + elementData.add(o); + } + + //鍑烘爤 + public Object pop(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = peek(); + elementData.remove(o);//閲嶆柊鍐欐牴鎹璞emove + return o; + } + + public Object peek(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} +class TestStack { + public static void main(String[] args){ + Stack myStack=new Stack(); + myStack.push("a"); + myStack.push(2); + myStack.push("123"); + myStack.push("ahu"); + while(!myStack.isEmpty()){ + System.out.println(myStack.pop()); + } + } +} diff --git a/group18/1159828430/README.md b/group18/1159828430/README.md new file mode 100644 index 0000000000..387ebebf6d --- /dev/null +++ b/group18/1159828430/README.md @@ -0,0 +1,2 @@ +# 2017缂栫▼鎻愰珮缇 +杩欓噷鏄1159828430 澶ц繛鈥斾功鐢 鐨勪唬鐮佹彁浜ゅ尯 diff --git a/group18/744888802/dataStructure/pom.xml b/group18/744888802/dataStructure/pom.xml new file mode 100644 index 0000000000..3b262d6184 --- /dev/null +++ b/group18/744888802/dataStructure/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + dataStructure + dataStructure + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + + \ No newline at end of file diff --git a/group18/744888802/dataStructure/src/main/java/com/coding/basic/ArrayList.java b/group18/744888802/dataStructure/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..728edc3500 --- /dev/null +++ b/group18/744888802/dataStructure/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,109 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + //姣忔澧炲姞鐨勯暱搴 + private Integer addArrayLength = 10; + //鍒濆 鏁扮粍闀垮害 + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size < elementData.length) + { + elementData[size]=o; + }else{ + //鎵╁鏁扮粍 + grow(); + elementData[size] = 0; + } + size++; + + } + public void add(int index, Object o){ + if(index>size) + { + throw new RuntimeException("ArrayIndexOutOfBoundsException"); + } + + //鎴彇绱㈠紩寮濮嬪埌鍘熸暟缁勭粨灏 缁勬垚涓涓柊鐨勬暟缁 + Object [] tempObjs = Arrays.copyOfRange(elementData,index,elementData.length); + //瑕嗙洊鍘熸湁绱㈠紩浣嶇疆鐨勫璞 + elementData[index] = o; + //鏁扮粍鎵╁ + elementData = Arrays.copyOf(elementData,elementData.length+1); + + //灏嗕复鏃剁敓鎴愮殑鏁扮粍鍚堝苟鍥炲師鏁扮粍 + System.arraycopy(tempObjs,0,elementData,index+1,tempObjs.length); + size++; + } + + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + + if(index>size) + { + throw new RuntimeException("ArrayIndexOutOfBoundsException"); + } + + Object o = elementData[index]; + + //鎴彇绱㈠紩寮濮嬪埌鍘熸暟缁勭粨灏 缁勬垚涓涓柊鐨勬暟缁 + Object [] tempObjs = Arrays.copyOfRange(elementData,index+1,elementData.length); + elementData = Arrays.copyOf(elementData,elementData.length-1); + //灏嗕复鏃剁敓鎴愮殑鏁扮粍鍚堝苟鍥炲師鏁扮粍 + System.arraycopy(tempObjs,0,elementData,index,tempObjs.length); + size--; + + return o; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + ArratListIterator arratListIterator = new ArratListIterator(this); + + return arratListIterator; + } + + private void grow(){ + elementData = Arrays.copyOf(elementData,elementData.length+addArrayLength); + } + + class ArratListIterator implements Iterator{ + + ArrayList arrayList = new ArrayList(); + + int index = 0; + + ArratListIterator(ArrayList arrayList){ + + this.arrayList = arrayList; + index = arrayList.size; + } + + @Override + public boolean hasNext() { + if(index == 0) + { + return false; + } + return true; + } + + @Override + public Object next() { + return this.arrayList.get(--index); + } + } + +} diff --git a/group18/744888802/dataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group18/744888802/dataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..8fc6e03297 --- /dev/null +++ b/group18/744888802/dataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,84 @@ +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) { + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.data = o; + + add(this, binaryTreeNode); + return this; + } + + private void add(BinaryTreeNode binaryTreeNodeOld, BinaryTreeNode binaryTreeNodeNew) { + if (binaryTreeNodeOld.data == null) { + binaryTreeNodeOld.data = binaryTreeNodeNew.data; + return; + } + + + if (binaryTreeNodeOld.left == null) { + binaryTreeNodeOld.left = binaryTreeNodeNew; + return; + } + if (binaryTreeNodeOld.right == null) { + if (comparator(binaryTreeNodeNew, binaryTreeNodeOld.left)){ + binaryTreeNodeOld.right = binaryTreeNodeNew; + }else{ + binaryTreeNodeOld.right = binaryTreeNodeOld.left; + binaryTreeNodeOld.left = binaryTreeNodeNew; + } + return; + } + + if(comparator(binaryTreeNodeOld.left, binaryTreeNodeNew)) + { + add(binaryTreeNodeOld.left,binaryTreeNodeNew); + return; + } + + if(comparator(binaryTreeNodeOld.right, binaryTreeNodeNew)){ + add(binaryTreeNodeOld.right,binaryTreeNodeNew); + return; + }else{ + binaryTreeNodeNew.left = binaryTreeNodeOld.right; + binaryTreeNodeOld.right = binaryTreeNodeNew; + } + + + + + } + + private boolean comparator(BinaryTreeNode binaryTreeNode1, BinaryTreeNode binaryTreeNode2) { + if ((Integer) binaryTreeNode1.getData() > (Integer) binaryTreeNode2.getData()) { + return true; + } + return false; + } + +} diff --git a/group18/744888802/dataStructure/src/main/java/com/coding/basic/Iterator.java b/group18/744888802/dataStructure/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group18/744888802/dataStructure/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group18/744888802/dataStructure/src/main/java/com/coding/basic/LinkedList.java b/group18/744888802/dataStructure/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..098246a4bb --- /dev/null +++ b/group18/744888802/dataStructure/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,237 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + + private int size = 0; + + public void add(Object o){ + addLast(o); + + } + public void add(int index , Object o){ + + Node node = new Node(); + node.data = o; + if(size == 0) + { + throw new NullPointerException(" linked list is null"); + } + if(index == 0) + { + node.next=head; + head = node; + } + Node nodeNow = head; + for(int i=1;i=size) + { + throw new IndexOutOfBoundsException(" this index too big by this list"); + } + + Node nodeNow = head; + for(int i=0;i=size) + { + throw new IndexOutOfBoundsException(" this index too big by this list"); + } + if(size == 0) + { + throw new NullPointerException("linked list is null"); + } + if(index == 0) + { + if(size == 1) + { + size = 0; + return head.data; + } + Object o = head.data; + head.next = null; + + head = head.next; + return o; + + } + Node result = null; + + + Node beforeNode = head; + Node nextNode = head.next; + for(int i=1;i elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length*2); + } + elementData[size-1] = o; + + } + public void add(int index, Object o){ + Object[] tmp = new Object[elementData.length]; + + if(index<0 || index >elementData.length-1){ + return; + } + if(++size > elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length*2); + tmp = new Object[elementData.length]; + } + System.arraycopy(elementData, 0, tmp, 0, index); + System.arraycopy(elementData, index, tmp, index+1, size-index); + tmp[index] = o; + elementData=tmp; + + } + + public Object get(int index){ + if(index<0 || index >elementData.length-1){ + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + Object o=null; + o = elementData[index]; + if(--size%5 == 0){ + elementData = Arrays.copyOf(elementData, elementData.length/2); + }else if(index == size-1){ + elementData[index] = null; + }else if(index == size-1){ + + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayIterator(this); + } + +} + + + + + + + + + + + + + + + + + + + diff --git a/group18/784140710/week01/src/com/coding/basic/BinaryTree.java b/group18/784140710/week01/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..01b7a90a43 --- /dev/null +++ b/group18/784140710/week01/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,65 @@ +package com.coding.basic; + + +public class BinaryTree { + + private BinaryTreeNode root; + + public void insert(Integer o){ + BinaryTreeNode node = new BinaryTreeNode(o); + if(root == null){ + root = node; + }else{ + BinaryTreeNode current = root; + BinaryTreeNode parent; + + while(true){ + parent = current; + if(onode.getData()){ + node = node.getRight(); + }else{ + return node; + } + } + + return null; + } + + +} + + + + + + + + + + + + diff --git a/group18/784140710/week01/src/com/coding/basic/BinaryTreeNode.java b/group18/784140710/week01/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..15cbb94d28 --- /dev/null +++ b/group18/784140710/week01/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,66 @@ +package com.coding.basic; + + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data){ + this.data = data; + } + + public Integer getData() { + return data; + } + public void setData(Integer data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group18/784140710/week01/src/com/coding/basic/Iterator.java b/group18/784140710/week01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group18/784140710/week01/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group18/784140710/week01/src/com/coding/basic/LinkedList.java b/group18/784140710/week01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..eb0b6fbeae --- /dev/null +++ b/group18/784140710/week01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,114 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + ++size; + Node node = new Node(o); + + if(head == null){ + head = node; + return; + } + Node tmp = new Node(o); + tmp = head; + while(tmp.next!=null){ + tmp = tmp.next; + } + tmp.next = node; + } + public void add(int index , Object o){ + ++size; + Node node = new Node(o); + Node tmp = new Node(o); + tmp = head; + int i =0; + while(tmp.next!=null && i + + + + + + + diff --git a/group18/935542673/Coding/.gitignore b/group18/935542673/Coding/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/935542673/Coding/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/935542673/Coding/.project b/group18/935542673/Coding/.project new file mode 100644 index 0000000000..8b11575db2 --- /dev/null +++ b/group18/935542673/Coding/.project @@ -0,0 +1,17 @@ + + + Coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java new file mode 100644 index 0000000000..f8bb390595 --- /dev/null +++ b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java @@ -0,0 +1,92 @@ +package com.ikook.basic_data_structure; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +/** + * 姝ゅ崟鍏冩祴璇曞彧娴嬭瘯浜嗘甯告儏鍐碉紝涓浜涘紓甯告儏鍐垫病鏈夋祴璇曘 + * @author ikook + */ +public class MyArrayListTest { + + private MyArrayList list; + + @Before + public void setUp() { + list = new MyArrayList(); + list.add("111"); + list.add("222"); + list.add(33); + list.add("444"); + list.add(new Date()); + list.add("666"); + list.add("777"); + list.add("888"); + list.add("999"); + } + + @Test + public void testAdd() { + //娴嬭瘯add(Object obj)鏂规硶 + list.add(100); + assertEquals(10, list.size()); + + //娴嬭瘯add(int index, Object obj)鏂规硶 + list.add(3, 444); + assertEquals(444, list.get(3)); + + assertEquals("444", list.get(4)); + assertEquals(11, list.size()); + } + + @Test + public void testIsEmpty() { + + assertEquals(false, list.isEmpty()); + } + + @Test + public void testGet() { + assertEquals("111", list.get(0)); + assertEquals(new Date(), list.get(4)); + } + + @Test + public void testRemove() { + + // 娴嬭瘯remove(int index)鏂规硶 + assertEquals(33, list.remove(2)); + assertEquals("444", list.get(2)); + + // 娴嬭瘯remove(Object obj)鏂规硶 + assertEquals(true, list.remove("222")); + assertEquals("444", list.get(1)); + } + + @Test + public void testSet() { + assertEquals(33, list.set(2, "333")); + assertEquals("333", list.get(2)); + } + + @Test + public void testIterator() { + int i = 0; + for(MyIterator iter = list.iterator(); iter.hasNext();) { + Object str = (Object) iter.next(); + assertEquals(list.get(i++), str); + } + + int j = list.size(); + for(MyIterator iter = list.iterator(); iter.hasNext();) { + iter.next(); + iter.remove(); + assertEquals( --j , list.size()); + } + } + +} diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java new file mode 100644 index 0000000000..59e1ad3797 --- /dev/null +++ b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java @@ -0,0 +1,64 @@ +package com.ikook.basic_data_structure; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +/** + * 姝ゅ崟鍏冩祴璇曞彧娴嬭瘯浜嗘甯告儏鍐碉紝涓浜涘紓甯告儏鍐垫病鏈夋祴璇曘 + * @author ikook + */ +public class MyBinarySearchTreeTest { + + private MyBinarySearchTree tree; + + @Before + public void setUp() { + tree = new MyBinarySearchTree(); + + tree.insert(3); + tree.insert(8); + } + + @SuppressWarnings("static-access") + @Test + public void testInsert() { + tree.insert(1); + tree.insert(4); + tree.insert(6); + tree.insert(2); + tree.insert(10); + tree.insert(9); + + assertEquals("1 2 3 4 6 8 9 10 ", tree.inorderTraverse(tree.root)); + } + + @Test + public void testFind() { + tree.insert(1); + tree.insert(4); + tree.insert(6); + tree.insert(2); + tree.insert(10); + tree.insert(9); + + assertEquals(false, tree.find(5)); + assertEquals(true, tree.find(10)); + } + + @SuppressWarnings("static-access") + @Test + public void testDelete() { + tree.insert(1); + tree.insert(4); + tree.insert(6); + tree.insert(2); + tree.insert(10); + tree.insert(9); + + assertEquals(false, tree.delete(5)); + assertEquals(true, tree.delete(4)); + assertEquals("1 2 3 6 8 9 10 ", tree.inorderTraverse(tree.root)); + } +} diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java new file mode 100644 index 0000000000..d479408183 --- /dev/null +++ b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java @@ -0,0 +1,131 @@ +package com.ikook.basic_data_structure; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +/** + * 姝ゅ崟鍏冩祴璇曞彧娴嬭瘯浜嗘甯告儏鍐碉紝涓浜涘紓甯告儏鍐垫病鏈夋祴璇曘 + * @author ikook + */ +public class MyLinkedListTest { + + private MyLinkedList list; + + @Before + public void setUp() { + list = new MyLinkedList(); + list.add("111"); + list.add(222); + list.add("333"); + } + + @Test + public void testAddFirst() { + list.addFirst(444); + assertEquals(4, list.size()); + assertEquals(444, list.get(0)); + assertEquals(444, list.getFirst()); + + } + + @Test + public void testAddLast() { + list.addLast("444"); + assertEquals(4, list.size()); + assertEquals("444", list.getLast()); + assertEquals("444", list.get(3)); + } + + @Test + public void testAddObject() { + list.add(new Date()); + assertEquals(new Date(), list.get(3)); + } + + @Test + public void testAddIntObject() { + list.add(1, "222"); + assertEquals("222", list.get(1)); + assertEquals(4, list.size()); + } + + @Test + public void testSize() { + assertEquals(3, list.size()); + } + + @Test + public void testIsEmpty() { + assertEquals(false, list.isEmpty()); + + MyLinkedList list = new MyLinkedList(); + assertEquals(true, list.isEmpty()); + } + + @Test + public void testGetFirst() { + assertEquals("111", list.getFirst()); + } + + @Test + public void testGetLast() { + assertEquals("333", list.getLast()); + } + + @Test + public void testGet() { + assertEquals(222, list.get(1)); + } + + @Test + public void testSet() { + assertEquals(222, list.set(1, new Date())); + assertEquals(new Date(), list.get(1)); + } + + @Test + public void testRemoveFirst() { + assertEquals("111", list.removeFirst()); + assertEquals(222, list.getFirst()); + } + + @Test + public void testRemoveLast() { + assertEquals("333", list.removeLast()); + assertEquals(222, list.getLast()); + } + + @Test + public void testRemoveObject() { + assertEquals(true, list.remove((Integer) 222)); + assertEquals("333", list.get(1)); + } + + @Test + public void testRemoveInt() { + assertEquals(222, list.remove(1)); + assertEquals("333", list.get(1)); + + } + + @Test + public void testIterator() { + int i = 0; + for(MyIterator iter = list.iterator(); iter.hasNext();) { + Object str = (Object) iter.next(); + assertEquals(list.get(i++), str); + } + + int j = list.size(); + for(MyIterator iter = list.iterator(); iter.hasNext();) { + iter.next(); + iter.remove(); + assertEquals( --j , list.size()); + } + } + +} diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java new file mode 100644 index 0000000000..8fb8ba825f --- /dev/null +++ b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java @@ -0,0 +1,54 @@ +package com.ikook.basic_data_structure; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +/** + * 姝ゅ崟鍏冩祴璇曞彧娴嬭瘯浜嗘甯告儏鍐碉紝涓浜涘紓甯告儏鍐垫病鏈夋祴璇曘 + * @author ikook + */ +public class MyQueueTest { + + private MyQueue queue; + + @Before + public void setUp() { + queue = new MyQueue(); + + queue.enQueue(111); + queue.enQueue("222"); + queue.enQueue(new Date()); + } + + @Test + public void testEnQueue() { + queue.enQueue(444); + assertEquals(4, queue.size()); + } + + @Test + public void testDeQueue() { + assertEquals(111, queue.deQueue()); + } + + @Test + public void testSize() { + assertEquals(3, queue.size()); + + MyQueue queue = new MyQueue(); + assertEquals(0, queue.size()); + } + + @Test + public void testIsEmpty() { + assertEquals(false, queue.isEmpty()); + + MyQueue queue = new MyQueue(); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java new file mode 100644 index 0000000000..fe1084e343 --- /dev/null +++ b/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java @@ -0,0 +1,55 @@ +package com.ikook.basic_data_structure; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +/** + * 姝ゅ崟鍏冩祴璇曞彧娴嬭瘯浜嗘甯告儏鍐碉紝涓浜涘紓甯告儏鍐垫病鏈夋祴璇曘 + * @author ikook + */ +public class MyStackTest { + + private MyStack stack; + + @Before + public void setUp() { + stack = new MyStack(); + stack.push(111); + stack.push("222"); + stack.push(333); + stack.push(new Date()); + stack.push("555"); + } + + @Test + public void testPush() { + stack.push(93554); + assertEquals(6, stack.size()); + } + + @Test + public void testPop() { + assertEquals("555", stack.pop()); + assertEquals(4, stack.size()); + + assertEquals(new Date(), stack.pop()); + } + + @Test + public void testGetTop() { + assertEquals("555", stack.getTop()); + } + + @Test + public void testIsEmpty() { + assertEquals(false, stack.isEmpty()); + + MyStack stack = new MyStack(); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java new file mode 100644 index 0000000000..6891a76879 --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java @@ -0,0 +1,215 @@ +package com.ikook.basic_data_structure; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +/** + * @author ikook; QQ鍙风爜: 935542673 + */ +public class MyArrayList implements MyList{ + + private Object[] elementData; + + private int size; + + /** + * 浣縊bject[]鐨勯暱搴﹂粯璁や负10锛 + */ + public MyArrayList() { + this(10); + } + + /** + * 鍦ㄦ瀯閫犲嚱鏁颁腑鍒濆鍖栭泦鍚堢殑闀垮害 + * @param initialCapacity + */ + public MyArrayList(int initialCapacity) { + if(initialCapacity < 0) { + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + } + this.elementData = new Object[initialCapacity]; + } + + /** + * 鍦ㄩ泦鍚堜腑娣诲姞鍏冪礌 + * @param obj + */ + public void add(Object obj) { + + ensureCapacity(); + elementData[size++] = obj; + + } + + /** + * 娣诲姞鍏冪礌鍒伴泦鍚堢殑鎸囧畾浣嶇疆 + * @param index + * @param obj + */ + public void add(int index, Object obj) { + rangeCheck(index); + ensureCapacity(); + + System.arraycopy(elementData, index, elementData, index + 1, size-index); + elementData[index] = obj; + size++; + } + + /** + * 杩斿洖闆嗗悎鐨勯暱搴 + * @return + */ + public int size() { + return size; + } + + /** + * 鍒ゆ柇闆嗗悎鏄潪涓虹┖ + * @return + */ + public boolean isEmpty() { + return size == 0; + } + + /** + * 鑾峰彇闆嗗悎鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @return + */ + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + /** + * 鍒犻櫎鎸囧畾浣嶇疆鐨勫璞 + * @param index + */ + public Object remove(int index) { + + rangeCheck(index); + + Object oldValue = elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0){ + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + } + elementData[--size] = null; + + return oldValue; + } + + /** + * 鍒犻櫎鎸囧畾鐨勫璞(Object 瀵硅薄) + * @param obj + */ + public boolean remove(Object obj){ + for(int i = 0; i < size; i++) { + if(get(i).equals(obj)) { + remove(i); + return true; + } + } + return false; + } + + /** + * 鏇存敼闆嗗悎涓寚瀹氫綅缃殑鍏冪礌锛屽苟杩斿洖鍘熸潵鐨勫璞 + * @param index + * @param obj + * @return + */ + public Object set(int index, Object obj) { + rangeCheck(index); + + Object oldValue = elementData[index]; + elementData[index] = obj; + + return oldValue; + } + + /** + * 闆嗗悎鎵╁灏佽绫 + */ + private void ensureCapacity() { + if(size == elementData.length) { + Object[] newArray = new Object[size * 2 + 1]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + + /** + * 鍒ゆ柇闆嗗悎鑼冨洿鏄惁瓒婄晫鐨勫皝瑁呯被 + * @param index + */ + private void rangeCheck(int index) { + if(index < 0 || index >= size) { + try { + throw new Exception("绱㈠紩寮傚父"); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 杩斿洖涓涓凯浠e櫒鐨勫疄鐜 + * @return + */ + public MyIterator iterator() { + return new Iter(); + } + + /** + * 杩唬鍣ㄧ殑瀹炵幇绫 + * @author ikook + */ + private class Iter implements MyIterator { + + int cursor; // 杩斿洖涓嬩竴涓厓绱犵殑绱㈠紩 + int lastRet = -1; // 杩斿洖鏈鍚庝竴涓厓绱犵殑绱㈠紩(濮嬬粓鎸囧悜鍒氶亶鍘嗗畬鐨勫厓绱)锛屽鏋滄病鏈夊厓绱犱簡锛屽垯涓 -1 + + @Override + public boolean hasNext() { + return cursor != size; // cursor 绛変簬 size 鍒欓泦鍚堥亶鍘嗗畬銆 + } + + @Override + public Object next() { + + try{ + int i = cursor; + Object next = get(i); + lastRet = i; + cursor = i + 1; + return next; + } catch (IndexOutOfBoundsException e) { + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱, 杩唬鍣ㄩ亶鍘嗗け璐"); + } + + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException("闈炴硶鐘舵佸紓甯革紝鍒犻櫎澶辫触"); + } + + try{ + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } catch (IndexOutOfBoundsException e) { + throw new ConcurrentModificationException("绔炰簤鑰呮敼鍙樺紓甯革紝鍒犻櫎澶辫触"); + } + } + + } +} \ No newline at end of file diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java new file mode 100644 index 0000000000..d30be18a7a --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java @@ -0,0 +1,200 @@ +package com.ikook.basic_data_structure; + +/** + * @author ikook; QQ鍙风爜: 935542673 + */ +public class MyBinarySearchTree { + + public static Node root; + + public MyBinarySearchTree() { + root = null; + } + + /** + * 鎻掑叆鎿嶄綔 + * @param id + */ + public void insert(int id) { + Node newNode = new Node(id); + if (root == null) { + root = newNode; + return; + } + Node current = root; //褰撳墠鑺傜偣 + Node parent = null; //鐖惰妭鐐癸紝鍗充笂涓涓妭鐐 + while (true) { + parent = current; + if (id < current.data) { + current = current.left; + if (current == null) { + parent.left = newNode; + return; + } + } else { + current = current.right; + if (current == null) { + parent.right = newNode; + return; + } + } + } + } + + /** + * 鏌ユ壘鎿嶄綔 + * @param id + * @return + */ + public boolean find(int id) { + Node current = root; + while (current != null) { + if (current.data == id) { + return true; + } else if (current.data > id) { + current = current.left; + } else { + current = current.right; + } + } + return false; + } + + /** + * 鍒犻櫎鎿嶄綔 + * @param id + * @return + */ + public boolean delete(int id) { + if (root == null) // 鏍硅妭鐐逛负绌猴紝鍒欐爲涓虹┖锛岃繑鍥瀎alse銆 + return false; + else { + Node parent = root; + Node current = root; + boolean isLeftChild = false; // 鏄惁鍦ㄥ乏瀛愭爲锛岄粯璁や负false锛氬嵆涓嶅湪銆 + // 鎵惧埌鍒犻櫎鐐逛互鍙婃槸鍚﹀湪宸﹀瓙鏍 + while (current.data != id) { + parent = current; + if (current.data > id) { + isLeftChild = true; + current = current.left; + } else { + isLeftChild = false; + current = current.right; + } + if (current == null) { + return false; + } + } + + // 濡傛灉鍒犻櫎鑺傜偣鐨勫乏鑺傜偣涓虹┖锛屽彸鑺傜偣涔熶负绌恒 + if (current.left == null && current.right == null) { + if (current == root) { + root = null; + } + if (isLeftChild == true) { + parent.left = null; + } else { + parent.right = null; + } + } + // 濡傛灉鍒犻櫎鑺傜偣鍙湁涓涓瓙鑺傜偣锛屽垯璇ヨ妭鐐逛负宸﹁妭鐐规垨鑰呭彸鑺傜偣銆 + else if (current.right == null) { + if (current == root) { + root = current.left; + } else if (isLeftChild) { + parent.left = current.left; + } else { + parent.right = current.left; + } + } else if (current.left == null) { + if (current == root) { + root = current.right; + } else if (isLeftChild) { + parent.left = current.right; + } else { + parent.right = current.right; + } + } + // 濡傛灉鍒犻櫎鑺傜偣宸﹁妭鐐瑰彸鑺傜偣閮戒笉涓虹┖銆 + else if (current.left != null && current.right != null) { + + // 瀵绘壘鍒犻櫎鑺傜偣鐨勫悗缁ц咃細杩欒鏄庡凡缁忓彂鐜版渶灏忓厓绱犲湪鍙冲瓙鏍戜腑 + Node successor = getSuccessor(current); + if (current == root) { + root = successor; + } else if (isLeftChild) { + parent.left = successor; + } else { + parent.right = successor; + } + successor.left = current.left; + } + return true; + } + } + + /** + * 鑾峰彇鍒犻櫎鑺傜偣鐨勫悗缁ц咃細鍒犻櫎鑺傜偣鐨勫悗缁ц呮槸鍦ㄥ叾鍙宠妭鐐规爲涓渶灏忕殑鑺傜偣 + * @param deleleNode + * @return + */ + private Node getSuccessor(Node deleleNode) { + Node successsor = null; + Node successsorParent = null; + Node current = deleleNode.right; + while (current != null) { + successsorParent = successsor; + successsor = current; + current = current.left; + } + // 妫鏌ュ悗缁ц呮槸鍚︽湁鍙宠妭鐐 + // 濡傛灉鏈夊彸鑺傜偣鏍戯紝鍒欏皢鍏舵坊鍔犲埌successorParent(鍚庣户鑰呯埗鑺傜偣)鐨勫乏鑺傜偣銆 + if (successsor != deleleNode.right) { + successsorParent.left = successsor.right; + successsor.right = deleleNode.right; + } + return successsor; + } + + /** + * 鏄剧ず浜屽弶鏍 + * @param root + * @param sb + */ + private void display(Node root, StringBuilder sb) { + if (root != null) { + display(root.left, sb); + sb.append(root.data + " "); + display(root.right, sb); + } + } + + /** + * 涓簭閬嶅巻锛氬乏瀛愭爲->鏍硅妭鐐->鍙冲瓙鏍 + * @param root + * @return + */ + public String inorderTraverse(Node root) { + StringBuilder sb = new StringBuilder(); + this.display(root, sb); + return sb.toString(); + } +} + +/** + * 鐢ㄤ簬琛ㄧず鑺傜偣 + * @author ikook + */ +class Node { + + int data; + Node left; + Node right; + + public Node(int data) { + this.data = data; + left = null; + right = null; + } +} \ No newline at end of file diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java new file mode 100644 index 0000000000..1fe7a69fcb --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java @@ -0,0 +1,14 @@ +package com.ikook.basic_data_structure; + +/** + * @author ikook QQ鍙风爜: 935542673 + */ +public interface MyIterator { + + public boolean hasNext(); // 鍒ゆ柇鏄惁鏈夊厓绱犳病鏈夎閬嶅巻 + + public Object next(); // 杩斿洖娓告爣褰撳墠浣嶇疆鐨勫厓绱犲苟灏嗘父鏍囩Щ鍔ㄥ埌涓嬩竴涓綅缃 + + public void remove(); // 鍒犻櫎娓告爣宸﹁竟鐨勫厓绱狅紝鍦ㄦ墽琛屽畬 next 涔嬪悗璇ユ搷浣滃彧鑳芥墽琛屼竴娆 + +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java new file mode 100644 index 0000000000..a255657911 --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java @@ -0,0 +1,359 @@ +package com.ikook.basic_data_structure; + +import java.util.NoSuchElementException; + +/** + * @author ikook; QQ鍙风爜: 935542673 + */ +public class MyLinkedList implements MyList{ + + private Node first; + private Node last; + private int size; + + /** + * 鍦ㄩ摼琛ㄧ殑澶撮儴鎻掑叆鏂扮殑鍏冪礌 + * @param obj + */ + public void addFirst(Object obj) { + final Node f = first; + final Node newNode = new Node(null, obj, f); + first = newNode; + if (f == null) + last = newNode; + else + f.previous = newNode; + size++; + } + + /** + * 鍦ㄩ摼琛ㄥ熬閮ㄦ彃鍏ユ柊鐨勫厓绱 + * @param obj + */ + public void addLast(Object obj) { + final Node l = last; + final Node newNode = new Node(l, obj, null); + last = newNode; + if (l == null) + first = newNode; + else + l.next = newNode; + size++; + } + + /** + * 鍦ㄩ摼琛ㄤ腑鎻掑叆鏂扮殑鍏冪礌 + * @param obj + */ + public void add(Object obj) { + addLast(obj); + } + + /** + * 鍦ㄦ寚瀹氫綅缃彃鍏ユ柊鐨勫厓绱 + * @param index + * @param obj + */ + public void add(int index, Object obj) { + if (!(index >= 0 && index <= size)) { + throw new IndexOutOfBoundsException("绱㈠紩浣嶇疆瓒婄晫"); + } + + if (index == size) { + addLast(obj); + } else { + Node temp = node(index); + final Node pred = temp.previous; + final Node newNode = new Node(pred, obj, temp); + temp.previous = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + } + + /** + * 杩斿洖闆嗗悎鐨剆ize銆 + * @return + */ + public int size() { + return size; + } + + /** + * 鍒ゆ柇闆嗗悎鏄潪涓虹┖ + * @return + */ + public boolean isEmpty() { + return size == 0; + } + + /** + * 鑾峰彇閾捐〃澶撮儴鐨勫厓绱 + * @return + */ + public Object getFirst() { + final Node f = first; + if (f == null) { + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱"); + } + + return f.data; + } + + /** + * 鑾峰彇閾捐〃灏鹃儴鐨勫厓绱 + * @return + */ + public Object getLast() { + final Node l = last; + if (l == null) { + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱"); + } + + return l.data; + } + + /** + * 鑾峰彇鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @return + */ + public Object get(int index) { + rangeCheckElementIndex(index); + return node(index).data; + } + + /** + * 鏇存敼鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @param element + * @return + */ + public Object set(int index, Object element) { + rangeCheckElementIndex(index); + Node temp = node(index); + Object oldValue = temp.data; + temp.data = element; + return oldValue; + } + + + /** + * 鍒犻櫎閾捐〃澶撮儴鐨勫厓绱 + * @return + */ + public Object removeFirst() { + final Node f = first; + if (f == null) { + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱"); + } + + final Object element = f.data; + final Node next = f.next; + f.data = null; + f.next = null; + first = next; + if (next == null) { + last = null; + } else { + next.previous = null; + } + size--; + + return element; + } + + /** + * 鍒犻櫎閾捐〃灏鹃儴鐨勫厓绱 + * @return + */ + public Object removeLast() { + final Node l = last; + if (l == null){ + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱"); + } + + final Object element = l.data; + final Node prev = l.previous; + l.data = null; + l.previous = null; + last = prev; + if (prev == null) { + first = null; + } else { + prev.next = null; + } + size--; + + return element; + } + + /** + * 鍒犻櫎鎸囧畾鍏冪礌 + * @param o + * @return + */ + public boolean remove(Object o) { + if (o == null) { + for (Node temp = first; temp != null; temp = temp.next) { + if (temp.data == null) { + deleteElement(temp); + return true; + } + } + } else { + for (Node temp = first; temp != null; temp = temp.next) { + if (o.equals(temp.data)) { + deleteElement(temp); + return true; + } + } + } + return false; + } + + /** + * 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @return + */ + public Object remove(int index) { + rangeCheckElementIndex(index); + return deleteElement(node(index)); + } + + /** + * 鍒犻櫎鎸囧畾鑺傜偣鍏冪礌 + * @param temp + * @return + */ + private Object deleteElement(Node temp) { + final Object element = temp.data; + final Node next = temp.next; + final Node prev = temp.previous; + + if (prev == null) { + first = next; + } else { + prev.next = next; + temp.previous = null; + } + + if (next == null) { + last = prev; + } else { + next.previous = prev; + temp.next = null; + } + + temp.data = null; + size--; + + return element; + } + + /** + * 妫鏌ョ储寮曞厓绱犵殑鑼冨洿 + * @param index + */ + private void rangeCheckElementIndex(int index) { + if (!(index >= 0 && index < size)) { + throw new IndexOutOfBoundsException("绱㈠紩瓒婄晫"); + } + } + + /** + * 杩斿洖鎸囧畾绱㈠紩浣嶇疆鐨勮妭鐐 + * @param index + * @return + */ + Node node(int index) { + if (index < (size >> 1)) { + Node temp = first; + for (int i = 0; i < index; i++) + temp = temp.next; + return temp; + } else { + Node temp = last; + for (int i = size - 1; i > index; i--) + temp = temp.previous; + return temp; + } + } + + /** + * 鐢ㄤ簬琛ㄧず涓涓妭鐐 + * @author ikook + */ + private static class Node { + Node previous; // 涓婁竴涓妭鐐 + Object data; + Node next; // 涓嬩竴涓妭鐐 + + public Node(Node previous, Object data, Node next) { + this.previous = previous; + this.data = data; + this.next = next; + } + } + + /** + * 杩斿洖涓涓凯浠e櫒鐨勫疄鐜扮被 + * @return + */ + public MyIterator iterator() { + return new LinkIter(); + } + + /** + * 杩唬鍣ㄧ殑瀹炵幇绫 + * @author ikook + */ + private class LinkIter implements MyIterator { + private Node lastRet; //濮嬬粓鎸囧悜鍒氶亶鍘嗗畬鐨勮妭鐐 + private Node next; // 褰撳墠鎸囧悜鐨勮妭鐐 + private int nextIndex; //褰撳墠鑺傜偣鐨勭储寮曞 + + LinkIter () { + next = node(0); + nextIndex = 0; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + if(!hasNext()) { + throw new NoSuchElementException("娌℃湁鎵惧埌鎸囧畾鐨勫厓绱, 杩唬鍣ㄩ亶鍘嗗け璐"); + } + + lastRet = next; + next = next.next; + nextIndex++; + return lastRet.data; + } + + @Override + public void remove() { + if(lastRet == null) { + throw new IllegalStateException("闈炴硶鐘舵佸紓甯革紝鍒犻櫎澶辫触"); + } + + Node lastNext = lastRet.next; + deleteElement(lastRet); + if(next == lastRet) { + next = lastNext; + } else { + nextIndex--; + } + lastRet = null; + } + + } +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java new file mode 100644 index 0000000000..f1fa613d0e --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java @@ -0,0 +1,22 @@ +package com.ikook.basic_data_structure; + +/** + * @author ikook QQ鍙风爜: 935542673 + */ +public interface MyList { + + public void add(Object o); + public void add(int index, Object o); + + public int size(); + + public boolean isEmpty(); + + public Object get(int index); + + public Object remove(int index); + public boolean remove(Object obj); + + public Object set(int index, Object obj); + +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java new file mode 100644 index 0000000000..824d7579c6 --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java @@ -0,0 +1,42 @@ +package com.ikook.basic_data_structure; + +/** + * @author ikook; QQ鍙风爜: 935542673 + */ +public class MyQueue { + + private MyLinkedList queue = new MyLinkedList(); + + /** + * 鍏ラ槦鎿嶄綔 + * @param obj + */ + public void enQueue(Object obj) { + queue.addLast(obj); + } + + /** + * 鍑洪槦鎿嶄綔 + * @return + */ + public Object deQueue() { + return queue.removeFirst(); + } + + /** + * 闃熷垪鐨勯暱搴 + * @return + */ + public int size() { + return queue.size(); + } + + /** + * 闃熷垪鏄惁涓虹┖ + * @return + */ + public boolean isEmpty() { + return queue.isEmpty(); + } + +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java new file mode 100644 index 0000000000..0ad6c05910 --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java @@ -0,0 +1,73 @@ +package com.ikook.basic_data_structure; + +/** + * @author ikook; QQ鍙风爜: 935542673 + */ +public class MyStack { + + private MyArrayList elementDate = new MyArrayList(); + + /** + * 鍏ユ爤鎿嶄綔 + * @param obj + */ + public void push(Object obj) { + elementDate.add(obj); + } + + /** + * 鍑烘爤鎿嶄綔 + * @return + */ + public Object pop() { + emptyExce(); + return elementDate.remove(topIndex()); + } + + /** + * 鑾峰彇鏍堥《鍏冪礌 + * @return + */ + public Object getTop() { + emptyExce(); + return elementDate.get(topIndex()); + } + + /** + * 鍒ゆ柇鏍堟槸鍚︿负绌 + * @return + */ + public boolean isEmpty() { + return size() == 0; + } + + /** + * 鑾峰彇鏍堢殑娣卞害 + * @return + */ + public int size() { + return elementDate.size(); + } + + /** + * 鏍堥《鍏冪礌鎵鍦ㄧ储寮曞皝瑁呯被 + * @return + */ + private int topIndex() { + return size() - 1; + } + + /** + * 闃熷垪涓虹┖寮傚父澶勭悊灏佽绫 + */ + private void emptyExce() { + if (isEmpty()) { + try { + throw new Exception("闃熷垪涓虹┖"); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md b/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md new file mode 100644 index 0000000000..a565fb7148 --- /dev/null +++ b/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md @@ -0,0 +1,83 @@ +## 2017缂栫▼鎻愰珮绀剧兢浣滀笟锛氬疄鐜板熀鏈殑鏁版嵁缁撴瀯(2017.2.19) + +1. 瀹炵幇浜咥rrayList銆丩inkedList銆丵ueue(渚濋潬LinkedList瀹炵幇)銆丼tack(渚濋潬ArrayList瀹炵幇) + + 1.1 ArrayList瀹炵幇浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塧dd(Object): 娣诲姞鍏冪礌鍒伴泦鍚堬紱 + 锛2锛塧dd(int, Object): 娣诲姞鍏冪礌鍒伴泦鍚堢殑鎸囧畾浣嶇疆锛 + 锛3锛塻ize(): 杩斿洖闆嗗悎鐨勫ぇ灏忥紝绫诲瀷涓 int锛 + 锛4锛塱sEmpty(): 鍒ゆ柇闆嗗悎鏄惁涓虹┖锛岀被鍨嬩负 boolean锛 + 锛5锛塯et(int): 鑾峰彇闆嗗悎鎸囧畾浣嶇疆鐨勫厓绱狅紱 + 锛6锛塺emove(int): 鍒犻櫎鎸囧畾浣嶇疆鐨勫璞★紱 + 锛7锛塺emove(Object): 鍒犻櫎鎸囧畾鐨勫璞★紱 + 锛8锛塻et(int, Object): 鏇存敼闆嗗悎涓寚瀹氫綅缃殑鍏冪礌锛屽苟杩斿洖鍘熸潵鐨勫璞★紱 + 锛9锛塱terator(): 杩斿洖涓涓凯浠e櫒鐨勫疄鐜扮被銆 + ``` + + 1.2 LinkedList瀹炵幇浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塧ddFirst(Object): 鍦ㄩ摼琛ㄥご閮ㄦ彃鍏ユ柊鐨勫厓绱狅紱 + 锛2锛塧ddLast(Object): 鍦ㄩ摼琛ㄥ熬閮ㄦ彃鍏ユ柊鐨勫厓绱狅紱 + 锛3锛塧dd(Object): 鍦ㄩ摼琛ㄤ腑鎻掑叆鏂扮殑鍏冪礌锛 + 锛4锛塧dd(int, Object): 鍦ㄩ摼琛ㄦ寚瀹氫綅缃彃鍏ユ柊鐨勫厓绱狅紱 + 锛5锛塻ize(): 杩斿洖閾捐〃鐨勫ぇ灏忥紝绫诲瀷涓 int锛 + 锛6锛塱sEmpty(): 鍒ゆ柇閾捐〃鏄惁涓虹┖锛岀被鍨嬩负 boolean锛 + 锛7锛塯etFirst(): 鑾峰彇閾捐〃澶撮儴鐨勫厓绱狅紱 + 锛8锛塯etLast(): 鑾峰彇閾捐〃灏鹃儴鐨勫厓绱狅紱 + 锛9锛塯et(int): 鑾峰彇閾捐〃鎸囧畾浣嶇疆鐨勫厓绱狅紱 + 锛10锛塻et(int, Object): 鏇存敼閾捐〃涓寚瀹氫綅缃殑鍏冪礌锛屽苟杩斿洖鍘熸潵鐨勫璞° + 锛11锛塺emoveFirst(): 鍒犻櫎閾捐〃澶撮儴鐨勫厓绱狅紱 + 锛12锛塺emoveLast(int): 鍒犻櫎閾捐〃灏鹃儴鐨勫厓绱狅紱 + 锛13锛塺emove(Object): 鍒犻櫎鎸囧畾鍏冪礌锛 + 锛14锛塺emove(int): 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱狅紱 + 锛15锛塱terator(): 杩斿洖涓涓凯浠e櫒鐨勫疄鐜扮被銆 + ``` + + 1.3 Queue瀹炵幇浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塭nQueue(Object): 鍏ラ槦鎿嶄綔锛 + 锛2锛塪eQueue(): 鍑洪槦鎿嶄綔锛 + 锛3锛塻ize(): 杩斿洖闃熷垪鐨勯暱搴︼紱 + 锛4锛塱sEmpty(): 鍒ゆ柇闃熷垪鏄惁涓虹┖銆 + ``` + + 1.4 Stack瀹炵幇浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塸ush(Object)锛氬叆鏍堟搷浣滐紱 + 锛2锛塸op()锛氬嚭鏍堟搷浣滐紱 + 锛3锛塯etTop()锛氳幏鍙栨爤椤跺厓绱狅紱 + 锛4锛塱sEmpty()锛氬垽鏂爤鏄惁涓虹┖锛 + 锛5锛塻ize()锛氳幏鍙栨爤鐨勬繁搴︺ + ``` + 鈥 + +2. 瀹炵幇浜咮inarySearchTree銆両terator鎺ュ彛 + + 2.1 BinarySearchTree瀹炵幇浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塱nsert(int)锛氭彃鍏ユ搷浣滐紱 + 锛2锛塮ind(int)锛氭煡鎵炬搷浣滐紱 + 锛3锛塪elete(int)锛氬垹闄ゆ搷浣滐紱 + 锛4锛塱norderTraverse(Node)锛氶亶鍘嗘搷浣滐紝閲囩敤涓簭閬嶅巻銆 + ``` + + 2.2 Iterator瀹氫箟浜嗕竴涓嬫柟娉曪細 + + ``` + 锛1锛塰asNext()锛氬垽鏂槸鍚︽湁鍏冪礌娌℃湁琚亶鍘嗭紱 + 锛2锛塶ext()锛氳繑鍥炴父鏍囧綋鍓嶄綅缃殑鍏冪礌骞跺皢娓告爣绉诲姩鍒颁笅涓涓綅缃紱 + 锛3锛塺emove()锛氬垹闄ゆ父鏍囧乏杈圭殑鍏冪礌锛屽湪鎵ц瀹 next 涔嬪悗璇ユ搷浣滃彧鑳芥墽琛屼竴娆° + ``` + 鈥 + +3. 瀵瑰簲浠ヤ笂绫诲仛浜嗗崟鍏冩祴璇 + + 璇存槑锛氱敱浜庝綔涓氫互瀹炵幇鍩烘湰鐨勬暟鎹粨鏋勪负涓伙紝鍒欏湪瀹炵幇鍗曞厓娴嬭瘯鏃讹紝鍙姝e父鎯呭喌杩涜浜嗘祴璇曪紝涓浜涘紓甯告儏鍐靛苟杩涜缂栧啓娴嬭瘯鐢ㄤ緥銆 + + 鐐瑰嚮鏌ョ湅: [unit聽test](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/junit/com/ikook/basic_data_structure) \ No newline at end of file diff --git "a/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" "b/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" new file mode 100644 index 0000000000..d169fd91bf Binary files /dev/null and "b/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" differ diff --git a/group18/group18.md b/group18/group18.md index 8b13789179..d3f5a12faa 100644 --- a/group18/group18.md +++ b/group18/group18.md @@ -1 +1 @@ - + diff --git a/group19/group19.md b/group19/group19.md index 8b13789179..d3f5a12faa 100644 --- a/group19/group19.md +++ b/group19/group19.md @@ -1 +1 @@ - + diff --git a/group20/.gitignore b/group20/.gitignore new file mode 100644 index 0000000000..389e717ef0 --- /dev/null +++ b/group20/.gitignore @@ -0,0 +1,43 @@ +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +target/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Intellij +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/dictionaries +.idea/libraries +.idea + +# Project generated files +.project +.settings +.classpath +.com +.metadata + +# Mac file +.DS_Store + +# Do not ignore .jar file +!*.jar diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java new file mode 100644 index 0000000000..7cdf1e7f26 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java @@ -0,0 +1,75 @@ +package SinglyLinkedList; + +/** + * Created by Honoka on 2/16/2017. + */ +public class LinkedList0 { + //Node class represents a list node + private class Node + { + String value; + Node next; + /** + * Constructor + * @param val The element to store in this node. + * @param n The reference to the next node. + */ + Node (String val, Node n) + { + value = val; + next = n; + } + + /** + * Constructor + * @param val The element to store in this node. + */ + Node(String val) + { + value = val; + next = null; + } + } + //Reference to the first node in the list + private Node first = null; + /** + * Constructor + * Builds a linked list + */ + public LinkedList0() + { + //test + first = new Node("Apple"); + first.next = new Node("Peach"); + first.next.next = new Node("Kiwi"); + first = new Node("Blueberry",first); + + //Using an array to add elements into list + String[] fruits = {"Banana", "Cherry"}; + for (String f : fruits) + { + first = new Node(f, first); + } + } + /** + * print method + * traverses the list and prints all elements + */ + public void print() + { + Node reference = first; + while(reference != null) + { + System.out.println(reference.value + " "); + reference = reference.next; + } + } + + //Main test method + public static void main(String [] args) + { + LinkedList0 list = new LinkedList0(); + System.out.println("The elements inside this list are "); + list.print(); + } +} diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java new file mode 100644 index 0000000000..8c93bbc640 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java @@ -0,0 +1,242 @@ +package SinglyLinkedList2; +/** + * Created by Honoka on 2/16/2017. + */ +public class LinkedList1 { + private class Node + { + String value; + Node next; + + Node(String val, Node n) + { + value = val; + next = n; + } + Node(String val) + { + //Call the other(daddy(or sister(whatever))) constructor. + this(val, null); + } + } + + private Node first; // head + private Node last; //the last element in list + + public LinkedList1() + { + first = null; + last = null; + } + + /**This method checks to see + * if the list is empty + * @return true if list is empty + */ + public boolean isEmpty() + { + return first == null; + } + + /** + * size method returns the length of the list + * @return The number of the elements in the list + */ + public int size() + { + int counter = 0; + Node p = first; + while (p != null) + { + counter ++; + p = p.next; + } + return counter; + } + + /** + * add method add an element to the end of the list + * @param element the value to add + */ + public void add(String element) + { + if (isEmpty()) + { + //Obviously, add the element to the first position in the list + first = new Node(element); + last = first; + } + else + { + //add to the end of existing list + last.next = new Node(element); + last = last.next; + } + } + + /** + * add method, or you might call it insert method since it can + * add element to a specific position + * @param index The position at which to add the element + * @param element you should know what is this + */ + public void add (int index, String element) + { + if (index < 0 || index > size()) + { + String message = String.valueOf(index); + throw new IndexOutOfBoundsException(message); + } + + //index is at least 0 + if(index == 0) + { + //new element add to the head + first = new Node(element, first); + if (last == null) + { + last = first; + } + return; + } + //set a reference predecessor to point to the node that + //will be the predecessor of the new node + Node predecessor = first; + for (int k = 1; k <= index - 1; k++) + { + predecessor = predecessor.next; + } + //Splice in a node containing the new element + predecessor.next = new Node(element, predecessor.next); + + //if there is a new last element + if(predecessor.next.next == null) + last = predecessor.next; + } + + /** + * toString method, like print method, hopefully it will display the contents of the list + * @return say something I'm giving up on you( + */ + public String toString() + { + StringBuffer strBuilder = new StringBuffer(); + //Use p to walk down the list + Node p = first; + while (p != null) + { + strBuilder.append(p.value + "\n"); + p = p.next; + } + return strBuilder.toString(); + } + + /** + * remove method removes the element with the position you want + * @param index the position of the element that you want to remove + * @return the removed element + */ + public String remove (int index) + { + /* Index out of bounds */ + if (index < 0 || index >= size()) + { + String message = String.valueOf(index); + throw new IndexOutOfBoundsException(message); + } + String element = null; + if(index == 0) + { + //Removal of first item in the list + element = first.value; + first = first.next; + if (first == null) + { + last = null; + } + } + else + { + /* find the predecessor of the element to be removed */ + Node predecessor = first; + + /* Move predecessor forward index - 1 times */ + for (int k = 1; k <= index - 1; k++) + { + predecessor = predecessor.next; + /* Store the value to return */ + element = predecessor.next.value; + /* Route link around the node to be removed */ + predecessor.next = predecessor.next.next; + /* Check if predecessor is now last */ + if(predecessor.next == null) + { + last = predecessor; + } + } + } + return element; + } + + /** + * The remove method removes an element + * @param element the element to remove + * @return true if the remove succeeded + */ + public boolean remove(String element) + { + if (isEmpty()) + { + return false; + } + + if (element.equals(first.value)) + { + //Removal of first element in the list + first = first.next; + if(first == null) + { + last = null; + } + return true; + } + + /* Find the predecessor of the element to remove */ + Node predecessor = first; + while (predecessor.next != null && + !predecessor.next.value.equals(element)) + { + predecessor = predecessor.next; + } + /* predecessor.next == null OR predecessor.next.value is element */ + if(predecessor.next == null) + { + return false; + } + /* predecessor.next.value is element */ + predecessor.next = predecessor.next.next; + + /* check if predecessor is now last */ + if (predecessor.next == null) + { + last = predecessor; + } + return true; + } + + public static void main (String [] args) + { + LinkedList1 testList = new LinkedList1(); + testList.add("Apple"); + testList.add("Banana"); + testList.add(0,"Blueberry"); + testList.add(2,"Cherry"); + testList.add(4,"Peach"); + System.out.println("The list has : "); + System.out.println(testList); + testList.remove("Cherry"); + testList.remove(2); + System.out.println("The list has : "); + System.out.println(testList); + } +} diff --git a/group20/1040154728/1040154728Learning/src/honoka.md b/group20/1040154728/1040154728Learning/src/honoka.md new file mode 100644 index 0000000000..ff92a7f300 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/honoka.md @@ -0,0 +1,5 @@ +##Honoka's blog link is down below. + +https://honokabiu.github.io/ + +Super simple... diff --git a/group20/1107837739/1107837739Learning/lib/hamcrest-core-1.3.jar b/group20/1107837739/1107837739Learning/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000000..9d5fe16e3d Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/hamcrest-core-1.3.jar differ diff --git a/group20/1107837739/1107837739Learning/lib/junit-4.12.jar b/group20/1107837739/1107837739Learning/lib/junit-4.12.jar new file mode 100644 index 0000000000..3a7fc266c3 Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/junit-4.12.jar differ diff --git a/group20/1107837739/1107837739Learning/src/org/korben/Main.java b/group20/1107837739/1107837739Learning/src/org/korben/Main.java new file mode 100644 index 0000000000..671f67a9a7 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/Main.java @@ -0,0 +1,8 @@ +package org.korben; + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello Korben : )"); + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java new file mode 100644 index 0000000000..4d6236b537 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java @@ -0,0 +1,169 @@ +package org.korben.list; + +import java.util.Objects; + +/** + * Korben's ArrayList + * + * Created by Korben on 18/02/2017. + */ +public class KArrayList implements KList { + + private int size; + private Object[] dataArray = new Object[0]; + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + @Override + public boolean contains(Object o) { + for (Object obj : dataArray) { + if (Objects.equals(obj, o)) { + return true; + } + } + return false; + } + + @Override + @SuppressWarnings("unchecked") + public Object[] toArray() { + Object[] array = new Object[size]; + System.arraycopy(dataArray, 0, array, 0, size); + return array; + } + + @Override + public boolean add(T o) { + ensureCapacity(size + 1); + dataArray[size] = o; + size++; + return true; + } + + @Override + public boolean remove(T o) { + int index = indexOf(o); + if (index < 0) { + return false; + } + + System.arraycopy(dataArray, index + 1, dataArray, index, size - 1 - index); + dataArray[size - 1] = null; + + size--; + + return true; + } + + @Override + public void clear() { + for (int i = 0; i < size; i++) { + dataArray[i] = null; + } + size = 0; + } + + @Override + @SuppressWarnings("unchecked") + public T get(int index) { + if (index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + return (T) dataArray[index]; + } + + @Override + public T set(int index, T element) { + if (index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + dataArray[index] = element; + + return element; + } + + @Override + public void add(int index, T element) { + if (index < -1 || index > size) { + throw new IndexOutOfBoundsException(); + } + + ensureCapacity(size + 1); + + System.arraycopy(dataArray, index, dataArray, index + 1, size - index); + + dataArray[index] = element; + size++; + } + + @Override + @SuppressWarnings("unchecked") + public T remove(int index) { + if (index < -1 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + T removeData = (T) dataArray[index]; + System.arraycopy(dataArray, index + 1, dataArray, index, size - 1 - index); + dataArray[size - 1] = null; + size--; + return removeData; + } + + @Override + public int indexOf(T o) { + for (int i = 0; i < size; i++) { + if (Objects.equals(o, dataArray[i])) { + return i; + } + } + return -1; + } + + @Override + public KIterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > dataArray.length) { + int newCapacity = Math.max(minCapacity, dataArray.length * 2); + Object[] newDataArray = new Object[newCapacity]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + + dataArray = newDataArray; + } + } + + private class ArrayListIterator implements KIterator { + private int position; + private KArrayList list; + + ArrayListIterator(KArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position < list.size(); + } + + @Override + public T next() { + if (hasNext()) { + return list.get(position++); + } + return null; + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java new file mode 100644 index 0000000000..c29e566178 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java @@ -0,0 +1,10 @@ +package org.korben.list; + +/** + * Created by Korben on 24/02/2017. + */ +public interface KIterator { + boolean hasNext(); + + T next(); +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java new file mode 100644 index 0000000000..e14efad19c --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java @@ -0,0 +1,172 @@ +package org.korben.list; + +import java.util.Objects; + +/** + * Korben's LinkedList + * + * Created by Korben on 18/02/2017. + */ +public class KLinkedList implements KList { + + private int size; + + private Node head; + + private Node last; + + public KLinkedList() { + this.head = new Node<>(null); + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + @Override + public boolean contains(Object o) { + Node node = this.head; + while (node.next != null) { + node = node.next; + if (Objects.equals(node.data, o)) { + return true; + } + } + return false; + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + @Override + public boolean add(T o) { + if (this.last == null) { + this.last = new Node<>(o); + this.last.pre = this.head; + this.head.next = this.last; + } else { + Node oldLast = this.last; + this.last = new Node<>(o); + this.last.pre = oldLast; + oldLast.next = this.last; + } + this.size++; + return true; + } + + @Override + public boolean remove(T o) { + Node node = this.head; + while (node.next != null) { + node = node.next; + if (Objects.equals(node.data, o)) { + node.pre.next = node.next; + if (node.next != null) { + node.next.pre = node.pre; + } + this.size--; + return true; + } + } + return false; + } + + @Override + public void clear() { + this.head.next = null; + this.last = null; + + this.size = 0; + } + + @Override + public T get(int index) { + return getNode(index).data; + } + + @Override + public T set(int index, T element) { + Node node = getNode(index); + node.data = element; + return element; + } + + @Override + public void add(int index, T element) { + Node node = this.head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + Node pre = node.pre; + Node newNode = new Node<>(element); + pre.next = newNode; + newNode.pre = pre; + newNode.next = node; + node.pre = newNode; + + this.size++; + } + + @Override + public T remove(int index) { + Node node = getNode(index); + Node pre = node.pre; + Node next = node.next; + pre.next = next; + if (next != null) { + next.pre = pre; + } + + this.size--; + return node.data; + } + + @Override + public int indexOf(T o) { + Node node = this.head; + int index = 0; + while (node.next != null) { + node = node.next; + if (Objects.equals(node.data, o)) { + return index; + } + index++; + } + return -1; + } + + @Override + public KIterator iterator() { + throw new IllegalStateException("鏂规硶鏈疄鐜"); + } + + private Node getNode(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node node = this.head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node; + } + + private static class Node { + T data; + Node pre; + Node next; + + Node(T data) { + this.data = data; + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java new file mode 100644 index 0000000000..e5d33b984b --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java @@ -0,0 +1,35 @@ +package org.korben.list; + +/** + * Korben's List + * + * Created by Korben on 18/02/2017. + */ +public interface KList { + + int size(); + + boolean isEmpty(); + + boolean contains(Object o); + + Object[] toArray(); + + boolean add(T o); + + boolean remove(T o); + + void clear(); + + T get(int index); + + T set(int index, T element); + + void add(int index, T element); + + T remove(int index); + + int indexOf(T o); + + KIterator iterator(); +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java new file mode 100644 index 0000000000..994538732f --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java @@ -0,0 +1,36 @@ +package org.korben.list; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Iterator娴嬭瘯 + * + * Created by Korben on 24/02/2017. + */ +public class KListIteratorTest { + + private KList list; + + @Before + public void init() { + this.list = new KArrayList<>(); + + for (int i = 0; i < 5; i++) { + list.add(i); + } + } + + @Test + public void testIterator() { + KIterator iterator = list.iterator(); + Assert.assertTrue(iterator.hasNext()); + int count = 0; + while (iterator.hasNext()) { + int value = iterator.next(); + Assert.assertEquals(count, value); + count++; + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java b/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java new file mode 100644 index 0000000000..ed3055b74e --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java @@ -0,0 +1,142 @@ +package org.korben.list; + +import java.util.Objects; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * KList娴嬭瘯 + * + * Created by Korben on 18/02/2017. + */ +public class KListTest { + + private KList list; + + private int initTestSize; + + @Before + public void init() { + // 娴嬭瘯KArrayList + //list = new KArrayList<>(); + + // 娴嬭瘯KLinkedList + list = new KLinkedList<>(); + + initTestSize = 5; + + for (int i = 0; i < initTestSize; i++) { + list.add(i); + } + } + + @Test + public void size() throws Exception { + Assert.assertEquals(initTestSize, list.size()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertFalse(list.isEmpty()); + + KList list = new KArrayList<>(); + Assert.assertTrue(list.isEmpty()); + } + + @Test + public void contains() throws Exception { + Assert.assertTrue(list.contains(1)); + Assert.assertFalse(list.contains(5)); + } + + @Test + public void toArray() throws Exception { + //Object[] array = list.toArray(); + //Assert.assertEquals(initTestSize, array.length); + //for (int i = 0; i < array.length; i++) { + // Assert.assertEquals(i, array[i]); + //} + } + + @Test + public void add() throws Exception { + Assert.assertTrue(list.add(6)); + Assert.assertTrue(list.contains(6)); + Assert.assertEquals(initTestSize + 1, list.size()); + } + + @Test + public void remove() throws Exception { + Assert.assertEquals(0, list.remove(0).intValue()); + Assert.assertEquals(initTestSize - 1, list.size()); + Assert.assertFalse(list.contains(0)); + } + + @Test + public void clear() throws Exception { + list.clear(); + Assert.assertTrue(list.isEmpty()); + Assert.assertTrue(list.size() == 0); + } + + @Test + public void get() throws Exception { + for (int i = 0; i < initTestSize; i++) { + Assert.assertTrue(Objects.equals(i, list.get(i))); + } + } + + @Test + public void set() throws Exception { + for (int i = 0; i < initTestSize; i++) { + list.set(i, initTestSize); + Assert.assertEquals(initTestSize, list.get(i).intValue()); + } + } + + @Test + public void addByIndex() throws Exception { + // test add by first index + list.add(0, 6); + Assert.assertEquals(initTestSize + 1, list.size()); + Assert.assertEquals(6, list.get(0).intValue()); + for (int i = 0; i < initTestSize; i++) { + Assert.assertEquals(i, list.get(i + 1).intValue()); + } + + // test add by last index + init(); + list.add(initTestSize - 1, 6); + Assert.assertEquals(initTestSize + 1, list.size()); + Assert.assertEquals(initTestSize - 1, list.get(initTestSize).intValue()); + Assert.assertEquals(6, list.get(initTestSize - 1).intValue()); + for (int i = 0; i < initTestSize - 1; i++) { + Assert.assertEquals(i, list.get(i).intValue()); + } + + // test add by middle index + init(); + list.add(3, 90); + Assert.assertEquals(initTestSize + 1, list.size()); + Assert.assertEquals(list.get(0).intValue(), 0); + Assert.assertEquals(list.get(1).intValue(), 1); + Assert.assertEquals(list.get(2).intValue(), 2); + Assert.assertEquals(list.get(3).intValue(), 90); + Assert.assertEquals(list.get(4).intValue(), 3); + Assert.assertEquals(list.get(5).intValue(), 4); + } + + @Test + public void removeByObject() throws Exception { + Assert.assertTrue(list.remove(new Integer(3))); + Assert.assertEquals(initTestSize - 1, list.size()); + } + + @Test + public void indexOf() throws Exception { + for (int i = 0; i < initTestSize; i++) { + Assert.assertEquals(i, list.indexOf(i)); + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java new file mode 100644 index 0000000000..3e975058f4 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java @@ -0,0 +1,111 @@ +package org.korben.queue; + +import java.util.NoSuchElementException; + +/** + * Created by Korben on 18/02/2017. + */ +public class KArrayQueue implements KQueue { + + private int size; + private Object[] dataArray = {}; + private int front = 0; + private int end = -1; + + public KArrayQueue() { + + } + + @Override + public boolean add(T t) { + ensureCapacity(size + 1); + dataArray[end + 1] = t; + end++; + size++; + return true; + } + + @Override + public boolean offer(T t) { + ensureCapacity(size + 1); + dataArray[end + 1] = t; + end++; + size++; + return true; + } + + @Override + @SuppressWarnings("unchecked") + public T remove() { + if (end == -1) { + throw new NoSuchElementException(); + } + T value = (T) dataArray[front]; + dataArray[front] = null; + size--; + front++; + if (front == dataArray.length) { + front = 0; + } + if (size == 0) { + end = -1; + } + + return value; + } + + @Override + @SuppressWarnings("unchecked") + public T poll() { + if (end == -1) { + return null; + } + T value = (T) dataArray[front]; + dataArray[front] = null; + size--; + front++; + if (front == dataArray.length) { + front = 0; + } + + return value; + } + + @Override + @SuppressWarnings("unchecked") + public T element() { + if (end == -1) { + throw new NoSuchElementException(); + } + return (T) dataArray[front]; + } + + @Override + @SuppressWarnings("unchecked") + public T peek() { + if (end == -1) { + return null; + } + return (T) dataArray[front]; + } + + private void ensureCapacity(int minSize) { + if (end == -1) { + dataArray = new Object[8]; + } else if (minSize >= dataArray.length) { + int newLength = dataArray.length * 2; + Object[] newDataArray = new Object[newLength]; + if (front != 0) { + System.arraycopy(dataArray, front, + newDataArray, newLength - dataArray.length + front, + dataArray.length - 1 - front); + + front += newLength - dataArray.length; + } else { + System.arraycopy(dataArray, front, newDataArray, front, size); + } + + dataArray = newDataArray; + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java new file mode 100644 index 0000000000..14763efd99 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java @@ -0,0 +1,20 @@ +package org.korben.queue; + +/** + * Korben's Queue Interface + * + * Created by Korben on 18/02/2017. + */ +public interface KQueue { + boolean add(T t); + + boolean offer(T t); + + T remove(); + + T poll(); + + T element(); + + T peek(); +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java b/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java new file mode 100644 index 0000000000..3d9557748f --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java @@ -0,0 +1,78 @@ +package org.korben.queue; + +import java.util.NoSuchElementException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * KQueue Test + * + * Created by Korben on 19/02/2017. + */ +public class KQueueTest { + private KQueue queue; + + @Before + public void init() { + queue = new KArrayQueue<>(); + } + + @Test + public void add() throws Exception { + for (int i = 0; i < 5; i++) { + queue.add(i); + } + } + + @Test + public void offer() throws Exception { + for (int i = 0; i < 100; i++) { + queue.offer(i); + } + } + + @Test(expected = NoSuchElementException.class) + public void remove() throws Exception { + for (int i = 0; i < 9; i++) { + queue.add(i); + } + for (int i = 0; i < 9; i++) { + Assert.assertEquals(i, queue.remove().intValue()); + } + queue.remove(); + } + + @Test + public void poll() throws Exception { + for (int i = 0; i < 5; i++) { + queue.add(i); + } + for (int i = 0; i < 5; i++) { + Assert.assertEquals(i, queue.poll().intValue()); + } + Assert.assertNull(queue.poll()); + } + + @Test(expected = NoSuchElementException.class) + public void element() throws Exception { + for (int i = 0; i < 5; i++) { + queue.add(i); + Assert.assertEquals(queue.element().intValue(), 0); + } + init(); + + queue.element(); + } + + @Test + public void peek() throws Exception { + for (int i = 0; i < 5; i++) { + queue.add(i); + Assert.assertEquals(queue.peek().intValue(), 0); + } + init(); + + Assert.assertNull(queue.peek()); + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java b/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java new file mode 100644 index 0000000000..8dc39e4efd --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java @@ -0,0 +1,72 @@ +package org.korben.stack; + +import java.util.EmptyStackException; +import java.util.Objects; + +/** + * Korben's Stack + * + * Created by Korben on 18/02/2017. + */ +public class KStack { + + private int size; + private Object[] dataArray = {}; + + public KStack() { + } + + public int size() { + return size; + } + + public T push(T item) { + ensureCapacity(size + 1); + this.dataArray[size] = item; + this.size++; + return item; + } + + @SuppressWarnings("unchecked") + public T pop() { + if (size == 0) { + throw new EmptyStackException(); + } + + T data = (T) this.dataArray[size - 1]; + this.dataArray[size - 1] = null; + this.size--; + return data; + } + + @SuppressWarnings("unchecked") + public T peek() { + if (size == 0) { + throw new EmptyStackException(); + } + return (T) dataArray[size - 1]; + } + + public boolean empty() { + return size == 0; + } + + public synchronized int search(Object o) { + for (int i = 0; i < size; i++) { + if (Objects.equals(o, dataArray[i])) { + return i; + } + } + return -1; + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > dataArray.length) { + int newCapacity = Math.max(minCapacity, dataArray.length * 2); + Object[] newDataArray = new Object[newCapacity]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + + this.dataArray = newDataArray; + } + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java b/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java new file mode 100644 index 0000000000..e3f173eb3f --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java @@ -0,0 +1,71 @@ +package org.korben.stack; + +import java.util.EmptyStackException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * KStack娴嬭瘯 + * + * Created by Korben on 18/02/2017. + */ +public class KStackTest { + private KStack stack; + private int initTestSize; + + @Before + public void init() { + stack = new KStack<>(); + initTestSize = 5; + + for (int i = 0; i < initTestSize; i++) { + stack.push(i); + } + } + + @Test + public void size() throws Exception { + Assert.assertEquals(initTestSize, stack.size()); + } + + @Test + public void push() throws Exception { + stack.push(9); + } + + @Test(expected = EmptyStackException.class) + public void pop() throws Exception { + for (int i = 0; i < initTestSize; i++) { + Integer value = stack.pop(); + Assert.assertEquals(value.intValue(), initTestSize - 1 - i); + } + + stack.pop(); + } + + @Test(expected = EmptyStackException.class) + public void peek() throws Exception { + Assert.assertEquals(initTestSize - 1, stack.peek().intValue()); + for (int i = 0; i < initTestSize; i++) { + stack.pop(); + } + stack.peek(); + } + + @Test + public void empty() throws Exception { + Assert.assertFalse(stack.empty()); + for (int i = 0; i < initTestSize; i++) { + stack.pop(); + } + Assert.assertTrue(stack.empty()); + } + + @Test + public void search() throws Exception { + for (int i = 0; i < initTestSize; i++) { + Assert.assertEquals(i, stack.search(i)); + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java b/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..30c613edd0 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java @@ -0,0 +1,127 @@ +package org.korben.tree; + +/** + * Korben's BinaryTreeNode + * + * Created by Korben on 21/02/2017. + */ +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode insert(T data) { + if (this.data == null) { + this.data = data; + return this; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + this.left = new BinaryTreeNode(); + this.left.data = data; + return this.left; + } else { + return this.left.insert(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + this.right = new BinaryTreeNode(); + this.right.data = data; + return this.right; + } else { + return this.right.insert(data); + } + } else { + return this; + } + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode delete(T data) { + BinaryTreeNode treeNode = search(data); + if (treeNode == null) { + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + return this.left.delete(data); + } else if (compareResult < 0) { + return this.right.delete(data); + } else { + if (treeNode.right == null) { + if (this.left == null) { + this.data = null; + } else { + this.left = this; + } + } else { + this.data = (T) this.right.findMin().data; + + this.right.delete(this.data); + } + } + + return this; + } + + private BinaryTreeNode findMin() { + if (this.data == null) { + return null; + } + if (this.left == null) { + return this; + } + return this.left.findMin(); + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode search(T data) { + if (this.data == null) { + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java b/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..a6fb4ed4e9 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java @@ -0,0 +1,59 @@ +package org.korben.tree; + +import org.junit.Assert; + +/** + * Korben's BinaryTreeNode Test + * + * Created by Korben on 21/02/2017. + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode treeNode; + + @org.junit.Before + public void setUp() throws Exception { + treeNode = new BinaryTreeNode<>(); + treeNode.insert(5); + treeNode.insert(3); + treeNode.insert(7); + treeNode.insert(1); + treeNode.insert(4); + treeNode.insert(2); + treeNode.insert(8); + treeNode.insert(6); + } + + @org.junit.Test + public void insert() { + Assert.assertEquals(treeNode.getData().intValue(), 5); + Assert.assertEquals(treeNode.getLeft().getData(), 3); + Assert.assertEquals(treeNode.getRight().getData(), 7); + Assert.assertEquals(treeNode.getLeft().getLeft().getData(), 1); + Assert.assertEquals(treeNode.getLeft().getRight().getData(), 4); + Assert.assertEquals(treeNode.getLeft().getLeft().getRight().getData(), 2); + Assert.assertEquals(treeNode.getRight().getRight().getData(), 8); + Assert.assertEquals(treeNode.getRight().getLeft().getData(), 6); + } + + @org.junit.Test + public void delete() throws Exception { + treeNode.delete(3); + for (int i = 1; i < 9; i++) { + if (i != 3) { + Assert.assertNotNull(treeNode.search(i)); + } else { + Assert.assertNull(treeNode.search(i)); + } + } + } + + @org.junit.Test + public void search() throws Exception { + for (int i = 1; i < 9; i++) { + Assert.assertNotNull(treeNode.search(i)); + } + Assert.assertNull(treeNode.search(0)); + Assert.assertNull(treeNode.search(9)); + } +} \ No newline at end of file diff --git a/group20/1107837739/korben.md b/group20/1107837739/korben.md new file mode 100644 index 0000000000..983f6f47b6 --- /dev/null +++ b/group20/1107837739/korben.md @@ -0,0 +1,7 @@ +## Korben's Blog Here + +-------- + +| Blog Title | Date| +| ---------- | -----------| +| [鍒濈璁$畻鏈虹▼搴忕殑杩愯](http://korben-chy.github.io/2017/02/26/%E5%88%9D%E7%AA%A5%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C/) | 2017/02/26 | diff --git a/group20/286166752/.gitignore b/group20/286166752/.gitignore new file mode 100644 index 0000000000..d120d1749d --- /dev/null +++ b/group20/286166752/.gitignore @@ -0,0 +1,4 @@ +.idea/ +286166752.iml +out/ + diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/ArrayList.java b/group20/286166752/src/wiki/liven/code/dataStructures/ArrayList.java new file mode 100644 index 0000000000..68444b87a9 --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/dataStructures/ArrayList.java @@ -0,0 +1,86 @@ +package wiki.liven.code.dataStructures; + +/** + * Created by leven on 2017/2/21. + */ +public class ArrayList implements List{ + + /** + * 鍒楄〃涓厓绱犵殑涓暟 + */ + private int size = 0; + private int maxSize = 100; + /** + * 鍒濆鏁扮粍 + */ + private Object[] elementData = new Object[maxSize]; + + /** + * 鍦ㄦ寚瀹氱殑浣嶇疆i鎻掑叆鍏冪礌O + * 鎻掑叆鍏冪礌,鍒ゆ柇褰撳墠鍒楄〃涓厓绱犵殑涓暟, + * 褰搒ize==100,鍒欓渶瑕佹墿寮犳暟缁 + * 褰搒ize<100,鍒欎娇鐢ㄥ垵濮嬫暟缁勫畬鎴愭彃鍏ユ搷浣 + * 鎻掑叆鎿嶄綔: + * 浠庢渶鍚庝竴涓厓绱犲紑濮,寰鍚庣Щ鍔ㄤ竴浣,鐩村埌鍒癷ndex涓烘. + * @param index + * @param o + */ + @Override + public void add(int index, Object o) { + if (size>=maxSize){ + Object[] targt = new Object[++maxSize]; + System.arraycopy(elementData,0,targt,0,maxSize); + for (int j = targt.length;j>=index;j--){ + targt[j-1] = targt[j-2]; + } + targt[index] = o; + size++; + }else if(size=index;j--){ + elementData[j-1] = elementData[j-2]; + } + elementData[index] = o; + size++; + } + } + + /** + * 杩藉姞鍏冪礌 + * @param o + */ + @Override + public void add(Object o) { + if (size>=maxSize){ + Object[] targt = new Object[++maxSize]; + System.arraycopy(elementData,0,targt,0,maxSize); + targt[maxSize-1] = o; + size++; + }else if(sizesize-1;i++){ + elementData[i] = elementData[i+1]; + } + return temp; + } + + @Override + public int size() { + return size; + } + + + + +} diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java new file mode 100644 index 0000000000..b2cc5f8668 --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java @@ -0,0 +1,40 @@ +package wiki.liven.code.dataStructures; + +/** + * Created by leven on 2017/2/21. + */ +public class LinkedList implements List{ + + private Node head; + + private static class Node{ + Object data; + Node next; + } + + + @Override + public void add(int index, Object o) { + + } + + @Override + public void add(Object o) { + + } + + @Override + public Object get(int index) { + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + return 0; + } +} diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/List.java b/group20/286166752/src/wiki/liven/code/dataStructures/List.java new file mode 100644 index 0000000000..2d1840ef0f --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/dataStructures/List.java @@ -0,0 +1,14 @@ +package wiki.liven.code.dataStructures; + +/** + * Created by leven on 2017/2/21. + */ +public interface List { + + public void add(Object o); + public void add(int index,Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + +} diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java new file mode 100644 index 0000000000..b8c8430daa --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java @@ -0,0 +1,7 @@ +package wiki.liven.code.dataStructures; + +/** + * Created by leven on 2017/2/21. + */ +public class Queue { +} diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java new file mode 100644 index 0000000000..59cb18c416 --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java @@ -0,0 +1,7 @@ +package wiki.liven.code.dataStructures; + +/** + * Created by leven on 2017/2/21. + */ +public class Stack { +} diff --git a/group20/286166752/src/wiki/liven/code/test/SomeDemos.java b/group20/286166752/src/wiki/liven/code/test/SomeDemos.java new file mode 100644 index 0000000000..90a74edc35 --- /dev/null +++ b/group20/286166752/src/wiki/liven/code/test/SomeDemos.java @@ -0,0 +1,15 @@ +package wiki.liven.code.test; + +/** + * Created by leven on 2017/2/21. + */ +public class SomeDemos { + + public static void main(String[] args){ + int[] a = new int[10]; + a[0] = 4; + System.out.println(a.length); + + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..0d960ee3d8 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,122 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (i < n) { + elementData[i] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + temp[n] = o; + elementData = temp; + } + size++; + } + public void add(int index, Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index > i) { + System.out.println(index + " is invalid index!"); + return; + } + if (i < n) { + for (int j = i; j > index; j--) { + elementData[j] = elementData[j - 1]; + } + elementData[index] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + for (int j = i; j > index; j--) { + temp[j] = temp[j - 1]; + } + temp[index] = o; + elementData = temp; + } + size++; + } + + public Object get(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } + Object result = elementData[index]; + for (int j = index; j < i; j++) { + elementData[j] = elementData[j + 1]; + } + size--; + return result; + } + + public int size(){ + return size; + } + + public String toString() { + int i = 0; + while (elementData[i] != null) { + i++; + } + String result = ""; + for (int j = 0; j < i - 1; j++) { + result += elementData[j].toString() + ", "; + } + result += elementData[size - 1].toString(); + return result; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String args[]){ + ArrayList list1 = new ArrayList(); + list1.add("a"); + list1.add("b"); + list1.add("c"); + System.out.println(list1.toString()); + list1.add(5, "d"); + list1.add(1, "d"); + System.out.println(list1.toString()); + list1.add(4, "e"); + System.out.println(list1.toString()); + list1.get(5); + System.out.println(list1.get(0)); + list1.remove(2); + System.out.println(list1.toString()); + System.out.println(list1.size()); + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group20/331798361/assignment1/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/group20/331798361/assignment1/src/com/coding/basic/Iterator.java b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group20/331798361/assignment1/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/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e029beaeb4 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,148 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public void add(int index , Object o){ + + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return; + } + if (head.data == null) { + if (index == 0) { + head.data = o; + head.next = null; + size++; + return; + } else { + System.out.println("invalid index!"); + return; + } + } + Node node = new Node(o); + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node temp = curr.next; + curr.next = node; + node.next = temp; + size++; + } + public Object get(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node result = head; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + public Object remove(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = curr.next.next; + size--; + return result; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node temp = head; + head = new Node(o); + head.next = temp; + size++; + } + + public void addLast(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + + public Object removeFirst(){ + if (head.data == null) { + return null; + } + Node result = head; + head = head.next; + size--; + return result; + } + + public Object removeLast(){ + if (head.data == null) { + return null; + } + Node curr = head; + for (int i = 0; i < size - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = null; + size--; + return result; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + Node(Object o) { + data = o; + next = null; + } + + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/List.java b/group20/331798361/assignment1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group20/331798361/assignment1/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/group20/331798361/assignment1/src/com/coding/basic/Queue.java b/group20/331798361/assignment1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c4991a07f5 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + int length = list.size(); + if (length == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + if (list.size() == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return list.size(); + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Stack.java b/group20/331798361/assignment1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..0a48545cea --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.remove(length - 1); + } + + public Object peek(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.get(length - 1); + } + + public boolean isEmpty(){ + if (elementData.size() != 0) { + return false; + } else { + return true; + } + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/ArrayList.java b/group20/404130810/src/com/basic/datastructure/ArrayList.java new file mode 100644 index 0000000000..86c001b2c1 --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/ArrayList.java @@ -0,0 +1,110 @@ +package com.basic.datastructure; + +public class ArrayList implements List { + + private Object[] elementData; + private int size; + + private int enableCapacity; + + public ArrayList() { + this.enableCapacity = 10; + this.elementData = new Object[enableCapacity]; + } + + @Override + public void add(Object o) { + growIfNeeded(); + elementData[size] = o; + this.size++; + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + growIfNeeded(); + + Object[] tmpObjects = new Object[elementData.length]; + System.arraycopy(elementData, 0, tmpObjects, 0, index); + tmpObjects[index] = o; + System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); + + elementData = tmpObjects; + + this.size++; + } + + @Override + public Object get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObj = this.get(index); + rangeCheck(index); + Object[] tmpObjects = new Object[elementData.length]; + + System.arraycopy(elementData, 0, tmpObjects, 0, index); + System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); + + elementData = tmpObjects; + + return removedObj; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + for (int i = 0; i < elementData.length; i++) { + System.out.print(elementData[i] + ","); + } + return ""; + } + + private void rangeCheck(int paramInt) { + if ((paramInt < 0) || (paramInt >= size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private void rangeCheckForAdd(int paramInt) { + if ((paramInt < 0) || (paramInt > size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private String outOfBoundsMsg(int paramInt) { + return "Index: " + paramInt + ", Size: " + size; + } + + private Object[] growIfNeeded() { + if (enableCapacity <= this.size) { + enableCapacity = enableCapacity * 2; + Object[] largerElementData = new Object[enableCapacity]; + System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); + elementData = largerElementData; + } + return elementData; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for (int i = 0; i <= 11; i++) { + list.add(String.valueOf(i)); + } + System.out.println(list.get(11)); + //list.add(10,"test"); + //list.get(10); + //list.remove(10); + //System.out.println(list); + } + +} diff --git a/group20/404130810/src/com/basic/datastructure/LinkedList.java b/group20/404130810/src/com/basic/datastructure/LinkedList.java new file mode 100644 index 0000000000..43ba7ddd59 --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/LinkedList.java @@ -0,0 +1,176 @@ +package com.basic.datastructure; + +public class LinkedList implements List { + private Node first; + private Node last; + + private int size; + + public void add(Object item) { + addLast(item); + } + + public void add(int index, Object item) { + checkRange(index); + if (index == 0) { + addFirst(item); + } else if (index == size) { + addLast(item); + } else { + Node tmpNode = new Node(item); + Node preNode = get(index - 1); + Node nextNode = get(index + 1); + preNode.next = tmpNode; + tmpNode.next = nextNode; + size ++; + } + } + + public Node get(int index) { + checkRange(index); + if(size > 0){ + int loopTimes = 0; + Node p = first; + while(index > loopTimes){ + p = p.next; + loopTimes ++; + } + return p; + } + + return null; + } + + public Object remove(int index) { + checkRange(index); + Node tmpNode = null; + if(index == 0){ + removeFirst(); + }else if(index == size -1){ + removeLast(); + }else{ + tmpNode = get(index); + Node preNode = get(index-1); + Node nextNode = get(index + 1); + preNode.next = nextNode; + size --; + } + + return tmpNode; + } + + public int size() { + return size; + } + + public void addFirst(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + Node tmpNode = new Node(item); + tmpNode.next = first; + first = tmpNode; + } + size++; + } + + public void addLast(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + last.next = new Node(item); + last = last.next; + } + size++; + } + + public Object removeFirst() { + Node tmpNode = first; + if(tmpNode == null){ + last = null; + }else if(size == 2){ + first = last; + last = null; + size --; + }else{ + first = first.next; + size--; + } + return tmpNode; + } + + public Object removeLast() { + Node tmpNode = null; + if(size == 1){ + this.removeFirst(); + }else if(size >0){ + int index = size - 1; + tmpNode = last; + get(index - 1).next = null; + last = get(index - 1); + size --; + } + return tmpNode; + } + + private void checkRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + Node p = first; + while (p != null) { + sb.append(p.item + "\n"); + p = p.next; + } + return sb.toString(); + } + + private static class Node { + private Object item; + private Node next; + Node(Object item) { + this.item = item; + } + } + + public static void main(String[] args) { + + /*Test add + LinkedList list = new LinkedList(); + for (int i = 0; i <= 5; i++) { + list.add(i); + } + list.add(3, "test"); + System.out.println(list); + */ + + /*Test remove + list.remove(3); + System.out.println(list); + */ + + /*Test removeLast and removeFirst + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + */ + + /*Test from Java API + java.util.LinkedList linkedList = new java.util.LinkedList(); + linkedList.add("test"); + linkedList.removeFirst(); + System.out.println(linkedList); + */ + + } +} diff --git a/group20/404130810/src/com/basic/datastructure/List.java b/group20/404130810/src/com/basic/datastructure/List.java new file mode 100644 index 0000000000..13fc25acfd --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/List.java @@ -0,0 +1,10 @@ +package com.basic.datastructure; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + +} diff --git a/group20/404130810/src/com/basic/datastructure/Queue.java b/group20/404130810/src/com/basic/datastructure/Queue.java new file mode 100644 index 0000000000..f83d937e5c --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/Queue.java @@ -0,0 +1,33 @@ +package com.basic.datastructure; + +public class Queue { + LinkedList list = new LinkedList(); + private int size; + + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + size --; + return list.removeLast(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return size; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + queue.deQueue(); + System.out.println("Finished"); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/Stack.java b/group20/404130810/src/com/basic/datastructure/Stack.java new file mode 100644 index 0000000000..7a58fd49e6 --- /dev/null +++ b/group20/404130810/src/com/basic/datastructure/Stack.java @@ -0,0 +1,41 @@ +package com.basic.datastructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + public Object pop(){ + size --; + return elementData.remove(elementData.size() - 1); + } + + /** + * Looks at the object at the top of this stack without removing it from the stack. + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + stack.push(i); + } + System.out.println(stack.peek()); + + stack.pop(); + System.out.println("Finished"); + } + +} diff --git a/group20/404130810/src/com/basic/practice/PolymorphicInJava.java b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java new file mode 100644 index 0000000000..6bf9996da3 --- /dev/null +++ b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java @@ -0,0 +1,41 @@ +package com.basic.practice; + +/** + * + * @author Wu Alvin + * Java Polymorphic Only represent in method level + * + */ + +class Fruit{ + String name = "Fruit"; + public void print(int i){ + System.out.println("Fruit" + i); + } +} + + +class Apple extends Fruit{ + String name = "Apple"; + public void print(int i){ + System.out.println("Apple" + i); + } +} + + +public class PolymorphicInJava { + + public static void main(String[] args) { + Apple apple = new Apple(); + apple.print(100); + //Apple100 + System.out.println(apple.name); + //Apple + Fruit fruit = apple; + fruit.print(100); + //Apple100 + System.out.println(fruit.name); + //Fruit + } + +} diff --git a/group20/404130810/src/com/basic/practice/ValuePassInJava.java b/group20/404130810/src/com/basic/practice/ValuePassInJava.java new file mode 100644 index 0000000000..6187b5e0fc --- /dev/null +++ b/group20/404130810/src/com/basic/practice/ValuePassInJava.java @@ -0,0 +1,44 @@ +package com.basic.practice; + +public class ValuePassInJava { + + /* + * Pass the Simple value, etc int String... + * Change will NOT happened + * + */ + public static void main(String[] args) { + String s = new String("123"); + int i = 1; + changeVal(i); + System.out.println(i); + + } + + private static void changeVal(int i){ + i = 2; + } + + /* + * Pass whole OBJECT, but change the Member variable + * Change will happened + */ + + /* + public static void main(String[] args) { + Person p = new Person(); + p.age = 10; + changeAge(p); + System.out.println(p.age); + } + + private static void changeAge(Person p){ + p.age = 20; + } + */ + +} + +class Person{ + int age; +} diff --git a/group20/423184723/src/com/coding/basic/ArrayList.java b/group20/423184723/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/BinaryTreeNode.java b/group20/423184723/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/Iterator.java b/group20/423184723/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/LinkedList.java b/group20/423184723/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/List.java b/group20/423184723/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/Queue.java b/group20/423184723/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group20/423184723/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/group20/423184723/src/com/coding/basic/Stack.java b/group20/423184723/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group20/423184723/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/group20/452472201/.gitignore b/group20/452472201/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group20/452472201/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group20/452472201/src/com/coding/basic/ArrayList.java b/group20/452472201/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1af26ce934 --- /dev/null +++ b/group20/452472201/src/com/coding/basic/ArrayList.java @@ -0,0 +1,98 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size=0; + + private Object[] elementData =new Object[10]; + + + //鏁扮粍鎵╁ + private void ensureCapacityInternal(){ + if(size==elementData.length){ + Object[] newArray = new Object[size*2]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData=newArray; + } + } + + public void add(Object o){ + ensureCapacityInternal(); + elementData[size]=o; + size++; + } + + public void add(int index, Object o){ + ensureCapacityInternal(); + if(index<0){ + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + System.arraycopy(elementData, index, elementData, index+1,size-index ); + elementData[index]=o; + size++; + } + + public Object get(int index){ + if(index<0||index>=size){ + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return elementData[index]; + } + + public Object remove(int index){ + if(index>=size){ + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + }else{ + + int numMoved=size-index-1; + if(numMoved>0){ + System.arraycopy(elementData, index+1, elementData, index, numMoved); + } + + elementData[size--] = null; + } + return index; + + } + + + public int size(){ + return size; + } + + + private class Iter implements Iterator { + //璁℃暟鍣 + private int coursor=-1; + //鍒ゆ柇鏄惁瀛樺湪涓嬩竴涓 + public boolean hasNext(){ + return coursor+1 + */ +public class ArrayList implements List { + + /** 鍏冪礌涓暟 */ + private int size; + + /** 榛樿瀹归噺 */ + private static final int DEFAULT_CAPACITY = 10; + + /** 榛樿鏈澶у閲 */ + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + + /** 榛樿鏁扮粍 */ + private static final Object[] EMPTY_ELEMENTDATA = {}; + + /** 鐢ㄤ簬瀛樻斁鍏冪礌 */ + private Object[] elementData; + + /** + * 鏃犲弬鏋勯犲皢浣跨敤榛樿鏁扮粍 + */ + public ArrayList() { + elementData = EMPTY_ELEMENTDATA; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + // @Override + // public boolean contains(Object o) { + // // TODO Auto-generated method stub + // return false; + // } + + @Override + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor; // 褰撳墠绱㈠紩 + int lastRet = -1;// + + @Override + public boolean hasNext() { + return cursor != size; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + if (cursor > size) { + throw new NoSuchElementException(); + } + Object[] elementData = ArrayList.this.elementData; + if (cursor >= elementData.length) { + throw new ConcurrentModificationException(); + } + return (E) elementData[lastRet = cursor++]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + size--; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + } + + // @Override + // public Object[] toArray() { + // // TODO Auto-generated method stub + // return null; + // } + // + // @Override + // public T[] toArray(T[] a) { + // // TODO Auto-generated method stub + // return null; + // } + + @Override + public boolean add(E element) { + // 淇濊瘉鏁扮粍闀垮害姝g‘ + ensureCapacityInternal(size + 1); + // 浣跨敤鍚庣紑鑷搷浣滅 + elementData[size++] = element; + return true; + } + + @Override + public boolean add(int index, E element) { + rangeCheckForAdd(index);// 杩涜娣诲姞鎿嶄綔鐨勭储寮曡寖鍥存鏌 + // 淇濊瘉鏁扮粍闀垮害姝g‘ + ensureCapacityInternal(size + 1); + // 婧愭暟缁勪腑浣嶇疆鍦 srcPos 鍒 srcPos+length-1 涔嬮棿鐨勭粍浠惰鍒嗗埆澶嶅埗鍒扮洰鏍囨暟缁勪腑鐨 destPos 鍒 + // destPos+length-1 浣嶇疆 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + return true; + } + + private void ensureCapacityInternal(int minCapacity) { + if (elementData == EMPTY_ELEMENTDATA) + minCapacity = Math.max(minCapacity, DEFAULT_CAPACITY); + // 浼犲叆鏈灏忓閲忓ぇ浜庡綋鍓嶆暟缁勯暱搴︼紝鍒欐墿瀹 + if (minCapacity > elementData.length) + grow(minCapacity); + } + + /** + * 鎵╁ + * + * @param minCapacity + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length;// 鑾峰彇鍘熸暟缁勯暱搴 + // 璁$畻鏂板閲 + int newCapacity = oldCapacity + (oldCapacity >> 1);// 鍘熷閲+(鍘熷閲/2)锛屼娇鐢ㄤ綅绉荤鎻愰珮杩愯閫熷害 + newCapacity = newCapacity < minCapacity ? minCapacity : newCapacity; + if (newCapacity > MAX_ARRAY_SIZE) + newCapacity = hugeCapacity(newCapacity); + // 灏嗗師鏁扮粍鏁版嵁澶嶅埗鍒颁竴涓暱搴︿负newCapacity鐨勬柊鏁扮粍涓 + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /** + * 浼犲叆瀹归噺鏄惁澶т簬鏈澶у閲忓父閲忥紝濡傚ぇ浜庢渶澶у閲忥紝鍒欒繑鍥瀒nt绫诲瀷鎵鑳借〃绀虹殑鏈澶у ArrayList鏈澶у閲忎负int绫诲瀷鎵鑳借〃绀虹殑鏈澶у + * + * @param minCapacity + * @return + */ + private int hugeCapacity(int minCapacity) { + if (minCapacity < 0) { + throw new OutOfMemoryError("The index cannot be negative"); + } + return minCapacity > MAX_ARRAY_SIZE ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + rangeCheck(index);// 杩涜绱㈠紩鐨勮寖鍥存鏌 + return (E) elementData[index]; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + rangeCheck(index);// 杩涜绱㈠紩鐨勮寖鍥存鏌 + // 鍙栧埌鎸囧畾绱㈠紩鍏冪礌锛屽皢鏂板厓绱犵疆鍏ヨ绱㈠紩浣嶏紝骞惰繑鍥炲師鍏冪礌 + E oldValue = (E) elementData[index]; + elementData[index] = element; + return oldValue; + } + + @SuppressWarnings("unchecked") + @Override + public E remove(int index) { + rangeCheck(index);// 杩涜绱㈠紩鐨勮寖鍥存鏌 + // 鑾峰彇鎸囧畾绱㈠紩澶勫厓绱 + E rmValue = (E) elementData[index]; + // 婧愭暟缁勪腑浣嶇疆鍦 srcPos 鍒 srcPos+length-1 涔嬮棿鐨勭粍浠惰鍒嗗埆澶嶅埗鍒扮洰鏍囨暟缁勪腑鐨 destPos 鍒 + // destPos+length-1 浣嶇疆 + System.arraycopy(elementData, index + 1, elementData, index, size - (index - 1)); + size--; + return rmValue; + } + + // @Override + // public int indexOf(Object o) { + // // TODO Auto-generated method stub + // return 0; + // } + + /** + * 娣诲姞鏃剁殑绱㈠紩鑼冨洿妫鏌 + * + * @param index + */ + private void rangeCheckForAdd(int index) { + if (index > this.size || index < 0)// 娣诲姞鍙互寰鏈綅鎻掑叆锛屾墍浠ヨ繖閲岀储寮曠瓑浜庡厓绱犱釜鏁颁篃鍙互 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 绱㈠紩鑼冨洿妫鏌 + * + * @param index + */ + private void rangeCheck(int index) { + if (index < 0 || index >= this.size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 浠ュ瓧绗︿覆褰㈠紡杩斿洖绱㈠紩鍜屽厓绱犱釜鏁颁俊鎭 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + this.size; + } + +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java b/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java new file mode 100644 index 0000000000..ccb9ff2783 --- /dev/null +++ b/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java @@ -0,0 +1,284 @@ +package cn.wsc.util; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +/** + * LinkedList绫 + * + * @author Administrator + * @date 2017骞2鏈25鏃ヤ笂鍗10:52:41 + * @version v1.0 + * + * @param + */ +public class LinkedList implements List { + + private int size; + Node first; // 閾捐〃鐨勫ご鑺傜偣 + Node last; // 閾捐〃鐨勫熬鑺傜偣 + + private static class Node { + E item; // 瀛樺偍鏁版嵁 + Node prev; // 涓婁竴涓妭鐐 + Node next; // 涓嬩竴涓妭鐐 + + Node(Node prev, E element, Node next) { + this.item = element; + this.next = next; + this.prev = prev; + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor; // 褰撳墠绱㈠紩 + int lastRet = -1;// 涓婁竴娆$储寮 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public E next() { + if (cursor > size) { + throw new NoSuchElementException(); + } + return get(lastRet = cursor++); + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + try { + LinkedList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + size--; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + } + + @Override + public boolean add(E e) { + linkLast(e); + return true; + } + + @Override + public boolean add(int index, E element) { + checkPositionIndex(index);// 浣嶇疆鑼冨洿妫鏌 + // 濡傛灉绱㈠紩绛変簬鍏冪礌涓暟锛屽垯鐩存帴鎻掑叆灏鹃儴 + if (index == size) { + linkLast(element); + } else { + linkBefore(element, node(index)); + } + return true; + } + + @Override + public E get(int index) { + return node(index).item; + } + + /** + * 缁存姢澶磋妭鐐 + * + * @param e + */ + void linkFirst(E e) { + Node f = first; + // 鍒涘缓鏂拌妭鐐癸紝灏嗗師澶磋妭鐐逛綔涓烘柊鑺傜偣鐨勪笅涓涓妭鐐 + Node newNode = new Node(null, e, f); + // 灏嗘柊鑺傜偣璁剧疆涓哄ご鑺傜偣 + first = newNode; + // 濡傚師澶磋妭鐐逛负null锛屽垯灏捐妭鐐逛篃涓簄ewNode + if (f == null) { + last = newNode; + } else {// 鍚﹀垯灏嗘柊鑺傜偣浣滀负鍘熷ご鑺傜偣鐨勪笂涓涓妭鐐 + f.prev = newNode; + } + size++; + + } + + /** + * 缁存姢灏捐妭鐐 + * + * @param e + */ + void linkLast(E e) { + Node l = last; + // 鍒涘缓鏂拌妭鐐癸紝灏嗗熬鑺傜偣浣滀负鏂拌妭鐐圭殑涓婁竴涓妭鐐 + Node newNode = new Node(l, e, null); + // 灏嗘柊鑺傜偣璁剧疆涓哄熬鑺傜偣 + last = newNode; + // 濡傚師灏捐妭鐐逛负null锛屽垯澶磋妭鐐逛篃涓簄ewNode + if (l == null) { + first = newNode; + } else {// 鍚﹀垯灏嗘柊鑺傜偣浣滀负鍘熷熬鑺傜偣鐨勪笅涓涓妭鐐 + l.next = newNode; + } + size++; + } + + /** + * 鍦ㄦ寚瀹氳妭鐐瑰墠鎻掑叆鏂拌妭鐐 + * + * @param e + * @param node + */ + void linkBefore(E e, Node node) { + // 鑾峰彇node鐨勪笂涓涓妭鐐癸紝骞跺垱寤烘柊鑺傜偣锛屽皢pred鍋氫负鏂拌妭鐐圭殑涓婁竴涓妭鐐癸紝灏唍ode浣滀负鏂拌妭鐐圭殑涓嬩竴涓妭鐐 + final Node pred = node.prev; + final Node newNode = new Node<>(pred, e, node); + // 灏唍ode鐨勪笂涓涓妭鐐规寚鍚憂ewNode + node.prev = newNode; + // 濡俻rev涓簄ull锛屽垯璇存槑node涓篺irst锛岄偅涔堝皢鏂拌妭鐐硅涓篺irst + if (pred == null) { + first = newNode; + } else {// 鍚﹀垯锛屽皢鏂拌妭鐐硅涓簆red鐨勪笅涓涓妭鐐 + pred.next = newNode; + } + size++; + } + + /** + * 鑾峰彇鑺傜偣 + * + * @param index + * @return + */ + Node node(int index) { + // 绱㈠紩灏忎簬闀垮害鐨2鍒嗕箣涓鍒欎粠鍓嶅悜鍚庨亶鍘嗭紝鍚﹀垯浠庡悗鍚戝墠閬嶅巻锛屽噺灏戦亶鍘嗘鏁 + if (index < (size >> 1)) { + Node x = first; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** + * 鑾峰彇澶磋妭鐐 + * + * @return + */ + public E getFirst() { + Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.item; + } + + /** + * 鑾峰彇灏捐妭鐐 + * + * @return + */ + public E getLast() { + Node l = last; + if (l == null) + throw new NoSuchElementException(); + return l.item; + } + + @Override + public E set(int index, E e) { + checkElementIndex(index);// 绱㈠紩鑼冨洿妫鏌 + // 鑾峰彇绱㈠紩澶勮妭鐐癸紝濉叆鏂板硷紝杩斿洖鍘熷 + Node x = node(index); + E oldVal = x.item; + x.item = e; + return oldVal; + } + + @Override + public E remove(int index) { + checkElementIndex(index);// 绱㈠紩鑼冨洿妫鏌 + return unlink(node(index)); + } + + /** + * 鍒犻櫎鑺傜偣 + * + * @param x + * @return + */ + E unlink(Node x) { + // 鑾峰彇姝よ妭鐐圭殑涓婁竴涓妭鐐瑰拰涓嬩竴涓妭鐐 + E element = x.item; + Node prev = x.prev; + Node next = x.next; + // 濡俻rev鑺傜偣涓簄ull锛屽垯璇存槑x鑺傜偣涓篺irst锛岄偅涔堝皢next鑺傜偣璁句负first + if (prev == null) { + first = next; + } else {// 鍚﹀垯锛屽皢prev鑺傜偣鐨勪笅涓涓妭鐐硅涓簄ext + prev.next = next; + } + // 濡俷ext鑺傜偣涓簄ull锛屽垯璇存槑x鑺傜偣涓簂ast锛岄偅涔堝皢prev鑺傜偣璁句负last + if (next == null) { + last = prev; + } else {// 鍚﹀垯锛屽皢next鑺傜偣鐨勪笂涓涓妭鐐硅涓簆rev + next.prev = prev; + } + x.item = null; + size--; + return element; + } + + /** + * 浣嶇疆鑼冨洿妫鏌 >0 && <=size + * + * @param index + */ + private void checkPositionIndex(int index) { + if (index > this.size || index < 0)// 娣诲姞鍙互寰鏈綅鎻掑叆锛屾墍浠ヨ繖閲岀储寮曠瓑浜庡厓绱犱釜鏁颁篃鍙互 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 绱㈠紩鑼冨洿妫鏌 >0 && = this.size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 浠ュ瓧绗︿覆褰㈠紡杩斿洖绱㈠紩鍜屽厓绱犱釜鏁颁俊鎭 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + this.size; + } +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/List.java b/group20/592146505/data _structure/src/cn/wsc/util/List.java new file mode 100644 index 0000000000..9b61278fc7 --- /dev/null +++ b/group20/592146505/data _structure/src/cn/wsc/util/List.java @@ -0,0 +1,105 @@ +package cn.wsc.util; + +/** + * List鎺ュ彛 + * + * @author Administrator + * @date 2017骞2鏈20鏃ヤ笅鍗9:15:32 + * @version v1.0 + * + * @param + */ +public interface List { + + /** + * 鑾峰彇闆嗗悎鍏冪礌涓暟 + * + * @return + */ + int size(); + + /** + * 闆嗗悎鏄惁涓虹┖ + * + * @return + */ + boolean isEmpty(); + + // /** + // * 鏄惁鍖呭惈鎸囧畾鍏冪礌 + // * @param o + // * @return + // */ + // boolean contains(Object o); + + /** + * 鑾峰彇褰撳墠闆嗗悎杩唬鍣ㄥ璞 + * + * @return + */ + Iterator iterator(); + // + // /** + // * 杩斿洖闆嗗悎鏁扮粍瀵硅薄 + // * + // * @return + // */ + // Object[] toArray(); + // + // /** + // * 灏嗛泦鍚堝厓绱犲鍒跺埌鏂版暟缁勪腑 + // * @param a + // * @return + // */ + // T[] toArray(T[] a); + + /** + * 鍦ㄩ泦鍚堟湯灏捐拷鍔犲厓绱 + * + * @param e + * @return + */ + boolean add(E e); + + /** + * 灏嗗厓绱犳坊鍔犺嚦鎸囧畾鎸囧畾绱㈠紩澶 + * + * @param index + * @param e + * @return + */ + boolean add(int index, E e); + + /** + * 鑾峰彇鎸囧畾绱㈠紩澶勫厓绱 + * + * @param index + * @return + */ + E get(int index); + + /** + * 鏇挎崲鎸囧畾绱㈠紩澶勫厓绱犱负鏂板厓绱狅紝骞惰繑鍥炶鏇挎崲鍏冪礌锛 + * + * @param index + * @param e + * @return + */ + E set(int index, E e); + + /** + * 鍒犻櫎骞惰繑鍥炴寚瀹氭寚瀹氱储寮曞鍏冪礌 + * + * @param index + * @return + */ + E remove(int index); + + // /** + // * 杩斿洖璇ュ璞″湪闆嗗悎涓殑涓嬫爣锛屼笉瀛樺湪杩斿洖-1 + // * @param o + // * @return + // */ + // int indexOf(Object o); + +} \ No newline at end of file diff --git a/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java b/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java new file mode 100644 index 0000000000..3f156461e8 --- /dev/null +++ b/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java @@ -0,0 +1,22 @@ +package cn.wsc.utils; + +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/group20/755659358/blogs/\350\256\241\347\256\227\346\234\272\351\203\250\344\273\266\347\220\206\350\247\243.txt" "b/group20/755659358/blogs/\350\256\241\347\256\227\346\234\272\351\203\250\344\273\266\347\220\206\350\247\243.txt" new file mode 100644 index 0000000000..155001683a --- /dev/null +++ "b/group20/755659358/blogs/\350\256\241\347\256\227\346\234\272\351\203\250\344\273\266\347\220\206\350\247\243.txt" @@ -0,0 +1,2 @@ + 瀵硅绠楁満纭欢鐨勭悊瑙 + 璁$畻鏈轰腑cpu鏄牳蹇冮儴浠讹紝cpu鏄富瑕佺殑璁$畻閮ㄤ欢锛屾墍鏈夌殑閫昏緫杩愮畻鍜岀畻鏈繍绠楅兘鏄敱cpu鏉ョ帺鎴愮殑銆備絾cpu鍙杩愮畻锛屾庝箞杩愮畻锛屼綍鏃惰繍绠楋紝杩愮畻浠涔堣繖浜沜pu閮芥槸涓嶇鐨勶紝杩欎簺鍏跺疄灏辨槸涓涓釜鐨勬寚浠わ紝鎸囦护鐢ㄦ潵琛ㄧず鎵鏈夎繍绠楃殑淇℃伅锛屽寘鎷仛浠涔堣繍绠楋紝杩愮畻浠涔堬紝杩愮畻缁撴灉濡備綍澶勭疆绛夈傝繖浜涙寚浠ら兘鏄瓨鍦ㄥ唴瀛樺綋涓殑锛宑pu鎸夌収涓瀹氱殑椤哄簭浠庡唴瀛樺綋涓鍙栨寚浠わ紝鎵ц鎸囦护鍋氳繍绠楋紝鐒跺悗鎸夌収鐩稿簲鐨勬寚浠よ緭鍑虹粨鏋溿傝繖灏辨槸涓涓畝鍗曠殑绋嬪簭杩愯杩囩▼銆傚唴瀛樼┖闂存槸鏈夐檺鐨勶紝鍙﹀锛屽唴瀛樹笂鐨勬寚浠や篃涓嶈兘鎸佷箙鍖栫殑淇濆瓨锛屾柇鐢典箣鍚庡氨娌℃湁浜嗐傛墍浠ラ渶瑕佷竴涓‖浠朵繚瀛樺ぇ鐨勪簩杩涘埗璧勬簮浠ュ強鎸佷箙鍖栫殑淇濆瓨鎸囦护锛屾墍浠ュ氨鏈変簡纭洏銆傛墍浠ヨ绠楁満鍏堜粠纭洏涓鍙栨寚浠ゅ拰璧勬簮鍒板唴瀛樺綋涓潵锛岀劧鍚巆pu鍐嶄粠鍐呭瓨褰撲腑璇诲彇鎸囦护鍜岃祫婧愶紝鍋氳繍绠楋紝杩愮畻瀹屼箣鍚庡啀鎸夌収鎸囦护灏嗚緭鍑轰繚瀛樺湪鍐呭瓨涓垨鑰呯‖鐩樹腑锛岀▼搴忔墽琛屽畬姣曘 diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java new file mode 100644 index 0000000000..8a4434041f --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java @@ -0,0 +1,56 @@ +package liuxincourse; + +import java.util.Arrays; + + +public class ArrayList implements List{ + + private int size=0; + + private Object [] elementDataObjects = new Object[3]; + + public void add (Object o){ + if (size>=elementDataObjects.length) { + elementDataObjects=Arrays.copyOf(elementDataObjects, elementDataObjects.length+50); + } + elementDataObjects[size]=o; + size++; + } + + public void add (int index ,Object o){ + if (index>=size||index<0) { + throw new IndexOutOfBoundsException(); + } + if (size>=elementDataObjects.length) { + elementDataObjects=Arrays.copyOf(elementDataObjects, elementDataObjects.length+50); + } + System.arraycopy(elementDataObjects, index, elementDataObjects, index+1, size-index); + elementDataObjects[index]=o; + size++; + } + + public Object get (int index){ + if (index>=size||index<0) { + throw new IndexOutOfBoundsException(); + } + return elementDataObjects[index]; + } + + public Object remove(int index){ + if (index>=size||index<0) { + throw new IndexOutOfBoundsException(); + } + Object rem=elementDataObjects[index]; + System.arraycopy(elementDataObjects, index+1, elementDataObjects, index, size-index-1); + size--; + return rem; + } + + public int size(){ + return size; + } + +// public Iterator iterator(){ +// +// } +} diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java new file mode 100644 index 0000000000..36e46e6412 --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java @@ -0,0 +1,120 @@ +package liuxincourse; + + +public class LinkedList implements List{ + + private Node head; + + private int size=0; + + + public void add(Object o){ + if (size==0) { + head=new Node(); + head.data=o; + size++; + return; + } + Node last=head; + for (int i = 0; i < size-1; i++) { + last=last.next; + } + Node added=new Node(); + last.next=added; + added.data=o; + size++; + } + + public void add(int index,Object o){ + if (index<0||index>size) { + throw new IndexOutOfBoundsException(); + } + Node pre=getNode(index-1); + Node next=getNode(index); + Node addedNode=new Node(); + addedNode.data=o; + pre.next=addedNode; + addedNode.next=next; + size++; + } + + private Node getNode(int index){ + Node node=head; + + for (int i = 0; i < index; i++) { + node=node.next; + } + + return node; + } + + public Object get(int index){ + if (index<0||index>size-1) { + throw new IndexOutOfBoundsException(); + } + if (index==0&&head==null) { + return null; + } + return getNode(index).data; + + } + + public Object remove(int index) { + if (index<0||index>size-1) { + throw new IndexOutOfBoundsException(); + } + Node pre=getNode(index-1); + Node next=getNode(index+1); + pre.next=next; + return getNode(index); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + if (head==null) { + head=new Node(); + head.data=o; + size++; + return; + } + Node addNode=new Node(); + addNode.data=o; + addNode.next=head; + head=addNode; + size++; + } + + public void addLast(Object o){ + Node preLast=getNode(size-1); + Node addNode=new Node(); + addNode.data=o; + preLast.next=addNode; + size++; + } + + public Object removeFirst(){ + Node preHead=head; + head=head.next; + size--; + return preHead.data; + } + + public Object removeLast(){ + Node preLast=getNode(size-1); + Node last=getNode(size-2); + last.next=null; + size--; + return preLast.data; + } + + private static class Node{ + + Object data; + Node next; + + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java new file mode 100644 index 0000000000..c4eaa0a02c --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java @@ -0,0 +1,10 @@ +package liuxincourse; + +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/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java new file mode 100644 index 0000000000..bbe11c043d --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java @@ -0,0 +1,25 @@ +package liuxincourse; + +public class Queue { + + LinkedList list=new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + + return list.removeFirst(); + } + + public boolean isEmpty() { + + return size()==0?true:false; + } + + public int size(){ + return list.size(); + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java new file mode 100644 index 0000000000..552d8c9f9e --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java @@ -0,0 +1,27 @@ +package liuxincourse; + +public class Stack { + + private LinkedList elementData=new LinkedList(); + + public void push (Object o){ + elementData.addFirst(o); + } + + public Object pop() { + return elementData.removeFirst(); + } + + public Object peek(){ + return elementData.get(0); + } + + public boolean isEmpty(){ + return size()==0?true:false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java new file mode 100644 index 0000000000..8b8d82b3b1 --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java @@ -0,0 +1,53 @@ +package liuxincourse; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + + @Test + public void testAdd(){ + ArrayList list=new ArrayList(); + list.add(12); + list.add(2); + list.add(8); + assertEquals(8, list.get(2)); + } + + @Test + public void testAddIndex(){ + ArrayList list=new ArrayList(); + list.add(12); + list.add(2); + list.add(8); + list.add(1,33); + list.add(6); + list.add(7); + assertEquals(8, list.get(3)); + } + + @Test + public void testRemove(){ + ArrayList list=new ArrayList(); + list.add(12); + list.add(2); + list.add(8); + list.add(1,33); + list.remove(1); + assertEquals(2, list.get(1)); + } + + @Test + public void testSize(){ + ArrayList list=new ArrayList(); + list.add(12); + list.add(2); + list.add(8); + list.add(9); + list.add(77); + assertEquals(5, list.size()); + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java new file mode 100644 index 0000000000..69857b0e29 --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java @@ -0,0 +1,69 @@ +package liuxincourse; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void testAdd() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.add(88); + assertEquals(88, list.get(3)); + } + + @Test + public void testAddIndex() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.add(1, 88); + assertEquals(55, list.get(3)); + } + + @Test + public void testAddFirst() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.addFirst(88); + assertEquals(88, list.get(0)); + } + + @Test + public void testAddLast() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.addFirst(88); + list.addLast(00); + assertEquals(00, list.get(list.size()-1)); + } + + @Test + public void testRemoveFirst() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.addFirst(88); + assertEquals(88, list.removeFirst()); + } + + @Test + public void testRemoveLast() { + LinkedList list=new LinkedList(); + list.add(33); + list.add(44); + list.add(55); + list.addFirst(88); + assertEquals(55, list.removeLast()); + } +} diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java new file mode 100644 index 0000000000..613df89fc3 --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java @@ -0,0 +1,27 @@ +package liuxincourse; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class QueueTest { + + @Test + public void testEnqueue() { + Queue queue=new Queue(); + queue.enQueue(11); + queue.enQueue(22); + assertEquals(11, queue.deQueue()); + } + + @Test + public void testIsempty() { + Queue queue=new Queue(); + queue.enQueue(11); + queue.enQueue(22); + queue.deQueue(); + queue.deQueue(); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java new file mode 100644 index 0000000000..9f1175ffea --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java @@ -0,0 +1,26 @@ +package liuxincourse; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class StackTest { + + @Test + public void testPush() { + Stack stack=new Stack(); + stack.push(22); + stack.push(33); + assertEquals(33, stack.pop()); + } + + @Test + public void testIsempty() { + Stack stack=new Stack(); + stack.push(22); + stack.push(33); + stack.pop(); + assertEquals(false, stack.isEmpty()); + } + +} diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java new file mode 100644 index 0000000000..5c12e657b6 --- /dev/null +++ b/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java @@ -0,0 +1,14 @@ +package liuxincourse; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ArrayListTest.class,LinkedListTest.class,QueueTest.class,StackTest.class}) +public class SuiteTest { + +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyArrayList.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyArrayList.java" new file mode 100644 index 0000000000..de63307566 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyArrayList.java" @@ -0,0 +1,136 @@ +package BasicData; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * 实现基本的数据结构——ArrayList + * + * @author Ralf + * + * @param + */ +public class MyArrayList implements MyList { + + private static final int DEFAULT_CAPACITY = 10; + private static int msize; + private T[] elements; + + public MyArrayList() { + msize = 0; + ensureCapacity(DEFAULT_CAPACITY); + } + + @SuppressWarnings("unchecked") + private void ensureCapacity(int newCapacity) { + // TODO Auto-generated method stub + if (msize > newCapacity) { + return; + } + T[] oldElements = elements; + elements = (T[]) new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + elements[i] = oldElements[i]; + } + + } + public void trimSize(){ + if (msize < elements.length) { + ensureCapacity(msize); + } + } + + @Override + public boolean add(T t) { + // TODO Auto-generated method stub + + if (elements.length == size()) { + ensureCapacity(2 * size() + 1); + } + elements[msize++] = t; + return true; + } + + @Override + public void add(int index, T t) { + + if (msize == elements.length) { + ensureCapacity(2 * msize + 1); + } + for (int i = size(); i >= index; i--) { + elements[i + 1] = elements[i]; + } + elements[index] = t; + msize++; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return msize; + } + + @Override + public T remove(int index) { + // TODO Auto-generated method stub + if (index < 0 || index > size()) { + throw new ArrayIndexOutOfBoundsException(); + } + T old = elements[index]; + for (int i = index; i < msize; i++) { + elements[i] = elements[i + 1]; + } + elements[msize--] = null; + return old; + + } + + @Override + public boolean set(int index, T t) { + // TODO Auto-generated method stub + if (index < 0 || index > size()) { + throw new ArrayIndexOutOfBoundsException(); + } + elements[index] = t; + return true; + } + + @Override + public T get(int index) { + // TODO Auto-generated method stub + if (index < 0 || index > msize) { + throw new ArrayIndexOutOfBoundsException(); + } + return elements[index]; + } + + @Override + public String toString() { + return Arrays.toString(elements); + } + + public MyIterator iterator() { + return new MyArrayListIterator(); + } + + private class MyArrayListIterator implements MyIterator { + + private int current = 0;// 迭代器的指针 + + public boolean hasNext() { + // TODO Auto-generated method stub + + return current < size(); + } + + public T Next() { + // TODO Auto-generated method stub + if (!hasNext()) { + throw new NoSuchElementException(); + } + return elements[current++]; + } + + } + +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyIterator.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyIterator.java" new file mode 100644 index 0000000000..42bf1d4fa1 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyIterator.java" @@ -0,0 +1,7 @@ +package BasicData; + +public interface MyIterator { + + public abstract boolean hasNext(); + public abstract T Next(); +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyList.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyList.java" new file mode 100644 index 0000000000..2b2ba5057a --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyList.java" @@ -0,0 +1,16 @@ +package BasicData; + +/** + * 创建自己的List接口 + * @author Ralf + * + */ +public interface MyList { + + public abstract boolean add(T t); + public abstract void add(int index, T t); + public abstract int size(); + public abstract T remove(int index); + public abstract boolean set(int index, T t); + public abstract T get(int index); +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyQueue.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyQueue.java" new file mode 100644 index 0000000000..772b93f879 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyQueue.java" @@ -0,0 +1,79 @@ +package BasicData; + +/** + * 实现基本数据结构Queue(环形) + * + * @author Administrator + * + */ +public class MyQueue { + + private int head; + private int tail; + private T[] elements; + private static final int DEFAUL_SIZE = 10; + private int numOfelements; + + public MyQueue() { + head = 0; + tail = 0; + numOfelements = 0; + setCapacity(DEFAUL_SIZE); + } + + public MyQueue(int capacity) { + head = 0; + tail = 0; + numOfelements = 0; + setCapacity(capacity); + + } + + @SuppressWarnings("unchecked") + private void setCapacity(int capacity) { + elements = (T[]) new Object[capacity]; + } + + public boolean enQueue(T t) { + + if (numOfelements == elements.length) { + return false; + } else { + elements[tail] = t; + numOfelements++; + if (tail == elements.length) + tail = 0; + else + tail++; + return true; + } + + } + + public T deQueue() { + if (head == tail) { + return null; + } else { + T t = elements[head]; + numOfelements--; + elements[head] = null; + if (head == elements.length) + head = 0; + else { + head++; + } + return t; + } + + } + + public boolean isEmpty() { + return numOfelements == 0; + } + + public int size() { + int msize = head - tail; + return msize > 0 ? msize : -msize; + } + +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyStack.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyStack.java" new file mode 100644 index 0000000000..fb12e0dd23 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyStack.java" @@ -0,0 +1,44 @@ +package BasicData; + +import java.util.LinkedList; +import java.util.NoSuchElementException; + +/** + * 实现基本数据结构栈 + * + * @author Ralf + * + */ +public class MyStack { + + private LinkedList linkedList; + + public MyStack() { + if (null == linkedList) { + linkedList = new LinkedList(); + } + } + + public void push(T t) { + linkedList.addFirst(t); + } + + public T pop() { + if (size() == 0) { + throw new NoSuchElementException(); + } + return linkedList.removeFirst(); + } + + public T peek() { + return (size() == 0) ? null : linkedList.getFirst(); + } + + public int size() { + return linkedList.size(); + } + + public boolean isEmpty(){ + return linkedList.isEmpty(); + } +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" new file mode 100644 index 0000000000..6ca5cecb15 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" @@ -0,0 +1,99 @@ +package BasicData; + +//insert 方法有问题 +public class MyTreeNode> { + + private T data; + private MyTreeNode left = null; + private MyTreeNode right = null; + private MyTreeNode root = null; + private MyTreeNode cureeTreeNode = null; + + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public MyTreeNode getLeft() { + return left; + } + public void setLeft(MyTreeNode left) { + this.left = left; + } + public MyTreeNode getRight() { + return right; + } + public void setRight(MyTreeNode right) { + this.right = right; + } + @Override + public String toString() { + StringBuilder string = new StringBuilder(); + string.append("["); + if (cureeTreeNode == null) { + string.append("]"); + return string.toString(); + } else { + string.append(cureeTreeNode.toString()).append("]"); + return string.toString(); + } + } + + public MyTreeNode insert(T o){ + MyTreeNode newNode = new MyTreeNode(); + MyTreeNode current = null; + newNode.setData(o); + if (root == null) { + root = newNode; + cureeTreeNode = newNode;//测试 + return newNode; + } + else { + Digui(o, root); + current = cureeTreeNode; + if (current.getData().compareTo(o) == -1) { + current.right = newNode; + } else { + current.left = newNode; + } + cureeTreeNode = newNode; //测试 + return newNode; + } + + } + + public void Digui(T o,MyTreeNode parentnode){ + cureeTreeNode = parentnode; + if (parentnode.left!= null) { + if (parentnode.getData().compareTo(o) == -1) { + parentnode = parentnode.right; + Digui(o, parentnode); + } + else + return; + } + if (parentnode.right != null) { + if (parentnode.getData().compareTo(o) == 1) { + parentnode = parentnode.left; + Digui(o, parentnode); + } + else + return; + } + } + + public void preOrder(MyTreeNode root) { + visit(root); + if(root.getLeft() != null) { + preOrder(root.getLeft()); + } + if(root.getRight() != null) { + preOrder(root.getRight()); + } + } + + public void visit(MyTreeNode btree) { + System.out.print(btree.getData() + "\t"); + } +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/SingleLinkedList.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/SingleLinkedList.java" new file mode 100644 index 0000000000..a9e72ed4fa --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/SingleLinkedList.java" @@ -0,0 +1,212 @@ +package BasicData; + +import java.util.NoSuchElementException; + +import javax.lang.model.element.Element; + +public class SingleLinkedList implements MyList { + + private int msize;// 记录元素的个数 + Note head;// 指向第一个元素 + Note last;// 指向最后一个元素 + + private static class Note { + private T item = null; + Note next = null; + + public Note(T t) { + this.item = t; + } + } + + public SingleLinkedList() { + + } + + @Override + public boolean add(T t) { + // TODO Auto-generated method stub + final Note l = last; + final Note newNote = new Note(t); + last = newNote; + if (l == null) { + head = newNote;// 只允许有一个null元素 + } else { + l.next = last; + } + msize++; + return true; + } + + @Override + public void add(int index, T t) { + // TODO Auto-generated method stub + if (index < 0 || index > msize) { + throw new IndexOutOfBoundsException(); + } + if (index == 0) { + addFirst(t); + } + if (index == msize) { + add(t); + } + Note current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + final Note newNote = new Note(t); + newNote.next = current.next; + current.next = newNote; + msize++; + + } + + public boolean addFirst(T t) { + final Note newNote = new Note(t); + newNote.next = head; + head = newNote; + msize++; + return true; + } + + public boolean addLast(T t) { + final Note newNote = new Note(t); + newNote.next = null; + last.next = newNote; + last = newNote; + msize++; + return true; + } + + public T removeLast() throws Exception { + if (head == null) { + throw new Exception("LinkedList is Empty!"); + } + Note current = head; + if (head.next == null) { + head = null; + last = null; + } else { + while (current.next != null) { + if (current.next == last) { + last = current; + last.next = null; + break; + } + current = current.next; + } + + } + msize--; + return current.item; + + } + + public T removeFirst() throws Exception { + if (head == null) { + throw new Exception("LinkedList is Empty!"); + } + Note element = head; + head = head.next; + msize--; + return element.item; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return msize; + } + + @Override + public T remove(int index) { + // TODO Auto-generated method stub + if (index < 0 || index > msize) { + throw new IndexOutOfBoundsException(); + } + Note element; + if (index == 0) { + element = head; + head = head.next; + msize--; + return element.item; + } + Note current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + element = current.next; + if (index == msize) { + current.next = null; + last = current; + } else { + current.next = current.next.next; + } + + msize--; + return element.item; + } + + @Override + public boolean set(int index, T t) { + // TODO Auto-generated method stub + if (index < 0 || index > msize) { + throw new IndexOutOfBoundsException(); + } + Note current = head; + for (int i = 0; i < index; i++) { + current = head.next; + } + current.item = t; + return true; + } + + @Override + public T get(int index) { + // TODO Auto-generated method stub + if (index < 0 || index > msize) { + throw new IndexOutOfBoundsException(); + } + Note current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + return current.item; + } + + public MyIterator iterator() { + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements MyIterator { + + private int current = 0; + private Note nextNote = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return current < msize; + } + + @Override + public T Next() { + // TODO Auto-generated method stub + if (!hasNext()) { + throw new NoSuchElementException(); + } else { + current++; + Note eleNote = nextNote; + if (last == nextNote) { + nextNote = null; + } else { + nextNote = nextNote.next; + } + return eleNote.item; + + } + + } + } + +} diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240\345\234\260\345\235\200" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240\345\234\260\345\235\200" new file mode 100644 index 0000000000..2ebe35c33a --- /dev/null +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240\345\234\260\345\235\200" @@ -0,0 +1 @@ +[鏂囩珷閾炬帴](http://blog.csdn.net/u011371324/article/details/57146892) diff --git a/group20/group20.md b/group20/group20.md index 8b13789179..d3f5a12faa 100644 --- a/group20/group20.md +++ b/group20/group20.md @@ -1 +1 @@ - + diff --git a/group21/group21.md b/group21/group21.md index 8b13789179..d3f5a12faa 100644 --- a/group21/group21.md +++ b/group21/group21.md @@ -1 +1 @@ - + diff --git a/liuxin/.classpath b/liuxin/.classpath index fceb4801b5..373dce4005 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -2,5 +2,6 @@ + diff --git a/liuxin/.gitignore b/liuxin/.gitignore index ae3c172604..3e2fcc7171 100644 --- a/liuxin/.gitignore +++ b/liuxin/.gitignore @@ -1 +1 @@ -/bin/ +/bin/ diff --git a/liuxin/.project b/liuxin/.project index fab8d7f04c..2858b5b710 100644 --- a/liuxin/.project +++ b/liuxin/.project @@ -1,17 +1,17 @@ - - - 2017Learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/.settings/org.eclipse.jdt.core.prefs b/liuxin/.settings/org.eclipse.jdt.core.prefs index 3a21537071..bb35fa0a87 100644 --- a/liuxin/.settings/org.eclipse.jdt.core.prefs +++ b/liuxin/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +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 +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/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 = [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鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 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, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + 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 diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/src/com/coding/basic/ArrayList.java index 1f185736f9..57412dcf7f 100644 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ b/liuxin/src/com/coding/basic/ArrayList.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index d7ac820192..266eff3d56 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/liuxin/src/com/coding/basic/Iterator.java b/liuxin/src/com/coding/basic/Iterator.java index 06ef6311b2..dbe8b9afb2 100644 --- a/liuxin/src/com/coding/basic/Iterator.java +++ b/liuxin/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java index e2c4e5e795..1fd99bf26b 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/src/com/coding/basic/LinkedList.java @@ -1,46 +1,46 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/liuxin/src/com/coding/basic/List.java b/liuxin/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/liuxin/src/com/coding/basic/List.java +++ b/liuxin/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/liuxin/src/com/coding/basic/Queue.java b/liuxin/src/com/coding/basic/Queue.java index 36e516e266..08d2d86b14 100644 --- a/liuxin/src/com/coding/basic/Queue.java +++ b/liuxin/src/com/coding/basic/Queue.java @@ -1,19 +1,19 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java index a5a04de76d..4bfe28057f 100644 --- a/liuxin/src/com/coding/basic/Stack.java +++ b/liuxin/src/com/coding/basic/Stack.java @@ -1,22 +1,22 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +}