TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
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
15
template
<
class
ValueType>
16
struct
Node
{
17
using
value_type = ValueType;
18
ValueType data = {};
19
std::shared_ptr<Node<ValueType>> next = {};
20
};
21
22
template
<
typename
Node,
typename
Action>
23
void
traverse(
const
Node
*
const
inNode,
const
Action& action) {
24
if
(inNode) {
25
action(*inNode);
26
traverse(inNode->next.get(), action);
27
}
28
}
29
30
template
<
typename
Node>
31
void
display_all(
const
Node
*
const
inNode) {
32
traverse(inNode,
33
[](
const
Node
& curNode) { std::cout << curNode.data <<
" "
; });
34
}
35
36
template
<
typename
Node>
37
std::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_
Node
Definition
linkedlist_implentation_usingarray.cpp:14
data_structures
node.hpp
Generated by
1.18.0