#include <iostream>
using namespace std;
struct QueueNode{
int data;
QueueNode* next;
};
struct LinkQueue{
QueueNode* top;
QueueNode* rear;
};
void InitQueue(LinkQueue*& Q) {
Q = new LinkQueue;
Q->top = Q->rear = new QueueNode; //设头结点便于操作
Q->top->next = 0;
}
int Push(LinkQueue*& Q, int x) {
QueueNode* p = new QueueNode;
if (p == 0) return 0;
p->data = x;
p->next = 0;
Q->rear->next = p;
Q->rear = p;
return 1;
}
int Pop(LinkQueue*& Q) {
if (Q->rear == Q->top) return 0;
QueueNode* p = Q->top->next;
if (p->next == 0) {
Q->top->next = 0;
Q->rear = Q->top;
}
else {
Q->top->next = p->next;
}
delete p;
return 1;
}
int GetTop(LinkQueue* Q,int &x) { //得到队头元素
if (Q->rear == Q->top) return 0;
x = Q->top->next->data;
return 1;
}
bool IsEmpty(LinkQueue* Q) { //判断列表是否为空
if (Q->rear == Q->top)
return true;
return false;
}
void Print(LinkQueue* Q) { //打印队列
QueueNode* p = Q->top->next;
while (p){
if (p != Q->top->next) cout << ' ';
cout << p->data;
p = p->next;
}
cout << endl;
}
int main() {
LinkQueue* Q;
InitQueue(Q);
Push(Q, 1);
Push(Q, 2);
Push(Q, 3);
Push(Q, 4);
Push(Q, 5);
Print(Q);
cout << IsEmpty(Q) << endl;
int x;
GetTop(Q, x);
cout << x << endl;
Pop(Q);
Print(Q);
Pop(Q);
Pop(Q);
Pop(Q);
Pop(Q);
Pop(Q);
cout << IsEmpty(Q);
return 0;
}