#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define MAX 80
typedef struct
{
int data[MAX];
int front,rear;
int num;
}SeQue;
SeQue *Init_SeQue()
{
SeQue *s;
s=new SeQue;
s->front=s->rear=MAX-1;
s->num=0;
return s;
}
int Empty_SeQue(SeQue *s)
{
if(s->num==0)
return 1;
else
return 0;
}
int In_SeQue(SeQue *s,int x)
{
if(s->num==MAX)
return 0;
else
{
s->rear=(s->rear+1)%MAX;
s->data[s->rear]=x;
s->num++;
return 1;
}
}
int Out_SeQue(SeQue *s,int *x)
{
if(Empty_SeQue(s))
return 0;
else
{
s->front=(s->front+1)%MAX;
*x=s->data[s->front];
s->num--;
return 1;
}
}
void Print_SeQue(SeQue *s)
{
int i,n;
i=(s->front+1)%MAX;
n=s->num;
while(n>0)
{ printf("%d ",s->data[i]);
i=(i+1)%MAX;
n--;
}
}
void main()
{
SeQue *s;
int k,flag,x;
s=Init_SeQue();
do{
printf("\n\n\n");
printf("\t\t\t 循环顺序队列\n");
printf("\t\t\t***********************\n");
printf("\t\t\t** 1-入 队 **\n");
printf("\t\t\t** 2-出 队 **\n");
printf("\t\t\t** 3-判 队 空 **\n");
printf("\t\t\t** 4-队列显示 **\n");
printf("\t\t\t** 0-返 回 **\n");
printf("\t\t\t***********************\n");
printf("\t\t\t 请输入菜单项(0-4):");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n请输入入队元素:");
scanf("%d",&x);
flag=In_SeQue(s,x);
if(flag==0)
printf("\n队满不能入队!按任意键返回..");
else
printf("\n元素已入队!按任意键返回..");
getch();
system("cls");
break;
case 2:
flag=Out_SeQue(s,&x);
if(flag==0)
printf("\n队列空出队失败!按任意键返回..");
else
printf("\n队列头元素已出队~!按任意键返回..");
getch();
system("cls");
break;
case 3:
flag=Empty_SeQue(s);
if(flag==1)
printf("\n该队列为空!按任意键返回..");
else
printf("\n该队列不为空!按任意键返回..");
getch();
system("cls");
break;
case 4:
printf("\n该队列元素为:");
Print_SeQue(s);
printf("\n按任意键返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}