diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/ArrayList.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e9c2a390fb --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.github.ipk2015.coding2017.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); + } + /* + * 分两种情况,index的范围为0到size,超出则抛出异常 + * */ + public void add(int index, Object o){ + ListUtils.checkIndexInRange(index,size); + if(size==elementData.length){ + elementData=Arrays.copyOf(elementData, size+1); + } + if(indexindex;i--){ + elementData[i]=elementData[i-1]; + } + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + ListUtils.checkIndexInRange(index,size-1); + return elementData[index]; + } + + public Object remove(int index){ + ListUtils.checkIndexInRange(index,size-1); + Object object=elementData[index]; + for(int i=index;i0){ + if(null==compareNode.getLeft()){ + compareNode.setLeft(insertNode); + break; + } + compareNode=compareNode.getLeft(); + }else if(result<0){ + if(null==compareNode.getRight()){ + compareNode.setRight(insertNode); + break; + } + compareNode=compareNode.getRight(); + } + } + } + + return insertNode; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Iterator.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..145acc5ef7 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.ipk2015.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..d10c40a563 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java @@ -0,0 +1,244 @@ +package com.github.ipk2015.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + if(null==head){ + head=new Node(); + head.data=o; + }else{ + Node node=head; + while(null!=node.next){ + node=node.next; + } + Node addNode=new Node(); + addNode.data=o; + node.next=addNode; + } + } + public void add(int index , Object o){ + int size=size(); + ListUtils.checkIndexInRange(index, size); + if(index==size){ + add(o); + }else{ + if(size==0){ + head=new Node(); + head.data=o; + }else{ + Node node=head; + Node addNode=new Node(); + addNode.data=o; + for(int i=0;i7->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 rsection( LinkedList list){ + return null; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/List.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/List.java new file mode 100644 index 0000000000..4e2f4036c0 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/List.java @@ -0,0 +1,8 @@ +package com.github.ipk2015.coding2017.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/group24/121111914/src/com/github/ipk2015/coding2017/basic/ListUtils.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/ListUtils.java new file mode 100644 index 0000000000..4d73d7eec6 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/ListUtils.java @@ -0,0 +1,14 @@ +package com.github.ipk2015.coding2017.basic; + +public class ListUtils { + public static boolean checkIndexInRange(int index,int range){ + if(index>=0 && index<=range){ + return true; + } + throw new IndexOutOfBoundsException(); + } + + public static void log(String msg){ + System.out.println(msg); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java new file mode 100644 index 0000000000..6342314df6 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java @@ -0,0 +1,19 @@ +package com.github.ipk2015.coding2017.basic; + +public class Queue { + private LinkedList elementDatas=new LinkedList(); + public void enQueue(Object o){ + elementDatas.add(o); + } + public Object deQueue(){ + return elementDatas.removeFirst(); + } + + public boolean isEmpty(){ + return size()==0; + } + + public int size(){ + return elementDatas.size(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java new file mode 100644 index 0000000000..4dae60e12b --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java @@ -0,0 +1,32 @@ +package com.github.ipk2015.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + Object data=elementData.remove(size()-1); + return data; + } + + public Object peek(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/ArrayListTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..72a9a84b66 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,53 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.ArrayList; + +public class ArrayListTest { + ArrayList list; + @Before + public void setUp() throws Exception { + list=new ArrayList(); + + } + @Test + public void testAddObject() { + list.add("hehe1"); + assertEquals("hehe1", list.get(0)); + } + + @Test + public void testAddIntObject() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.add(1, "arm"); + assertEquals("arm", list.get(1)); + } + + @Test + public void testGet() { + list.add("hehe1"); + assertEquals("hehe1", list.get(0)); + } + + @Test + public void testRemove() { + list.add("hehe1"); + assertEquals("hehe1", list.remove(0)); + } + + @Test + public void testSize() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + + assertEquals(3, list.size()); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..4b2fe88ee0 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,29 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + BinaryTreeNode node=new BinaryTreeNode(); + + node.setData(5); + node.insert(2); + node.insert(7); + node.insert(1); + node.insert(4); + node.insert(3); + assertEquals(3,node.getLeft().getRight().getLeft().getData()); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/LinkedListTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..57fed394d6 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,92 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.LinkedList; + +public class LinkedListTest { + LinkedList list; + @Before + public void setUp() throws Exception { + list=new LinkedList(); + } + + @Test + public void testAddObject() { + list.add("hehe1"); + list.add("hehe2"); + assertEquals("hehe2", list.get(1)); + } + + @Test + public void testAddIntObject() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.add(1,"arm"); + assertEquals("arm", list.get(1)); + } + + @Test + public void testGet() { + list.add("hehe1"); + list.add("hehe2"); + assertEquals("hehe2", list.get(1)); + } + + @Test + public void testRemoveInt() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.remove(1); + assertEquals(2, list.size()); + } + + @Test + public void testSize() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + assertEquals(3, list.size()); + } + + @Test + public void testAddFirst() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.addFirst("arm"); + assertEquals("arm", list.get(0)); + } + + @Test + public void testAddLast() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.addLast("arm"); + assertEquals("arm", list.get(list.size()-1)); + } + + @Test + public void testRemoveFirst() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.removeFirst(); + assertEquals("hehe2", list.get(0)); + } + @Test + public void testRemoveLast() { + list.add("hehe1"); + list.add("hehe2"); + list.add("hehe3"); + list.removeLast(); + assertEquals("hehe2", list.get(list.size()-1)); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/QueueTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..53c63d8564 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/QueueTest.java @@ -0,0 +1,49 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.Queue; + +public class QueueTest { + Queue queue; + @Before + public void setUp() throws Exception { + queue=new Queue(); + } + + @Test + public void testEnQueue() { + queue.enQueue("hehe1"); + queue.enQueue("hehe2"); + assertEquals(2, queue.size()); + } + + @Test + public void testDeQueue() { + queue.enQueue("hehe1"); + queue.enQueue("hehe2"); + queue.deQueue(); + assertEquals(1, queue.size()); + } + + @Test + public void testIsEmpty() { + queue.enQueue("hehe1"); + queue.enQueue("hehe2"); + queue.deQueue(); + queue.deQueue(); + assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + queue.enQueue("hehe1"); + queue.enQueue("hehe2"); + queue.deQueue(); + assertEquals(1, queue.size()); + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..7cc10b77df --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java @@ -0,0 +1,57 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.Stack; + +public class StackTest { + Stack stack; + @Before + public void setUp() throws Exception { + stack=new Stack(); + } + + @Test + public void testPush() { + stack.push("hehe1"); + stack.push("hehe2"); + assertEquals(2,stack.size()); + } + + @Test + public void testPop() { + stack.push("hehe1"); + stack.push("hehe2"); + stack.push("hehe3"); + assertEquals(true,stack.pop()=="hehe3" && stack.size()==2); + } + + @Test + public void testPeek() { + stack.push("hehe1"); + stack.push("hehe2"); + stack.push("hehe3"); + assertEquals(true,stack.peek()=="hehe3" && stack.size()==3); + } + + @Test + public void testIsEmpty() { + stack.push("hehe1"); + stack.push("hehe2"); + stack.pop(); + stack.pop(); + assertEquals(true,stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push("hehe1"); + stack.push("hehe2"); + stack.pop(); + assertEquals(1,stack.size()); + } + +}