From aca0e519c326e036b9b2f1d84ba2ef9f5865e9e7 Mon Sep 17 00:00:00 2001 From: BlindingDark Date: Fri, 10 Mar 2017 18:56:23 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ajie9608 <1778842360@qq.com> --- group26/1778842360/first heomework/.classpath | 6 + group26/1778842360/first heomework/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../first heomework/src/ArrayList.java | 149 +++++++++++++ .../first heomework/src/ArrayTest.java | 27 +++ .../first heomework/src/BinaryTreeNode.java | 34 +++ .../first heomework/src/Iterator.java | 7 + .../first heomework/src/LinkedList.java | 209 ++++++++++++++++++ .../first heomework/src/LinkedTest.java | 34 +++ .../1778842360/first heomework/src/List.java | 8 + .../1778842360/first heomework/src/Queue.java | 28 +++ .../first heomework/src/QueueTest.java | 21 ++ .../1778842360/first heomework/src/Stack.java | 35 +++ .../first heomework/src/StackTest.java | 24 ++ 14 files changed, 610 insertions(+) create mode 100644 group26/1778842360/first heomework/.classpath create mode 100644 group26/1778842360/first heomework/.project create mode 100644 group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs create mode 100644 group26/1778842360/first heomework/src/ArrayList.java create mode 100644 group26/1778842360/first heomework/src/ArrayTest.java create mode 100644 group26/1778842360/first heomework/src/BinaryTreeNode.java create mode 100644 group26/1778842360/first heomework/src/Iterator.java create mode 100644 group26/1778842360/first heomework/src/LinkedList.java create mode 100644 group26/1778842360/first heomework/src/LinkedTest.java create mode 100644 group26/1778842360/first heomework/src/List.java create mode 100644 group26/1778842360/first heomework/src/Queue.java create mode 100644 group26/1778842360/first heomework/src/QueueTest.java create mode 100644 group26/1778842360/first heomework/src/Stack.java create mode 100644 group26/1778842360/first heomework/src/StackTest.java diff --git a/group26/1778842360/first heomework/.classpath b/group26/1778842360/first heomework/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group26/1778842360/first heomework/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group26/1778842360/first heomework/.project b/group26/1778842360/first heomework/.project new file mode 100644 index 0000000000..88536f7819 --- /dev/null +++ b/group26/1778842360/first heomework/.project @@ -0,0 +1,17 @@ + + + first heomework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs b/group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group26/1778842360/first heomework/.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/group26/1778842360/first heomework/src/ArrayList.java b/group26/1778842360/first heomework/src/ArrayList.java new file mode 100644 index 0000000000..79c83046be --- /dev/null +++ b/group26/1778842360/first heomework/src/ArrayList.java @@ -0,0 +1,149 @@ + +//package javaTest; + +import java.util.Arrays; + +public class ArrayList { + + private Object[] elementData=new Object[2]; + int size; + //添加元素 + public void add(Object o) + { + if(sizei;x--) + { + if(x==index+1) + { + elementData[x]=t; + } + else + { + elementData[x]=elementData[x-1]; + } + + } + } + } + } + public Iterator iterator() + { + return new ArrayListIterator(this); + } + private class ArrayListIterator implements Iterator + { + ArrayList l=null; + int pos=0; + private ArrayListIterator(ArrayList l) + { + this.l=l; + } + public boolean hasNext() { + // TODO Auto-generated method stub + boolean flag=true; + pos++; + if(pos>size) + { + flag=false; + } + return flag; + } + + public Object next() { + // TODO Auto-generated method stub + + return elementData[pos-1]; + } + } + + + + +} + diff --git a/group26/1778842360/first heomework/src/ArrayTest.java b/group26/1778842360/first heomework/src/ArrayTest.java new file mode 100644 index 0000000000..4a5a9eabe5 --- /dev/null +++ b/group26/1778842360/first heomework/src/ArrayTest.java @@ -0,0 +1,27 @@ + +//package javaTest; + +public class ArrayTest { + + /** + * @param args + * 模拟ArrayList实现对元素的增删查找 + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + ArrayList a=new ArrayList(); + a.add("hello"); + a.add("java"); + a.add("world"); + a.add("hello"); + // a.add(2,"hello"); + Iterator it=a.iterator(); + while(it.hasNext()) + { + System.out.print(it.next()+" "); + } + System.out.println(a.size()); + } + +} + diff --git a/group26/1778842360/first heomework/src/BinaryTreeNode.java b/group26/1778842360/first heomework/src/BinaryTreeNode.java new file mode 100644 index 0000000000..b5e788838e --- /dev/null +++ b/group26/1778842360/first heomework/src/BinaryTreeNode.java @@ -0,0 +1,34 @@ + +//package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} + diff --git a/group26/1778842360/first heomework/src/Iterator.java b/group26/1778842360/first heomework/src/Iterator.java new file mode 100644 index 0000000000..dbb092f46e --- /dev/null +++ b/group26/1778842360/first heomework/src/Iterator.java @@ -0,0 +1,7 @@ + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group26/1778842360/first heomework/src/LinkedList.java b/group26/1778842360/first heomework/src/LinkedList.java new file mode 100644 index 0000000000..0f63676418 --- /dev/null +++ b/group26/1778842360/first heomework/src/LinkedList.java @@ -0,0 +1,209 @@ +//package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + int size; + public void add(Object o){ + if(head==null) + { + head=new Node(o); + head.next=null; + head.data=o; + } + else{ + Node p=head; + { + while(p.next!=null) + { + p=p.next; + } + Node n=new Node(o); + p.next=n; + n.data=o; + n.next=null; + } + } + size++; + } + public void add(int index , Object o){ + int i=1; + Node p=head; + while(iindex) {return null;} + return p.data; + } + public Object remove(int index){ + int i=1; + Node p=head; + Object o=null; + if(index==1) + { + o=head.data; + if(head.next!=null) + { + p=head.next; + head.data=p.data; + p=head; + } + else{ + head=null; + } + } + else{ + while(i0) + { + flag=false; + } + return flag; + } + + public int size(){ + return l.size; + } +} + diff --git a/group26/1778842360/first heomework/src/QueueTest.java b/group26/1778842360/first heomework/src/QueueTest.java new file mode 100644 index 0000000000..81131511c9 --- /dev/null +++ b/group26/1778842360/first heomework/src/QueueTest.java @@ -0,0 +1,21 @@ + +//package com.coding.basic; + +public class QueueTest { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Queue q=new Queue(); + q.enQueue("add"); + q.enQueue("world"); + int length=q.size(); + System.out.println(length); + System.out.println(q.deQueue()); + System.out.println(q.isEmpty()); + } + +} + diff --git a/group26/1778842360/first heomework/src/Stack.java b/group26/1778842360/first heomework/src/Stack.java new file mode 100644 index 0000000000..20cdba1c4b --- /dev/null +++ b/group26/1778842360/first heomework/src/Stack.java @@ -0,0 +1,35 @@ + +//package javaTest; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + //将元素压入栈 + public void push(Object o){ + elementData.add(o); + } + //将元素出栈 + public Object pop(){ + Object o=elementData.remove(elementData.size-1); + return o; + } + //获取栈顶元素 + public Object peek(){ + Object o=elementData.get(elementData.size-1); + return o; + } + //判断栈是否为空 + public boolean isEmpty(){ + boolean flag=true; + if(elementData.size>0) + { + flag=false; + } + return flag; + } + //获取栈的大小 + public int size(){ + return elementData.size; + } +} + diff --git a/group26/1778842360/first heomework/src/StackTest.java b/group26/1778842360/first heomework/src/StackTest.java new file mode 100644 index 0000000000..271ba04daf --- /dev/null +++ b/group26/1778842360/first heomework/src/StackTest.java @@ -0,0 +1,24 @@ +//package javaTest; + +public class StackTest { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + Stack s=new Stack(); + s.push("world"); + s.push("java"); + s.push("world"); + Object o1=s.peek(); + System.out.println(o1); + Object o=s.pop(); + System.out.println(o); + int size=s.size(); + System.out.println(size); + System.out.println(s.isEmpty()); + } + +} + From 1e1f7ded7ecc5d2e1bf5910f5655bc93d6ec9149 Mon Sep 17 00:00:00 2001 From: BlindingDark Date: Fri, 10 Mar 2017 18:56:23 +0800 Subject: [PATCH 2/7] =?UTF-8?q?.gitignore=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group26/1778842360/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 group26/1778842360/.gitignore diff --git a/group26/1778842360/.gitignore b/group26/1778842360/.gitignore new file mode 100644 index 0000000000..4f4c622218 --- /dev/null +++ b/group26/1778842360/.gitignore @@ -0,0 +1,2 @@ +/bin +/.project/.classpath \ No newline at end of file From 5b3de6578f8cdaa46d69efccc207c39a202192ac Mon Sep 17 00:00:00 2001 From: luojunyi Date: Mon, 13 Mar 2017 13:39:51 +0800 Subject: [PATCH 3/7] Signed-off-by: luojunyi --- .../week1/com/coding/Test/ArrayListTest.java | 64 ++++ .../com/coding/Test/BinaryTreeNodeTest.java | 20 + .../week1/com/coding/Test/LinkedListTest.java | 90 +++++ .../src/week1/com/coding/Test/QueueTest.java | 39 ++ .../src/week1/com/coding/Test/StackTest.java | 31 ++ .../src/week1/com/coding/basic/ArrayList.java | 123 +++++++ .../com/coding/basic/BinaryTreeNode.java | 74 ++++ .../src/week1/com/coding/basic/Iterator.java | 9 + .../week1/com/coding/basic/LinkedList.java | 341 ++++++++++++++++++ .../src/week1/com/coding/basic/List.java | 14 + .../src/week1/com/coding/basic/Queue.java | 80 ++++ .../src/week1/com/coding/basic/Stack.java | 52 +++ 12 files changed, 937 insertions(+) create mode 100644 group26/191191717/src/week1/com/coding/Test/ArrayListTest.java create mode 100644 group26/191191717/src/week1/com/coding/Test/BinaryTreeNodeTest.java create mode 100644 group26/191191717/src/week1/com/coding/Test/LinkedListTest.java create mode 100644 group26/191191717/src/week1/com/coding/Test/QueueTest.java create mode 100644 group26/191191717/src/week1/com/coding/Test/StackTest.java create mode 100644 group26/191191717/src/week1/com/coding/basic/ArrayList.java create mode 100644 group26/191191717/src/week1/com/coding/basic/BinaryTreeNode.java create mode 100644 group26/191191717/src/week1/com/coding/basic/Iterator.java create mode 100644 group26/191191717/src/week1/com/coding/basic/LinkedList.java create mode 100644 group26/191191717/src/week1/com/coding/basic/List.java create mode 100644 group26/191191717/src/week1/com/coding/basic/Queue.java create mode 100644 group26/191191717/src/week1/com/coding/basic/Stack.java diff --git a/group26/191191717/src/week1/com/coding/Test/ArrayListTest.java b/group26/191191717/src/week1/com/coding/Test/ArrayListTest.java new file mode 100644 index 0000000000..b08cfe0dfc --- /dev/null +++ b/group26/191191717/src/week1/com/coding/Test/ArrayListTest.java @@ -0,0 +1,64 @@ +package week1.com.coding.Test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week1.com.coding.basic.ArrayList; +import week1.com.coding.basic.Iterator; + +public class ArrayListTest +{ + private ArrayList list = null; + + @Before + public void before() + { + list = new ArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + } + + public void testAdd() + { + list.add("a"); + list.add("b"); + list.add("c"); + } + + public void testRemove() + { + Assert.assertEquals("c", list.remove(2)); + } + + public void testSize() + { + Assert.assertEquals(3, list.size()); + } + + public void testGet() + { + Assert.assertEquals("c", list.get(2)); + } + + public void testInsert() + { + list.add(2, "e"); + for (int i = 0; i < list.size(); i++) + { + System.out.printf("%s\t", list.get(i)); + } + } + + @Test + public void testIterator() + { + Iterator iterator = list.iterator(); + while (iterator.hasNext()) + { + System.out.println(iterator.next()); + } + } + +} diff --git a/group26/191191717/src/week1/com/coding/Test/BinaryTreeNodeTest.java b/group26/191191717/src/week1/com/coding/Test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..c2482fe68e --- /dev/null +++ b/group26/191191717/src/week1/com/coding/Test/BinaryTreeNodeTest.java @@ -0,0 +1,20 @@ +package week1.com.coding.Test; + +import org.junit.Test; + +import week1.com.coding.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest +{ + + @Test + public void test() + { + BinaryTreeNode baiseTreeNode = new BinaryTreeNode("根节点"); + BinaryTreeNode.TreeNode tn1 = baiseTreeNode.insert("第二层左节点", baiseTreeNode.root()); + BinaryTreeNode.TreeNode tn2 = baiseTreeNode.insert("第二层右节点", baiseTreeNode.root()); + BinaryTreeNode.TreeNode tn3 = baiseTreeNode.insert("第三层左节点", tn1); + BinaryTreeNode.TreeNode tn4 = baiseTreeNode.insert("第三层右节点", tn1); + } + +} diff --git a/group26/191191717/src/week1/com/coding/Test/LinkedListTest.java b/group26/191191717/src/week1/com/coding/Test/LinkedListTest.java new file mode 100644 index 0000000000..7a5565d47f --- /dev/null +++ b/group26/191191717/src/week1/com/coding/Test/LinkedListTest.java @@ -0,0 +1,90 @@ +package week1.com.coding.Test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week1.com.coding.basic.Iterator; +import week1.com.coding.basic.LinkedList; + +public class LinkedListTest +{ + LinkedList list = null; + + @Before + public void testAdd() + { + list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + } + + @Test + public void testPrintNode() + { + list.printNode(); + } + + @Test + public void testSize() + { + System.out.println(list.size()); + } + + @Test + public void testInsertAdd() + { + list.add(0, "f"); + list.printNode();// a b f c d + } + @Test + public void testAddFirst() + { + list.addFirst("f"); + list.printNode(); + System.out.println(list.size()); + } + @Test + public void testAddLast() + { + list.addLast("f"); + list.printNode(); + } + @Test + public void testGet() + { + Assert.assertEquals("d", list.get(3)); + } + @Test + public void testRemoveFirst() + { + Assert.assertEquals("a", list.removeFirst()); + list.printNode(); + } + @Test + public void testRemoveLast() + { + System.out.println(list.removeLast()); + list.printNode(); + } + @Test + public void testRemove() + { + Assert.assertEquals("a", list.remove(0)); + } + + @Test + public void testIterator() + { + Iterator iter = list.iterator(); + String[] strs = {"a", "b", "c", "d"}; + int i = 0; + while (iter.hasNext()) + { + Assert.assertEquals(strs[i], iter.next()); + i++; + } + } +} diff --git a/group26/191191717/src/week1/com/coding/Test/QueueTest.java b/group26/191191717/src/week1/com/coding/Test/QueueTest.java new file mode 100644 index 0000000000..2154dbd1d9 --- /dev/null +++ b/group26/191191717/src/week1/com/coding/Test/QueueTest.java @@ -0,0 +1,39 @@ +package week1.com.coding.Test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week1.com.coding.basic.Queue; + +public class QueueTest +{ + Queue queue = null; + + @Before + public void before() + { + queue = new Queue(); + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + } + + // @Test + public void testSize() + { + Assert.assertEquals(5, queue.size()); + } + + @Test + public void testDeQueue() + { + int i = 0; + while (i < queue.size()) + { + System.out.println(queue.deQueue()); + } + } +} diff --git a/group26/191191717/src/week1/com/coding/Test/StackTest.java b/group26/191191717/src/week1/com/coding/Test/StackTest.java new file mode 100644 index 0000000000..1acb33aa46 --- /dev/null +++ b/group26/191191717/src/week1/com/coding/Test/StackTest.java @@ -0,0 +1,31 @@ +package week1.com.coding.Test; + +import org.junit.Before; +import org.junit.Test; + +import week1.com.coding.basic.Stack; + +public class StackTest +{ + Stack stack = null; + + @Before + public void testPush() + { + stack = new Stack(); + stack.push("a"); + stack.push("b"); + stack.push("c"); + stack.push("d"); + stack.push("e"); + } + + @Test + public void testPop() + { + while (stack.size()>1) + { + System.out.println(stack.pop()); + } + } +} diff --git a/group26/191191717/src/week1/com/coding/basic/ArrayList.java b/group26/191191717/src/week1/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..504b568c1b --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/ArrayList.java @@ -0,0 +1,123 @@ +package week1.com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List +{ + /** ArrayList本质上是一个动态数组,跟普通数组相比,他的容量能够自动增长 **/ + // 表示当前长度 + private int size = 0; + + // 默认容量为100 + private Object[] elementData; + + // 默认的构造函数 + public ArrayList() + { + this(2); + } + + public ArrayList(final int initCapacity) + { + if (initCapacity < 0) + { + throw new RuntimeException("初始容量不能小于0"); + } + elementData = new Object[initCapacity]; + } + + /** + * 在当前数组的最后添加元素,这里要考虑到如果超出默认数组长度,需要对数组扩展 + */ + public void add(Object o) + { + int oldCapacity = elementData.length; + // 如果超过当前容量 + if ((size + 1) > oldCapacity) + { + int newCapacity = oldCapacity << 1;// 直接扩充2倍,并将旧数组直接copyof到新数组 + elementData = Arrays.copyOf(elementData, newCapacity); + } + elementData[size++] = o; + } + + /** + * 在当前数组插入元素,因此需要将后面的元素整体后移,这也显现了ArrayList相比LinkedList在增删方面效率很差 + */ + public void add(int index, Object o) + { + if (index > size || index < 0) + { + throw new RuntimeException("要插入的元素索引不能大于整体数组的长度"); + } + int oldCapacity = elementData.length; + if ((size + 1) > oldCapacity) + { + int newCapacity = oldCapacity << 1; + System.arraycopy(elementData, index, elementData, index + 1, newCapacity - index); + size++; + return; + } + // 从index序号开始整体后移,这里用到了System.arrayCopy方法 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) + { + if (index > size || index < 0) + { + throw new RuntimeException("要获取的元素索引不能大于当前数组的长度"); + } + return elementData[index]; + } + + public Object remove(int index) + { + Object oldElement = elementData[index]; + int moveLength = size - index - 1; + if (moveLength > 0) + // 从index位后整体向左偏移 + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return oldElement; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return new IteratorImpl(); + } + + /** + * 定义一个内部类实现Iterator接口 + */ + private class IteratorImpl implements Iterator + { + int cursor;// 游标,代表当前的索引位置 + + int lastRet = -1;// 表示上一个元素的索引位置 + + @Override + public boolean hasNext() + { + // 如果当前索引大于ArrayList的实际数量则返回flase,否则true; + return cursor >= size ? false : true; + } + + @Override + public Object next() + { + lastRet = cursor; + Object object = elementData[lastRet]; + cursor++; + return object; + } + + } +} diff --git a/group26/191191717/src/week1/com/coding/basic/BinaryTreeNode.java b/group26/191191717/src/week1/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a1ff768bc6 --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,74 @@ +package week1.com.coding.basic; + +public class BinaryTreeNode +{ + + private TreeNode root;// 要有一个根节点 + + public BinaryTreeNode() + { + this.root = new TreeNode(); + } + + public BinaryTreeNode(Object o) + { + this.root = new TreeNode(o); + } + + public TreeNode insert(Object obj, TreeNode parent) + { + if (parent == null) + { + throw new RuntimeException("父节点为null,不能添加子节点"); + } + TreeNode newNode = new TreeNode(); + if (parent.left == null) + { + parent.left = newNode; + } + else + { + parent.right = newNode; + } + return newNode; + } + + public boolean isEmpty() + { + return root.data == null; + } + + public TreeNode root() + { + if (isEmpty()) + { + throw new RuntimeException("空树,无法返回根节点"); + } + return root; + } + + /** + * 按照欣哥的架构不好写,所以加一个静态内部类作为节点 + * + * @author Administrator + * + */ + public static class TreeNode + { + private Object data; + + private TreeNode left; + + private TreeNode right; + + public TreeNode() + { + } + + public TreeNode(Object data) + { + this.data = data; + } + + } +} diff --git a/group26/191191717/src/week1/com/coding/basic/Iterator.java b/group26/191191717/src/week1/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..cc72a27e5f --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package week1.com.coding.basic; + + +public interface Iterator +{ + public boolean hasNext(); + + public Object next(); +} diff --git a/group26/191191717/src/week1/com/coding/basic/LinkedList.java b/group26/191191717/src/week1/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de031b87ef --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/LinkedList.java @@ -0,0 +1,341 @@ +package week1.com.coding.basic; + +public class LinkedList implements List +{ + + private Node head;// 棣栬妭鐐 + + private Node last;// 鏈妭鐐 + + private int size;// 鑺傜偣闀垮害 + + public LinkedList() + { + this.head = this.last; + } + + /* 鏂板鑺傜偣灏辨槸灏嗗綋鍓嶆渶鍚庝竴涓妭鐐圭殑寮曠敤鎸囧畾鏂板鐨勮妭鐐 */ + public void add(Object o) + { + addLast(o); + } + + /** 鍦ㄦ寚瀹氫綅缃鍔犺妭鐐圭浉褰撲簬瑕佹妸鍓嶄竴涓妭鐐圭殑寮曠敤鎸囧悜鏂拌妭鐐癸紝鐒跺悗鎶婃柊鑺傜偣鐨勫紩鐢ㄦ寚鍚戝悗涓涓妭鐐 **/ + public void add(int index, Object o) + { + if (size == index) + { + add(o); + } + else + { + Node prevNode = getNode(index).prev; + if (prevNode == null)// 濡傛灉娌℃湁鍓嶉┍鑺傜偣,鐩稿綋浜庡湪棣栬妭鐐瑰墠鎻掑叆 + { + addFirst(o); + } + else + { + addBefore(o, getNode(index)); + } + } + size++; + } + + // 鍦ㄨ妭鐐瑰墠澧炲姞,鍓嶅悗椹辨寚閽堣缁存姢 + public void addBefore(Object o, Node curr) + { + Node prevNode = curr.prev;// 鍓嶄竴涓妭鐐 + Node newNode = new Node(o); + if (prevNode == null) + { + head = newNode; + } + else + { + prevNode.next = newNode; + } + curr.prev = newNode; + newNode.next = curr; + } + + public Node getNode(int index) + { + if (index < 0 || index > size - 1) + { + throw new RuntimeException("绱㈠紩瓒婄晫"); + } + // 杩欓噷閲囩敤鐨勬槸浜屽垎鏌ユ壘娉曞垯 + if (index < (size >> 1)) + {// 浠庡墠鍚戝悗鎵 + Node n = head; + for (int i = 0; i < index; i++) + n = n.next;// 涓鐩存壘鍒版渶鍚庝竴涓 + return n; + } + else // 浠庡悗鍚戝墠鎵 + { + Node n = last; + for (int i = size; i > index + 1; i--) + n = last.prev; + return n; + } + } + + /* 鎵撳嵃鑺傜偣 */ + public void printNode() + { + Node temp = head; + while (temp != null) + { + System.out.printf("%s ", temp.data); + temp = temp.next; + } + } + + public Object get(int index) + { + return getNode(index).data; + } + + /* + * 鐢ㄥ墠闈㈢殑浜屽垎鏌ユ壘娉曚細鏇村ソ public Node getNode(int index) { Node temp = head; int i = 0; while (temp != null) { if (i == index) + * { return temp; } temp = temp.next; i++; } return temp; } + */ + + /* remove灏辩浉瀵逛簬灏嗕竴涓妭鐐瑰垹闄わ紝鍚屾椂灏嗗墠椹辫妭鐐规寚鍚戝悗缃妭鐐 */ + public Object remove(int index) + { + if (index == 0) + { + return removeFirst(); + } + if (index == size) + { + return removeLast(); + } + if (index > size) + { + throw new RuntimeException("鍒犻櫎涓涓笉瀛樺湪鐨勭储寮曚笂鐨勫厓绱"); + } + /* + * Node temp = head; int i = 0; while (temp != null) { if (i == index - 1) { Node curNode = temp.next;// 瑕佸垹闄ょ殑鑺傜偣 + * temp.next = curNode.next;// 灏嗗墠椹辫妭鐐规寚鍚戝悗缃妭鐐 return curNode.data; } temp = temp.next; i++; } + */ + // 浜ゆ崲寮曠敤 + Node currNode = getNode(index); + Node prevNode = currNode.prev; + Node nextNode = currNode.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + size--; + return currNode.data; + } + + public int size() + { + return size; + } + + public void addFirst(Object o) + { + Node newNode = new Node(o); + Node firstTemp = head; + if (head == null) + { + head = newNode; + } + else + { + firstTemp.prev = newNode; + } + head = newNode; + newNode.next = firstTemp; + size++; + } + + // 杩欓噷鍦ㄦ柊澧炶妭鐐圭殑鏃跺欎竴瀹氳缁存姢涓涓墠椹辨寚閽堝拰鍚庨┍鎸囬拡 + public void addLast(Object o) + { + Node newNode = new Node(o); + Node lastTemp = last; + if (lastTemp == null) + { + head = newNode; + } + else + { + lastTemp.next = newNode; + } + last = newNode; + newNode.prev = lastTemp; + size++; + } + + public Object removeFirst() + { + Object object = head.data; + Node node = head.next; + head = node; + size--; + return object; + } + + public Object removeLast() + { + Object object = last.data; + last = last.prev;// 灏嗗墠涓涓妭鐐硅缃负last + last.next = null;// 骞跺皢寮曠敤璁剧疆涓虹┖ + size--; + return object; + } + + public Iterator iterator() + { + return new IteratorImple(); + } + + private class IteratorImple implements Iterator + { + + private int cursor;// 娓告爣绱㈠紩鍊 + + private Node curr = head;//閬嶅巻閮芥槸浠巋ead寮濮嬬殑 + + @Override + public boolean hasNext() + { + return cursor >= size ? false : true; + } + + @Override + public Object next() + { + Node temp=curr; + curr = temp.next; + cursor++; + return temp.data; + } + + } + + /** + * 閾捐〃鑺傜偣鍐呴儴绫 + * + * @author Administrator + * + */ + private static class Node + { + Object data;// 鏁版嵁鍖 + + Node next;// 鎸囬拡鍩燂紝涓嬩竴涓璞$殑寮曠敤 + + Node prev;// 鍓嶆寚閽 + + public Node(Object data) + { + this.data = data; + } + } + + /** + * 鎶婅閾捐〃閫嗙疆 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() + { + head = last; + Node temp = last; + System.out.println(head.data); + while ((temp = temp.prev) != null) + { + head.next = temp.prev;// 涔嬪墠鐨勫墠涓涓妭鐐癸紝鍙樻垚鍚庝竴涓妭鐐 + head.prev = temp.next;// 涔嬪墠鐨勫悗涓涓妭鐐瑰墠鎴愬墠涓涓妭鐐 + System.out.println(head.next.data + " " + head.prev.data); + // if ((temp=temp.prev)!= null) + // { + // System.out.println(temp.data); + // } + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 渚嬪锛歭ist = 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) + { + + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺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() + { + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) + { + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) + { + return null; + } + + public static void main(String[] args) + { + int[] num = {1, 2, 3, 4, 5, 6}; + for (int i = num.length; i > 0; i--) + { + System.out.println(num[i - 1]); + } + } +} diff --git a/group26/191191717/src/week1/com/coding/basic/List.java b/group26/191191717/src/week1/com/coding/basic/List.java new file mode 100644 index 0000000000..636a6da108 --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/List.java @@ -0,0 +1,14 @@ +package week1.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/group26/191191717/src/week1/com/coding/basic/Queue.java b/group26/191191717/src/week1/com/coding/basic/Queue.java new file mode 100644 index 0000000000..dda4e784de --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/Queue.java @@ -0,0 +1,80 @@ +package week1.com.coding.basic; + + +/** + * 队列的特征,先进先出,就像一列火车进入一节随道,先进的车箱肯定先出。
+ * 抽像分析,队列还有如下特征:1、队头允许删除 2、队尾允许插入 3、队列具有一定的容量 4、队列有长度 + * + * @author Administrator + * + */ +public class Queue +{ + private Object[] objects = null; + + private int size;// 长度 + + private int head;// 队头 + + private int last;// 队尾 + + private int maxCapacity;// 最大容量 + + public Queue() + { + this(10); + } + + public Queue(int initSize) + { + if (initSize > 0) + { + maxCapacity = initSize; + objects = new Object[initSize]; + head = last = 0; + } + else + { + throw new RuntimeException("初始化容量不能小于0"); + } + } + + // 入队 + public void enQueue(Object o) + { + if (size == maxCapacity)// 队列已满 + { + throw new RuntimeException("队列已满,不允许入队"); + } + else + { + objects[last] = o; + last++; + size++; + } + } + + // 出队 + public Object deQueue() + { + if (isEmpty()) + { + throw new RuntimeException("队列已空,没有值可出"); + } + Object obj = objects[head]; + objects[head] = null;// 释放队头原有值 + head++;// 队头指针+1 + size--; + return obj; + } + + public boolean isEmpty() + { + return size == 0; + } + + public int size() + { + return size; + } +} diff --git a/group26/191191717/src/week1/com/coding/basic/Stack.java b/group26/191191717/src/week1/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6d8a7d645d --- /dev/null +++ b/group26/191191717/src/week1/com/coding/basic/Stack.java @@ -0,0 +1,52 @@ +package week1.com.coding.basic; + + +/** + * 栈的特征:先进后出 + * + * @author Administrator + * + */ +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + int size; + + /** 每次推进都是在最后推进 **/ + public void push(Object o) + { + if (o == null) + { + throw new RuntimeException("推进元素不能为空"); + } + elementData.add(o); + size++; + } + + public Object pop() + { + // 每次从最后弹出 + if (size ==0) + { + throw new RuntimeException("空栈,没有可弹出元素"); + } + Object object = elementData.get(--size); + return object; + } + + public Object peek() + { + return null; + } + + public boolean isEmpty() + { + return elementData.size() == 0; + } + + public int size() + { + return elementData.size(); + } +} From c4f624a51fa8d2b39f119eab1caa7df42d941eda Mon Sep 17 00:00:00 2001 From: luojunyi <191191717@qq.com> Date: Mon, 13 Mar 2017 13:47:29 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update=2026=E7=BB=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\204\346\203\205\345\206\265\347\273\237\350\256\241.md" | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git "a/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" "b/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" index 8700b37aaf..36e8fcf78e 100644 --- "a/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" +++ "b/group26/26\347\273\204\346\203\205\345\206\265\347\273\237\350\256\241.md" @@ -4,7 +4,5 @@ | | 鏂囩珷1 | | | | | | | | | | | JEE-閫嗘按鐧惧窛 | 宸插畬鎴 | 瀹屾垚90% | | | | | | | | | | | 鐧惧害 www.baidu.com | | | | | | | | | | +|191191717 |宸插畬鎴恷 聽 聽 聽 | 聽 聽 聽 聽 聽 | 聽 聽 | 聽 聽 | 聽 聽 | 聽 聽 | 聽 聽 | 聽 聽 | 聽 聽 | | | | | | | | | | | | | -| | | | | | | | | | | | -| | | | | | | | | | | | -| | | | | | | | | | | | \ No newline at end of file From d6c75243f00f1076735b45b7932d1463a4c8040b Mon Sep 17 00:00:00 2001 From: BlindingDark Date: Mon, 13 Mar 2017 22:18:19 +0800 Subject: [PATCH 5/7] Delete .project --- group26/1778842360/first heomework/.project | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 group26/1778842360/first heomework/.project diff --git a/group26/1778842360/first heomework/.project b/group26/1778842360/first heomework/.project deleted file mode 100644 index 88536f7819..0000000000 --- a/group26/1778842360/first heomework/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - first heomework - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - From 813df4978c30addf0c9b155f38f4186e50d04c4f Mon Sep 17 00:00:00 2001 From: BlindingDark Date: Mon, 13 Mar 2017 22:18:28 +0800 Subject: [PATCH 6/7] Delete .classpath --- group26/1778842360/first heomework/.classpath | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 group26/1778842360/first heomework/.classpath diff --git a/group26/1778842360/first heomework/.classpath b/group26/1778842360/first heomework/.classpath deleted file mode 100644 index fb565a588d..0000000000 --- a/group26/1778842360/first heomework/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - From ff697c00748848b0d5b52d2037d4e682a2d989d7 Mon Sep 17 00:00:00 2001 From: BlindingDark Date: Mon, 13 Mar 2017 22:18:38 +0800 Subject: [PATCH 7/7] Delete org.eclipse.jdt.core.prefs --- .../.settings/org.eclipse.jdt.core.prefs | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs diff --git a/group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs b/group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 7341ab1683..0000000000 --- a/group26/1778842360/first heomework/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -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