链队列——队列的链式表示和实现(数据结构)#includestdio.h#includemalloc.h#includestdlib.h#defineTURE1#defineFALSE0#defineOK1#defineERROR0#defineINEEASLIBE-1#defineOVERFLOW-2typedefintstatus;typedefintelemtype;typedefstructnode{elemtypedata;structnode*next;}lnode,*queueptr;typedefstruct{queueptrfront;queueptrrear;}linkqueue;statusinitqueue(linkqueue&q){q.front=q.rear=(queueptr)malloc(sizeof(lnode));if(!q.front)returnERROR;q.front-next=NULL;returnOK;}statusenqueue(linkqueue&q,elemtypee){queueptrp;p=(queueptr)malloc(sizeof(lnode));if(!p)returnERROR;p-data=e;p-next=NULL;q.rear-next=p;q.rear=p;returnOK;}statusdestroyqueue(linkqueue&p){if(p.front==p.rear){free(p.front);returnOK;}elsewhile(1){queueptrq;q=p.front;if(p.front==p.rear){free(p.front);returnOK;}p.front=p.front-next;free(q);}}statusdequeue(linkqueue&q,elemtype&e){if(q.front==q.rear){printf(该队列中没有数据元素\n);returnERROR;}e=q.front-next-data;queueptrp;p=q.front-next;if(p==q.rear){q.rear=q.front;q.rear-next=NULL;}elseq.front-next=p-next;free(p);returnOK;}statusvist(linkqueueq){if(q.front==q.rear){printf(没有数据元素\n);returnERROR;}queueptrp;p=q.front-next;printf(队列中元素有:\n);while(1){printf(%d,p-data);if(p-next==NULL)break;p=p-next;}printf(\n);returnOK;}main(){linkqueuep;charc;elemtypee;printf(进行测试前一定要先对对队列进行初始化\n);while(1){printf(1:初始化队列\n2:销毁队列\n3:进入队列\n4:出队列\n5:遍历队列\n0:退出\n);scanf(%c,&c);getchar();system(cls);switch(c){case'1':if(initqueue(p)==OK)printf(初始化成功\n);else{printf(初始化失败\n);exit(0);}break;case'2':if(destroyqueue(p)==OK)printf(销毁成功\n);break;case'3':printf(请输入需要进入队列的元素e\n);scanf(%d,&e);getchar();if(enqueue(p,e)==OK)printf(入队列成功\n);elseprintf(入队失败\n);break;case'4':if(dequeue(p,e)==OK)printf(从队列中取到得元素为%d\n,e);break;case'5':vist(p);break;case'0':exit(0);}}}