Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. ///insert at end
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. int main()
  12. {
  13.     struct node *prev,*head,*p;
  14.     int n,i;
  15.     scanf("%d",&n);
  16.     head=NULL;
  17.     for(i=0;i<n;i++)
  18.     {
  19.         p=(struct node*)malloc(sizeof(struct node));
  20.         scanf("%d",&p->data);
  21.         p->next=NULL;
  22.         if(head==NULL){
  23.             head=p;
  24.         }
  25.         else{
  26.             prev->next=p;
  27.         }
  28.         prev=p;
  29.     }
  30.     struct node* ptr;
  31.     ptr=head;
  32.  
  33.     int dat;
  34.     printf("Enter data");
  35.     scanf("%d",&dat);
  36.  
  37.     while(ptr!=NULL){
  38.         prev=ptr;
  39.         ptr=ptr->next;
  40.     }
  41.     p=(struct node*)malloc(sizeof(struct node));
  42.  
  43.     p->data=dat;
  44.     prev->next=p;
  45.     p->next=NULL;
  46.  
  47.     ptr=head;
  48.  
  49.     while(ptr!=NULL)
  50.     {
  51.         printf("data : %d\n",ptr->data);
  52.         ptr=ptr->next;
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement