fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class List
  5. {
  6. struct Node
  7. {
  8. int data;
  9. Node *next = nullptr;
  10.  
  11. Node(int data1) : data(data1) { }
  12. };
  13.  
  14. Node *head, *tail;
  15.  
  16. public:
  17. List() : head(nullptr), tail(nullptr) { }
  18. List(const List&) = delete;
  19. List& operator=(const List&) = delete;
  20.  
  21. ~List()
  22. {
  23. Node *n = head;
  24. while (n)
  25. {
  26. Node *next = n->next;
  27. delete n;
  28. n = next;
  29. }
  30. }
  31.  
  32. void push_back(int data)
  33. {
  34. Node **n = (tail) ? &(tail->next) : &head;
  35. *n = new Node(data);
  36. tail = *n;
  37. }
  38.  
  39. friend ostream& operator<<(ostream &os, const List &list)
  40. {
  41. Node *n = list.head;
  42. if (n)
  43. {
  44. os << n->data;
  45. while (n = n->next)
  46. {
  47. os << ',' << n->data;
  48. }
  49. }
  50. return os;
  51. }
  52. };
  53.  
  54. int main()
  55. {
  56. int arr[] = {1, 2, 3, 4, 5, 6};
  57. List list;
  58. for(int elem : arr)
  59. list.push_back(elem);
  60. cout << list;
  61. return 0;
  62. }
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
1,2,3,4,5,6