#include <stdio.h> #include <stdlib.h> //定义队列结构体 typedef int ElemType; typedef struct node { ElemType elem; struct node *next; } Node; typedef struct queue { Node *front,*rear; }Queue; //创建队列 void Create(Queue *Q) { Node *p=(Node*)malloc(sizeof(Node)); Q->front = p; Q->rear = p; Q->front->next = NULL; printf("队列创建成功!\n"); } //判断队列是否为空 int IsEmpty(Queue *Q) { if(Q->front->next == Q->rear) { printf("队列为空\n"); return 1; } else return 0; } //在队尾插入元素x void EnQueue(Queue *Q,ElemType x) { Node *p=(Node*)malloc(sizeof(Node)); p->elem = x; p->next = NULL; Q->rear->next = p; Q->rear = p; printf("插入元素成功!\n"); } //获取队首元素x int Front(Queue *Q,ElemType *x) { if(IsEmpty(Q)) return 0; Node *p = Q->front->next; *x = p->elem; printf("获取队首元素成功,队首元素为:%d\n",*x); return 1; } //删除队首元素 int DeQueue(Queue *Q) { if(IsEmpty(Q)) return 0; Node *p = Q->front; Q->front = p->next; free(p); printf("删除队首元素成功\n"); return 1; } //打印输出队列元素 int Output(Queue *Q) { if(IsEmpty(Q)) return 0; Node *p = Q->front->next; printf("输出队列:"); while(p) { printf("%d ",p->elem); p = p->next; } printf("\n"); } //销毁队列 void Destory(Queue *Q) { Node *p = Q->front,*q=NULL; while(p) { q = p->next; free(p); p = q->next; } printf("销毁队列成功!\n"); } int main(void) { int i; ElemType x; Queue q; Create(&q); for(i=0;i<5;i++) EnQueue(&q,i); Output(&q); Front(&q,&x); DeQueue(&q); Output(&q); return 0; }