diff --git a/group12/446031103/src/com/coding/basic/ArrayList.java b/group12/446031103/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c627a7d2fa --- /dev/null +++ b/group12/446031103/src/com/coding/basic/ArrayList.java @@ -0,0 +1,121 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * + * arrayList集合-数组 + * + * @ClassName ArrayList + * @author msh + * @date 2017年2月21日 下午3:49:24 + */ +public class ArrayList implements List { + // 记录ArrayList集合大小 + private int size = 0; + // 初始化存储数组 + private Object[] elementData = new Object[100]; + /** + * + * 向最后插入元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + // 数组不够时增长 + growOrNot(size + 1); + elementData[size] = o; + ++size; + } + /** + * + * 向指定位置插入元素 + * + * @Method add 添加 + * @param index 下标 + * @param o 元素 + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o){ + validate(index); + growOrNot(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + ++size; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + return elementData[index]; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return 删除的元素 + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index){ + Object oldValue = elementData[index]; + validate(index); + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[size] = null; + --size; + return oldValue; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.coding.basic.List#size() + */ + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + /** + * + * 判断是否需要增长数组 + * + * @MethodName growOrNot + * @author msh + * @date 2017年2月21日 下午3:53:29 + * @param minCapacity + */ + private void growOrNot(int minCapacity) { + // 当增加长度大于数组长度时,增长 + if (minCapacity > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} diff --git a/group12/446031103/src/com/coding/basic/BinaryTreeNode.java b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..ed26d946bb --- /dev/null +++ b/group12/446031103/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,44 @@ +package com.coding.basic; + + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode( Object data,BinaryTreeNode left,BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = 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(BinaryTreeNode tree,Object o){ + if(null== tree){ + tree = new BinaryTreeNode(o, null, null); + } + if(Integer.valueOf(o.toString())>Integer.valueOf(tree.data.toString())){ + tree.right =insert(tree.right,o); + }else{ + tree.left = insert(tree.left,o); + } + return tree; + } + +} diff --git a/group12/446031103/src/com/coding/basic/Iterator.java b/group12/446031103/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/LinkedList.java b/group12/446031103/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de508694a0 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/LinkedList.java @@ -0,0 +1,225 @@ +package com.coding.basic; + +/** + * + * LinkedList集合-链 + * + * @ClassName LinkedList + * @author msh + * @date 2017年2月21日 下午4:08:01 + */ +public class LinkedList implements List { + //链头 + private Node head; + //集合大小 + private int size=0; + /** + * + * 向链中添加元素 + * + * @Method add 添加 + * @param o 元素 + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o){ + Node newNode = new Node(o, null); + if (null == head) { + head = newNode; + } else { + Node lastNode = null; + for (int i = 0; i < size; i++) { + lastNode = (Node) get(i); + } + lastNode.next = newNode; + } + size++; + } + /** + * + * 向链中添加元素 + * + * @Method add 增加 + * @param index 下标 + * @param o 元素 + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + validate(index); + Node newNode = null; + Node perNode = null; + Node nextNode = null; + // 当为最后插入时 + if (index == size - 1) { + newNode = new Node(o, null); + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + perNode.next = newNode; + } else if (0 == index) { + nextNode = head.next; + newNode = new Node(o, nextNode); + head = newNode; + } else { + for (int i = 0; i < index; i++) { + Node tempNode = (Node) get(i); + perNode = tempNode.next; + } + nextNode = perNode.next.next; + newNode = new Node(o, nextNode); + perNode.next = newNode; + } + size++; + } + /** + * + * 取得元素 + * + * @Method get 取得 + * @param index 下标 + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + validate(index); + Node tempNode = head; + for (int i = 0; i <= index; i++) { + tempNode = tempNode.next; + } + return tempNode; + } + /** + * + * 删除元素 + * + * @Method remove 删除 + * @param index 下标 + * @return + * @see com.coding.basic.List#remove(int) + */ + public Object remove(int index){ + Node removeNode = (Node) get(index); + validate(index); + if (index == size - 1) { + Node tempNode = head; + for (int i = 0; i < index; i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + } else if (index == 0) { + Node tempNode = head.next; + head.next = null; + head = tempNode; + } else { + } + size--; + return removeNode; + } + /** + * + * 取得集合大小 + * + * @Method size 集合大小 + * @return 集合大小 + * @see com.coding.basic.List#size() + */ + public int size(){ + return size; + } + /** + * + * 想链头中插入元素 + * + * @MethodName addFirst + * @author msh + * @date 2017年2月21日 下午4:10:56 + * @param o + */ + public void addFirst(Object o){ + Node newNode = new Node(o, head); + head = newNode; + } + /** + * + * 向链后加入元素 + * + * @MethodName addLast + * @author msh + * @date 2017年2月21日 下午4:11:43 + * @param o + */ + public void addLast(Object o){ + add(o); + } + /** + * + * 删除链头 + * + * @MethodName removeFirst + * @author msh + * @date 2017年2月21日 下午4:12:14 + * @return + */ + public Object removeFirst(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node orgHead = head; + Node tempNode = head.next; + head.next = null; + head = tempNode; + return orgHead; + } + /** + * + * 删除链尾 + * + * @MethodName removeLast + * @author zhaogd + * @date 2017年2月21日 下午4:12:44 + * @return + */ + public Object removeLast(){ + if(null==head) + throw new IndexOutOfBoundsException("Size: " + size); + Node lastNode = (Node) get(size); + Node tempNode = head; + for (int i = 0; i < (size - 1); i++) { + tempNode = tempNode.next; + } + tempNode.next = null; + return lastNode; + } + public Iterator iterator(){ + return null; + } + + /** + * + * 验证 + * + * @MethodName validate 下标 + * @author msh + * @date 2017年2月21日 下午3:54:21 + * @param index + */ + private void validate(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + /** + * + * 链中元素 + * + * @ClassName Node + * @author zhaogd + * @date 2017年2月21日 下午4:13:10 + */ + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group12/446031103/src/com/coding/basic/List.java b/group12/446031103/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group12/446031103/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/group12/446031103/src/com/coding/basic/Queue.java b/group12/446031103/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3844d9dd24 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +/** + * + * 队列-先进先出 + * + * @ClassName Queue + * @author msh + * @date 2017年2月21日 下午9:29:03 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + /** + * + * 入队列 + * + * @MethodName enQueue + * @author msh + * @date 2017年2月21日 下午9:45:15 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + /** + * + * 离开队列 + * + * @MethodName deQueue + * @author msh + * @date 2017年2月21日 下午9:56:06 + * @return + */ + public Object deQueue(){ + if(isEmpty()) + throw new IndexOutOfBoundsException("size:"+size()); + Object o=elementData.get(0); + elementData.removeFirst(); + return o; + } + /** + * + * 是否为空 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:57:14 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==elementData.size()) + temp= true; + return temp; + } + /** + * + * 队列中元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:57:28 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group12/446031103/src/com/coding/basic/Stack.java b/group12/446031103/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d1c58b671 --- /dev/null +++ b/group12/446031103/src/com/coding/basic/Stack.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +/** + * + * 栈-先进后出 + * + * @ClassName Stack + * @author msh + * @date 2017年2月21日 下午9:05:39 + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + /** + * + * 向栈中加入元素 + * + * @MethodName push + * @author msh + * @date 2017年2月21日 下午9:12:03 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + /** + * + * 从栈中取出元素 + * + * @MethodName pop + * @author msh + * @date 2017年2月21日 下午9:12:51 + * @return + */ + public Object pop(){ + Object o= peek(); + elementData.remove(size()-1); + return o; + } + /** + * + * 取出栈顶元素 + * + * @MethodName peek + * @author msh + * @date 2017年2月21日 下午9:13:08 + * @return + */ + public Object peek(){ + Object o=elementData.get(size()-1); + return o; + } + /** + * + * 判断栈中是否有元素 + * + * @MethodName isEmpty + * @author msh + * @date 2017年2月21日 下午9:14:26 + * @return + */ + public boolean isEmpty(){ + boolean temp = false; + if(0==size()) + temp = true; + return temp; + } + /** + * + * 栈中有多少元素 + * + * @MethodName size + * @author msh + * @date 2017年2月21日 下午9:16:42 + * @return + */ + public int size(){ + return elementData.size(); + } + +}