You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
774 B
C++
36 lines
774 B
C++
#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';
|
|
}
|