资源描述:
《《数据结构》队列的创建,进队和出队操作(C语言描述)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#includestructQueueRecord;typedefstructQueueRecord*Queue;#defineMinQueueSize(5)structQueueRecord{intCapacity;intFront;intRear;intSize;int*Array;};/**创建队列。*/QueueCreateQueue(intcapacity){Queuequeue;queue=(Queue)malloc(sizeof(Queue));if(queue=
2、=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(Queuequeue,intcapacity){return(queue-
3、>SizeSize==0);}/**打印队列。*/intShowQueue(Queuequeue,intcapacity){inti;printf("queue->Front=%d",queue->Front);printf("queue->Rear=%d",queue->Rear);if(!IsEmpty(queue)){for(i=queue->Front;i<=queue->
4、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++;queue->Array[queue->Rear]=a;queue->Size++;if(IsFull(queue,capacity)){if(qu
5、eue->Rear==capacity){queue->Rear=0;}}}/*出队*/voidDequeue(Queuequeue,intcapacity){queue->Front++;queue->Size--;if(IsFull(queue,capacity)){if(queue->Front==capacity){queue->Front=0;}}}intmain(void){Queuequeue;intcapacity=10;if(capacity6、");}queue=CreateQueue(capacity);Enqueue(queue,1,capacity);Enqueue(queue,2,capacity);Enqueue(queue,3,capacity);Enqueue(queue,4,capacity);Enqueue(queue,5,capacity);Enqueue(queue,6,capacity);Dequeue(queue,capacity);Enqueue(queue,12,capacity);//Enqueue(queue,3,capacity)
7、;ShowQueue(queue,capacity);}