#include <stdio.h>
#include <stdlib.h>
// 定义结点数据类型
typedef struct LNode {
int data;
struct LNode *next;
}LNode;
// 定义链队数据类型
typedef struct {
LNode *front, *rear;
}LiQueue;
void initLiQueue(LiQueue &Q) {
Q.front = Q.rear = (LNode *)malloc(sizeof(LNode));
Q.front->next = NULL;
}
// 判断队列空
bool isEmpty(LiQueue Q) {
if(Q.front == Q.rear)
return true;
return false;
}
// 入队操作
void enQueue(LiQueue &Q, int e) {
LNode *p = (LNode *)malloc(sizeof(LNode));
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
// 出队操作
bool deQueue(LiQueue &Q, int &e) {
if(isEmpty(Q))
return false;
LNode *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
// 如果出队元素是队列的最后一个元素,需要修改尾指针
if(p == Q.rear)
Q.rear = Q.front;
free(p);
return true;
}
int main(void) {
LiQueue Q;
initLiQueue(Q);
enQueue(Q, 1);
enQueue(Q, 2);
enQueue(Q, 3);
int e = -1;
deQueue(Q, e);
printf("出队元素:%d\n", e);
deQueue(Q, e);
printf("出队元素:%d\n", e);
deQueue(Q, e);
printf("出队元素:%d\n", e);
if(isEmpty(Q))
printf("队列已空。\n");
system("pause");
return 0;
}