#include<stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue *Q){
Q->front=Q->rear=(QNode*)malloc(sizeof(QNode));
if(Q->front==NULL) exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
int EnQueue(LinkQueue *Q,QElemType 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 OK;
}
QElemType DeQueue(LinkQueue *Q){
QueuePtr p=(QNode*)malloc(sizeof(QNode));
QElemType e;
if(Q->front==Q->rear) return ERROR;
p=Q->front->next; e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return e;
}
void main()
{ QNode Q;
int k,i;
QElemType e,m;
InitQueue(&Q);
do{ printf("执行1插入,2删除.\n");
scanf("%d",&k);
switch(k)
{
case 1: printf("输入插入元素:");
scanf("%d", &m);
if(EnQueue(&Q,m)) printf("插入成功!\n");
else printf("插入失败!\n");
break;
case 2: e=DeQueue(&Q);
printf("删除元素是:%d.\n",e);
break;
}
printf("是否继续操作?1是,0否.\n");
scanf("%d",&i);
}while(i==1);
}