Advertisement
AlexandruFilipescu

C++ Lista inlantuita Simplu(Single linked list)

May 29th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // A simple CPP program to introduce
  5. // A linked list
  6.  
  7. class Node {
  8.  
  9.     public:
  10.         int data;
  11.         Node *next;
  12. };
  13.  
  14. // Program to create a simple linked
  15. // List with 3 nodes
  16.  
  17. int main() {
  18.  
  19.     Node* head = NULL;
  20.     Node* second = NULL;
  21.     Node* third = NULL;
  22.  
  23.     // allocate 3 nodes in the heap
  24.  
  25.     head = new Node();
  26.     second = new Node();
  27.     third = new Node();
  28.  
  29.  
  30.  
  31.     head->data = 1; // Assign data in first node
  32.     head->next = second; // Link first node with
  33.  
  34.  
  35.     second->data = 2;
  36.     second->next = third;
  37.     third->data = 3;
  38.     third->next = NULL;
  39.  
  40.    
  41.     cout << endl;
  42.     cout << second->next;
  43.  
  44.     Node* temp = head;
  45.  
  46.     while (temp != NULL) {
  47.         cout << temp->data << " " << endl;
  48.         temp = temp->next;
  49.     }
  50.  
  51.     system("pause");
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement