队列(个人笔记)

队列的创建,增加,删除,判断空满

#include <iostream>
#include <cstdlib>
#define ERROR -1
#define N 100
using namespace std;
/*队列*/
struct Queue{
	int *data;
	int front,rear;
	int MaxSize;
};
/*二叉树*/
struct BinTree{
	int data;
	struct BinTree *left;
	struct BinTree *right;
};
/*队列的创建*/
struct Queue *CreateQueue(int Maxsize)
{
	struct Queue *Q = (struct Queue *)malloc(sizeof(struct Queue));
	Q->data = (int *)malloc(sizeof(int));
	Q->front = Q->rear = 0;
	Q->MaxSize = Maxsize;
	return Q;
}
/*判断队列是否为满*/
bool Isfull(struct Queue *Q)
{
	return ((Q->rear+1) % Q->MaxSize == Q->front);
}
/*队列的插入*/
bool AddQ(struct Queue *Q,int x)
{
	if(Isfull(Q))
	{
		cout << "队列已满" << endl;
		return false;
	}
	else
	{
		Q->rear = (Q->rear+1) % Q->MaxSize;
		Q->data[Q->rear] = x;
		return true;
	}
}
/*判断队列是否为空*/
bool IsEmpty(struct Queue *Q)
{
	return (Q->front == Q->rear);
}
/*删除并返回队列头元素*/
int DeleteQ(struct Queue *Q)
{
	if(IsEmpty(Q))
	{
		cout << "队列为空" << endl;
		return ERROR;
	}
	else
	{
		Q->front = (Q->front+1) % Q->MaxSize;
		return Q->data[Q->front];
	}
}

int main()
{
	int n,x;
	struct Queue *Q = CreateQueue(N);
	cout << "请输入数据个数";
	cin >> n;
	while(n--)
	{
		cin >> x;
		AddQ(Q,x);
	}
	//cout << Q->front << " " << Q->rear << endl;
	for(int i = Q->front;i != Q->rear+1;i = (i+1)%Q->MaxSize)
	{
		cout << Q->data[i] << " ";
	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值