队列的顺序存储类型
typedef struct{
ElemType data[Maxsize];
int front,rear;
}SqQueue;
队列的初始化
Status InitQuene(SqQueue &Q)
{
Q.rear = Q.front = 0;
return OK;
}
队列长度
int QueueLength(SqQueue Q) //求队列长度
{
return (Q.rear-Q.front+Maxsize)%Maxsize; //可能会出现rear < front的情况
}
队列判空
Status isEmpty(SqQueue &Q)
{
if(Q.rear == Q.front)
return TRUE;
return FALSE;
}
入队
Status EnQuene(SqQueue &Q, ElemType e) //入队列
{
if((Q.rear+1)%Maxsize == Q.front)
return FALSE;
Q.data[Q.rear] = e;
Q.rear = (Q.rear+1)%Maxsize;
return TRUE;
}
出队
Status DeQuene(SqQueue &Q, ElemType &e) //出队列
{
if(Q.rear == Q.front)
return FALSE;
e = Q.data[Q.front];
Q.front = (Q.front+1)%Maxsize;
return TRUE;
}