diff --git a/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java new file mode 100644 index 0000000000..a64cb7cd19 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java @@ -0,0 +1,174 @@ +/** +* @Title: ArrayList.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月22日 下午10:41:58 +*/ +package per.zyf.bds; + +import java.util.Arrays; + +/** + * ArrayList的存储结构其实就是一维数组 + * 只不过在这个类中将对数组的一些操作(包括添加元素、删除元素等)封装了起来 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +public class ArrayList implements List { + // 默认数组大小 + private static final int DEFAULT_CAPACITY = 10; + + // 数组实际大小 + private int size; + + // 存储元素的数组 + protected Object[] elementData; + + // 一个用来记录初始状态的空数组实例 + private static final Object[] CAPACITY_EMPTY_ELEMENTDATA = {}; + + /*** + * 构造初始元素数组 + */ + public ArrayList() { + this.elementData = CAPACITY_EMPTY_ELEMENTDATA; + } + + /*** + * + * @Description: 在末尾添加元素 + * @param e 元素 + */ + @Override + public boolean add(E e) { + int minCapacity = size + 1; + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 添加元素 + elementData[size++] = e; + + return true; + } + + /*** + * + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 元素 + */ + @Override + public boolean add(int index, E e) { + int minCapacity = size + 1; + + // 索引位置不合法抛出异常 + rangeCheck(index); + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 插入点后的元素后移 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + // 在索引处加入数据 + elementData[index] = e; + + return true; + } + + /*** + * + * @Description: 得到索引指定位置的元素 + * @param index 索引 + * @return E 索引指定的元素 + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + return (E) elementData[index]; + } + + /*** + * + * @Description: 删除索引指定位置的元素 + * @param index 索引 + * @return void + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + E removeElement = (E) elementData[index]; + + // 将要移除元素后的元素前移 + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + // 数组大小减一,并清除多余元素 + elementData[--size] = null; + + return removeElement; + } + + /* + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + + /* + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + return true; + } + return false; + } + + /*** + * + * @Description: 溢出时增长空间 + * @param minCapacity 最小容量 + * @return void + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + // 容量增大一半 + int newCapacity = oldCapacity + (oldCapacity >> 1); + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} \ No newline at end of file diff --git a/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java new file mode 100644 index 0000000000..87d7010456 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java @@ -0,0 +1,81 @@ +/** +* @Title: BinaryTree.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月25日 下午10:22:03 +*/ +package per.zyf.bds; + +import java.util.Comparator; + +/** + * @author glorychou + * + */ +public class BinaryTree> { + // 根节点 + private Node root; + // 树大小 + private int size; + + /** + * @Description: 在树中插入元素 + * @param e 节点数据 + * @return boolean 处理情况 + */ + public boolean add(E e) { + + // 创建新节点 + final Node newNode = new Node<>(null, e, null); + + // 按照二叉排序方式插入 + if (root != null) { + Node parentNode = null; + Node compareNode = root; + + while(compareNode != null) { + // 新节点大于比较节点则插入右子树中 + if(e.compareTo(compareNode.item) > 0) { + parentNode = compareNode; + compareNode = compareNode.rightChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } else {// 新节点小于或等于比较节点则插入左子树中 + parentNode = compareNode; + compareNode = compareNode.leftChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } + } + } else + root = newNode; + + return true; + } + + /** + * @Description: 中序遍历输出 + * @return void 返回类型 + */ + public void inorderPrint(Node e) { + if(e == null) return; + inorderPrint(e.leftChild); + System.out.print(e.item.toString() + " "); + inorderPrint(e.rightChild); + } + + // 树节点 + private static class Node { + E item; + Node leftChild; + Node rightChild; + + Node(Node l, E e, Node r) { + leftChild = l; + item = e; + rightChild = r; + } + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java new file mode 100644 index 0000000000..3955ef22ab --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java @@ -0,0 +1,246 @@ +/** +* @Title: LinkedList.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 上午12:23:00 +*/ +package per.zyf.bds; + +/** + * LinkedList的存储结构其实就是链表 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +/** + * @author glorychou + * + * @param + */ +/** + * @author glorychou + * + * @param + */ +public class LinkedList implements List { + // 链表头 + private Node first; + + // 链表尾 + private Node last; + + // 链表大小 + private int size; + + /* + * @see per.zyf.bds.List#add(java.lang.Object) + */ + @Override + public boolean add(E e) { + linkLast(e); + return true; + } + + /* + * @see per.zyf.bds.List#add(int, java.lang.Object) + */ + @Override + public boolean add(int index, E e) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + // 索引值为链表长度,则添加到链表末尾 + if (index == size) { + linkLast(e); + } else { + // 找到索引指定的节点,然后将新节点插入该节点后面 + final Node p = node(index); + final Node newNode = new Node<>(p, e, p.next); + p.next = newNode; + size++; + } + + return true; + } + + /* + * @see per.zyf.bds.List#get(int) + */ + @Override + public E get(int index) { + return node(index).item; + } + + + /* (non-Javadoc) + * @see per.zyf.bds.List#remove(int) + */ + @Override + public E remove(int index) { + rangeCheck(index); + + Node p = node(index); + + // 所需删除节点的前面有节点,则改变前一节点的下一跳 + if(p.prev != null) + p.prev.next = p.next; + // 所需删除节点的后面有节点,则改变后一节点的上一跳 + if(p.next != null) + p.next.prev = p.prev; + + size--; + + return p.item; + } + + /** + * @Description: 在链表头部增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + public void addFirst(E e) { + final Node newNode = new Node<>(null, e, first); + first.prev = newNode; + size++; + } + + /** + * @Description: 移除首节点 + * @return E 所删除节点的数据 + */ + public E removeFirst() { + if(first != null) { + final E e = first.item; + final Node n = first.next; + + if(n != null) + n.prev = null; + + // 清空数据,让GC来回收内存 + first.item = null; + first.next = null; + // 指定新的头节点 + first = n; + + size--; + return e; + } else + return null; + } + + /** + * @Description: 删除最后一个节点 + * @return 设定文件 + * @return E 所删除数据 + * @throws + */ + public E removeLast() { + if(last != null) { + final E e = last.item; + final Node p = last.prev; + + if(p != null) + p.next = null; + + // 清空数据,让GC来回收内存 + last.item = null; + last.prev = null; + // 指定新的尾节点 + last = p; + + size--; + return e; + } else + return null; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + return true; + } + + /** + * @Description: 在链尾增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + private void linkLast(E e) { + // 保存旧链尾节点 + final Node p = last; + + // 创建新节点,并将新节点设置为链尾节点 + final Node newNode = new Node<>(p, e, null); + last = newNode; + + // 若链表无节点,则将新节点设置为表头节点 + if (p == null) + first = newNode; + else // 若链表已经有节点,则将新节点设置为表尾节点 + p.next = newNode; + + // 表长度加一 + size++; + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + + /** + * @Description: 获取索引位置的节点 + * @param index 索引 + * @return Node 指定的节点 + */ + private Node node(int index) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + Node specifyNode; + + // 根据判断索引位置在链表的前半部还是后半部,选择不同的检索方式获取指定节点 + if(index < (size >> 1)) { + specifyNode = first; + for (int i = 0; i < index; i++) + specifyNode = specifyNode.next; + } else { + specifyNode = last; + for (int i = 0; i > index; i--) + specifyNode = specifyNode.prev; + } + + return specifyNode; + } + + // 链表节点 + private static class Node { + E item; + Node next; + Node prev; + + Node(Node prev, E e, Node next) { + this.prev = prev; + this.item = e; + this.next = next; + } + } + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/List.java b/group09/396077060/20170226/src/per/zyf/bds/List.java new file mode 100644 index 0000000000..847edd1013 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/List.java @@ -0,0 +1,55 @@ +/** +* @Title: List.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:02:34 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public interface List { + /** + * @Description: 添加元素 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(E e); + + /** + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(int index, E e); + + /** + * @Description: 获取指定元素 + * @param index 索引 + * @return E 返回元素 + */ + E get(int index); + + /** + * @Description: 删除指定元素 + * @param index 索引 + * @return E 返回被删除的元素 + */ + E remove(int index); + + /** + * @Description: 获取List容量 + * @return int List容量 + */ + int size(); + + /** + * @Description: 判断是否为空 + * @return boolean 是否为空 + */ + boolean isEmpty(); + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Queue.java b/group09/396077060/20170226/src/per/zyf/bds/Queue.java new file mode 100644 index 0000000000..70b72c9b49 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Queue.java @@ -0,0 +1,59 @@ +/** +* @Title: Queue.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:10:08 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Queue { + // 队列元素 + private LinkedList elementData; + + // 队列大小 + private int size; + + public Queue() { + elementData = new LinkedList<>(); + } + + /** + * @Description: 入队 + * @param e 入队数据 + * @return void 返回类型 + */ + public void enQueue(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 出队 + * @return E 出队的数据 + */ + public E deQueue() { + final E e = elementData.removeFirst(); + size = elementData.size(); + return e; + } + + /** + * @Description: 判断队列是否为空 + * @return boolean 是否为空 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取队列大小 + * @return int 队列大小 + */ + public int size() { + return size; + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Stack.java b/group09/396077060/20170226/src/per/zyf/bds/Stack.java new file mode 100644 index 0000000000..bf8d4f5611 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Stack.java @@ -0,0 +1,71 @@ +/** +* @Title: Stack.java +* @Description: TODO(用一句话描述该文件做什么) +* @author glorychou +* @date 2017年2月24日 下午3:05:29 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Stack { + // 栈元素 + private ArrayList elementData; + + // 栈大小 + private int size; + + /** + * 初始化栈 + */ + public Stack() { + elementData = new ArrayList<>(); + } + + /** + * @Description: 压栈 + * @param e 数据 + * @return void 返回类型 + */ + public void push(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 弹栈 + * @return E 所弹出的数据 + */ + public E pop() { + // 移除最后一项数据 + final E e = elementData.remove(elementData.size()); + size = elementData.size(); + return e; + } + + /** + * @Description: 获取栈顶元素 + * @return E 返回栈顶元素数据 + */ + public E peek() { + return elementData.get(size - 1); + } + + /** + * @Description: 判断栈是否为空 + * @return boolean 返回类型 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取栈大小 + * @return int 栈大小 + */ + public int size() { + return size; + } +}