From a459047b8944ac86dc9ceea93c644a598e6f6615 Mon Sep 17 00:00:00 2001 From: chishiwu <1107225491@qq.com> Date: Tue, 7 Mar 2017 15:51:56 +0800 Subject: [PATCH 01/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/1107225491/1107225491/.classpath | 6 ++++++ group24/1107225491/1107225491/.project | 17 +++++++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++++++++++ group24/1107225491/1107225491/test.txt | 1 + 4 files changed, 35 insertions(+) create mode 100644 group24/1107225491/1107225491/.classpath create mode 100644 group24/1107225491/1107225491/.project create mode 100644 group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs create mode 100644 group24/1107225491/1107225491/test.txt diff --git a/group24/1107225491/1107225491/.classpath b/group24/1107225491/1107225491/.classpath new file mode 100644 index 0000000000..07ca123c63 --- /dev/null +++ b/group24/1107225491/1107225491/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group24/1107225491/1107225491/.project b/group24/1107225491/1107225491/.project new file mode 100644 index 0000000000..2d1d33be8a --- /dev/null +++ b/group24/1107225491/1107225491/.project @@ -0,0 +1,17 @@ + + + 1107225491 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs b/group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..54e493c07c --- /dev/null +++ b/group24/1107225491/1107225491/.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.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +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.6 diff --git a/group24/1107225491/1107225491/test.txt b/group24/1107225491/1107225491/test.txt new file mode 100644 index 0000000000..2086f8809c --- /dev/null +++ b/group24/1107225491/1107225491/test.txt @@ -0,0 +1 @@ +�ܶ��ܶ� \ No newline at end of file From c4b94e171d1f2b3f840282b6ce875318cd81e94a Mon Sep 17 00:00:00 2001 From: xiaozi Date: Wed, 8 Mar 2017 23:08:18 +0800 Subject: [PATCH 02/26] This is a test. --- group24/1054283210/.classpath | 6 ++++++ group24/1054283210/.gitignore | 1 + group24/1054283210/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group24/1054283210/.classpath create mode 100644 group24/1054283210/.gitignore create mode 100644 group24/1054283210/.project diff --git a/group24/1054283210/.classpath b/group24/1054283210/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group24/1054283210/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group24/1054283210/.gitignore b/group24/1054283210/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/1054283210/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/1054283210/.project b/group24/1054283210/.project new file mode 100644 index 0000000000..b401802bd0 --- /dev/null +++ b/group24/1054283210/.project @@ -0,0 +1,17 @@ + + + 1054283210Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From bfc1e2476e4fee5b46d344b5a0ffc4506b59c2e8 Mon Sep 17 00:00:00 2001 From: ipk Date: Thu, 9 Mar 2017 01:29:01 +0800 Subject: [PATCH 03/26] this is test for git --- group24/121111914/src/com/ipk/code2017/TestGit.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 group24/121111914/src/com/ipk/code2017/TestGit.java diff --git a/group24/121111914/src/com/ipk/code2017/TestGit.java b/group24/121111914/src/com/ipk/code2017/TestGit.java new file mode 100644 index 0000000000..dbfa49c571 --- /dev/null +++ b/group24/121111914/src/com/ipk/code2017/TestGit.java @@ -0,0 +1,10 @@ +package com.ipk.code2017; + +public class TestGit { + + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println("hehe"); + } + +} From 391f19c32957f463d2af82e383a963f53932ee02 Mon Sep 17 00:00:00 2001 From: LF Date: Thu, 9 Mar 2017 01:35:01 +0800 Subject: [PATCH 04/26] test for git --- group24/121111914/src/com/ipk/code2017/TestGit1.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 group24/121111914/src/com/ipk/code2017/TestGit1.java diff --git a/group24/121111914/src/com/ipk/code2017/TestGit1.java b/group24/121111914/src/com/ipk/code2017/TestGit1.java new file mode 100644 index 0000000000..2b1a0cd7ca --- /dev/null +++ b/group24/121111914/src/com/ipk/code2017/TestGit1.java @@ -0,0 +1,10 @@ +package com.ipk.code2017; + +public class TestGit1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From 87a70cdb6239aa7bc2e5a0ade0f70366c14f8ff3 Mon Sep 17 00:00:00 2001 From: LF Date: Thu, 9 Mar 2017 23:46:56 +0800 Subject: [PATCH 05/26] test for newName --- .../ipk2015/coding2017/basic/TestForNewName.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java new file mode 100644 index 0000000000..79a296c37d --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java @@ -0,0 +1,10 @@ +package com.github.ipk2015.coding2017.basic; + +public class TestForNewName { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From be43af55826ff74d93cbe1dde9af45df58a37ca1 Mon Sep 17 00:00:00 2001 From: LF Date: Thu, 9 Mar 2017 23:48:23 +0800 Subject: [PATCH 06/26] delete the test --- group24/121111914/src/com/ipk/code2017/TestGit.java | 10 ---------- group24/121111914/src/com/ipk/code2017/TestGit1.java | 10 ---------- 2 files changed, 20 deletions(-) delete mode 100644 group24/121111914/src/com/ipk/code2017/TestGit.java delete mode 100644 group24/121111914/src/com/ipk/code2017/TestGit1.java diff --git a/group24/121111914/src/com/ipk/code2017/TestGit.java b/group24/121111914/src/com/ipk/code2017/TestGit.java deleted file mode 100644 index dbfa49c571..0000000000 --- a/group24/121111914/src/com/ipk/code2017/TestGit.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.ipk.code2017; - -public class TestGit { - - public static void main(String[] args) { - // TODO Auto-generated method stub - System.out.println("hehe"); - } - -} diff --git a/group24/121111914/src/com/ipk/code2017/TestGit1.java b/group24/121111914/src/com/ipk/code2017/TestGit1.java deleted file mode 100644 index 2b1a0cd7ca..0000000000 --- a/group24/121111914/src/com/ipk/code2017/TestGit1.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.ipk.code2017; - -public class TestGit1 { - - public static void main(String[] args) { - // TODO Auto-generated method stub - - } - -} From 53397c7b35d908ca2549be6b1c6a437d772dedde Mon Sep 17 00:00:00 2001 From: LF Date: Thu, 9 Mar 2017 23:56:26 +0800 Subject: [PATCH 07/26] delete the test --- .../ipk2015/coding2017/basic/TestForNewName.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java deleted file mode 100644 index 79a296c37d..0000000000 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/TestForNewName.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.ipk2015.coding2017.basic; - -public class TestForNewName { - - public static void main(String[] args) { - // TODO Auto-generated method stub - - } - -} From 0d834083eef96e49b6ec067e95dd03dd5ff8795c Mon Sep 17 00:00:00 2001 From: wangxin Date: Fri, 10 Mar 2017 17:58:33 +0800 Subject: [PATCH 08/26] init --- group24/626451284Learning/pom.xml | 12 ++ .../wdn/coding2017/basic/ArrayList.java | 32 +++++ .../wdn/coding2017/basic/BinaryTreeNode.java | 32 +++++ .../github/wdn/coding2017/basic/Iterator.java | 7 + .../wdn/coding2017/basic/LinkedList.java | 124 ++++++++++++++++++ .../com/github/wdn/coding2017/basic/List.java | 9 ++ .../github/wdn/coding2017/basic/Queue.java | 19 +++ .../github/wdn/coding2017/basic/Stack.java | 22 ++++ .../coderising/array/ArrayUtil.java | 96 ++++++++++++++ .../coderising/download/DownloadThread.java | 20 +++ .../coderising/download/FileDownloader.java | 73 +++++++++++ .../download/FileDownloaderTest.java | 59 +++++++++ .../coderising/download/api/Connection.java | 23 ++++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 ++ .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 ++++ .../download/impl/ConnectionManagerImpl.java | 15 +++ .../coderising/litestruts/LoginAction.java | 39 ++++++ .../coderising/litestruts/Struts.java | 34 +++++ .../coderising/litestruts/StrutsTest.java | 43 ++++++ .../coderising/litestruts/View.java | 23 ++++ .../coderising/litestruts/struts.xml | 11 ++ 23 files changed, 740 insertions(+) create mode 100644 group24/626451284Learning/pom.xml create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java create mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml diff --git a/group24/626451284Learning/pom.xml b/group24/626451284Learning/pom.xml new file mode 100644 index 0000000000..534ff39232 --- /dev/null +++ b/group24/626451284Learning/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.github.wdn + coding2017 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..bfd96d3202 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.github.wdn.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..39d4e62c93 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..f601a50c7b --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.wdn.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..ba5b32cb90 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.github.wdn.coding2017.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java new file mode 100644 index 0000000000..d6c9e95cb0 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.wdn.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/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java new file mode 100644 index 0000000000..627f2d3a87 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java @@ -0,0 +1,19 @@ +package com.github.wdn.coding2017.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java new file mode 100644 index 0000000000..a5970ed4c7 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java @@ -0,0 +1,22 @@ +package com.github.wdn.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..45c32815f4 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.github.wdn.coding2017.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..cc0e750c98 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.github.wdn.coding2017.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..a5c19a7d77 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.github.wdn.coding2017.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..490db5c08b --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.github.wdn.coding2017.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java new file mode 100644 index 0000000000..91e8aa463b --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.github.wdn.coding2017.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..5c90080401 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.github.wdn.coding2017.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..f814016839 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.github.wdn.coding2017.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..078b14365d --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.github.wdn.coding2017.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..e2bc4dcd10 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.github.wdn.coding2017.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..082e2af8fd --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.github.wdn.coding2017.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b342a51061 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..cf3d0564f1 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9fee464283 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..31e1805dc7 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..171848ecd1 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From b8c5dcec1d760d49f8e40708539d8b00c8aa6242 Mon Sep 17 00:00:00 2001 From: wdn <626451284@163.com> Date: Sat, 11 Mar 2017 17:26:31 +0800 Subject: [PATCH 09/26] =?UTF-8?q?=E7=AE=80=E5=8D=95=E5=AE=9E=E7=8E=B0Array?= =?UTF-8?q?List=E3=80=81Stack=E3=80=81Queue,LinkedList;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wdn/coding2017/basic/ArrayList.java | 84 ++++++++-- .../wdn/coding2017/basic/BinaryTreeNode.java | 3 + .../github/wdn/coding2017/basic/Iterator.java | 3 + .../wdn/coding2017/basic/LinkedList.java | 157 +++++++++++++++--- .../github/wdn/coding2017/basic/Queue.java | 15 +- .../github/wdn/coding2017/basic/Stack.java | 15 +- 6 files changed, 229 insertions(+), 48 deletions(-) diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java index bfd96d3202..3b3ea048f3 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java @@ -1,32 +1,88 @@ package com.github.wdn.coding2017.basic; +import java.util.Arrays; + public class ArrayList implements List { - + private int size = 0; - - private Object[] elementData = new Object[100]; - + + private Object[] elementData; + public ArrayList(){ + elementData = new Object[10]; + } + public ArrayList(int size){ + elementData = new Object[size]; + } public void add(Object o){ - + if(size>=elementData.length){ + elementData = Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; } public void add(int index, Object o){ - + Object[] newElementData; + if(size()+1>=elementData.length){ + newElementData=new Object[elementData.length*2]; + + }else{ + newElementData=new Object[elementData.length]; + } + for (int i = 0; i < elementData.length; i++) { + if(index==1){ + newElementData[i]=o; + }else if(i>index) { + newElementData[i]=elementData[i-1]; + }else{ + newElementData[i]=elementData[i]; + } + } + elementData = newElementData; + size++; } - + public Object get(int index){ - return null; + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; } - - public Object remove(int index){ - return null; + + public Object remove(int index) { + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + Object returnO = elementData[index]; + for (int i = index; i < size; i++) { + if(i==size-1){ + elementData[i]=null; + }else { + elementData[i] = elementData[i + 1]; + } + } + size--; + return returnO; } - + public int size(){ - return -1; + return size; } - + public Iterator iterator(){ return null; } + + public static void main(String[] args) { + ArrayList list = new ArrayList(2); + list.add("1"); + list.add("2"); + list.remove(1); + list.add("3"); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + int[] i = {}; + System.out.println(i[0]); + } } diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java index 39d4e62c93..b8b613d6f0 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java @@ -1,5 +1,8 @@ package com.github.wdn.coding2017.basic; +/** + * TODO 代码在公司电脑里。。。后续提交 + */ public class BinaryTreeNode { private Object data; diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java index f601a50c7b..044ddf0993 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java @@ -1,5 +1,8 @@ package com.github.wdn.coding2017.basic; +/** + * TODO 主要考虑遍历中安全删除元素的实现 + */ public interface Iterator { public boolean hasNext(); public Object next(); diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java index ba5b32cb90..6040cb5a47 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -1,92 +1,201 @@ package com.github.wdn.coding2017.basic; - +/** + * TODO 只是简单实现 缺少对边界的处理 + * 参考JDK源码改为更优雅的实现 + */ public class LinkedList implements List { private Node head; - + private Node tail; + private int size; + + public LinkedList(){ + this.head=null; + this.tail=null; + } public void add(Object o){ - + Node newNode = new Node(o); + if (head == null) { + head = newNode; + tail = newNode; + }else{ + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + size++; } public void add(int index , Object o){ - + checkIndex(index); + Node indexNode = getNode(index); + Node newNode = new Node(o); + Node pre = indexNode.getPre(); + if(pre!=null){ + pre.setNext(newNode); + }else{ + head = newNode; + } + newNode.setPre(pre); + newNode.setNext(indexNode); + indexNode.setPre(newNode); + } + private void checkIndex(int index){ + if(index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + private Node getNode(int index){ + checkIndex(index); + // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 + Node result=null; + if(index==0){ + return head; + } + if(index==size-1){ + return tail; + } + for (int i = 0; i < index; i++) { + result = head.getNext(); + } + return result; } public Object get(int index){ - return null; + return getNode(index).getData(); } public Object remove(int index){ - return null; + checkIndex(index); + Node indexNode = getNode(index); + Node pre = indexNode.getPre(); + Node next = indexNode.getNext(); + if(pre!=null){ + pre.setNext(next); + }else{ + head = next; + } + if(next!=null){ + next.setPre(pre); + }else{ + tail = pre; + } + return indexNode.getData(); } public int size(){ - return -1; + return size; } public void addFirst(Object o){ - + Node newNode = new Node(o); + head.setPre(newNode); + newNode.setNext(head); + head = newNode; } public void addLast(Object o){ - + Node newNode = new Node(o); + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; } public Object removeFirst(){ - return null; + Node next = head.getNext(); + Node oldHead = head; + head = next; + head.setPre(null); + return oldHead; } public Object removeLast(){ - return null; + Node oldTail = tail; + Node pre = tail.getPre(); + tail = pre; + tail.setNext(null); + return oldTail; } public Iterator iterator(){ + return null; } - + /** + * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 + */ private static class Node{ + Object data; Node next; - + Node pre; + public Node(){ + + } + public Node(Object data){ + this.data = data; + } + 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; + } + + public Node getPre() { + return pre; + } + + public void setPre(Node pre) { + this.pre = pre; + } } /** * 把该链表逆置 * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ - public void reverse(){ - - } - - /** + 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] + * 返回的结果应该是[101,301,401,601] * @param list */ public static int[] getElements(LinkedList list){ return null; } - + /** * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 + * 从当前链表中中删除在list中出现的元素 * @param list */ diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java index 627f2d3a87..59992f0cbd 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java @@ -1,19 +1,24 @@ package com.github.wdn.coding2017.basic; +/** + * 队列使用链表实现比较简单理论上是无界队列 + * 如果使用数组需要处理很多问题比如长度限制,元素的复制 + */ public class Queue { - - public void enQueue(Object o){ + private LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); } public Object deQueue(){ - return null; + return queue.remove(0); } public boolean isEmpty(){ - return false; + return queue.size()==0; } public int size(){ - return -1; + return queue.size(); } } diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java index a5970ed4c7..f0a0fe56e9 100644 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java @@ -1,22 +1,27 @@ package com.github.wdn.coding2017.basic; +/** + * 使用list实现比较简单 + * 可以考虑使用其它结构 + */ public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o){ + public void push(Object o){ + elementData.add(o); } public Object pop(){ - return null; + return elementData.remove(elementData.size()-1); } public Object peek(){ - return null; + return elementData.get(elementData.size()-1); } public boolean isEmpty(){ - return false; + return elementData.size()==0; } public int size(){ - return -1; + return elementData.size(); } } From dcf2f2d29a8aa24880f23f73b1b53169fa21c63e Mon Sep 17 00:00:00 2001 From: wdn <626451284@163.com> Date: Sun, 12 Mar 2017 17:28:50 +0800 Subject: [PATCH 10/26] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=9C=AC=E5=91=A8?= =?UTF-8?q?=E4=B8=8D=E7=9B=B8=E5=85=B3=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/array/ArrayUtil.java | 96 ------------------- .../coderising/download/DownloadThread.java | 20 ---- .../coderising/download/FileDownloader.java | 73 -------------- .../download/FileDownloaderTest.java | 59 ------------ .../coderising/download/api/Connection.java | 23 ----- .../download/api/ConnectionException.java | 5 - .../download/api/ConnectionManager.java | 10 -- .../download/api/DownloadListener.java | 5 - .../download/impl/ConnectionImpl.java | 27 ------ .../download/impl/ConnectionManagerImpl.java | 15 --- .../coderising/litestruts/LoginAction.java | 39 -------- .../coderising/litestruts/Struts.java | 34 ------- .../coderising/litestruts/StrutsTest.java | 43 --------- .../coderising/litestruts/View.java | 23 ----- .../coderising/litestruts/struts.xml | 11 --- 15 files changed, 483 deletions(-) delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java deleted file mode 100644 index 45c32815f4..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.github.wdn.coding2017.coderising.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java deleted file mode 100644 index cc0e750c98..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.github.wdn.coding2017.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java deleted file mode 100644 index a5c19a7d77..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.github.wdn.coding2017.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - - - - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java deleted file mode 100644 index 490db5c08b..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.github.wdn.coding2017.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java deleted file mode 100644 index 91e8aa463b..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos, int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java deleted file mode 100644 index 5c90080401..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java deleted file mode 100644 index f814016839..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java deleted file mode 100644 index 078b14365d..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index e2bc4dcd10..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 082e2af8fd..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.wdn.coding2017.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java deleted file mode 100644 index b342a51061..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java deleted file mode 100644 index cf3d0564f1..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java deleted file mode 100644 index 9fee464283..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java deleted file mode 100644 index 31e1805dc7..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.wdn.coding2017.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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml deleted file mode 100644 index 171848ecd1..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - From 751e08ba0bf457b9c125cb48d88fe2e5426db38b Mon Sep 17 00:00:00 2001 From: CJ-chen <120509419@qq.com> Date: Sun, 12 Mar 2017 18:05:33 +0800 Subject: [PATCH 11/26] CJ-chen --- .../CJ-chen/coding2017/basic/ArrayList.java | 123 +++++ .../coding2017/basic/BinaryTreeNode.java | 45 ++ .../CJ-chen/coding2017/basic/Iterator.java | 16 + .../CJ-chen/coding2017/basic/LinkedList.java | 423 ++++++++++++++++++ .../github/CJ-chen/coding2017/basic/List.java | 18 + .../CJ-chen/coding2017/basic/Queue.java | 58 +++ .../CJ-chen/coding2017/basic/Stack.java | 63 +++ 7 files changed, 746 insertions(+) create mode 100644 group24/com/github/CJ-chen/coding2017/basic/ArrayList.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/Iterator.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/LinkedList.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/List.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/Queue.java create mode 100644 group24/com/github/CJ-chen/coding2017/basic/Stack.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cbb7084045 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final int defaultGrowSize = 100; // 每次增长 100 个元素 + +// private int curIterIndex = 0; // 用于记录 Iterator的索引 + + private void CheckAndGrowUp() { + if (size+1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + defaultGrowSize); + } + } + + // 添加一个方法,检测看,添加元素的话,是否需要增长,专门用于数组长度扩展的 + public void add(Object o) { + CheckAndGrowUp(); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + // 先探测是否添加数组大小 + CheckAndGrowUp(); + // 保留后半部分,然后 全部替换即可 + + Object[] tmpObjectArr = Arrays.copyOfRange(elementData, index, elementData.length); + elementData[index] = o; + for (int i = index+1; i < size+1; i++) { + elementData[i] = tmpObjectArr[i-index-1]; + } + size++; + + } + + public Object get(int index) { + // 应该是 不需要跑出 下标越界异常的,因为数组越界会自动抛出 + return elementData[index]; + } + + public Object remove(int index) { + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size] = null; // 后续后面是 数值?那么就不能为null了,而是零? + size--; + return null; + } + + @Override + public int size() { + return size; + } + + // 覆盖toString方法,方便测试 + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public Iterator iterator() { + + return new Iterator() { + int curIterIndex = 0; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return curIterIndex < size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[curIterIndex++]; + } + + }; + } + + public static void main(String[] args) { + ArrayList curArrList = new ArrayList(); + for (int i = 0; i <= 101; i++) { + curArrList.add(i); + } + System.err.println("Test add Arr"); + System.err.println(curArrList); + System.err.println("Test add in specific index"); + curArrList.add(10, 1010); + System.err.println(curArrList); + System.err.println("Test remove"); + curArrList.remove(10); + System.err.println(curArrList); + System.err.println("Test Iterator"); + Iterator curIter = curArrList.iterator(); + while(curIter.hasNext()){ + System.err.println(curIter.next()); + } + } + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..3adcc7040b --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,45 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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/group24/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/com/github/CJ-chen/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..29ab5ecaf0 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Iterator.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..0364c16480 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java @@ -0,0 +1,423 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class LinkedList implements List { + + private Node head; + + private int size; +// private int curIterIndex; + + public LinkedList() { + head = new Node(); + size = 0; +// curIterIndex = 0; + } + + public void add(Object o) { + addLast(o); + } + + private Node getNode(int index) { + if (index == -1) { + return head; + } else { + Node returnNode = head.next; + for (int i = 0; i < index; i++) { + returnNode = returnNode.next; + } + return returnNode; + } + } + + public void add(int index, Object o) { + Node preNode = getNode(index - 1); + Node addNode = new Node(); + addNode.data = o; + addNode.next = preNode.next; + preNode.next = addNode; + size++; + } + + public Object get(int index) { + return getNode(index).data; + } + + public Object remove(int index) { + Node preNode = getNode(index - 1); + Node delNode = preNode.next; + preNode.next = delNode.next; + // 返回被删除的Node... 可能是为了测试吧 + size--; + return delNode; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node fNode = new Node(); + fNode.data = o; + fNode.next = head.next; + head.next = fNode; + size++; + } + + public void addLast(Object o) { +// System.err.println("Curr add: "+ o); + Node lastNode = getNode(size - 1); +// System.err.println(lastNode); + Node lNode = new Node(); + lNode.data = o; + lastNode.next = lNode; + size++; + } + + public Object removeFirst() { + return removeFirstNode().data; + } + + private Node removeFirstNode() { + Node firstNode = head.next; + head.next = firstNode.next; + size--; + return firstNode; + } + + public Object removeLast() { + return removeLastNode().data; + } + + private Node removeLastNode() { + Node last2Node = getNode(size - 2); + Node lastNode = last2Node.next; + last2Node.next = null; + size--; + return lastNode; + } + + public Iterator iterator() { + return new Iterator() { +// int curIterIndex = 0; + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode.next != null; + } + + @Override + public Object next() { + curNode = curNode.next; + return curNode.data; + } + }; + } + + private static class Node { + + Object data; + Node next; + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + int oriSize = size; + Node newHead = new Node(); + newHead.next = this.removeLastNode(); + Node preNode = newHead.next; + + while (size > 0) { + preNode.next = this.removeLastNode(); + preNode = preNode.next; + } + // 不考虑线程安全的情况下,恢复size + size = oriSize; + head = newHead; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int reDirIndex = size / 2; // java会自动 取 0 + size = size - reDirIndex; + System.err.println(reDirIndex); + Node jumpNode = getNode(reDirIndex); + head.next = jumpNode; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + Node fromNode = getNode(i - 1); + for (int j = 0; j < length; j++) { + fromNode.next = fromNode.next.next; + size--; + } + } + + /** + * 假定当前链表和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) { + // 这个似乎 不应该是 静态方法 + public int[] getElements(LinkedList list) { + int[] returnIndex = new int[list.size]; + int validCounts = 0; +// Iterator curIter = list.iterator(); +// int curIndex = 0; +// while(curIter.hasNext()){ +// returnIndex[curIndex++] =(Integer) this.get((Integer) curIter.next()); +// } + + // 已知是内外都是玩去排序好的,所以是需要一个高效的算法 + Iterator innerIter = this.iterator(); + Iterator curIter = list.iterator(); + int curCount = 0; + int curIndex; + int preIndex = 0; + + while (curIter.hasNext()) { + curIndex = (Integer) curIter.next(); + if (curIndex < preIndex) { + System.err.println("Warning: Skip index no in sorted order..."); + continue; + } +// int skipCounts = curIndex-preIndex; + for (int i = preIndex; i < curIndex; i++) { + innerIter.next(); + } + validCounts++; + returnIndex[curCount++] = (Integer) innerIter.next(); + preIndex = curIndex + 1; + } + return Arrays.copyOf(returnIndex, validCounts); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode.next = curNode.next; + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (curNode.data == preNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + // 假定 当前链表 是 从小到大 排列的 + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (min < (Integer) curNode.data && max > (Integer) curNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + + + LinkedList newList = new LinkedList(); + + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode = curNode; + newList.add(curNode.data); // 添加data + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + + + return newList; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("LinkdedList ["); + Node curNode = head; + while (curNode.next != null) { + curNode = curNode.next; + sb.append(curNode.data).append(", "); + } + sb.delete(sb.length() - 2, sb.length()).append("]"); + return sb.toString(); + } + + public void clear() { + head.next = null; + size = 0; + } + + public static void main(String[] args) { + // 先写一些测试 + LinkedList ll = new LinkedList(); + System.err.println("Test Add"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println("Size: " + ll.size); + System.err.println(ll); + System.err.println("Test Add last"); + ll.addLast(10); + System.err.println(ll); + System.err.println("Test Add First"); + ll.addFirst(-1); + System.err.println(ll); + System.err.println("Test remove "); + ll.remove(0); + System.err.println(ll); +// ll.remove(11); +// System.err.println(ll); + ll.removeFirst(); + System.err.println(ll); + ll.removeLast(); + System.err.println(ll); + System.err.println("Test reverse()"); + ll.reverse(); + System.err.println(ll); + System.err.println("Test removeFirstHalf()"); + ll.removeFirstHalf(); + System.err.println(ll); +// System.err.println(9/2); + System.err.println("Test remove()"); + ll.remove(1, 2); + System.err.println(ll); +// System.err.println(ll.); + LinkedList newList = new LinkedList(); + newList.add(3); + newList.add(4); + newList.add(6); + newList.add(5); +// System.err.println(); + ll.clear(); + System.err.println("Re Init"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println(ll); + for (int curI : ll.getElements(newList)) { + System.err.println(curI); + } + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + ll.add(i); + ll.add(i); + } + System.err.println(ll); + ll.removeDuplicateValues(); + System.err.println(ll); + System.err.println("Test Remove removeRange(3, 6)"); + ll.removeRange(3, 6); + System.err.println(ll); + + LinkedList rmList = new LinkedList(); + rmList.add(3); + rmList.add(4); + rmList.add(7); + rmList.add(8); + ll.subtract(rmList); + System.err.println("Test subtract(3,4,7,8)"); + System.err.println(ll); + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + rmList.add(11); + System.err.println(ll); + System.err.println(rmList); + System.err.println(ll.intersection(rmList)); +// System.err.println("Test substract"); +// ll.subtract(newList); +// System.err.println(ll); + + } + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/List.java b/group24/com/github/CJ-chen/coding2017/basic/List.java new file mode 100644 index 0000000000..d347e59c47 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/List.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/com/github/CJ-chen/coding2017/basic/Queue.java new file mode 100644 index 0000000000..4cbc45e065 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Queue.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public class Queue { + + private final LinkedList innerList; + + public Queue(){ + innerList = new LinkedList(); + } + + public void enQueue(Object o) { + innerList.addLast(o); + } + + public Object deQueue() { + return innerList.removeFirst(); + } + + public boolean isEmpty() { + return innerList.size()==0; + } + + public int size() { + return innerList.size(); + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < innerList.size(); i++) { + sb.append(innerList.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public static void main(String[] args) { + Queue newQ = new Queue(); + for(int i=0;i<10;i++){ + newQ.enQueue(i); + } + System.err.println(newQ); + newQ.deQueue(); + System.err.println(newQ); + } + + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/com/github/CJ-chen/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3ba85c6bcf --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Stack.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < elementData.size(); i++) { + sb.append(elementData.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + public static void main(String[] args) { + Stack newS = new Stack(); + for(int i=0;i<10;i++){ + newS.push(i); + } + System.err.println("Test push()"); + System.err.println(newS); + newS.pop(); + System.err.println("Test pop()"); + System.err.println(newS); + System.err.println("Test peek()"); + System.err.println(newS.peek()); + System.err.println(newS); + + } + +} From af38e398a7884ec24ca03b42f622294153ec4224 Mon Sep 17 00:00:00 2001 From: LF Date: Sun, 12 Mar 2017 18:28:59 +0800 Subject: [PATCH 12/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4=EF=BC=9Alist=EF=BC=8Cstack,queue?= =?UTF-8?q?=EF=BC=8C=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=8F=8A=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ipk2015/coding2017/basic/ArrayList.java | 77 ++++++ .../coding2017/basic/BinaryTreeNode.java | 61 +++++ .../ipk2015/coding2017/basic/Iterator.java | 6 + .../ipk2015/coding2017/basic/LinkedList.java | 244 ++++++++++++++++++ .../github/ipk2015/coding2017/basic/List.java | 8 + .../ipk2015/coding2017/basic/ListUtils.java | 14 + .../ipk2015/coding2017/basic/Queue.java | 19 ++ .../ipk2015/coding2017/basic/Stack.java | 32 +++ .../coding2017/basic/test/ArrayListTest.java | 53 ++++ .../basic/test/BinaryTreeNodeTest.java | 29 +++ .../coding2017/basic/test/LinkedListTest.java | 92 +++++++ .../coding2017/basic/test/QueueTest.java | 49 ++++ .../coding2017/basic/test/StackTest.java | 57 ++++ 13 files changed, 741 insertions(+) create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/ArrayList.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/Iterator.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/LinkedList.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/List.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/ListUtils.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/Queue.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/ArrayListTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/LinkedListTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/QueueTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java 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()); + } + +} From 9df1dd327d35716eacaaad14295e95fc6afea015 Mon Sep 17 00:00:00 2001 From: CJ-chen <120509419@qq.com> Date: Sun, 12 Mar 2017 18:51:43 +0800 Subject: [PATCH 13/26] add Btree --- .../coding2017/basic/BinaryTreeNode.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java index 3adcc7040b..ff40538e64 100644 --- a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java +++ b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java @@ -40,6 +40,48 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Object o) { - return null; + // 应该只需要实现这个就可以了 + int curValue = (Integer) this.getData(); + int insertValue = (Integer) o; + + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + + if (curValue > insertValue) { + if (this.getLeft() != null) { + return this.getLeft().insert(o); + } else { + this.setLeft(newNode); + return this; + } + } else{ + if (this.getRight() != null) { + return this.getRight().insert(o); + } else { + this.setRight(newNode); + return this; + } + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(this.getData()).append("\n"); + sb.append(this.getLeft()).append("<--").append(this.getData()).append("\n"); + sb.append(this.getData()).append("-->").append(this.getRight()).append("\n"); + return sb.toString(); } + + public static void main(String[] args) { + BinaryTreeNode btn = new BinaryTreeNode(); + btn.setData(5); +// btn.insert(5); + btn.insert(7); + btn.insert(8); + btn.insert(9); + btn.insert(4); + + System.err.println(btn); + } + } From 104bd9b3dc35590ec0bb7110ccd926e113f3944d Mon Sep 17 00:00:00 2001 From: likeleaf <893022254@qq.com> Date: Sun, 12 Mar 2017 19:39:57 +0800 Subject: [PATCH 14/26] first commit --- group24/893022254/task1-list/.gitignore | 5 + .../src/com/oneflyingleaf/util/ArrayList.java | 106 +++++ .../oneflyingleaf/util/BinaryTreeNode.java | 57 +++ .../src/com/oneflyingleaf/util/Iterator.java | 7 + .../com/oneflyingleaf/util/LinkedList.java | 380 ++++++++++++++++++ .../src/com/oneflyingleaf/util/List.java | 9 + .../src/com/oneflyingleaf/util/Queue.java | 26 ++ .../src/com/oneflyingleaf/util/Stack.java | 23 ++ .../src/com/oneflyingleaf/util/TestClass.java | 115 ++++++ 9 files changed, 728 insertions(+) create mode 100644 group24/893022254/task1-list/.gitignore create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/BinaryTreeNode.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/Iterator.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/LinkedList.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/List.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/Queue.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/Stack.java create mode 100644 group24/893022254/task1-list/src/com/oneflyingleaf/util/TestClass.java diff --git a/group24/893022254/task1-list/.gitignore b/group24/893022254/task1-list/.gitignore new file mode 100644 index 0000000000..166a44c4db --- /dev/null +++ b/group24/893022254/task1-list/.gitignore @@ -0,0 +1,5 @@ +/.settings/ +/bin/ +.classpath +.project + diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.java new file mode 100644 index 0000000000..b584ffb99b --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.java @@ -0,0 +1,106 @@ +package com.oneflyingleaf.util; + +import java.util.Arrays; + +public class ArrayList implements List { + + //非线程安全 + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + checkLength(1); + + elementData[ size ++] = o; + } + + + public void add(int index, Object o){ + checkBound(index); + + if(index != size){ + checkLength(1); + } + + int temp = index; + + //index == size 不移动,直接放 + while(index < size){ + elementData[ ++ index ] = elementData[index]; + } + + size ++ ; + elementData[temp] = o; + } + + public Object get(int index){ + checkBound(index); + + return elementData [index]; + } + + public Object remove(int index){ + checkBound(index); + + Object ret = elementData[index]; + while(index < size){ + elementData[index] = elementData[ ++ index]; + } + + -- size ; + + return ret; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + /** + * 判断扩容 + * @param length + */ + private void checkLength(int length){ + if( (size + length) > elementData.length){ + //默认扩大一倍 + Arrays.copyOf(elementData, elementData.length << 1); + } + } + + /** + * 校验是否超出 + * @param index + */ + private void checkBound(int index){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("size : " + size +" , index : " + index); + } + } + + + private class ArrayListIterator implements Iterator{ + + private int count = -1; + + private ArrayListIterator(){ + } + + @Override + public boolean hasNext() { + return (count+1) < size?true:false; + } + + @Override + public Object next() { + count ++ ; + return elementData[count]; + } + + } +} diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/BinaryTreeNode.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/BinaryTreeNode.java new file mode 100644 index 0000000000..4b1a92b1fb --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package com.oneflyingleaf.util; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(! (o instanceof Comparable)){ + throw new RuntimeException("未实现Comparable接口"); + } + Comparable temp = (Comparable)o; + + if(temp.compareTo(data) > 0){ + if(right == null){ + right= new BinaryTreeNode(); + right.data = o; + }else{ + right.insert(o); + } + } + + if(temp.compareTo(data) <= 0){ + if(left == null){ + + left = new BinaryTreeNode(); + left.data = o; + }else{ + left.insert(o); + } + } + + + return this; + } + +} diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/Iterator.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Iterator.java new file mode 100644 index 0000000000..984a561ab9 --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Iterator.java @@ -0,0 +1,7 @@ +package com.oneflyingleaf.util; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/LinkedList.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/LinkedList.java new file mode 100644 index 0000000000..06c5643f53 --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/LinkedList.java @@ -0,0 +1,380 @@ +package com.oneflyingleaf.util; + + + +public class LinkedList implements List { + + private Node head; + + //虚拟tail节点,方便add最后节点,减少成,tail为链表的最后一个元素 + private Node tail; + + private int size ; + + public void add(Object o){ + size ++ ; + + if(head == null){ + head = new Node(); + + head.data = o; + + tail = head; + + return ; + } + + Node node = tail; + tail = new Node(); + + node.next = tail; + tail.data = o; + + } + public void add(int index , Object o){ + checkBound(index,true); + if(index == 0){ + addFirst(o); + return ; + } + + if(index == (size - 1)){ + add(o); + return ; + } + + size ++; + Node node = head; + while(index-- > 0){ + node = node.next; + } + + Node temp = new Node(); + temp.data = o; + temp.next = node.next; + + node.next = temp; + + } + public Object get(int index){ + checkBound(index,false); + + Node node = head; + + while(index > 0){ + index -- ; + node = node.next; + } + + return node.data; + } + + public Object remove(int index){ + checkBound(index,false); + + if(index == 0){ + removeFirst(); + } + if(index == (size-1)){ + removeLast(); + } + + size --; + + Node node = head; + while(--index > 0){ + node = node.next; + } + Object o = node.next.data; + node.next = node.next.next; + + return o; + } + + public int size(){ + return size ; + } + + public void addFirst(Object o){ + size ++ ; + + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + } + public void addLast(Object o){ + add(o); + } + + + public Object removeFirst(){ + checkBound(0,true); + + if(size == 0){ + //处理tail节点,防止占用引用资源回收不了 + tail.data = null; + } + size -- ; + + Object o = head.data; + + head = head.next; + + return o; + } + + public Object removeLast(){ + if(size == 0){ + //直接移除首节点,则无需处理尾节点 + removeFirst(); + } + size -- ; + + Object o = tail.data; + + tail = null; + + Node temp = head ; + while(temp != null){ + temp = temp.next; + } + + tail = temp; + return o; + + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node node = head.next; + Node pre = head; + + Node next; + while(node.next != null){ + next = node.next; + + node.next = pre; + + pre = node; + node = next; + } + //处理尾元素 + node.next = pre; + + next = tail; + tail = head; + head = next; + + tail.next = null; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int size = this.size / 2; + + for(int i = 0;i < size ; i++){ + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + for(int j = i;length > 0; length -- ,j++ ){ + remove(j); + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public Object[] getElements(LinkedList list){ + Object [] ret = new Object[list.size]; + + for(int i = 0 ;i < list.size;i++){ + ret[i] = get((int) list.get(i)); + } + return ret; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + int size = this.size > list.size()?this.size : list.size(); + + int temp = 0; + for(int i = 0;i < size ;i++){ + if(temp == size || i == list.size){ + return ; + } + + + if((int)list.get(i) > (int)get(temp)){ + temp ++; + }else if((int)list.get(i) < (int)get(temp)){ + continue; + }else{ + remove(temp); + } + + } + + + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(size == 0){ + return ; + } + + Object o = get(0); + + for(int i = 0;i < (size - 1) ;i++){ + if(o.equals(get(i + 1))){ + remove(i + 1); + i--; + } + o = get(i); + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(size == 0 || max < (int)get(0) || min > (int)get(size - 1)){ + return ; + } + int minIndex = getIndex(min,0,size); + int maxIndex = getIndex(max,0,size); + + remove(minIndex,maxIndex - minIndex ); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList ret = new LinkedList(); + + int temp = 0; + for(int i = 0;i < size ;i++){ + if(temp == size || i == list.size){ + return ret ; + } + + + if((int)list.get(i) > (int)get(temp)){ + temp ++; + }else if((int)list.get(i) < (int)get(temp)){ + continue; + }else{ + ret.add(list.get(i)); + } + + } + return ret ; + } + + + private class LinkedListIterator implements Iterator{ + Node node = null; + @Override + public boolean hasNext() { + if(node == null){ + return head != null; + } + return node.next != null; + } + + @Override + public Object next() { + if(node == null){ + node = head; + }else{ + node = node.next; + } + return node.data; + } + + } + + /** + * 数组越界提示 + * @param index 数组下标 + * @param contailLast + */ + private void checkBound(int index , boolean containLast){ + if(containLast && index == (size + 1) ){ + return ; + } + + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("idnex:" + index +", size:" + size); + } + } + + /** + * 获取不大于val的下标 + * @return + */ + private int getIndex(int val,int min ,int max){ + int mid = (0 + size)/2 ; + if(max <= min){ + return min; + } + if(val > (int)get(mid)){ + mid = (mid + 1 + max) /2 ; + return getIndex(val,mid,max); + }else if(val < (int)get(mid)){ + mid = (mid + min -1) /2; + return getIndex(val,min,mid); + }else{ + return mid; + } + + + + + } +} diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/List.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/List.java new file mode 100644 index 0000000000..a9fc12cfd4 --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/List.java @@ -0,0 +1,9 @@ +package com.oneflyingleaf.util; + +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/893022254/task1-list/src/com/oneflyingleaf/util/Queue.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Queue.java new file mode 100644 index 0000000000..0f26cfadcf --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Queue.java @@ -0,0 +1,26 @@ +package com.oneflyingleaf.util; + +public class Queue { + + private LinkedList list; + + + public void enQueue(Object o){ + if(list == null){ + list = new LinkedList(); + } + list.add(o); + } + + public Object deQueue(){ + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list == null || list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group24/893022254/task1-list/src/com/oneflyingleaf/util/Stack.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Stack.java new file mode 100644 index 0000000000..b90e8c44f9 --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/Stack.java @@ -0,0 +1,23 @@ +package com.oneflyingleaf.util; + +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/group24/893022254/task1-list/src/com/oneflyingleaf/util/TestClass.java b/group24/893022254/task1-list/src/com/oneflyingleaf/util/TestClass.java new file mode 100644 index 0000000000..d9056f25ba --- /dev/null +++ b/group24/893022254/task1-list/src/com/oneflyingleaf/util/TestClass.java @@ -0,0 +1,115 @@ +package com.oneflyingleaf.util; + +import org.junit.Assert; +import org.junit.Test; + +public class TestClass { + + @Test + public void testArrayList(){ + ArrayList list = new ArrayList(); + + list.add("1A"); + list.add("2B"); + list.add("3B"); + + list.add(3, "4D"); + + list.remove(3); + Assert.assertEquals(3, list.size()); + list.remove(0); + Assert.assertEquals(2, list.size()); + Iterator iterator = list.iterator(); + + String []str = {"2B","3B"}; + int i = 0; + + while(iterator.hasNext()){ + Assert.assertEquals(str[i++], (String)iterator.next()); + } + + } + + @Test + public void testLinkedList(){ + LinkedList list = new LinkedList(); + String []str = {"1A","2B","3C","4D","5E","6F"}; + + for(String s : str){ + list.add(s); + } + + for(int i = 0; i Date: Sun, 12 Mar 2017 19:44:14 +0800 Subject: [PATCH 15/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 167 +++++++++++++ .../coding2017/basic/LinkedList.java | 231 ++++++++++++++++++ .../com/johnChnia/coding2017/basic/Queue.java | 83 +++++++ .../com/johnChnia/coding2017/basic/Stack.java | 79 ++++++ .../coding2017/basic/test/ArrayListTest.java | 61 +++++ .../coding2017/basic/test/LinkedListTest.java | 92 +++++++ .../coding2017/basic/test/QueueTest.java | 56 +++++ .../coding2017/basic/test/StackTest.java | 69 ++++++ 8 files changed, 838 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..4881c6518c --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -0,0 +1,167 @@ +package com.johnChnia.coding2017.basic; + +import java.util.Arrays; + +/** + * Created by john on 2017/3/8. + * @// TODO: 2017/3/15 支持泛型 + */ + +public class ArrayList { + private int[] elementData; + private int size = 0; + + /** + * Constructs an list with the specified initial capacity. + * + * @param initialCapacity + * @throws IllegalArgumentException if the specified initial capacity + * is negative or zero + */ + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + elementData = new int[initialCapacity]; + } else { + throw new IllegalArgumentException("Illegal Capacity: " + + initialCapacity); + } + } + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position in this list + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public int get(int index) { + rangeCheck(index); + rangeCheckForAdd(index); + return elementData[index]; + } + + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list + */ + public void add(int element) { + ensureCapacityInternal(size + 1); + elementData[size++] = element; + } + + + /** + * Inserts the specified element at the specified position in this + * list. Shifts the element currently at that position (if any) and + * any subsequent elements to the right (adds one to their indices). + * + * @param element element to be inserted + * @param index index at which the specified element is to be inserted + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public void add(int element, int index) { + rangeCheckForAdd(index); + ensureCapacityInternal(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = element; + size++; + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent elements to the left (subtracts one from their + * indices). + * + * @param index the index of the element to be removed + * @return the element that was removed from the list + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + public int remove(int index) { + rangeCheckForAdd(index); + int oldValue = elementData[index]; + int numMoved = size() - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, + numMoved); + } + elementData[--size] = 0; // let jc to clear + return oldValue; + } + + /** + * Returns true if this list contains no elements. + * + * @return true if this list contains no elements + */ + public boolean empty() { + return size == 0; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + + + /** + * Increases the capacity to ensure that it can hold at least the + * number of elements specified by the double length of list. + */ + private void grow() { + elementData = Arrays.copyOf(elementData, 2 * elementData.length); + } + + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("["); + for (int index = 0; index < size(); index++) { + stringBuilder.append(elementData[index]); + stringBuilder.append(", "); + } + stringBuilder.append("]"); + return stringBuilder.toString(); + } + + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity - elementData.length > 0) + grow(); + } + + /** + * A version of rangeCheck used by add and addAll. + */ + private void rangeCheckForAdd(int index) { + if (index > elementData.length - 1 || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + /** + * Constructs an IndexOutOfBoundsException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + elementData.length; + } + + /** + * Checks if the given index is in range. If not, throws an appropriate + * runtime exception. This method does *not* check if the index is + * negative: It is always used immediately prior to an array access, + * which throws an ArrayIndexOutOfBoundsException if index is negative. + */ + private void rangeCheck(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..f2825659b9 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java @@ -0,0 +1,231 @@ +package com.johnChnia.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * Created by john on 2017/3/9. + * + * @// TODO: 2017/3/15 支持泛型 + */ + +public class LinkedList { + + private Node first = null; + private int size = 0; + + /** + * Constructs an empty list. + */ + public LinkedList() { + + } + + private static class Node { + int element; + Node next; + Node prev; + } + + /** + * Appends the specified element to the end of this list. + * + * @param element element to be appended to this list + */ + public void add(int element) { + Node newNode = new Node(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + Node last = first; + while (last.next != null) + last = last.next; + last.next = newNode; + newNode.prev = last; + newNode.next = null; + newNode.element = element; + size++; + } + + private void addWhenListIsEmpty(Node newNode, int element) { + first = newNode; + first.element = element; + first.next = null; + first.prev = null; + size++; + } + + /** + * Inserts the specified element at the beginning of this list. + * + * @param element the element to add + */ + public void addFirst(int element) { + Node newNode = new Node(); + if (first == null) { + addWhenListIsEmpty(newNode, element); + return; + } + newNode.next = first; + newNode.prev = null; + newNode.element = element; + + first.prev = newNode; + first = newNode; + size++; + } + + + /** + * Inserts the specified element at the specified position in this list. + * Shifts the element currently at that position (if any) and any + * subsequent elements to the right (adds one to their indices). + * + * @param index index at which the specified element is to be inserted. + * @param element element to be inserted. + * @throws RuntimeException if list size less than 2. + */ + public void add(int index, int element) { + if (size() < 2) + throw new RuntimeException("list size should greater than or equal to 2"); + isElementIndex(index); + if (index == 0) { + addFirst(element); + return; + } else { + Node temp = new Node(); + Node temp2 = first; + for (int i = 0; i < index; i++) { + temp2 = temp2.next; + } + temp2.prev.next = temp; + temp.prev = temp2.prev; + + temp.next = temp2; + temp2.prev = temp; + temp.element = element; + } + size++; + + } + + + /** + * remove last element in the list. + * + * @throws RuntimeException if the list is empty. + */ + public void remove() { + if (size == 0) + throw new RuntimeException("linkList size should greater than or equal to 1"); + Node next = first.next; + if (next == null) { + first = null; + } else { + Node last = first; + while (last.next != null) + last = last.next; + last.prev.next = null; + last = null; // help GC + } + size--; + } + + + /** + * Removes and returns the first element from this list. + * + * @return the first element from this list + */ + public int removeFirst() { + Node f = first; + if (f == null) + throw new NoSuchElementException(); + int element = f.element; + Node next = first.next; + first.element = 0; + first.next = null; // help GC + + first = next; + if (next != null) { + next.prev = null; + } + size--; + return element; + } + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position in this list + */ + public int get(int index) { + checkElementIndex(index); + Node node = first; + if (index == 0) { + return first.element; + } + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.element; + } + + /** + * Returns the first element in this list. + * + * @return the first element in this list + * @throws NoSuchElementException if this list is empty + */ + public int getFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.element; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + + /** + * Tells if the argument is the index of an existing element. + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(first.element); + Node temp = first; + while (temp.next != null) { + temp = temp.next; + sb.append("→"); + sb.append(temp.element); + } + return sb.toString(); + } + + /** + * Constructs an IndexOutOfBoundsException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java new file mode 100644 index 0000000000..4fd2558ea3 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java @@ -0,0 +1,83 @@ +package com.johnChnia.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * Created by john on 2017/3/10. + * @// TODO: 2017/3/15 支持泛型 + */ +public class Queue { + + private ArrayList arrayList; + + /** + * Constructs an queue using 10 capacity of ArrayList. + */ + public Queue() { + arrayList = new ArrayList(10); + } + + + /** + * Inserts the specified element into this queue.returning + * {@code true} upon success. + * if no space is currently available. + * + * @param element the element to add + * @return {@code true} + */ + public boolean add(int element) { + arrayList.add(element); + return true; + } + + /** + * Retrieves and removes the head of this queue,throws an exception + * if this queue is empty. + * + * @return the head of this queue + * @throws NoSuchElementException if this queue is empty + */ + public int remove() { + if (arrayList.empty()) + throw new NoSuchElementException(emptyMsg()); + return arrayList.remove(0); + + } + + + /** + * Retrieves, but does not remove, the head of this queue, + * or returns {@code null} if this queue is empty. + * + * @return the head of this queue, or {@code 0} if this queue is empty + */ + public int peek() { + if (arrayList.empty()) + return 0; + return arrayList.get(0); + } + + + public String toString() { + return arrayList.toString(); + } + + /** + * Returns the number of elements in this queue. + * + * @return the number of elements in this queue. + */ + public int size() { + return arrayList.size(); + } + + /** + * Constructs an NoSuchElementException detail message. + * Of the many possible refactorings of the error handling code, + * this "outlining" performs best with both server and client VMs. + */ + private String emptyMsg() { + return "Size: " + size(); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java new file mode 100644 index 0000000000..e11be853f9 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java @@ -0,0 +1,79 @@ +package com.johnChnia.coding2017.basic; + +import java.util.EmptyStackException; + +/** + * Created by john on 2017/3/10. + * @// TODO: 2017/3/15 支持泛型 + */ +public class Stack { + private LinkedList linkList; + + /** + * Creates an empty Stack. + */ + public Stack() { + linkList = new LinkedList(); + } + + + /** + * Pushes an item onto the top of this stack. + * + * @param element the element to be pushed onto this stack. + */ + public void push(int element) { + linkList.addFirst(element); + } + + /** + * Removes the object at the top of this stack and returns that + * object as the value of this function. + * + * @return The object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public int pop() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.removeFirst(); + } + + /** + * Looks at the object at the top of this stack without removing it + * from the stack. + * + * @return the object at the top of this stack. + * @throws EmptyStackException if this stack is empty. + */ + public int peek() { + if (empty()) { + throw new EmptyStackException(); + } + return linkList.getFirst(); + } + + /** + * Tests if this stack is empty. + * + * @return true if and only if this stack contains + * no elements; false otherwise. + */ + public boolean empty() { + return linkList.size() == 0; + } + + public String toString() { + return linkList.toString(); + } + + /** + * Returns the number of elements in this stack. + * + * @return the number of elements in this stack + */ + public int size() { + return linkList.size(); + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..13c81724ad --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,61 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.ArrayList; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/8. + */ + +public class ArrayListTest { + private ArrayList arrayList1; + private ArrayList arrayList2; + private ArrayList arrayList3; + private ArrayList arrayList4; + + @Before + public void setUp() throws Exception { + arrayList1 = new ArrayList(3); + arrayList2 = new ArrayList(3); + arrayList3 = new ArrayList(3); + arrayList4 = new ArrayList(3); + } + + @Test + public void testAddAndGet() { + arrayList1.add(99); + assertThat(arrayList1.get(0), equalTo(99)); + } + + @Test + public void testGrow() { + for (int i = 0; i < 6; i++) { + arrayList2.add(10); + } + assertThat(arrayList2.size(), equalTo(6)); + } + + @Test + public void testAddElementByIndex() { + for (int i = 0; i < 3; i++) { + arrayList3.add(10); + } + arrayList3.add(1000, 1); + assertThat(arrayList3.get(1), equalTo(1000)); + } + + @Test + public void testRemoveElementByIndex() { + for (int i = 0; i < 6; i++) { + arrayList4.add(i); + } + int removed = arrayList4.remove(4); + System.out.println(arrayList4); + assertThat(removed, equalTo(4)); + assertThat(arrayList4.size(), equalTo(5)); + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..bd8ad5ac4c --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,92 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.LinkedList; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/9. + */ +public class LinkedListTest { + + private LinkedList linkList1; + private LinkedList linkList2; + private LinkedList linkList3; + private LinkedList linkList4; + private LinkedList linkList5; + private LinkedList linkList6; + + @Before + public void setUp() throws Exception { + linkList1 = new LinkedList(); + linkList2 = new LinkedList(); + linkList3 = new LinkedList(); + linkList4 = new LinkedList(); + linkList5 = new LinkedList(); + linkList6 = new LinkedList(); + } + + @Test + public void testAddAndGet() { + for (int i = 0; i < 4; i++) { + linkList1.add(i); + } + assertThat(linkList1.get(0), equalTo(0)); + assertThat(linkList1.get(1), equalTo(1)); + assertThat(linkList1.get(2), equalTo(2)); + } + + @Test + public void testSize() { + for (int i = 0; i < 4; i++) { + linkList3.add(i); + } + assertThat(linkList3.size(), equalTo(4)); + } + + @Test + public void testAddFirst() { + for (int i = 0; i < 4; i++) { + linkList2.addFirst(i); + } + assertThat(linkList2.get(0), equalTo(3)); + assertThat(linkList2.get(1), equalTo(2)); + } + + @Test + public void testRemove() { + for (int i = 0; i < 2; i++) { + linkList4.addFirst(i); + } + linkList4.remove(); + linkList4.remove(); + assertThat(linkList4.size(), equalTo(0)); + } + + + @Test + public void testAddByIndex() { + for (int i = 0; i < 2; i++) { + linkList5.add(i); + } + linkList5.add(0, 100); + linkList5.add(1, 1000); + System.out.println(linkList5); + assertThat(linkList5.get(1), equalTo(1000)); + } + + @Test + public void testRemoveFirst() { + for (int i = 0; i < 4; i++) { + linkList6.addFirst(i); + } + linkList6.removeFirst(); + linkList6.removeFirst(); + linkList6.removeFirst(); + assertThat(linkList6.get(0), equalTo(0)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..54cd7f9385 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java @@ -0,0 +1,56 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.Queue; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/11. + */ +public class QueueTest { + Queue queue1; + Queue queue2; + Queue queue3; + + @Before + public void setUp() throws Exception { + queue1 = new Queue(); + queue2 = new Queue(); + queue3 = new Queue(); + + } + + @Test + public void testAdd() throws Exception { + for (int i = 0; i < 3; i++) { + queue1.add(i); + } + System.out.println(queue1); + assertThat(queue1.peek(), equalTo(0)); + + } + + @Test + public void testRemove() throws Exception { + for (int i = 0; i < 3; i++) { + queue2.add(i); + } + assertThat(queue2.remove(), equalTo(0)); + assertThat(queue2.remove(), equalTo(1)); + assertThat(queue2.remove(), equalTo(2)); + assertThat(queue2.size(), equalTo(0)); + } + + @Test + public void testPeek() throws Exception { + for (int i = 0; i < 3; i++) { + queue3.add(i); + } + assertThat(queue3.peek(), equalTo(0)); + assertThat(queue3.size(), equalTo(3)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..7fb4a35757 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java @@ -0,0 +1,69 @@ +package com.johnChnia.coding2017.basic.test; + +import com.johnChnia.coding2017.basic.Stack; +import org.junit.Before; +import org.junit.Test; + +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/10. + */ +public class StackTest { + Stack stack1; + Stack stack2; + Stack stack3; + Stack stack4; + + @Before + public void setUp() throws Exception { + stack1 = new Stack(); + stack2 = new Stack(); + stack3 = new Stack(); + stack4 = new Stack(); + } + + @Test + public void testPush() throws Exception { + for (int i = 0; i < 6; i++) { + stack1.push(i); + } + assertThat(stack1.peek(), equalTo(5)); + + } + + @Test + public void testPop() throws Exception { + for (int i = 0; i < 6; i++) { + stack2.push(i); + } + assertThat(stack2.pop(), equalTo(5)); + assertThat(stack2.size(), equalTo(5)); + + } + + @Test + public void testPeek() throws Exception { + for (int i = 0; i < 6; i++) { + stack3.push(i); + } + assertThat(stack3.peek(), equalTo(5)); + assertThat(stack3.size(), equalTo(6)); + + } + + @Test() + public void testEmpty() throws Exception { + for (int i = 0; i < 2; i++) { + stack4.push(i); + } + assertFalse(stack4.empty()); + stack4.pop(); + stack4.pop(); + assertTrue(stack4.empty()); + } + +} \ No newline at end of file From 834d80e9117f470dbb950953a552a80c876b1cbe Mon Sep 17 00:00:00 2001 From: chishiwu <1107225491@qq.com> Date: Sun, 12 Mar 2017 19:46:32 +0800 Subject: [PATCH 16/26] =?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 只实现ArrayList和Stack和博客,其他暂时未完成。 --- group24/1107225491/1107225491/.classpath | 6 - group24/1107225491/1107225491/.project | 17 --- .../.settings/org.eclipse.jdt.core.prefs | 11 -- group24/1107225491/1107225491/test.txt | 1 - .../chishiwu/coding2017/basic/ArrayList.java | 90 +++++++++++++ .../chishiwu/coding2017/basic/Iterator.java | 7 + .../chishiwu/coding2017/basic/LinkedList.java | 124 ++++++++++++++++++ .../chishiwu/coding2017/basic/List.java | 9 ++ .../chishiwu/coding2017/basic/Queue.java | 19 +++ .../chishiwu/coding2017/basic/Stack.java | 49 +++++++ ...2\345\256\242\345\234\260\345\235\200.txt" | 1 + 11 files changed, 299 insertions(+), 35 deletions(-) delete mode 100644 group24/1107225491/1107225491/.classpath delete mode 100644 group24/1107225491/1107225491/.project delete mode 100644 group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs delete mode 100644 group24/1107225491/1107225491/test.txt create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/ArrayList.java create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Iterator.java create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/LinkedList.java create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/List.java create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Queue.java create mode 100644 group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Stack.java create mode 100644 "group24/1107225491/\345\215\232\345\256\242\345\234\260\345\235\200.txt" diff --git a/group24/1107225491/1107225491/.classpath b/group24/1107225491/1107225491/.classpath deleted file mode 100644 index 07ca123c63..0000000000 --- a/group24/1107225491/1107225491/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/group24/1107225491/1107225491/.project b/group24/1107225491/1107225491/.project deleted file mode 100644 index 2d1d33be8a..0000000000 --- a/group24/1107225491/1107225491/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - 1107225491 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs b/group24/1107225491/1107225491/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 54e493c07c..0000000000 --- a/group24/1107225491/1107225491/.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.6 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.6 -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.6 diff --git a/group24/1107225491/1107225491/test.txt b/group24/1107225491/1107225491/test.txt deleted file mode 100644 index 2086f8809c..0000000000 --- a/group24/1107225491/1107225491/test.txt +++ /dev/null @@ -1 +0,0 @@ -�ܶ��ܶ� \ No newline at end of file diff --git a/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/ArrayList.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..193fbbb536 --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/ArrayList.java @@ -0,0 +1,90 @@ +package com.github.chishiwu.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + Object[] elementData = new Object[100]; + + // 动态添加元素 + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size] = o; + size++; + + } + + public void add(int index, Object o) { + Check(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size + - index); + elementData[index] = o; + size++; + } + + // 动态扩容 + private void ensureCapacity(int minCapacity) { + // TODO Auto-generated method stub + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + int newCapacity = (oldCapacity * 3) / 2 + 1; + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public void Check(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("index" + index + "越界"); + } + } + + public Object get(int index) { + Check(index); + return elementData[index]; + } + + public Object remove(int index) { + Check(index); + // 备份 + Object oldValue = elementData[index]; + int num = size - index - 1; + if (num > 0) + System.arraycopy(elementData, index + 1, elementData, index + 1, + num); + elementData[--size] = null; + return oldValue; + + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int currentIndex = 0; + + public boolean hasNext() { + if (currentIndex >= size) + return false; + else + return true; + } + + public Object next() { + Object o = elementData[currentIndex]; + currentIndex++; + return o; + } + } + +} diff --git a/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Iterator.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..9096a3c111 --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.chishiwu.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/LinkedList.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..9e1a5f8646 --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.github.chishiwu.coding2017.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓�3->7->10 , 閫嗙疆鍚庡彉涓� 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓�釜鍗曢摼琛ㄧ殑鍓嶅崐閮ㄥ垎 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫�涓�7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫�涓�,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱�锛�娉ㄦ剰i浠�寮� + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁� + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺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涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛� + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸�澧炴湁搴忔帓鍒楋紙鍚屼竴琛ㄤ腑鐨勫厓绱犲�鍚勪笉鐩稿悓锛� + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸�澧炴湁搴忔帓鍒� + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/List.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/List.java new file mode 100644 index 0000000000..682d54dbfe --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.chishiwu.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/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Queue.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Queue.java new file mode 100644 index 0000000000..0bef3f55fb --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Queue.java @@ -0,0 +1,19 @@ +package com.github.chishiwu.coding2017.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Stack.java b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Stack.java new file mode 100644 index 0000000000..de1856f991 --- /dev/null +++ b/group24/1107225491/1107225491Learning/src/com/github/chishiwu/coding2017/basic/Stack.java @@ -0,0 +1,49 @@ +package com.github.chishiwu.coding2017.basic; + +public class Stack { + private Node mStackNode; + private int size; + + public void push(Object o) { + Node node = new Node(); + node.data = o; + if (null == mStackNode) { + mStackNode = node; + } else { + mStackNode.next = node; + mStackNode = node; + } + size++; + } + + public Object pop() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + Object obj = mStackNode.data; + mStackNode = mStackNode.pre; + size--; + return obj; + } + + public Object peek() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + return mStackNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} diff --git "a/group24/1107225491/\345\215\232\345\256\242\345\234\260\345\235\200.txt" "b/group24/1107225491/\345\215\232\345\256\242\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..eb815ca931 --- /dev/null +++ "b/group24/1107225491/\345\215\232\345\256\242\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +http://www.cnblogs.com/chishiwu/p/6514526.html \ No newline at end of file From fe56c0db53ce2cbd9bc7e605c4a265ce1f77da22 Mon Sep 17 00:00:00 2001 From: DonaldY <448641125@qq.com> Date: Sun, 12 Mar 2017 20:04:59 +0800 Subject: [PATCH 17/26] format error --- group24/626451284Learning/pom.xml | 12 - .../wdn/coding2017/basic/ArrayList.java | 88 ---- .../wdn/coding2017/basic/BinaryTreeNode.java | 35 -- .../github/wdn/coding2017/basic/Iterator.java | 10 - .../wdn/coding2017/basic/LinkedList.java | 233 ---------- .../com/github/wdn/coding2017/basic/List.java | 9 - .../github/wdn/coding2017/basic/Queue.java | 24 - .../github/wdn/coding2017/basic/Stack.java | 27 -- .../CJ-chen/coding2017/basic/ArrayList.java | 123 ----- .../coding2017/basic/BinaryTreeNode.java | 87 ---- .../CJ-chen/coding2017/basic/Iterator.java | 16 - .../CJ-chen/coding2017/basic/LinkedList.java | 423 ------------------ .../github/CJ-chen/coding2017/basic/List.java | 18 - .../CJ-chen/coding2017/basic/Queue.java | 58 --- .../CJ-chen/coding2017/basic/Stack.java | 63 --- 15 files changed, 1226 deletions(-) delete mode 100644 group24/626451284Learning/pom.xml delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/ArrayList.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/Iterator.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/LinkedList.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/List.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/Queue.java delete mode 100644 group24/com/github/CJ-chen/coding2017/basic/Stack.java diff --git a/group24/626451284Learning/pom.xml b/group24/626451284Learning/pom.xml deleted file mode 100644 index 534ff39232..0000000000 --- a/group24/626451284Learning/pom.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - 4.0.0 - - com.github.wdn - coding2017 - 1.0-SNAPSHOT - - - \ No newline at end of file diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java deleted file mode 100644 index 3b3ea048f3..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.github.wdn.coding2017.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData; - public ArrayList(){ - elementData = new Object[10]; - } - public ArrayList(int size){ - elementData = new Object[size]; - } - public void add(Object o){ - if(size>=elementData.length){ - elementData = Arrays.copyOf(elementData,elementData.length*2); - } - elementData[size]=o; - size++; - } - public void add(int index, Object o){ - Object[] newElementData; - if(size()+1>=elementData.length){ - newElementData=new Object[elementData.length*2]; - - }else{ - newElementData=new Object[elementData.length]; - } - for (int i = 0; i < elementData.length; i++) { - if(index==1){ - newElementData[i]=o; - }else if(i>index) { - newElementData[i]=elementData[i-1]; - }else{ - newElementData[i]=elementData[i]; - } - } - elementData = newElementData; - size++; - } - - public Object get(int index){ - if(index>=size){ - throw new IndexOutOfBoundsException(); - } - return elementData[index]; - } - - public Object remove(int index) { - if(index>=size){ - throw new IndexOutOfBoundsException(); - } - Object returnO = elementData[index]; - for (int i = index; i < size; i++) { - if(i==size-1){ - elementData[i]=null; - }else { - elementData[i] = elementData[i + 1]; - } - } - size--; - return returnO; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return null; - } - - public static void main(String[] args) { - ArrayList list = new ArrayList(2); - list.add("1"); - list.add("2"); - list.remove(1); - list.add("3"); - for (int i = 0; i < list.size(); i++) { - System.out.println(list.get(i)); - } - int[] i = {}; - System.out.println(i[0]); - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index b8b613d6f0..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 代码在公司电脑里。。。后续提交 - */ -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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java deleted file mode 100644 index 044ddf0993..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 主要考虑遍历中安全删除元素的实现 - */ -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java deleted file mode 100644 index 6040cb5a47..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 只是简单实现 缺少对边界的处理 - * 参考JDK源码改为更优雅的实现 - */ - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size; - - public LinkedList(){ - this.head=null; - this.tail=null; - } - public void add(Object o){ - Node newNode = new Node(o); - if (head == null) { - head = newNode; - tail = newNode; - }else{ - tail.setNext(newNode); - newNode.setPre(tail); - tail = newNode; - } - size++; - } - public void add(int index , Object o){ - checkIndex(index); - Node indexNode = getNode(index); - Node newNode = new Node(o); - Node pre = indexNode.getPre(); - if(pre!=null){ - pre.setNext(newNode); - }else{ - head = newNode; - } - newNode.setPre(pre); - newNode.setNext(indexNode); - indexNode.setPre(newNode); - } - private void checkIndex(int index){ - if(index >= size || index <0){ - throw new IndexOutOfBoundsException(); - } - } - private Node getNode(int index){ - checkIndex(index); - // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 - Node result=null; - if(index==0){ - return head; - } - if(index==size-1){ - return tail; - } - for (int i = 0; i < index; i++) { - result = head.getNext(); - } - return result; - } - public Object get(int index){ - return getNode(index).getData(); - } - public Object remove(int index){ - checkIndex(index); - Node indexNode = getNode(index); - Node pre = indexNode.getPre(); - Node next = indexNode.getNext(); - if(pre!=null){ - pre.setNext(next); - }else{ - head = next; - } - if(next!=null){ - next.setPre(pre); - }else{ - tail = pre; - } - return indexNode.getData(); - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node newNode = new Node(o); - head.setPre(newNode); - newNode.setNext(head); - head = newNode; - } - public void addLast(Object o){ - Node newNode = new Node(o); - tail.setNext(newNode); - newNode.setPre(tail); - tail = newNode; - } - public Object removeFirst(){ - Node next = head.getNext(); - Node oldHead = head; - head = next; - head.setPre(null); - return oldHead; - } - public Object removeLast(){ - Node oldTail = tail; - Node pre = tail.getPre(); - tail = pre; - tail.setNext(null); - return oldTail; - } - public Iterator iterator(){ - - return null; - } - - /** - * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 - */ - private static class Node{ - - Object data; - Node next; - Node pre; - public Node(){ - - } - public Node(Object data){ - this.data = data; - } - 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; - } - - public Node getPre() { - return pre; - } - - public void setPre(Node pre) { - this.pre = pre; - } - } - - /** - * 把该链表逆置 - * 例如链表为 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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java deleted file mode 100644 index d6c9e95cb0..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.wdn.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/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java deleted file mode 100644 index 59992f0cbd..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * 队列使用链表实现比较简单理论上是无界队列 - * 如果使用数组需要处理很多问题比如长度限制,元素的复制 - */ -public class Queue { - private LinkedList queue = new LinkedList(); - public void enQueue(Object o){ - queue.add(o); - } - - public Object deQueue(){ - return queue.remove(0); - } - - public boolean isEmpty(){ - return queue.size()==0; - } - - public int size(){ - return queue.size(); - } -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java deleted file mode 100644 index f0a0fe56e9..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * 使用list实现比较简单 - * 可以考虑使用其它结构 - */ -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/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java deleted file mode 100644 index cbb7084045..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -import java.util.Arrays; - -/** - * - * @author CJ - */ -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - private final int defaultGrowSize = 100; // 每次增长 100 个元素 - -// private int curIterIndex = 0; // 用于记录 Iterator的索引 - - private void CheckAndGrowUp() { - if (size+1 > elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length + defaultGrowSize); - } - } - - // 添加一个方法,检测看,添加元素的话,是否需要增长,专门用于数组长度扩展的 - public void add(Object o) { - CheckAndGrowUp(); - elementData[size] = o; - size++; - } - - @Override - public void add(int index, Object o) { - // 先探测是否添加数组大小 - CheckAndGrowUp(); - // 保留后半部分,然后 全部替换即可 - - Object[] tmpObjectArr = Arrays.copyOfRange(elementData, index, elementData.length); - elementData[index] = o; - for (int i = index+1; i < size+1; i++) { - elementData[i] = tmpObjectArr[i-index-1]; - } - size++; - - } - - public Object get(int index) { - // 应该是 不需要跑出 下标越界异常的,因为数组越界会自动抛出 - return elementData[index]; - } - - public Object remove(int index) { - for (int i = index; i < size; i++) { - elementData[i] = elementData[i + 1]; - } - elementData[size] = null; // 后续后面是 数值?那么就不能为null了,而是零? - size--; - return null; - } - - @Override - public int size() { - return size; - } - - // 覆盖toString方法,方便测试 - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("ArrayList: ["); - for (int i = 0; i < size; i++) { - sb.append(elementData[i]).append(", "); - } -// System.err.println(size); - sb.delete(sb.length()-2,sb.length()).append("]"); - return sb.toString(); - } - - public Iterator iterator() { - - return new Iterator() { - int curIterIndex = 0; - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return curIterIndex < size; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - return elementData[curIterIndex++]; - } - - }; - } - - public static void main(String[] args) { - ArrayList curArrList = new ArrayList(); - for (int i = 0; i <= 101; i++) { - curArrList.add(i); - } - System.err.println("Test add Arr"); - System.err.println(curArrList); - System.err.println("Test add in specific index"); - curArrList.add(10, 1010); - System.err.println(curArrList); - System.err.println("Test remove"); - curArrList.remove(10); - System.err.println(curArrList); - System.err.println("Test Iterator"); - Iterator curIter = curArrList.iterator(); - while(curIter.hasNext()){ - System.err.println(curIter.next()); - } - } - -} diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index ff40538e64..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -/** - * - * @author CJ - */ -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) { - // 应该只需要实现这个就可以了 - int curValue = (Integer) this.getData(); - int insertValue = (Integer) o; - - BinaryTreeNode newNode = new BinaryTreeNode(); - newNode.setData(o); - - if (curValue > insertValue) { - if (this.getLeft() != null) { - return this.getLeft().insert(o); - } else { - this.setLeft(newNode); - return this; - } - } else{ - if (this.getRight() != null) { - return this.getRight().insert(o); - } else { - this.setRight(newNode); - return this; - } - } - } - - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(this.getData()).append("\n"); - sb.append(this.getLeft()).append("<--").append(this.getData()).append("\n"); - sb.append(this.getData()).append("-->").append(this.getRight()).append("\n"); - return sb.toString(); - } - - public static void main(String[] args) { - BinaryTreeNode btn = new BinaryTreeNode(); - btn.setData(5); -// btn.insert(5); - btn.insert(7); - btn.insert(8); - btn.insert(9); - btn.insert(4); - - System.err.println(btn); - } - -} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/com/github/CJ-chen/coding2017/basic/Iterator.java deleted file mode 100644 index 29ab5ecaf0..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/Iterator.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -/** - * - * @author CJ - */ -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} \ No newline at end of file diff --git a/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java deleted file mode 100644 index 0364c16480..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,423 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -import java.util.Arrays; - -/** - * - * @author CJ - */ -public class LinkedList implements List { - - private Node head; - - private int size; -// private int curIterIndex; - - public LinkedList() { - head = new Node(); - size = 0; -// curIterIndex = 0; - } - - public void add(Object o) { - addLast(o); - } - - private Node getNode(int index) { - if (index == -1) { - return head; - } else { - Node returnNode = head.next; - for (int i = 0; i < index; i++) { - returnNode = returnNode.next; - } - return returnNode; - } - } - - public void add(int index, Object o) { - Node preNode = getNode(index - 1); - Node addNode = new Node(); - addNode.data = o; - addNode.next = preNode.next; - preNode.next = addNode; - size++; - } - - public Object get(int index) { - return getNode(index).data; - } - - public Object remove(int index) { - Node preNode = getNode(index - 1); - Node delNode = preNode.next; - preNode.next = delNode.next; - // 返回被删除的Node... 可能是为了测试吧 - size--; - return delNode; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - Node fNode = new Node(); - fNode.data = o; - fNode.next = head.next; - head.next = fNode; - size++; - } - - public void addLast(Object o) { -// System.err.println("Curr add: "+ o); - Node lastNode = getNode(size - 1); -// System.err.println(lastNode); - Node lNode = new Node(); - lNode.data = o; - lastNode.next = lNode; - size++; - } - - public Object removeFirst() { - return removeFirstNode().data; - } - - private Node removeFirstNode() { - Node firstNode = head.next; - head.next = firstNode.next; - size--; - return firstNode; - } - - public Object removeLast() { - return removeLastNode().data; - } - - private Node removeLastNode() { - Node last2Node = getNode(size - 2); - Node lastNode = last2Node.next; - last2Node.next = null; - size--; - return lastNode; - } - - public Iterator iterator() { - return new Iterator() { -// int curIterIndex = 0; - Node curNode = head; - - @Override - public boolean hasNext() { - return curNode.next != null; - } - - @Override - public Object next() { - curNode = curNode.next; - return curNode.data; - } - }; - } - - private static class Node { - - Object data; - Node next; - } - - /** - * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse() { - - int oriSize = size; - Node newHead = new Node(); - newHead.next = this.removeLastNode(); - Node preNode = newHead.next; - - while (size > 0) { - preNode.next = this.removeLastNode(); - preNode = preNode.next; - } - // 不考虑线程安全的情况下,恢复size - size = oriSize; - head = newHead; - } - - /** - * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 - * ,删除以后的值为7,8,10 - */ - public void removeFirstHalf() { - int reDirIndex = size / 2; // java会自动 取 0 - size = size - reDirIndex; - System.err.println(reDirIndex); - Node jumpNode = getNode(reDirIndex); - head.next = jumpNode; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * - * @param i - * @param length - */ - public void remove(int i, int length) { - Node fromNode = getNode(i - 1); - for (int j = 0; j < length; j++) { - fromNode.next = fromNode.next.next; - size--; - } - } - - /** - * 假定当前链表和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) { - // 这个似乎 不应该是 静态方法 - public int[] getElements(LinkedList list) { - int[] returnIndex = new int[list.size]; - int validCounts = 0; -// Iterator curIter = list.iterator(); -// int curIndex = 0; -// while(curIter.hasNext()){ -// returnIndex[curIndex++] =(Integer) this.get((Integer) curIter.next()); -// } - - // 已知是内外都是玩去排序好的,所以是需要一个高效的算法 - Iterator innerIter = this.iterator(); - Iterator curIter = list.iterator(); - int curCount = 0; - int curIndex; - int preIndex = 0; - - while (curIter.hasNext()) { - curIndex = (Integer) curIter.next(); - if (curIndex < preIndex) { - System.err.println("Warning: Skip index no in sorted order..."); - continue; - } -// int skipCounts = curIndex-preIndex; - for (int i = preIndex; i < curIndex; i++) { - innerIter.next(); - } - validCounts++; - returnIndex[curCount++] = (Integer) innerIter.next(); - preIndex = curIndex + 1; - } - return Arrays.copyOf(returnIndex, validCounts); - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 - * - * @param list - */ - public void subtract(LinkedList list) { - // 假定 当前 链表 和 待删除 链表 都是顺序排序的 - Node preNode = head; - Node curNode; - Iterator element2rmIter = list.iterator(); - while (element2rmIter.hasNext()) { - int curValue2rm = (Integer) element2rmIter.next(); - while (preNode.next != null) { - curNode = preNode.next; - if ((Integer) curNode.data == curValue2rm) { - // 删除 - preNode.next = curNode.next; - }else if((Integer) curNode.data > curValue2rm){ - break; // 跳出内层循环,从而获取下一个待删除数据 - }else { - // 更新 - preNode = curNode; - } - } - } - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues() { - Node preNode = head; - Node curNode; - while (preNode.next != null) { - curNode = preNode.next; - if (curNode.data == preNode.data) { - preNode.next = curNode.next; - } else { - preNode = curNode; - } - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * - * @param min - * @param max - */ - public void removeRange(int min, int max) { - // 假定 当前链表 是 从小到大 排列的 - Node preNode = head; - Node curNode; - while (preNode.next != null) { - curNode = preNode.next; - if (min < (Integer) curNode.data && max > (Integer) curNode.data) { - preNode.next = curNode.next; - } else { - preNode = curNode; - } - } - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * - * @param list - */ - public LinkedList intersection(LinkedList list) { - - - LinkedList newList = new LinkedList(); - - // 假定 当前 链表 和 待删除 链表 都是顺序排序的 - Node preNode = head; - Node curNode; - Iterator element2rmIter = list.iterator(); - while (element2rmIter.hasNext()) { - int curValue2rm = (Integer) element2rmIter.next(); - while (preNode.next != null) { - curNode = preNode.next; - if ((Integer) curNode.data == curValue2rm) { - // 删除 - preNode = curNode; - newList.add(curNode.data); // 添加data - }else if((Integer) curNode.data > curValue2rm){ - break; // 跳出内层循环,从而获取下一个待删除数据 - }else { - // 更新 - preNode = curNode; - } - } - } - - - return newList; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("LinkdedList ["); - Node curNode = head; - while (curNode.next != null) { - curNode = curNode.next; - sb.append(curNode.data).append(", "); - } - sb.delete(sb.length() - 2, sb.length()).append("]"); - return sb.toString(); - } - - public void clear() { - head.next = null; - size = 0; - } - - public static void main(String[] args) { - // 先写一些测试 - LinkedList ll = new LinkedList(); - System.err.println("Test Add"); - for (int i = 0; i < 10; i++) { - ll.add(i); - } - System.err.println("Size: " + ll.size); - System.err.println(ll); - System.err.println("Test Add last"); - ll.addLast(10); - System.err.println(ll); - System.err.println("Test Add First"); - ll.addFirst(-1); - System.err.println(ll); - System.err.println("Test remove "); - ll.remove(0); - System.err.println(ll); -// ll.remove(11); -// System.err.println(ll); - ll.removeFirst(); - System.err.println(ll); - ll.removeLast(); - System.err.println(ll); - System.err.println("Test reverse()"); - ll.reverse(); - System.err.println(ll); - System.err.println("Test removeFirstHalf()"); - ll.removeFirstHalf(); - System.err.println(ll); -// System.err.println(9/2); - System.err.println("Test remove()"); - ll.remove(1, 2); - System.err.println(ll); -// System.err.println(ll.); - LinkedList newList = new LinkedList(); - newList.add(3); - newList.add(4); - newList.add(6); - newList.add(5); -// System.err.println(); - ll.clear(); - System.err.println("Re Init"); - for (int i = 0; i < 10; i++) { - ll.add(i); - } - System.err.println(ll); - for (int curI : ll.getElements(newList)) { - System.err.println(curI); - } - ll.clear(); - for (int i = 0; i < 10; i++) { - ll.add(i); - ll.add(i); - ll.add(i); - } - System.err.println(ll); - ll.removeDuplicateValues(); - System.err.println(ll); - System.err.println("Test Remove removeRange(3, 6)"); - ll.removeRange(3, 6); - System.err.println(ll); - - LinkedList rmList = new LinkedList(); - rmList.add(3); - rmList.add(4); - rmList.add(7); - rmList.add(8); - ll.subtract(rmList); - System.err.println("Test subtract(3,4,7,8)"); - System.err.println(ll); - ll.clear(); - for (int i = 0; i < 10; i++) { - ll.add(i); - } - rmList.add(11); - System.err.println(ll); - System.err.println(rmList); - System.err.println(ll.intersection(rmList)); -// System.err.println("Test substract"); -// ll.subtract(newList); -// System.err.println(ll); - - } - -} diff --git a/group24/com/github/CJ-chen/coding2017/basic/List.java b/group24/com/github/CJ-chen/coding2017/basic/List.java deleted file mode 100644 index d347e59c47..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/List.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -/** - * - * @author CJ - */ -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/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/com/github/CJ-chen/coding2017/basic/Queue.java deleted file mode 100644 index 4cbc45e065..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/Queue.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -/** - * - * @author CJ - */ -public class Queue { - - private final LinkedList innerList; - - public Queue(){ - innerList = new LinkedList(); - } - - public void enQueue(Object o) { - innerList.addLast(o); - } - - public Object deQueue() { - return innerList.removeFirst(); - } - - public boolean isEmpty() { - return innerList.size()==0; - } - - public int size() { - return innerList.size(); - } - - public String toString(){ - StringBuilder sb = new StringBuilder(); - sb.append("ArrayList: ["); - for (int i = 0; i < innerList.size(); i++) { - sb.append(innerList.get(i)).append(", "); - } -// System.err.println(size); - sb.delete(sb.length()-2,sb.length()).append("]"); - return sb.toString(); - } - - public static void main(String[] args) { - Queue newQ = new Queue(); - for(int i=0;i<10;i++){ - newQ.enQueue(i); - } - System.err.println(newQ); - newQ.deQueue(); - System.err.println(newQ); - } - - -} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/com/github/CJ-chen/coding2017/basic/Stack.java deleted file mode 100644 index 3ba85c6bcf..0000000000 --- a/group24/com/github/CJ-chen/coding2017/basic/Stack.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package javaclass; - -/** - * - * @author CJ - */ -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(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("ArrayList: ["); - for (int i = 0; i < elementData.size(); i++) { - sb.append(elementData.get(i)).append(", "); - } -// System.err.println(size); - sb.delete(sb.length()-2,sb.length()).append("]"); - return sb.toString(); - } - public static void main(String[] args) { - Stack newS = new Stack(); - for(int i=0;i<10;i++){ - newS.push(i); - } - System.err.println("Test push()"); - System.err.println(newS); - newS.pop(); - System.err.println("Test pop()"); - System.err.println(newS); - System.err.println("Test peek()"); - System.err.println(newS.peek()); - System.err.println(newS); - - } - -} From 5e5842b1b479ed6928ca946e252fe7ae647ccc4a Mon Sep 17 00:00:00 2001 From: DonaldY <448641125@qq.com> Date: Sun, 12 Mar 2017 20:06:40 +0800 Subject: [PATCH 18/26] correct fomat --- .../CJ-chen/coding2017/basic/ArrayList.java | 123 +++++ .../coding2017/basic/BinaryTreeNode.java | 87 ++++ .../CJ-chen/coding2017/basic/Iterator.java | 16 + .../CJ-chen/coding2017/basic/LinkedList.java | 423 ++++++++++++++++++ .../github/CJ-chen/coding2017/basic/List.java | 18 + .../CJ-chen/coding2017/basic/Queue.java | 58 +++ .../CJ-chen/coding2017/basic/Stack.java | 63 +++ group24/626451284/pom.xml | 12 + .../wdn/coding2017/basic/ArrayList.java | 88 ++++ .../wdn/coding2017/basic/BinaryTreeNode.java | 35 ++ .../github/wdn/coding2017/basic/Iterator.java | 10 + .../wdn/coding2017/basic/LinkedList.java | 233 ++++++++++ .../com/github/wdn/coding2017/basic/List.java | 9 + .../github/wdn/coding2017/basic/Queue.java | 24 + .../github/wdn/coding2017/basic/Stack.java | 27 ++ 15 files changed, 1226 insertions(+) create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/ArrayList.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/Iterator.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/LinkedList.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/List.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/Queue.java create mode 100644 group24/120509419/com/github/CJ-chen/coding2017/basic/Stack.java create mode 100644 group24/626451284/pom.xml create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cbb7084045 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/ArrayList.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final int defaultGrowSize = 100; // 每次增长 100 个元素 + +// private int curIterIndex = 0; // 用于记录 Iterator的索引 + + private void CheckAndGrowUp() { + if (size+1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + defaultGrowSize); + } + } + + // 添加一个方法,检测看,添加元素的话,是否需要增长,专门用于数组长度扩展的 + public void add(Object o) { + CheckAndGrowUp(); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + // 先探测是否添加数组大小 + CheckAndGrowUp(); + // 保留后半部分,然后 全部替换即可 + + Object[] tmpObjectArr = Arrays.copyOfRange(elementData, index, elementData.length); + elementData[index] = o; + for (int i = index+1; i < size+1; i++) { + elementData[i] = tmpObjectArr[i-index-1]; + } + size++; + + } + + public Object get(int index) { + // 应该是 不需要跑出 下标越界异常的,因为数组越界会自动抛出 + return elementData[index]; + } + + public Object remove(int index) { + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size] = null; // 后续后面是 数值?那么就不能为null了,而是零? + size--; + return null; + } + + @Override + public int size() { + return size; + } + + // 覆盖toString方法,方便测试 + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public Iterator iterator() { + + return new Iterator() { + int curIterIndex = 0; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return curIterIndex < size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[curIterIndex++]; + } + + }; + } + + public static void main(String[] args) { + ArrayList curArrList = new ArrayList(); + for (int i = 0; i <= 101; i++) { + curArrList.add(i); + } + System.err.println("Test add Arr"); + System.err.println(curArrList); + System.err.println("Test add in specific index"); + curArrList.add(10, 1010); + System.err.println(curArrList); + System.err.println("Test remove"); + curArrList.remove(10); + System.err.println(curArrList); + System.err.println("Test Iterator"); + Iterator curIter = curArrList.iterator(); + while(curIter.hasNext()){ + System.err.println(curIter.next()); + } + } + +} diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..ff40538e64 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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) { + // 应该只需要实现这个就可以了 + int curValue = (Integer) this.getData(); + int insertValue = (Integer) o; + + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + + if (curValue > insertValue) { + if (this.getLeft() != null) { + return this.getLeft().insert(o); + } else { + this.setLeft(newNode); + return this; + } + } else{ + if (this.getRight() != null) { + return this.getRight().insert(o); + } else { + this.setRight(newNode); + return this; + } + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(this.getData()).append("\n"); + sb.append(this.getLeft()).append("<--").append(this.getData()).append("\n"); + sb.append(this.getData()).append("-->").append(this.getRight()).append("\n"); + return sb.toString(); + } + + public static void main(String[] args) { + BinaryTreeNode btn = new BinaryTreeNode(); + btn.setData(5); +// btn.insert(5); + btn.insert(7); + btn.insert(8); + btn.insert(9); + btn.insert(4); + + System.err.println(btn); + } + +} diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..29ab5ecaf0 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/Iterator.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..0364c16480 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/LinkedList.java @@ -0,0 +1,423 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class LinkedList implements List { + + private Node head; + + private int size; +// private int curIterIndex; + + public LinkedList() { + head = new Node(); + size = 0; +// curIterIndex = 0; + } + + public void add(Object o) { + addLast(o); + } + + private Node getNode(int index) { + if (index == -1) { + return head; + } else { + Node returnNode = head.next; + for (int i = 0; i < index; i++) { + returnNode = returnNode.next; + } + return returnNode; + } + } + + public void add(int index, Object o) { + Node preNode = getNode(index - 1); + Node addNode = new Node(); + addNode.data = o; + addNode.next = preNode.next; + preNode.next = addNode; + size++; + } + + public Object get(int index) { + return getNode(index).data; + } + + public Object remove(int index) { + Node preNode = getNode(index - 1); + Node delNode = preNode.next; + preNode.next = delNode.next; + // 返回被删除的Node... 可能是为了测试吧 + size--; + return delNode; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node fNode = new Node(); + fNode.data = o; + fNode.next = head.next; + head.next = fNode; + size++; + } + + public void addLast(Object o) { +// System.err.println("Curr add: "+ o); + Node lastNode = getNode(size - 1); +// System.err.println(lastNode); + Node lNode = new Node(); + lNode.data = o; + lastNode.next = lNode; + size++; + } + + public Object removeFirst() { + return removeFirstNode().data; + } + + private Node removeFirstNode() { + Node firstNode = head.next; + head.next = firstNode.next; + size--; + return firstNode; + } + + public Object removeLast() { + return removeLastNode().data; + } + + private Node removeLastNode() { + Node last2Node = getNode(size - 2); + Node lastNode = last2Node.next; + last2Node.next = null; + size--; + return lastNode; + } + + public Iterator iterator() { + return new Iterator() { +// int curIterIndex = 0; + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode.next != null; + } + + @Override + public Object next() { + curNode = curNode.next; + return curNode.data; + } + }; + } + + private static class Node { + + Object data; + Node next; + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + int oriSize = size; + Node newHead = new Node(); + newHead.next = this.removeLastNode(); + Node preNode = newHead.next; + + while (size > 0) { + preNode.next = this.removeLastNode(); + preNode = preNode.next; + } + // 不考虑线程安全的情况下,恢复size + size = oriSize; + head = newHead; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int reDirIndex = size / 2; // java会自动 取 0 + size = size - reDirIndex; + System.err.println(reDirIndex); + Node jumpNode = getNode(reDirIndex); + head.next = jumpNode; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + Node fromNode = getNode(i - 1); + for (int j = 0; j < length; j++) { + fromNode.next = fromNode.next.next; + size--; + } + } + + /** + * 假定当前链表和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) { + // 这个似乎 不应该是 静态方法 + public int[] getElements(LinkedList list) { + int[] returnIndex = new int[list.size]; + int validCounts = 0; +// Iterator curIter = list.iterator(); +// int curIndex = 0; +// while(curIter.hasNext()){ +// returnIndex[curIndex++] =(Integer) this.get((Integer) curIter.next()); +// } + + // 已知是内外都是玩去排序好的,所以是需要一个高效的算法 + Iterator innerIter = this.iterator(); + Iterator curIter = list.iterator(); + int curCount = 0; + int curIndex; + int preIndex = 0; + + while (curIter.hasNext()) { + curIndex = (Integer) curIter.next(); + if (curIndex < preIndex) { + System.err.println("Warning: Skip index no in sorted order..."); + continue; + } +// int skipCounts = curIndex-preIndex; + for (int i = preIndex; i < curIndex; i++) { + innerIter.next(); + } + validCounts++; + returnIndex[curCount++] = (Integer) innerIter.next(); + preIndex = curIndex + 1; + } + return Arrays.copyOf(returnIndex, validCounts); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode.next = curNode.next; + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (curNode.data == preNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + // 假定 当前链表 是 从小到大 排列的 + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (min < (Integer) curNode.data && max > (Integer) curNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + + + LinkedList newList = new LinkedList(); + + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode = curNode; + newList.add(curNode.data); // 添加data + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + + + return newList; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("LinkdedList ["); + Node curNode = head; + while (curNode.next != null) { + curNode = curNode.next; + sb.append(curNode.data).append(", "); + } + sb.delete(sb.length() - 2, sb.length()).append("]"); + return sb.toString(); + } + + public void clear() { + head.next = null; + size = 0; + } + + public static void main(String[] args) { + // 先写一些测试 + LinkedList ll = new LinkedList(); + System.err.println("Test Add"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println("Size: " + ll.size); + System.err.println(ll); + System.err.println("Test Add last"); + ll.addLast(10); + System.err.println(ll); + System.err.println("Test Add First"); + ll.addFirst(-1); + System.err.println(ll); + System.err.println("Test remove "); + ll.remove(0); + System.err.println(ll); +// ll.remove(11); +// System.err.println(ll); + ll.removeFirst(); + System.err.println(ll); + ll.removeLast(); + System.err.println(ll); + System.err.println("Test reverse()"); + ll.reverse(); + System.err.println(ll); + System.err.println("Test removeFirstHalf()"); + ll.removeFirstHalf(); + System.err.println(ll); +// System.err.println(9/2); + System.err.println("Test remove()"); + ll.remove(1, 2); + System.err.println(ll); +// System.err.println(ll.); + LinkedList newList = new LinkedList(); + newList.add(3); + newList.add(4); + newList.add(6); + newList.add(5); +// System.err.println(); + ll.clear(); + System.err.println("Re Init"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println(ll); + for (int curI : ll.getElements(newList)) { + System.err.println(curI); + } + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + ll.add(i); + ll.add(i); + } + System.err.println(ll); + ll.removeDuplicateValues(); + System.err.println(ll); + System.err.println("Test Remove removeRange(3, 6)"); + ll.removeRange(3, 6); + System.err.println(ll); + + LinkedList rmList = new LinkedList(); + rmList.add(3); + rmList.add(4); + rmList.add(7); + rmList.add(8); + ll.subtract(rmList); + System.err.println("Test subtract(3,4,7,8)"); + System.err.println(ll); + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + rmList.add(11); + System.err.println(ll); + System.err.println(rmList); + System.err.println(ll.intersection(rmList)); +// System.err.println("Test substract"); +// ll.subtract(newList); +// System.err.println(ll); + + } + +} diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/List.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/List.java new file mode 100644 index 0000000000..d347e59c47 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/List.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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/120509419/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/Queue.java new file mode 100644 index 0000000000..4cbc45e065 --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/Queue.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public class Queue { + + private final LinkedList innerList; + + public Queue(){ + innerList = new LinkedList(); + } + + public void enQueue(Object o) { + innerList.addLast(o); + } + + public Object deQueue() { + return innerList.removeFirst(); + } + + public boolean isEmpty() { + return innerList.size()==0; + } + + public int size() { + return innerList.size(); + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < innerList.size(); i++) { + sb.append(innerList.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public static void main(String[] args) { + Queue newQ = new Queue(); + for(int i=0;i<10;i++){ + newQ.enQueue(i); + } + System.err.println(newQ); + newQ.deQueue(); + System.err.println(newQ); + } + + +} diff --git a/group24/120509419/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/120509419/com/github/CJ-chen/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3ba85c6bcf --- /dev/null +++ b/group24/120509419/com/github/CJ-chen/coding2017/basic/Stack.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +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(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < elementData.size(); i++) { + sb.append(elementData.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + public static void main(String[] args) { + Stack newS = new Stack(); + for(int i=0;i<10;i++){ + newS.push(i); + } + System.err.println("Test push()"); + System.err.println(newS); + newS.pop(); + System.err.println("Test pop()"); + System.err.println(newS); + System.err.println("Test peek()"); + System.err.println(newS.peek()); + System.err.println(newS); + + } + +} diff --git a/group24/626451284/pom.xml b/group24/626451284/pom.xml new file mode 100644 index 0000000000..534ff39232 --- /dev/null +++ b/group24/626451284/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.github.wdn + coding2017 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..3b3ea048f3 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java @@ -0,0 +1,88 @@ +package com.github.wdn.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + public ArrayList(){ + elementData = new Object[10]; + } + public ArrayList(int size){ + elementData = new Object[size]; + } + public void add(Object o){ + if(size>=elementData.length){ + elementData = Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + Object[] newElementData; + if(size()+1>=elementData.length){ + newElementData=new Object[elementData.length*2]; + + }else{ + newElementData=new Object[elementData.length]; + } + for (int i = 0; i < elementData.length; i++) { + if(index==1){ + newElementData[i]=o; + }else if(i>index) { + newElementData[i]=elementData[i-1]; + }else{ + newElementData[i]=elementData[i]; + } + } + elementData = newElementData; + size++; + } + + public Object get(int index){ + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index) { + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + Object returnO = elementData[index]; + for (int i = index; i < size; i++) { + if(i==size-1){ + elementData[i]=null; + }else { + elementData[i] = elementData[i + 1]; + } + } + size--; + return returnO; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(2); + list.add("1"); + list.add("2"); + list.remove(1); + list.add("3"); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + int[] i = {}; + System.out.println(i[0]); + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b8b613d6f0 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 代码在公司电脑里。。。后续提交 + */ +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/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..044ddf0993 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java @@ -0,0 +1,10 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 主要考虑遍历中安全删除元素的实现 + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..6040cb5a47 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -0,0 +1,233 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 只是简单实现 缺少对边界的处理 + * 参考JDK源码改为更优雅的实现 + */ + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public LinkedList(){ + this.head=null; + this.tail=null; + } + public void add(Object o){ + Node newNode = new Node(o); + if (head == null) { + head = newNode; + tail = newNode; + }else{ + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + size++; + } + public void add(int index , Object o){ + checkIndex(index); + Node indexNode = getNode(index); + Node newNode = new Node(o); + Node pre = indexNode.getPre(); + if(pre!=null){ + pre.setNext(newNode); + }else{ + head = newNode; + } + newNode.setPre(pre); + newNode.setNext(indexNode); + indexNode.setPre(newNode); + } + private void checkIndex(int index){ + if(index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + private Node getNode(int index){ + checkIndex(index); + // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 + Node result=null; + if(index==0){ + return head; + } + if(index==size-1){ + return tail; + } + for (int i = 0; i < index; i++) { + result = head.getNext(); + } + return result; + } + public Object get(int index){ + return getNode(index).getData(); + } + public Object remove(int index){ + checkIndex(index); + Node indexNode = getNode(index); + Node pre = indexNode.getPre(); + Node next = indexNode.getNext(); + if(pre!=null){ + pre.setNext(next); + }else{ + head = next; + } + if(next!=null){ + next.setPre(pre); + }else{ + tail = pre; + } + return indexNode.getData(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o); + head.setPre(newNode); + newNode.setNext(head); + head = newNode; + } + public void addLast(Object o){ + Node newNode = new Node(o); + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + public Object removeFirst(){ + Node next = head.getNext(); + Node oldHead = head; + head = next; + head.setPre(null); + return oldHead; + } + public Object removeLast(){ + Node oldTail = tail; + Node pre = tail.getPre(); + tail = pre; + tail.setNext(null); + return oldTail; + } + public Iterator iterator(){ + + return null; + } + + /** + * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 + */ + private static class Node{ + + Object data; + Node next; + Node pre; + public Node(){ + + } + public Node(Object data){ + this.data = data; + } + 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; + } + + public Node getPre() { + return pre; + } + + public void setPre(Node pre) { + this.pre = pre; + } + } + + /** + * 把该链表逆置 + * 例如链表为 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/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java new file mode 100644 index 0000000000..d6c9e95cb0 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.wdn.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/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java new file mode 100644 index 0000000000..59992f0cbd --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java @@ -0,0 +1,24 @@ +package com.github.wdn.coding2017.basic; + +/** + * 队列使用链表实现比较简单理论上是无界队列 + * 如果使用数组需要处理很多问题比如长度限制,元素的复制 + */ +public class Queue { + private LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.remove(0); + } + + public boolean isEmpty(){ + return queue.size()==0; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java new file mode 100644 index 0000000000..f0a0fe56e9 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java @@ -0,0 +1,27 @@ +package com.github.wdn.coding2017.basic; + +/** + * 使用list实现比较简单 + * 可以考虑使用其它结构 + */ +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(); + } +} From 43aece6c0f38ed06904695de001272eb3e90aeb6 Mon Sep 17 00:00:00 2001 From: xiaozi Date: Sun, 12 Mar 2017 20:08:46 +0800 Subject: [PATCH 19/26] This is the first week's work. --- group24/1054283210/.classpath | 1 + .../xiaozi123/coding2017/basic/ArrayList.java | 103 +++++++++++ .../coding2017/basic/BinaryTreeNode.java | 72 ++++++++ .../xiaozi123/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 163 ++++++++++++++++++ .../xiaozi123/coding2017/basic/List.java | 9 + .../xiaozi123/coding2017/basic/Queue.java | 46 +++++ .../xiaozi123/coding2017/basic/Stack.java | 51 ++++++ ...7\347\253\240\345\234\260\345\235\200.txt" | 1 + 9 files changed, 453 insertions(+) create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/ArrayList.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Iterator.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/LinkedList.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/List.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Queue.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Stack.java create mode 100644 "group24/1054283210/src/com/github/xiaozi123/coding2017/basic/\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git a/group24/1054283210/.classpath b/group24/1054283210/.classpath index d171cd4c12..2d7497573f 100644 --- a/group24/1054283210/.classpath +++ b/group24/1054283210/.classpath @@ -2,5 +2,6 @@ + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/ArrayList.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..891c355406 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/ArrayList.java @@ -0,0 +1,103 @@ +package com.github.xiaozi123.coding2017.basic; + +import java.util.Arrays; + +import org.omg.CORBA.PUBLIC_MEMBER; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size>=elementData.length){//���û���ڴ� ��ȡ�����ڴ� + elementData=Arrays.copyOf(elementData,size+1); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if(index<0||index>=elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBound"); + } + + if(size>=elementData.length){//���û���ڴ� ��ȡ�����ڴ� + elementData=Arrays.copyOf(elementData, size+1); + } + System.arraycopy(elementData, index, elementData, index+1, elementData.length-index-1); + elementData[index]=o; + size++; + } + + public Object get(int index){ + if(index<0||index>=elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBound"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBound"); + } + Object temp=elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, elementData.length-index-1); + size--; + return temp; + } + + /* + * (non-Javadoc)��ȡĿǰ���� + * @see com.github.xiaozi123.coding2017.basic.List#size() + */ + public int size(){ + if(size>=elementData.length){//���û���ڴ� ��ȡ�����ڴ� + elementData=Arrays.copyOf(elementData,size+1); + } + return size; + } + + // next() hasnext()���� + public Iterator iterator(){ + return new Iterator(){ + private int index = 0; + public Object next(){ + return elementData[index++]; + } + public boolean hasNext(){ + return index >= size; + } + }; + } + + public static void main(String[] args) { + ArrayList arrayList=new ArrayList(); + + arrayList.add(0); + arrayList.add(1); + arrayList.add(2); + System.out.println("���ݸ���Ϊ3��"+(arrayList.size==3)); + + System.out.print("����Ӧ��Ϊ0,1,2: "); + for (int i = 0; i < arrayList.size(); i++) { + System.out.print(arrayList.get(i)+" "); + } + System.out.println(); + + arrayList.add(1,1); + System.out.print("����Ӧ��Ϊ��0,1,1,2: "); + for (int i = 0; i < arrayList.size(); i++) { + System.out.print(arrayList.get(i)+" "); + } + System.out.println(); + + System.out.print("����Ӧ��Ϊ0: "); + System.out.println(arrayList.remove(0)); + + + } + + +} + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/BinaryTreeNode.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a86b1b15bd --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package com.github.xiaozi123.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if (data==null) { + this.setData(0); + } + if ((Integer)o<=(Integer)data) { + if (left==null) { + left=new BinaryTreeNode(); + left.setData(o); + return left; + } + return left.insert(o); + }else{ + if (right==null) { + right=new BinaryTreeNode(); + right.setData(o); + return right; + } + return right.insert(o); + + } + } + @Override + public String toString() { + // TODO Auto-generated method stub + return data+" "+left+" "+right; + } + + public static void main(String[] args) { + BinaryTreeNode binaryTreeNode=new BinaryTreeNode(); + for (int i = 0; i < 5; i++) { + binaryTreeNode.insert(i); + } + System.out.println(binaryTreeNode); + } + +// 0 +// 0 1 +// null null null 2 +// null 3 +// null 4 +// null null + + + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Iterator.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..35c6b39017 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.xiaozi123.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/LinkedList.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..66ab1e0300 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/LinkedList.java @@ -0,0 +1,163 @@ +package com.github.xiaozi123.coding2017.basic; + +import java.util.NoSuchElementException; + +import jdk.nashorn.internal.ir.IndexNode; + +public class LinkedList implements List { + + private Node head; + private int size; + + private Node node(Object o) { + Node now=new Node(); + now.data=o; + now.next=null; + size++; + return now; + } + + public void add(Object o){ + if (head==null) { + head=node(o); + } + else { + addLast(o); + } + + } + public void add(int index , Object o){ + if (index<0||index>size) { + throw new IndexOutOfBoundsException("OutOfBound"); + }else if (index==0) { + addFirst(o); + }else if (index==size+1) { + addLast(o); + }else{ + Node beforeNode=head; + for (int i = 0; i < index-1; i++) { + beforeNode=beforeNode.next; + } + Node addNode=node(o); + addNode.next=beforeNode.next; + beforeNode.next=addNode; + } + } + + + public Object get(int index){ + if (index<0||index>size) { + throw new IndexOutOfBoundsException("OutOfBound"); + } else { + Node indexNode=head; + for (int i = 0; i < index; i++) { + indexNode=indexNode.next; + } + return indexNode.data; + } + + } + public Object remove(int index){ + if (index<0||index>size) { + throw new IndexOutOfBoundsException("OutOfBound"); + }else if(index==0){ + return removeFirst(); + }else if(index==size){ + return removeLast(); + + }else{ + Node beforeNode=head; + for (int i = 0; i < index-1; i++) { + beforeNode=beforeNode.next; + } + + Node indexNode=head; + for (int i = 0; i < index; i++) { + indexNode=indexNode.next; + } + beforeNode.next=indexNode.next; + indexNode.next=null; + size--; + return indexNode.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node headNode=node(o); + headNode.data=o; + headNode.next=head.next; + head=headNode; + + } + + public void addLast(Object o){ + Node tailNode=head; + while(tailNode.next!=null){ + tailNode=tailNode.next; + } + Node lastNode=node(o); + tailNode.next=lastNode; + } + public Object removeFirst(){ + if (head==null) { + throw new NoSuchElementException(); + } + Object temp= head.data; + head=head.next; + size--; + return temp; + } + public Object removeLast(){ + if (head==null) { + throw new NoSuchElementException(); + } + Node newNode=head; + while(newNode.next.next!=null){ + newNode=newNode.next; + } + Node lastNode=newNode.next; + newNode.next=null; + size--; + return lastNode.data; + + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + public static void main(String[] args) { + LinkedList linkedList=new LinkedList(); + + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + + System.out.println("���ָ���Ϊ3��"+linkedList.size()); + + System.out.println("��ȡ������Ϊ1��"+linkedList.get(0)); + System.out.println("��ȡ������Ϊ2��"+linkedList.get(1)); + System.out.println("��ȡ������Ϊ3��"+linkedList.get(2)); + // add get remove size + + System.out.println("*************"); + + System.out.println(linkedList.remove(0));//1 + System.out.println("��ȡ������Ϊ2��"+linkedList.get(0)); + System.out.println("��ȡ������Ϊ3��"+linkedList.get(1)); + System.out.println("���ָ���Ϊ2��"+linkedList.size()); + + } + +} + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/List.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/List.java new file mode 100644 index 0000000000..950ce93269 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.xiaozi123.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/1054283210/src/com/github/xiaozi123/coding2017/basic/Queue.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Queue.java new file mode 100644 index 0000000000..75b122a253 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Queue.java @@ -0,0 +1,46 @@ +package com.github.xiaozi123.coding2017.basic; + +public class Queue { + private LinkedList elementData=new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + Queue queue=new Queue(); + if (queue.isEmpty()) { + System.out.println("���������ǿա�"); + } + + int n=3; + for (int i = 0; i < n; i++) { + queue.enQueue(i); + } + System.out.println("����������"+queue.size()+"����"); + System.out.print("����Ӧ���ǣ�0---"); + System.out.println(queue.deQueue()); + System.out.print("����ڶ�����Ӧ���ǣ�1---"); + System.out.println(queue.deQueue()); + System.out.print("����ڶ�����Ӧ���ǣ�2---"); + System.out.println(queue.deQueue()); + + + + + } + + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Stack.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Stack.java new file mode 100644 index 0000000000..d9994f5648 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/Stack.java @@ -0,0 +1,51 @@ +package com.github.xiaozi123.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + /* + * ��ջ + */ + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + Stack stack=new Stack(); + if (stack.isEmpty()) { + System.out.println("stackΪ�ա�"); + } + int n=3; + for (int i = 0; i < n; i++) { + stack.push(i); + } + System.out.println("stack���ڳߴ�Ӧ��Ϊ3:"+(stack.size()==3)); + System.out.println("stack��������Ӧ��Ϊ��2,1,0."); + for (int i = 0; i < n; i++) { + System.out.println(stack.pop()); + } +// for (int i = 0; i < n; i++) { +// stack.peek(); +// } +// + + + + } + + +} diff --git "a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..3977d579a8 --- /dev/null +++ "b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +http://www.jianshu.com/p/6688791d16c0 \ No newline at end of file From 6c28d0bd450d2394c8ea927fee2c4ee555c0eb52 Mon Sep 17 00:00:00 2001 From: chishiwu <1107225491@qq.com> Date: Sun, 12 Mar 2017 20:37:30 +0800 Subject: [PATCH 20/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除无关的内容 --- group24/1107225491/1107225491Learning/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group24/1107225491/1107225491Learning/test.txt diff --git a/group24/1107225491/1107225491Learning/test.txt b/group24/1107225491/1107225491Learning/test.txt new file mode 100644 index 0000000000..2086f8809c --- /dev/null +++ b/group24/1107225491/1107225491Learning/test.txt @@ -0,0 +1 @@ +�ܶ��ܶ� \ No newline at end of file From 87eab63a90471f48a41c142649a578f603a45c07 Mon Sep 17 00:00:00 2001 From: xiaozi Date: Sun, 12 Mar 2017 20:44:08 +0800 Subject: [PATCH 21/26] add --- .../src/com/github/xiaozi123/coding2017/basic/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore @@ -0,0 +1 @@ +/bin/ From 3cc4c758b251120daf89975304997fa6816ff7df Mon Sep 17 00:00:00 2001 From: chishiwu <1107225491@qq.com> Date: Sun, 12 Mar 2017 20:52:58 +0800 Subject: [PATCH 22/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=EF=BC=88=E9=83=A8=E5=88=86=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 尝试提交 --- group24/1107225491/1107225491Learning/test.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group24/1107225491/1107225491Learning/test.txt diff --git a/group24/1107225491/1107225491Learning/test.txt b/group24/1107225491/1107225491Learning/test.txt deleted file mode 100644 index 2086f8809c..0000000000 --- a/group24/1107225491/1107225491Learning/test.txt +++ /dev/null @@ -1 +0,0 @@ -�ܶ��ܶ� \ No newline at end of file From 2d87167e4a56def4f78f0556cabc2ebe1b9c8d2a Mon Sep 17 00:00:00 2001 From: xiaozi Date: Sun, 12 Mar 2017 20:54:01 +0800 Subject: [PATCH 23/26] This is my first week's work. --- .../xiaozi123/coding2017/basic/.gitignore | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore index ae3c172604..2c93a035dc 100644 --- a/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/basic/.gitignore @@ -1 +1,27 @@ -/bin/ +*.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.* + +# Idea +*.iml +*.ipr +*.iws +.idea + +target From 33f6171d07fc2a22f815e443e0a79acce066ce37 Mon Sep 17 00:00:00 2001 From: sargeles <330657387@qq.com> Date: Sun, 12 Mar 2017 23:04:16 +0800 Subject: [PATCH 24/26] =?UTF-8?q?=E5=8F=AA=E5=86=99=E4=BA=86linkedlist?= =?UTF-8?q?=E5=92=8Carraylist=E7=9A=84=E7=AE=80=E5=8D=95=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=EF=BC=8C=E8=BF=99=E5=91=A8=E5=AE=9E=E5=9C=A8=E5=A4=AA=E5=BF=99?= =?UTF-8?q?=EF=BC=8C=E8=A6=81=E4=B8=8A=E7=BA=BFT=20T?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/330657387/.classpath | 7 + group24/330657387/.gitignore | 1 + group24/330657387/.project | 17 +++ .../org.eclipse.core.resources.prefs | 2 + .../src/com/coding/basic/ArrayList.java | 62 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 122 ++++++++++++++++++ .../330657387/src/com/coding/basic/List.java | 9 ++ .../330657387/src/com/coding/basic/Queue.java | 19 +++ .../330657387/src/com/coding/basic/Stack.java | 22 ++++ 11 files changed, 300 insertions(+) create mode 100644 group24/330657387/.classpath create mode 100644 group24/330657387/.gitignore create mode 100644 group24/330657387/.project create mode 100644 group24/330657387/.settings/org.eclipse.core.resources.prefs create mode 100644 group24/330657387/src/com/coding/basic/ArrayList.java create mode 100644 group24/330657387/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group24/330657387/src/com/coding/basic/Iterator.java create mode 100644 group24/330657387/src/com/coding/basic/LinkedList.java create mode 100644 group24/330657387/src/com/coding/basic/List.java create mode 100644 group24/330657387/src/com/coding/basic/Queue.java create mode 100644 group24/330657387/src/com/coding/basic/Stack.java diff --git a/group24/330657387/.classpath b/group24/330657387/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group24/330657387/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group24/330657387/.gitignore b/group24/330657387/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/330657387/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/330657387/.project b/group24/330657387/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group24/330657387/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group24/330657387/.settings/org.eclipse.core.resources.prefs b/group24/330657387/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..a77fc6aa89 --- /dev/null +++ b/group24/330657387/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/com/coding/basic/LinkedList.java=UTF-8 diff --git a/group24/330657387/src/com/coding/basic/ArrayList.java b/group24/330657387/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1728a28291 --- /dev/null +++ b/group24/330657387/src/com/coding/basic/ArrayList.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void ensureCapacity(int input) { + if (input > elementData.length) { + growCapacity(); + } + } + + private void growCapacity(){ + elementData = Arrays.copyOf(elementData, size * 2); + } + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + + + public void add(int index, Object o){ + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData,index, elementData, index + 1, size - index); + elementData[index] = o; + size ++; + } + + private void rangeCheck(int index){ + if (index > size || index < 0){ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object dest = elementData[index]; + System.arraycopy(elementData, index +1, elementData, index, size-index-1); + size --; + return dest; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java b/group24/330657387/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group24/330657387/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group24/330657387/src/com/coding/basic/Iterator.java b/group24/330657387/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group24/330657387/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/group24/330657387/src/com/coding/basic/LinkedList.java b/group24/330657387/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..8357e13254 --- /dev/null +++ b/group24/330657387/src/com/coding/basic/LinkedList.java @@ -0,0 +1,122 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + if (isEmpty()) { + head = new Node(o); + } else { + Node tail = (Node)get(size-1); + Node node = new Node(o); + tail.next = node; + } + size++; + } + + public boolean isEmpty() { + return (size == 0) ? true : false; + } + + public void add(int index, Object o) { + rangeCheck(index); + if (index ==0) { + Node node = new Node(o); + node.next = head; + head = node; + } else { + Node pre = (Node)get(index-1); + Node node = new Node(o); + node.next = pre.next; + pre.next = node; + } + } + + private void rangeCheck(int index){ + if (index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index) { + rangeCheck(index); + Node dest = head; + for (int i = 0; i< index; i++){ + dest = dest.next; + } + return dest.data; + } + + public Object remove(int index) { + rangeCheck(index); + Node pre = (Node)get(index); + Node dest = pre.next; + pre.next = dest.next; + size --; + return dest; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + node.next = head; + head = node; + size ++; + } + + public void addLast(Object o) { + Node last = (Node)get(size-1); + Node node = new Node(o); + last.next = node; + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node newhead = head; + head = head.next; + size --; + return newhead; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + if (head.next == null) { + Node tmp = head; + head = null; + size --; + return tmp; + } + Node newLastNode = (Node)get(size-2); + Node oldLastNode = newLastNode.next; + newLastNode.next = null; + size --; + return oldLastNode; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + Node(Object data) { + this.data = data; + next = null; + } + } + +} diff --git a/group24/330657387/src/com/coding/basic/List.java b/group24/330657387/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group24/330657387/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/group24/330657387/src/com/coding/basic/Queue.java b/group24/330657387/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group24/330657387/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group24/330657387/src/com/coding/basic/Stack.java b/group24/330657387/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group24/330657387/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From 59d8962cbc6421e7502d93bfc4ac2dfc805db278 Mon Sep 17 00:00:00 2001 From: sargeles <330657387@qq.com> Date: Sun, 12 Mar 2017 23:12:37 +0800 Subject: [PATCH 25/26] =?UTF-8?q?=E4=BF=AE=E6=94=B9Linkedlist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/330657387/.gitignore | 4 ++++ group24/330657387/.settings/org.eclipse.core.resources.prefs | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 group24/330657387/.settings/org.eclipse.core.resources.prefs diff --git a/group24/330657387/.gitignore b/group24/330657387/.gitignore index ae3c172604..e2e6235a8a 100644 --- a/group24/330657387/.gitignore +++ b/group24/330657387/.gitignore @@ -1 +1,5 @@ /bin/ + +#ide config +.settings/ + diff --git a/group24/330657387/.settings/org.eclipse.core.resources.prefs b/group24/330657387/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index a77fc6aa89..0000000000 --- a/group24/330657387/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/com/coding/basic/LinkedList.java=UTF-8 From ebda0625eed1c6f8a58cc383946e2caf78c2022b Mon Sep 17 00:00:00 2001 From: liangduoduo666666 <798277403@qq.com> Date: Sun, 12 Mar 2017 23:48:16 +0800 Subject: [PATCH 26/26] =?UTF-8?q?zl=E7=9A=84=E7=AC=AC=E4=B8=80=E5=91=A8?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/798277403/src/week1/ArrayList.java | 147 ++++++++++++++++ .../798277403/src/week1/ArrayListTest.java | 55 ++++++ group24/798277403/src/week1/BinaryTree.java | 124 ++++++++++++++ .../798277403/src/week1/BinaryTreeNode.java | 72 ++++++++ group24/798277403/src/week1/Iterator.java | 10 ++ group24/798277403/src/week1/LinkedList.java | 161 ++++++++++++++++++ .../798277403/src/week1/LinkedListTest.java | 53 ++++++ group24/798277403/src/week1/List.java | 13 ++ group24/798277403/src/week1/Queue.java | 33 ++++ group24/798277403/src/week1/QueueTest.java | 36 ++++ group24/798277403/src/week1/Stack.java | 34 ++++ group24/798277403/src/week1/StackTest.java | 39 +++++ 12 files changed, 777 insertions(+) create mode 100644 group24/798277403/src/week1/ArrayList.java create mode 100644 group24/798277403/src/week1/ArrayListTest.java create mode 100644 group24/798277403/src/week1/BinaryTree.java create mode 100644 group24/798277403/src/week1/BinaryTreeNode.java create mode 100644 group24/798277403/src/week1/Iterator.java create mode 100644 group24/798277403/src/week1/LinkedList.java create mode 100644 group24/798277403/src/week1/LinkedListTest.java create mode 100644 group24/798277403/src/week1/List.java create mode 100644 group24/798277403/src/week1/Queue.java create mode 100644 group24/798277403/src/week1/QueueTest.java create mode 100644 group24/798277403/src/week1/Stack.java create mode 100644 group24/798277403/src/week1/StackTest.java diff --git a/group24/798277403/src/week1/ArrayList.java b/group24/798277403/src/week1/ArrayList.java new file mode 100644 index 0000000000..03257803df --- /dev/null +++ b/group24/798277403/src/week1/ArrayList.java @@ -0,0 +1,147 @@ +package week1; + +import java.util.Arrays; + +/** + java泛型: + ? 表示不确定的java类型。 + T 表示java类型。 + K V 分别代表java键值中的Key Value。 + E 代表Element。 + + * 自己实现的ArrayList + * Created by zhouliang on 2017-03-10. + */ +class ArrayList implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int size){ + this.elementData = new Object[size]; + } + + //默认大小为10 + public ArrayList(){ + this.elementData = new Object[10]; + } + + //判断是否需要扩展容量 ((旧容量 * 3) / 2) + 1 + private void checkcapacity(int index){ + if(index>elementData.length){ + int length = (elementData.length*3)/2+1; + /* + Object[] temp = new Object[length]; + for(int i=0; i= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private ArrayList list = null; + private int position = 0; + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public E next() { + return list.get(position++); + } + } +} diff --git a/group24/798277403/src/week1/ArrayListTest.java b/group24/798277403/src/week1/ArrayListTest.java new file mode 100644 index 0000000000..98e30a222a --- /dev/null +++ b/group24/798277403/src/week1/ArrayListTest.java @@ -0,0 +1,55 @@ +package week1; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp(){ + for(int i=0; i<100; i++){ + arrayList.add(i); + } + } + + @Test + public void add() throws Exception { + for(int i=100; i<1000; i++){ + arrayList.add(i); + } + Assert.assertEquals(1000,arrayList.size()); + } + + @Test + public void add1() throws Exception { + java.util.LinkedList l = new java.util.LinkedList(); + } + + @Test + public void get() throws Exception { + System.out.println(arrayList.get(99)); + } + + @Test + public void remove() throws Exception { + System.out.println(arrayList.size()); + arrayList.remove(arrayList.size()-1); + System.out.println(arrayList.size()); + //Assert.assertEquals((Integer)99,(Integer)arrayList.size()); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git a/group24/798277403/src/week1/BinaryTree.java b/group24/798277403/src/week1/BinaryTree.java new file mode 100644 index 0000000000..3c480dc012 --- /dev/null +++ b/group24/798277403/src/week1/BinaryTree.java @@ -0,0 +1,124 @@ +package week1; + +/** + * Created by zhouliang on 2017-03-11. + */ +class BinaryTree { + private TreeNode root; + + class TreeNode { + int val = 0; + TreeNode left = null; + TreeNode right = null; + public TreeNode(int val) { + this.val = val; + } + } + + /** + * 递归创建二叉树 + * @param node + * @param data + */ + public void buildTree(TreeNode node,int data){ + if(root == null){ + root = new TreeNode(data); + }else{ + if(data < node.val){ + if(node.left == null){ + node.left = new TreeNode(data); + }else{ + buildTree(node.left,data); + } + }else{ + if(node.right == null){ + node.right = new TreeNode(data); + }else{ + buildTree(node.right,data); + } + } + } + } + + /** + * 前序遍历 + * @param node + */ + public void preOrder(TreeNode node){ + if(node != null){ + System.out.println(node.val); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * @param node + */ + public void inOrder(TreeNode node){ + if(node != null){ + inOrder(node.left); + System.out.println(node.val); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * @param node 一般是传入根节点 + */ + public void postOrder(TreeNode node){ + if(node != null){ + postOrder(node.left); + postOrder(node.right); + System.out.println(node.val); + } + } + + + /** + * 分层打印 + * @param root 树的根节点 + * @return 分层后的数组 + */ + public int[][] printTree(TreeNode root) { + if(root == null){ + return null; + } + java.util.LinkedList queue = new java.util.LinkedList(); + TreeNode last = root; + TreeNode nlast = null ; + TreeNode currentNode = null; + java.util.ArrayList> lists = new java.util.ArrayList>(); + java.util.ArrayList list = new java.util.ArrayList(); + queue.add(last); + while(!queue.isEmpty()){ + currentNode = queue.poll(); + list.add(currentNode.val); + + if(currentNode.left!=null){ + queue.add(currentNode.left); + nlast = currentNode.left; + } + if(currentNode.right!=null){ + queue.add(currentNode.right); + nlast = currentNode.right; + } + if(currentNode == last){ + lists.add(list); + last = nlast; + list = new java.util.ArrayList(); + } + } + + int[][] result = new int[lists.size()][]; + for(int i = 0 ; i < lists.size() ; i++){ + result[i] = new int[lists.get(i).size()]; + for(int j = 0 ; j < lists.get(i).size() ; j++){ + result[i][j] = lists.get(i).get(j); + } + } + return result; + } +} diff --git a/group24/798277403/src/week1/BinaryTreeNode.java b/group24/798277403/src/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..e680d5ed15 --- /dev/null +++ b/group24/798277403/src/week1/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package week1; + +/** + * 自己实现的BinaryTreeNode + * Created by zhouliang on 2017-03-10. + */ +class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(data==null && left==null && right==null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + }else{ + BinaryTreeNode temp = this; + BinaryTreeNode node = new BinaryTreeNode(); + while(true){ + if((Integer)o > (Integer)temp.getData()){ + if(temp.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setRight(node); + return this; + }else{ + temp = temp.getRight(); + } + }else{ + if(temp.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + temp.setLeft(node); + return this; + }else{ + temp = temp.getLeft(); + } + } + } + } + } +} diff --git a/group24/798277403/src/week1/Iterator.java b/group24/798277403/src/week1/Iterator.java new file mode 100644 index 0000000000..73ba87c125 --- /dev/null +++ b/group24/798277403/src/week1/Iterator.java @@ -0,0 +1,10 @@ +package week1; + +/** + * 自己实现的Iterator + * Created by zhouliang on 2017-03-10. + */ +interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group24/798277403/src/week1/LinkedList.java b/group24/798277403/src/week1/LinkedList.java new file mode 100644 index 0000000000..d0933cfcb1 --- /dev/null +++ b/group24/798277403/src/week1/LinkedList.java @@ -0,0 +1,161 @@ +package week1; + +/** + * 自己实现的LinkedList + * Created by zhouliang on 2017-03-10. + */ +class LinkedList implements List { + + private int size; + private Node first; + private Node last; + + public LinkedList(){ + } + + @Override + public void add(E e) { + Node temp = new Node(e); + if(first != null){ + last.next = temp; + last = temp; + }else{ + first = temp; + last = temp; + } + size++; + } + + /** + * 指定下标添加元素 + * @param index 可以在链表末尾加,就是可以的等于size,不能大于size + * @param e 代表Element + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + + Node temp = new Node(e); + if(index == size){ + last.next = temp; + last = temp; + }else{ + Node begin = first; + index--; + while(index>0){ + begin = begin.next; + index--; + } + Node next = begin.next; + begin.next = temp; + temp.next = next; + } + size++; + } + + @Override + public E get(int index) { + checkElementIndex(index); + + Node temp = first; + while(index>0){ + temp = temp.next; + index--; + } + return temp.value; + } + + @Override + public E remove(int index) { + checkElementIndex(index); + + Node temp; + if(index == 0){ + temp = first; + first = first.next; + size--; + return temp.value; + }else{ + temp = first; + index--; + //找到要删除节点的前一个节点 + while(index>0){ + temp = temp.next; + index--; + } + Node removeNode = temp.next; + temp.next = removeNode.next; + size--; + return removeNode.value; + + } + + } + + public E removeLast(){ + return remove(size-1); + } + + public void addFirst(E e){ + Node temp = new Node(e); + if(first == null){ + first = temp; + last = temp; + }else{ + temp.next = first; + first = temp; + } + size++; + } + + public void addLast(E e){ + Node temp = new Node(); + if(first == null){ + first = temp; + last = temp; + }else{ + last.next = temp; + last = temp; + } + size++; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + @Override + public int size() { + return size; + } + + private static class Node{ + E value; + Node next; + + Node(){ + + } + + Node(E e){ + this.value = e; + } + } +} diff --git a/group24/798277403/src/week1/LinkedListTest.java b/group24/798277403/src/week1/LinkedListTest.java new file mode 100644 index 0000000000..9a8a9d936d --- /dev/null +++ b/group24/798277403/src/week1/LinkedListTest.java @@ -0,0 +1,53 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class LinkedListTest { + + private LinkedList myLinkedList = new LinkedList<>(); + + private java.util.LinkedList systemLinkedList = new java.util.LinkedList<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + myLinkedList.add(i); + systemLinkedList.add(i); + } + } + @Test + public void add() throws Exception { + for(int i=0; i<10; i++){ + System.out.println(myLinkedList.get(i)); + } + } + + @Test + public void remove() throws Exception { + myLinkedList.remove(5); + for(int i=0; i { + void add(E e); + void add(int index, E e); + E get(int index); + E remove(int index); + int size(); +} diff --git a/group24/798277403/src/week1/Queue.java b/group24/798277403/src/week1/Queue.java new file mode 100644 index 0000000000..184f3a5336 --- /dev/null +++ b/group24/798277403/src/week1/Queue.java @@ -0,0 +1,33 @@ +package week1; + +/** + * 自己实现的Queue,用自己的LinkedList实现 + * Created by zhouliang on 2017-03-10. + */ +class Queue { + + private LinkedList linkedList; + + public Queue(){ + this.linkedList = new LinkedList(); + } + /** + * 从队列头部添加元素 + * @param e 代表Element + */ + public void enQueue(E e){ + linkedList.addFirst(e); + } + + public E deQueue(){ + return linkedList.removeLast(); + } + + public boolean isEmpty(){ + return linkedList.size() > 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group24/798277403/src/week1/QueueTest.java b/group24/798277403/src/week1/QueueTest.java new file mode 100644 index 0000000000..b5a333f64d --- /dev/null +++ b/group24/798277403/src/week1/QueueTest.java @@ -0,0 +1,36 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * 自己实现的队列,先进先出 + * Created by zhouliang on 2017-03-10. + */ +public class QueueTest { + private Queue queue = new Queue<>(); + + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + queue.enQueue(i); + } + } + + @Test + public void enQueue() throws Exception { + System.out.println(queue.size()); + } + + @Test + public void deQueue() throws Exception { + System.out.println(queue.deQueue()+" "+queue.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file diff --git a/group24/798277403/src/week1/Stack.java b/group24/798277403/src/week1/Stack.java new file mode 100644 index 0000000000..f85aa2ada5 --- /dev/null +++ b/group24/798277403/src/week1/Stack.java @@ -0,0 +1,34 @@ +package week1; + +/** + * 自己实现的Stack + * Created by zhouliang on 2017-03-10. + */ +class Stack { + + private ArrayList elementData; + + public Stack(){ + this.elementData = new ArrayList(); + } + + public void push(E e){ + elementData.add(e); + } + + public E pop(){ + return elementData.remove(elementData.size()-1); + } + + public E peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() > 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group24/798277403/src/week1/StackTest.java b/group24/798277403/src/week1/StackTest.java new file mode 100644 index 0000000000..f4213d66d3 --- /dev/null +++ b/group24/798277403/src/week1/StackTest.java @@ -0,0 +1,39 @@ +package week1; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zhouliang on 2017-03-10. + */ +public class StackTest { + private Stack stack = new Stack<>(); + + @Before + public void setUp(){ + for(int i=0; i<10; i++){ + stack.push(i); + } + } + + @Test + public void pop() throws Exception { + System.out.println("size "+stack.size()); + while(stack.size()>0){ + System.out.println(stack.pop()); + } + } + + @Test + public void peek() throws Exception { + System.out.println(stack.size()); + int i = stack.peek(); + System.out.println(i+" "+stack.size()); + } + + @Test + public void isEmpty() throws Exception { + + } + +} \ No newline at end of file