4.2
This commit is contained in:
parent
ad1ff383ef
commit
6571f0eb89
|
@ -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';
|
||||||
|
}
|
Loading…
Reference in New Issue