C++ 数据结构算法 学习笔记(10) - 队列及企业级应用 (续3)

本文详细介绍了C++中的循环队列实现,包括顺序存储、出队和入队操作的优化,以及如何处理队列满和空的情况。作者还展示了循环队列在企业级应用中的潜在优势和注意事项。
摘要由CSDN通过智能技术生成

C++ 数据结构算法 学习笔记(10) - 队列及企业级应用 (续3)

队列循环

在队列的顺序存储中,采用出队方式 2, 删除 front 所指的元素,然后加 1 并返回被删元素。这样可以避免元素 移动,但是也带来了一个新的问题“假溢出”。

在这里插入图片描述

能否利用前面的空间继续存储入队呢?采用循环队列

在这里插入图片描述

循环队列入队, 队尾循环****后移SQ->rear =(SQ->rear+1)

循环队列出队, 队首循环后移SQ->front =(SQ->front+1)%Maxsize;

队空SQ.front=SQ.rear; SQ.rear 和 SQ.front 指向同一个位置

队满(SQ.rear+1) %Maxsize=SQ.front; SQ.rear 向后移一位正好是 SQ.front

在这里插入图片描述

可以分两种情况判断:

  • 如果 SQ.rear>= SQ.front:元素个数为 SQ.rear-SQ.front
  • 如果 SQ.rear<SQ.front:元素个数为 SQ.rear-SQ.front+ Maxsize;

采用取模的方法把两种情况统一为 (SQ.rear-SQ.front+Maxsize)% Maxsize

完整代码实现

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

#define MaxSize 5

typedef int DataType;

typedef struct Queue
{
	DataType queue[MaxSize];
	int front;
	int rear;

}SeqQueue;

void InitQueue(SeqQueue* SQ)
{
	if (!SQ) return;
	SQ->front = SQ->rear = 0;
}

int IsFull(SeqQueue* SQ)
{
	if (!SQ) return 0;
	//if (SQ->rear == MaxSize)
	//{
	//	return 1;
	//}

	//Change to ... Because now is using loop queue
	if ((SQ->rear+1)% MaxSize == SQ->front)
	{
		return 1;
	}
	return 0;
}

int isEmpty(SeqQueue* SQ)
{
	if (!SQ) return 0;
	if (SQ->front == SQ->rear)
	{
		return 1;
	}
	return 0;
}

// Insert the element inside the loop queue
int EnterQueue(SeqQueue* SQ, DataType data)
{
	if (!SQ) return 0;
	if (IsFull(SQ))
	{
		cout << "Not able to minsert the element inside the queue because it's full!" << endl;
		return 0;
	}

	SQ->queue[SQ->rear] = data;
	//SQ->rear++;

	//Change to below code because using loop queue
	SQ->rear = (SQ->rear + 1) % MaxSize;

	return 1;
}


int DeleteQueue2(SeqQueue* SQ, DataType* data)
{
	if (!SQ || isEmpty(SQ))
	{
		cout << "The queue is empty!" << endl;
		return 0;
	}

	//if (SQ->front >= MaxSize)
	//{
	//	cout << "The queue is reach until the end" << endl;
	//	return 0;
	//}

	*data = SQ->queue[SQ->front];
	SQ->front = (SQ->front+1)%MaxSize;
	return 1;
}

//Printout the element of the queue
void PrintQueue(SeqQueue* SQ)
{
	if (!SQ)return;
	int i = SQ->front;
	//while (i < SQ->rear)
	//{
	//	cout << setw(4) << SQ->queue[i];
	//	i++;
	//}
	
	//Change to below because using loop queue
	while (i != SQ->rear)
	{
		cout << setw(4) << SQ->queue[i];
		i=(i+1)%MaxSize;
	}
	cout << endl;
}

//Only want to get the first element, but not remove it from the queue
int GetHead(SeqQueue* SQ, DataType* data)
{
	if (!SQ || isEmpty(SQ))
	{
		cout << "The queue is empty" << endl;
	}
	return *data = SQ->queue[SQ->front];
}

// Clear the queue
void ClearQueue(SeqQueue* SQ)
{
	if (!SQ) return;
	SQ->front = SQ->rear = 0;
}

//Get the length of the queue
int getLength(SeqQueue* SQ)
{
	//if (!SQ) return 0;
	//return (SQ->rear - SQ->front);

	//Change to below code because loop queue
	return (SQ->rear-SQ->front+MaxSize)%MaxSize;
}

int main()
{
	SeqQueue* SQ = new SeqQueue;
	DataType data;

	//Initial the Queue List
	InitQueue(SQ);

	//2. Insert the element inside the queue
	for (int i = 1; i < 8; i++)
	{
		EnterQueue(SQ, i);
	}

	PrintQueue(SQ);

	//3. Comeout from the queue
	for (int i = 0; i < 10; i++)
	{
		if (DeleteQueue2(SQ, &data))
		{
			cout << "The comeout element from the queue by using second function is " << data << endl;
			cout << "The remaining element inside the queue is ";
			cout << getLength(SQ) << endl;
			PrintQueue(SQ);
		}
		else
		{
			cout << "Failed to remove the element from the queue" << endl;
		}
	}

	for (int i = 0; i < 4; i++)
	{
		EnterQueue(SQ, i+10);
	}
	PrintQueue(SQ);
	
	system("pause");
	return 0;
}

注: 代码是从之前的数组队列代码实现拿过来修改的

具体修改部分为

IsFull 函数

int IsFull(SeqQueue* SQ)
{
	if (!SQ) return 0;
	//if (SQ->rear == MaxSize)
	//{
	//	return 1;
	//}

	//Change to ... Because now is using loop queue
	if ((SQ->rear+1)% MaxSize == SQ->front)
	{
		return 1;
	}
	return 0;
}

EnterQueue 函数

int EnterQueue(SeqQueue* SQ, DataType data)
{
	if (!SQ) return 0;
	if (IsFull(SQ))
	{
		cout << "Not able to minsert the element inside the queue because it's full!" << endl;
		return 0;
	}

	SQ->queue[SQ->rear] = data;
	//SQ->rear++;

	//Change to below code because using loop queue
	SQ->rear = (SQ->rear + 1) % MaxSize;

	return 1;
}

DeleteQueue2 函数


int DeleteQueue2(SeqQueue* SQ, DataType* data)
{
	if (!SQ || isEmpty(SQ))
	{
		cout << "The queue is empty!" << endl;
		return 0;
	}

	//if (SQ->front >= MaxSize)
	//{
	//	cout << "The queue is reach until the end" << endl;
	//	return 0;
	//}
	//Change to below code because using loop queue
	*data = SQ->queue[SQ->front];
	SQ->front = (SQ->front+1)%MaxSize;
	return 1;
}

PrintQueue 函数

void PrintQueue(SeqQueue* SQ)
{
	if (!SQ)return;
	int i = SQ->front;
	//while (i < SQ->rear)
	//{
	//	cout << setw(4) << SQ->queue[i];
	//	i++;
	//}
	
	//Change to below because using loop queue
	while (i != SQ->rear)
	{
		cout << setw(4) << SQ->queue[i];
		i=(i+1)%MaxSize;
	}
	cout << endl;
}

getLength 函数

int getLength(SeqQueue* SQ)
{
	//if (!SQ) return 0;
	//return (SQ->rear - SQ->front);

	//Change to below code because loop queue
	return (SQ->rear-SQ->front+MaxSize)%MaxSize;
}
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值