Advertisement
afrinahoque

Eibar dekh Jackson!!

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