Advertisement
afrinahoque

Ekhon thik achhey ki?

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