This commit is contained in:
NaiJi ✨ 2021-09-07 16:54:07 +03:00
parent ad1ff383ef
commit 6571f0eb89
1 changed files with 35 additions and 0 deletions

35
4.2/iterate_list.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <iostream>
#include <memory>
template <typename T>
struct Node
{
T value;
std::unique_ptr<Node<T>> next;
};
template <typename T>
std::size_t countStep(const std::unique_ptr<Node<T>>& node, std::size_t i)
{
return (node)
? countStep(node->next, i + 1)
: i;
}
template <typename T>
std::size_t count(const std::unique_ptr<Node<T>>& head)
{
return countStep(head, 0);
}
int main()
{
using NodeT = Node<int>;
auto head = std::make_unique<NodeT>(NodeT{5, nullptr});
head->next = std::make_unique<NodeT>(NodeT{12, nullptr});
head->next->next = std::make_unique<NodeT>(NodeT{35, nullptr});
head->next->next->next = std::make_unique<NodeT>(NodeT{1, nullptr});
std::cout << "Size: " << count(head) << '\n';
}