c顺序队

#include <iostream>
#include <malloc.h>
using namespace std;

#define maxSize 100

typedef struct {
	char data[maxSize];
	int front, rear;
}sqQueue;
//初始化队列
void InitQueue(sqQueue* &q) {
	q = (sqQueue*)malloc(sizeof(sqQueue));
	q->front = q->rear = 0;
}
//销毁队列
void DestroyQueue(sqQueue*& q) {
	free(q);
}
//判断队列是否为空
bool EmptyQueue(sqQueue* q) {
	return (q->front == q->rear);
}
//进队
bool EnterQueue(sqQueue*& q, char c) {
	//判断是否队满
	if ((q->rear+1)%maxSize == q->front)
	{
		return false;
	}
	else {
		q->rear = (q->rear + 1) % maxSize;
		q->data[q->rear] = c;
		return true;
	}
}
//出队
bool OutQueue(sqQueue*& q, char &c) {
	//判断是否队空
	if (q->rear == q->front)
	{
		return false;
	}
	else {
		q->front = (q->front + 1) % maxSize;
		c = q->data[q->front];
		return true;
	}
}
int main() {
	sqQueue* q = NULL;
	char c = '\0';
	InitQueue(q);
	if (!(EnterQueue(q, 'a')))
	{
		cout << "队满了";
	}
	if (!(EnterQueue(q, 'b')))
	{
		cout << "队满了";
	}
	if (!(EnterQueue(q, 'c')))
	{
		cout << "队满了";
	}
	cout << (EmptyQueue(q) ? "空" : "非空") << endl;
	if (OutQueue(q,c) == false)
	{
		cout << "队空,不能出对";
	}
	else {
		cout << c << endl;
	}
	if (!(EnterQueue(q, 'd')))
	{
		cout << "队满了";
	}
	if (!(EnterQueue(q, 'e')))
	{
		cout << "队满了";
	}
	if (!(EnterQueue(q, 'f')))
	{
		cout << "队满了";
	}
	while (!EmptyQueue(q))
	{
		OutQueue(q, c);
		cout << c << ' ';
	}
	cout << endl;
	cout << (EmptyQueue(q) ? "空" : "非空") << endl;
	//释放队列
	DestroyQueue(q);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值