实验08 队列(循环队列)的表示和实现(4)
发布时间:2021-06-06
发布时间:2021-06-06
}
SeqQueue.h
struct Queue{
ElemType *queue;
int front,rear,len;
int MaxSize;
};
void InitQueue(Queue &Q)
{
Q.MaxSize=10;
Q.queue=new ElemType[Q.MaxSize];
Q.front=Q.rear=0;
}
void EnQueue(Queue &Q,ElemType item)
{
if((Q.rear+1)%Q.MaxSize==Q.front){
int k=sizeof(ElemType);
Q.queue=(ElemType*)realloc(Q.queue,2*Q.MaxSize*k); if(Q.rear!=Q.MaxSize-1){
for(int i=0;i<=Q.rear;i++)
Q.queue[i+Q.MaxSize]=Q.queue[i]; Q.rear+=Q.MaxSize;
}
Q.MaxSize=2*Q.MaxSize;
}
Q.rear=(Q.rear+1)%Q.MaxSize;
Q.queue[Q.rear]=item;
}
ElemType OutQueue(Queue &Q)
{
if(Q.front==Q.rear){
cerr<<"队列已空,无法删除!"<<endl;
exit(1);
}
Q.front=(Q.front+1)%Q.MaxSize;
return Q.queue[Q.front];
}
ElemType PeekQueue(Queue &Q)
{
上一篇:星火英语 四级音频讲解提纲