简单队列的实现(基于链表)

本文介绍了一种使用链表来实现队列数据结构的方法,并提供了完整的C++代码示例。该队列支持基本操作如入队、出队、查看队首元素等。

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

实现一个简单的队列,基于链表。

#ifndef _LINKEDQUEUE_
#define _LINKEDQUEUE_

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

template <typename T>
class LinkedQueue
{
public:
	LinkedQueue() : _size(0),_head(NULL),_tail(NULL) {}
	~LinkedQueue() { Clear(); }
	LinkedQueue(const LinkedQueue& rhs) : _size(0),_head(NULL),_tail(NULL)
	{
		operator=(rhs);
	}

	const LinkedQueue& operator= (const LinkedQueue& rhs)
	{
		if( this != &rhs )
		{
			Clear();
			LinkedQueueNode* temp = rhs._head;
			while( temp != NULL)
			{
				this->Enqueue(temp->data);
				temp = temp->next;
			}
		}

		return *this;
	}

	int Size() const { return _size; }
	bool IsEmpty() const { return _size == 0; }
	void Clear()
	{
		_size = 0;

		LinkedQueueNode* temp = _head;
		while(temp != NULL)
		{
			_head = _head->next;
			delete temp;
			temp = _head;
		}

		_head = NULL;
		_tail = NULL;
	}

	void Print() const
	{
		cout<< "Size=" << _size <<endl;
		LinkedQueueNode* temp = _head;
		while(temp != NULL)
		{
			cout << temp->data << ",";
			temp = temp->next;
		}
		cout << endl;
	}

	void Enqueue(const T& value)
	{
		if(_size == 0)
		{
			_tail = new LinkedQueueNode(value,NULL);
			_head = _tail;
		}
		else
		{
			_tail->next = new LinkedQueueNode(value,NULL);
			_tail = _tail->next;
		}

		++_size;
	}

	T Dequeue()
	{
		if(IsEmpty())
			throw logic_error("Queue is empty");

		LinkedQueueNode* temp = _head;
		T value = temp->data;

		if(_size == 1)
		{
			_head = _tail = NULL;
		}
		else
		{
			_head = _head->next;
		}

		--_size;
		delete temp;
		return value;
	}

	T& Peek() const
	{
		if(IsEmpty())
			throw logic_error("Queue is empty");

		return _head->data;
	}

private:
	class LinkedQueueNode
	{
	public:
		T data;
		LinkedQueueNode* next;
		LinkedQueueNode(const T& v, LinkedQueueNode* n=NULL) : data(v),next(n) {}
	};

private:
	int _size;
	LinkedQueueNode* _head;
	LinkedQueueNode* _tail;
};
#endif

测试代码:

#include "LinkedQueue.cpp"

void LinkedQueueTest1();
void Test( void (*fp)() );

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

void LinkedQueueTest1()
{
	LinkedQueue<int> q;
	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();

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

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

	cout << "q content" << endl;
	q.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;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值