学习随记二十八——循环队列的实现

循环队列的实现

基本思路:利用模运算来判断队列的空与满,其他操作与思路与最基本的用数组实现队列一样
整体代码:

#include<iostream>
const int quesize=10;
typedef struct Queue{
	int data[quesize]={0};
	int front=0;
	int rear=0;
}Queue;
using namespace std;
int Isempty(Queue*);          //判队空
int Isfull(Queue*);           //判队满
int Enqueue(Queue*,int);      //插入数据到队列中
int Dequeue(Queue*);          //删除队尾元素
void Display(Queue*);         //打印队列
void Inputdata(Queue*);       //输入数据
int main(void){
	Queue p;
	Inputdata(&p);
	Dequeue(&p);
	Enqueue(&p,1);
	Display(&p);
	return 0;
}
int Isempty(Queue* p){
	return p->front==p->rear;
}
int Isfull(Queue* p){
	return (p->rear+1)%quesize==p->front;
}
int Enqueue(Queue* p,int x){
	if(!Isfull(p)) p->data[p->rear++]=x;
	else exit(0);
	return 1;
}
int Dequeue(Queue* p){
	if(Isempty(p)){
		cout<<"The queue is empty."<<endl;
		exit(0);
	}else return p->data[p->front++];
}
void Inputdata(Queue* p){
	int x;
	while(cin>>x){
	 if(!Isfull(p)){
	  if(p->rear+1==quesize) p->rear=0;
	  p->data[p->rear++]=x;
		}else{
			cout<<"The queue is full."<<endl;
			exit(0);
		}
	}
	cin.clear();
	cin.ignore();
}
void Display(Queue* p){
	int i;
	if(!Isempty(p))
	for(i=p->front;i<p->rear;i++) cout<<p->data[i]<<endl;
	else cout<<"The queue is empty."<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值