循环队列 队列的顺序表示和实现

#includestdio.h
#includestdlib.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.\n");
	printf("2.Delete an element of the CircleQueue.\n");
	printf("3.Export the length of the CircleQueue.\n");
	printf("4.Export the element of the CircleQueue.\n");
	printf("0.End up the operation.\n");
	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!\n");
			break;
		}
	}
}
void InitQueue(CircleQueue Q)
{	Q.base=(int *)malloc(MAXQSIZE*sizeof(int));
	if(!Q.base)
	{	printf("Fail to create CircleQueue!\n");
		return;
	}
	Q.front=Q.rear=0;
	printf("Succeed to create CircleQueue!\n");
}
void QueueLength(CircleQueue Q)
{	printf("The length of the CircleQueue is %d\n",(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);
}
void EnQueue(CircleQueue Q)
{	int n;
	if((Q.rear+1)%MAXQSIZE==Q.front)
	{	printf("The CircleQueue is full!\n");
		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",n);
}
void DeQueue(CircleQueue Q)
{	if(Q.rear==Q.front)
	{	printf("The CircleQueue is empty!\n");
		return;
	}
	printf("Succeed to delete %d\n",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("\n");
}

最新回复(0)
/jishuyvv3VY7wRw2GGloKQ_2FZih50eBVLrpj7tmDiwgAddV8o_3D4858384
8 简首页