代码拉取完成,页面将自动刷新
package week_2.practice;
import week_1.practice.EmptyCollectionException;
public class CircularArrayQueue<T> implements QueueADT<T> {
private final int DEFAULT_CAPACITY = 100;
private int front,rear,count;
private T[] queue;
/*Creates an empty queue using the specified capacity.
* @param initialCapacity the initial size of the circular array queue
* */
public CircularArrayQueue(int initialCapacity)
{
front = rear = count = 0;
queue = ((T[])(new Object[initialCapacity]));
}
/*Creates an empty queue using the default capacity.
* @param initialCapacity the initial size of the circular array queue
* */
public CircularArrayQueue(){
queue = ((T[])(new Object[DEFAULT_CAPACITY]));
}
@Override
public void enqueue(T element) {
if(size() == queue.length)
expandCapacity();
}
private void expandCapacity() {
T[] larger = (T[]) (new Object[queue.length*2]);
for(int scan = 0;scan <count; scan++){
larger[scan] = queue[front];
front = (front + 1) % queue.length;
}
front = 0;
rear = count;
queue = larger;
}
@Override
public T dequeue() {
if(isEmpty())
throw new EmptyCollectionException("queue");
T result = queue[front];
queue[rear] = null;
front = (front + 1) % queue.length;
count--;
return result;
}
@Override
public T first() {
return null;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int size() {
return 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。