fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <iostream>
  5.  
  6. class LinkedList{
  7. // Struct inside the class LinkedList
  8. // This is one node which is not needed by the caller. It is just
  9. // for internal work.
  10. struct Node {
  11. int x;
  12. Node *next;
  13. };
  14.  
  15. // public member
  16. public:
  17. // constructor
  18. /* constexpr */ LinkedList() : head(nullptr) {
  19. }
  20.  
  21. // This prepends a new value at the beginning of the list
  22. void addValue(int val){
  23. Node *n = new Node(); // create new Node
  24. n->x = val; // set value
  25. n->next = head; // make the node point to the next node.
  26. // If the list is empty, this is NULL, so the end of the list --> OK
  27. head = n; // last but not least, make the head point at the new node.
  28. }
  29.  
  30. // returns the first element in the list and deletes the Node.
  31. // caution, no error-checking here!
  32. int popValue(){
  33. Node *n = head;
  34. int ret = n->x;
  35.  
  36. head = head->next;
  37. delete n;
  38. return ret;
  39. }
  40.  
  41. // private member
  42. private:
  43. Node *head; // this is the private member variable. It is just a pointer to the first Node
  44. };
  45.  
  46. extern LinkedList list;
  47.  
  48. int my_crazy_variable = ([]{ list.addValue(10); return 0; })();
  49. LinkedList list;
  50.  
  51. int main() {
  52. using namespace std;
  53. cout << my_crazy_variable << endl;
  54. cout << list.popValue() << endl;
  55. return 0;
  56. }
Runtime error #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
0