当前位置:   article > 正文

C++数据结构队列的链式存储和循环队列(含完整代码)_c++队列保存

c++队列保存

队列的链式存储

实现代码

#include <iostream>
using namespace std;
//带头结点的实现方式
//定义节点
template <typename T>
struct QueueNode
{
   
	T data;//数据域
	QueueNode<T>* next;//存放下一个节点的指针
};
template <typename T>
class LinkQueue
{
   
public:
	LinkQueue();//构造函数
	~LinkQueue();//析构函数
	bool EnQueue(const T& e);//入队函数
	bool DeQueue(T& e);//出队函数
	bool GetQueue(T& e);//获取队尾元素
	bool Empty();//判断是否为空队列
	int Queue();//返回队列长度
	void DisplayQueue();//扫描队列
	void ClearQueue();//清空队列
private:
	QueueNode<T>* m_front;//队首
	QueueNode<T>* m_rear;//队尾
	int m_size;//队列长度
	QueueNode<T>* m_head;//头节点
};
template <typename T>
LinkQueue<T>::LinkQueue()
{
   
	QueueNode<T>* m_head = new QueueNode<T>;//新建一个头结点
	m_front = nullptr;
	m_rear = nullptr;
	m_head->next = m_front;
	m_size = 0;
}
template <typename T>
LinkQueue<T>::~LinkQueue()
{
   
	T k;
	while (m_size > 0)
	{
   
		DeQueue(k);
	}//释放队列
	delete m_head;//释放队首
}
template <typename T>
bool LinkQueue<T>::EnQueue(const T& e)
{
   
	QueueNode<T>* p = new QueueNode<T>;//为新的头结点分配空间
	p->data = e;//传入数据域
	if (m_size == 0)
	{
   
		m_rear = p;//将新节点的指针传入队尾
		m_front = p;//将新节点的指针传入队首
		m_size++;//队列长度增加
		return true;
	}
	if (m_size == 1)
	{
   
		m_rear = p;//将新节点的指针传入队头
		m_front->next = m_rear;//将m_rear设为m_front的后继节点
		m_size+&
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/706763
推荐阅读
相关标签
  

闽ICP备14008679号