Algorithms_in_C++
1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1
/**
2
* @file
3
* @brief Provides Node class and related utilities
4
**/
5
#ifndef DATA_STRUCTURES_NODE_HPP_
6
#define DATA_STRUCTURES_NODE_HPP_
7
8
#include <iostream>
/// for std::cout
9
#include <memory>
/// for std::shared_ptr
10
#include <vector>
/// for std::vector
11
12
/** Definition of the node as a linked-list
13
* \tparam ValueType type of data nodes of the linked list should contain
14
*/
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_
std::cout
std::vector::push_back
T push_back(T... args)
std::vector::reserve
T reserve(T... args)
std::shared_ptr
std::size_t
Node
Definition
linkedlist_implentation_usingarray.cpp:14
std::vector
data_structures
node.hpp
Generated by
1.12.0