数据结构(十一)循环队列的基本操作----6个基本操作

//顺序队列存在一个问题 ---假溢出现象,为了解决这个问题,提出了循环队列
//循环队列中存在队空和队满条件一样的情况,因此提出了牺牲一个空间的方法 
//循环队列的基本操作 
#include <iostream>

using namespace std;

#define MAXSIZE 5
//队列的结构体
struct Node
{
	int *base;
	int front;
	int rear;	
}; 
//队列的初始化操作
void initQueue(struct Node &Q)
{
 	Q.base = new int[MAXSIZE];
 	if(Q.base == NULL)
 	{
 		cout<<"地址分配失败\n";
 		exit(1);
 	}
 	Q.front = Q.rear=0;	
}
//队空的判断
int isEmpty(struct Node Q)
{
	if(Q.front == Q.rear)
	{
		return 1; 
	} 
	else
	{
		return 0;
	}
} 
//队满的判断
int isFull(struct Node Q)
{
	if((Q.rear+1)%MAXSIZE==Q.front)
	{
		return 1;
	}
	else
	{
		return 0; 
	}
}  
//入队列操作
void Enqueue(struct Node &Q)
{
	int e;
	cout<<"请输入你要入队的数据:\n";
	cin>>e;
	Q.base[Q.rear] = e;
	Q.rear=(Q.rear+1)%MAXSIZE;
} 
//出队列的操作 
void inQueue(struct Node &Q,int &e)
{

	e = Q.base[Q.front];
	Q.front = (Q.front +1)%MAXSIZE;
}
//队列的实际长度
int length(struct Node Q)
{
	int len;
	len = (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
	return len;
}

int main()
{
	struct Node Q;
	initQueue(Q);	
	for(int i=0;i<4;i++)
	{
		if(isFull(Q))
		{
			cout<<"队列已经满了\n";
			break;
		}			
		Enqueue(Q);
		
	}
	int e;
	for(int i=0;i<4;i++)
	{
	
		inQueue(Q,e);
		cout<<e<<" ";
	}

	if(isEmpty(Q))
	{
		cout<<"队列为空\n";
		exit(1);
	}
	return 0;
}

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值