diff --git a/.gitignore b/.gitignore index ec55baf87d..7f06ac1c28 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.class - # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -8,9 +7,25 @@ *.war *.ear +*.iml +*.idea + + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* #ide config .metadata .recommenders + + +#macOS +.DS_Store + +.idea/ +*.iml +rebel.* +.rebel.* + +target + 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��Ҫ�������Ϳ��ƵIJ��֣�����˼�壬���ǿ��Ƶ�Ԫ�����㵥Ԫ�����֣������˵Ĵ��Խ��м��������һ���� +�����Եļ����������һ��ģ����������ÿ�����ܵ�Ԫ���Ƿֹ���ȷ�ġ����п��Ƶ�Ԫ���ݳ���ͨ��ָ�����������㵥Ԫ����������ó������Ȼ����ƴ洢��Ԫ���洢�м�Ľ���� +�ڴ��Ǵ洢CPU��Ҫ���������ϣ���Ҫ�����У�����ϵͳ�е���ʱ���ݣ������Ӳ�̶�ȡ�����ݣ��ṩ��CPUʹ�á� +Ӳ���Ǹ���洢������Ҫ���ô洢�����ݣ��������ǵij�������ݣ��ŵ��Ƿdz��̶���������׼ȷ���������Դ洢����Ϣ��������ʱ��������� +�ڴ��ȡ���ݵ��ٶȱ�Ӳ�̵Ĵ�ȡ�ٶȿ���10���� ��ijЩ��������ܻ���� +CPU���ٶȱ��ڴ治֪Ҫ����ٱ��������ǰѳ����Ӳ�̷ŵ��ڴ��Ժ�CPU��ֱ�����ڴ����г���������CPUֱ����Ӳ�����г����Ҫ��ܶࡣ +�����ڴ�����һ����CPU���й��죬��Ӳ�����ݴ�ȡ̫�������⡣ ��������ǵĵ��Ե������ٶȡ� +���������г����ʱ��CPU���Ƚ��ܵ����ǵ����֮��CPU�Ǹ���Ӳ�̣���Ҫ������洢�ij���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 + * 若b为空树,插入节点作为根 + * 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("元素不可为NULL"); + 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;//当前节点的前驱改为null + current.prev = null;//当前节点的后继改为null + 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;//当前节点的前驱改为null + current.prev = null;//当前节点的后继改为null + 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: 获取集合的迭代器 + * @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/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 和Iterator +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 前两步的逻辑和get方法相同 + 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的值小于root的值,则插入到root的左边。 + * 3. insert的值大于root的值,则插入到root的右边。 + * PS:目前只支持Integer类型的对象插入 + * @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的值小于root的值,则插入到root的左边。 + //如果左节点有值,则递归 + if(-1 == result){ + if(node.getLeft() != null){ + insert(node.getLeft(), insertNode); + }else{ + node.setLeft(insertNode); + } + } + //insert的值大于root的值,则插入到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; + // ���еij��� + private int size; + + /** + * ���ض��еij��� + * + * @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从内存读取指令,有pc指定位置,放入指令寄存器; + 2,根据读取到的指令,由运算器执行加减命令, + + 1.程序在cpu,内存和硬盘运行的速度依次递减,由快到慢,同样的他们价格成本由高到底;存储容量也是由大到小; + + 2,cpu和内存中的数据断电后就会消失,硬盘不会消失,所以要把需要长期使用的程序放在硬盘中,程序是由指令组成的, + 当开机后,先加载需要的数据从硬盘中读取放到内存中,有cpu从内存中读取指令,要执行的指令和执行后的结果放入cpu的 + 寄存器中;就相当于原料进机器之前排队一样,把半成品也要在这里放一下,再进去加工,它只是一个临时中转站(寄存器); + + 3.CPU从内存中取出指令,放入指令寄存器,并对指令译码操作控制器,操作指令,指令在cpu中暂存在寄存器中; + 但是,cpu不能直接操作硬盘,所以需要内存,要把硬盘中的数据先加载到内存;内存相当于中转站,内存的速度决定电脑程序运行的速度; + + 有的网络文章形象比喻:“CPU是工厂,硬盘是大仓库,内存是正规中转中心 \ 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 @@ +#指令集 +---- +通俗的理解,指令集就是CPU能认识的语言,指令集运行于一定的微架构之上.指令集有很多,比如复杂指令集AMD,INTEL用的,精简指令集ARM用的 + +# CPU +--- +CPU使整个计算机的核心,进行整体的逻辑操作,他的运算速度很快,我们在主机上的操作最终都会变成原子指令由CPU来执行 + +历史上的CPU对程序的执行存在很多的执行方案,比如说时间片法,令牌环之类的的,每个程序依次交换执行,这样就存在一个问题,程序的换入换出需要保留当前执行情况的现场,还要加载历史程序的历史现场 + +这些数据存储在哪里?由于CPU的速度过快,而我们是用的大规模的数据存储一般是价格较低速度较慢的磁盘.如果说CPU直接从磁盘加载,CPU的资源利用率会变的很低,会严重浪费了CPU的运算能力.因此我们需要缓存 + +# 缓存 +--- +缓存的出是为加载相关的数据提供CPU路基运算的存储,CPU中的寄存器就是一种缓存,这是CPU本身对操作的优化.但是说这样高速的缓存本身的制造成本是非常高的,完全使用这样高性能的存储设备并不是一个具有性价比的解决方案 + +因此出现了多级缓存,常见的如INTEL的L1,L2,L3,这是多级缓存的一种实现,随着层级的升高,存储空间在升高,速度在下将,通过诸如LFU、LRU的缓存置换算法优化相关的缓存命中率提高CPU利用率 + +理解 Cache --> 传送门 `http://www.mouseos.com/arch/cache.html` + +即便是L3的缓存也以就是十分昂贵的,依旧是很难大规模使用的存储设备,因此需要速度相对较快,但是价格又是普通用户可以接收的中间设备,比如内存 + +# 内存 +--- +内存的存在实际上解决了很多问题,他的价格相对高速缓存来说相对较低,能够提供较大的存储,也可以看作缓存的一种,将磁盘的数据预加载,供高速缓存加载. + +内存有很多的工作频率,内存主频越高在一定程度上代表着内存所能达到的速度越快,计算机系统的时钟速度是以频率来衡量的.晶体振荡器控制着时钟速度,在石英晶片上加上电压,其就以正弦波的形式震动起来,这一震动可以通过晶片的形变和大小记录下来。晶体的震动以正弦调和变化的电流的形式表现出来,这一变化的电流就是时钟信号. + +内存是一种断电后存储洗洗就会丢失的存储设备,这就尴尬了,对于宕机我们并无法预测,我们需要将众多的资源数据进行存储,这个时候我们需要硬盘. + +# 硬盘 +--- +硬盘对于我们而言他的速度反而是其次的,他最大的意义是数据的安全性.在数据安全的基础之上我们再去追求硬盘的速度. + +硬盘有很多比如说机械硬盘,固态硬盘,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; + +/** + * 更符合Queue语义的方法 + * + * @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; + +/** + * + * 更符合Stack语义的方法 + * + * @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; + + /** + * 节点查找,找到返回节点信息,找不到返回null + */ + 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 | 代码目录| + +### 代码说明 + +> 作业在homework目录下 + + + 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)的缩写,也叫处理器,是计算机的运算核心和控制核心。人靠大脑思考,电脑靠CPU来运算、控制。让电脑的各个部件顺利工作,起到协调和控制作用。 + +内存:1. 负责硬盘等硬件上的数据与CPU之间数据交换处理;2. 缓存系统中的临时数据。3. 断电后数据丢失。 +硬盘:存储资料和软件等数据的设备,有容量大,断电数据不丢失的特点。也被人们称之为“数据仓库”。 + +现在我们来说一下CPU、硬盘、内存三者之间的关系。 + +首先 ,我们先回想一下三者的作用: + +CPU:是计算机的运算核心和控制核心,让电脑的各个部件顺利工作,起到协调和控制作用。 +硬盘:存储资料和软件等数据的设备,有容量大,断电数据不丢失的特点。也被人们称之为“数据仓库”。 +内存:1. 负责硬盘等硬件上的数据与CPU之间数据交换处理;2. 缓存系统中的临时数据。3. 断电后数据丢失。 + +然后, 我们再来看一下程序是如何执行起来的。 + +当我们在电脑上打开QQ时(右键-打开 或者双击QQ图标),其实是通过鼠标(输入设备)向CPU发送了一条命令,CPU接收到这条命令后,QQ程序就从硬盘里被加载到内存(加载时不通过处理器,直接从硬盘加载程序到内存里),加载完成后,CPU就开始执行QQ程序。程序执行起来后,CPU可以让QQ程序显示在我们的在显示器上。也就是你看到了QQ 程序运行起来了。如果这个时候,你用QQ截取了一张屏幕的图片,那么这张图片会首先保存到内存,在没有退出截屏状态时,你可以在这张图片上写字、画线条,等你右键保存这张图片的时候,这张图片就会保存到硬盘里。 + +通过了解一个程序是如何运行起来的,我们就可以了解三者是如何工作的 。 + +可能有些人会不明白,如果程序是这样执行起来的话,那么为什么CPU不直接在硬盘里执行程序,而非要把程序放到内存后在执行呢? + +因为速度差别太大: +内存存取数据的速度比硬盘的存取速度快了10倍, 在某些环境里,硬盘和内存之间的速度差距可能会更大。 + +而CPU的速度比内存不知还要快多少倍。当我们把程序从硬盘放到内存以后,CPU就直接在内存运行程序,这样比CPU直接在硬盘运行程序就要快很多。 + +内存解决了一部分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 + +如果一次运算需要一个逻辑电路,那么这就需要2*10000000个逻辑电路, + +额,恕我直言,这个成本有点高, + +于是人们想到了一个好办法,就是做一个逻辑器件,有2个输入,分别是数据输入,指令输入,数据输入就是输入要运算的两个数据,指令输入输入让这个器件干什么,比如00就是做加法,01就是做减法,10是乘法,11是除法,然后给他配个储存设备用来保存数据,这样我们让这一个设备不断的运行,按照一定规则把输出的数据送回输入就能完成上面提到的运算了. + +后来这个设备被称为CPU, + +CPU除了运算逻辑以外,还能执行: 1 移动数据 2 逻辑运算 3 跳转指令 还有些杂七杂八的指令 + +移动数据就是把输出的数据送回输入的指令,通常数据存储在寄存器或者RAM中,储存器就像游泳馆的柜子,上面有个编号,里面存了个数字.后来编号被程序猿称为地址. + +有了这些我们还有一件事没做,想想是什么? + +就是告诉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); + } + + /** + * 解除传入节点的属性,并且将传入节点的上一个和下一个节点 链接。使传入节点的属性 全部为 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 在前半区间还是后半区间。而不是一直从头到尾搜索 + * 将节点访问复杂度从O(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是否为null + if (l == null) { + //链表first指向新添加的 节点 + first = newNode; + } else { + //链表last不为null将链表last节点的的next设置为新节点 + l.next = newNode; + } + size++; + modCount++; + } + + /** + * 检查index是否越界 + */ + 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不仅是ArrayList中实际存值得体现,直接add时,也是在elementData[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(); + } + + // 检测索引值,当前有size个元素,占了elementData[0]至elementData[size-1],add时,只能在已有[0,size-1]处插入,或者在列表尾size处add + // 所以index在[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 @@ +# 测试上传文件到git +## 第一次上传以作学习 +## 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是计算机的大脑,由运算器和寄存器组成。可以储存指令和数据,并执行指令进行简单的计算。 +指令是CPU可以直接执行的命令。 +内存是所谓的RAM,读写速度较快。常用来作为CPU和硬盘之间数据交换的缓存。 +硬盘容量极大,但是读写速度比内存慢很多。用来存放程序的二进制码,音频,视频等等数据资源。 +程序运行时,数据由硬盘拷贝到内存,CPU再从内存里加载包含指令的数据,逐一执行。 \ 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/group16/1012075117/DataStructure219/.classpath b/group16/1012075117/DataStructure219/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group16/1012075117/DataStructure219/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1012075117/DataStructure219/.project b/group16/1012075117/DataStructure219/.project new file mode 100644 index 0000000000..567baae65f --- /dev/null +++ b/group16/1012075117/DataStructure219/.project @@ -0,0 +1,17 @@ + + + DataStructure219 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs b/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java new file mode 100644 index 0000000000..a1d46a21d8 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/ArrayList.java @@ -0,0 +1,94 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class ArrayList implements List { + + private int flag = -1; + private static final int DEFAULT_CAPACITY = 1; + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + @Override + public void add(Object element) { + // 当要添加数据的位置已经超过数组长度时,增长数组长度 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[flag + 1] = element; + flag++; + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > getFlag() + 1) { + System.out.println("在--" + index + "--添加的--" + element + "--无效,因为越界了!"); + return; + } + // 数组长度永远比已存数据大一个。 + if (size() + 1 == elementData.length) { + grow(); + } + elementData[index] = element; + if (index > getFlag()) { + flag++; + } + } + + @Override + public Object get(int index) { + if (index < 0 || index > getFlag()) { + System.out.print("在--" + index + "--的get无效,因为越界了!"); + return null; + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > getFlag()) { + System.out.println("在--" + index + "--的remove无效,因为越界了!"); + return null; + } + Object oldValue = elementData[index]; + elementData[index] = null; + // 将删除处后面的数据往前移一格。 + Object[] data2 = new Object[elementData.length - 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag()); + elementData = data2; + flag--; + return oldValue; + } + + @Override + public int size() { + return getFlag() + 1; + } + + public int getFlag() { + return flag; + } + + private void grow() { + Object[] data2 = new Object[elementData.length + 1]; + System.arraycopy(elementData, 0, data2, 0, getFlag() + 2);// 最后一个参数是需要复制的数据的数量。 + elementData = data2; + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + ArrayList al = new ArrayList(); + al.add(0, 99); + al.add(1, 100); + System.out.println(al.get(1)); + al.remove(1); + System.out.println(al.get(1)); + System.out.println(al.size()); + } +} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java new file mode 100644 index 0000000000..a1c728f0a1 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/LinkedList.java @@ -0,0 +1,194 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class LinkedList implements List { + + private Node head = null; + private Node last = null; + private int size = 0; + + private static class Node { + Object item; + Node prev; + Node next; + + public Node(Node prev, Object item, Node next) { + this.prev = prev; + this.item = item; + this.next = next; + } + } + + @Override + public void add(Object element) { + addLast(element); + } + + @Override + public void add(int index, Object element) { + if (index < 0 || index > size) { + System.out.println("操作无效,越界了"); + return; + } + if (index == 0) { + addFirst(element); + return; + } + if (index == size) { + addLast(element); + return; + } + Node indexNode = node(index); + Node newNode = new Node(indexNode.prev, element, indexNode); + indexNode.prev.next = newNode; + indexNode.prev = newNode; + size++; + } + + @Override + public Object get(int index) { + if (index < 0 || index >= size) { + System.out.println("查询无效,越界了"); + return null; + } + if (index == 0) { + return head.item; + } + return node(index).item; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > size) { + System.out.println("是空的,无法删除"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node x = node(index); + final Object element = x.item; + final Node next = x.next; + final Node prev = x.prev; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.item = null; + size--; + return element; + } + + @Override + public int size() { + return size; + } + + private void addFirst(Object element) { + final Node f = head; + Node newNode = new Node(null, element, f); + head = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object element) { + if (head == null) { + addFirst(element); + } else { + Node newNode = new Node(last, element, null); + last.next = newNode; + last = newNode; + size++; + } + } + + public Object removeFirst() { + if (head == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + Node x = head; + Node next = head.next; + Object element = x.item; + x.item = null; + x.next = null; + head = next; + if (next == null) + last = null; + else + x.prev = null; + size--; + return element; + } + } + + public Object removeLast() { + if (last == null) { + System.out.println("是空的,无法删除"); + return null; + } else { + final Node l = last; + final Object element = l.item; + final Node p = l.prev; + l.item = null; + l.prev = null; + last = p; + if (p == null) + head = null; + else + p.next = null; + size--; + return element; + } + } + + Node node(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + /** + * 测试用例 + * + * @param args + */ + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.add(0, "xxx"); + ll.add(1, 111); + System.out.println(ll.size()); + System.out.println(ll.get(2)); + + } +} \ No newline at end of file diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java new file mode 100644 index 0000000000..5226796141 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/List.java @@ -0,0 +1,13 @@ +package com.stackwei.DataStructure; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java new file mode 100644 index 0000000000..4a227495e9 --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Queue.java @@ -0,0 +1,52 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class Queue { + + private LinkedList ll = new LinkedList(); + + /** + * 在队尾添加数据 + * @param element + */ + public void enQueue(Object element) { + ll.addLast(element); + } + + /** + * 删除队头数据 + * @return + */ + public Object deQueue() { + return ll.removeFirst(); + } + + /** + * 队列是否为空 + * @return + */ + public boolean isEmpty() { + if (ll.size() > 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Queue q = new Queue(); + q.enQueue(97); + q.enQueue(98); + q.enQueue(99); + System.out.println(q.isEmpty()); + System.out.println(q.deQueue()); + } + +} diff --git a/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java new file mode 100644 index 0000000000..1b047ffafd --- /dev/null +++ b/group16/1012075117/DataStructure219/src/com/stackwei/DataStructure/Stack.java @@ -0,0 +1,59 @@ +package com.stackwei.DataStructure; + +/** + * + * @author stackwei -2017.2.25 + * + */ +public class Stack { + + private ArrayList al = new ArrayList(); + + /** + * 进栈 + * @param item + */ + public void push(Object item) { + al.add(item); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + return al.remove(al.getFlag()); + } + + /** + * 获取栈顶元素 + * @return + */ + public Object peek() { + return al.get(al.getFlag()); + } + + /** + * 栈是否为空 + * @return + */ + public boolean isEmpty() { + if (al.getFlag() >= 0) { + return false; + } + return true; + } + + /** + * 测试用例 + * @param args + */ + public static void main(String[] args) { + Stack s = new Stack(); + s.push(98); + s.push(99); + s.pop(); + System.out.println(s.peek()); + } + +} diff --git a/group16/1154151360/.classpath b/group16/1154151360/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/1154151360/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1154151360/.gitignore b/group16/1154151360/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/1154151360/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/1154151360/.project b/group16/1154151360/.project new file mode 100644 index 0000000000..f88388f6d7 --- /dev/null +++ b/group16/1154151360/.project @@ -0,0 +1,17 @@ + + + DataStructure2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1154151360/.settings/org.eclipse.core.resources.prefs b/group16/1154151360/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group16/1154151360/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group16/1154151360/src/com/list/ArrayList.java b/group16/1154151360/src/com/list/ArrayList.java new file mode 100644 index 0000000000..733dc0f341 --- /dev/null +++ b/group16/1154151360/src/com/list/ArrayList.java @@ -0,0 +1,93 @@ +package com.list; + + +//ArrayList +public class ArrayList { + + private int size; + + private Object [] elementData = new Object[10]; + + public boolean add(Object data){ + + getRow(size+1); + elementData[size++] = data; + return true; + } + + public boolean add(int index, Object data){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("�±�Խ��"); + } + getRow(size + 1); + Object object = elementData [index]; + System.arraycopy(elementData, index,elementData , index + 1,size - index ); + elementData[index] = data; + size++; + return true; + } + + public Object get(int index){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("下标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("下标越界"); + } + Object object = elementData [index]; + System.arraycopy(elementData, index + 1 , elementData, index, size - 1 - index); + elementData[size--]= null; + return object; + } + + + private void getRow(int num){ + if (num > elementData.length ){ + int oldLength = elementData.length; + int newLength = ((num + elementData.length) * 3) >> 2; + Object [] oldelements = elementData; + elementData = new Object[newLength]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + public int size(){ + return size; + } + + public int length(){ + return elementData.length; + } + public static void main(String[] args) { + ArrayList list = new ArrayList(); + + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add("E"); + list.add("F"); + list.add("G"); + list.add("H"); + list.add("I"); + list.add("J"); + list.add("K"); + list.add("L"); + list.add(2, 1); + System.out.println("elementsData.Length: "+list.length()); + System.out.println("elementsData.size: "+list.size()); + for (int i = 0; i < list.size; i++){ + System.out.print(list.get(i)+ " "); + } + System.out.println(" "); + list.remove(2); + + for (int i = 0; i < list.size; i++){ + System.out.print(list.get(i)+ " "); + } + } +} diff --git a/group16/1154151360/src/com/list/LinkedList.java b/group16/1154151360/src/com/list/LinkedList.java new file mode 100644 index 0000000000..46f61fb667 --- /dev/null +++ b/group16/1154151360/src/com/list/LinkedList.java @@ -0,0 +1,158 @@ +package com.list; + +public class LinkedList { + + private int size; + + private Node head;//头节点 + + Node current; //当前节点 + + public LinkedList(){ + this.head = current = new Node(null); + this.size = 0; + } + + + private boolean add(Object object){ + addAfter(object); + size++; + return true; + } + + + private boolean add(int index,Object object) throws Exception{ + index(index - 1); + current.nextNode = new Node(object,current.nextNode.nextNode); + size++; + return true; + } + + + private boolean addFirst(Object object){ + Node node = new Node(object,null); + current = head.nextNode; + head.nextNode = node; + node.nextNode = current; + size++; + return true; + } + + + private boolean addLast(Object object){ + add(object); + return true; + } + + + private Object get(int index) throws Exception{ + index(index); + return current.object; + } + + + private Object remove(int index) throws Exception{ + + if (index == size - 1){ + Object object = removeLast(); + return object; + } + index(index - 1); + Object object = current.nextNode.object; + + current.nextNode = current.nextNode.nextNode; + size--; + return object; + } + + private Object removeFirst(){ + Object object = null; + if (size > 0){ + current = head.nextNode; + object = current.object; + head.nextNode = head.nextNode.nextNode; + size--; + } + return object; + } + + private Object removeLast() throws Exception{ + Object object = null; + if (size > 0){ + int j = 0; + current = head.nextNode; + + while (current != null){ + current = current.nextNode; + j++; + } + index(j - 1); + object = current.nextNode.object; + current.nextNode = null; + size--; + } + return object; + } + private void index (int index) throws Exception{ + + if (index < -1 || index > size){ + + throw new Exception(" "); + } + + if (index == -1){ + return; + } + current = head.nextNode; + int j = 0; + while (current != null && j < index){ + current = current.nextNode; + j++; + } + } + + + private void addAfter(Object object){ + + if (head.nextNode == null){ + + Node newNode = new Node(object,null); + }else{ + current = head.nextNode; + while (current.nextNode == null){ + current = current.nextNode; + } + current.setNode(new Node(object,null)); + } + + + } + + + + + private static class Node{ + + Object object; + + Node nextNode; + + + Node (Node nextNode){ + this.nextNode = nextNode; + } + + + Node (Object object, Node nextNode){ + this.nextNode = nextNode; + this.object = object; + } + + private void setNode(Node node){ + this.nextNode = node; + } + + } + + +} diff --git a/group16/1154151360/src/com/list/Queue.java b/group16/1154151360/src/com/list/Queue.java new file mode 100644 index 0000000000..faa3e87381 --- /dev/null +++ b/group16/1154151360/src/com/list/Queue.java @@ -0,0 +1,51 @@ +package com.list; +//队列 +public class Queue { + + Object [] element; + + private static int DEFAULT_SIZE = 10; + + int front;//头指针 + + int rear;//尾指针 + + public Queue(){ + this(DEFAULT_SIZE); + } + public Queue(int size){ + element = new Object[size]; + this.front = 0; + this.rear = 0; + } + + public boolean enQueue(Object object){ + + if ((rear + 1) % element.length == front){ + return false; + }else{ + element[rear] = object; + rear = (rear + 1) % element.length; + return true; + } + } + + public Object deQueue(){ + if (front == rear){ + return null; + }else{ + Object object = element[front]; + front = (front + 1) % element.length; + return object; + } + + } + + public int size(){ + return (rear -front) & (element.length - 1); + } + + public boolean isEmpty(){ + return rear == front; + } +} diff --git a/group16/1154151360/src/com/list/Stack.java b/group16/1154151360/src/com/list/Stack.java new file mode 100644 index 0000000000..f92a9e731c --- /dev/null +++ b/group16/1154151360/src/com/list/Stack.java @@ -0,0 +1,35 @@ +package com.list; + +import java.util.ArrayList; +import java.util.EmptyStackException; + +public class Stack { + + ArrayList elelmentData = new ArrayList(); + + //压入栈 + public void push(Object object){ + elelmentData.add(object); + } + + //弹出栈 + public Object pop(){ + if (isEmpty()){ throw new EmptyStackException();} + return elelmentData.remove(elelmentData.size() - 1); + } + + //取栈顶元素 + public Object peek(){ + if (isEmpty()){return null;} + return elelmentData.get(elelmentData.size() - 1); + } + + public boolean isEmpty(){ + return elelmentData.isEmpty(); + } + + public int size(){ + if (isEmpty()){throw new EmptyStackException();} + return elelmentData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e287419dc0 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,63 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // ���� + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ��ǰ���鳤�� + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ��ǰ���鳤�ȵ�1.5�� + if (newCapacity - num < 0) { + newCapacity = num; // �����������Dz���,ֱ����Ϊ����ֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // �±�Խ���ж� + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..99c92fb9f1 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,101 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //��ȡ�ڵ� + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..95dee3d81b --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..c0b7da89f8 --- /dev/null +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1325756593/.classpath b/group16/1325756593/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group16/1325756593/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/1325756593/.gitignore b/group16/1325756593/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/1325756593/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/1325756593/.project b/group16/1325756593/.project new file mode 100644 index 0000000000..e3d07b6899 --- /dev/null +++ b/group16/1325756593/.project @@ -0,0 +1,17 @@ + + + DongqiHomeWork + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1325756593/dongqihust.readme b/group16/1325756593/dongqihust.readme new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group16/1325756593/src/com/dong/week1/ArrayList.java b/group16/1325756593/src/com/dong/week1/ArrayList.java new file mode 100644 index 0000000000..a700aecfb5 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/ArrayList.java @@ -0,0 +1,109 @@ +package com.dong.week1; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + /** + * ���ж������Ƿ��Ѿ����ˣ�����Ѿ����������ݴ��� + */ + if(elementData.length==size){ + elementData = Arrays.copyOf(elementData, elementData.length*2+1); + } + elementData[size++]=o; + + } + public void add(int index, Object o){ + if(index >size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("����Խ��,��ǰ���鳤����"+size+",��������Ԫ�ص�������:"+index); + } + /** + * �����Ѿ����ˣ������� + */ + if(size==elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length*2+1); + } + Object[] elementDataClone = elementData.clone(); + System.arraycopy(elementData, index, elementDataClone, index+1, size-index); + elementDataClone[index++]=o; + size++; + elementData = elementDataClone; + } + + + public Object get(int index){ + if(index >=size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("����Խ��,��ǰ���鳤����"+size+",��������Ԫ�ص�������:"+index); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >=size || index < 0 ){ + throw new ArrayIndexOutOfBoundsException("����Խ��,��ǰ���鳤����"+size+",����ɾ��Ԫ�ص�������:"+index); + } + elementData[index]=null; + size--; + Object[] elementDataClone = elementData.clone(); + System.arraycopy(elementData, index+1, elementDataClone, index, size-index-1); + elementData = elementDataClone; + return elementData[index]; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new IteratorArrayList(this); + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + + private class IteratorArrayList implements Iterator{ + + private ArrayList arrayList; + private int index=0; + + + public IteratorArrayList(ArrayList arrayList) { + super(); + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.arrayList.size() >index; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(hasNext()){ + return this.arrayList.get(index++); + } + return null; + } + + } + + public static void main(String[] args) { + ArrayList arrayList= new ArrayList(); + + Iterator iterator= arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + +} diff --git a/group16/1325756593/src/com/dong/week1/ArrayListTest.java b/group16/1325756593/src/com/dong/week1/ArrayListTest.java new file mode 100644 index 0000000000..128130cfb2 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/ArrayListTest.java @@ -0,0 +1,63 @@ +package com.dong.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + //@Test + public void testAddObject() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=200;i++){ + arrayList.add(i); + } + System.out.println(arrayList); + } + + //@Test + public void testAddIntObject() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=2;i++){ + arrayList.add(i); + } + arrayList.add(1,100); + arrayList.add(1, 1000); + System.out.println(arrayList); + } + +// @Test + public void testGet() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=200;i++){ + arrayList.add(i); + } + //System.out.println(arrayList.get(-1)); + //System.out.println(arrayList.get(50)); + System.out.println(arrayList.get(200)); + //System.out.println(arrayList.get(300)); + + + } + + @Test + public void testRemove() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=10;i++){ + arrayList.add(i); + } + arrayList.remove(1); + arrayList.remove(1); + System.out.println(arrayList); + } + +// @Test + public void testSize() { + ArrayList arrayList = new ArrayList(); + for(int i=0;i<=10;i++){ + arrayList.add(i); + } + System.out.println(arrayList.size()); + } + +} diff --git a/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java b/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..ef41ced8df --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/BinaryTreeNode.java @@ -0,0 +1,39 @@ +package com.dong.week1; + +public class BinaryTreeNode { + private TreeNode node; + + private static class TreeNode{ + private int key=0; + private TreeNode leftChild=null; + private TreeNode rightChild=null; + + public TreeNode(){} + + /** + * @param key ������� + * @param data ������ + */ + public TreeNode(int key){ + this.key=key; + this.leftChild=null; + this.rightChild=null; + } + + + } + + + + public TreeNode insert(TreeNode o){ + if(node == null){ + return o; + } + if(node.key > o.key){ + return insert(o.leftChild); + }else{ + return insert(node.leftChild); + } + } + +} diff --git a/group16/1325756593/src/com/dong/week1/Iterator.java b/group16/1325756593/src/com/dong/week1/Iterator.java new file mode 100644 index 0000000000..9147ab8264 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Iterator.java @@ -0,0 +1,7 @@ +package com.dong.week1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/1325756593/src/com/dong/week1/LinkedList.java b/group16/1325756593/src/com/dong/week1/LinkedList.java new file mode 100644 index 0000000000..339b5862ee --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/LinkedList.java @@ -0,0 +1,208 @@ +package com.dong.week1; + +public class LinkedList implements List { + + private int size = 0; + private Node head; + private Node tail; + + + public void add(Object o){ + //����һ��Ԫ�ص��߼��dz��򵥣�ֻ��Ҫ�ж�head�Ƿ�Ϊ�ա���Ϊ�գ�����ֱ�ӼӼ��� + Node node = new Node(o,null); + if(head ==null){ + head =node; + }else{ + tail.next=node; + } + tail=node; + size++; + + } + public void add(int index , Object o){ + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("index����Ϊ����"); + } + if(index > size){ + throw new ArrayIndexOutOfBoundsException("��ǰlist����Ϊ"+size+",��ȡ�����ǣ�"+index); + } + if(size==0){ + head=new Node(o, null); + tail=head; + return; + } + if(index==0){ + Node curNode =head; + Node newNode =new Node(o, curNode); + head=newNode; + return; + } + Node curNode =head; + Object retVal = null; + for(int i=0;i= size){ + throw new ArrayIndexOutOfBoundsException("��ǰlist����Ϊ"+size+",�Ƴ������ǣ�"+index); + } + Node curNode = head; + for(int i=0;i= size){ + throw new ArrayIndexOutOfBoundsException("��ǰlist����Ϊ"+size+",��ȡ�����ǣ�"+index); + } + Object retVal = null; + if(index==0){ + retVal =head.data; + head=head.next; + return retVal; + } + Node curNode =head; + + for(int i=0;iindex; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(hasNext()){ + return this.list.get(index++); + } + return null; + } +} + public static void main(String[] args) { + + LinkedList arrayList= new LinkedList(); + + Iterator iterator= arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + +} + +} diff --git a/group16/1325756593/src/com/dong/week1/List.java b/group16/1325756593/src/com/dong/week1/List.java new file mode 100644 index 0000000000..adc694241a --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/List.java @@ -0,0 +1,9 @@ +package com.dong.week1; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1325756593/src/com/dong/week1/Queue.java b/group16/1325756593/src/com/dong/week1/Queue.java new file mode 100644 index 0000000000..445390a168 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Queue.java @@ -0,0 +1,24 @@ +package com.dong.week1; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(elementData.size(), o); + } + + public Object deQueue(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("����Ϊ��"); + } + return elementData.remove(0); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1325756593/src/com/dong/week1/Stack.java b/group16/1325756593/src/com/dong/week1/Stack.java new file mode 100644 index 0000000000..1dea6cdfd9 --- /dev/null +++ b/group16/1325756593/src/com/dong/week1/Stack.java @@ -0,0 +1,31 @@ +package com.dong.week1; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + + public Object pop(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("ջΪ��"); + } + return elementData.remove(elementData.size()-1); + + } + + public Object peek(){ + if(elementData.size()==0){ + throw new IndexOutOfBoundsException("ջΪ��"); + } + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1924332561/.classpath b/group16/1924332561/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/1924332561/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/1924332561/.project b/group16/1924332561/.project new file mode 100644 index 0000000000..5f960262bc --- /dev/null +++ b/group16/1924332561/.project @@ -0,0 +1,17 @@ + + + 1924332561Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/1924332561/.settings/org.eclipse.core.resources.prefs b/group16/1924332561/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group16/1924332561/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group16/1924332561/src/com/coding/basic/ArrayList.java b/group16/1924332561/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3fc0960e08 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/ArrayList.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class ArrayList implements List{ + private int size = 0; + + private Object[] elementDate = new Object[100]; + + + @Override + public void add(Object o) { + + this.elementDate[this.size]=o; + this.size++; + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int dex) { + + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java b/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e7d9e43e24 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class BinaryTreeNode { + private Object date; + private BinaryTreeNode left; + private BinaryTreeNode right; + public Object getDate() { + return date; + } + public void setDate(Object date) { + this.date = date; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } +} diff --git a/group16/1924332561/src/com/coding/basic/Iterator.java b/group16/1924332561/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1924332561/src/com/coding/basic/LinkedList.java b/group16/1924332561/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d39557be29 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/LinkedList.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private static class Node{ + Object date; + Node next; + } + + private Node head; + + + @Override + public void add(Object o) { + + } + + @Override + public void add(int index, Object o) { + + } + + @Override + public Object get(int dex) { + return null; + } + + @Override + public Object remove(int index) { + return null; + } + + @Override + public int size() { + + return 0; + } + + public void addFirst(Object o){ + + } + + public void addLast(Object o){ + + } + + public Object removeFirst(){ + + return null; + } + + public Object removeLast(){ + + return null; + } + + public Iterator iterator(){ + + return null; + } + +} diff --git a/group16/1924332561/src/com/coding/basic/List.java b/group16/1924332561/src/com/coding/basic/List.java new file mode 100644 index 0000000000..d48b1f4827 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index,Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1924332561/src/com/coding/basic/Queue.java b/group16/1924332561/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..7dc7b4820a --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(){ + + + } + public Object deQueue(){ + + return null; + } + + public boolean isEmpty(){ + + return false; + } + + public int size(){ + + return -1; + } +} diff --git a/group16/1924332561/src/com/coding/basic/Stack.java b/group16/1924332561/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7a49c48279 --- /dev/null +++ b/group16/1924332561/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementDate = new ArrayList(); + + public void push(Object o){ + + } + + public Object pop(){ + + return null; + } + + public Object peek(){ + + return null; + } + + public boolean isEmpty(){ + + return false; + } + + public int size(){ + + return -1; + } +} diff --git a/group16/214074094/readme.txt b/group16/214074094/readme.txt new file mode 100644 index 0000000000..c1b06ddcc2 --- /dev/null +++ b/group16/214074094/readme.txt @@ -0,0 +1 @@ +I am 北京-Shane diff --git a/group16/214074094/src/com/coding/basic/ArrayList.java b/group16/214074094/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..158c866d45 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/ArrayList.java @@ -0,0 +1,165 @@ +package coding.basic; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * @Author shane + * @Time 2017/2/25 13:06 + * @Email stevenchenguang@gmail.com + * @Desc OwnArrayList + */ +public class ArrayList implements List { + + private int size = 0; + + private final static Object[] EMPTY_ELEMENTDATA = {}; + + /** + * 默认容量 + */ + private static int DEFAULT_CAPACITY = 10; + + private Object[] elementData; + + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + @Override + public void add(Object o) { + if (elementData == EMPTY_ELEMENTDATA) { + elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY); + elementData[0] = o; + } else if (size < elementData.length) { + elementData[size] = o; + } else { + _grow(); + elementData[size] = o; + } + size++; + _analyze(); + } + + @Override + public void add(int index, Object o) { + if (index < 0) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + if (elementData == EMPTY_ELEMENTDATA) { + if (index != 0) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } else { + elementData = new Object[DEFAULT_CAPACITY]; + elementData[0] = o; + } + } else if (index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } else if (index == size) { + _grow(); + elementData[size] = o; + size++; + } else { + if (elementData.length == size) { + _grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + _analyze(); + } + + @Override + public Object get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + Object oldValue = elementData[index]; + //需要复制的长度 + int needMoveLength = size - index - 1; + //如果该长度小于0, 说明只有一个元素, 直接置空即可 + if (needMoveLength > 0) { + System.arraycopy(elementData, index + 1, elementData, index, needMoveLength); + } + elementData[--size] = null; + _analyze(); + return oldValue; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListItrator(); + } + + /** + * @Author: shane + * @Time: 2017/2/25 20:18 + * @Email: stevenchenguang@gmail.com + * @param: + * @Return: + * @Throw: + * @Desc: 返回真实长度的数组数据 + */ + private void _analyze() { + if (size < elementData.length) { + elementData = Arrays.copyOf(elementData, size); + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 20:19 + * @Email: stevenchenguang@gmail.com + * @param: + * @Return: + * @Throw: + * @Desc: 将数组的长度扩容至2倍 + */ + private void _grow() { + elementData = Arrays.copyOf(elementData, elementData.length << 1); + } + + @Override + public String toString() { + return Arrays.toString(elementData); + } + + public boolean isEmpty() { + return size == 0; + } + + private class ArrayListItrator implements Iterator { + + private int position = 0; + + @Override + public boolean hasNext() { + return position != size; + } + + @Override + public Object next() { + int i = position; + if (i >= size) { + throw new NoSuchElementException(); + } + position = i + 1; + return elementData[i]; + } + } +} diff --git a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b40066ebe1 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,39 @@ +package coding.basic; + +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} diff --git a/group16/214074094/src/com/coding/basic/Iterator.java b/group16/214074094/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..1acc5349a4 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package coding.basic; + +public interface Iterator { + + boolean hasNext(); + + Object next(); + +} diff --git a/group16/214074094/src/com/coding/basic/LinkedList.java b/group16/214074094/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9108c2b6fe --- /dev/null +++ b/group16/214074094/src/com/coding/basic/LinkedList.java @@ -0,0 +1,222 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/25 21:01 + * @Email stevenchenguang@gmail.com + * @Desc OwnLinkedList + */ +public class LinkedList implements List { + + private int size = 0; + + private Node first; + + private Node last; + + public void add(Object o) { + if (size == 0) { + first = new Node(null, o, null); + last = first; + size++; + } else { + addLast(o); + } + } + + public void add(int index, Object o) { + _checkIndex(index); + if (index == size - 1) { + addLast(o); + } else { + Node prev = _node(index); + Node next = _node(index + 1); + Node newNode = new Node(prev, o, next); + prev.next = newNode; + next.prev = newNode; + size++; + } + } + + public Object get(int index) { + _checkIndex(index); + return node(index); + } + + public Object remove(int index) { + _checkIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } + Node curr = _node(index); + Object data = curr.data; + final Node prev = curr.prev; + final Node next = curr.next; + + prev.next = next; + next.prev = prev; + curr = null; + size--; + + return data; + } + + private Object removeFirst() { + Node oldFirst = first; + Object data = first.data; + final Node oldSecond = oldFirst.next; + if (null == oldSecond) { + first = null; + last = null; + } else { + oldSecond.prev = null; + first = oldSecond; + oldFirst = null; + } + size--; + return data; + } + + private Object removeLast() { + Node oldLast = last; + Object data = last.data; + final Node oldLastButOne = last.prev; + if (null == oldLastButOne) { + first = null; + last = null; + } else { + oldLastButOne.next = null; + last = oldLastButOne; + oldLast = null; + } + size--; + return data; + } + + public void addFirst(Object o) { + final Node oldFirst = first; + final Node param = new Node(null, o, null); + if (null == oldFirst) { + first = param; + } else { + oldFirst.prev = param; + param.next = oldFirst; + first = param; + } + size++; + } + + public void addLast(Object o) { + final Node n = last; + final Node newNode = new Node(n, o, null); + last = newNode; + n.next = newNode; + size++; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + public boolean isEmpty() { + return size == 0; + } + + private static class Node { + Node prev; + Object data; + Node next; + + public Node(Node prev, Object data, Node next) { + this.prev = prev; + this.data = data; + this.next = next; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:44 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: Node + * @Throw: + * @Desc: 根据下标获取节点元素上的数据 + */ + private Object node(int index) { + //如果下标在左一半, 从左往右取 + if (index < size >> 1) { + Node tmp = first; + for (int i = 0; i < index; i++) { + tmp = tmp.next; + } + return tmp.data; + } else { + Node tmp = last; + for (int i = size - 1; i > index; i--) { + tmp = tmp.prev; + } + return tmp.data; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:44 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: Node + * @Throw: + * @Desc: 根据下标获取节点元素 + */ + private Node _node(int index) { + //如果下标在左一半, 从左往右取 + if (index < size >> 1) { + Node tmp = first; + for (int i = 0; i < index; i++) { + tmp = tmp.next; + } + return tmp; + } else { + Node tmp = last; + for (int i = size - 1; i > index; i--) { + tmp = tmp.prev; + } + return tmp; + } + } + + /** + * @Author: shane + * @Time: 2017/2/25 22:43 + * @Email: stevenchenguang@gmail.com + * @param: int index + * @Return: + * @Throw: IndexOutOfBoundsException + * @Desc: 校验下标是否合法 + */ + private void _checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public String toString() { + if (0 == size) { + return "[]"; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < this.size; i++) { + sb.append(get(i)).append(", "); + } + String tmp = sb.substring(0, sb.length() - 2); + return "[" + tmp + "]"; + } +} \ No newline at end of file diff --git a/group16/214074094/src/com/coding/basic/List.java b/group16/214074094/src/com/coding/basic/List.java new file mode 100644 index 0000000000..5da9b0d4c6 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/List.java @@ -0,0 +1,15 @@ +package coding.basic; + +public interface List { + + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); + +} diff --git a/group16/214074094/src/com/coding/basic/Queue.java b/group16/214074094/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..869d0f7333 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Queue.java @@ -0,0 +1,36 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 17:19 + * @Email stevenchenguang@gmail.com + * @Desc Own Queue + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + if (isEmpty()) { + throw new RuntimeException("Queue is empty"); + } + return elementData.remove(0); + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group16/214074094/src/com/coding/basic/Stack.java b/group16/214074094/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7ef1c9ad06 --- /dev/null +++ b/group16/214074094/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 16:55 + * @Email stevenchenguang@gmail.com + * @Desc Own Stack + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(elementData.size() - 1); + } + + public Object peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group16/214074094/src/com/reading/blog_test.txt b/group16/214074094/src/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/214074094/src/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/214074094/src/test/coding/basic/AbstractTest.java b/group16/214074094/src/test/coding/basic/AbstractTest.java new file mode 100644 index 0000000000..80eaaa6fe5 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/AbstractTest.java @@ -0,0 +1,19 @@ +package coding.basic; + +/** + * @Author shane + * @Time 2017/2/25 13:06 + * @Email stevenchenguang@gmail.com + * @Desc 测试基类 + */ +public class AbstractTest { + + protected void printStar() { + System.out.println("********************************************"); + } + + protected void printHyphen() { + System.out.println("--------------------------------------------"); + } + +} diff --git a/group16/214074094/src/test/coding/basic/ArrayListTest.java b/group16/214074094/src/test/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..2f03342d61 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/ArrayListTest.java @@ -0,0 +1,81 @@ +package coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/25 13:02 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ + +public class ArrayListTest extends AbstractTest { + + private static ArrayList list; + + @Before + public void before() { + + list = new ArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + list.add("e"); + + printStar(); + System.out.println("Before Test data :" + list); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + list); + printStar(); + } + + @Test + public void testAddI() { + int index = list.size(); + list.add(index, "test add i"); + Assert.assertEquals(list.get(index), "test add i"); + } + + @Test + public void test() { + java.util.ArrayList list = new java.util.ArrayList(); + list.add("a"); + list.add("b"); + java.util.Iterator it = list.iterator(); + while (it.hasNext()) { + + } + System.out.println(it.next()); + System.out.println(it.next()); + System.out.println(it.next()); + } + + @Test + public void testSize() { + Assert.assertEquals(5, list.size()); + } + + @Test + public void testRemove() { + list.remove(5); + Assert.assertEquals(list.get(3), "d"); + } + + @Test + public void testIterator() { + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + +} diff --git a/group16/214074094/src/test/coding/basic/LinkedListTest.java b/group16/214074094/src/test/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..bc78728a25 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/LinkedListTest.java @@ -0,0 +1,63 @@ +package coding.basic; + +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/25 23:32 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class LinkedListTest extends AbstractTest { + + private static LinkedList list; + + @Before + public void before() { + list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + list.add("e"); + + printStar(); + System.out.println("Before Test data :" + list); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + list); + printStar(); + } + + @Test + public void testAddIndex() { + list.add(0, "after a"); + Assert.assertEquals("after a", list.get(1)); + + list.add(3, "after c"); + Assert.assertEquals("after c", list.get(4)); + + list.add(6, "after e"); + Assert.assertEquals("after e", list.get(7)); + } + + @Test + public void testRemove() { + list.remove(0); + Assert.assertEquals("b", list.get(0)); + + list.remove(list.size() - 1); + Assert.assertEquals("d", list.get(list.size() - 1)); + + Object obj = list.remove(1); + Assert.assertEquals("c", obj); + Assert.assertEquals(2, list.size()); + } +} diff --git a/group16/214074094/src/test/coding/basic/QueueTest.java b/group16/214074094/src/test/coding/basic/QueueTest.java new file mode 100644 index 0000000000..12302783b3 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/QueueTest.java @@ -0,0 +1,62 @@ +package coding.basic; + +import junit.framework.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/26 17:24 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class QueueTest extends AbstractTest { + + private static Queue queue; + + @Before + public void before() { + queue = new Queue(); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + printStar(); + System.out.println("Before Test data :" + queue); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + queue); + printStar(); + } + + @Test + public void testDeQueueAndIsEmpty() { + Assert.assertEquals("a", queue.deQueue()); + + queue.deQueue(); + queue.deQueue(); + queue.deQueue(); + queue.deQueue(); + + Assert.assertEquals(true, queue.isEmpty()); + + try { + queue.deQueue(); + } catch (RuntimeException e) { + Assert.assertEquals("Queue is empty", e.getMessage()); + } + } + + @Test + public void testSize() { + Assert.assertEquals(5, queue.size()); + } +} diff --git a/group16/214074094/src/test/coding/basic/StackTest.java b/group16/214074094/src/test/coding/basic/StackTest.java new file mode 100644 index 0000000000..f289744a67 --- /dev/null +++ b/group16/214074094/src/test/coding/basic/StackTest.java @@ -0,0 +1,68 @@ +package coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @Author shane + * @Time 2017/2/26 16:58 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class StackTest extends AbstractTest { + + private static Stack stack; + + @Before + public void before() { + stack = new Stack(); + + stack.push("a"); + stack.push("b"); + stack.push("c"); + stack.push("d"); + stack.push("e"); + + printStar(); + System.out.println("Before Test data :" + stack); + printHyphen(); + } + + @After + public void after() { + printHyphen(); + System.out.println("After Test data : " + stack); + printStar(); + } + + @Test + public void testPop() { + Assert.assertEquals("e", stack.pop()); + } + + @Test + public void testPeek() { + Assert.assertEquals("e", stack.peek()); + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(false, stack.isEmpty()); + + stack.pop(); + stack.pop(); + stack.pop(); + stack.pop(); + stack.pop(); + + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(5, stack.size()); + } + +} diff --git a/group16/214074094/target/production/214074094/reading/blog_test.txt b/group16/214074094/target/production/214074094/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/214074094/target/production/214074094/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/2562124714/.idea/dictionaries/zhangwj.xml b/group16/2562124714/.idea/dictionaries/zhangwj.xml new file mode 100644 index 0000000000..d07fd80ae1 --- /dev/null +++ b/group16/2562124714/.idea/dictionaries/zhangwj.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/misc.xml b/group16/2562124714/.idea/misc.xml new file mode 100644 index 0000000000..e97ef03f44 --- /dev/null +++ b/group16/2562124714/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/modules.xml b/group16/2562124714/.idea/modules.xml new file mode 100644 index 0000000000..c3fdba38f0 --- /dev/null +++ b/group16/2562124714/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/.idea/workspace.xml b/group16/2562124714/.idea/workspace.xml new file mode 100644 index 0000000000..d357c0f9a1 --- /dev/null +++ b/group16/2562124714/.idea/workspace.xml @@ -0,0 +1,780 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + binaryTree.PriOder + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487640652721 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/2562124714.iml b/group16/2562124714/2562124714.iml new file mode 100644 index 0000000000..3a8ffcf1f5 --- /dev/null +++ b/group16/2562124714/2562124714.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group16/2562124714/src/Test/ArrayListTest.java b/group16/2562124714/src/Test/ArrayListTest.java new file mode 100644 index 0000000000..9bd8f2aeca --- /dev/null +++ b/group16/2562124714/src/Test/ArrayListTest.java @@ -0,0 +1,70 @@ +package Test; + +import com.coding.basic.ArrayList; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class ArrayListTest { + private static ArrayList arraylist = new ArrayList(); + @BeforeClass + public static void setUp() throws Exception { + + System.out.println("初始化变量"); + for (Integer i = 0; i < 5; i++) + { + arraylist.add(i); + } + + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void add() throws Exception { + Integer i = arraylist.size(); + Integer AddElement = 999; + arraylist.add(AddElement); + assertEquals(i + 1, arraylist.size()); + assertEquals(AddElement, arraylist.get(arraylist.size())); + arraylist.remove(arraylist.size()); + } + + @Test + public void add1() throws Exception { + Integer AddElement = 999; + arraylist.add(1, AddElement); + assertEquals(AddElement, arraylist.get(1)); + arraylist.remove(1); + } + + @Test + public void get() throws Exception { + assertEquals(null, arraylist.get(9999)); + } + + @Test + public void remove() throws Exception { + Integer i = (Integer)arraylist.get(1); + assertEquals(i, arraylist.remove(1)); + arraylist.add(1, i); + + } + + @Test + public void size() throws Exception { + assertEquals(5, arraylist.size()); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/BinaryTreeNodeTest.java b/group16/2562124714/src/Test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..7d8f4fdd01 --- /dev/null +++ b/group16/2562124714/src/Test/BinaryTreeNodeTest.java @@ -0,0 +1,100 @@ +package Test; + +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.TreeData; +import com.sun.org.apache.bcel.internal.generic.NEW; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class BinaryTreeNodeTest { + private BinaryTreeNode binaryTree = new BinaryTreeNode(); + @Before + public void setUp() throws Exception { + //System.out.println("初始化二叉树,5, 4, 7"); + TreeData element = new TreeData(); + element.setT((Integer)5); + binaryTree.insert(element); + TreeData element2 = new TreeData(); + element2.setT((Integer)4); + binaryTree.insert(element2); + TreeData element3 = new TreeData(); + element3.setT((Integer)7); + binaryTree.insert(element3); +// binaryTree.PriOder(this.binaryTree); + } + + @Test + public void getData() throws Exception { + assertEquals(5, binaryTree.getData().getT()); + + } + + @Test + public void setData() throws Exception { + TreeData element = new TreeData(); + element.setT(6); + binaryTree.setData(element); + assertEquals(6, binaryTree.getData().getT()); +// binaryTree.PriOder(this.binaryTree); + + } + + @Test + public void getLeft() throws Exception { + assertEquals(4, binaryTree.getLeft().getData().getT()); + + } + + @Test + public void setLeft() throws Exception { + TreeData element = new TreeData(); + element.setT(2); + BinaryTreeNode NewTreeNode = new BinaryTreeNode(); + NewTreeNode.setData(element); + binaryTree.setLeft(NewTreeNode); + assertEquals(2, binaryTree.getLeft().getData().getT()); +// binaryTree.PriOder(this.binaryTree); + } + + @Test + public void getRight() throws Exception { + assertEquals(7, binaryTree.getRight().getData().getT()); + + } + + @Test + public void setRight() throws Exception { + TreeData element = new TreeData(); + element.setT(9); + BinaryTreeNode NewTreeNode = new BinaryTreeNode(); + NewTreeNode.setData(element); + binaryTree.setRight(NewTreeNode); + assertEquals(9, binaryTree.getRight().getData().getT()); + } + + @Test + public void priOder() throws Exception { + + + } + + @Test + public void insert() throws Exception { + TreeData element = new TreeData(); + element.setT(2); + binaryTree.insert(element); + binaryTree.PriOder(this.binaryTree); + element.setT(9); + binaryTree.insert(element); + binaryTree.PriOder(this.binaryTree); + element.setT(8); +// binaryTree.PriOder(this.binaryTree); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/LinkedListTest.java b/group16/2562124714/src/Test/LinkedListTest.java new file mode 100644 index 0000000000..276b389f15 --- /dev/null +++ b/group16/2562124714/src/Test/LinkedListTest.java @@ -0,0 +1,81 @@ +package Test; + +import com.coding.basic.LinkedList; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class LinkedListTest { + private LinkedList linkedlist = new LinkedList(); + + @Before + public void Init() + { + System.out.println("初始化"); + linkedlist.add(9.9); + linkedlist.add(9.99); + } + + + @Test + public void add() throws Exception { + linkedlist.add(8.8); + assertEquals(3, linkedlist.size()); + System.out.println("after add size is " + linkedlist.size()); + System.out.println("after add last element is " + linkedlist.get(linkedlist.size())); + + } + + @Test + public void add1() throws Exception { + linkedlist.add(2, 7.7); + assertEquals(3, linkedlist.size()); + System.out.println("after add in 2th size is " + linkedlist.size()); + System.out.println("after add 2th element is " + linkedlist.get(2)); + } + + @Test + public void get() throws Exception { + assertEquals(9.9, linkedlist.get(1)); + } + + @Test + public void remove() throws Exception { + assertEquals(9.9, linkedlist.remove(1)); + } + + @Test + public void size() throws Exception { + assertEquals(2, linkedlist.size()); + + } + + @Test + public void addFirst() throws Exception { + linkedlist.addFirst(3.3); + assertEquals(3.3, linkedlist.get(1)); +// System.out.println(); + } + + @Test + public void addLast() throws Exception { + linkedlist.addLast(3.3); + assertEquals(3.3, linkedlist.get(linkedlist.size())); + + } + + @Test + public void removeFirst() throws Exception { + assertEquals(9.9, linkedlist.removeFirst()); + } + + @Test + public void removeLast() throws Exception { + assertEquals(9.99, linkedlist.removeLast()); + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/QueueTest.java b/group16/2562124714/src/Test/QueueTest.java new file mode 100644 index 0000000000..3f0557f262 --- /dev/null +++ b/group16/2562124714/src/Test/QueueTest.java @@ -0,0 +1,58 @@ +package Test; + +import com.coding.basic.Queue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class QueueTest { + private Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + System.out.println("初始化队列,元素为a,b,c,d"); + + String[] s = {"a", "b","c","d"}; + for (String a:s + ) { + queue.enQueue(a); + } + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("dasdas"); + assertEquals(5, queue.size()); +// assertEquals("dasdas", queue.); + } + + @Test + public void deQueue() throws Exception { + assertEquals("a",queue.deQueue()); + assertEquals(3, queue.size()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(false, queue.isEmpty()); + + } + + @Test + public void size() throws Exception { + assertEquals(4, queue.size()); + + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/StackTest.java b/group16/2562124714/src/Test/StackTest.java new file mode 100644 index 0000000000..0a36d4dc0f --- /dev/null +++ b/group16/2562124714/src/Test/StackTest.java @@ -0,0 +1,57 @@ +package Test; + +import com.coding.basic.Stack; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class StackTest { + private Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + System.out.println("初始化栈,元素为a,b,c,d"); + + String[] s = {"a", "b","c","d"}; + for (String a:s + ) { + stack.push(a); + } + + } + + @Test + public void push() throws Exception { + stack.push("aaa"); + assertEquals(5, stack.size()); + assertEquals("aaa", stack.peek()); + + } + + @Test + public void pop() throws Exception { + assertEquals("d", stack.pop()); + assertEquals(3, stack.size()); + + } + + @Test + public void peek() throws Exception { + assertEquals("d", stack.peek()); + assertEquals(4, stack.size()); + } + + @Test + public void isEmpty() throws Exception { + assertEquals(false, stack.isEmpty()); + } + + @Test + public void size() throws Exception { + assertEquals(4, stack.size()); + } + +} \ No newline at end of file diff --git a/group16/2562124714/src/Test/TestRunner.java b/group16/2562124714/src/Test/TestRunner.java new file mode 100644 index 0000000000..2bf465f832 --- /dev/null +++ b/group16/2562124714/src/Test/TestRunner.java @@ -0,0 +1,19 @@ +package Test; + +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.Failure; + +import javax.xml.transform.Result; + +/** + * Created by zhangwj on 2017/2/23. 调用测试类 可重复使用 + */ +public class TestRunner { + public static void main(String[] args) { + org.junit.runner.Result result = JUnitCore.runClasses(BinaryTreeNodeTest.class); + for (Failure failure:result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} diff --git a/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f1d5a9fdd9 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private int Scale; //每次扩展大小 + + private Object[] elementData = new Object[100]; + + public ArrayList() + { + this.Scale = 10; + } + + public ArrayList(int i) + { + this.Scale = i; + } + + public void add(Object o){ + if (this.size == elementData.length) + { + DoEnlage(); + } + elementData[size] = o; + this.size++; + } + + private void DoEnlage() + { + if (this.Scale >= 1 && this.Scale <= 10000) + { + Object[] NewElementData = new Object[this.elementData.length + this.Scale]; + System.arraycopy(this.elementData,0,NewElementData,0,this.elementData.length); + + this.elementData = NewElementData; + } + + } + + //index从1开始 位置1,2,3,4,5,6 + public void add(int index, Object o){ + if (this.size == elementData.length) + { + DoEnlage(); + } + int i = 0; + //遍历赋值 + for(i = this.size; i >= index;i--) + { + this.elementData[i] = this.elementData[i - 1]; + } + + this.elementData[i] = o; + this.size++; + + } + + public Object get(int index){ + if (index >= 1 && index <= this.size) + { + return this.elementData[index - 1]; + } + else { + return null; + } + + + } + + public Object remove(int index){ + if (index >= 1 && index <= this.size) + { + int i = 0; + Object DelElement = this.elementData[index - 1]; + for(i = index; i <= this.size; i++) + { + this.elementData[i - 1] = this.elementData[i]; + } + this.elementData[i] = null; + this.size--; + + return DelElement; + + } + else { + return null; + } + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java b/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a703ad3165 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +import java.util.Properties; + +public class BinaryTreeNode { + + private TreeData treeData; + //private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public TreeData getData() { + return treeData; + } + public void setData(TreeData data) { + this.treeData = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public void PriOder(BinaryTreeNode Node) + { + if (Node.treeData != null) + { + System.out.println(Node.treeData.getT()); + if (Node.left != null) + { + PriOder(Node.left); + } + if (Node.right != null) + { + PriOder(Node.right); + } + } + + } + + + public BinaryTreeNode insert(TreeData o){ + if (this.treeData == null && this.left == null && this.right == null) + { + this.treeData = o; + this.left = null; + this.right = null; + return null; + } + + //遍历寻找元素应该插入的位置 + if (o.compareTo(this.treeData) <= 0) + { + if (this.left != null) + { + this.left.insert(o); + } + else + { + BinaryTreeNode NewNode = new BinaryTreeNode(); + NewNode.setData(o); + NewNode.setLeft(null); + NewNode.setRight(null); + this.left = NewNode; + } + } + else + { + if (this.right != null) + { + this.right.insert(o); + } + else + { + BinaryTreeNode NewNode = new BinaryTreeNode(); + NewNode.setData(o); + NewNode.setLeft(null); + NewNode.setRight(null); + this.right = NewNode; + } + } + + + return null; + } + +} diff --git a/group16/2562124714/src/com/coding/basic/Iterator.java b/group16/2562124714/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/2562124714/src/com/coding/basic/LinkedList.java b/group16/2562124714/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..f1de0a6839 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/LinkedList.java @@ -0,0 +1,200 @@ +package com.coding.basic; + +import com.sun.org.apache.bcel.internal.generic.NEW; + +import java.awt.*; + +public class LinkedList implements List { + public LinkedList() + { + head = null; + this.Size = 0; + } + + private Node head; + private int Size; + + public void add(Object o){ + Node NewNode = new Node(o); + + if (this.head == null) + { + head = NewNode; + } + else + { + Node node; + for (node = head; node.next != null; node = node.next) + { + } + node.next = NewNode; + } + this.Size++; + + } + //index为位置1,2,3,4,5,6,7 + public void add(int index , Object o){ + Node NewNode = new Node(o); + + if (1 == index) + { + NewNode.next = head; + head = NewNode; + } + else { + Node node; + int i = 0; + for (i = 1, node = head; i < index - 1; i++, node = node.next) { + } + NewNode.next = node.next; + node.next = NewNode; + } + this.Size++; + + } + public Object get(int index){ + Node node; + int i = 0; + + for (i = 1, node = head; i < index ; i++, node = node.next) { + } + + return node.data; +// return null; + } + public Object remove(int index){ + Node node; + int i = 0; + + if (1 == index) + { + if (head.next == null) + { + Object DelData = head.data; + head = null; + this.Size--; + return DelData; + } + else + { + Node DelNode = head; + head = DelNode.next; + DelNode.next = null; + this.Size--; + return DelNode.data; + + } + } + else { + + for (i = 1, node = head; i < index - 1; i++, node = node.next) { + } + Node DelNode = node.next; + node.next = DelNode.next; + DelNode.next = null; + this.Size--; + return DelNode.data; + } + } + + public int size(){ + + return this.Size; + } + + public void addFirst(Object o){ + Node NewNode = new Node(o); + + if (null == this.head) + { + NewNode.next = null; + head = NewNode; + } + else + { + NewNode.next = head; + head = NewNode; + } + + this.Size++; + + } + public void addLast(Object o){ + Node NewNode = new Node(o); + + if (this.Size == 0) + { + NewNode.next = null; + head = NewNode; + } + else + { +// int i = 0; + Node node; + for (node = head; node.next != null; node = node.next) { + + } + node.next = NewNode; + } + + this.Size++; + + } + public Object removeFirst(){ + Node DelFirst; + + if (1 == this.Size) + { + DelFirst = this.head; + DelFirst.next = null; + head = null; + } + else + { + DelFirst = this.head; + head = head.next; + DelFirst.next = null; + } + this.Size--; + + return DelFirst.data; + } + public Object removeLast(){ + Node DelLast; + + if (1 == this.Size) + { + DelLast = head; + DelLast.next = null; + head = null; + } + else + { + Node node; + for (node = head; node.next.next != null; node = node.next) { + + } + DelLast = node.next; + node.next = null; + } + this.Size--; + + return DelLast.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object o) + { + this.data = o; + this.next = null; + } + + } +} diff --git a/group16/2562124714/src/com/coding/basic/List.java b/group16/2562124714/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/2562124714/src/com/coding/basic/Queue.java b/group16/2562124714/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..40b8f22607 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + + return elementData.get(1); + } + + public boolean isEmpty(){ + + return elementData.size() == 0 ? true : false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/2562124714/src/com/coding/basic/Stack.java b/group16/2562124714/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..9dba3befa5 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + +// public Stack() +// { +// elementData +// } + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.remove(elementData.size()); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()); + return o; + } + public boolean isEmpty(){ + if (elementData.size() == 0) + { + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group16/2562124714/src/com/coding/basic/TestJunit.java b/group16/2562124714/src/com/coding/basic/TestJunit.java new file mode 100644 index 0000000000..59ef43a992 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TestJunit.java @@ -0,0 +1,17 @@ +package com.coding.basic; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class TestJunit { + @Test + + public void testAdd() + { + String str = "Junit is working fine"; + assertEquals("Junit is working fine", str); + } + +} diff --git a/group16/2562124714/src/com/coding/basic/TestRunner.java b/group16/2562124714/src/com/coding/basic/TestRunner.java new file mode 100644 index 0000000000..625ace52c6 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TestRunner.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import com.sun.net.httpserver.Authenticator; +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.Failure; +import org.junit.runners.model.TestClass; + +import javax.xml.transform.Result; + +/** + * Created by zhangwj on 2017/2/23. + */ +public class TestRunner { + public static void main(String[] args) + { + org.junit.runner.Result result = JUnitCore.runClasses(TestJunit.class); + + for (Failure failure : result.getFailures()) + { + System.out.println(failure.toString()); + } + + System.out.println(result.wasSuccessful()); + } +} diff --git a/group16/2562124714/src/com/coding/basic/TreeData.java b/group16/2562124714/src/com/coding/basic/TreeData.java new file mode 100644 index 0000000000..e79b7bd450 --- /dev/null +++ b/group16/2562124714/src/com/coding/basic/TreeData.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +/** + * Created by zhangwj on 2017/2/22. + */ +public class TreeData> implements Comparable>{ + private int s; + private T t; + + public T getT() + { + return t; + } + + public void setT(T o) { t = o;} + + @Override + public int compareTo(TreeData o) { + return getT().compareTo(o.getT()); + } + +// public int compareTo(TreeData o) +// { +// +// } + + +} diff --git a/group16/313001956/.classpath b/group16/313001956/.classpath new file mode 100644 index 0000000000..b42037dde2 --- /dev/null +++ b/group16/313001956/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group16/313001956/.gitignore b/group16/313001956/.gitignore new file mode 100644 index 0000000000..84c048a73c --- /dev/null +++ b/group16/313001956/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/group16/313001956/.project b/group16/313001956/.project new file mode 100644 index 0000000000..16d7526efa --- /dev/null +++ b/group16/313001956/.project @@ -0,0 +1,36 @@ + + + assignment + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/group16/313001956/.settings/.jsdtscope b/group16/313001956/.settings/.jsdtscope new file mode 100644 index 0000000000..92e666d77d --- /dev/null +++ b/group16/313001956/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.jdt.core.prefs b/group16/313001956/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..c537b63063 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/group16/313001956/.settings/org.eclipse.wst.common.component b/group16/313001956/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000000..2a267dc19d --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml b/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..611d2dfa73 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000000..3bd5d0a480 --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000000..05bd71b6ec --- /dev/null +++ b/group16/313001956/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/group16/313001956/WebContent/META-INF/MANIFEST.MF b/group16/313001956/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..254272e1c0 --- /dev/null +++ b/group16/313001956/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..eaaa690fa6 Binary files /dev/null and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/502059278/.classpath b/group16/502059278/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group16/502059278/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group16/502059278/.gitignore b/group16/502059278/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/502059278/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/502059278/.project b/group16/502059278/.project new file mode 100644 index 0000000000..72a951f7c1 --- /dev/null +++ b/group16/502059278/.project @@ -0,0 +1,17 @@ + + + DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" new file mode 100644 index 0000000000..31dfe4c14b Binary files /dev/null and "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" differ diff --git a/group16/502059278/src/cn/mark/MyArrayList.java b/group16/502059278/src/cn/mark/MyArrayList.java new file mode 100644 index 0000000000..9e0e406274 --- /dev/null +++ b/group16/502059278/src/cn/mark/MyArrayList.java @@ -0,0 +1,144 @@ +package cn.mark; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 自定义实现ArrayList的数据结构 + * @author hilih + * + */ +public class MyArrayList implements MyList{ + + private int size = 0; + + private Object[] elementData; + + public MyArrayList(){ + //默认容量初始化为10 + this(10); + } + + /** + * 初始即指定大小的构造方法 + * @param size 集合容量 + */ + public MyArrayList(int size){ + if ( size < 0 ){ + System.out.println("不合法的容量输入"); + return; + } + elementData = new Object[size]; + } + + /** + * 集合增容 + * @param minSize + */ + private void ensureSize(int minSize){ + int oldSize = elementData.length; + if(minSize > oldSize){ + int newSize = 3 * oldSize / 2 + 1; + if(minSize > newSize){ + newSize = minSize; + } + elementData = Arrays.copyOf(elementData, newSize); + } + } + + /** + * 下标范围判断 + * @param index + */ + private boolean rangeCheck(int index){ + if ( index >= size || index < 0 ){ + System.out.println("索引不合法!"); + return false; + } + return true; + } + + @Override + public boolean add(Object o) { + ensureSize(size+1); + elementData[size++] = o; + return true; + } + + @Override + public boolean add(int index, Object o) { + if (!rangeCheck(index)){ + return false; + } + ensureSize(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + return true; + } + + @Override + public Object get(int index) { + if (!rangeCheck(index)){ + return null; + } + Object o = elementData[index]; + return o; + } + + @Override + public Object remove(int index) { + if (!rangeCheck(index)){ + return null; + } + Object oldValue = elementData[index]; + int numMoved = size - index - 1; + if( numMoved > 0 ){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + return oldValue; + } + + @Override + public int size() { + return size; + } + + + + @Override + public String toString() { + StringBuilder s = new StringBuilder(); + s.append("["); + for (int i = 0; i < size; i++){ + Object o = elementData[i]; + s.append(o.toString()); + if( i < size-1 ){ + s.append(","); + } + } + s.append("]"); + return s.toString(); + } + + /** + * 判断当前集合是否为空 + * @return + */ + public boolean isEmpty(){ + return size == 0; + } + + public static void main(String[] args) { + MyList list = new MyArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add(2,"d"); + Object o = list.get(5); + System.out.println(o); + System.out.println(list.size()); + System.out.println(list); + } +} diff --git a/group16/502059278/src/cn/mark/MyLinkedList.java b/group16/502059278/src/cn/mark/MyLinkedList.java new file mode 100644 index 0000000000..7f9c3856a2 --- /dev/null +++ b/group16/502059278/src/cn/mark/MyLinkedList.java @@ -0,0 +1,67 @@ +package cn.mark; +/** + * 自定义实现LinkedList数据结构 + * @author hilih + * + */ +public class MyLinkedList implements MyList{ + + private Node head; + private int size;//集合的长度 + + /** + * 添加元素 + */ + @Override + public boolean add(Object o) { + //为空判断 + if ( o == null ){ + System.out.println("不允许null的元素插入!"); + return false; + } + if(head == null){ + head = new Node(); + head.data = o; + }else{ + + } + + return false; + } + + @Override + public boolean add(int index, Object o) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object get(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object remove(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return 0; + } + + private static class Node{ + Object data; + Node next; + } + + + public static void main(String[] args) { + + + } + +} diff --git a/group16/502059278/src/cn/mark/MyList.java b/group16/502059278/src/cn/mark/MyList.java new file mode 100644 index 0000000000..19bc3f92fe --- /dev/null +++ b/group16/502059278/src/cn/mark/MyList.java @@ -0,0 +1,32 @@ +package cn.mark; + +public interface MyList { + /** + * 向集合中增加元素 + * @param o + */ + public boolean add(Object o); + /** + * 向集合指定的位置中增加元素 + * @param index 下标 + * @param o 元素 + */ + public boolean add(int index, Object o); + /** + * 从集合指定位置取出元素 + * @param index 下标 + * @return + */ + public Object get(int index); + /** + * 从集合中删除指定位置的元素 + * @param index 下标 + * @return + */ + public Object remove(int index); + /** + * 当前集合的元素个数 + * @return + */ + public int size(); +} \ No newline at end of file diff --git a/group16/542087872/src/com/coding/basic/ArrayList.java b/group16/542087872/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1b10b441cf --- /dev/null +++ b/group16/542087872/src/com/coding/basic/ArrayList.java @@ -0,0 +1,88 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + // 每次乘2增长 + private void grow() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + + public void add(Object o){ + if (size >= elementData.length) { + this.grow(); + } + + elementData[size++] = o; + } + public void add(int index, Object o){ + if (size >= elementData.length) { + this.grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + + Object el = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + size--; + return el; + } + + public int size(){ + return size; + } + + private class ArrIter implements Iterator { + int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < size; + } + + @Override + public Object next() { + return elementData[cursor++]; + } + } + + public Iterator iterator(){ + return new ArrIter(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]); + if (i < size - 1) { + sb.append(","); + } + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java b/group16/542087872/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..df167343a0 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public int getData() { + return data; + } + public void setData(int data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + + public BinaryTreeNode(int data) { + this.data = data; + } + + private BinaryTreeNode insertAt(BinaryTreeNode node, int o) { + if (o < node.getData()) { + if (node.getLeft() != null) { + return insertAt(node.getLeft(), o); + } else { + BinaryTreeNode nowNode = new BinaryTreeNode(o); + node.setLeft(nowNode); + + return nowNode; + } + } else { + if (node.getRight() != null) { + return insertAt(node.getRight(), o); + } else { + BinaryTreeNode nowNode = new BinaryTreeNode(o); + node.setRight(nowNode); + return nowNode; + } + } + } + + public BinaryTreeNode insert(int o){ + return insertAt(this, o); + } + + @Override + public String toString() { + return "data: " + data; + } +} diff --git a/group16/542087872/src/com/coding/basic/Iterator.java b/group16/542087872/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/542087872/src/com/coding/basic/LinkedList.java b/group16/542087872/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..144af4ec8d --- /dev/null +++ b/group16/542087872/src/com/coding/basic/LinkedList.java @@ -0,0 +1,192 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + + public void add(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + } else { + tail.next = nowNode; + } + tail = nowNode; + } + public void add(int index , Object o){ + int count = 0; + Node lastOne = null; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + lastOne = tpHead; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + + Node nowNode = new Node(o); + if (lastOne == null) { + head = nowNode; + head.next = tpHead; + } else { + lastOne.next = nowNode; + nowNode.next = tpHead; + } + } + public Object get(int index){ + int count = 0; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + return tpHead.data; + } + public Object remove(int index){ + int count = 0; + Node lastOne = null; + Node tpHead = head; + while (tpHead != null && count != index) { + count++; + lastOne = tpHead; + tpHead = tpHead.next; + } + if (count != index) { + throw new IndexOutOfBoundsException(); + } + + if (lastOne == null) { + head = tpHead.next; + } else { + lastOne.next = tpHead.next; + } + + if (tpHead.next == null) { + tail = lastOne; + } + + return tpHead.data; + } + + public int size(){ + int count = 0; + Node tpHead = head; + while (tpHead != null) { + count ++; + tpHead = tpHead.next; + } + + return count; + } + + public void addFirst(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + tail = nowNode; + } else { + nowNode.next = head; + head = nowNode; + } + } + public void addLast(Object o){ + Node nowNode = new Node(o); + if (head == null) { + head = nowNode; + tail = nowNode; + } else { + tail.next = nowNode; + tail = nowNode; + } + } + public Object removeFirst(){ + if (head == null) { + throw new IndexOutOfBoundsException(); + } + + Node nowValue = head; + + Node nextNode = head.next; + if (nextNode == null) { + tail = null; + } + head = nextNode; + + return nowValue.data; + } + public Object removeLast(){ + if (head == null) { + throw new IndexOutOfBoundsException(); + } + + Node nowValue = tail; + + Node lastOne = null; + Node tpHead = head; + while (tpHead != tail) { + lastOne = tpHead; + tpHead = tpHead.next; + } + if (lastOne == null) { + head = null; + } else { + lastOne.next = null; + } + tail = lastOne; + + return nowValue.data; + } + + private class LinkIter implements Iterator { + + Node cursor = head; + + @Override + public boolean hasNext() { + return cursor != null; + } + + @Override + public Object next() { + Node ret = cursor; + cursor = cursor.next; + return ret.data; + } + } + + public Iterator iterator(){ + return new LinkIter(); + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } + + @Override + public String toString() { + Node tpHead = head; + StringBuilder sb = new StringBuilder("["); + while (tpHead != null) { + sb.append(tpHead.data); + sb.append(","); + tpHead = tpHead.next; + } + sb.append("]"); + return sb.toString(); + } + +} diff --git a/group16/542087872/src/com/coding/basic/List.java b/group16/542087872/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/542087872/src/com/coding/basic/Queue.java b/group16/542087872/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..8e4285464b --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group16/542087872/src/com/coding/basic/Stack.java b/group16/542087872/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..bfe98dd8b7 --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return o; + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group16/542087872/src/com/coding/basic/Test.java b/group16/542087872/src/com/coding/basic/Test.java new file mode 100644 index 0000000000..2db5b2f9ab --- /dev/null +++ b/group16/542087872/src/com/coding/basic/Test.java @@ -0,0 +1,166 @@ +package com.coding.basic; + + +/** + * Created by xiaoyuan on 25/02/2017. + */ +public class Test { + public static void main(String[] args) { + + testArrayList(); + testLinkedList(); + + testQueue(); + testStack(); + + + testBinaryTreeNode(); + } + + private static void testBinaryTreeNode() { + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(10); + binaryTreeNode.insert(5); + binaryTreeNode.insert(4); + binaryTreeNode.insert(6); + binaryTreeNode.insert(11); + + traverse(binaryTreeNode); + + } + + private static void traverse(BinaryTreeNode node) { + if (node.getLeft() != null) { + traverse(node.getLeft()); + } + + System.out.println("-- " + node.getData() + " --"); + + if (node.getRight() != null) { + traverse(node.getRight()); + } + + } + + + static void testStack() { + + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + System.out.println(stack.size()); + System.out.println(stack.isEmpty()); + + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + System.out.println(stack.isEmpty()); + + } + + static void testQueue() { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + + System.out.println(queue.size()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.size()); + } + static void testLinkedList() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + + System.out.println(linkedList.size()); + System.out.println(linkedList); + + linkedList.add(4); + linkedList.add(5); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.add(0, 10); + linkedList.add(0, 9); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + System.out.println(linkedList.get(3)); + + linkedList.remove(0); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.addFirst(100); + linkedList.addLast(8888); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + + linkedList.removeFirst(); + linkedList.removeLast(); + System.out.println(linkedList.size()); + System.out.println(linkedList); + + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + + } + + static void testArrayList() { + ArrayList arrayList = new ArrayList(); + arrayList.add("1"); + arrayList.add("2"); + // test size and add + System.out.println(arrayList.size()); + System.out.println(arrayList); + + + arrayList.add("3"); + arrayList.add("4"); + arrayList.add("5"); + arrayList.add("6"); + arrayList.add("7"); + arrayList.add("8"); + arrayList.add("9"); + arrayList.add("10"); + arrayList.add("11"); + arrayList.add("12"); + arrayList.add("13"); + + // test size + // test grow + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test add at index + arrayList.add(2, 100); + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test remove + arrayList.remove(0); + System.out.println(arrayList.size()); + System.out.println(arrayList); + arrayList.remove(2); + System.out.println(arrayList.size()); + System.out.println(arrayList); + + // test iterator + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + + } +} diff --git a/group16/out/production/214074094/com/reading/blog_test.txt b/group16/out/production/214074094/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/out/production/214074094/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group16/out/production/214074094/readme.txt b/group16/out/production/214074094/readme.txt new file mode 100644 index 0000000000..c1b06ddcc2 --- /dev/null +++ b/group16/out/production/214074094/readme.txt @@ -0,0 +1 @@ +I am 北京-Shane diff --git a/group16/out/production/214074094/src/com/reading/blog_test.txt b/group16/out/production/214074094/src/com/reading/blog_test.txt new file mode 100644 index 0000000000..b7e5cbfe14 --- /dev/null +++ b/group16/out/production/214074094/src/com/reading/blog_test.txt @@ -0,0 +1 @@ +just test new fork \ No newline at end of file diff --git a/group17/102228177/.classpath b/group17/102228177/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /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..350a2aab85 --- /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..0f0be01806 --- /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]; + } + + /** + * 在ArrayList末尾处追加元素 + * @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..6b091c87b3 --- /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..6d1fd8a2e3 --- /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..87613d0704 --- /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; + } + //判断是否找到第index-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..9f1a3f5ce6 --- /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..e945f4a67a --- /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..2b17605631 --- /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/1204187480/code/homework/basic/pom.xml b/group17/1204187480/code/homework/basic/pom.xml new file mode 100644 index 0000000000..039af72d42 --- /dev/null +++ b/group17/1204187480/code/homework/basic/pom.xml @@ -0,0 +1,12 @@ + + 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 new file mode 100644 index 0000000000..6ddf8cef58 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +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); + // 给index处 设值 + 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 new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group17/1204187480/code/homework/basic/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/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 new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group17/1204187480/code/homework/basic/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/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 new file mode 100644 index 0000000000..175d3adb74 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,147 @@ +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 new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group17/1204187480/code/homework/basic/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/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 new file mode 100644 index 0000000000..0f6caaec6f --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000000..7d91c326e0 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000000..85d3cf6901 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArrayListTest.java @@ -0,0 +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); + } +} 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 new file mode 100644 index 0000000000..eb41a7e262 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/ArraysTest.java @@ -0,0 +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)); + } +} 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 new file mode 100644 index 0000000000..efc4022378 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/api/SystemTest.java @@ -0,0 +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)); + + } +} 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 new file mode 100644 index 0000000000..9241fe72da --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +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)); + } + +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/parent/pom.xml b/group17/1204187480/code/homework/parent/pom.xml new file mode 100644 index 0000000000..9d29a4d93e --- /dev/null +++ b/group17/1204187480/code/homework/parent/pom.xml @@ -0,0 +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} + + + + + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/pom.xml b/group17/1204187480/code/homework/pom.xml new file mode 100644 index 0000000000..e939b51bd0 --- /dev/null +++ b/group17/1204187480/code/homework/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.coding + coding2017 + 1.0-SNAPSHOT + pom + + parent + basic + + + diff --git a/group17/1264835468/.classpath b/group17/1264835468/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group17/1264835468/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group17/1264835468/.gitignore b/group17/1264835468/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/1264835468/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/1264835468/.project b/group17/1264835468/.project new file mode 100644 index 0000000000..d2cad6d6dd --- /dev/null +++ b/group17/1264835468/.project @@ -0,0 +1,17 @@ + + + 1264835468 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/1264835468/src/assignment/BinaryTree.java b/group17/1264835468/src/assignment/BinaryTree.java new file mode 100644 index 0000000000..a74d5fecc8 --- /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..81d9e13004 --- /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..7cf6048b3d --- /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..5152d882c6 --- /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..5ab03b0fdf --- /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..d0f417e845 --- /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..d39b19c4b5 --- /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..c3d4f9fb9a --- /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/1540186032/First/src/ArrayList.java b/group17/1540186032/First/src/ArrayList.java new file mode 100644 index 0000000000..c039889a8e --- /dev/null +++ b/group17/1540186032/First/src/ArrayList.java @@ -0,0 +1,64 @@ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + if (size >= 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..f5ff087138 --- /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..73b303c8c2 --- /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..8510036684 --- /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..8cb4ba5aff --- /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..325fa033d1 --- /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..47f5244f0d --- /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..373dce4005 --- /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..ae3c172604 --- /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..96a5027846 --- /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..3a21537071 --- /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..61df9ada90 --- /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..2f4e0e3067 --- /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..124fba53ec --- /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..032b636962 --- /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..b902dea844 --- /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..6f48b4b4fd --- /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..5447a64fa9 --- /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..e59106a7ab --- /dev/null +++ b/group17/240094626/.gitignore @@ -0,0 +1,22 @@ +*.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/**/* 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..c20eb376d4 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java @@ -0,0 +1,166 @@ +package com.coding.basic.impl; + +import java.util.Arrays; + +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); + } + 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() { + return "ArrayList [data=" + Arrays.toString(data) + ", size=" + size + + "]"; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + /** + * @描述: 简单实现迭代器 + * @作者:240094626 + * @创建日期:2017-2-21 + */ + private class ArrayListIterator implements Iterator{ + + /** + * @column:index + * @comment:当前位置下标 + */ + private int index; + + /** + * 无参构造,初始化迭代器的下标为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..d9478146bd --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java @@ -0,0 +1,82 @@ +package com.coding.basic.impl; + + +/** + * 二叉树简单实现(key为int类型) + * @author 240094626 + * + */ +public class BinaryTree { + private Node rootNode = null; + + public Node insert(int key){ + return insert(key,null); + } + + 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; + } + return newNode; + } + + public Node getNode(int key){ + return get(rootNode, key); + } + + 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 null; + } + + + + + 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..a48b9e4705 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java @@ -0,0 +1,184 @@ +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; + + + /** + * 无参构造函数,初始化header节点,前后均指向header节点,形成环形链表 + * 环形链表:为了使链表节点的开头是header,结尾也是header; + * 由于实现了List,那么链表就是有序的,根据下标查询时可借助环形特点双向查找,提升效率; + */ + public LinkedList() { + header.next = header.pre = header; + } + + + /** + * 将Object 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); + } + // 查找从header开始 + 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.next = n.pre = null; + 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; + } + /** + * 环形链表结构,header.next就是第一个节点 + * @param o + */ + public void addFirst(Object o){ + addBefore(o, header.next); + } + /** + * 环形链表结构,header.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(); + } + + 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; + } + + } + + 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..3a9aa129e2 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java @@ -0,0 +1,67 @@ +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(); + } + + 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..d4c3158f94 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java @@ -0,0 +1,78 @@ +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; + } + + /** + * 栈的长度,既是容器ArrayList的长度 + * @return int + */ + public int size(){ + return elementData.size(); + } + + 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/51075907/HomeWork/.classpath b/group17/51075907/HomeWork/.classpath new file mode 100644 index 0000000000..10b13d9f7a --- /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..c27db84883 --- /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..43785549a2 --- /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..5a164ce8ca --- /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..4fce964dce --- /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..9a0468c683 --- /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..259c5f0910 --- /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 new file mode 100644 index 0000000000..bf5a6b639c --- /dev/null +++ b/group17/785396327/binarytree/BinaryTree.java @@ -0,0 +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); + } +} diff --git a/group17/785396327/binarytree/DLRSearchType.java b/group17/785396327/binarytree/DLRSearchType.java new file mode 100644 index 0000000000..3d1adbbdc7 --- /dev/null +++ b/group17/785396327/binarytree/DLRSearchType.java @@ -0,0 +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()); + } + } +} diff --git a/group17/785396327/binarytree/LDRSearchType.java b/group17/785396327/binarytree/LDRSearchType.java new file mode 100644 index 0000000000..da18dbc1b9 --- /dev/null +++ b/group17/785396327/binarytree/LDRSearchType.java @@ -0,0 +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()); + } + } +} diff --git a/group17/785396327/binarytree/LFSearchType.java b/group17/785396327/binarytree/LFSearchType.java new file mode 100644 index 0000000000..d983c50ff9 --- /dev/null +++ b/group17/785396327/binarytree/LFSearchType.java @@ -0,0 +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()); + } + + } + +} diff --git a/group17/785396327/binarytree/LRDSearchType.java b/group17/785396327/binarytree/LRDSearchType.java new file mode 100644 index 0000000000..250645e6df --- /dev/null +++ b/group17/785396327/binarytree/LRDSearchType.java @@ -0,0 +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() + " "); + } + } +} diff --git a/group17/785396327/binarytree/SearchType.java b/group17/785396327/binarytree/SearchType.java new file mode 100644 index 0000000000..606124a781 --- /dev/null +++ b/group17/785396327/binarytree/SearchType.java @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000000..9d79bd2013 --- /dev/null +++ b/group17/785396327/list/ArrayList.java @@ -0,0 +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++;//有效元素内容先加,保证长度极限情况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(); + } +} diff --git a/group17/785396327/list/Iterator.java b/group17/785396327/list/Iterator.java new file mode 100644 index 0000000000..0df87c6cf1 --- /dev/null +++ b/group17/785396327/list/Iterator.java @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000000..1b1fa0fccb --- /dev/null +++ b/group17/785396327/list/LinkedList.java @@ -0,0 +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(); + } +} diff --git a/group17/785396327/list/List.java b/group17/785396327/list/List.java new file mode 100644 index 0000000000..706f9f29bb --- /dev/null +++ b/group17/785396327/list/List.java @@ -0,0 +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); +} diff --git a/group17/785396327/queue/Queue.java b/group17/785396327/queue/Queue.java new file mode 100644 index 0000000000..68bea83b77 --- /dev/null +++ b/group17/785396327/queue/Queue.java @@ -0,0 +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); + } +} diff --git a/group17/785396327/stack/Stack.java b/group17/785396327/stack/Stack.java new file mode 100644 index 0000000000..c917809cc3 --- /dev/null +++ b/group17/785396327/stack/Stack.java @@ -0,0 +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); + } +} diff --git a/group17/82427129/.gitignore b/group17/82427129/.gitignore new file mode 100644 index 0000000000..e0db6fed9c --- /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..b83d22266a --- /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..974fb14110 --- /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..07f92770c9 --- /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..d7ac820192 --- /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..601c803f74 --- /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..ffed2d52c4 --- /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..2d47d3eaea --- /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/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..b023a3ea07 --- /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" new file mode 100644 index 0000000000..0cec6481d5 --- /dev/null +++ "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" @@ -0,0 +1,56 @@ +# 写一篇文章介绍cpu, 内存, 磁盘, 指令以及他们之间的关系 + +## 须知 +--- + +交作业时请在QQ 号后面填上各自的文章链接, 比如: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 文章 +--- + +1204187480 + +102228177 http://note.youdao.com/noteshare?id=74a51e7f93461dfb77c69a1cf4755624&sub=004F10FA5D2046ABAA060F19C0D2A18F + +876385982 http://www.totoro-fly.com/?p=59 + +785396327 + +1059107701 + +240094626 + +82427129 http://blog.csdn.net/walk_er/article/details/57406278 + +296910598 + +1264835468 + +516886559 + +1282579502 + +614982500 + +865797761 + +1540186032 http://blog.csdn.net/mpx_xb/article/details/56679603 + +176653813 + +116665530 + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + 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) { + //�������,��ǰ���������޷�����������ʱ,�Ƿ񳬹��˵�ǰ����ij��� + 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中允许存放null,因此下面通过两种情况来分别处理。 + if (o == null) { + for (int index = 0; index < size; index++){ + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int index = 0; index < size; index++){ + if (o.equals(elementData[index])) { + fastRemove(index); + return true; + } + } + } + return false; + } + + //返回现有元素数量 + public int size(){ + return size; + } + + //是否为空 + public boolean isEmpty(){ + return size == 0; + } + + public Iterator iterator(){ + return null; + } + + //动态增加ArrayList大小 + private void ensureCapacityInternal(int minCapacity) { + //当前数组无法再存放时将数组长度增加至原长度的1.5倍 + if (minCapacity - elementData.length > 0) { + int newCapacity = (elementData.length * 3)/2; + elementData = Arrays.copyOf(elementData, newCapacity); + } + + } + + //检查是否下标越界 + private void rangeCheck(int index){ + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + } + + //删除元素,与remove的 差别就是没有下标检查 + private void fastRemove(int index) { + int numMoved = size - index - 1; + if (numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + } + +} +class testArrayList{ + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + for (int i = 0; i < 10; i++) { + arrayList.add(i+1); + } + arrayList.add(5,15); + arrayList.remove(11); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("value is "+arrayList.get(i)); + } + } +} diff --git a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java 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,前项指针null + first = newNode;//将新节点变为头部节点 + if (f == null) {//头节点为null则代表链表为空,那么新节点也是既是头结点也是尾结点 + 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,后项指针null + last = newNode;//将新节点变为尾部节点 + if (l == null) {//尾节点为null则代表链表为空,那么新节点也是既是头结点也是尾结点 + 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为null代表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为空,则代表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为空,则代表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为空代表要删除的是头节点 + unlinkFirst(x); + } else {//prev后项指针指向next + prev.next = next; + x.previous = null; + } + if (next == null) {//next为空代表要删除的是尾节点 + 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);//重新写根据对象remove + return o; + } + + public Object peek(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} +class TestStack { + public static void main(String[] args){ + Stack myStack=new Stack(); + myStack.push("a"); + myStack.push(2); + myStack.push("123"); + myStack.push("ahu"); + while(!myStack.isEmpty()){ + System.out.println(myStack.pop()); + } + } +} diff --git a/group18/1159828430/README.md b/group18/1159828430/README.md 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; + + /** + * 使Object[]的长度默认为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(); + } + } + } + + /** + * 返回一个迭代器的实现 + * @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) // 根节点为空,则树为空,返回false。 + 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++; + } + } + + /** + * 返回集合的size。 + * @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; + } + } + + /** + * 返回一个迭代器的实现类 + * @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. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) + + 1.1 ArrayList实现了一下方法: + + ``` + (1)add(Object): 添加元素到集合; + (2)add(int, Object): 添加元素到集合的指定位置; + (3)size(): 返回集合的大小,类型为 int; + (4)isEmpty(): 判断集合是否为空,类型为 boolean; + (5)get(int): 获取集合指定位置的元素; + (6)remove(int): 删除指定位置的对象; + (7)remove(Object): 删除指定的对象; + (8)set(int, Object): 更改集合中指定位置的元素,并返回原来的对象; + (9)iterator(): 返回一个迭代器的实现类。 + ``` + + 1.2 LinkedList实现了一下方法: + + ``` + (1)addFirst(Object): 在链表头部插入新的元素; + (2)addLast(Object): 在链表尾部插入新的元素; + (3)add(Object): 在链表中插入新的元素; + (4)add(int, Object): 在链表指定位置插入新的元素; + (5)size(): 返回链表的大小,类型为 int; + (6)isEmpty(): 判断链表是否为空,类型为 boolean; + (7)getFirst(): 获取链表头部的元素; + (8)getLast(): 获取链表尾部的元素; + (9)get(int): 获取链表指定位置的元素; + (10)set(int, Object): 更改链表中指定位置的元素,并返回原来的对象。 + (11)removeFirst(): 删除链表头部的元素; + (12)removeLast(int): 删除链表尾部的元素; + (13)remove(Object): 删除指定元素; + (14)remove(int): 删除指定位置的元素; + (15)iterator(): 返回一个迭代器的实现类。 + ``` + + 1.3 Queue实现了一下方法: + + ``` + (1)enQueue(Object): 入队操作; + (2)deQueue(): 出队操作; + (3)size(): 返回队列的长度; + (4)isEmpty(): 判断队列是否为空。 + ``` + + 1.4 Stack实现了一下方法: + + ``` + (1)push(Object):入栈操作; + (2)pop():出栈操作; + (3)getTop():获取栈顶元素; + (4)isEmpty():判断栈是否为空; + (5)size():获取栈的深度。 + ``` + ​ + +2. 实现了BinarySearchTree、Iterator接口 + + 2.1 BinarySearchTree实现了一下方法: + + ``` + (1)insert(int):插入操作; + (2)find(int):查找操作; + (3)delete(int):删除操作; + (4)inorderTraverse(Node):遍历操作,采用中序遍历。 + ``` + + 2.2 Iterator定义了一下方法: + + ``` + (1)hasNext():判断是否有元素没有被遍历; + (2)next():返回游标当前位置的元素并将游标移动到下一个位置; + (3)remove():删除游标左边的元素,在执行完 next 之后该操作只能执行一次。 + ``` + ​ + +3. 对应以上类做了单元测试 + + 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 + + 点击查看: [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/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 + * 插入元素,判断当前列表中元素的个数, + * 当size==100,则需要扩张数组 + * 当size<100,则使用初始数组完成插入操作 + * 插入操作: + * 从最后一个元素开始,往后移动一位,直到到index为止. + * @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) { + // 保证数组长度正确 + ensureCapacityInternal(size + 1); + // 使用后缀自操作符 + elementData[size++] = element; + return true; + } + + @Override + public boolean add(int index, E element) { + rangeCheckForAdd(index);// 进行添加操作的索引范围检查 + // 保证数组长度正确 + 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); + } + + /** + * 传入容量是否大于最大容量常量,如大于最大容量,则返回int类型所能表示的最大值 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,则尾节点也为newNode + 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,则头节点也为newNode + if (l == null) { + first = newNode; + } else {// 否则将新节点作为原尾节点的下一个节点 + l.next = newNode; + } + size++; + } + + /** + * 在指定节点前插入新节点 + * + * @param e + * @param node + */ + void linkBefore(E e, Node node) { + // 获取node的上一个节点,并创建新节点,将pred做为新节点的上一个节点,将node作为新节点的下一个节点 + final Node pred = node.prev; + final Node newNode = new Node<>(pred, e, node); + // 将node的上一个节点指向newNode + node.prev = newNode; + // 如prev为null,则说明node为first,那么将新节点设为first + if (pred == null) { + first = newNode; + } else {// 否则,将新节点设为pred的下一个节点 + 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; + // 如prev节点为null,则说明x节点为first,那么将next节点设为first + if (prev == null) { + first = next; + } else {// 否则,将prev节点的下一个节点设为next + prev.next = next; + } + // 如next节点为null,则说明x节点为last,那么将prev节点设为last + if (next == null) { + last = prev; + } else {// 否则,将next节点的上一个节点设为prev + 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只管运算,怎么运算,何时运算,运算什么这些cpu都是不管的,这些其实就是一个个的指令,指令用来表示所有运算的信息,包括做什么运算,运算什么,运算结果如何处置等。这些指令都是存在内存当中的,cpu按照一定的顺序从内存当中读取指令,执行指令做运算,然后按照相应的指令输出结果。这就是一个简单的程序运行过程。内存空间是有限的,另外,内存上的指令也不能持久化的保存,断电之后就没有了。所以需要一个硬件保存大的二进制资源以及持久化的保存指令,所以就有了硬盘。所以计算机先从硬盘中读取指令和资源到内存当中来,然后cpu再从内存当中读取指令和资源,做运算,运算完之后再按照指令将输出保存在内存中或者硬盘中,程序执行完毕。 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/liuxin/.classpath b/liuxin/.classpath index fceb4801b5..b387714202 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -1,6 +1,7 @@ - - - - - - + + + + + + + diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..78845d06d0 --- /dev/null +++ b/liuxin/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/liuxin/src/com/coderising/litestruts/View.java b/liuxin/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/liuxin/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file