From 7ce083216f67174bcab0eed52964401a57d29a4e Mon Sep 17 00:00:00 2001 From: zg Date: Sat, 25 Feb 2017 11:28:46 +0800 Subject: [PATCH] first work --- group14/254659936/.gitignore | 10 ++ group14/254659936/src/Main.java | 7 + .../src/com/coding/basic/ArrayList.java | 73 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 69 ++++++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 155 ++++++++++++++++++ .../254659936/src/com/coding/basic/List.java | 13 ++ .../254659936/src/com/coding/basic/Queue.java | 45 +++++ .../254659936/src/com/coding/basic/Stack.java | 50 ++++++ 9 files changed, 430 insertions(+) create mode 100644 group14/254659936/.gitignore create mode 100644 group14/254659936/src/Main.java create mode 100644 group14/254659936/src/com/coding/basic/ArrayList.java create mode 100644 group14/254659936/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/254659936/src/com/coding/basic/Iterator.java create mode 100644 group14/254659936/src/com/coding/basic/LinkedList.java create mode 100644 group14/254659936/src/com/coding/basic/List.java create mode 100644 group14/254659936/src/com/coding/basic/Queue.java create mode 100644 group14/254659936/src/com/coding/basic/Stack.java diff --git a/group14/254659936/.gitignore b/group14/254659936/.gitignore new file mode 100644 index 0000000000..5561991601 --- /dev/null +++ b/group14/254659936/.gitignore @@ -0,0 +1,10 @@ +*.iml +.gradle +.idea +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild \ No newline at end of file diff --git a/group14/254659936/src/Main.java b/group14/254659936/src/Main.java new file mode 100644 index 0000000000..4d4fbb71ef --- /dev/null +++ b/group14/254659936/src/Main.java @@ -0,0 +1,7 @@ + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/group14/254659936/src/com/coding/basic/ArrayList.java b/group14/254659936/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7fb9c076b6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.Objects; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + if (size == elementData.length) { + Object[] newArr = new Object[elementData.length * 2]; + System.arraycopy(newArr, 0, elementData, 0, elementData.length); + elementData = newArr; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + elementData[index] = o; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + Object resultObj = elementData[index]; + size--; + for (int i = index; i < size; i++) { + elementData[index] = elementData[index + 1]; + } + return resultObj; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private class ArrayIterator implements Iterator { + + private int iteratorIndex = 0; + + @Override + public boolean hasNext() { + return iteratorIndex < size; + } + + @Override + public Object next() { + if (iteratorIndex >= size) { + throw new RuntimeException("the index is out of the list"); + } + return elementData[iteratorIndex++]; + } + } + +} diff --git a/group14/254659936/src/com/coding/basic/BinaryTreeNode.java b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..8b89c37114 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public T getData() { + return data; + } + + public void setData(T 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(T o) { + BinaryTreeNode node = new BinaryTreeNode(); + node.setData(o); + insert(this, node); + return node; + } + + private void insert(BinaryTreeNode parent, BinaryTreeNode child) { + BinaryTreeNode node; + if (child.getData().isLargeThanTarget(parent.getData())) { + // 子节点比父节点大,需要向右插入 + node = getRight(); + if (null == node) { + // 右节点为空则可以直接插入 + parent.setRight(node); + } else { + // 递归检查右边子树的插入位置 + insert(node, child); + } + } else { + // 子节点比父节点小,或者等于父节点,需要向左插入 + node = getLeft(); + if (null == node) { + // 左节点为空,则直接插入 + parent.setLeft(node); + } else { + // 递归检查左子树的插入位置 + insert(node, child); + } + } + } + + public interface Compare { + boolean isLargeThanTarget(Compare target); + } + +} diff --git a/group14/254659936/src/com/coding/basic/Iterator.java b/group14/254659936/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..160f044ad1 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group14/254659936/src/com/coding/basic/LinkedList.java b/group14/254659936/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e0ad67a6d8 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/LinkedList.java @@ -0,0 +1,155 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node node = new Node(); + node.data = 0; + if (index == 0) { + node.next = head; + head = node; + } + Node next = head; + int i = 0; + while (null != next) { + i++; + if (index == i) { + node.next = next.next; + next.next = node; + break; + } + next.next = next.next; + } + size++; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + return next; + } + next = next.next; + i++; + } + return null; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + size--; + Node resultNode = null; + if (index == 0) { + resultNode = head; + head = head.next; + return resultNode.data; + } + + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + resultNode = next.next; + next.next = resultNode.next; + } + } + return resultNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + size++; + Node node = new Node(); + node.data = 0; + node.next = head; + head = node; + } + + public void addLast(Object o) { + size++; + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + return; + } + Node next = head; + while (null != next.next) { + next = next.next; + } + next.next = node; + } + + public Object removeFirst() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + size--; + Object obj = head.data; + head = head.next; + return obj; + } + + public Object removeLast() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + Node next = head; + Object obj = null; + for (int i = 0; i < size - 1; i++) { + next = next.next; + } + next.next = null; + size--; + return obj; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + } + + private class LinkedListIterator implements Iterator { + + private Node next = head; + + @Override + public boolean hasNext() { + return null != next; + } + + @Override + public Object next() { + if (null == next) { + throw new RuntimeException("the LinkedList is out of index"); + } + Object obj = next.data; + next = next.next; + return obj; + } + } +} diff --git a/group14/254659936/src/com/coding/basic/List.java b/group14/254659936/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +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/group14/254659936/src/com/coding/basic/Queue.java b/group14/254659936/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..50f99e6d1c --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Queue.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Queue { + + private Node head; + private Node tail; + private int size; + + public void enQueue(Object o) { + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + tail = node; + } else { + tail.next = node; + tail = tail.next; + } + size++; + } + + public Object deQueue() { + if (size <= 0) { + throw new RuntimeException("the queue is empty"); + } + Object obj = head.data; + head = head.next; + size--; + return obj; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} diff --git a/group14/254659936/src/com/coding/basic/Stack.java b/group14/254659936/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..796a3e2e24 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.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; + } +}