#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct qnode
{
Elemtype data;
struct qnode *next;
} QNode;
typedef struct
{
QNode *front;
QNode *rear;
} LiQueue;
void InitQueue(LiQueue * &q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}
void DestroyQueue(LiQueue *&q)
{
QNode *p=q->front,*r;
while(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(p);
free(q);
}
bool QueueEmpty(LiQueue* &q)
{
return (q->rear==NULL);
}
void enQueue(LiQueue *&q,Elemtype &e)
{
QNode *p;
p=(QNode*)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL)
{
q->front=q->rear=p;
}
else
{
q->rear->next=p;
q->rear=p;
}
}
void deQueue(LiQueue* &q,Elemtype &e)
{
QNode *t;
if(q->rear==NULL)
printf("空队列!\n");
else
{
if(q->front==q->rear)
q->front=q->rear=NULL;
e=q->front->data;
t=q->front;
q->front=q->front->next;
free(t);
}
}
void printtQueue(LiQueue *&q)
{
QNode *p=q->front;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
int i;
LiQueue *q;
InitQueue(q);
for(i=0; i<5; i++)
{
enQueue(q,i);
printf("入队成功!\n");
}
deQueue(q,i);
printf("%d\n",i);
deQueue(q,i);
printf("%d\n",i);
printtQueue(q);
return 0;
}