From 75018e04b984364e4fe3f7ba2d0a3f263e2354ce Mon Sep 17 00:00:00 2001 From: JiaMing Q <809203042@qq.com> Date: Tue, 14 Mar 2017 17:43:55 +0800 Subject: [PATCH 1/2] 809203042first homework --- group24/809203042/.classpath | 6 + group24/809203042/.gitignore | 1 + group24/809203042/.project | 17 ++ .../basic/structures/MyArrayList.java | 99 ++++++++++ .../basic/structures/MyBinaryTree.java | 154 ++++++++++++++++ .../basic/structures/MyLinkedList.java | 172 ++++++++++++++++++ .../coding2017/basic/structures/MyList.java | 23 +++ .../coding2017/basic/structures/MyQueue.java | 30 +++ .../coding2017/basic/structures/MyStack.java | 41 +++++ .../basic/structurestest/MyArrayListTest.java | 32 ++++ .../structurestest/MyBinaryTreeTest.java | 23 +++ .../structurestest/MyLinkedListTest.java | 35 ++++ .../basic/structurestest/MyQueueTest.java | 9 + .../basic/structurestest/MyStackTest.java | 25 +++ 14 files changed, 667 insertions(+) create mode 100644 group24/809203042/.classpath create mode 100644 group24/809203042/.gitignore create mode 100644 group24/809203042/.project create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java diff --git a/group24/809203042/.classpath b/group24/809203042/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group24/809203042/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group24/809203042/.gitignore b/group24/809203042/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/809203042/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/809203042/.project b/group24/809203042/.project new file mode 100644 index 0000000000..28fba6c7a5 --- /dev/null +++ b/group24/809203042/.project @@ -0,0 +1,17 @@ + + + 809203042Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java new file mode 100644 index 0000000000..711dbfd0e9 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java @@ -0,0 +1,99 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.Arrays; + +/* + * 根据api中arraylist的方法描述,实现一个自己的arraylist + * 基于数组实现 + * + */ +public class MyArrayList implements MyList { + // 定义一个私有数组,初始长度为10 + private Object[] elementData = new Object[10]; + // 定义变量记录ArrayList的长度 + private int size = 0; + + // 获取指定索引上的元素的值 + @Override + public Object get(int index) { + if (index >= 0 && index < size) { + return elementData[index]; + } else { + throw new IndexOutOfBoundsException("查询的索引不存在"); + } + } +// 向列表尾部添加元素 + @Override + public boolean add(Object obj) { + if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作 + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size] = obj; + size++; + return true; +// 什么时候会返回false,如何返回false? + } +// 向列表指定位置添加元素 + @Override + public boolean add(Object obj,int index) { + if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作 + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } +// 将从index开始的所有元素向后移动一位 + moveBackward(elementData,index,size-1,1); +// 将元素添加到指定位置 + elementData[index] = obj; + size++; + return true; + } + +// 删除指定位置的元素 + @Override + public Object remove(int index) { + if(size == 0){ + throw new IndexOutOfBoundsException("列表为空"); + } + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("想要删除的元素索引不存在"); + } + Object removeElement = elementData[index]; + moveForward(elementData,index+1,size-1,1); + size--; + return removeElement; + } + + @Override + public int size() { + + return size; + } +// 判断列表是否为空 + @Override + public boolean isEmpty() { + + return size == 0; + } +// 将数组从startPos位置到endPos位置的元素向后移动step步长 + private void moveBackward(Object[] elementData, int startPos, int endPos, int step) { + for(int i = endPos + step; i >= startPos + step; i--){ + elementData[i] = elementData[i-step]; + } + + } +// 将数组从startPos位置到endPos位置的元素向前移动step步长 + private void moveForward(Object[] elementData, int startPos, int endPos, int step) { + for(int i = startPos - step; i <= endPos - step; i++){ + elementData[i] = elementData[i+step]; + } + + } + @Override + public String toString() { +// return Arrays.toString(elementData); + Object[] myArrayList = Arrays.copyOf(elementData, size); + return Arrays.toString(myArrayList); + + } + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java new file mode 100644 index 0000000000..c5109cb954 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java @@ -0,0 +1,154 @@ +package com.github.qq809203042.coding2017.basic.structures; + + +/* + * 实现二叉树, + * 只存储int类型 + * 内部类代表每个节点:每个节点含有一个键,一个值,一条左链接,一条右链接 + * + * 实现插入值操作:当root为空时插在root上:确定某一节点上是否为空 + * + */ +public class MyBinaryTree { + private BinaryTreeNode root ; + private int size; + +// 构造函数 + public MyBinaryTree(){ + + } + + public boolean getValue(Integer key){ + return getValue(root,key); + } + + private boolean getValue(BinaryTreeNode node,Integer key){ + if(node == null){//如果为空,则为空,没有该元素 + return false; + } + Integer value = node.getValue(); + if(value == key){//找到 + return true; + } + else if(value.compareTo(key) > 0){//如果小于该节点对象,比较左节点 + return getValue(node.left,key); + }else{ + return getValue(node.right,key);//如果小于该节点对象,比较右节点 + } + + } + + public void add(Integer key){ + root = add(root, key); + + } + + private BinaryTreeNode add(BinaryTreeNode node, Integer key) { + if(node == null){//若没有该节点,则创建并添加数据至节点中 + node = new BinaryTreeNode(key); + size++; + return node; + } + Integer value = node.getValue(); + if(value.compareTo(key) > 0){ + node.left =add(node.left,key); + }else if(value.compareTo(key) < 0){ + node.right=add(node.right,key); + } + return node; + } + + public int size(){ + return size; + } + + +// 前序遍历 + public void preOrder(){ + preOrder(root); + System.out.println(); + } +// 中序遍历 + public void midOrder(){ + midOrder(root); + System.out.println(); + } +// 后序遍历 + public void aftOrder(){ + aftOrder(root); + System.out.println(); + } + +// 前序遍历 + private void preOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + System.out.print(node.value + " "); + preOrder(node.left); + preOrder(node.right); + + } +// 中序遍历 + private void midOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + midOrder(node.left); + System.out.print(node.value + " "); + midOrder(node.right); + + } +// 后序遍历 + private void aftOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + aftOrder(node.left); + aftOrder(node.right); + System.out.print(node.value + " "); + + } + + + + // 二叉树的节点对象:每个节点含有一个键,一个值,一条左链接,一条右链接和一个节点计数器 + private static class BinaryTreeNode { + private Integer value; + + private BinaryTreeNode left; + private BinaryTreeNode right; +// 构造函数 + BinaryTreeNode(Integer value){ + this.value = value; + } + + + + public Integer getValue() { + return value; + } + public void setValue(Integer value) { + this.value = value; + } + + 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(Integer value) { + this.value = value; + return null; + } + + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java new file mode 100644 index 0000000000..79f80df63e --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java @@ -0,0 +1,172 @@ +package com.github.qq809203042.coding2017.basic.structures; + +/* + * 根据API实现自己的LinkedList数据结构 + */ +public class MyLinkedList implements MyList { + // 头节点对象,不存储元素,用于指示链表的起始位置 + private Node head = new Node(null,null); + // 尾节点对象 + private Node last = new Node(null,null); + // 链表长度 + int size = 0; + + @Override + public Object get(int index) { + Node node = node(index); + return node.data; + } + + @Override + public boolean add(Object obj) { + addLast(obj); + return true; + } + + @Override + public boolean add(Object obj, int index) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("下标越界"); + } + if(index == size){//等同于添加到末尾 + addLast(obj); + return true; + } + Node nodeIndex = node(index);//获取index处的节点 + Node newNode = new Node(obj,nodeIndex);//新增元素的next指向原来index处 + if(index == 0){ + head.next = newNode; + }else{ + Node nodeBeforeIndex = node(index-1); + nodeBeforeIndex.next = newNode; + } + size++; + return true; + } + + @Override + public Object remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("下标越界"); + } + if(index == 0){ + return removeFirst(); + }else{ + Node nodeIndex = node(index); + Object removeData = nodeIndex.data; + node(index-1).next = nodeIndex.next; + size--; + return removeData; + } + } + + @Override + public int size() { + + return size; + } + + @Override + public boolean isEmpty() { + + return size == 0; + } + + public void addFirst(Object obj) { + // 创建一个临时节点存储第一个节点 + Node tempFirst = head.next; + // 创建所需添加元素的节点对象 + Node newNode = new Node(obj, tempFirst); + head.next = newNode; + if (tempFirst == null) {// 如果链表中没有元素,则将添加的节点同时赋值为尾节点 + last = newNode; + } + size++; + } + + public void addLast(Object obj) { + // 创建一个临时节点存储尾节点 + Node tempLast = last; + // 创建所需添加元素的节点对象 + Node newNode = new Node(obj, null); + last = newNode; + if (size == 0) {// 如果链表中没有元素,则将添加的节点同时赋值为头节点 + head.next = newNode; + } else {// 如果链表中原先存在元素,则将原来尾节点的next指向现在的节点 + tempLast.next = newNode; + } + size++; + + } + + public Object removeFirst() { + if(size == 0){ + throw new NullPointerException("链表为空"); + } + Node removeNode =head.next; + Object removeData = removeNode.data; + head.next = removeNode.next; + size--; + return removeData; + } + + public Object removeLast() { + if(size <= 1){ + return removeFirst(); + }else{ + Object removeData = last.data; + last = node(size-2); + last.next = null; + size--; + return removeData; + } + } + + + + + @Override + public String toString() { + if(size == 0){ + return "[]"; + } + StringBuffer listToString = new StringBuffer(); + listToString.append("["); + Node node = head; + for(int i = 0; i < size;i++){ + node = node.next; + listToString.append(node.data); + if(i= size){ + throw new IndexOutOfBoundsException("下标越界"); + } + Node node = head.next; + for(int i = 1; i <= index; i++){ + node = node.next; + } + return node; + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java new file mode 100644 index 0000000000..13bd0920c5 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java @@ -0,0 +1,23 @@ +package com.github.qq809203042.coding2017.basic.structures; +/* + * 自定义list接口 + */ +public interface MyList { +// 查 + public abstract Object get(int index); + +// 增 + public abstract boolean add(Object obj); +// 在指定位置增 + public abstract boolean add(Object obj,int index); + +// 删除 + public abstract Object remove(int index); + +// 判断大小 + public abstract int size(); +// 判断是否为空 + public abstract boolean isEmpty(); + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java new file mode 100644 index 0000000000..cced80f1e8 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java @@ -0,0 +1,30 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.LinkedList; + +/* + * 实现队列的结构,使用链表结构 + */ +public class MyQueue { + private LinkedList queueList = new LinkedList<>(); + private int size; +// 进入队列 + public void enQueue(Object obj){ + queueList.addLast(obj); + size++; + } + + public Object deQueue(){ + Object removeElement = queueList.removeFirst(); + size--; + return removeElement; + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java new file mode 100644 index 0000000000..5657c91b50 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java @@ -0,0 +1,41 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.ArrayList; + +public class MyStack { + private ArrayList elementData = new ArrayList(); +// 栈指针 + private int pos; +// 压栈 + public Object push(Object obj) { + elementData.add(obj); + pos++; + return obj; + } +// 弹栈 + public Object pop() { + if(isEmpty()){ + throw new StackOverflowError("栈溢出"); + } + return elementData.remove(--pos); + } +// 返回栈顶对象 + public Object peek() { + int topIndex = pos -1; + return elementData.get(topIndex); + } + + public boolean isEmpty() { + return pos == 0; + } + + public int size() { + return pos; + } + @Override + public String toString() { + return elementData.toString(); + } + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java new file mode 100644 index 0000000000..cda34461ea --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java @@ -0,0 +1,32 @@ +package com.github.qq809203042.coding2017.basic.structurestest; +import com.github.qq809203042.coding2017.basic.structures.MyArrayList; + + /* + * 用于测试MyArrayList + */ + +public class MyArrayListTest { + + public static void main(String[] args) { + MyArrayList mList = new MyArrayList(); + mList.add(new String("hahah")); + mList.add(new String("heihei")); + mList.add(new String("xixi")); + mList.add(new String("papapa")); + mList.add(new String("xiaoqiang")); + mList.add(new String("xiaoming")); + + System.out.println(mList); + System.out.println(mList.get(0)); + System.out.println(mList); + System.out.println(mList.add(new String("新元素"),3)); + System.out.println(mList); + System.out.println(mList.remove(0)); + System.out.println(mList); + System.out.println(mList.isEmpty()); + System.out.println(mList); + + System.out.println(mList.size()); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java new file mode 100644 index 0000000000..3e62719570 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java @@ -0,0 +1,23 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyBinaryTree; + +public class MyBinaryTreeTest { + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree(); + tree.add(5); + tree.add(2); + tree.add(7); + tree.add(1); + tree.add(6); + tree.add(4); + tree.add(8); + + System.out.println(tree.size()); + tree.preOrder(); + tree.midOrder(); + tree.aftOrder(); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java new file mode 100644 index 0000000000..a13b940a3c --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java @@ -0,0 +1,35 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyLinkedList; + +/* + * 用于测试MyLinkedList类 + */ +public class MyLinkedListTest { + + public static void main(String[] args) { + MyLinkedList mList = new MyLinkedList(); + System.out.println(mList.add(new String("hahah"))); + System.out.println(mList.add(new String("heihei"))); + System.out.println(mList.add(new String("xixi"))); + System.out.println(mList.add(new String("papapa"))); + System.out.println(mList.add(new String("xiaoqiang"))); + System.out.println(mList.add(new String("xiaoming"))); + + System.out.println(mList.size()); + System.out.println(mList); + System.out.println(mList.get(0)); + System.out.println(mList); + System.out.println(mList.add(new String("新元素"),0)); + mList.addFirst(new String("新元素2")); + mList.addLast(new String("新元素3")); + + System.out.println(mList.size()); + System.out.println(mList); + System.out.println(mList.remove(5)); + System.out.println(mList); + + System.out.println(mList.size()); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java new file mode 100644 index 0000000000..c9d25c9c4b --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java @@ -0,0 +1,9 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +public class MyQueueTest { + + public static void main(String[] args) { + + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java new file mode 100644 index 0000000000..cd32a0a50e --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java @@ -0,0 +1,25 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyStack; + +public class MyStackTest { + + public static void main(String[] args) { + MyStack ms = new MyStack(); + ms.push(new String("yi")); + ms.push(new String("er")); + ms.push(new String("san")); + ms.push(new String("si")); + ms.push(new String("wu")); + ms.push(new String("liu")); + + System.out.println(ms); + ms.pop(); + System.out.println(ms); + ms.pop(); + System.out.println(ms); + System.out.println(ms.peek()); + System.out.println(ms.size()); + } + +} From e05a0fb5df1455e2ae1e899e75a4cfa8f8ecc337 Mon Sep 17 00:00:00 2001 From: JiaMing Q <809203042@qq.com> Date: Tue, 14 Mar 2017 23:11:08 +0800 Subject: [PATCH 2/2] my first homework --- .../coding2017/basic/structures/MyArrayList.java | 2 ++ .../coding2017/basic/structures/MyBinaryTree.java | 6 ++++-- .../coding2017/basic/structurestest/MyArrayListTest.java | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java index 711dbfd0e9..b5157e1030 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java @@ -58,6 +58,8 @@ public Object remove(int index) { } Object removeElement = elementData[index]; moveForward(elementData,index+1,size-1,1); +// 最后一位置为null;!! + elementData[size-1] = null; size--; return removeElement; } diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java index c5109cb954..8d57321a1e 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java @@ -51,9 +51,11 @@ private BinaryTreeNode add(BinaryTreeNode node, Integer key) { } Integer value = node.getValue(); if(value.compareTo(key) > 0){ - node.left =add(node.left,key); + node.setLeft(add(node.left,key)); + }else if(value.compareTo(key) < 0){ - node.right=add(node.right,key); + node.setRight(add(node.right,key)); + } return node; } diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java index cda34461ea..897fc596b2 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java @@ -1,4 +1,8 @@ package com.github.qq809203042.coding2017.basic.structurestest; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + import com.github.qq809203042.coding2017.basic.structures.MyArrayList; /* @@ -8,6 +12,8 @@ public class MyArrayListTest { public static void main(String[] args) { + + MyArrayList mList = new MyArrayList(); mList.add(new String("hahah")); mList.add(new String("heihei"));