运行截图:
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0;
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//初始化
Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}
//入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr p;
p = new QNode;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
//出队
Status DeQueue(LinkQueue &Q,QElemType &e)
{
if(Q.rear == Q.front)
return ERROR ;
QueuePtr p;
p = Q.front ->next;
e = p->data;
Q.front ->next = p->next;
if(Q.rear == p) Q.rear = Q.front;
delete p;
return OK;
}
int main()
{
LinkQueue Q;
InitQueue(Q);
int e = 1;
printf("入队(以00结束):");
while (e)
{
scanf("%d",&e);
if (e == 00)
break;
EnQueue(Q,e);
}
printf("出队:");
while(Q.rear != Q.front)
{
DeQueue(Q,e);
printf("%5d",e);
}
printf("\n");
return 0;
}