diff --git a/group27/1016908591/guoqixuan/src/com/coding/basic/ArrayList.java b/group27/1016908591/guoqixuan/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2eab2cd640 --- /dev/null +++ b/group27/1016908591/guoqixuan/src/com/coding/basic/ArrayList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +import java.util.Arrays; + + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private int length; + private static final int MAX_SIZE = 50; + + public ArrayList(){ + this(MAX_SIZE); + } + public ArrayList(int maxSize){ + length = 0; + elementData = new Object[maxSize]; + } + + public void add(Object o){ + if(! isFull()){ + elementData[size] =o; + size++; + } + else + elementData = Arrays.copyOf(elementData, elementData.length*2); + + + } + public void add(int index, Object o){ + + makeRoom(index); + elementData[size-1] =o; + size++; + + + + } + + public Object get(int index){ + if(index>0 && indexsize){ + System.out.println("����Խ��"); + return null; + } + Object o = elementData[size]; + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + elementData[size--] = null; + return o; + + + } + + public int size(){ + return elementData.length; + } + + public Iterator iterator(){ + return null; + } + + public boolean isFull(){ + return size == elementData.length; + } + + + private void makeRoom(int index){ + for(int i = size;i>=index;i--){ + elementData[i] =elementData[i-1]; + + } + } + + public Iterator getiterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + ArrayList l = null; + private int nextIndex; + ArrayListIterator(ArrayList l){ + nextIndex = 0; + this.l = l; + + } + @Override + public boolean hasNext() { + + return nextIndex7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group27/1016908591/guoqixuan/src/com/coding/basic/List.java b/group27/1016908591/guoqixuan/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group27/1016908591/guoqixuan/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/group27/1016908591/guoqixuan/src/com/coding/basic/Queue.java b/group27/1016908591/guoqixuan/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b31c1625b4 --- /dev/null +++ b/group27/1016908591/guoqixuan/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList List = new LinkedList(); + + public void enQueue(Object o){ + List.addLast(o); + } + + public Object deQueue(){ + return List.removeFirst(); + } + + public boolean isEmpty(){ + return List.size()==0; + } + + public int size(){ + return List.size(); + } +} diff --git a/group27/1016908591/guoqixuan/src/com/coding/basic/Stack.java b/group27/1016908591/guoqixuan/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..060fd1862d --- /dev/null +++ b/group27/1016908591/guoqixuan/src/com/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package com.coding.basic; +import java.util.Arrays; +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int topIndex =-1;//栈顶元素索引 + private static final int DEFAULT_MAX_SIZE = 50; + private int length; + + //压入一个元素 + public void push(Object o){ + topIndex++; + elementData.add(o); + + + + } + + public Object pop(){ + if(elementData.size() ==0 ){ + throw new EmptyStackException(); + } + topIndex--; + return elementData.remove(elementData.size()-1); + + } + + public Object peek(){ + if(elementData.size()!=1){ + return elementData.get(elementData.size()-1); + } + return null; + } + public boolean isEmpty(){ + return topIndex>0; + } + public int size(){ + return topIndex; + } +} diff --git a/group27/1016908591/week01/.classpath b/group27/1016908591/week01/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group27/1016908591/week01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group27/1016908591/week01/.gitignore b/group27/1016908591/week01/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group27/1016908591/week01/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group27/1016908591/week01/.project b/group27/1016908591/week01/.project new file mode 100644 index 0000000000..3b3c4fa7ee --- /dev/null +++ b/group27/1016908591/week01/.project @@ -0,0 +1,17 @@ + + + week01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/1016908591/week01/.settings/org.eclipse.jdt.core.prefs b/group27/1016908591/week01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group27/1016908591/week01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group27/1016908591/week01/src/com/coding/basic/ArrayList.java b/group27/1016908591/week01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2eab2cd640 --- /dev/null +++ b/group27/1016908591/week01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +import java.util.Arrays; + + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private int length; + private static final int MAX_SIZE = 50; + + public ArrayList(){ + this(MAX_SIZE); + } + public ArrayList(int maxSize){ + length = 0; + elementData = new Object[maxSize]; + } + + public void add(Object o){ + if(! isFull()){ + elementData[size] =o; + size++; + } + else + elementData = Arrays.copyOf(elementData, elementData.length*2); + + + } + public void add(int index, Object o){ + + makeRoom(index); + elementData[size-1] =o; + size++; + + + + } + + public Object get(int index){ + if(index>0 && indexsize){ + System.out.println("����Խ��"); + return null; + } + Object o = elementData[size]; + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + elementData[size--] = null; + return o; + + + } + + public int size(){ + return elementData.length; + } + + public Iterator iterator(){ + return null; + } + + public boolean isFull(){ + return size == elementData.length; + } + + + private void makeRoom(int index){ + for(int i = size;i>=index;i--){ + elementData[i] =elementData[i-1]; + + } + } + + public Iterator getiterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + ArrayList l = null; + private int nextIndex; + ArrayListIterator(ArrayList l){ + nextIndex = 0; + this.l = l; + + } + @Override + public boolean hasNext() { + + return nextIndex7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group27/1016908591/week01/src/com/coding/basic/List.java b/group27/1016908591/week01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group27/1016908591/week01/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/group27/1016908591/week01/src/com/coding/basic/Queue.java b/group27/1016908591/week01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b31c1625b4 --- /dev/null +++ b/group27/1016908591/week01/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList List = new LinkedList(); + + public void enQueue(Object o){ + List.addLast(o); + } + + public Object deQueue(){ + return List.removeFirst(); + } + + public boolean isEmpty(){ + return List.size()==0; + } + + public int size(){ + return List.size(); + } +} diff --git a/group27/1016908591/week01/src/com/coding/basic/Stack.java b/group27/1016908591/week01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..060fd1862d --- /dev/null +++ b/group27/1016908591/week01/src/com/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package com.coding.basic; +import java.util.Arrays; +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int topIndex =-1;//栈顶元素索引 + private static final int DEFAULT_MAX_SIZE = 50; + private int length; + + //压入一个元素 + public void push(Object o){ + topIndex++; + elementData.add(o); + + + + } + + public Object pop(){ + if(elementData.size() ==0 ){ + throw new EmptyStackException(); + } + topIndex--; + return elementData.remove(elementData.size()-1); + + } + + public Object peek(){ + if(elementData.size()!=1){ + return elementData.get(elementData.size()-1); + } + return null; + } + public boolean isEmpty(){ + return topIndex>0; + } + public int size(){ + return topIndex; + } +} diff --git "a/group27/1016908591/week01/src/\346\265\205\350\260\210CPU.txt" "b/group27/1016908591/week01/src/\346\265\205\350\260\210CPU.txt" new file mode 100644 index 0000000000..ae0478214c --- /dev/null +++ "b/group27/1016908591/week01/src/\346\265\205\350\260\210CPU.txt" @@ -0,0 +1 @@ +http://blog.csdn.net/weixin_37604160/article/details/61195314 \ No newline at end of file diff --git a/group27/1067041567/RemoteSystemsTempFiles/.project b/group27/1067041567/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group27/1067041567/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group27/1067041567/TestColleaction/.classpath b/group27/1067041567/TestColleaction/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group27/1067041567/TestColleaction/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group27/1067041567/TestColleaction/.gitignore b/group27/1067041567/TestColleaction/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group27/1067041567/TestColleaction/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group27/1067041567/TestColleaction/.project b/group27/1067041567/TestColleaction/.project new file mode 100644 index 0000000000..955a5e1e5d --- /dev/null +++ b/group27/1067041567/TestColleaction/.project @@ -0,0 +1,17 @@ + + + TestColleaction + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/1067041567/TestColleaction/.settings/org.eclipse.jdt.core.prefs b/group27/1067041567/TestColleaction/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group27/1067041567/TestColleaction/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group27/1067041567/TestColleaction/src/cn/task1/ArrayList.java b/group27/1067041567/TestColleaction/src/cn/task1/ArrayList.java new file mode 100644 index 0000000000..b1f4e43e1d --- /dev/null +++ b/group27/1067041567/TestColleaction/src/cn/task1/ArrayList.java @@ -0,0 +1,100 @@ +package cn.task1; + +import java.util.LinkedList; + +public class ArrayList implements List { + + int size; + Object[] obj; + + public ArrayList(){ + size = 0; + obj = new Object[size]; + } + + @Override + public void add(Object object) { + if(size==0){ + obj = new Object[1]; + obj[0] = object; + size = 1; + }else{ + Object[] temp = new Object[size+1]; +// for(int k =0;kindex){ + temp[k-1] = obj[k]; + } + } + obj = temp; + size--; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + if(size>0){ + return true; + } + return false; + } + + public void set(int index,Object object){ + Object[] temp = new Object[size+1]; + for(int k=0;kindex){ + temp[k] = obj[k-1]; + } + } + obj = temp; + size++; + } + + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + + list.add("d"); + list.add("dd"); + list.add("cc"); + list.remove(2); + list.set(1, "dwe"); + + System.out.println(list.get(0)); + System.out.println(list.get(1)); + System.out.println(list.get(2)); + System.out.println(list.size()); + System.out.println(list.isEmpty()); + + } + +} diff --git a/group27/1067041567/TestColleaction/src/cn/task1/LinkedList.java b/group27/1067041567/TestColleaction/src/cn/task1/LinkedList.java new file mode 100644 index 0000000000..283129ddd8 --- /dev/null +++ b/group27/1067041567/TestColleaction/src/cn/task1/LinkedList.java @@ -0,0 +1,146 @@ +package cn.task1; + +public class LinkedList{ + + Node head; + int size; + + public LinkedList(){ + head = new Node(); + this.size = 0; + } + + public void add(Object data) { + Node temp = head; + while(temp.next != null){ + temp = temp.next; + } + temp.next = new Node(data); + size++; + } + + public void set(int index,Object obj){ + try { + Node temp = new Node(obj); + Node pre = null; + if (index > 0) { + pre = getNode(index - 1); + } else { + pre = head; + } + Node last = getNode(index); + pre.next = temp; + temp.next = last; + size++; + } catch (Exception e) { + // TODO: handle exception + try { + throw new Exception("存在异常错误!"); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + } + + + public Object get(int index) { + Node temp = head; + if(index>=size){ + throw new ArrayIndexOutOfBoundsException("超出范围!"); + } + for(int k=0;k=size){ + throw new ArrayIndexOutOfBoundsException("超出范围!"); + } + for(int k=0;k=size){ + throw new ArrayIndexOutOfBoundsException("超出范围!"); + } + for(int k=0;k0){ + return true; + }else{ + return false; + } + } + + public void clear(){ + head.next = null; + size=0; + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.add("dd"); + list.add("ff"); + list.set(4, "4546"); +// list.remove(2);list.remove(2); + System.out.println(list.size()); + System.out.println(list.get(0)); + System.out.println(list.get(1)); + System.out.println(list.get(2)); + System.out.println(list.get(3)); + System.out.println(list.get(4)); + System.out.println(list.get(5)); + System.out.println(list.isEmpty()); + list.clear(); + System.out.println(list.isEmpty()); + } + +} + + +class Node{ + + Object obj; + Node next; + + public Node(){ + this.obj = null; + this.next = null; + } + + public Node(Object obj){ + this.obj = obj; + this.next = null; + } + +} diff --git a/group27/1067041567/TestColleaction/src/cn/task1/List.java b/group27/1067041567/TestColleaction/src/cn/task1/List.java new file mode 100644 index 0000000000..d75d579c80 --- /dev/null +++ b/group27/1067041567/TestColleaction/src/cn/task1/List.java @@ -0,0 +1,14 @@ +package cn.task1; + +public interface List { + + public void add(Object obj); + + public Object get(int index); + + public void remove(int index); + + public int size(); + + public boolean isEmpty(); +} diff --git a/group27/1067041567/TestColleaction/src/cn/task1/Queue.java b/group27/1067041567/TestColleaction/src/cn/task1/Queue.java new file mode 100644 index 0000000000..30a5743ae4 --- /dev/null +++ b/group27/1067041567/TestColleaction/src/cn/task1/Queue.java @@ -0,0 +1,115 @@ +package cn.task1; + +import java.util.Arrays; + +public class Queue { + + private static final int CAPACITY = 1024; + private static int capacity; + private static int front; + private static int tail; + private static Object[] array; + + public Queue() { + this.capacity = CAPACITY; + array = new Object[capacity]; + front = tail = 0; + } + + public static int getSize() { + if (isEmpty()){ + return 0; + }else{ + return (capacity + tail - front) % capacity; + } + } + + + public static boolean isEmpty() { + return (front == tail); + } + + //进栈 + public static void enqueue(Object element) throws ExceptionQueueFull { + if (getSize() == capacity - 1){ + throw new ExceptionQueueFull("Queue is full"); + } + array[tail] = element; + tail = (tail + 1) % capacity; + } + + //出栈 + public static Object dequeue() throws ExceptionQueueEmpty { + Object element; + if(isEmpty()){ + throw new ExceptionQueueEmpty("Queue is empty"); + } + element = array[front]; + front = (front + 1) % capacity; + return element; + } + + + public static Object frontElement() throws ExceptionQueueEmpty { + if(isEmpty()){ + throw new ExceptionQueueEmpty("Queue is empty"); + } + return array[front]; + } + + public static void getAllElements() { + Object[] arrayList = new Object[getSize()]; + for(int i=front,j=0;j-1); + } + + public int search(Object obj){ + + return -1; + } + + public static void main(String[] args) { + + Stack s = new Stack(); + + s.push("1"); + s.push("2"); + s.push("12"); + s.push("3"); + s.push("34"); + + System.out.println(s.pop()+" "+s.isEmpty()); + + System.out.println(s.size()+" "+s.peek()); + + } +} diff --git a/group27/1067041567/TestColleaction/src/cn/task1/Test.java b/group27/1067041567/TestColleaction/src/cn/task1/Test.java new file mode 100644 index 0000000000..1487409c0e --- /dev/null +++ b/group27/1067041567/TestColleaction/src/cn/task1/Test.java @@ -0,0 +1,20 @@ +package cn.task1; + +import java.util.Queue; +import java.util.Stack; + +public class Test { + public static void main(String[] args) { + + Stack obj = new Stack<>(); + //Queue queue = new + + obj.push("a"); + obj.push("b"); + obj.push("c"); + obj.push("d"); + obj.peek(); + System.out.println(obj.peek()); + System.out.println(obj.size()); + } +} diff --git a/group27/2567012201/.DS_Store b/group27/2567012201/.DS_Store new file mode 100644 index 0000000000..28934e70a8 Binary files /dev/null and b/group27/2567012201/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/.DS_Store b/group27/2567012201/2567012201learning/.DS_Store new file mode 100644 index 0000000000..8c045f0bb5 Binary files /dev/null and b/group27/2567012201/2567012201learning/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/.classpath b/group27/2567012201/2567012201learning/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group27/2567012201/2567012201learning/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group27/2567012201/2567012201learning/.gitignore b/group27/2567012201/2567012201learning/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group27/2567012201/2567012201learning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group27/2567012201/2567012201learning/.project b/group27/2567012201/2567012201learning/.project new file mode 100644 index 0000000000..eb67d06bb5 --- /dev/null +++ b/group27/2567012201/2567012201learning/.project @@ -0,0 +1,17 @@ + + + 2567012201learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/2567012201/2567012201learning/.settings/org.eclipse.jdt.core.prefs b/group27/2567012201/2567012201learning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group27/2567012201/2567012201learning/.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/group27/2567012201/2567012201learning/src/.DS_Store b/group27/2567012201/2567012201learning/src/.DS_Store new file mode 100644 index 0000000000..72a20b04fb Binary files /dev/null and b/group27/2567012201/2567012201learning/src/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/src/com/.DS_Store b/group27/2567012201/2567012201learning/src/com/.DS_Store new file mode 100644 index 0000000000..aadedc0f59 Binary files /dev/null and b/group27/2567012201/2567012201learning/src/com/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/src/com/coding/.DS_Store b/group27/2567012201/2567012201learning/src/com/coding/.DS_Store new file mode 100644 index 0000000000..5a44c340ce Binary files /dev/null and b/group27/2567012201/2567012201learning/src/com/coding/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/.DS_Store b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/.DS_Store new file mode 100644 index 0000000000..602ec50de1 Binary files /dev/null and b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/.DS_Store differ diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/ArrayList.java b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/ArrayList.java new file mode 100644 index 0000000000..fe3a8c70e2 --- /dev/null +++ b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/ArrayList.java @@ -0,0 +1,54 @@ +package com.coding.DataStructure; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +public abstract class ArrayList implements List { + + private int size = 0; + private static final int INCREMENT = 10; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + + if ((size + 1)>= 100) { + throw new RuntimeException("数组越界异常"); + } + elementData[size] = 0; + size++; + } + + public void add(int index, Object o){ + growLength(); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (index > size) throw new IndexOutOfBoundsException("index: "+index+", size: "+size); + Object retVal = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + elementData[--size] = null; + return retVal; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + private void growLength() { + if (this.size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length+INCREMENT); + } + } +} diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/LinkedList.java b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/LinkedList.java new file mode 100644 index 0000000000..641998a2ad --- /dev/null +++ b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/LinkedList.java @@ -0,0 +1,10 @@ +package com.coding.DataStructure; + +public class LinkedList { + + public void addLast(E o) { + // TODO Auto-generated method stub + + } + +} diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Queue.java b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Queue.java new file mode 100644 index 0000000000..07e3fcc700 --- /dev/null +++ b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Queue.java @@ -0,0 +1,36 @@ +package com.coding.DataStructure; + +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o);; + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object val = list.removeFirst(); + size--; + return val; + } + + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + + public int size(){ + return size; + } + +} + diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Stack.java b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Stack.java new file mode 100644 index 0000000000..ef20d5c4e8 --- /dev/null +++ b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Stack.java @@ -0,0 +1,41 @@ +package com.coding.DataStructure; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + Object val = elementData.remove(len); + size--; + return val; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + public int size(){ + return size; + } +} diff --git a/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Tree.java b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Tree.java new file mode 100644 index 0000000000..d3e4c84488 --- /dev/null +++ b/group27/2567012201/2567012201learning/src/com/coding/DataStructure/Tree.java @@ -0,0 +1,51 @@ +package com.coding.DataStructure; + +public class Tree { + private Object data; + private Tree left; + private Tree right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Tree getLeft() { + return left; + } + + public void setLeft(Tree left) { + this.left = left; + } + + public Tree getRight() { + return right; + } + + public void setRight(Tree right) { + this.right = right; + } + + public Tree insert(Object o) { + if (data == null) { + setData(o); + } else { + Integer i = (Integer) o; + if (i.compareTo((Integer) data) == -1) { + if(right == null) + right = new Tree(); + return right.insert(i); + } else if (i.compareTo((Integer) data) == 1) { + if(left == null) + left = new Tree(); + return left.insert(i); + } + return null; + } + return null; + } + +} diff --git a/group27/2567012201/RemoteSystemsTempFiles/.project b/group27/2567012201/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group27/2567012201/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group27/276961139/src/learn/ArrayList.java b/group27/276961139/src/learn/ArrayList.java new file mode 100644 index 0000000000..c0993491d1 --- /dev/null +++ b/group27/276961139/src/learn/ArrayList.java @@ -0,0 +1,93 @@ +package com.liam.learn.code2017; + +import java.lang.reflect.Array; +import java.util.Arrays; + +public class ArrayList implements List { + + private int capacity = 10; + private int size = 0; + + private Object[] elementData = null; //new Object[100]; + + public ArrayList(){ + this.capacity = capacity; + elementData = new Object[capacity]; + } + + public ArrayList(int custCapacity) { + if(custCapacity <= 0){ + throw new IllegalArgumentException("Arraylist 长度不能为负数或0"); + } + this.capacity = custCapacity; + elementData = new Object[capacity]; + } + + public void add(Object o){ + if (size >= capacity){ + enlargeCapacity(); + } + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if(index <0 || index >= size){ + throw new IllegalArgumentException("数组越界"); + } + if (size >= capacity){ + enlargeCapacity(); + } + 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 IllegalArgumentException("数组越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + Object removedObj = get(index); + + int movedSize = size - (index + 1); + if (movedSize > 0) { + System.arraycopy(elementData, index+1, elementData, index, movedSize); + } + elementData[--size] = null; + + return removedObj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + + @Override + public String toString() { + if (size < capacity){ + //int needRemove = capacity - size; + Object[] toStringObj = new Object[size]; + System.arraycopy(elementData, 0, toStringObj, 0, size); + return Arrays.toString(toStringObj); + } + return Arrays.toString(elementData); + } + + private void enlargeCapacity(){ + capacity = capacity * 2; + Object[] temp = new Object[capacity]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + } + + + +} diff --git a/group27/276961139/src/learn/BinaryTreeNode.java b/group27/276961139/src/learn/BinaryTreeNode.java new file mode 100644 index 0000000000..86e8d5ce7b --- /dev/null +++ b/group27/276961139/src/learn/BinaryTreeNode.java @@ -0,0 +1,46 @@ +package com.liam.learn.code2017; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + } + + public BinaryTreeNode() { + } + + 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 (left != null){ + return left = new BinaryTreeNode(o); + } + if (right !=null){ + return right = new BinaryTreeNode(o); + } + throw new RuntimeException("左右子树已经都有值了"); + } + +} diff --git a/group27/276961139/src/learn/Iterator.java b/group27/276961139/src/learn/Iterator.java new file mode 100644 index 0000000000..0f85a64dfd --- /dev/null +++ b/group27/276961139/src/learn/Iterator.java @@ -0,0 +1,7 @@ +package com.liam.learn.code2017; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group27/276961139/src/learn/LinkedList.java b/group27/276961139/src/learn/LinkedList.java new file mode 100644 index 0000000000..79924fa07f --- /dev/null +++ b/group27/276961139/src/learn/LinkedList.java @@ -0,0 +1,255 @@ +package com.liam.learn.code2017; + + + +public class LinkedList implements List { + private int size; + + private Node head; + + private Node tail; + + public void add(Object o){ + Node node = new Node(o, null); + if(head == null){ + head = node; + tail = node; + }else{ + tail.setNext(node); + tail = node; + } + size++; + } + public void add(int index , Object o){ + if (index < 0 || index >= size){ + throw new IllegalArgumentException("链表越界"); + } + if (index == 0){ + addFirst(o); + }else if(index == size-1){ + addLast(o); + }else{ + Node indexNode = getNode(index); + Node newNode = new Node(o, indexNode); + Node previousNode = getNode(index-1); + previousNode.setNext(newNode); + } + size++; + } + public Object get(int index){ + Node node = getNode(index); + return node.getData(); + } + + private Node getNode(int index){ + if (index < 0 || index >= size){ + throw new IllegalArgumentException("链表越界"); + } + if(index == 0){ + return head; + } + if(index == size-1){ + return tail; + } + Node temp = head; + for(int i=0; i= size){ + throw new IllegalArgumentException("链表越界"); + } + + if (index == 0){ + return removeFirst(); + }else if(index == size-1){ + return removeLast(); + }else{ + Node previousNode = getNode(index-1); + Node nextNode = getNode(index+1); + previousNode.setNext(nextNode); + size--; + } + return get(index); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o, null); + if(head == null){ + head = node; + tail = node; + }else{ + node.setNext(head); + head = node; + } + size++; + } + public void addLast(Object o){ + Node node = new Node(o, null); + if(head == null){ + head = node; + tail = node; + }else{ + tail.setNext(node); + tail = node; + } + size++; + } + public Object removeFirst(){ + if (head == null){ + throw new RuntimeException("链表为空"); + } + if (size ==1){ + Object ret = head.getData(); + head = null; + tail = null; + return ret; + } + Object headData = head.getData(); + head = getNode(1); + size--; + return headData; + } + public Object removeLast(){ + if (head == null){ + throw new RuntimeException("链表为空"); + } + Object tailData = tail.getData(); + tail = getNode(size-2); + size--; + return tailData; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + private Object data; + private Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(size == 0 || size ==1){ + return; + } + /*LinkedList linkedList = new LinkedList(); + for(int i=size-1; i>=0; i--){ + linkedList.add(getNode(i)); + }*/ + + Node temp = head; + head = tail; + tail = temp; + for(int i=size-2; i>=1; i--){ + getNode(i+1).setNext(getNode(i)); + } + getNode(1).setNext(tail); + } + + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int pre = size/2; + for (int i=0; i101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group27/276961139/src/learn/List.java b/group27/276961139/src/learn/List.java new file mode 100644 index 0000000000..9b5fc82f83 --- /dev/null +++ b/group27/276961139/src/learn/List.java @@ -0,0 +1,9 @@ +package com.liam.learn.code2017; + +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/group27/276961139/src/learn/Queue.java b/group27/276961139/src/learn/Queue.java new file mode 100644 index 0000000000..dd6ed22559 --- /dev/null +++ b/group27/276961139/src/learn/Queue.java @@ -0,0 +1,32 @@ +package com.liam.learn.code2017; + +public class Queue { + + private LinkedList linkedList; + + public Queue() { + this.linkedList = new LinkedList(); + } + + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + if (linkedList == null || isEmpty()){ + return null; + } + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size()==0; + } + + public int size(){ + if (linkedList == null || isEmpty()){ + return 0; + } + return linkedList.size(); + } +} diff --git a/group27/276961139/src/learn/Stack.java b/group27/276961139/src/learn/Stack.java new file mode 100644 index 0000000000..1fc79645f1 --- /dev/null +++ b/group27/276961139/src/learn/Stack.java @@ -0,0 +1,23 @@ +package com.liam.learn.code2017; + +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/group27/383117348/.classpath b/group27/383117348/.classpath new file mode 100644 index 0000000000..d98690584e --- /dev/null +++ b/group27/383117348/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group27/383117348/.gitignore b/group27/383117348/.gitignore new file mode 100644 index 0000000000..9ded3b51b7 --- /dev/null +++ b/group27/383117348/.gitignore @@ -0,0 +1,21 @@ +*.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 +.idea/ +*.iml +rebel.* +.rebel.* + +target \ No newline at end of file diff --git a/group27/383117348/.project b/group27/383117348/.project new file mode 100644 index 0000000000..ccad29da35 --- /dev/null +++ b/group27/383117348/.project @@ -0,0 +1,17 @@ + + + 383117348Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/383117348/src/com/coding/basic/ArrayList.java b/group27/383117348/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..773d8f6c6b --- /dev/null +++ b/group27/383117348/src/com/coding/basic/ArrayList.java @@ -0,0 +1,213 @@ +package com.coding.basic; + +import java.util.Arrays; + +import org.junit.Test; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 自动增长值 + */ + private static final int GROW_UP_SIZE = 100; + + /** + * 在数组最末尾添加对象 + */ + public void add(Object o) { + growUp(size); + elementData[size] = o; + size++; + } + + /** + * 向指定下标处添加对象 + */ + public void add(int index, Object o) { + checkIndex(index); + growUp(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + + } + + /** + * 根据index获取对象 + */ + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + /** + * 移除指定下标对象 + */ + public Object remove(int index) { + checkIndex(index); + Object obj = elementData[index]; + int afterRemove = size - index - 1; + // System.out.println("@@@@"+afterRemove+"---"+index); + if (afterRemove > 0) + System.arraycopy(elementData, index + 1, elementData, index, afterRemove); + elementData = Arrays.copyOf(elementData, --size); + return obj; + } + + /** + * 获取数组大小 + */ + public int size() { + return size; + } + + /** + * 迭代器 + * + * @return + */ + public Iterator iterator() { + return new Iter(); + } + + /** + * 迭代器内部类 + * + * @author Adminstater + * + */ + private class Iter implements Iterator { + private int nextIndex = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return nextIndex != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + int i = nextIndex++; + if (i > size - 1) + throw new IndexOutOfBoundsException(); + + return elementData[i]; + } + + } + + /** + * 检查数组长度是否越界,越界就自动增长100 + */ + private void growUp(int size) { + if (size > elementData.length - 1) { + Object[] elementGrow = new Object[elementData.length + GROW_UP_SIZE]; + System.arraycopy(elementData, 0, elementGrow, 0, elementData.length); + elementData = elementGrow; + // System.out.println(elementData.length); + } + } + + /** + * 检查下标是否越界,越界抛出异常 + */ + private void checkIndex(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + /*------------------------------------------------------单元测试----------------------------------------------------*/ + /** + * 测试自增长数组长度变化及增加功能 + */ + @Test + public void TestAddFunction() { + ArrayList list = new ArrayList(); + for (int x = 0; x < 300; x++) { + list.add(x); + } + } + + /** + * 测试add(int index,Object obj)方法 + */ + @Test + public void TestAddIndexFunction() { + ArrayList list = new ArrayList(); + // System.out.println(list.size()); + list.add(0, 20); + list.add(1, 30); + System.out.println(list.get(1)); + + } + + /** + * 测试get方法 + */ + @Test + public void TestGetFunction() { + ArrayList list = new ArrayList(); + for (int x = 0; x < 300; x++) { + list.add(x); + } + for (int x = 0; x < list.size; x++) { + System.out.println(list.get(x)); + } + } + + /** + * 测试size方法 + */ + @Test + public void TestSizeFunction() { + ArrayList list = new ArrayList(); + for (int x = 0; x < 259; x++) { + list.add(x); + } + /* + * for(int x=0;x list = null; + } + +} diff --git a/group27/383117348/src/com/coding/basic/BinaryTreeNode.java b/group27/383117348/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..bd2ad5effb --- /dev/null +++ b/group27/383117348/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,115 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode root; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Comparable getData() { + return data; + } + + public void setData(Comparable 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 getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + /** + * 插入节点方法 + * + * @param o + * @return + */ + public BinaryTreeNode insert(Comparable o) { + if (o != null) { + root = insert(root, o); + //System.out.println("@@@@@@"+root.getData()); + } + return root; + } + + /** + * 递归比较插入平衡树 + * + * @param node + * @param o + * @return + */ + private BinaryTreeNode insert(BinaryTreeNode node, Comparable o) { + if (node == null) { + node = new BinaryTreeNode(); + node.setData(o); + } else if (compare(node, o) >= 0) { + node.left = insert(node.left, o); + //System.out.println(o+":insert into left"); + //System.out.println("left:"+node.getData()+":"+o); + } else if (compare(node, o) < 0) { + node.right = insert(node.right, o); + //System.out.println(o+":insert into right"); + //System.out.println("right:"+node.getData()+":"+o); + } + return node; + } + + /** + * 比较内容大小 + * + * @param node + * @param o + * @return + */ + private int compare(BinaryTreeNode node, Comparable o) { + // TODO Auto-generated method stub + return node.data.compareTo(o); + } + + /*-----------------------------------------------------单元测试-----------------------------------------------------*/ + + private void printTree() { + printTree(root); + } + + private void printTree(BinaryTreeNode node) { + if (node == null) + return; + printTree(node.left); + System.out.println("---"+node.data + ""); + printTree(node.right); + } + + public static void main(String[] args) { + BinaryTreeNode tree = new BinaryTreeNode(); + for (int x = 0; x < 100; x++) { + Double i = new Double(Math.random() * 100); + //System.out.println(i); + tree.insert(i); + } + System.out.println("@@@@@@" + tree.getRoot().getData()); + tree.printTree(); + } +} diff --git a/group27/383117348/src/com/coding/basic/Iterator.java b/group27/383117348/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group27/383117348/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/group27/383117348/src/com/coding/basic/LinkedList.java b/group27/383117348/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..faeafffca6 --- /dev/null +++ b/group27/383117348/src/com/coding/basic/LinkedList.java @@ -0,0 +1,382 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +import org.junit.Test; + +public class LinkedList implements List { + + private Node first; + + private Node last; + + private int size; + + /** + * 头部添加节点方法 + */ + private void linkFirst(Object o) { + final Node node = first; + final Node firstNode = new Node(o, node, null); + this.first = firstNode; + if (node == null) { + last = firstNode; + } else { + node.prev = firstNode; + } + size++; + } + + /** + * 末端添加节点方法 + * + * @param o + */ + private void linkLast(Object o) { + final Node node = last; + final Node lastNode = new Node(o, null, node); + last = lastNode; + if (node == null) { + first = lastNode; + } else { + node.next = lastNode; + } + size++; + } + + /** + * 在链表末端添加节点 + */ + public void add(Object o) { + if (o != null) + linkLast(o); + } + + /** + * 在指定下标处添加节点 + */ + public void add(int index, Object o) { + checkIndex(index); + Node befo = getNodeByIndex(index); + linkBefore(o, befo); + } + + /** + * 根据下标获取节点 + */ + public Object get(int index) { + checkIndex(index); + return getNodeByIndex(index).data; + } + + /** + * 根据下标移除节点 + */ + public Object remove(int index) { + checkIndex(index); + Node current = getNodeByIndex(index); + final Node node = current; + final Node prev = node.prev; + final Node next = node.next; + if (prev == null) { + first = next; + } else { + prev.next = next; + next.prev = prev; + current = null; + } + if (next == null) { + last = prev; + } else { + prev.next = next; + next.prev = prev; + current = null; + } + size--; + return node.data; + } + + /** + * 返回链表长度 + */ + public int size() { + return size; + } + + /** + * 在链表头添加节点 + * + * @param o + */ + public void addFirst(Object o) { + linkFirst(o); + } + + /** + * 在链表最后添加节点 + * + * @param o + */ + public void addLast(Object o) { + linkLast(o); + } + + /** + * 移除链表首个元素 + * + * @return + */ + public Object removeFirst() { + Node node = first; + if (node == null) + throw new NoSuchElementException(); + else { + Node next = node.next; + if (next == null) + first = null; + else { + first = next; + first.prev = null; + } + } + size--; + return node.data; + } + + /** + * 移除链表最后一个元素 + * + * @return + */ + public Object removeLast() { + Node node = last; + if (last == null) + throw new NoSuchElementException(); + else { + Node prev = node.prev; + if (prev == null) + last = null; + else { + last = prev; + last.next = null; + } + } + size--; + return node.data; + } + + /** + * 迭代方法 + * + * @return + */ + public Iterator iterator() { + return new Iter(); + } + + /** + * 在节点之前插入一个新的节点 + * + * @param data + * @param befo + */ + private void linkBefore(Object data, Node befo) { + final Node prev = befo.prev; + final Node node = new Node(data, befo, prev); + befo.prev = node; + if (prev == null) + first = node; + else + prev.next = node; + size++; + } + + /** + * 根据下标获取节点 + * + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + checkIndex(index); + + Node node = first; + for (int x = 0; x < index; x++) { + node = node.next; + } + return node; + } + + /** + * 检查下标是否越界 + * + * @param index + */ + private void checkIndex(int index) { + // TODO Auto-generated method stub + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("链表下标越界"); + } + } + + /** + * 迭代器内部类 + * + * @author Adminstater + * + */ + private class Iter implements Iterator { + private int nextIndex = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return nextIndex != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + int i = nextIndex++; + if (i > size - 1) + throw new IndexOutOfBoundsException(); + return getNodeByIndex(i).data; + } + + } + + /** + * 节点内部类 + * + * @author Adminstater + * + */ + private static class Node { + Object data; + Node next; + Node prev; + + private Node(Object data, Node next, Node prev) { + this.data = data; + this.next = next; + this.prev = prev; + } + + private Node() { + + } + } + + /*------------------------------------------------------单元测试----------------------------------------------------*/ + + /** + * 测试添加方法功能 + */ + // add(Object o) + @Test + public void TestAddFunction() { + + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println("添加完毕"); + + } + + // add(int index,Object o) + @Test + public void TestAddIndexFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + for (int x = 0; x < list.size(); x++) { + System.out.println(list.get(x)); + } + list.add(3, 111); + System.out.println(list.get(3)); + } + + // addFirst(Object o) + @Test + public void TestAddFirstFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(0)); + list.addFirst(20); + System.out.println(list.get(0)); + } + + // addLast(Object o) + @Test + public void TestAddLastFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(list.size() - 1)); + list.addLast(111); + System.out.println(list.get(list.size() - 1)); + } + + /** + * remove方法测试 + */ + // removeFirst() + @Test + public void TestRemoveFirst() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(0)); + list.removeFirst(); + System.out.println(list.get(0)); + } + + // removeLast() + @Test + public void TestRemoveLast() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(list.size() - 1)); + list.removeLast(); + System.out.println(list.get(list.size() - 1)); + } + + // remove(int index) + @Test + public void TestRemoveFunction() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + System.out.println(list.get(50)); + list.remove(50); + System.out.println(list.get(50)); + } + + /** + * Iterator测试 + */ + @Test + public void TestIterator() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + Iterator ite = list.iterator(); + while (ite.hasNext()) { + System.out.println(ite.next()); + } + } + + public static void main(String[] args) { + java.util.LinkedList list = null; + } +} diff --git a/group27/383117348/src/com/coding/basic/List.java b/group27/383117348/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group27/383117348/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/group27/383117348/src/com/coding/basic/Queue.java b/group27/383117348/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..84cb43e3db --- /dev/null +++ b/group27/383117348/src/com/coding/basic/Queue.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +import org.junit.Test; + +public class Queue { + private int size = 0; + private LinkedList linkedList = new LinkedList(); + + /** + * 入队方法 + * + * @param o + */ + public void enQueue(Object o) { + linkedList.add(o); + size++; + } + + /** + * 出队方法 + * + * @return + */ + public Object deQueue() { + Object result = linkedList.removeFirst(); + size--; + return result; + } + + /** + * 判断队列是否为空 + * + * @return + */ + public boolean isEmpty() { + return size == 0; + } + + /** + * 获取队列的长度 + * + * @return + */ + public int size() { + return size; + } + + /*------------------------------------------------------单元测试----------------------------------------------------*/ + /** + * 入队测试 + */ + @Test + public void enQueueTest() { + Queue queue = new Queue(); + queue.enQueue(1); + } + + /** + * 出队测试 + */ + @Test + public void deQueueTest() { + Queue queue = new Queue(); + for (int x = 0; x < 100; x++) { + queue.enQueue(x); + } + for (int x = 0; x < queue.size();) { + System.out.println(queue.deQueue()); + } + } + + public static void main(String[] args) { + } +} diff --git a/group27/383117348/src/com/coding/basic/Stack.java b/group27/383117348/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1b12b63b0f --- /dev/null +++ b/group27/383117348/src/com/coding/basic/Stack.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +import org.junit.Test; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + /** + * 压栈方法 + * + * @param o + */ + public void push(Object o) { + if (o != null) + elementData.add(o); + } + + /** + * 弹栈方法 + * + * @return + */ + public Object pop() { + Object result = elementData.remove(elementData.size() - 1); + return result; + } + + /** + * 查看栈顶对象 + * + * @return + */ + public Object peek() { + Object result = elementData.get(elementData.size() - 1); + return result; + } + + /** + * 判断是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 获取栈的长度 + * + * @return + */ + public int size() { + return elementData.size(); + } + + /*------------------------------------------------------单元测试----------------------------------------------------*/ + /** + * push(Object obj)方法测试 + */ + @Test + public void TestPushFunction() { + Stack stack = new Stack(); + for (int x = 0; x < 100; x++) { + stack.push(x); + } + } + + /** + * peek()方法测试 + */ + @Test + public void TestPeekFunction() { + Stack stack = new Stack(); + for (int x = 0; x < 100; x++) { + stack.push(x); + } + for (int x = 0; x < stack.size(); x++) { + System.out.println(x + ":" + stack.peek()); + } + } + + /** + * pop()方法测试 + */ + @Test + public void TestPopFunction() { + Stack stack = new Stack(); + for (int x = 0; x < 100; x++) { + stack.push(x); + } + System.out.println("before:" + stack.size()); + for (int x = 0; x < stack.size();) { + System.out.println(stack.pop()); + } + System.out.println("after:" + stack.size()); + } + +} diff --git a/group27/425044891/src/com/coding/basic/ArrayList.java b/group27/425044891/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..929a41661c --- /dev/null +++ b/group27/425044891/src/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +/** + * ArrayList + * + * @author Guang + * @date 2017年3月12日 下午1:55:47 + */ +public class ArrayList implements List { + + /** + * 数组中元素个数 + */ + private int size = 0; + + /** + * 数组 + */ + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size >= 100 || size < 0) { + throw new IndexOutOfBoundsException("越界."); + } + if (null == o) { + return; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkRangeForAdd(index); + if (null == o) { + return; + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + private void checkRangeForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("越界."); + } + } + + private void checkRangeForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("越界."); + } + } + + public Object get(int index) { + checkRangeForGet(index); + return elementData[index]; + } + + /** + * 移除第index位置的元素,后续元素前移 + */ + public Object remove(int index) { + checkRangeForGet(index); + int i = index; + Object o = elementData[index]; + for (; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[i] = null; + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + int index = 0; + @Override + public Object next() { + checkRangeForGet(index); + Object o = elementData[index]; + index++; + return o; + } + + @Override + public boolean hasNext() { + return index < size && index >= 0; + } + }; + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/BinaryTreeNode.java b/group27/425044891/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..c702b191e4 --- /dev/null +++ b/group27/425044891/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode> { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T o) { + if (o.compareTo(data) <= 0) { + return getLeft().insert(o); + } else { + return getRight().insert(o); + } + } + +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Iterator.java b/group27/425044891/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..de308cef25 --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/LinkedList.java b/group27/425044891/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..0c8520c62e --- /dev/null +++ b/group27/425044891/src/com/coding/basic/LinkedList.java @@ -0,0 +1,274 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = null; + + private int size = 0; + + public LinkedList() { + this.head = new Node(); + this.head.next = null; + this.head.data = null; + this.size = 0; + } + + public void add(Object o) { + if (null == o) { + return; + } + Node next = new Node(); + next.data = o; + next.next = null; + + /** + * 寻找尾部节点 + */ + Node p = head; + while (p.next != null) { + p = p.next; + } + p.next = next; + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (null == o) { + return; + } + Node next = new Node(); + next.data = o; + next.next = null; + + Node p = head.next; + Node pre = head; + int pos = 0; + while (p != null && pos < index) { + pre = p; + p = p.next; + pos++; + } + next.next = p; + pre.next = next; + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + int pos = 0; + while (p != null && pos < index) { + p = p.next; + pos++; + } + return p == null ? null : p.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Node pre = p; + int pos = 0; + while (p != null && pos < index) { + pre = p; + p = p.next; + pos++; + } + Object o = p.data; + pre.next = p.next; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (null == o) { + return; + } + Node next = new Node(); + next.next = null; + next.data = o; + + next.next = head.next; + head.next = next; + size++; + } + + public void addLast(Object o) { + if (null == o) { + return; + } + Node p = head; + while (p.next != null) { + p = p.next; + } + + Node next = new Node(); + next.data = o; + next.next = null; + + p.next = next; + size++; + } + + public Object removeFirst() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Object o = p.data; + + head.next = p.next; + size--; + return o; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Node pre = head; + while (p.next != null) { + pre = p; + p = p.next; + } + Object o = p.data; + pre.next = p.next; + size--; + return o; + } + + public Iterator iterator() { + return new Iterator() { + Node current = head; + + @Override + public Object next() { + Object o = current.next.data; + current = current.next; + return o; + } + + @Override + public boolean hasNext() { + return current.next != null; + } + }; + } + + private static class Node { + Object data; + Node next; + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node p = head.next.next; + Node pre = head.next; + pre.next = null; + while (p != null) { + Node pp = p.next; + p.next = pre; + pre = p; + p = pp; + } + head.next = pre; + // --- --- --- + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 从当前链表中取出那些listB所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + + public static void main(String[] args) { + + LinkedList list = new LinkedList(); + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.reverse(); + Iterator it = list.iterator(); + while (it.hasNext()) { + Object o = it.next(); + System.out.println(o); + } + + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/List.java b/group27/425044891/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group27/425044891/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Queue.java b/group27/425044891/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fcec6ac57c --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Queue.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Queue { + + private Object[] elementData = new Object[100]; + + int size = 0; + + public void enQueue(Object o) { + if (size < 0 || size >= 100) { + throw new IndexOutOfBoundsException(); + } + elementData[size++] = o; + } + + public Object deQueue() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Object o = elementData[0]; + int i = 0; + for (; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[i] = null; + size--; + return o; + } + + public boolean isEmpty() { + return elementData.length != 0; + } + + public int size() { + return elementData.length; + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Stack.java b/group27/425044891/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1217d25fbe --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + int size = elementData.size(); + elementData.add(size, o); + } + + public Object pop() { + int size = elementData.size(); + return elementData.remove(size - 1); + } + + public Object peek() { + int size = elementData.size(); + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return elementData.size() != 0; + } + + public int size() { + return elementData.size(); + } +} \ No newline at end of file diff --git a/group27/513274874/homework/.classpath b/group27/513274874/homework/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group27/513274874/homework/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group27/513274874/homework/.gitignore b/group27/513274874/homework/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group27/513274874/homework/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group27/513274874/homework/.project b/group27/513274874/homework/.project new file mode 100644 index 0000000000..b6d8ce6204 --- /dev/null +++ b/group27/513274874/homework/.project @@ -0,0 +1,17 @@ + + + coding2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/513274874/homework/src/com/coding/basic/ArrayList.java b/group27/513274874/homework/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1b6f5c4e7d --- /dev/null +++ b/group27/513274874/homework/src/com/coding/basic/ArrayList.java @@ -0,0 +1,120 @@ + +package com.coding.basic; + +import java.util.Arrays; + +/** + * @autor zhougd 20170306 + * 数组实现ArrayList + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + //扩容默认值 + private static final int INCREAMENT_CAP = 10; + + //含参数的构造函数 + public ArrayList(int size, Object[] elementData) { + this.size = size; + this.elementData = elementData; + } + + //默认100容量的构造函数 + public ArrayList() { + this.size = 0; + this.elementData = new Object[100]; + } + + @Override + public void add(Object o) { + //判断超过容量自动扩容 + if (this.size + 1 > this.elementData.length) { + increase(); + } + this.elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index out of bound!"); + } + //判断超过容量自动扩容 + if (this.size + 1 > this.elementData.length) { + increase(); + } + this.size++; + //index后面数组后移一位 + for (int cur = this.size; cur > index; cur--) { + this.elementData[cur] = this.elementData[cur - 1]; + } + + this.elementData[index] = o; + + } + + public Object get(int index) { + if (index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index out of bound!"); + } + return this.elementData[index]; + } + + public Object remove(int index) { + Object o = this.get(index); + this.size--; + //index后面的数向前移动一位 + for (int cur = index + 1; cur < this.size; cur++) { + this.elementData[cur] = this.elementData[cur + 1]; + } + return o; + } + + public int size() { + return this.size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + @Override + public String toString() { + String arrayStr = "ArrayList{ size = " + this.size() + " , "; + + arrayStr += "elementData=["; + for(int i = 0 ;isize()){ + throw new IndexOutOfBoundsException("Index out of bound !"); + } + } + + private Node getNode(int index ){ + + Node node = this.head; + for(int i = 0 ;i +* @since
三月 6, 2017
+* @version 1.0 +*/ +public class ArrayListTest { + +@Before +public void before() throws Exception { + +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: add(Object o) +* +*/ +@Test +public void testAddO() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + + System.out.println(arrayList); + + assert(arrayList.size() == 202); +} + +/** +* +* Method: add(int index, Object o) +* +*/ +@Test +public void testAddForIndexO() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + arrayList.add(3,"添加"); + //arrayList.add(100,3); + assert(arrayList.size() == 6); + System.out.println(arrayList); +} + +/** +* +* Method: get(int index) +* +*/ +@Test +public void testGet() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + assert(((Integer)arrayList.get(3)).intValue() == 4); +} + +/** +* +* Method: remove(int index) +* +*/ +@Test +public void testRemove() throws Exception { + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + + arrayList.remove(3); + //arrayList.add(100,3); + assert(arrayList.size() == 4); + System.out.println(arrayList); +} + +/** +* +* Method: size() +* +*/ +@Test +public void testSize() throws Exception { +//TODO: Test goes here... + + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + + System.out.println(arrayList); + + assert(arrayList.size() == 202); +} + +/** +* +* Method: iterator() +* +*/ +@Test +public void testIterator() throws Exception { +//TODO: Test goes here... + ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add(100); + arrayList.add(20); + + for(int i = 1 ;i <= 200 ;i++){ + arrayList.add(new Random().nextInt(100)); + } + System.out.println(arrayList); + + Iterator iterator = arrayList.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + ","); + } + + assert(arrayList.size() == 202); +} + +} diff --git a/group27/772642286/basic/ArrayList.java b/group27/772642286/basic/ArrayList.java new file mode 100644 index 0000000000..108eb02ff4 --- /dev/null +++ b/group27/772642286/basic/ArrayList.java @@ -0,0 +1,80 @@ +package week01.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){ + add(size , o); + } + public void add(int index, Object o){ + if(index<0||index > size){ + throw new ArrayIndexOutOfBoundsException(index); + } + size++; + if(size>=elementData.length){ + expand(); + } + for(int i = size -1 ;i> index; i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + + public Object get(int index){ + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException(index); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException(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 new ArrayListIterator(); + } + + class ArrayListIterator implements Iterator{ + + int count = 0; + @Override + public boolean hasNext() { + count++; + if(size<= count){ + return false; + } + return true; + } + + @Override + public Object next() { + return elementData[count]; + } + + } + + + private void expand(){ + elementData = Arrays.copyOf(elementData, elementData.length*2); + } + +} diff --git a/group27/772642286/basic/BinaryTreeNode.java b/group27/772642286/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..75a9327421 --- /dev/null +++ b/group27/772642286/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +package week01.basic; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T o){ + return insert(this,o); + } + + + public BinaryTreeNode insert(BinaryTreeNode root,T o){ + if(o.compareTo(root) >= 0){ + if(root.left == null){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + root.left = binaryTreeNode; + binaryTreeNode.data = o; + return binaryTreeNode; + } + return insert(root.left, o); + } + else{ + if (root.right == null) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + root.right = binaryTreeNode; + binaryTreeNode.data = o; + return binaryTreeNode; + } + return insert(root.right, o); + } + } + + +} diff --git a/group27/772642286/basic/Iterator.java b/group27/772642286/basic/Iterator.java new file mode 100644 index 0000000000..305ed43120 --- /dev/null +++ b/group27/772642286/basic/Iterator.java @@ -0,0 +1,7 @@ +package week01.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group27/772642286/basic/LinkedList.java b/group27/772642286/basic/LinkedList.java new file mode 100644 index 0000000000..98cf2d651b --- /dev/null +++ b/group27/772642286/basic/LinkedList.java @@ -0,0 +1,208 @@ +package week01.basic; + +import java.util.Objects; + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size; + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if(Objects.isNull(head)){ + head = node; + tail = head; + size++; + return ; + } + tail.next = node; + tail = node; + size++; + } + + public void add(int index , Object o){ + if(index<0 || index >size){ + throw new ArrayIndexOutOfBoundsException(index); + } + if(Objects.isNull(head)||index==size){ + add(o); + return; + } + + Node headNode = getNode(index - 1); + Node temp = headNode.next; + Node node = new Node(); + node.data = o; + node.next = temp; + headNode.next = node; + size++; + } + + + public Object get(int index){ + if(index< 0 || index >= size){ + throw new ArrayIndexOutOfBoundsException(index); + } + + return getNode(index).data; + } + + private Node getNode(int index){ + + + int count = 0; + Node headNode = head; + while(count< index){ + headNode = headNode.next; + count++; + } + return headNode; + } + + public Object remove(int index){ + if(index< 0 || index >= size){ + throw new ArrayIndexOutOfBoundsException(index); + } + + if(index==0){ + Node node = head; + head = head.next; + size --; + return node.data; + } + + Node headNode = getNode(index - 1); + Node node = headNode.next; + head.next = node.next; + size --; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator{ + + private Node currentNode; + + @Override + public boolean hasNext() { + if(currentNode==null){ + currentNode = head; + }else{ + currentNode = currentNode.next; + } + return Objects.nonNull(currentNode); + } + + @Override + public Object next() { + return currentNode.data; + } + + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group27/772642286/basic/List.java b/group27/772642286/basic/List.java new file mode 100644 index 0000000000..912285e171 --- /dev/null +++ b/group27/772642286/basic/List.java @@ -0,0 +1,9 @@ +package week01.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/group27/772642286/basic/Queue.java b/group27/772642286/basic/Queue.java new file mode 100644 index 0000000000..320280635f --- /dev/null +++ b/group27/772642286/basic/Queue.java @@ -0,0 +1,21 @@ +package week01.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(elementData.size()); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group27/772642286/basic/Stack.java b/group27/772642286/basic/Stack.java new file mode 100644 index 0000000000..0e6901c0ce --- /dev/null +++ b/group27/772642286/basic/Stack.java @@ -0,0 +1,23 @@ +package week01.basic; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group27/772642286/test/ArrayListTest.java b/group27/772642286/test/ArrayListTest.java new file mode 100644 index 0000000000..e8409a7d40 --- /dev/null +++ b/group27/772642286/test/ArrayListTest.java @@ -0,0 +1,55 @@ +package week01.test; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.ArrayList; +import week01.basic.Iterator; + + +public class ArrayListTest { + ArrayList list = null; + + @Before + public void init(){ + list = new ArrayList(); + for(int i=1;i<=500;i++){ + list.add(i); + } + } + + @Test + public void addTest(){ + Assert.assertEquals(500, list.size()); + for(int i=1;i<=list.size();i++){ + Assert.assertEquals(i, list.get(i-1)); + } + } + + @Test + public void addIndexTest(){ + list.add(250, 3333); + Assert.assertEquals(3333, list.get(250)); + Assert.assertEquals(500, list.get(500)); + } + + @Test + public void removeIndexTest(){ + list.remove(250); + Assert.assertEquals(499, list.size()); + Assert.assertEquals(252, list.get(250)); + Assert.assertEquals(500, list.get(498)); + } + + @Test + public void iteratorTest(){ + Iterator iterator = list.iterator(); + int count = 1; + while(iterator.hasNext()){ + Assert.assertEquals(++count, iterator.next()); + } + } + +} diff --git a/group27/772642286/test/BinaryTreeNodeTest.java b/group27/772642286/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..c0e12d2eb5 --- /dev/null +++ b/group27/772642286/test/BinaryTreeNodeTest.java @@ -0,0 +1,6 @@ +package week01.test; + +public class BinaryTreeNodeTest { + + +} diff --git a/group27/772642286/test/LinkedListTest.java b/group27/772642286/test/LinkedListTest.java new file mode 100644 index 0000000000..cae56aad19 --- /dev/null +++ b/group27/772642286/test/LinkedListTest.java @@ -0,0 +1,52 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.Iterator; +import week01.basic.LinkedList; + +public class LinkedListTest { + LinkedList list = null; + + @Before + public void init(){ + list = new LinkedList(); + for(int i=1;i<=500;i++){ + list.add(i); + } + } + + @Test + public void addTest(){ + Assert.assertEquals(500, list.size()); + for(int i=1;i<=list.size();i++){ + Assert.assertEquals(i, list.get(i-1)); + } + } + + @Test + public void addIndexTest(){ + list.add(250, 3333); + Assert.assertEquals(3333, list.get(250)); + Assert.assertEquals(500, list.get(500)); + } + + @Test + public void removeIndexTest(){ + list.remove(250); + Assert.assertEquals(499, list.size()); + Assert.assertEquals(252, list.get(250)); + Assert.assertEquals(500, list.get(498)); + } + + @Test + public void iteratorTest(){ + Iterator iterator = list.iterator(); + int count = 0; + while(iterator.hasNext()){ + Assert.assertEquals(++count, iterator.next()); + } + } +} diff --git a/group27/772642286/test/QueueTest.java b/group27/772642286/test/QueueTest.java new file mode 100644 index 0000000000..14008f51b2 --- /dev/null +++ b/group27/772642286/test/QueueTest.java @@ -0,0 +1,45 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; + +import week01.basic.Queue; + +public class QueueTest { + + private Queue queue; + + @Before + public void init(){ + queue = new Queue(); + for(int i=1;i<=500;i++){ + queue.enQueue(i); + } + } + + public void enQueueTest(){ + Assert.assertEquals(500, queue.size()); + } + + public void deQueue(){ + for(int i=500;i>=1 ;i--){ + Assert.assertEquals(i, queue.deQueue()); + } + + } + + public void isEmpty(){ + Assert.assertEquals(false, queue.isEmpty()); + for(int i=500;i>=1 ;i--){ + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertEquals(true, queue.isEmpty()); + } + + public void size(){ + for(int i=499;i>0 ;i--){ + queue.deQueue(); + Assert.assertEquals(i, queue.size()); + } + } +} diff --git a/group27/772642286/test/StackTest.java b/group27/772642286/test/StackTest.java new file mode 100644 index 0000000000..40f0928a14 --- /dev/null +++ b/group27/772642286/test/StackTest.java @@ -0,0 +1,58 @@ +package week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void init(){ + stack = new Stack(); + for(int i=1;i<=500;i++){ + stack.push(i); + } + } + + @Test + public void pushTest(){ + Assert.assertEquals(500, stack.size()); + } + + @Test + public void popTest(){ + for(int i=1;i<=500 ;i++){ + Assert.assertEquals(i, stack.pop()); + } + } + + @Test + public void peekTest(){ + Assert.assertEquals(1, stack.peek()); + Assert.assertEquals(1, stack.peek()); + Assert.assertEquals(1, stack.pop()); + Assert.assertEquals(2, stack.peek()); + Assert.assertEquals(2, stack.peek()); + } + + @Test + public void isEmpty(){ + Assert.assertEquals(false, stack.isEmpty()); + for(int i=1;i<=500 ;i++){ + Assert.assertEquals(i, stack.pop()); + } + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void size(){ + for(int i=499;i>0 ;i--){ + stack.pop(); + Assert.assertEquals(i, stack.size()); + } + } +} diff --git a/group27/815591664/2017Learning/.classpath b/group27/815591664/2017Learning/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group27/815591664/2017Learning/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group27/815591664/2017Learning/.gitignore b/group27/815591664/2017Learning/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group27/815591664/2017Learning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group27/815591664/2017Learning/.project b/group27/815591664/2017Learning/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group27/815591664/2017Learning/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group27/815591664/2017Learning/.settings/org.eclipse.core.resources.prefs b/group27/815591664/2017Learning/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..3af089907a --- /dev/null +++ b/group27/815591664/2017Learning/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/array/ArrayUtil.java=UTF-8 diff --git a/group27/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java b/group27/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7c30103d89 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,348 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; + +import java.util.List; + + +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 static void main(String[] args) { + int[] a = {7, 9, 30, 3, 4}; + reverseArray(a); + System.out.println(Arrays.toString(a)); + + + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5,0} ; + System.out.println(Arrays.toString(removeZero(oldArr))); + + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + + System.out.println(Arrays.toString(merge(a1,a2))); + + + int[] b = { 2,3,6}; + System.out.println(Arrays.toString(grow(b,5))); + + System.out.println(genFibonacci(5)); + + System.out.println(Arrays.toString(fibonacci(30))); + + System.out.println(Arrays.toString(getPrimes(10000))); + + System.out.println(getFactor(10)); + + System.out.println(isPerfectNum(1000)); + +// System.out.println(); + System.out.println(Arrays.toString(getPerfectNumbers(100))); + + System.out.println(join(a,"&")); + + + } + public static void reverseArray(int[] origin){ + + if(origin.length==0){ + return; + + } + int[] copy = new int[origin.length]; + System.arraycopy(origin, 0, copy, 0, origin.length); + + for (int i = 0; i < copy.length; i++) { + + origin[i] = copy[copy.length-1-i]; + } + + + } + + /** + * 现在有如下的一个数组: 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 static int[] removeZero(int[] oldArray){ + int newSize = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newSize++; + } + } + int index = 0; + int[] newArr = new int[newSize]; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newArr[index] = oldArray[i]; + index++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2){ + Arrays.sort(array1); + Arrays.sort(array2); + + int[] newArr = new int[array1.length+array2.length]; + + System.arraycopy(array1, 0, newArr, 0,array1.length ); + System.arraycopy(array2, 0, newArr, array1.length, array2.length); + Arrays.sort(newArr); + List list = new ArrayList(); + for(int i=0;i list = new ArrayList(); + for(int i =0;i list = new ArrayList(); + for(int i=2;i<=max;i++){ + if(isPrime(i)){ + list.add(i); + } + } + + return listToArray(list); + + + } + + public static int[] listToArray(List list){ + if(list == null){ + return null; + } + + int[] arr = new int[list.size()]; + + for(int i=0;i list = new ArrayList(); + for(int i=1;i<=max;i++){ + if(isPerfectNum(i)){ + list.add(i); + } + } + + return listToArray(list); + } + + + public static boolean isPerfectNum(int num){ + if(num <=1){ + return false; + } + + List factors = getFactor(num); + int sum = 0; + for (Integer integer : factors) { + sum = integer+sum; + } + if(sum == num){ + return true; + } + return false; + } + + public static List getFactor(int num){ + List list = new ArrayList(); + list.add(1); + + + for(int i=2;i getFactor(int num){ + List list = new ArrayList(); + list.add(1); + int temp = num; + + while(!isPrime(temp)){ + if(temp ==1){ + break; + } + for(int i=2;i<=temp;i++){ + if(temp % i ==0){ + list.add(i); + temp = temp/i; + break; + } + } + + } + list.add(temp); + + return list; + }*/ + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + + } + + return sb.subSequence(0, sb.length()-1).toString(); + } + + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java b/group27/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group27/815591664/2017Learning/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/group27/815591664/2017Learning/src/com/coderising/litestruts/Struts.java b/group27/815591664/2017Learning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group27/815591664/2017Learning/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/group27/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java b/group27/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group27/815591664/2017Learning/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/group27/815591664/2017Learning/src/com/coderising/litestruts/View.java b/group27/815591664/2017Learning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group27/815591664/2017Learning/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/group27/815591664/2017Learning/src/com/coderising/litestruts/struts.xml b/group27/815591664/2017Learning/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group27/815591664/2017Learning/src/com/coding/basic/ArrayList.java b/group27/815591664/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c983c04968 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * @author hugaoqing + * created on 2017-3-8 + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + + /* + * sizelengthʱbufferԭ + * + */ + public void add(Object o){ + if(elementData.length == size){ + Object[] buffer = new Object[size+15]; + System.arraycopy(elementData, 0, buffer, 0, size); + elementData = buffer; + elementData[size] = o; + size++; + }else { + + elementData[size] = o; + size++; + } + + } + + + /* + * ָλԪ + * size+1 lengthҪٽӲ + */ + public void add(int index, Object o) throws Exception{ + if(index <0){ + throw new Exception("Ϊ0"); + + }else if(index >= size){ + throw new Exception("ޣ"); + }else if(size+1=size){ + throw new Exception("ޣ"); + } + return elementData[index]; + } + + public Object remove(int index) throws Exception{ + if(index>=size){ + throw new Exception("ޣ"); + } + Object out = elementData[index]; + Object[] temp = new Object[size-index-1]; + System.arraycopy(elementData, index+1, temp, 0, size-index-1); + System.arraycopy(temp, 0, elementData,index, size-index-1); + + size--; + return out; + } + + public int size(){ + return this.size; + } + + @Override + public String toString() { + Object[] objs = new Object[size]; + System.arraycopy(elementData, 0,objs , 0, size); + return Arrays.toString(objs); + + } + public Iterator iterator(){ + return new Iterator() { + int cursor = 0; + public Object next() throws Exception { + cursor++; + return get(cursor-1); + } + + public boolean hasNext() { + + return this.cursor ll = new LinkedList(); + ll.add(null); + + + + } + + + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/BinaryTree.java b/group27/815591664/2017Learning/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..ed69a3f16c --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode insert(Integer data){ + BinaryTreeNode node = new BinaryTreeNode(data); + + + if(this.root==null){ + root = node; + root.setLeft(null); + root.setRight(null); + }else{ + BinaryTreeNode curNode = this.root; + + if(data.compareTo(root.getData())>0){ + while(curNode.getRight()!=null){ + curNode = curNode.getRight(); + } + curNode = node; + + }else{ + while(curNode.getLeft()!=null){ + curNode = curNode.getLeft(); + } + curNode = node; + } + + } + return null; + } + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group27/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..23e2b871a3 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,43 @@ +package com.coding.basic; + + + +/** + * @author hugaoqing + * created on 2017-3-11 + */ +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data) { + super(); + 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; + } + + /*public BinaryTreeNode insert(Object o){ + this.data = o; + return this; + }*/ + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/Iterator.java b/group27/815591664/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d4656a7daf --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + //ӿijԱĬ϶final static +// int cursor = 0; + public boolean hasNext(); + public Object next() throws Exception; + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..6f2fbf1f5b --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,236 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + + public void add(Object o){ + this.addLast(o); + + } + public void add(int index , Object o) throws Exception{ + if(index==0){ + this.addFirst(o); + return; + }else if(index==size-1){ + this.addLast(o); + return; + }else{ + + + Node curNode = this.getNode(index); + Node pre = curNode.previous; +// Node next = curNode.next; + // + Node newNode = new Node(o, pre, curNode); + curNode.previous = newNode; + pre.next = newNode; + } + size++; + + + } + private Node getNode(int index) throws Exception{ + if(index>=size){ + throw new Exception("±곬"); + } + if(index ==0){ + return head; + }else if(index==size-1){ + return tail; + + }else{ + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; + } + return temp; + } + } + public Object get(int index) throws Exception{ + if(index>=size){ + throw new Exception("±곬"); + } + if(index ==0){ + return head.data; + }else if(index==size-1){ + return tail.data; + + }else{ + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; + } + return temp.data; + } + } + public Object remove(int index) throws Exception{ + if(index>=size){ + throw new Exception("±곬"); + } + Object o = null; + if(size == 1){ + o = head.data; + }else{ + if(index==0){ + + Node afterHead = head.next; + afterHead.previous = null; + head = afterHead; + o = head.data; + + }else if(index == size-1){ + Node beforeTail = tail.previous; + beforeTail.next = null; + tail = beforeTail; + o = tail.data; + }else{ + Node curNode = this.getNode(index); + Node pre = curNode.previous; + Node next = curNode.next; + //мڶϿָ + Node temp = new Node(next.data, pre, next.next); + pre.next = temp; + next = temp; + o = curNode.data; + + } + + + + } + + size--; + return o; + + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(o, null, head); + + if(head == null){ + head = node; + tail = node; + }else{ + head.previous = node; + head = node; + } + size++; + + } + public void addLast(Object o){ + //½ڵpreviousָָtail + Node curNode = new Node(o, tail, null); + if(tail==null){ + //ǰΪʱýڵͷβΪýڵ + head = curNode; + tail = curNode; + }else{ + //Ϊʱһڵnextָָ¼Ľڵ㼴 + tail.next = curNode; + //¼ڵΪtail + tail = curNode; + + } + size++; + + } + public Object removeFirst() throws Exception{ + return this.remove(0); + } + public Object removeLast() throws Exception{ + return this.remove(size-1); + } + + private class Itr implements Iterator{ + int cursor = 0; + public boolean hasNext() { + return cursor