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