队列链表建立

#include <stdio.h>
#include <stdlib.h>
#define MAXN 1000 typedef struct queue
{
int qq[MAXN]; //队中元素
int front; //队首下标
int rear; //队尾下标
int count; //队列中元素个数
}queue;
queue *q;
int init() //队列初始化
{
q=(struct queue*)malloc(sizeof(struct queue)); //申请内存
q->front=0; //都置为0
q->rear=0;
q->count=0;
return 0;
}

int push(int n) //往队尾加数
{
if(q->count>=MAXN)
{
printf("队列已满\n");
return 0;
}
q->qq[q->rear]=n;
q->count++;
q->rear++;
return 1;
}

int pop() //删除队首,并返回原队尾数
{
int n;
if(q->count<=0)
{
printf("队列已空\n");
return -1;
}
n=q->qq[q->front];
q->front++;
q->count--;
return n;
}

int empty() //判断是否为空
{
if(q->count<=0)
return 1;
return 0;
}
int size() //队列大小
{
return q->count;
}

int main()
{
int t;

queue *q=NULL;

freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);

init();

while(scanf("%d",&t)==1,t) //从文件读取,读到0结束
push(t);

if(empty()) //判断是否为空
printf("为空\n");
else
printf("不为空\n");

printf("queue_size = %d\n",size());//输出大小

for(t=0;t<3;t++)
{
if(t)
printf(" ");
printf("%d",pop()); //删除并输出队首
}
printf("\n");

push(123);


printf("queue_size = %d\n",size());

while(!empty())
{
printf("%d\n",pop());
}
return 0;
}

sum.in

1 2 3 4 5 6 7 8 9 10 0
sum.out

不为空
queue_size = 10
1 2 3
queue_size = 8
4
5
6
7
8
9
10
123

 

你可能感兴趣的:(链表)