2 Star 0 Fork 0

CS-IMIS-23/20172328lxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CircularArrayQueue.java 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172328lxy.git
git@gitee.com:CS-IMIS-23/20172328lxy.git
CS-IMIS-23
20172328lxy
20172328lxy
master

搜索帮助