核酸检测是控制疫情的有效手段,核酸检测过程中需要按规则进行排队,然而排队的过程中遇见年龄较大的人,会优先排到队首部。设计一个底层容器为链表类模板Listr<T>的适配器,要求实现年龄大于80岁的人优先拍队首的队列,测试main函数中依次在队列中插入18,22,88三个数来模拟不同年龄的三个人,然后进行核酸检测出队并显示。(20分)
时间: 2024-03-30 19:37:28 浏览: 8
好的,我理解了你的问题。下面是实现代码:
```cpp
#include <iostream>
#include <list>
using namespace std;
template <typename T>
class Listr {
public:
void push_front(T val) {
_list.push_front(val);
}
void push_back(T val) {
_list.push_back(val);
}
void pop_front() {
_list.pop_front();
}
void pop_back() {
_list.pop_back();
}
T front() {
return _list.front();
}
T back() {
return _list.back();
}
bool empty() {
return _list.empty();
}
int size() {
return _list.size();
}
private:
list<T> _list;
};
template <typename T>
class ElderPriorityQueue {
public:
void push(T val) {
if (val > 80) {
_list.push_front(val);
} else {
_list.push_back(val);
}
}
void pop() {
_list.pop_front();
}
T top() {
return _list.front();
}
bool empty() {
return _list.empty();
}
int size() {
return _list.size();
}
private:
Listr<T> _list;
};
int main() {
ElderPriorityQueue<int> queue;
queue.push(18);
queue.push(22);
queue.push(88);
while (!queue.empty()) {
int val = queue.top();
cout << "核酸检测出队: " << val << endl;
queue.pop();
}
return 0;
}
```
上述代码实现了一个适配器`Listr`,它是基于标准库`list`容器实现的。同时,我们实现了一个基于`Listr`的适配器`ElderPriorityQueue`,它对于年龄大于80岁的人优先排队。
在`main`函数中,我们依次插入了18、22和88三个数,模拟了不同年龄的三个人排队。然后进行核酸检测出队并显示。运行输出如下:
```
核酸检测出队: 88
核酸检测出队: 18
核酸检测出队: 22
```