欢迎来到天天文库
浏览记录
ID:53278729
大小:34.50 KB
页数:3页
时间:2020-04-02
《C语言-初始化队列+入队列+出队列+销毁队列.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、链队列题目:初始化队列+入队列+出队列+销毁队列(1)初始化一个链队列;(2)在初始化好的链队列中放入数,入队列,完成后要求显示;(3)从队列中出队列,要求显示出来的元素和之后的队列;(4)销毁创建的队列,释放内存;#include#include#defineNULL0typedefintQElemType;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*QueuePtr;typedefstruct{Que
2、uePtrfront;QueuePtrrear;}LinkQueue;voidCreateQueue(LinkQueue*Q){inta;QueuePtrp;p=(QueuePtr)malloc(sizeof(QNode));if(!p)printf("创建失败");else{p->next=NULL;Q->front=p;Q->rear=p;scanf("%d",&a);while(a!=-1){p=(QueuePtr)malloc(sizeof(QNode));p->next=NULL;if(!p)print
3、f("创建失败");else{p->data=a;Q->rear->next=p;Q->rear=p;scanf("%d",&a);}}}}voidPrintfQueue(LinkQueue*Q){QueuePtrp;for(p=Q->front->next;p!=NULL;p=p->next){printf("%d",p->data);}}voidEnQueue(LinkQueue*Q,QElemTypex){QueuePtrp;p=(QueuePtr)malloc(sizeof(QNode));if(!p)p
4、rintf("创建失败");else{p->data=x;p->next=NULL;Q->rear->next=p;Q->rear=p;}}voidDeQueue(LinkQueue*Q,QElemType*f){QueuePtrp;if(Q->front==Q->rear)printf("Error");else{p=Q->front->next;*f=p->data;Q->front->next=p->next;if(Q->rear==p)Q->rear=Q->front;free(p);}}voidDe
5、leteQueue(LinkQueue*Q){QueuePtrp;for(;Q->front!=NULL;free(p)){p=Q->front;Q->front=Q->front->next;}}voidmain(){int*f,e;LinkQueueQueue,*Q;e=0;f=&e;Q=&Queue;printf("输入队列的元素依次为:");CreateQueue(Q);printf("初始队列为:");PrintfQueue(Q);printf("要入队的元素为:");scanf("%d",
6、&e);EnQueue(Q,e);printf("入队后的队列为:");PrintfQueue(Q);DeQueue(Q,f);printf("出队的元素为:");printf("%d",e);printf("出队后的队列为:");PrintfQueue(Q);DeleteQueue(Q);}
此文档下载收益归作者所有