队列模拟实现
来源:互联网 发布:网络客服工资怎么算 编辑:程序博客网 时间:2024/06/06 01:23
队列的特点:
先进先出/后进后出
队列的常见操作:
Push——往队尾插入一个元素
Pop——从队头删除一个元素
Front——返回队列的第一个元素
Back——返回队列的最后一个元素
Size——求队列的元素个数
Empty——判断队列是否为空
队列的模拟实现:
#include<assert.h>template<class T>struct QueueNode{T _data;QueueNode<T>* _next;QueueNode(const T& data): _data(data), _next(NULL){}};template<class T>class Queue{typedef QueueNode<T> Node;public:Queue()//构造函数: _head(NULL), _tail(NULL){}~Queue()//析构函数{Node* cur = _head;while (cur){Node* del = cur;cur = cur->_next;delete del;}}void Push(const T& data)//在末尾插入一个数{if (_head == NULL)//为空{_head = _tail = new Node(data);}else //不为空{_tail->_next = new Node(data);_tail = _tail->_next;}}void Pop()//删除第一个元素{if (_head == _tail)//只有一个元素时{delete _head;_head = _tail = NULL;}else //有多个元素时{Node* del = _head;_head = _head->_next;delete del;}}size_t Size()//返回队列中元素的个数{size_t count = 0;Node* cur = _head;while (cur){++count;cur = cur->_next;}return count;}T& Front()//返回队列中的第一个元素{assert(_head);return _head->_data;}T& Back()//返回队列的最后一个元素{assert(_tail);return _tail->_data;}bool Empty()//判断一个队列是否为空{return _head == NULL;}protected:Node* _head;Node* _tail;};void TestQueue(){Queue<int> q;q.Push(1);q.Push(2);q.Push(3);q.Push(4);q.Push(5);cout << q.Size() << endl;while (!q.Empty()){cout << q.Front() << " ";q.Pop();}cout << endl;}#include<iostream>using namespace std;#include "queue.h"int main(){TestQueue();system("pause");return 0;}
0 0
- 队列的模拟实现
- 模拟实现 队列
- 队列模拟实现
- 模拟实现循环队列
- 模拟实现队列
- 模拟实现队列
- 【C++】模拟实现队列
- MYSQL数据库模拟队列实现
- 利用优先级队列实现事件驱动模拟
- java模拟阻塞队列的实现
- 【c++】模拟实现顺序队列方法一
- 【c++】模拟实现顺序队列方法二
- java 使用线程模拟 队列实现
- 两个栈模拟实现一个队列
- 【C++】优先级队列的模拟实现
- 【算法】用两个栈模拟实现队列
- 栈和队列,以及模拟实现
- (C++)栈和队列的模拟实现
- 3576: [Hnoi2014]江南乐
- HDU5936 Difference 【中途相遇法】
- [LeetCode]222. Count Complete Tree Nodes
- 文章标题
- Q102:光线追踪场景(2)——PLYs(多种模型汇集)
- 队列模拟实现
- 算法学习之路(3):二分查找法
- HTML5中的Canvas元素
- autodraw有意思的开源项目
- 关于Web API 2.0中的Options请求返回405的问题
- 使用keepalived实现双机热备
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 4.14
- C#学习笔记--关于银行存取款的小实验