#include <iostream>
template <typename T>
struct node
{
T data ;
node<T>* next = nullptr;
node(const T& t) : data(t) {}
};
template <typename T>
void append(node<T>* list, node<T>* appendMe)
{
if (!list->next)
list->next = appendMe;
else
append(list->next, appendMe);
}
template <typename T>
void append(node<T>*& list, const T& item)
{
if (list == nullptr)
{
list = new node<T>(item);
}
else
append(list, new node<T>(item));
}
template <typename T>
void print(const node<T>* list)
{
const node<T>* current = list;
while (current)
{
std::cout << current->data << '\n';
current = current->next;
}
}
template <typename T>
void destroy(node<T>* list)
{
node<T>* current = list;
while (current)
{
node<T>* temp = current;
current = current->next;
delete temp;
}
}
int main()
{
node<int>* list = nullptr;
for (int i = 1; i <= 10; ++i)
append(list, i);
print(list);
destroy(list);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CnN0cnVjdCBub2RlCnsKICAgIFQgZGF0YSA7CiAgICBub2RlPFQ+KiBuZXh0ID0gbnVsbHB0cjsKCiAgICBub2RlKGNvbnN0IFQmIHQpIDogZGF0YSh0KSB7fQp9OwoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CnZvaWQgYXBwZW5kKG5vZGU8VD4qIGxpc3QsIG5vZGU8VD4qIGFwcGVuZE1lKQp7CiAgICBpZiAoIWxpc3QtPm5leHQpCiAgICAgICAgbGlzdC0+bmV4dCA9IGFwcGVuZE1lOwogICAgZWxzZQogICAgICAgIGFwcGVuZChsaXN0LT5uZXh0LCBhcHBlbmRNZSk7Cn0KCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgp2b2lkIGFwcGVuZChub2RlPFQ+KiYgbGlzdCwgY29uc3QgVCYgaXRlbSkKewogICAgaWYgKGxpc3QgPT0gbnVsbHB0cikKICAgIHsKICAgICAgICBsaXN0ID0gbmV3IG5vZGU8VD4oaXRlbSk7CiAgICB9CiAgICBlbHNlCiAgICAgICAgYXBwZW5kKGxpc3QsIG5ldyBub2RlPFQ+KGl0ZW0pKTsKfQoKdGVt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