Advertisement
apl-mhd

LinkedList:insertInlast

Feb 23rd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. struct node{   
  6.     int data;      
  7.     struct node *link; 
  8. };
  9.  
  10.  
  11. struct node *head, *start, *root;
  12.  
  13.    
  14. void insert(int x){
  15.    
  16.     struct node *temp = new node();
  17.     //struct node *newNode = new node();
  18.    
  19.     if(head == NULL){
  20.                
  21.         temp->data = x;
  22.         temp->link = NULL;
  23.         head = temp;
  24.        
  25.         }
  26.        
  27.     else{
  28.          temp->data = x;
  29.          temp->link = NULL;
  30.         root = head;
  31.         while(root->link != NULL){
  32.        
  33.             root = root->link;
  34.         }
  35.         root->link = temp;
  36.         //head->link = temp;
  37.    
  38. }
  39.        
  40.     }
  41.    
  42. void print(){
  43.    
  44.     struct node *temp = head;
  45.    
  46.     printf("data:\n");
  47.    
  48.     while(temp != NULL){
  49.    
  50.         printf("%d",temp->data);
  51.        
  52.         temp = temp->link;
  53.        
  54.     }
  55.     printf("\n");
  56.    
  57.    
  58.     }
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.      start = new node();
  63.     head = NULL;
  64.     //start = NULL;
  65.    
  66.     int i,n;
  67.    
  68.     for(i=0; i<10 ;i++){
  69.        
  70.         cin>>n;
  71.         insert(n);
  72.         print();
  73.            
  74.         }
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement