#ifndef QUEUE
#include <iostream>
using namespace std;
typedef int ElemType;
typedef bool Status;
typedef struct QNode {
ElemType data;
QNode* next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue *Q) {
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q->front)
exit(OVERFLOW);
Q->front->next = NULL;
return 1;
}
Status DestroyQueue(LinkQueue *Q) {
while (Q->front) {
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return 1;
}
Status ClearQueue(LinkQueue *Q) {
QueuePtr p, q;
Q->rear = Q->front;
p = Q->front->next;
Q->front->next = NULL;
while (p) {
q = p;
p = p->next;
free(q);
}
return 1;
}
Status QueueEmpty(LinkQueue *Q) {
if (Q->front == Q->rear)
return 1;
return 0;
}
int QueueLength(LinkQueue Q) {
int n = 0;
QueuePtr p=Q.front->next;
while (p) {
n++;
p = p->next;
}
return n;
}
Status GetHead(LinkQueue Q, ElemType &e) {
e = Q.front->next->data;
return 1;
}
Status EnQueue(LinkQueue *Q, ElemType e) {
QueuePtr p = (QNode *)malloc(sizeof(QNode));
if (!p)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return 1;
}
Status DeQueue(LinkQueue *Q, ElemType &e) {
if (Q->front == Q->rear) return 0;
QueuePtr p = Q->front->next;
e = p->data;
Q->front->next = p->next;
free(p);
return 1;
}
Status QueueTraverse(LinkQueue Q) {
QueuePtr p = Q.front->next;
while (p) {
cout << p->data << " ";
p = p->next;
}
return 1;
}
#endif QUEUE
队列
最新推荐文章于 2023-11-14 11:39:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)