at3107

Untitled

Feb 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node* link;
  7. };
  8.  
  9. struct node* root=NULL;
  10. void add_begin()
  11. {  
  12.     struct node* temp;
  13.     temp=(struct node*)malloc(sizeof(struct node));
  14.     printf("enter data to add at begin\n");
  15.     scanf("%d",&temp->data);
  16.     temp->link=root;
  17.     root=temp;
  18.     printf("pehle pe add ho gya\n");
  19. }
  20. void traverse()
  21. {
  22.     struct node* temp;
  23.     temp=root;
  24.     if(root==NULL)
  25.         printf("linked list is empty\n");
  26.     else
  27.     {
  28.         while(temp->link!=NULL)
  29.         {
  30.             printf("%d\n",temp->data);
  31.             temp=temp->link;
  32.         }
  33.         printf("%d\n",temp->data);
  34.         //last elemnt k liye
  35.     }
  36. }
  37. void append()
  38. {
  39.     struct node* temp;
  40.     temp=(struct node*)malloc(sizeof(struct node));
  41.     printf("enter data\n");
  42.     scanf("%d",&temp->data);
  43.     temp->link=NULL;
  44.     if(root==NULL)
  45.         root=temp;
  46.     else
  47.     {
  48.         struct node* p;
  49.         p=root;
  50.         while(p->link!=NULL)
  51.             p=p->link;
  52.         p->link=temp;
  53.     }
  54. }
  55. void main()
  56. {
  57.     printf("You can only append the link list\n");
  58.     int count;
  59.     printf("enter number of nodes\n");
  60.     scanf("%d",&count);
  61.     traverse();
  62.     while(count>0)
  63.     {
  64.         append();
  65.         count--;
  66.     }
  67.     printf("linked list ban gya\n");
  68.     traverse();
  69.     add_begin();
  70.     traverse();
  71.     //while isliye use kiya ki jab tak user khud program cut na
  72.     //kare tab tak input leta rhe
  73. }
Add Comment
Please, Sign In to add comment