5. Implement Queue<T>
using ForwardList<T>
interface#
点击查看考点
队列, 类层次
Complete the following tasks using the ForwardList
provided below,
Implement
Queue1<T>
usingForwardList<T>
by privately inheritance.Implement
Queue2<T>
usingForwardList<T>
by composition.Define a function template that consumes (get and pop element) all elements of the queue and print them out. After that, explicitly instantiate it with
Queue1<char>
and specialize it withQueue2<int>
.
翻译
用下面提供的
ForwardList<T>
完成以下任务,
私用继承
ForwardList<T>
从而实现Queue1<T>
.组合
ForwardList<T>
从而实现Queue2<T>
.定义一个函数模板, 消耗 (即 get 并 pop) 队列中的所有元素并打印. 然后, 用
Queue1<char>
显式实例化它, 用Queue2<int>
特例化它.
forward_list.hpp#
1#ifndef FORWARD_LIST_HPP
2#define FORWARD_LIST_HPP
3
4template <typename T>
5struct Node {
6 public:
7 Node* next = nullptr;
8 T data;
9};
10
11template <typename T>
12class ForwardList {
13 public:
14 ForwardList();
15
16 int size() const;
17 bool empty() const;
18 T const& front() const;
19 T const& back() const;
20 void remove(Node<T> const* value);
21 void push_front(T const& value);
22 void push_back(T const& value);
23
24 // Returns the first element that equals to value in the range [start_position, list_last_node]
25 // - If `start_position == nullptr`, the range is [list_first_node, list_last_node]
26 // - Returns nullptr if no such element is found
27 Node<T> const* find(Node<T> const* start_position, T const& value) const;
28};
29
30#endif
点击查看参考解答