#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct qnode
{
ElemType data;
struct qnode *next;
} DataNode;
typedef struct
{
DataNode *front;
DataNode *rear;
} LinkQuNode;
void InitQueue(LinkQuNode *&q)
{
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void DestroyQueue(LinkQuNode *&q)
{
DataNode *pre=q->front,*p;
if (p!=NULL)
{ p=pre->next;
while (p!=NULL)
{ free(pre);
pre=p;p=p->next;
}
}
free(pre);
free(q);
}
bool QueueEmpty(LinkQuNode *q)
{
return(q->rear==NULL);
}
void enQueue(LinkQuNode *&q,ElemType e)
{ DataNode *p;
p=(DataNode *)malloc(sizeof(DataNode));
p->data=e;
p->next=NULL;
if (q->rear==NULL)
q->front=q->rear=p;
else
{ q->rear->next=p;
q->rear=p;
}
}
bool deQueue(LinkQuNode *&q)
{ DataNode *t;
if (q->rear==NULL)
return false;
t=q->front;
if (q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
printf("%c
",t->data);
free(t);
return true;
}
int main()
{
LinkQuNode *q;
InitQueue(q);
printf("初始化完毕
");
if(QueueEmpty(q))
{
printf("链队为空
");
}
else
{
printf("链队不为空
");
}
enQueue(q,'a');
enQueue(q,'b');
enQueue(q,'c');
printf("进队完毕
");
deQueue(q);
printf("a已经出队
");
enQueue(q,'d');
enQueue(q,'e');
enQueue(q,'f');
deQueue(q);
deQueue(q);
deQueue(q);
deQueue(q);
deQueue(q);
DestroyQueue(q);
return 0;
}