#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 //宏定义MAXSIZE数值
typedef int ElemType;
typedef struct queue//定义结构体
{
ElemType data[MAXSIZE];
int front,rear;//定义队头,队尾
} Cirqueue;
Cirqueue *InitCirqueue()//置空队
{
Cirqueue *q;
q = (Cirqueue *)malloc(sizeof(Cirqueue));
q->front = q->rear = 0;
return q;
}
void EnCirquene (Cirqueue *q, ElemType x)//入队
{
int i,a;
if((q->rear+1)%MAXSIZE == q->front)
{
printf("队满");
return;
}
printf("请输入你想入队的个数:");
scanf("%d",&a);
printf("请输入你想入队的数字:");
for(i=0; i<a; i++)
{
scanf("%d",&x);
q->data[q->rear] = x;//赋值给q->data[]
q->rear=(q->rear+1)%MAXSIZE;
}
}
void DeCirquene(Cirqueue *q, int *x)//出队
{
if(q->front == q->rear)//判队空
{
printf("队空");
return;
}
else
{
*x = q->data[q->front];//读出队头元素
q->front = (q->front+1)%MAXSIZE;
printf("%d\n",*x);
}
}
void printquene(Cirqueue *q)
{
int i;
printf("队中元素为:");
if(q->rear > q->front)
{
for(i = q->front; i < q->rear; i++)
{
printf("%-4d",q->data[i]);
}
}
else
{
for(i = q->front; i<MAXSIZE;i++)
printf("%-4d",q->data[i]);
}
}
int main()
{
int a,x,m;
int *p;
Cirqueue *cq = InitCirqueue();
while(1)
{
printf("\t\t1,入队\n");
printf("\t\t2,出队\n");
printf("\t\t3,打印\n");
printf("\t\t请输入你的操作:");
scanf("%d",&a);
switch(a)
{
case 1:
EnCirquene(cq,x);
system("pause");
system("cls");
break;
case 2:
DeCirquene(cq,&p);
system("pause");
system("cls");
break;
case 3:
printquene(cq);
printf("\n");
system("pause");
system("cls");
break;
}
}
return 0;
}
#include <stdlib.h>
#define MAXSIZE 100 //宏定义MAXSIZE数值
typedef int ElemType;
typedef struct queue//定义结构体
{
ElemType data[MAXSIZE];
int front,rear;//定义队头,队尾
} Cirqueue;
Cirqueue *InitCirqueue()//置空队
{
Cirqueue *q;
q = (Cirqueue *)malloc(sizeof(Cirqueue));
q->front = q->rear = 0;
return q;
}
void EnCirquene (Cirqueue *q, ElemType x)//入队
{
int i,a;
if((q->rear+1)%MAXSIZE == q->front)
{
printf("队满");
return;
}
printf("请输入你想入队的个数:");
scanf("%d",&a);
printf("请输入你想入队的数字:");
for(i=0; i<a; i++)
{
scanf("%d",&x);
q->data[q->rear] = x;//赋值给q->data[]
q->rear=(q->rear+1)%MAXSIZE;
}
}
void DeCirquene(Cirqueue *q, int *x)//出队
{
if(q->front == q->rear)//判队空
{
printf("队空");
return;
}
else
{
*x = q->data[q->front];//读出队头元素
q->front = (q->front+1)%MAXSIZE;
printf("%d\n",*x);
}
}
void printquene(Cirqueue *q)
{
int i;
printf("队中元素为:");
if(q->rear > q->front)
{
for(i = q->front; i < q->rear; i++)
{
printf("%-4d",q->data[i]);
}
}
else
{
for(i = q->front; i<MAXSIZE;i++)
printf("%-4d",q->data[i]);
}
}
int main()
{
int a,x,m;
int *p;
Cirqueue *cq = InitCirqueue();
while(1)
{
printf("\t\t1,入队\n");
printf("\t\t2,出队\n");
printf("\t\t3,打印\n");
printf("\t\t请输入你的操作:");
scanf("%d",&a);
switch(a)
{
case 1:
EnCirquene(cq,x);
system("pause");
system("cls");
break;
case 2:
DeCirquene(cq,&p);
system("pause");
system("cls");
break;
case 3:
printquene(cq);
printf("\n");
system("pause");
system("cls");
break;
}
}
return 0;
}