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 */
15template <class ValueType>
16struct Node {
17 using value_type = ValueType;
18 ValueType data = {};
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>
38 const Node* const inNode, const std::size_t expected_size = 0) {
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_
T push_back(T... args)
T reserve(T... args)
Definition linkedlist_implentation_usingarray.cpp:14