/* list.cpp * Date: 2021-11-20 * Author: Aryadev Chavali */ #include #include #include template struct List { T value; struct List *next; ~List() { if (next == nullptr) return; delete next; } }; template List *append(List *lst, T value) { List *node; if (lst == nullptr) { node = new List; node->value = value; node->next = nullptr; return node; } for (node = lst; node->next != nullptr; node = node->next) continue; node->next = new List; node->next->value = value; node->next->next = nullptr; return lst; } /** Reverse a list */ template List *reverse(List *lst, List *prev = nullptr) { auto next = lst->next; lst->next = prev; if (next == nullptr) return lst; return reverse(next, lst); } template std::ostream& operator<<(std::ostream& ostream, const List *lst) { if (!lst) return ostream; ostream << "|" << lst->value << lst->next; return ostream; } int main(void) { auto lst = append(nullptr, 1); for (int i = 2; i < 10; ++i) lst = append(lst, i); std::cout << lst << std::endl; lst = reverse(lst); std::cout << lst << std::endl; delete lst; return 0; }