fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. struct node
  5. {
  6. T data ;
  7. node<T>* next = nullptr;
  8.  
  9. node(const T& t) : data(t) {}
  10. };
  11.  
  12. template <typename T>
  13. void append(node<T>* list, node<T>* appendMe)
  14. {
  15. if (!list->next)
  16. list->next = appendMe;
  17. else
  18. append(list->next, appendMe);
  19. }
  20.  
  21. template <typename T>
  22. void append(node<T>*& list, const T& item)
  23. {
  24. if (list == nullptr)
  25. {
  26. list = new node<T>(item);
  27. }
  28. else
  29. append(list, new node<T>(item));
  30. }
  31.  
  32. template <typename T>
  33. void print(const node<T>* list)
  34. {
  35. const node<T>* current = list;
  36. while (current)
  37. {
  38. std::cout << current->data << '\n';
  39. current = current->next;
  40. }
  41. }
  42.  
  43. template <typename T>
  44. void destroy(node<T>* list)
  45. {
  46. node<T>* current = list;
  47. while (current)
  48. {
  49. node<T>* temp = current;
  50. current = current->next;
  51. delete temp;
  52. }
  53.  
  54. }
  55.  
  56. int main()
  57. {
  58. node<int>* list = nullptr;
  59. for (int i = 1; i <= 10; ++i)
  60. append(list, i);
  61.  
  62. print(list);
  63. destroy(list);
  64. }
  65.  
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6
7
8
9
10