TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1
5#ifndef DATA_STRUCTURES_NODE_HPP_
6#define DATA_STRUCTURES_NODE_HPP_
7
8#include <iostream>
9#include <memory>
10#include <vector>
11
15template <class ValueType>
16struct Node {
17 using value_type = ValueType;
18 ValueType data = {};
19 std::shared_ptr<Node<ValueType>> next = {};
20};
21
22template <typename Node, typename Action>
23void traverse(const Node* const inNode, const Action& action) {
24 if (inNode) {
25 action(*inNode);
26 traverse(inNode->next.get(), action);
27 }
28}
29
30template <typename Node>
31void display_all(const Node* const inNode) {
32 traverse(inNode,
33 [](const Node& curNode) { std::cout << curNode.data << " "; });
34}
35
36template <typename Node>
37std::vector<typename Node::value_type> push_all_to_vector(
38 const Node* const inNode, const std::size_t expected_size = 0) {
39 std::vector<typename Node::value_type> res;
40 res.reserve(expected_size);
41 traverse(inNode,
42 [&res](const Node& curNode) { res.push_back(curNode.data); });
43 return res;
44}
45
46#endif // DATA_STRUCTURES_NODE_HPP_