fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node {
  5. public:
  6. int data;
  7. Node* next;
  8. };
  9.  
  10. // This function prints contents of linked list
  11. // starting from the given node
  12. void printList(Node* n)
  13. {
  14. while (n != NULL) {
  15. cout << n->data << " ";
  16. n = n->next;
  17. }
  18. }
  19.  
  20. // Driver code
  21. int main()
  22. {
  23. Node* head = NULL;
  24. Node* second = NULL;
  25. Node* third = NULL;
  26.  
  27. // allocate 3 nodes in the heap
  28. head = new Node();
  29. second = new Node();
  30. third = new Node();
  31.  
  32. head->data = 1; // assign data in first node
  33. head->next = second; // Link first node with second
  34.  
  35. second->data = 2; // assign data to second node
  36. second->next = third;
  37.  
  38. third->data = 3; // assign data to third node
  39. third->next = NULL;
  40.  
  41. printList(head);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
1 2 3