#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode;
typedef struct LinkQueue
{
QNode *front, *rear;
int size;
}LinkQueue;
void InitQueue(LinkQueue *q)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if (!p)
exit(0);
p->next = NULL;
q->front = q->rear = p;
q->size = 0;
}
void InsertQueue(LinkQueue *q, ElemType e)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
if (!p)
exit(0);
p->next = NULL;
p->data = e;
q->rear->next = p;
q->rear = p;
q->size++;
}
void DeleteQueue(LinkQueue *q, ElemType *e)
{
QNode *p;
if (q->front == q->rear)
return;
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if (p == q->rear)
q->rear = q->front;
free(p);
q->size--;
}
int main(void)
{
ElemType c;
LinkQueue q;
InitQueue(&q);
puts("Please input element, end with #");
scanf("%c", &c);
while (c != '#')
{
InsertQueue(&q, c);
scanf("%c", &c);
}
printf("There are %d elements in Queue\r\n", q.size);
while(q.size)
{
DeleteQueue(&q, &c);
putchar(c);
}
return 0;
}