#include<stdio.h>
#include<malloc.h>
#define MAXLEN 100
typedef struct{
int data[MAXLEN];
int front,rear;
}csequeue;
void emptyqueue(csequeue *q)
{
q->front=q->rear=MAXLEN-99;
}
void Inqueue(csequeue *q)
{
int x;
printf("\t\t请输入要进队的整数,以数字0结束!!!\n");
while(1)
{
scanf("%d",&x);
if(q->front==(q->rear+1)%MAXLEN)
{
printf("\t\t队列已满,无法进队\n");
return ;
}
else if(x==0)
break;
else
{
q->rear=(q->rear+1)%MAXLEN;
q->data[q->rear]=x;
}
}
printf("\t\t进队成功\n");
}
void Outqueue(csequeue *q)
{
if(q->front==q->rear)
{
printf("\t\t队列为空,无法出队\n");
return ;
}
else
{
q->front=(q->front+1)%MAXLEN;
printf("\t\t输出队首元素\n");
printf("%d\n",q->data[q->front]);
}
}
void Showqueue(csequeue *q)
{
int i;
if(q->rear==q->front)
{
printf("\t\t队列为空,请将元素入队\n");
return ;
}
else
{
i=q->front;
printf("\t\t队列中的元素为:\n");
while(1)
{
if(i==q->rear)
break;
else
{
i=(i+1)%MAXLEN;
printf("%d ",q->data[i]);
}
}
}
}
void Lenthqueue(csequeue *q)
{
int len;
len=(q->rear-q->front+MAXLEN)%MAXLEN;
printf("\t\t%d\n",len);
}
int main ()
{
int m;
csequeue q;
emptyqueue(&q);
printf("\t\t\t ----循环队列----\n\n");
while(1)
{
printf("\n\t\t*******************************************\n");
printf("\t\t* 指令说明 *\n");
printf("\t\t* 1:进 队 *\n");
printf("\t\t* 2:出 队 *\n");
printf("\t\t* 3:显 示 *\n");
printf("\t\t* 4:求队列长度 *\n");
printf("\t\t* 0:返 回 *\n");
printf("\t\t*******************************************\n\n");
printf("\t请输入指令:");
scanf("%d",&m);
if(m==1)
Inqueue(&q);
else if(m==2)
Outqueue(&q);
else if(m==3)
Showqueue(&q);
else if(m==4)
Lenthqueue(&q);
else if(m==0)
return 0;
else
printf("输入数据错误,请重新输入!!!\n");
}
return 0;
}
#include<malloc.h>
#define MAXLEN 100
typedef struct{
int data[MAXLEN];
int front,rear;
}csequeue;
void emptyqueue(csequeue *q)
{
q->front=q->rear=MAXLEN-99;
}
void Inqueue(csequeue *q)
{
int x;
printf("\t\t请输入要进队的整数,以数字0结束!!!\n");
while(1)
{
scanf("%d",&x);
if(q->front==(q->rear+1)%MAXLEN)
{
printf("\t\t队列已满,无法进队\n");
return ;
}
else if(x==0)
break;
else
{
q->rear=(q->rear+1)%MAXLEN;
q->data[q->rear]=x;
}
}
printf("\t\t进队成功\n");
}
void Outqueue(csequeue *q)
{
if(q->front==q->rear)
{
printf("\t\t队列为空,无法出队\n");
return ;
}
else
{
q->front=(q->front+1)%MAXLEN;
printf("\t\t输出队首元素\n");
printf("%d\n",q->data[q->front]);
}
}
void Showqueue(csequeue *q)
{
int i;
if(q->rear==q->front)
{
printf("\t\t队列为空,请将元素入队\n");
return ;
}
else
{
i=q->front;
printf("\t\t队列中的元素为:\n");
while(1)
{
if(i==q->rear)
break;
else
{
i=(i+1)%MAXLEN;
printf("%d ",q->data[i]);
}
}
}
}
void Lenthqueue(csequeue *q)
{
int len;
len=(q->rear-q->front+MAXLEN)%MAXLEN;
printf("\t\t%d\n",len);
}
int main ()
{
int m;
csequeue q;
emptyqueue(&q);
printf("\t\t\t ----循环队列----\n\n");
while(1)
{
printf("\n\t\t*******************************************\n");
printf("\t\t* 指令说明 *\n");
printf("\t\t* 1:进 队 *\n");
printf("\t\t* 2:出 队 *\n");
printf("\t\t* 3:显 示 *\n");
printf("\t\t* 4:求队列长度 *\n");
printf("\t\t* 0:返 回 *\n");
printf("\t\t*******************************************\n\n");
printf("\t请输入指令:");
scanf("%d",&m);
if(m==1)
Inqueue(&q);
else if(m==2)
Outqueue(&q);
else if(m==3)
Showqueue(&q);
else if(m==4)
Lenthqueue(&q);
else if(m==0)
return 0;
else
printf("输入数据错误,请重新输入!!!\n");
}
return 0;
}