欢迎来到天天文库
浏览记录
ID:47587975
大小:27.02 KB
页数:4页
时间:2019-09-21
《《数据结构》队列的创建,进队和出队操作(C语言描述)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#includestructQueueRecord;typedefstructQueueRecord*Queue;#defineMinQueueSize(5)structQueueRecord{intCapacity;intFront;intRear;intSize;int*Array;};/**创建队列。*/QueueCreateQueue(intcapacity){Queuequeue;queue=(Queue)malloc(sizeof
2、(Queue));if(queue==NULL){printf("outofspace!!!");}queue->Array=(int*)malloc(capacity*sizeof(int));if(queue->Array==NULL){printf("outofspace!!!");}queue->Size=0;queue->Rear=-1;queue->Front=0;returnqueue;}/**iffull,return0;elsereturn1;*/intIsFull(Q
3、ueuequeue,intcapacity){return(queue->SizeSize==0);}/**打印队列。*/intShowQueue(Queuequeue,intcapacity){inti;printf("queue->Front=%d",queue->Front);printf("queue->Rear=%d",queue->R
4、ear);if(!IsEmpty(queue)){for(i=queue->Front;i<=queue->Rear;i++){if(iRear){printf("%d,",queue->Array[i]);}else{printf("%d",queue->Array[i]);}}}else{printf("oh,sorry.itisempty.");}}/*进队*/voidEnqueue(Queuequeue,inta,intcapacity){queue->Rear++;qu
5、eue->Array[queue->Rear]=a;queue->Size++;if(IsFull(queue,capacity)){if(queue->Rear==capacity){queue->Rear=0;}}}/*出队*/voidDequeue(Queuequeue,intcapacity){queue->Front++;queue->Size--;if(IsFull(queue,capacity)){if(queue->Front==capacity){queue->Front=0;
6、}}}intmain(void){Queuequeue;intcapacity=10;if(capacity7、ty);Enqueue(queue,6,capacity);Dequeue(queue,capacity);Enqueue(queue,12,capacity);//Enqueue(queue,3,capacity);ShowQueue(queue,capacity);}
7、ty);Enqueue(queue,6,capacity);Dequeue(queue,capacity);Enqueue(queue,12,capacity);//Enqueue(queue,3,capacity);ShowQueue(queue,capacity);}
此文档下载收益归作者所有