2017-2018-1 20162307 队列加分项
要求
1 用实现循环队列
2 参考PPT用循环队列打印杨辉三角
3 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息
4 把代码推送到代码托管平台
5 把完成过程写一篇博客:重点是单步跟踪过程和遇到的问题及解决过程
6 提交博客链接
解题步骤
- 1.查看PPT
1.想象队列为一个环形,让【0】在【N-1】之后
2.分析杨辉三角N行元素与N-1之间关系
-
2.参考15.6 CircularArrayQueue
public class CircularArrayQueue<T> implements Queue<T> { private final int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; private T element; public CircularArrayQueue() { front = rear = count = 0; queue = (T[]) (new Object[DEFAULT_CAPACITY]); } public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = ( (T[])(new Object[initialCapacity]) ); } public void enqueue (T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } public T getElement() { return element; } public boolean isEmpty() { return (count == 0); } public int size() { return count; } public String toString() { String result = ""; int scan = 0; while(scan < count) { if(queue[scan]!=null) { result += queue[scan].toString()+" "; } scan++; } return result; } public void expandCapacity() { T[] larger = (T[])(new Object[queue.length *2]); for(int scan=0; scan < count; scan++) { front=(front+1) % queue.length; } front = 0; rear = count; queue = larger; } public T[] first() { return queue.clone (); } }