diff --git a/group04/1796244932/.gitignore b/group04/1796244932/.gitignore new file mode 100644 index 0000000000..b241c62e19 --- /dev/null +++ b/group04/1796244932/.gitignore @@ -0,0 +1,138 @@ +.metadata/ +RemoteSystemsTempFiles/ +.recommenders/ + +*.iml + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +*/.idea/ +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +### Java ### +# 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* + +# End of https://www.gitignore.io/api/eclipse,intellij,java diff --git a/group04/1796244932/learn01/.gitignore b/group04/1796244932/learn01/.gitignore new file mode 100644 index 0000000000..83ef0e8099 --- /dev/null +++ b/group04/1796244932/learn01/.gitignore @@ -0,0 +1,6 @@ +.settings/ +/target/ +.classpath +.project +/bin/ + diff --git a/group04/1796244932/learn01/pom.xml b/group04/1796244932/learn01/pom.xml new file mode 100644 index 0000000000..5a00c4d139 --- /dev/null +++ b/group04/1796244932/learn01/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + com.dudy + learn01 + 0.0.1-SNAPSHOT + jar + + learn01 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.11 + test + + + + + + + + jdk-1.8 + + true + 1.8 + + + 1.8 + 1.8 + 1.8 + + + + diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java new file mode 100644 index 0000000000..5a8e82aaaf --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java @@ -0,0 +1,124 @@ +package com.dudy.learn01.base; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[16]; + + /** + * 增加元素: ①数组没满之前,直接添加到最后,满了扩容添加 + */ + public void add(Object o) { + // 检查是否需要扩容 + this.checkGrow(size + 1); + elementData[size++] = o; + } + + /** + * 检查是否需要扩容 + * + * @param newSize + */ + private void checkGrow(int newSize) { + if (newSize > elementData.length) { + this.grow(elementData); + } + } + + /** + * 扩容 + * + * @param oldElementData + */ + private void grow(Object[] oldElementData) { + int lenth = (int) (oldElementData.length * 1.5); + elementData = Arrays.copyOf(oldElementData, lenth); + } + + /** + * 根据索引添加:①同 add ② 可能会出现 index 超出当前位置的情况 ③往 中间插入时需要移位 + */ + public void add(int index, Object o) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + this.checkGrow(size + 1); + // 循环移位 + int tmp = size; + for (int i = 0; i < size - index; i++) { + elementData[tmp] = elementData[tmp - 1]; + tmp--; + } + // 索引位置赋值 + elementData[index] = o; + size++; + } + + /** + * 直接返回相应数组下标就好 + */ + public Object get(int index) { + return elementData[index]; + } + + /** + * 删除元素:①注意移位 + */ + public Object remove(int index) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + Object desc = elementData[index]; + + for (int i = 0; i < size - index; i++) { + elementData[index] = elementData[index+1]; + index++; + } + size--; + return desc; + } + + public int size() { + return this.size; + } + + public MyIterator iterator() { + + + return new MyItr(); + } + + public class MyItr implements MyIterator{ + + int cursor; + + + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + return elementData[cursor++]; + } + } + + + + @Override + public String toString() { + StringBuffer str = new StringBuffer(); + str.append("["); + for (int i = 0; i < size; i++) { + str.append(elementData[i]+","); + } + str.append("]"); + return str.toString(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java new file mode 100644 index 0000000000..cfc06bc0d2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java @@ -0,0 +1,71 @@ +package com.dudy.learn01.base; + +public class MyBinaryTree { + + private Node root; + + public Node getRoot() { + return root; + } + + static class Node { + private int data; + private Node left; + private Node right; + + public Node(int data) { + this.data = data; + } + + } + + public Node insert(int o) { + Node newNode = new Node(o); + + if (root == null) { + root = newNode; + return newNode; + } + Node currentNode = root; + + while (true) { + // System.out.println("currentNode: " + currentNode.data ); + if (o <= currentNode.data) { // left + + if (currentNode.left != null) { + currentNode = currentNode.left; + } else { + currentNode.left = newNode; + // System.out.println("left return ..."); + // System.out.println(""); + return newNode; + } + } else { // right + if (currentNode.right != null) { + currentNode = currentNode.right; + } else { + currentNode.right = newNode; + // System.out.println("right return ..."); + // System.out.println(""); + return newNode; + } + } + + } + + } + + public void display(Node root) { + + System.out.print(root.data + " "); + if (root.left != null) { + display(root.left); + } + if (root.right != null) { + display(root.right); + } + } + + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java new file mode 100644 index 0000000000..e53306fa03 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java @@ -0,0 +1,6 @@ +package com.dudy.learn01.base; +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java new file mode 100644 index 0000000000..60254997aa --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java @@ -0,0 +1,132 @@ +package com.dudy.learn01.base; + + +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node head; + + public void add(Object o) { + Node newNode = new Node(o); + newNode.next = head;// next --> head + head = newNode; // head --> newNode + size++; + } + + public void add(int index, Object o) { + checkRange(index); + Node current = getCurrentNode(index); + Node newNode = new Node(o); + newNode.next = current.next;//new next --> next + current.next = newNode; // next -->new + size++; + } + + private Node getCurrentNode(int index) {// 获取当前节点 + Node current = head; + for(int i = 0; i< size-index -1; i++){ + current = current.next; + } + return current; + } + + private void checkRange(int index) { + if(index > size || index < 0){ + throw new RuntimeException("indexOutOfException:" + "Index: " + index + ", Size: " + size); + } + } + + public Object get(int index) { + checkRange(index); + Node node = getCurrentNode(index); + return node.data; + } + + public Object remove(int index) { + if(size < 1){// ①没有元素的情况 + throw new RuntimeException("NoSuchElementException: size= " + size); + } + if(index == 0) return removeFirst(); + if(index == size - 1) return removeLast(); + + Node node = getCurrentNode(index+1); + node.next = node.next.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addLast(Object o) { + add(o); + } + + public void addFirst(Object o) { + Node current = head; + while(current.next != null){// 找到最后一个节点 + current = current.next; + } + Node newNode = new Node(o); + current.next = newNode; + size++; + } + + public Object removeFirst() { + Node currentNode = getCurrentNode(1); + Node tmp = currentNode.next; + currentNode.next = null; + size--; + return tmp ==null? currentNode.data:tmp.data;// 可能只有一个数据 + } + + public Object removeLast() { + Node tmp = head; + head = head.next; + size--; + return tmp.data; + } + + public MyIterator iterator() { + return new MyLinkedListItr(); + } + + + public class MyLinkedListItr implements MyIterator { + + int cursor; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + Node currentNode = getCurrentNode(cursor++); + return currentNode.data; + } + } + + private static class Node { + Object data; + Node next; + public Node() { + } + public Node(Object data) { + this.data = data; + } + } + + + private void displayLink() {// 自己调试使用 + Node current = head; + while(current != null){ + System.out.print(current.data); + current = current.next; + } + System.out.println(""); + } + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java new file mode 100644 index 0000000000..455727381d --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java @@ -0,0 +1,8 @@ +package com.dudy.learn01.base; +public interface MyList { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java new file mode 100644 index 0000000000..25b9da93f2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java @@ -0,0 +1,22 @@ +package com.dudy.learn01.base; +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0 ; + } + + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java new file mode 100644 index 0000000000..e59c366e0a --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java @@ -0,0 +1,26 @@ +package com.dudy.learn01.base; +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()); + return o; + } + + public Object peek(){ + + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java new file mode 100644 index 0000000000..5c6edd8fec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java @@ -0,0 +1,59 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Iterator; + +public class MyArrayListTest { + + + + @Test + public void iteraterTest(){ + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void myArrayListTest() { + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + list.add(1, "s"); + list.add(21, 21); + System.out.println("--" + list.size()); + System.out.println(list); + + Object remove = list.remove(3); + System.out.println("remove:" + remove); + System.out.println("--" + list.size()); + System.out.println(list); + } + + @Test + public void arrayListTest(){ + + ArrayList list = new ArrayList(); + list.add("1"); + list.add("2"); + + for (Iterator it = list.iterator(); it.hasNext();){ + System.out.println(it.next()); + } + + } + + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java new file mode 100644 index 0000000000..65bee44580 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java @@ -0,0 +1,42 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/21. + */ +public class MyBinaryTreeTest { + + + /** + * 5 + * 2 7 + * 1 4 6 8 + * + */ + @Test + public void insert() throws Exception { + + + MyBinaryTree tree = new MyBinaryTree(); + tree.insert(5); + tree.insert(2); + tree.insert(1); + tree.insert(7); + tree.insert(8); + tree.insert(4); + tree.insert(6); + + + tree.display(tree.getRoot()); + + } + + @Test + public void display() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java new file mode 100644 index 0000000000..c6ebb096ec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java @@ -0,0 +1,88 @@ +package com.dudy.learn01.base; + +import java.util.LinkedList; + +import org.junit.Test; + +public class MyLinkedListTest { + + + + @Test + public void iteraterTest(){ + MyLinkedList list = new MyLinkedList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void removeTest(){ + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + //list.remove(0); + //list.remove(1); + list.remove(2); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + + + @Test + public void removeLastTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + //list.add(2); + list.removeLast(); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void removeFirstTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + Object first = list.removeFirst(); + System.out.println("--" + list.size() + ",first = " + first); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void baseTest() { + + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.addFirst(0); + list.addLast("last"); + list.add(3, "s");// 0 1 2 s 3 last + System.out.println(list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + Integer first = list.removeFirst(); + } + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java new file mode 100644 index 0000000000..de8d683abe --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java @@ -0,0 +1,27 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyQueueTest { + @Test + public void enQueue() throws Exception { + + + MyQueue queue = new MyQueue(); + queue.enQueue("1"); +// queue.enQueue("2"); +// +// queue.enQueue("3"); + + while (queue.size() > 0){ + System.out.println(queue.deQueue()); + } + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java new file mode 100644 index 0000000000..51175953b0 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java @@ -0,0 +1,58 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.Stack; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyStackTest { + @Test + public void push() throws Exception { + + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + + Object pop = stack.pop(); + System.out.println( "size :" + stack.size() + "pop:" + pop); + Object peek = stack.peek(); + System.out.println( "size :" + stack.size() + "peek: " + peek); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void stackTest(){ + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.peek(); + stack.pop(); + System.out.println(stack.size()); + + } + +} \ No newline at end of file