链队列的类型定义:
#define MAXQSIZE 100//最大队列长度
typedef struct Qnode{
QElemType data;
struct Qnode *next;
}Qnode, QueuePtr;
typedef struct{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
链队列的操作:
(1)初始化
Status InitQueue(LinkQueue &Q){
Q.front = Q.rear = (QueuePtr) malloc(sizeof (QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next =NULL;
return OK;
}
(2)销毁链队列
Status DestroyQueue(LinkQueue &Q){
while(Q.front){
p=Q.front->next; free(Q.front); Q.front=p;
}
return OK;
}
(3)将元素e入队
Status EnQueue(LinkQueue &Q,QElemType e){
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
(4)链队列出队
Status DeQueue(LinkQueue &Q,QElemType &e){
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete p;
return OK;
}
(5)求链队列的队头元素
Status GetHead(LinkQueue &Q,QElemType &e){
if(Q.front==Q.rear) return ERROR;
e=Q.front->next->data;
return OK;
}
