diff --git a/4.3/iterate_max.cpp b/4.3/iterate_max.cpp new file mode 100644 index 0000000..feeb2dc --- /dev/null +++ b/4.3/iterate_max.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +template +struct Node +{ + T value; + std::unique_ptr> next; +}; + +template +T max(const std::unique_ptr>& node, T max_value) +{ + return (node) + ? max(node->next, (node->value > max_value) + ? node->value + : max_value) + : max_value; +} + +int main() +{ + { + using NodeT = Node; + + auto head = std::make_unique(NodeT{"5", nullptr}); + head->next = std::make_unique(NodeT{"12", nullptr}); + head->next->next = std::make_unique(NodeT{"35", nullptr}); + head->next->next->next = std::make_unique(NodeT{"JKFHDJKSFHKJSDJFSDKH", nullptr}); + + std::cout << "Size 1: " << max(head, std::string{}) << '\n'; + } + + { + using NodeT = Node; + + auto head = std::make_unique(NodeT{1, nullptr}); + head->next = std::make_unique(NodeT{34, nullptr}); + head->next->next = std::make_unique(NodeT{12, nullptr}); + head->next->next->next = std::make_unique(NodeT{-4534, nullptr}); + head->next->next->next->next = std::make_unique(NodeT{std::numeric_limits::max(), nullptr}); + + std::cout << "Size 2: " << max(head, std::numeric_limits::min()) << '\n'; + } + + { + using NodeT = Node; + + auto head = std::make_unique(NodeT{'#', nullptr}); + + std::cout << "Size 3: " << max(head, std::numeric_limits::min()) << '\n'; + } + + { + std::cout << "Size empty: " << max(std::unique_ptr>(), std::numeric_limits::min()) << '\n'; + } +}