#include<stdio.h>
#include<malloc.h>
#define maxsize 100
typedef struct queue
{
int *base;
int front,rear;
}queue;
queue q;
void add(queue &q,int n)
{
int i;
q.base=(int*)malloc(maxsize*sizeof(int));
if(n>maxsize-1)
printf("error!\n");
q.front=q.rear=0;
printf("input the numbers:\n");
for(q.rear=0;q.rear<n;q.rear++)
{
scanf("%d",&i);
q.base[q.rear]=i;
}
}
int show(queue q)
{
if(q.front==q.rear)
{
printf("NULL!\n");
return 0;
}
int p;
p=q.front;
while(p!=q.rear)
{
printf("%d\t",q.base[p%maxsize]);
p++;
}
printf("\n");
return 1;
}
int insert(queue &q,int i)
{
if((q.rear+1)%maxsize==q.front)
{
printf("full!\n");
return 0;
}
q.base[q.rear]=i;
q.rear=(q.rear+1)%maxsize;
return 1;
}
int delet(queue &q)
{
int i;
if(q.front==q.rear)
{
printf("null!\n");
return 0;
}
i=q.base[q.front];
q.front=(q.front+1)%maxsize;
printf("number:%d\n",i);
return 1;
}
void main()
{
int n,i;
printf("length:");
scanf("%d",&n);
add(q,n);
show(q);
printf("insert the number:");
scanf("%d",&i);
insert(q,i);
delet(q);
show(q);
}
结果