/**************************************
/* Dynamic Queue Model 1.0
/* Kangqiao 2010-07-08 10:20
/* My E-mail:[email protected]
/* My Blog:http://hi.baidu.com/zunhuakq
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* #define isempty(q) ((q)==NULL) */
typedef int __type__; /* you can define the __type__ another type */
typedef char bool; /* because C language doesn't have the boolean type,except C++ */
typedef struct _queue_item
{
__type__ value;
struct _queue_item *next;
}queue_item;
typedef struct
{
queue_item *in;
queue_item *out;
}queue;
void enq(queue *q,__type__ value)
{
queue_item *tmp;
tmp=(queue_item *)malloc(sizeof(queue_item));
tmp->value=value;
tmp->next=NULL;
if(q->in) q->in->next=tmp,q->in=tmp;
else q->out=q->in=tmp;
}
bool deq(queue *q,__type__ *value)
{
queue_item *tmp;
tmp=q->out;
if(tmp)
{
*value=tmp->value;
q->out=q->out->next;
free(tmp);
if(q->out==NULL) q->in=NULL;
return 1;
}
else return 0;
}
void empty(queue *q)
{
queue_item *tmp;
tmp=q->out;
while(tmp)
{
q->out=q->out->next;
free(tmp);
tmp=q->out;
}
q->in=NULL;
}
void prt(queue q) /* only a test print function */
{
puts("*************************");
while(q.out)
{
printf("%d\n",q.out->value);
q.out=q.out->next;
}
}
int main(int argc,char *argv[])
{
/*
queue q={NULL,NULL};
int tmp;
prt(q);
enq(&q,16);
prt(q);
enq(&q,32);
prt(q);
deq(&q,&tmp);
printf("Result=%d\n",tmp);
printf("in=%x out=%x\n",q.in,q.out);
prt(q);
printf("Boolean=%d\n",deq(&q,&tmp));
printf("in=%x out=%x\n",q.in,q.out);
printf("Result=%d\n",tmp);
prt(q);
printf("Boolean=%d\n",deq(&q,&tmp));
printf("Result=%d\n",tmp);
prt(q);
enq(&q,8);
printf("in=%x out=%x\n",q.in,q.out);
enq(&q,16);
printf("in=%x out=%x\n",q.in,q.out);
prt(q);
empty(&q);
prt(q);
printf("in=%x out=%x\n",q.in,q.out);
getch();
*/
}