From 64878674b8fa3ebdc1dc74353121f3d6c8c27b83 Mon Sep 17 00:00:00 2001 From: longcloud Date: Fri, 24 Feb 2017 23:24:00 +0800 Subject: [PATCH 1/8] Commit the ArrayList --- group03/619224754/.classpath | 7 ++ group03/619224754/.gitignore | 1 + group03/619224754/.project | 17 +++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ group03/619224754/src/Main/Main.java | 5 ++ .../src/com/coding/basic/ArrayList.java | 75 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 ++++++++ .../src/com/coding/basic/Iterator.java | 7 ++ .../src/com/coding/basic/LinkedList.java | 46 ++++++++++++ .../619224754/src/com/coding/basic/List.java | 9 +++ .../619224754/src/com/coding/basic/Queue.java | 19 +++++ .../619224754/src/com/coding/basic/Stack.java | 22 ++++++ group03/619224754/src/test/ArrayListTest.java | 62 +++++++++++++++ 13 files changed, 313 insertions(+) create mode 100644 group03/619224754/.classpath create mode 100644 group03/619224754/.gitignore create mode 100644 group03/619224754/.project create mode 100644 group03/619224754/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/619224754/src/Main/Main.java create mode 100644 group03/619224754/src/com/coding/basic/ArrayList.java create mode 100644 group03/619224754/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/619224754/src/com/coding/basic/Iterator.java create mode 100644 group03/619224754/src/com/coding/basic/LinkedList.java create mode 100644 group03/619224754/src/com/coding/basic/List.java create mode 100644 group03/619224754/src/com/coding/basic/Queue.java create mode 100644 group03/619224754/src/com/coding/basic/Stack.java create mode 100644 group03/619224754/src/test/ArrayListTest.java diff --git a/group03/619224754/.classpath b/group03/619224754/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group03/619224754/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group03/619224754/.gitignore b/group03/619224754/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/619224754/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/619224754/.project b/group03/619224754/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group03/619224754/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/619224754/.settings/org.eclipse.jdt.core.prefs b/group03/619224754/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/619224754/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group03/619224754/src/Main/Main.java b/group03/619224754/src/Main/Main.java new file mode 100644 index 0000000000..2419e8fa45 --- /dev/null +++ b/group03/619224754/src/Main/Main.java @@ -0,0 +1,5 @@ +package Main; + +public class Main { + +} diff --git a/group03/619224754/src/com/coding/basic/ArrayList.java b/group03/619224754/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..05fc412f93 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if(elementData.length == size) { + Object[] arrTaget = new Object[size * 2]; + System.arraycopy(elementData, 0, arrTaget, 0, size); + this.elementData = arrTaget; + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + Object[] arrTarget = new Object[size - index]; + System.arraycopy(elementData, index, arrTarget, 0, size - index); + elementData[index] = o; + System.arraycopy(arrTarget, 0, elementData, index + 1, size - index); + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object retObj = elementData[index]; + + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + else if(index == size) { + elementData[index] = null; + } + else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + + size--; + return retObj; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int cursor = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.cursor != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[this.cursor++]; + } + } + +} diff --git a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/Iterator.java b/group03/619224754/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/LinkedList.java b/group03/619224754/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group03/619224754/src/com/coding/basic/List.java b/group03/619224754/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/Queue.java b/group03/619224754/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/Stack.java b/group03/619224754/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/619224754/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group03/619224754/src/test/ArrayListTest.java b/group03/619224754/src/test/ArrayListTest.java new file mode 100644 index 0000000000..9d9e014379 --- /dev/null +++ b/group03/619224754/src/test/ArrayListTest.java @@ -0,0 +1,62 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + + + +public class ArrayListTest { + + @Test + public void testAdd() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 105, array.size()); + } + + @Test + public void testAddIndex() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + array.add(100, 100); + Assert.assertEquals("Shoule be the same", 100, array.get(100)); + Assert.assertEquals("Shoule be the same", 100, array.get(101)); + } + + @Test + public void testRemove() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 100, array.remove(100)); + Assert.assertEquals("Shoule be the same", 104, array.size()); + } + + @Test + public void testIterator() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + Iterator iterator = array.iterator(); + int j = 0; + while(iterator.hasNext()){ + Assert.assertEquals("Shoule be the same", iterator.next(), array.get(j)); + j++; + } + } + +} From 3ba0f68994bd531f5f189afb26cb917dc23149af Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 11:43:25 +0800 Subject: [PATCH 2/8] A New Branch --- group03/617187912/Learning201702/.classpath | 6 +++ group03/617187912/Learning201702/.gitignore | 1 + group03/617187912/Learning201702/.project | 17 +++++++ .../org.eclipse.core.resources.prefs | 2 + .../src/com/coding/basic/ArrayList.java | 47 +++++++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++++++++++ .../src/com/coding/basic/Iterator.java | 7 +++ .../src/com/coding/basic/LinkedList.java | 46 ++++++++++++++++++ .../src/com/coding/basic/List.java | 9 ++++ .../src/com/coding/basic/Queue.java | 19 ++++++++ .../src/com/coding/basic/Stack.java | 22 +++++++++ 11 files changed, 208 insertions(+) create mode 100644 group03/617187912/Learning201702/.classpath create mode 100644 group03/617187912/Learning201702/.gitignore create mode 100644 group03/617187912/Learning201702/.project create mode 100644 group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Iterator.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/List.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Queue.java create mode 100644 group03/617187912/Learning201702/src/com/coding/basic/Stack.java diff --git a/group03/617187912/Learning201702/.classpath b/group03/617187912/Learning201702/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group03/617187912/Learning201702/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning201702/.gitignore b/group03/617187912/Learning201702/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning201702/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning201702/.project b/group03/617187912/Learning201702/.project new file mode 100644 index 0000000000..b3dfe82232 --- /dev/null +++ b/group03/617187912/Learning201702/.project @@ -0,0 +1,17 @@ + + + 617187912Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..517f614f71 --- /dev/null +++ b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size] = o; + size+=1; + } + public void add(int index, Object o){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + System.arraycopy(elementData, index, + elementData, index+1, size-index); + elementData[index]=o; + size+=1; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + System.arraycopy(elementData, index+1, + elementData, index, size-index); + size-=1; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} \ No newline at end of file diff --git a/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Iterator.java b/group03/617187912/Learning201702/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group03/617187912/Learning201702/src/com/coding/basic/List.java b/group03/617187912/Learning201702/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Queue.java b/group03/617187912/Learning201702/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Stack.java b/group03/617187912/Learning201702/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/617187912/Learning201702/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 07c1290a0d5024f49898120c69117fcc3f988953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=9D=BF?= Date: Sat, 25 Feb 2017 19:07:28 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group03/1360464792/.gitignore | 34 ++++ group03/1360464792/pom.xml | 42 +++++ .../java/rui/study/coding2017/ArrayList.java | 142 ++++++++++++++++ .../java/rui/study/coding2017/BinaryTree.java | 93 +++++++++++ .../rui/study/coding2017/BinaryTreeNode.java | 40 +++++ .../java/rui/study/coding2017/Iterator.java | 8 + .../java/rui/study/coding2017/LinkedList.java | 156 ++++++++++++++++++ .../main/java/rui/study/coding2017/List.java | 9 + .../main/java/rui/study/coding2017/Queue.java | 21 +++ .../main/java/rui/study/coding2017/Stack.java | 24 +++ .../rui/study/coding2017/ArrayListTest.java | 82 +++++++++ .../rui/study/coding2017/BinaryTreeTest.java | 34 ++++ .../rui/study/coding2017/LinkedListTest.java | 138 ++++++++++++++++ .../java/rui/study/coding2017/QueueTest.java | 39 +++++ .../java/rui/study/coding2017/StackTest.java | 54 ++++++ 15 files changed, 916 insertions(+) create mode 100644 group03/1360464792/.gitignore create mode 100644 group03/1360464792/pom.xml create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/List.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Queue.java create mode 100644 group03/1360464792/src/main/java/rui/study/coding2017/Stack.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/ArrayListTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/BinaryTreeTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/LinkedListTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java create mode 100644 group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java diff --git a/group03/1360464792/.gitignore b/group03/1360464792/.gitignore new file mode 100644 index 0000000000..b3c9df97d1 --- /dev/null +++ b/group03/1360464792/.gitignore @@ -0,0 +1,34 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### maven +target \ No newline at end of file diff --git a/group03/1360464792/pom.xml b/group03/1360464792/pom.xml new file mode 100644 index 0000000000..5fed7b15ab --- /dev/null +++ b/group03/1360464792/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + rui.study + coding2017 + 0.0.1-SNAPSHOT + + 学习数据结构 + + + 1.6 + UTF-8 + + + + + junit + junit + 4.12 + + + + + + coding2017 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${java.version} + ${java.version} + ${encoding} + + + + + \ No newline at end of file diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java new file mode 100644 index 0000000000..4040049ed8 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java @@ -0,0 +1,142 @@ +package rui.study.coding2017; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size; + + private Object[] elementData; + + private static Object[] emptyObjects={}; + + private static int defaultCapacity=10; + + public void add(Object o){ + ensureCapacity(this.size+1); + elementData[size++]=o; + } + + public void add(int index, Object o){ + rangeCheckForAdd(index); + if(elementData[index]!=null){ + ensureCapacity(this.size+1); + //执行数组拷贝 + System.arraycopy(elementData,index,elementData,index+1,size-index); + size++; + } + elementData[index]=o; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object object=elementData[index]; + + int numMoved=size-index-1; + //如果是最后一位remove ,无需进行数组拷贝 + if(numMoved>0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + elementData[--size]=null; + return object; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public ArrayList(){ + this.size=0; + this.elementData=emptyObjects; + } + + public ArrayList(int size){ + this.size=size; + if(size>0){ + this.elementData=new Object[size]; + }else if(size==0){ + this.elementData=emptyObjects; + }else{ + throw new IllegalArgumentException("非法容器大小 "+size); + } + + } + + /** + * 判断索引是否合法 + * @param index 索引 + */ + private void rangeCheckForAdd(int index) { + if(index>size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + /** + * 判断索引是否合法 , + * @param index 索引 + */ + private void rangeCheck(int index) { + if(index>=size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + + /** + * 确保当前数组能够长度能够容纳新的对象,如果不够,就自行增长 + * @param needLength 需要的数组长度 + */ + private void ensureCapacity(int needLength) { + if(elementData==emptyObjects){ + needLength = Math.max(defaultCapacity, needLength); + } + + if(needLength-elementData.length>0){ + this.grow(needLength); + } + } + + /** + * 数组扩容 + * @param needLength 需要的长度 + */ + private void grow(int needLength) { + int elementLength=elementData.length; + //扩容1.5倍 + int newLength=elementLength+(elementLength>>1); + + if(needLength-newLength>0){ + newLength=needLength; + } + this.elementData= Arrays.copyOf(this.elementData,newLength); + } + + private class ArrayListIterator implements Iterator{ + //游标,当前迭代器执行到何处了 + private int cursor=0; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + if (cursor >= size)throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + return elementData[cursor++]; + } + } + + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java new file mode 100644 index 0000000000..7d63153ffb --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java @@ -0,0 +1,93 @@ +package rui.study.coding2017; + +/** + * 二叉树 + * Created by 赵睿 on 2017/2/25. + */ +public class BinaryTree { + private BinaryTreeNode root; + + private int size; + + public void insert(Comparable comparable){ + BinaryTreeNode binaryTreeNode=new BinaryTreeNode(comparable); + + if(this.root==null){ + this.root=binaryTreeNode; + }else { + boolean flag=false; + BinaryTreeNode cursorNode=root; + while(!flag){ + if(comparable.compareTo(cursorNode.getData())<0){ + if(cursorNode.getLeft()==null){ + cursorNode.setLeft(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getLeft(); + } + }else { + if(cursorNode.getRight()==null){ + cursorNode.setRight(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getRight(); + } + } + + } + } + size++; + } + + public LinkedList inorder(){ + LinkedList linkedList=new LinkedList(); + sortLeft(linkedList,root); + sortRight(linkedList,root); + return linkedList; + } + + private void sortRight(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Queue queue=getRightList(binaryTreeNode); + while(!queue.isEmpty()){ + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + + } + + private void sortLeft(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Stack stack=getLeftList(binaryTreeNode); + while(!stack.isEmpty()) { + BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop(); + linkedList.add(stackNode.getData()); + Queue queue = getRightList(stackNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + } + linkedList.add(binaryTreeNode.getData()); + } + + + private Stack getLeftList(BinaryTreeNode binaryTreeNode){ + Stack stack=new Stack(); + while(binaryTreeNode.getLeft()!=null){ + binaryTreeNode=binaryTreeNode.getLeft(); + stack.push(binaryTreeNode); + } + return stack; + } + + private Queue getRightList(BinaryTreeNode binaryTreeNode){ + Queue queue=new Queue(); + while(binaryTreeNode.getRight()!=null){ + binaryTreeNode=binaryTreeNode.getRight(); + queue.enQueue(binaryTreeNode); + } + return queue; + } + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java new file mode 100644 index 0000000000..3895133f17 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java @@ -0,0 +1,40 @@ +package rui.study.coding2017; + +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Comparable getData() { + return data; + } + public void setData(Comparable data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Comparable data) { + this.data = data; + } + + public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java new file mode 100644 index 0000000000..dddde983c6 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java @@ -0,0 +1,8 @@ +package rui.study.coding2017; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java new file mode 100644 index 0000000000..f0a04cd32f --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java @@ -0,0 +1,156 @@ +package rui.study.coding2017; + +/** + * 单向链表 + */ +public class LinkedList { + private Node head; + + private Node current; + + private int size; + + public LinkedList(){ + } + + public int size(){ + return size; + } + + public void add(Object o){ + Node newNode=new Node(o,null); + if(size==0){ + head=current=newNode; + } + current.next=newNode; + current=newNode; + size++; + } + + public void add(int index , Object o){ + checkIndexForAdd(index); + if(index==size){ + add(o); + }else{ + Node newNode=new Node(o,null); + if(index==0){ + newNode.next=head; + head=newNode; + }else{ + Node after=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=newNode; + newNode.next=after; + } + size++; + } + } + + + public Object get(int index){ + return getIndexNode(index).data; + } + + public void addFirst(Object obj){ + add(0,obj); + } + public void addLast(Object obj){ + if(size==0){ + add(obj); + }else { + add(size,obj); + } + } + + public Object remove(int index){ + checkIndex(index); + Node needRemove; + if(index==0){ + needRemove=head; + if(size==1){ + head=null; + }else{ + head=head.next; + } + }else{ + needRemove=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=needRemove.next; + if(index==size-1){ + current=before; + } + } + size--; + return needRemove.data; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + + public class LinkedListIterator implements Iterator{ + + private int cursor=0; + + private Node cursorNode=head; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + Object object=cursorNode.data; + cursorNode=cursorNode.next; + cursor++; + return object; + } + } + + private void checkIndexForAdd(int index){ + if(!(index>=0&&index<=size)){ + throw new IndexOutOfBoundsException("索引"+index+"越界!"); + } + } + private void checkIndex(int index){ + if(!(index>=0&&index>>>>>>>>>>>>>>>>>>>>>>>>>"); + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + + @Test + public void addFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addFirst(-1); + + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + @Test + public void addLast() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addLast(2); + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + + + } + + @Test + public void remove() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + System.out.println(linkedList.size()); + try { + linkedList.remove(2); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.remove(1); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + try { + linkedList.remove(0); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void removeFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeFirst(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeFirst(); + System.out.println(linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeLast(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeLast(); + System.out.println(linkedList.size()); + + } +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java new file mode 100644 index 0000000000..baff49411c --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java @@ -0,0 +1,39 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试队列 + * Created by 赵睿 on 2017/2/25. + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue=new Queue(); + queue.enQueue(1); + queue.enQueue(2); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + } + + + @Test + public void isEmpty() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.isEmpty()); + queue.enQueue(1); + System.out.println(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.size()); + queue.enQueue(1); + System.out.println(queue.size()); + } + +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java new file mode 100644 index 0000000000..115a16f1fc --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java @@ -0,0 +1,54 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试栈 + * Created by 赵睿 on 2017/2/25. + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + } + + @Test + public void peek() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + + + } + + @Test + public void isEmpty() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.isEmpty()); + stack.push(1); + System.out.println(stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.size()); + stack.push(1); + System.out.println(stack.size()); + } + +} \ No newline at end of file From 43fc71637bfac0b55d6afe08a8f1eca5131bee79 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 21:27:23 +0800 Subject: [PATCH 4/8] Add Linked List --- .../src/com/coding/basic/LinkedList.java | 158 ++++++++++++++++-- 1 file changed, 147 insertions(+), 11 deletions(-) diff --git a/group03/619224754/src/com/coding/basic/LinkedList.java b/group03/619224754/src/com/coding/basic/LinkedList.java index e2c4e5e795..8ca8159baf 100644 --- a/group03/619224754/src/com/coding/basic/LinkedList.java +++ b/group03/619224754/src/com/coding/basic/LinkedList.java @@ -4,39 +4,175 @@ public class LinkedList implements List { private Node head; - public void add(Object o){ - + public void add(Object o) { + if(head == null) { + head = new Node(); + head.data = o; + } + else { + Node newNode = new Node(); + newNode.data = o; + Node lastNode = head; + while(head.next != null) { + lastNode = head.next; + } + lastNode.next = newNode; + } } - public void add(int index , Object o){ + + public void add(int index , Object o) { + if(index >= this.size()) + throw new IndexOutOfBoundsException("Index out of bound"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0) { + newNode.next = this.head; + this.head = newNode; + } + else if(index == this.size()) { + Node curNode = this.head; + while(curNode.next != null){ + curNode = curNode.next; + } + curNode.next = newNode; + } + else { + Node beforeNode = this.head; + Node afterNode = null; + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + afterNode = beforeNode.next; + newNode.next = afterNode; + beforeNode.next = newNode; + } } + public Object get(int index){ - return null; + Node retNode = this.head; + + if(index < 0){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + + if(index != 0) { + for(int i = 0; i < index; i++) { + retNode = retNode.next; + } + } + + return retNode.data; } + public Object remove(int index){ - return null; + Node beforeNode = null; + Node afterNode = null; + Node removedNode = null; + if(index == 0) { + removedNode = this.head; + this.head = this.head.next; + } + else { + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + removedNode = beforeNode.next; + afterNode = removedNode.next; + beforeNode.next = afterNode; + } + + + return removedNode.data; } public int size(){ - return -1; + int i = 0; + if(this.head == null) + return 0; + + Node curNode = this.head; + while(curNode != null){ + curNode = curNode.next; + i++; + } + return i; } public void addFirst(Object o){ - + Node firstNode = new Node(); + firstNode.data = o; + firstNode.next = this.head; + this.head = firstNode; } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + if(this.size() == 0){ + this.head = newNode; + } + Node curNode = this.head; + while(curNode.next != null) { + curNode = curNode.next; + } + curNode.next = newNode; } - public Object removeFirst(){ - return null; + + public Object removeFirst() { + Node retNode = this.head; + this.head = this.head.next; + + return retNode; } - public Object removeLast(){ - return null; + + public Object removeLast() { + Node curNode = null; + if(this.size() == 0) { + curNode = null; + } + else if(this.size() == 1) { + curNode = this.head; + this.head = null; + return curNode; + } + else { + Node beforeNode = this.head; + for (int i = 1; i < this.size() - 1; i++) { + beforeNode = beforeNode.next; + } + curNode = beforeNode.next; + beforeNode.next = null; + } + + return curNode; } + public Iterator iterator(){ return null; } + private class LinkedListIterator implements Iterator { + + private Node curNode = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.curNode.next != null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return this.curNode.next; + } + + + + } + private static class Node{ Object data; From c39f7f93c2d4a957bc7d87efa3ae76edc275fdd2 Mon Sep 17 00:00:00 2001 From: ESun Date: Sat, 25 Feb 2017 21:32:59 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayList,LinkedList,Stac?= =?UTF-8?q?k,Queue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group03/617187912/Learning02/.classpath | 6 + group03/617187912/Learning02/.gitignore | 1 + group03/617187912/Learning02/.project | 17 +++ .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/com/coding/basic/ArrayList.java | 117 +++++++++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 33 +++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 137 ++++++++++++++++++ .../Learning02/src/com/coding/basic/List.java | 10 ++ .../src/com/coding/basic/Queue.java | 33 +++++ .../src/com/coding/basic/Stack.java | 48 ++++++ 12 files changed, 423 insertions(+) create mode 100644 group03/617187912/Learning02/.classpath create mode 100644 group03/617187912/Learning02/.gitignore create mode 100644 group03/617187912/Learning02/.project create mode 100644 group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs create mode 100644 group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs create mode 100644 group03/617187912/Learning02/src/com/coding/basic/ArrayList.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Iterator.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/LinkedList.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/List.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Queue.java create mode 100644 group03/617187912/Learning02/src/com/coding/basic/Stack.java diff --git a/group03/617187912/Learning02/.classpath b/group03/617187912/Learning02/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group03/617187912/Learning02/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning02/.gitignore b/group03/617187912/Learning02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning02/.project b/group03/617187912/Learning02/.project new file mode 100644 index 0000000000..12dfa1118c --- /dev/null +++ b/group03/617187912/Learning02/.project @@ -0,0 +1,17 @@ + + + 617187912Learning02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs b/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9e22bd0c0b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList(){ + this(64); + } + + public ArrayList(int intSize) { + elementData = new Object[intSize]; + } + + public void add(Object o) { + checkMaxSize(); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkMaxSize(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void checkIndexRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限�?"); + } + } + + private void checkMaxSize() { + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + public Object get(int index) { + checkIndexRange(index); + return elementData[index]; + } + + public Object remove(int index) { + Object o = get(index); + if (index == size - 1) { + elementData[index] = null; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + private int current = 0; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + if (current >= size) { + throw new NoSuchElementException(); + } + return elementData[current++]; + } + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(5); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + arrayList.add(6); + System.out.println(arrayList.get(1)); + arrayList.add(1, 100); + System.out.println(arrayList.get(1)); + System.out.println(arrayList.size); + System.out.println(arrayList.remove(2)); + System.out.println(arrayList.get(2)); + + + ArrayList mixArraylist = new ArrayList(5); + mixArraylist.add("String"); + mixArraylist.add(100); + mixArraylist.add('f'); + mixArraylist.add(3.1f); + mixArraylist.add(4L); + System.out.println(mixArraylist.get(1)); + mixArraylist.add(1, 101); + System.out.println(mixArraylist.get(1)); + System.out.println(mixArraylist.size); + System.out.println(mixArraylist.remove(2)); + System.out.println(mixArraylist.get(2)); + } +} \ No newline at end of file diff --git a/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7b6479d535 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +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/group03/617187912/Learning02/src/com/coding/basic/Iterator.java b/group03/617187912/Learning02/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..017cbc4240 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..88a4c6c31b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkIndexRange(index); + if (index == 0) { + head = new Node(o, head); + size++; + } else { + Node nd = getNode(index-1); + nd.next = new Node(o, nd.next); + size++; + } + } + + public Object get(int index) { + return getNode(index).data; + } + + private Node getNode(int index) { + Node nd = head; + for (int i = 0; i < index; i++) { + nd = nd.next; + } + return nd; + } + + public Object remove(int index) { + if (size == 0) { + throw new NoSuchElementException(); + } + checkIndexRange(index); + if (index == 0) { + Object o = head.data; + head = head.next; + size--; + return o; + } else { + Node nd = getNode(index - 1); + Object o = nd.next.data; + nd.next = nd.next.next; + size--; + return o; + } + } + + private void checkIndexRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限?"); + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0, o); + } + + public void addLast(Object o) { + if (size == 0) { + addFirst(o); + } else { + add(size, o); + } + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size - 1); + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + public Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Object next() { + Object o = current.data; + current = current.next; + return o; + } + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + for (int i = 0; i < list.size; i++) { + System.out.println(list.get(i)); + } + System.out.println(list.get(2)); + list.add(2, 100); + System.out.println(list.get(2)); + list.addFirst(10); + System.out.println(list.get(2)); + list.addLast(100); + System.out.println(list.remove(1)); + System.out.println(list.removeFirst()); + System.out.println(list.removeLast()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/List.java b/group03/617187912/Learning02/src/com/coding/basic/List.java new file mode 100644 index 0000000000..cd5130be3b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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/group03/617187912/Learning02/src/com/coding/basic/Queue.java b/group03/617187912/Learning02/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..39e1a8b60b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Queue.java @@ -0,0 +1,33 @@ +package com.coding.basic; + + +public class Queue { + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() ==0; + } + + public int size() { + return linkedList.size(); + } + public static void main(String[] args) { + Queue que = new Queue(); + que.enQueue(10); + que.enQueue(11); + que.enQueue(12); + System.out.println(que.deQueue()); + System.out.println(que.isEmpty()); + que.deQueue(); + que.deQueue(); + System.out.println(que.isEmpty()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/Stack.java b/group03/617187912/Learning02/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1ee047ba4a --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Stack.java @@ -0,0 +1,48 @@ +package com.coding.basic; + + +import java.util.EmptyStackException; + +import javax.lang.model.element.QualifiedNameable; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkIsEmpty(); + return elementData.remove(size()-1); + } + + private void checkIsEmpty() { + if (isEmpty()){ + throw new EmptyStackException(); + } + } + + public Object peek(){ + checkIsEmpty(); + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } + public static void main(String[] args) { + Stack que = new Stack(); + que.push(10); + que.push(11); + que.push(12); + System.out.println(que.peek()); + System.out.println(que.isEmpty()); + System.out.println(que.pop()); + System.out.println(que.pop()); + que.pop(); + System.out.println(que.isEmpty()); + } +} From ad1d702c1f485b462bed971e4f21082ccc7405fa Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 21:43:07 +0800 Subject: [PATCH 6/8] add queue and stack --- group03/619224754/src/com/coding/basic/Queue.java | 12 ++++++++---- group03/619224754/src/com/coding/basic/Stack.java | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/group03/619224754/src/com/coding/basic/Queue.java b/group03/619224754/src/com/coding/basic/Queue.java index 36e516e266..de53482312 100644 --- a/group03/619224754/src/com/coding/basic/Queue.java +++ b/group03/619224754/src/com/coding/basic/Queue.java @@ -2,18 +2,22 @@ public class Queue { - public void enQueue(Object o){ + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o); } public Object deQueue(){ - return null; + Object ret = list.removeFirst(); + return ret; } public boolean isEmpty(){ - return false; + return list.size() == 0; } public int size(){ - return -1; + return list.size(); } } diff --git a/group03/619224754/src/com/coding/basic/Stack.java b/group03/619224754/src/com/coding/basic/Stack.java index a5a04de76d..cf10ecc1b3 100644 --- a/group03/619224754/src/com/coding/basic/Stack.java +++ b/group03/619224754/src/com/coding/basic/Stack.java @@ -4,19 +4,22 @@ public class Stack { private ArrayList elementData = new ArrayList(); public void push(Object o){ + this.elementData.add(o); } public Object pop(){ - return null; + Object ret = this.elementData.remove(this.elementData.size() - 1); + return ret; } public Object peek(){ + Object ret = this.elementData.get(this.elementData.size() - 1); return null; } public boolean isEmpty(){ - return false; + return this.elementData.size() == 0; } public int size(){ - return -1; + return this.elementData.size(); } } From 5918d329620437a1138bf5de5fdabdc5034b8aa1 Mon Sep 17 00:00:00 2001 From: longcloud Date: Sat, 25 Feb 2017 22:17:02 +0800 Subject: [PATCH 7/8] Finished binary tree --- .../src/com/coding/basic/BinaryTreeNode.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java index d7ac820192..58005fb1b4 100644 --- a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java +++ b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.Comparator; + public class BinaryTreeNode { private Object data; @@ -9,24 +11,70 @@ public class BinaryTreeNode { public Object getData() { return data; } + public void setData(Object data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } public BinaryTreeNode insert(Object o){ - return null; + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.data = o; + int intO = Integer.parseInt(o.toString()); + int intData = Integer.parseInt(this.data.toString()); + if(intO > intData){ + if(this.right == null){ + this.right = treeNode; + } + else { + this.right.insert(o); + } + } + else { + if(this.left == null) { + this.left = treeNode; + } + else { + this.left.insert(o); + } + } + return treeNode; + } + + private class MyComparator implements Comparator { + + @Override + public int compare(BinaryTreeNode arg0, BinaryTreeNode arg1) { + // TODO Auto-generated method stub + int int0 = Integer.parseInt(arg0.data.toString()); + int int1 = Integer.parseInt(arg1.data.toString()); + if(int0 > int1) { + return 1; + } + else if(int0 < int1){ + return -1; + } + + return 0; + + } + + } } From 0820189c23d6078fe1f4e8c2708127c98b52e490 Mon Sep 17 00:00:00 2001 From: LiANG XUHUI <172487938@qq.com> Date: Sat, 25 Feb 2017 22:29:27 +0800 Subject: [PATCH 8/8] 172487938 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基本数据结构ArrayList,LinkedList,stack,Queue的实现 --- group03/172487938/Iterator.java | 7 + group03/172487938/MyArrayList.java | 186 ++++++++++++++++ group03/172487938/MyLinkedList.java | 274 ++++++++++++++++++++++++ group03/172487938/MyList.java | 27 +++ group03/172487938/Queue.java | 32 +++ group03/172487938/Stack.java | 37 ++++ group03/172487938/TestMyArrayList.java | 36 ++++ group03/172487938/TestMyLinkedList.java | 19 ++ 8 files changed, 618 insertions(+) create mode 100644 group03/172487938/Iterator.java create mode 100644 group03/172487938/MyArrayList.java create mode 100644 group03/172487938/MyLinkedList.java create mode 100644 group03/172487938/MyList.java create mode 100644 group03/172487938/Queue.java create mode 100644 group03/172487938/Stack.java create mode 100644 group03/172487938/TestMyArrayList.java create mode 100644 group03/172487938/TestMyLinkedList.java diff --git a/group03/172487938/Iterator.java b/group03/172487938/Iterator.java new file mode 100644 index 0000000000..e6187ad2fd --- /dev/null +++ b/group03/172487938/Iterator.java @@ -0,0 +1,7 @@ +package 基本数据结构; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public void remove(); +} diff --git a/group03/172487938/MyArrayList.java b/group03/172487938/MyArrayList.java new file mode 100644 index 0000000000..0c00a277ff --- /dev/null +++ b/group03/172487938/MyArrayList.java @@ -0,0 +1,186 @@ +package 基本数据结构; + + +/** + * Created by LIANG on 2017/2/24. + */ +public class MyArrayList implements MyList +{ + public static final int INITIAL_CAPACITTY = 16; + private E[] data = (E[]) new Object[INITIAL_CAPACITTY]; + private static int size = 0; + + public MyArrayList(E[] objects) + { + + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); + size++; + } + } + + public MyArrayList() {} + + ; + + public void ensureCapicity() + { + if (size >= data.length) + { + E[] newData = (E[]) new Object[size * 2]; + System.arraycopy(data, 0, newData, 0, data.length); + data = newData; + } + } + +// public boolean add(E e) +// { +// ensureCapicity(); +// if(e != null) +// data[size++] = e; +// return true; +// } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + ensureCapicity(); + for (int i = size - 1; i >= index; i--) + { + data[i + 1] = data[i]; + } + data[index] = e; + size++; + } + + @Override + public E get(int index) + { + return data[index]; + } + + @Override + public E remove(int index) + { + checkIndex(index); + E e = data[index]; + for (int i = index; i < data.length - 1; i++) + data[i] = data[i + 1]; + + data[size - 1] = null; + size--; + return e; + + } + + private void checkIndex(int index) + { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("输入下标有误"); + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + data = (E[]) new Object[INITIAL_CAPACITTY]; + size = 0; + } + + @Override + public int indexOf(E e) + { + for (int i = 0; i < size; i++) + { + if (e.equals(data[i])) + return i; + } + return -1; + } + + + @Override + public boolean isEmpty() + { + return size == 0; + } + + @Override + public E set(int index, E e) + { + checkIndex(index); + E temp = data[index]; + data[index] = e; + return temp; + } + + @Override + public boolean contains(E e) + { + for (int i = 0; i < size; i++) + { + if(data[i].equals(e)) + return true; + } + return false; + } + + public void trimToSize() + { + if (size != data.length) + { + E[] newData = (E[]) new Object[size]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + for (int i = 0; i < size; i++) + { + result.append(data[i]); + if (i < size - 1) + result.append(","); + } + return result.toString() + "]"; + } + + private class ArrayListIterator implements Iterator + { + private int current = 0; + + @Override + public boolean hasNext() + { + return (current < size); + } + + @Override + public E next() + { + return data[current++]; + } + + @Override + public void remove() + { + MyArrayList.this.remove(current); + } + } + +} diff --git a/group03/172487938/MyLinkedList.java b/group03/172487938/MyLinkedList.java new file mode 100644 index 0000000000..609ab8c81d --- /dev/null +++ b/group03/172487938/MyLinkedList.java @@ -0,0 +1,274 @@ +package 基本数据结构; + +public class MyLinkedList implements MyList +{ + private Node head, tail; + private int size; + + public MyLinkedList() {} + + public MyLinkedList(E[] objects) + { + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); +// size++; + } + } + + public E getFirst() + { + if (size == 0) + return null; + else + return head.element; + } + + public E getLast() + { + if (size == 0) + return null; + else + return tail.element; + } + + public void addFirst(E e) + { + Node newNode = new Node<>(e); + newNode.next = head; + head = newNode; + size++; + + if (tail == null) + tail = head; + } + + public void addLast(E e) + { + Node newNode = new Node<>(e); + + if (tail == null) + head = tail = newNode; + else + { + tail.next = newNode; + tail = tail.next; + } + size++; + } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + if (index == 0) + { + addFirst(e); + } else if (index >= size) + { + addLast(e); + } else + { + Node current = head; + for (int i = 1; i < index; i++) + { + current = current.next; + } + Node temp = current.next; + current.next = new Node(e); + (current.next).next = temp; + size++; + } + } + + @Override + public E get(int index) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + return current.element; + } + + @Override + public E remove(int index) + { + if (index < 0 || index >= size) + return null; + else if (index == 0) + { + E e = removeFirst(); + return e; + } else if (index == size - 1) + return removeLast(); + else + { + Node previous = head; + + for (int i = 1; i < index; i++) + { + previous = previous.next; + } + + Node current = previous.next; + previous.next = current.next; + size--; + return current.element; + } + } + + public E removeFirst() + { + if (size == 0) + return null; + else + { + Node temp = head; + head = head.next; + size--; + if (head == null) + tail = null; + return temp.element; + } + } + + public E removeLast() + { + if (size == 0) + return null; + else if (size == 1) + { + Node temp = head; + head = tail = null; + size = 0; + return temp.element; + } else + { + Node current = head; + + for (int i = 0; i < size - 2; i++) + current = current.next; + + Node temp = tail; + tail = current; + tail.next = null; + size--; + return temp.element; + } + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + size = 0; + head = tail = null; + } + + @Override + public int indexOf(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return i; + current = current.next; + } + return -1; + } + + @Override + public boolean isEmpty() + { + if (size != 0) + return false; + else + return true; + } + + @Override + public E set(int index, E e) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + E temp = current.element; + current.element = e; + + return temp; + } + + @Override + public boolean contains(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return true; + current = current.next; + } + return false; + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + if (size == 0) + return null; + else + { + Node current = head; + for (int i = 0; i < size; i++) + { + try + { + result.append(current.element); + } catch (Exception e) + { + e.printStackTrace(); + } + current = current.next; + if (current != null) + { + result.append(", "); + } else + { + result.append("]"); + } + } + return result.toString(); + } + } + + public static class Node + { + E element; + Node next; + + public Node(E e) + { + this.element = e; + } + } +} \ No newline at end of file diff --git a/group03/172487938/MyList.java b/group03/172487938/MyList.java new file mode 100644 index 0000000000..5d16147855 --- /dev/null +++ b/group03/172487938/MyList.java @@ -0,0 +1,27 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public interface MyList +{ + + public void add(int index, E e); + + public E get(int index); + + public E remove(int index); + + public int size(); + + public void clear(); + + + public int indexOf(E e); + + public boolean isEmpty(); + + public E set(int index, E e); + + public boolean contains(E e); +} diff --git a/group03/172487938/Queue.java b/group03/172487938/Queue.java new file mode 100644 index 0000000000..af335f5274 --- /dev/null +++ b/group03/172487938/Queue.java @@ -0,0 +1,32 @@ +package 基本数据结构; + +import java.util.LinkedList; + +public class Queue +{ + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.addLast(o); + } + + public Object deQueue() + { + return list.removeFirst(); + } + + public boolean isEmpty() + { + if(list.size() != 0) + return true; + else + return false; + } + + public int size() + { + return list.size(); + } +} diff --git a/group03/172487938/Stack.java b/group03/172487938/Stack.java new file mode 100644 index 0000000000..f1b67822b1 --- /dev/null +++ b/group03/172487938/Stack.java @@ -0,0 +1,37 @@ +package 基本数据结构; + +import java.util.ArrayList; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(o); + } + + public Object pop() + { + Object popElement = elementData.remove(elementData.size() - 1); + return popElement; + } + + public Object peek() + { + Object peekElement = elementData.get(size() - 1); + return peekElement; + } + + public boolean isEmpty() + { + if(elementData.size() != 0) + return false; + return true; + } + + public int size() + { + return elementData.size(); + } +} diff --git a/group03/172487938/TestMyArrayList.java b/group03/172487938/TestMyArrayList.java new file mode 100644 index 0000000000..dbf8b610e1 --- /dev/null +++ b/group03/172487938/TestMyArrayList.java @@ -0,0 +1,36 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public class TestMyArrayList +{ + public static void main(String[] args) + { + MyArrayList list = new MyArrayList<>(); + list.add("America"); + System.out.println(list); + + list.add("Canada"); + System.out.println(list); + + list.add("Tokoy"); + System.out.println(list); + + list.add("Shanghai"); + + + int size = list.size(); + System.out.println(size); + list.add(0,"China"); + System.out.println(list); + String[] str = {"Japan","England","France","HonKong","BeiJing"}; + for(String s:str) + list.add(s); + System.out.println(list); + + list.remove(3); + System.out.println(list); + + } +} diff --git a/group03/172487938/TestMyLinkedList.java b/group03/172487938/TestMyLinkedList.java new file mode 100644 index 0000000000..cd412ef646 --- /dev/null +++ b/group03/172487938/TestMyLinkedList.java @@ -0,0 +1,19 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/25. + */ +public class TestMyLinkedList +{ + public static void main(String[] args) + { + String[] name = {"Tom", "George", "Peter", "Jean", "George", "Jane"}; + MyList list = new MyLinkedList(name); + + System.out.println(list.contains("George")); + System.out.println(list.get(3)); + System.out.println(list.indexOf("George")); + list.set(4, "Michael"); + System.out.println(list); + } +}