diff --git a/group17/240094626/.gitignore b/group17/240094626/.gitignore new file mode 100644 index 0000000000..e59106a7ab --- /dev/null +++ b/group17/240094626/.gitignore @@ -0,0 +1,22 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders + +*.settings +*.project +*.classpath +*/.settings +/**/target/**/* diff --git a/group17/240094626/warm-up/.gitignore b/group17/240094626/warm-up/.gitignore new file mode 100644 index 0000000000..c7b6e44e1d --- /dev/null +++ b/group17/240094626/warm-up/.gitignore @@ -0,0 +1,4 @@ +/bin/ +*.classpath +*.project +/.settings/ diff --git a/group17/240094626/warm-up/src/com/coding/basic/Iterator.java b/group17/240094626/warm-up/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group17/240094626/warm-up/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/group17/240094626/warm-up/src/com/coding/basic/List.java b/group17/240094626/warm-up/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group17/240094626/warm-up/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/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java b/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java new file mode 100644 index 0000000000..c20eb376d4 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java @@ -0,0 +1,166 @@ +package com.coding.basic.impl; + +import java.util.Arrays; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +/** + * + * @描述: ArrayList简单实现 + * @作者:240094626 + * @创建日期:2017-2-20 + */ +public class ArrayList implements List { + + + /** + * @comment:元素数组 + */ + private Object data[] = null; + + /** + * @comment:数组元素个数 + */ + private int size = 0; + + /** + * 无参构造函数,初始化容量为10的空列表 + */ + public ArrayList(){ + this(10); + } + + /** + * @param length + * 构造函数,初始化容量为length的空列表 + */ + public ArrayList(int length){ + if(length < 0){ + throw new IllegalArgumentException("初始容量参数非法:"+length); + } + data = new Object[length]; + } + + + /** + * @createTime: 2017-2-21 下午1:32:28 + * @param length + * @return:void + * @comment:列表结构扩展容量,每次增加原来的1/2容量 + */ + private void grow(int length){ + int oldLength = data.length; + if(length > oldLength){ + Object oldData[] = data; + int newLength = oldLength*3/2 + 1; + if(newLength < length){ + newLength = length; + } + data = new Object[newLength]; + System.arraycopy(oldData, 0, data, 0, oldLength); + } + } + + /** + * @createTime: 2017-2-21 下午1:32:05 + * @param index + * @return:void + * @comment:检验下标参数是否超限 + */ + private void check(int index) { + if( index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); + } + } + + @Override + public void add(Object o) { + grow(size+1); + data[size++]=o; + } + + @Override + public void add(int index, Object o) { + if( index > size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index); + } + grow(size+1); + System.arraycopy(data, index, data, index+1, size-index); + data[index] = o; + size++; + + } + + @Override + public Object get(int index) { + check(index); + return data[index]; + } + + + + @Override + public Object remove(int index) { + check(index); + Object remove = data[index]; + System.arraycopy(data, index+1, data, index, size-index); + data[--size] = null; + return remove; + } + + @Override + public int size() { + return size; + } + + + @Override + public String toString() { + return "ArrayList [data=" + Arrays.toString(data) + ", size=" + size + + "]"; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + /** + * @描述: 简单实现迭代器 + * @作者:240094626 + * @创建日期:2017-2-21 + */ + private class ArrayListIterator implements Iterator{ + + /** + * @column:index + * @comment:当前位置下标 + */ + private int index; + + /** + * 无参构造,初始化迭代器的下标为0 + */ + public ArrayListIterator(){ + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o = get(index++); + return o; + } + + } + +} + + diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java b/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java new file mode 100644 index 0000000000..d9478146bd --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java @@ -0,0 +1,82 @@ +package com.coding.basic.impl; + + +/** + * 二叉树简单实现(key为int类型) + * @author 240094626 + * + */ +public class BinaryTree { + private Node rootNode = null; + + public Node insert(int key){ + return insert(key,null); + } + + public Node insert(int key ,Object o){ + Node newNode = new Node(key, o); + if(rootNode == null){ + rootNode = newNode; + return rootNode; + } + Node fatherNode = rootNode; + Node currentNode = rootNode; + while(currentNode != null){ + fatherNode = currentNode; + if(key < currentNode.key){ + currentNode = currentNode.left; + }else{ + currentNode = currentNode.right; + } + } + if(key < fatherNode.key){ + fatherNode.left = newNode; + }else{ + fatherNode.right = newNode; + } + return newNode; + } + + public Node getNode(int key){ + return get(rootNode, key); + } + + private Node get(Node n,int key){ + if(n == null){ + return null; + } + if(key < n.key){ + return get(n.left, key); + }else if(key > n.key){ + return get(n.left, key); + } + return null; + } + + + + + private static class Node{ + + int key; + Object data; + Node left; + Node right; + + public Node(int key, Object data) { + this.key = key; + this.data = data; + this.left = null; + this.right = null; + } + + @Override + public String toString() { + return "Node [key=" + key + ", data=" + data + "]"; + } + + + } + + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java b/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java new file mode 100644 index 0000000000..a48b9e4705 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java @@ -0,0 +1,184 @@ +package com.coding.basic.impl; + +import java.util.NoSuchElementException; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +/** + * 双向链表简单实现 + * @author 240094626 + */ +public class LinkedList implements List { + /**头节点(空的)*/ + private Node header = new Node(null, null, null); + /**链表节点长度*/ + private int size = 0; + + + /** + * 无参构造函数,初始化header节点,前后均指向header节点,形成环形链表 + * 环形链表:为了使链表节点的开头是header,结尾也是header; + * 由于实现了List,那么链表就是有序的,根据下标查询时可借助环形特点双向查找,提升效率; + */ + public LinkedList() { + header.next = header.pre = header; + } + + + /** + * 将Object o 添加到 节点n之前 + * @param o + * @param n + */ + private void addBefore(Object o, Node n) { + Node newNode = new Node(o, n.pre, n); + newNode.next.pre = newNode; + newNode.pre.next = newNode; + size++; + } + + /** + * 根据下标位置查找结点 + * @param index + * @return + */ + private Node getNode(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("index:"+index); + } + // 查找从header开始 + Node n = header; + if(index < (size >> 1)){ + // 往next方向找第index个节点 + for(int i=0; i < index; i++){ + n = n.next; + } + }else{ + // 往pre方向找第size-index个节点 + for(int i=size-index; i > 0; i--){ + n = n.pre; + } + } + return n; + } + + + /** + * 移除节点,从当前节点的前后节点间删除当前节点 + * @param n + * @return + */ + private Object remove(Node n){ + if(n == header){ + throw new NoSuchElementException("未找到节点"); + } + Object result = n.data; + n.pre.next = n.next; + n.next.pre = n.pre; + n.next = n.pre = null; + n.data = null; + size--; + return result; + } + + @Override + public void add(Object o) { + // 默认往header前添加 + addBefore(o,header); + } + + + @Override + public void add(int index, Object o) { + addBefore(o,index==size?header:getNode(index)); + } + + @Override + public Object get(int index) { + Node n = getNode(index); + return n.data; + } + + + @Override + public Object remove(int index) { + return remove(getNode(index)); + } + + + @Override + public int size() { + return size; + } + /** + * 环形链表结构,header.next就是第一个节点 + * @param o + */ + public void addFirst(Object o){ + addBefore(o, header.next); + } + /** + * 环形链表结构,header.pre就是最后一个节点 + * @param o + */ + public void addLast(Object o){ + addBefore(o, header); + } + public Object removeFirst(){ + return remove(header.next); + } + public Object removeLast(){ + return remove(header.pre); + } + + public Iterator iterator(){ + + return new LinkedListIterator(); + } + + private static class Node{ + Object data; + Node pre; + Node next; + + /** + * 链表节点,带参构造函数 + * @param data 节点内容 + * @param pre 上一个节点 + * @param next 下一个节点 + */ + public Node(Object data, Node pre, Node next) { + super(); + this.data = data; + this.pre = pre; + this.next = next; + } + + } + + private class LinkedListIterator implements Iterator{ + int index ; + + public LinkedListIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public Object next() { + Node n = getNode(index++); + return n.data; + } + + } + + +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java b/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java new file mode 100644 index 0000000000..3a9aa129e2 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java @@ -0,0 +1,67 @@ +package com.coding.basic.impl; + +import com.coding.basic.Iterator; + +/** + * 队列简单实现 + * @author 240094626 + * + */ +public class Queue { + /**队列元素容器对象*/ + LinkedList elementData = new LinkedList(); + + /** + * 入队列 + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + } + + /** + * 出队列:先进先出,故取出链表首个节点 + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + if(elementData.size() > 0 ){ + return false; + } + return true; + } + + public int size(){ + return elementData.size(); + } + + public Iterator iterator(){ + return new QueueIterator(); + } + + private class QueueIterator implements Iterator{ + + int index; + + public QueueIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < elementData.size()){ + return true; + } + return false; + } + + @Override + public Object next() { + return elementData.get(index++); + } + + } +} diff --git a/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java b/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java new file mode 100644 index 0000000000..d4c3158f94 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java @@ -0,0 +1,78 @@ +package com.coding.basic.impl; + +import com.coding.basic.Iterator; + +/** + * 栈的简单实现 + * @author 240094626 + */ +public class Stack { + /**长度可变的元素容器*/ + private ArrayList elementData = new ArrayList(); + + /** + * 压入栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 出栈(末尾元素),并移除 + * @return Object + */ + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + /** + * 取出栈(末尾元素),不移除 + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + /** + * 判断栈是否为空 + * @return boolean + */ + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + /** + * 栈的长度,既是容器ArrayList的长度 + * @return int + */ + public int size(){ + return elementData.size(); + } + + public Iterator iterator(){ + return new StackIterator(); + } + private class StackIterator implements Iterator{ + int index; + + public StackIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if(index < elementData.size()){ + return true; + } + return false; + } + + @Override + public Object next() { + return elementData.get(index); + } + + + } +} \ No newline at end of file