fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. //singly linked list node structure
  5. class Node{
  6. public:
  7. int data;
  8. Node* next;
  9.  
  10. //constructor to iniatialize a new node with data
  11.  
  12. Node(int new_data) {
  13. this->data = new_data;
  14. this->next = nullptr;
  15. }
  16.  
  17. };
  18.  
  19. int main() {
  20.  
  21. //create the first node(head of the linked list)
  22. Node* head = new Node(10);
  23.  
  24. //Link the second node
  25. head->next = new Node(20);
  26.  
  27. //Link the third node
  28. head->next->next = new Node(30);
  29.  
  30. //Link the fourth node
  31. head->next->next->next = new Node(40);
  32.  
  33. Node* temp = head;
  34.  
  35. while(temp != nullptr) {
  36. cout << temp->data <<" ";
  37. temp = temp->next;
  38. }
  39. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
10 20 30 40