顺序队列:
在顺序队列中有queueElem,front,rear,maxSize
queueElem代表队列的存储空间
front代表队首的位置
rear代表队尾的位置的下一个位置。
maxSize代表最多放存储个数。
public class SqQuene implements Iqueue{
private Object[] queneElem;
private int front;
private int rear;
private int maxSize;
public SqQuene(int maxSize){
front=rear=0;
queneElem=new Object[maxSize];
this.maxSize=maxSize;
}
@Override
public void clear() {
front=rear=0;
}
@Override
public boolean isempty() {
return rear==front;
}
@Override
public int length() {
return rear=front;
}
@Override
public Object peek() {
if(isempty()){
return null;}
else {
return queneElem[front];
}
}
@Override
public void offer(Object x) throws Exception {
if(rear==maxSize)throw new Exception("队列满");
queneElem[rear]=x;
rear++;
}
@Override
public Object poll() {
if (rear==front)
return null;
Object p=queneElem[front];
front++;
return p;
}
@Override
public void display() {
if(!isempty()){
for(int i=front;i<rear;i++){
System.out.println(queneElem[i]+"");
}
}
else {
System.out.println("队列为空");
}
}
}
public static void main(String[] args) throws Exception {
SqQuene s1=new SqQuene(20);
if(s1.isempty()){
s1.offer("姓名:王昊");
s1.offer("姓名:孔亚生");
s1.offer("姓名:zx");
s1.display();
System.out.println(s1.peek());
s1.poll();
s1.display();
}
}
标签:顺序,队列,Object,Override,maxSize,front,public,rear
From: https://www.cnblogs.com/JK8395/p/16806639.html