#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
Node *next;
};
struct Queue {
Node* head;
Node* rear;
};
Queue* creatQueue() {
Queue *Q = new Queue;
Node *node = (Node*)malloc(sizeof(Node));
node->next = NULL;
Q->head = Q->rear = node;
return Q;
}
void enQueue(Queue *Q, int d) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = d;
node->next = NULL;
Q->rear->next = node;
Q->rear = node;
}
void output(Queue *Q) {
int data;
if (Q->head == Q->rear) {
printf("队列下溢!");
}
Node *node = Q->head->next;
data = node->data;
Q->head->next = node->next;
if (node->next == NULL) {
Q->rear = Q->head;
}
free(node);
}
void printQueue(Queue *Q) {
Node *node = Q->head->next;
puts("队列成员如下
");
while (node != NULL) {
printf("%d
", node->data);
node = node->next;
}
putchar('
');
}
void destroyQueue(Queue *Q) {
Node *node;
while (Q->head) {
node = Q->head->next;
free(Q->head);
Q->head = node;
}
free(Q);
}
int main(int argc,char** argv) {
int select,d;
Queue *Q = creatQueue();
while (1)
{
puts("1,入队
2,出队
3,遍历
0,退出
请输入操作号:");
scanf("%d", &select);
switch (select)
{
case 1:
puts("请输入需要入队的数字:");
scanf("%d", &d);
enQueue(Q, d);
break;
case 2:
output(Q);
break;
case 3:
printQueue(Q);
break;
case 0:
printf("程序已退出
");
exit(1);
break;
default:
puts("输入错误!
");
break;
}
}
destroyQueue(Q);
}