순환 대기 열의 순서 표시 와 실현

#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100
typedef struct//          
{	int *base;//      
	int front;
	int rear;
}CircleQueue;
void InitQueue(CircleQueue &Q);//         
void QueueLength(CircleQueue Q);//      
void EnQueue(CircleQueue &Q);//    
void DeQueue(CircleQueue &Q);//    
void ExportQueue(CircleQueue &Q);//      
void main()
{	int x;
	CircleQueue Qa;
	printf("1.Insert an element to the CircleQueue.
"); printf("2.Delete an element of the CircleQueue.
"); printf("3.Export the length of the CircleQueue.
"); printf("4.Export the element of the CircleQueue.
"); printf("0.End up the operation.
"); InitQueue(Qa); while(1) { printf("Please input your choice:"); scanf("%d",&x); switch(x) { case 1: EnQueue(Qa); break; case 2: DeQueue(Qa); break; case 3: QueueLength(Qa); break; case 4: ExportQueue(Qa); break; } if(x==0) { printf("The operation in the end!
"); break; } } } void InitQueue(CircleQueue &Q) { Q.base=(int *)malloc(MAXQSIZE*sizeof(int)); if(!Q.base) { printf("Fail to create CircleQueue!
"); return; } Q.front=Q.rear=0; printf("Succeed to create CircleQueue!
"); } void QueueLength(CircleQueue Q) { printf("The length of the CircleQueue is %d
",(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE); } void EnQueue(CircleQueue &Q) { int n; if((Q.rear+1)%MAXQSIZE==Q.front) { printf("The CircleQueue is full!
"); return; } printf("Please input the number to insert:"); scanf("%d",&n); Q.base[Q.rear]=n; Q.rear=(Q.rear+1)%MAXQSIZE;// Q.rear , printf("Succeed to insert %d
",n); } void DeQueue(CircleQueue &Q) { if(Q.rear==Q.front) { printf("The CircleQueue is empty!
"); return; } printf("Succeed to delete %d
",Q.base[Q.front]); Q.front=(Q.front+1)%MAXQSIZE; } void ExportQueue(CircleQueue &Q) { CircleQueue p; p=Q; printf("The CircleQueue is:"); while(p.front!=p.rear) { printf("%d",p.base[p.front]); p.front=(p.front+1)%MAXQSIZE; } printf("
"); }

좋은 웹페이지 즐겨찾기