#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef char QelemType;
typedef struct QNode
{
QelemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//初始化空队列
void initQueue(LinkQueue *q)
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!q->front)
{
exit(0);
}
q->rear->next = NULL;
}
//入队操作
void EnQueue(LinkQueue *q,QelemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
p->next = NULL;
if (!p)
{
exit(0);
}
p->data = e;
q->rear->next = p;
q->rear = p;
}
void DeQueue(LinkQueue *q,QelemType *e)
{
QueuePtr p;
if (q->front==q->rear)
{
return;
}
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if (q->rear==p)
{
q->rear = q->front;
}
free(p);
}
void main()
{
QelemType e;
LinkQueue q;
initQueue(&q);
printf("Please input a string into a queue\n");
scanf("%c",&e);
while (e!='#')
{
EnQueue(&q,e);
scanf("%c",&e);
}
printf("The string into the queue is\n");
while(q.front!= q.rear)
{
DeQueue(&q,&e);
printf("%c",e);
}
printf("\n");
getchar();
}