diff --git a/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java b/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6300e92259 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package basic; + +/** + * 二叉树的数据结构 + * @author Wayss + * 2017-02-25 + */ + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(Integer val){ + data = val; + left = null; + right = null; + } + + + 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/group08/1144989424/firstPractice/src/basic/MyArrayList.java b/group08/1144989424/firstPractice/src/basic/MyArrayList.java index 365c2363cb..f9cf5b1b80 100644 --- a/group08/1144989424/firstPractice/src/basic/MyArrayList.java +++ b/group08/1144989424/firstPractice/src/basic/MyArrayList.java @@ -1,5 +1,11 @@ package basic; +/** + * 我的ArrayList实现 + * @author Wayss + * 2017-02-22 + */ + public class MyArrayList implements MyList { private int size = 0; @@ -26,7 +32,7 @@ public void add(Object o){ */ public void add(int index, Object o){ int length = elementData.length; - //0.先对index的值进行判断,不能小于0,且不能大于size + //0.先对index的值进行判断,小于0,或者,大于size,越界 if(index < 0 || index > size){ throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); } @@ -43,15 +49,37 @@ public void add(int index, Object o){ } public Object get(int index){ - return null; + int length = elementData.length; + //0.先对index的值进行判断,小于0,或者,大于等于size,便越界 + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2.拿出index位置的值, + Object o = elementData[index]; + + return o; } public Object remove(int index){ + //remove 前两步的逻辑和get方法相同 + int length = elementData.length; + //0.先对index的值进行判断,小于0,或者,大于等于size,便越界 + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2.拿出index位置的值, + Object o = elementData[index]; + + //3.删除index位置的值,之后的值,向前移动。 + for(int i = index; i < size-1; i++){ + elementData[i] = elementData[i+1]; + } + size--; return null; } public int size(){ - return -1; + return size; } public MyIterator iterator(){ diff --git a/group08/1144989424/firstPractice/src/basic/MyBinaryTreeNode.java b/group08/1144989424/firstPractice/src/basic/MyBinaryTreeNode.java deleted file mode 100644 index 1cad28c4a8..0000000000 --- a/group08/1144989424/firstPractice/src/basic/MyBinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package basic; - -public class MyBinaryTreeNode { - - private Object data; - private MyBinaryTreeNode left; - private MyBinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public MyBinaryTreeNode getLeft() { - return left; - } - public void setLeft(MyBinaryTreeNode left) { - this.left = left; - } - public MyBinaryTreeNode getRight() { - return right; - } - public void setRight(MyBinaryTreeNode right) { - this.right = right; - } - - public MyBinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/group08/1144989424/firstPractice/src/basic/MyLinkedList.java b/group08/1144989424/firstPractice/src/basic/MyLinkedList.java index a62e77bc92..3894884373 100644 --- a/group08/1144989424/firstPractice/src/basic/MyLinkedList.java +++ b/group08/1144989424/firstPractice/src/basic/MyLinkedList.java @@ -1,46 +1,113 @@ package basic; +/** + * 实现LinkedList基本功能 + * @author Wayss + * 2017-02-23 + */ + public class MyLinkedList implements MyList { private Node head; + private int size = 0; public void add(Object o){ - + Node n = new Node(o); + head.next = n; + size++; } public void add(int index , Object o){ - + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找index位置的前一个节点 + //tempNode为当前链表的第一个节点 + Node tempNode = head.next; + for(int i = 0; i < index - 1 ; i++){ + tempNode = tempNode.next; + } + Node behindNode = tempNode.next; + Node insertNode = new Node(o); + tempNode.next = insertNode; + insertNode.next = behindNode; + size++; } public Object get(int index){ - return null; + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找当前节点 + Node tempNode = head.next; + for(int i = 0; i < index; i++){ + tempNode = tempNode.next; + } + return tempNode.data; } public Object remove(int index){ - return null; + //1.index校验 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); + } + //2. 查找当前节点的上一个节点 + Node tempNode = head.next; + for(int i = 0; i < index - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + Node behideNode = tempNode.next.next; + tempNode.next = behideNode; + size--; + return deleteNode.data; } public int size(){ - return -1; + return size; } public void addFirst(Object o){ - + Node insertNode = new Node(o); + insertNode.next = head.next; + head.next = insertNode; + size++; } public void addLast(Object o){ - + Node insertNode = new Node(o); + Node tempNode = head.next; + for(int i = 0; i < size; i++){ + tempNode = tempNode.next; + } + tempNode.next = insertNode; + size++; } public Object removeFirst(){ - return null; + Node firstNode = head.next; + head = firstNode.next; + size--; + return firstNode; } public Object removeLast(){ - return null; + Node tempNode = head.next; + //1.移除需要找到最后一个点的前一个点 + for(int i = 0; i < size - 1; i++){ + tempNode = tempNode.next; + } + Node deleteNode = tempNode.next; + tempNode.next = null; + size--; + return deleteNode; } public MyIterator iterator(){ return null; } - private static class Node{ + private static class Node{ Object data; Node next; - + public Node(Object data){ + this.data = data; + } } } diff --git a/group08/1144989424/firstPractice/src/basic/MyQueue.java b/group08/1144989424/firstPractice/src/basic/MyQueue.java index ee17135a2d..31fab53105 100644 --- a/group08/1144989424/firstPractice/src/basic/MyQueue.java +++ b/group08/1144989424/firstPractice/src/basic/MyQueue.java @@ -1,19 +1,31 @@ package basic; +/** + * 实现队列 + * @author Wayss + * 2017-02-25 + */ + public class MyQueue { - public void enQueue(Object o){ + MyLinkedList linkList = new MyLinkedList(); + + public void enQueue(Object o){ + linkList.addLast(o); } public Object deQueue(){ - return null; + return linkList.removeFirst(); } public boolean isEmpty(){ + if(linkList.size() == 0){ + return true; + } return false; } public int size(){ - return -1; + return linkList.size(); } } diff --git a/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java b/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java new file mode 100644 index 0000000000..a4579217e5 --- /dev/null +++ b/group08/1144989424/firstPractice/src/basic/MySortBinaryTree.java @@ -0,0 +1,81 @@ +package basic; + +import java.util.Comparator; + +/** + * 实现二叉排序树,使左节点的值,始终父节点小,右节点的值,始终比父节点大 + * @author Wayss + * 2017-02-25 + */ + +public class MySortBinaryTree { + + public BinaryTreeNode root = new BinaryTreeNode(); + + /** + * 1. 添加时,先判断与root节点值的大小 + * 2. insert的值小于root的值,则插入到root的左边。 + * 3. insert的值大于root的值,则插入到root的右边。 + * PS:目前只支持Integer类型的对象插入 + * @param val + */ + public void add(Integer val){ + BinaryTreeNode treeNode = new BinaryTreeNode(val); + insert(root, treeNode); + } + + private void insert(BinaryTreeNode node, BinaryTreeNode insertNode){ + + int result = compare((Integer)insertNode.getData(),(Integer)node.getData()); + + if(0 == result){ + //相等的话,当重复数据,不插了.呵呵 + } + //insert的值小于root的值,则插入到root的左边。 + //如果左节点有值,则递归 + if(-1 == result){ + if(node.getLeft() != null){ + insert(node.getLeft(), insertNode); + }else{ + node.setLeft(insertNode); + } + } + //insert的值大于root的值,则插入到root的右边。 + if(1 == result){ + if(node.getRight() != null){ + insert(node.getRight(), insertNode); + }else{ + node.setRight(insertNode); + } + } + } + + public MyArrayList getValue(){ + MyArrayList malist = new MyArrayList(); + getTreeValue(root,malist); + return malist; + } + + private void getTreeValue(BinaryTreeNode node,MyArrayList list){ + //遍历左子数 + if(node.getLeft() != null){ + getTreeValue(node.getLeft(),list); + } + list.add(node.getData()); + //遍历右子数 + if(node.getRight() != null){ + getTreeValue(node.getRight(),list); + } + } + + public static int compare(Integer i1, Integer i2){ + if(i1 < i2){ + return -1; + }else if(i1 == i2){ + return 0; + }else { + return 1; + } + } + +} diff --git a/group08/1144989424/firstPractice/src/basic/MyStack.java b/group08/1144989424/firstPractice/src/basic/MyStack.java index 0460746cf7..c8e8e0d2fd 100644 --- a/group08/1144989424/firstPractice/src/basic/MyStack.java +++ b/group08/1144989424/firstPractice/src/basic/MyStack.java @@ -1,22 +1,33 @@ package basic; +/** + * 栈实现 + * @author Wayss + * 2017-02-25 + */ + public class MyStack { - private MyArrayList elementData = new MyArrayList(); + private MyArrayList arrList = new MyArrayList(); - public void push(Object o){ + public void push(Object o){ + arrList.add(o); } public Object pop(){ - return null; + //elementData.size()-1是当前数组的最后一个元素的下标 + return arrList.remove(arrList.size() - 1); } public Object peek(){ - return null; + return arrList.get(arrList.size() - 1); } public boolean isEmpty(){ + if(arrList.size() == 0){ + return true; + } return false; } public int size(){ - return -1; + return arrList.size(); } }