循环队列java实现

时间:2021-06-28 14:34:25
public class SeqHeap
{
Object[] data;
int font;
int rear;
int maxSize; public SeqHeap(int maxSize)
{
this.maxSize = maxSize;
this.data = new Object[this.maxSize];
this.font=0;
this.rear=0;
} public boolean isEmpty()
{
return this.font==this.rear;
} public boolean isFull()
{
return (this.rear+1)%this.maxSize == this.font;
}
public void push(Object e)
{
if(this.isFull()) return ;
this.data[this.rear] = e;
this.rear=(this.rear+1)%this.maxSize;
}
public Object pop()
{
if(this.isEmpty()) return null;
Object o = this.data[this.font];
this.font = (this.font+1)%this.maxSize;
return o;
} public static void main(String[] args)
{
SeqHeap mSeqHeap = new SeqHeap(100);
for(int i=0;i<10;i++)
mSeqHeap.push(new Integer(i+1));
while(mSeqHeap.isEmpty()==false)
System.out.print((Integer)mSeqHeap.pop()+"\t"); }
}