队列是一种先入先出的数据结构,其操作与栈类似。有如下操作:
- clear() —— 清空队列
- empty() —— 判断队列是否为空
- push() —— 入队列
- pop() —— 出队列
- front() —— 返回队列的第一个元素
队列的链表实现代码如下:
#include<cassert>
#include<list>
using namespace std;
template<class T>
class myqueue {
public:
myqueue(){}
void clear() {
m_list.clear();
}
bool empty() {
return m_list.empty();
}
void push(const T& e) {
m_list.push_back(e);
}
void pop() {
m_list.pop_front();
}
T& front() {
return m_list.front();
}
private:
list<T> m_list;
};