Advertisement
afrinahoque

Dekhe dis tohh kothay shomossha :3 *Insert at nth*

Oct 26th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     int a;
  6.     struct node *next;
  7. }node;
  8.  
  9. node *head=NULL, *list=NULL;
  10.  
  11. void insert_at_nth(int a)
  12. {
  13.  
  14.     node *N=(node*)malloc(sizeof(node));
  15.     N->a=a;
  16.     N->next=NULL;
  17.  
  18.     int n;
  19.     scanf("%d", &n);
  20.  
  21.     if(n==1)
  22.     {
  23.         N->next=head;
  24.         head=N;
  25.         return;
  26.     }
  27.     else
  28.     {
  29.         list=head;
  30.         n=n-2;
  31.         while(n!=0 && list->next!=NULL)//while(p--)
  32.         {
  33.             list=list->next;
  34.             n--;
  35.         }
  36.         N->next=list->next;
  37.         list->next=N;
  38.     }
  39. }
  40.  
  41. void display()
  42. {
  43.     list=head;
  44.     while(list!=NULL)
  45.     {
  46.         printf("Data = %d\n", list->a);
  47.         list=list->next;
  48.     }
  49. }
  50.  
  51. int main()
  52. {
  53.     int x;
  54.     printf("ENTER THE CASE:\n");
  55.     scanf("%d", &x);
  56.     if(x==0)
  57.     {
  58.         printf("Empty\n");
  59.         return 0;
  60.     }
  61.         node *N=(node*)malloc(sizeof(node));
  62.         printf("ENTER THE VALUE:\n");
  63.         scanf("%d", &N->a);
  64.         head=N;
  65.         N->next=NULL;
  66.         x=x-1;
  67.         while(x--)
  68.         {
  69.             insert_at_nth();
  70.         }
  71.         display();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement