#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MaxSize 10
typedef int ElemType;
typedef struct SqQueue
{
ElemType elem[MaxSize];
int front,rear;
}SqQueue;
SqQueue *Init_SqQueue()
{
SqQueue *q;
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
return q;
}
int En_SqQueue(SqQueue *q,ElemType e)
{
if((q->rear+1)%MaxSize==q->front) return 0;
q->elem[q->rear]=e;
q->rear=(q->rear+1)%MaxSize; return 1;
}
int De_SqQueue(SqQueue *q,ElemType &e)
{
if(q->front==q->rear) return 0;
e=q->elem[q->front];
q->front=(q->front+1)%MaxSize; return 1;
}
void Display_SqQueue(SqQueue *q)
{
int i,Q;
Q=q->front;
for(i=q->front;i!=q->rear;i=(i+1)%MaxSize)
{printf("%d",q->elem[i]);}
printf("\n");
}
void main()
{
ElemType e;
SqQueue *q;
int i,k;
q=Init_SqQueue();En_SqQueue(q,1);
En_SqQueue(q,2);En_SqQueue(q,3);
En_SqQueue(q,4);
printf("初始化\n建立初始循环队列如下:\n");
Display_SqQueue(q);
while(i<=3)
{
printf("
主菜单
\n");
printf("
1
入队
\n");
printf("
2
出队
\n");
printf("
3
结束程序
\n");
printf("-------------------------\n");
printf("请输入您选择的菜单号<1, 2, 3>:\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("请输入入队元素:");
scanf("%d",&k);
En_SqQueue(q,k);
printf("该元素入队后得到的新队列如下:");
Display_SqQueue(q);
break;
case 2:
if(De_SqQueue(q,e))
{
printf("出队元素为:%d\n",e);
printf("出队后得到的新队列如下:");
Display_SqQueue(q);
}
else
printf("循环队列为空!\n");
break;
case 3:
printf("程序执行结束\n");
exit(0);
break;
default:
printf("输入有误!");
}
}
}