seqcircularqueue.java
来自「基本数据结构」· Java 代码 · 共 50 行
JAVA
50 行
package circularQueue;
public class SeqCircularQueue implements Queue
{
private int front=0,rear=0;
private Object[] queue;
public SeqCircularQueue(int maxSize){queue=new Object[maxSize];}
public void insert(Object o)
{
int temp=rear;
rear=(rear+1)%queue.length;
if (front==rear)
{
rear=temp;
throw new FullQueueException();
}
queue[rear]=o;
}
public Object remove()
{
if (front==rear)throw new EmptyQueueException();
front=(front+1)%queue.length;
return queue[front];
}
public class EmptyQueueException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = -4648041708571387891L;}
public class FullQueueException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = -5227957713299836660L;}
public boolean isEmpty(){return front==rear;}
public boolean isFull(){return ((rear+1)%queue.length)==front;}
public Object getFront()
{
if (front==rear)throw new FullQueueException();
return queue[front];
}
public Object getRear()
{
if (front==rear)throw new EmptyQueueException();
return queue[rear];
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?