#include "linkQueue.h"
Status InitQueue(LinkQueue &Q){
//初始化队列操作
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.rear){
exit(OVERFLOW);
}
Q.front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q){
//销毁队列操作
while (Q.front)
{
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e){
//入队列(队尾)操作
QueuePtr q = (QueuePtr)malloc(sizeof(QNode));
if (!q) exit(OVERFLOW);
q->data = e;
q->next = NULL;
Q.rear->next = q;
Q.rear = q;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e){
//出队列(队头)操作
if (Q.front == Q.rear) return ERROR;
QueuePtr q = Q.front->next;
e = q->data;
Q.front->next = q->next;
if (Q.rear == q) Q.rear = Q.front;
free(q);
return OK;
}
Status ClearQueue(LinkQueue &Q){
//清空队列
QElemType e;
while (!QueueEmpty(Q))
{
DeQueue(Q,e);
}
return OK;
}
Status QueueEmpty(LinkQueue Q){
//判断队列为空
if (Q.rear == Q.front)
return OK;
return ERROR;
}
Status GetHead(LinkQueue Q, QElemType &e){
//获取队头元素
if (Q.rear == Q.front)
return ERROR;
e = Q.front->next->data;
return e;
}
Status QueueTraverse(LinkQueue Q){
//遍历队列
QueuePtr p = Q.front->next;
QElemType e;
while (p)
{
e = p->data;
printf("%d ", e);
p = p->next;
}
printf("\n");
return OK;
}
int QueueLength(LinkQueue Q){
//返回队列的长度
int length=0;
if (Q.front == Q.rear)
return 0;
QueuePtr p=Q.front->next;
while (p){
length++;
p = p->next;
}
return length;
}
链队列的(9个基本操作)实现
最新推荐文章于 2021-10-04 18:41:07 发布