简单队列的实现(基于数组)

本文介绍了一种使用C++实现基于数组的简单队列的方法,包括队列的基本操作如入队、出队、判断是否为空或满等,并通过测试代码验证其实现的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用C++实现一个简单的队列,基于数组来实现。

#ifndef _ARRAYQUEUE_
#define _ARRAYQUEUE_

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

template <class T>
class ArrayQueue
{
public:
	explicit ArrayQueue(int maxSize = 100) : _size(0),_maxSize(maxSize),_front(-1),_rear(-1)
	{
		_values = new T[_maxSize];
	}

	~ArrayQueue()
	{
		delete [] _values;
	}

	ArrayQueue(const ArrayQueue& rhs) : _values(NULL)
	{
		operator=(rhs);
	}

	const ArrayQueue& operator= (const ArrayQueue& rhs)
	{
		if( this != &rhs )
		{
			this->_size = rhs._size;
			this->_maxSize = rhs._maxSize;
			this->_front = rhs._front;
			this->_rear = rhs._rear;

			delete [] _values;
			_values = new T[_maxSize];

			if(_size > 0)
			{
				for(int temp = _front; temp != _rear; temp = (temp+1)%_maxSize)
				{
					_values[temp] = rhs._values[temp];
				}
				_values[_rear] = rhs._values[_rear];
			}
		}

		return *this;
	}

	bool IsEmpty() const
	{
		return _size == 0;
	}

	bool IsFull() const
	{
		return _size == _maxSize;
	}

	int Size() const
	{
		return _size;
	}

	void Clear()
	{
		_size = 0;
		_front = -1;
		_rear = -1;
	}

	void Print() const
	{
		cout << "Size=" << _size << endl;
		cout << "MaxSize=" << _maxSize << endl;
		cout << "Front=" << _front << endl;
		cout << "Rear=" << _rear << endl;
		cout << "IsEmpty=" << IsEmpty() << endl;
		cout << "IsFull=" << IsFull() << endl;

		for(int i=0;i<_maxSize;++i)
		{
			cout << _values[i] << ",";
		}
		cout << endl;
	}

	void Enqueue(const T& value);
	T Dequeue();
	T& Peek() const;

private:
	int _size;
	int _maxSize;
	int _front;
	int _rear;
	T* _values;
};

template <typename T>
void ArrayQueue<T>::Enqueue(const T& value)
{
	if(IsFull())
		throw logic_error("Queue is full");

	if(_size == 0)
	{
		_front = _rear = 0;
	}
	else
	{
		_rear = (_rear + 1) % _maxSize;
	}

	_values[_rear] = value;
	++_size;
}

template <typename T>
T ArrayQueue<T>::Dequeue()
{
	if(IsEmpty())
		throw logic_error("Queue is empty");

	T value;
	if(_size == 1)
	{
		value = _values[_front];
		_front = _rear = -1;
	}
	else
	{
		value = _values[_front];
		_front = (_front + 1) % _maxSize;
	}

	--_size;
	return value;
}

template <typename T>
T& ArrayQueue<T>::Peek() const
{
	if(IsEmpty())
		throw logic_error("Queue is empty");

	return _values[_front];
}
#endif




如下是测试代码:

#include "ArrayQueue.cpp"

void ArrayQueueTest1();
void Test( void (*fp)() );

int main(int argc, char** argv)
{
	Test(ArrayQueueTest1);
	return 0;
}

void ArrayQueueTest1()
{
	ArrayQueue<int> q = ArrayQueue<int>(5);

	q.Print();

	q.Enqueue(1);
	q.Enqueue(2);
	q.Enqueue(3);
	q.Enqueue(4);
	q.Enqueue(5);
	q.Print();

	cout << "Peek=" << q.Peek() << endl;
	cout << "Dequeue=" << q.Dequeue() << endl;
	q.Print();

	q.Enqueue(6);
	q.Print();

	q.Dequeue();
	q.Dequeue();
	q.Print();

	ArrayQueue<int> q1 = q;
	cout<< "q1 content" << endl;
	q1.Print();

	cout << q1.Dequeue() << endl;
	cout << q1.Dequeue() << endl;
	cout << q1.Dequeue() << endl;
	q1.Print();
}

void Test( void (*fp)() )
{
	try
	{
		fp();
	}
	catch(out_of_range e)
	{
		cout<< "Catch Exception:" << e.what() << endl;
	}
	catch(overflow_error e)
	{
		cout<< "Catch Exception:" << e.what() << endl;
	}
	catch(logic_error e)
	{
		cout<< "Catch Exception:" << e.what() << endl;
	}
}



### 使用ADT实现基于无序数组的优先队列 #### 1. 抽象数据类型定义 为了实现基于无序数组的优先队列,首先需要明确定义其抽象数据类型 (Abstract Data Type, ADT) 的操作接口。这些基本操作通常包括: - `insert(item)`:向优先队列中插入一个新的项。 - `remove_max()` 或者 `remove_min()`:移除并返回具有最高/最低优先级的元素。 - `is_empty()`:判断优先队列是否为空。 对于基于无序数组的优先队列来说,在执行`remove_max()`或`remove_min()`时,由于数组中的元素是没有排序的,因此每次都需要遍历整个数组来找到最大值或最小值的位置[^3]。 #### 2. Python 实现示例 下面是一个简单的Python类实现,展示了如何利用无序列表作为底层存储机制构建一个支持最大值提取的最大化优先队列: ```python class PriorityQueueUnorderedArray: def __init__(self): self.items = [] def insert(self, item): """ 插入新项目 """ self.items.append(item) def remove_max(self): """ 移除并返回最大的元素 """ if not self.is_empty(): max_index = 0 for i in range(1, len(self.items)): if self.items[i] > self.items[max_index]: max_index = i return self.items.pop(max_index) def is_empty(self): """ 检查队列是否为空 """ return len(self.items) == 0 def peek_max(self): """ 查看但不移除最大元素 """ if not self.is_empty(): max_item = self.items[0] for item in self.items[1:]: if item > max_item: max_item = item return max_item ``` 此代码片段实现了上述提到的功能,并且通过线性查找的方式找到了当前未排序数组中的最大值来进行移除操作。需要注意的是,这种方法的时间复杂度为O(n),其中n表示队列内的元素数量;而在最坏情况下(即当我们要移除的那个最大值位于数组末端时),还需要额外花费O(n)时间成本用于重新调整剩余部分的数据结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值