数据结构-循环队列

在这里插入图片描述
在顺序队列中,当队尾指针已经到数组的上界,不能再有入队操作,但其实数组中还有空位置,这就叫做“假溢出”,解决假溢出的途径---- 采用循环队列。

 #include <iostream>
#include <fstream>
using namespace std;
#define max 100
#define ok 1
#define error 0

typedef struct 
{
	int *base;
	int front;
	int rear;
} squeel;

//初始化
int chushihua(squeel &q) 
{
	q.base = new int[max];
	q.front = 0;
	q.rear = 0;
	return ok;
}

//求长度
int lengthl(squeel &q) 
{
	int l = q.rear - q.front + max;
	{
		return l % max;
	}
}

//插入队列
int rudui(squeel &q, int e) 
{
	if ((q.rear + 1) % max == q.front) 
	{
		return error;
	}
	q.base[q.rear] = e;
	q.rear = (q.rear + 1) % max;
	return ok;
}

int chudui(squeel &q, int &e) 
{
	if (q.front == q.rear) 
	{
		return error;
	}
	e = q.base[q.front];
	q.front = (q.front + 1) % max;
	return ok;
}

int duitou(squeel q) 
{
	if (q.front != q.rear) 
	{
		return q.base[q.front];
	}
}

int main () 
{
	squeel q;
	int e, l, a;
	chushihua(q);
	rudui(q, 11);
	rudui(q, 22);
	rudui(q, 33);
	l = lengthl(q);
	cout << "长度" << l << endl;
	a = duitou(q);
	cout << "队头" << a << endl;
	chudui(q, e);
	cout << "第一个" << e << endl;
	chudui(q, e);
	cout << "第二个" << e << endl;
	chudui(q, e);
	cout << "第三个" << e << endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值