队列

1.利用数组来实现队列,缺点是数组有限定大小。

程序

/***********************************************/
//数组实现队列功能
//while(1);是在dev -c++4.92编译环境下可见
//C语言,缺点设置了四个全局变量

//int queue[SIZE];int *head=queue;int *tail=queue;int count;
/***********************************************/

 

#include<stdio.h>

#include<stdlib.h>

/***********************************************/
//queue defintion
/***********************************************/
#define SIZE 10
int queue[SIZE];
int *head=queue;
int *tail=queue;
int count;

void push(int num)
{if(count==SIZE){printf("The queue is full!");exit(1);}
if(head>queue+SIZE-1)head-=SIZE;
*head=num;
head++;
count++;  
}
int pop()
{
if(count<0){printf("The queue is empty!");exit(1);}
if(tail>queue+SIZE-1)tail-=SIZE;
count--;
return *tail++;   
   
   
}
/***********************************************/
//Main function
/***********************************************/
int main(void)
{
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
push(7);
push(8);
push(9);
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop()); 
printf("%d",pop());
printf("%d",pop());
printf("%d",pop()); 
push(11);
push(12);
push(13);
push(14);
push(15);
push(16);
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
printf("%d",pop());
while(1);   
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值