fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. struct Node {
  6. Node() { cout << this << ": created w/ default" << endl; }
  7.  
  8. Node(const T &value, Node<T> *next = nullptr)
  9. : value(value), next(next)
  10. {
  11. cout << this << ": created w/ value" << endl;
  12. }
  13.  
  14. ~Node() { cout << this << ": destroyed" << endl; }
  15.  
  16. T value{};
  17. Node<T> *next = nullptr;
  18. };
  19.  
  20. int main() {
  21. Node<int> nodes[4];
  22.  
  23. Node<int> *next = nullptr;
  24. for(int i = 3; i >= 0; --i) {
  25. nodes[i] = Node<int>(i+1, next);
  26. next = &nodes[i];
  27. }
  28.  
  29. Node<int> *LL1 = &nodes[0];
  30.  
  31. for(auto *curr = LL1; curr != nullptr; curr = curr->next) {
  32. cout << curr->value << " ";
  33. }
  34. cout << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5548KB
stdin
Standard input is empty
stdout
0x7ffe486b3120: created w/ default
0x7ffe486b3130: created w/ default
0x7ffe486b3140: created w/ default
0x7ffe486b3150: created w/ default
0x7ffe486b3110: created w/ value
0x7ffe486b3110: destroyed
0x7ffe486b3110: created w/ value
0x7ffe486b3110: destroyed
0x7ffe486b3110: created w/ value
0x7ffe486b3110: destroyed
0x7ffe486b3110: created w/ value
0x7ffe486b3110: destroyed
1 2 3 4 
0x7ffe486b3150: destroyed
0x7ffe486b3140: destroyed
0x7ffe486b3130: destroyed
0x7ffe486b3120: destroyed