C语言之循环队列的基本操作
break;
case 2: if(!DeQueue(S,e))printf("Delete Error!\n"); //判断出队是否合法,请填空
elseprintf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: if(!GetHead(S,e) )printf("Get Head Error!\n"); //判断Get Head是否合法,请填空
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
StatusDeQueue(SqQueue&Q,QElemType&e)
{
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
while(i!=Q.rear) //请填空
{
printf("%d ",Q.base[i]); //请填空
i=i+1; //请填空
}
}
printf("\n");
return OK;
}
intmain()
{
inta;
SqQueueS;
QElemTypex, e;
if(InitQueue(S)) //判断顺序表是否创建成功,请填空
{
printf("A Queue Has Created.\n");
}
while(1)
{
printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
return OK;
}
StatusGetHead(SqQueueQ,QElemType&e)
{
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
return OK;
}
intQueueLength(SqQueueQ)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
break;
case 0: return 1;
}
}
}
elseprintf("The Head of the Queue is %d!\n", e);
break;
case 4:printf("The Length of the Queue is %d!\n",QueueLength(S)); //请填空
break;
case 5:QueueTraverse(S);//请填空
if(!Q.base)
{
return ERROR;
}
Q.front=Q.rear=0;
return OK;
}
StatusEnQueue(SqQueue&Q,QElemTypee)
{
if((Q.rear+1)%MAXQSIZE==Q.front)
return ERR作
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
typedefintStatus; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedefintQElemType;
#define MAXQSIZE 100 //最大队列长度(对于循环队列,最大队列长度要减1)
scanf("%d",&a);
switch(a)
{
case 1:scanf("%d", &x);
if(!EnQueue(S,x))printf("Enter Error!\n"); //判断入队是否合法,请填空
elseprintf("The Element %d is Successfully Entered!\n", x);
typedefstruct
{
QElemType*base; //初始化的动态分配存储空间
intfront; //头指针,若队列不空,指向队列头元素
intrear; //尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
StatusInitQueue(SqQueue&Q)
{
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
}
StatusQueueTraverse(SqQueueQ)
{
//若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
inti;
i=Q.front;
if(Q.front==Q.rear)printf("The Queue is Empty!"); //请填空
else
{
printf("The Queue is: ");