StefiIOE

Kreiranje Na SLL vo c++

Oct 10th, 2020 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. class Node{
  7.     public:
  8.     int data;
  9.     Node *next;
  10.    
  11. };
  12. void print(Node *n){
  13.     while(n!=NULL){
  14.         cout<<n->data <<" ";
  15.         n= n->next;
  16.     }
  17.    
  18. }
  19. int main()
  20. {
  21.   Node* head=NULL;
  22.   Node* second=NULL;
  23.   Node* third=NULL;
  24.  
  25.   //kreiranje na 3 objekti
  26.   head = new Node ();
  27.   second = new Node ();
  28.   third = new Node () ;
  29.  
  30.   head -> data = 1;  // assign data in first node
  31.   head -> next = second;// Link first node with second
  32.   second->data = 2;// assign data to second node
  33.   second -> next = third;   //Link second node with third
  34.   third -> data = 3 ;// assign data to third node
  35.   third -> next = NULL;
  36.   print(head);
  37.     return 0;
  38. }
  39.  
Add Comment
Please, Sign In to add comment