循环队列-c++

 队列是一种常用的数据结构,但我们平常实现的队列有一个缺陷。想象一下,假设一个已经填满的队列,我们开始不断弹出队头元素,这时候我们会发现,虽然队尾看上去是满了,但实际上前面许多空间并没有使用,造成极大浪费。那么我们该怎么解决这个问题呢?

我们可以采用循环队列的方式。既然前面还没用,我们再绕回到前面不就好了吗。

具体看下图

 如果能理解这一点,那么下面的代码也就很显然了。

#include<iostream>
#include<cassert>
using namespace std;

#define CAPACITY 5

template<class T>
class LoopQueue
{
private:
	T* data;
	int front;//头
	int tail;//尾
	int capacity;

	void resize(int newcapacity)
	{
		T* newdata = new T[newcapacity];
		for (int i = 0; i < get_size(); i++)
		{
			newdata[i] = data[(i + front) % capacity];
		}
		data = newdata;
		front = 0;
		tail = get_size();
		data = newdata;
		newdata = nullptr;
	}

public:
	LoopQueue()
	{
		data = new T[CAPACITY];
		front = 0;
		tail = 0;
		capacity = CAPACITY;
	}

	LoopQueue(int cab)
	{
		data = new T[cab];
		front = 0;
		tail = 0;
		capacity = cab;
	}

	~LoopQueue()
	{
		delete[]data;
		data = nullptr;
	}

	bool isempty()
	{
		return front == tail;
	}

	int get_size()
	{
		return (tail + capacity - front) % capacity;
	}

	void enque(T elem)
	{
		if ((tail + 1) % capacity == front)
		{
			resize(2 * capacity);
		}

		data[tail++] = elem;
		tail = (tail + 1) % capacity;
	}

	T deque()
	{
		assert(front != tail);
		T res = data[front];
		front = (front + 1) % capacity;

		if (get_size() == capacity / 4 && capacity / 2) resize(capacity / 2);

		return res;
	}

	T get_front()
	{
		assert(front != tail);
		return data[front];
	}

};

应姐妹推荐,拿出全球高考秦究和游惑海报镇楼。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列是一种特殊的队列,它可以充分利用数组空间,实现队列的基本操作。在循环队列中,队列的头尾相连,当队列满时,可以将队列头指针指向数组的第一个位置,实现循环利用。C++中实现循环队列可以使用数组和指针结构体来实现。 以下是使用数组实现循环队列的C++代码: ```c++ #define MAXSIZE 10 // 定义循环队列的最大长度 typedef int ElementType; // 定义队列元素类型 class CircularQueue { private: ElementType data[MAXSIZE]; // 队列数组 int front; // 队头指针 int rear; // 队尾指针 public: CircularQueue() { // 构造函数,初始化队头和队尾指针 front = rear = 0; } bool isEmpty() { // 判断队列是否为空 return front == rear; } bool isFull() { // 判断队列是否为满 return (rear + 1) % MAXSIZE == front; } void enQueue(ElementType x) { // 入队 if (isFull()) { cout << "Queue is full!" << endl; return; } data[rear] = x; rear = (rear + 1) % MAXSIZE; } void deQueue() { // 出队 if (isEmpty()) { cout << "Queue is empty!" << endl; return; } front = (front + 1) % MAXSIZE; } void printQueue() { // 打印队列 if (isEmpty()) { cout << "Queue is empty!" << endl; return; } int i = front; while (i != rear) { cout << data[i] << " "; i = (i + 1) % MAXSIZE; } cout << endl; } }; ``` 使用指针结构体实现循环队列的C++代码如下: ```c++ #define MAXSIZE 10 // 定义循环队列的最大长度 typedef int ElementType; // 定义队列元素类型 struct Queue { ElementType* data; // 队列数组 int front; // 队头指针 int rear; // 队尾指针 int size; // 队列长度 int capacity; // 队列容量 }; Queue* createQueue(int k) { // 创建空队列 Queue* q = new Queue; q->data = new ElementType[k]; q->front = q->rear = 0; q->size = 0; q->capacity = k; return q; } bool isEmpty(Queue* q) { // 判断队列是否为空 return q->size == 0; } bool isFull(Queue* q) { // 判断队列是否为满 return q->size == q->capacity; } void makeEmpty(Queue* q) { // 置空 q->front = q->rear = 0; q->size = 0; } int lengthOfQueue(Queue* q) { // 计算队列长度 return q->size; } void enQueue(Queue* q, ElementType x) { // 入队 if (isFull(q)) { cout << "Queue is full!" << endl; return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % q->capacity; q->size++; } void deQueue(Queue* q) { // 出队 if (isEmpty(q)) { cout << "Queue is empty!" << endl; return; } q->front = (q->front + 1) % q->capacity; q->size--; } void printQueue(Queue* q) { // 打印队列 if (isEmpty(q)) { cout << "Queue is empty!" << endl; return; } int i = q->front; while (i != q->rear) { cout << q->data[i] << " "; i = (i + 1) % q->capacity; } cout << endl; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值