Advertisement
Guest User

Insert_new_node_two_times

a guest
Feb 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct my
  5. {
  6.   int data;
  7.   struct my *then;
  8. }my;
  9.  my *head = NULL;
  10. int main()
  11. {
  12.     int i,m,n,j=0;
  13.     my *coming,*again;
  14.     printf("Input the number of nodes : ");
  15.     scanf("%d",&m);
  16.        for(i=1;i<=m;i++)
  17.        {
  18.            printf("Inpute data for node %d : ",i);
  19.            scanf("%d",&n);
  20.            coming = malloc(sizeof(my));
  21.            coming->data = n;
  22.            coming->then = NULL;
  23.            if(head==NULL)
  24.            {
  25.                head = coming;
  26.            }
  27.            else
  28.            {
  29.                again = head;
  30.                 while(again -> then != NULL)
  31.                 {
  32.                     again = again->then;
  33.                 }
  34.                 again->then = coming;
  35.            }
  36.         }
  37.         again=head;
  38.         printf("Data entered in the list are :\n");
  39.         while(again!=NULL)
  40.         {
  41.         printf(" Data=%d\n",again->data);
  42.         again=again->then;
  43.         }
  44.         printf("\nInput the number of data to insert in the beginning of the list : ");
  45.         scanf("%d",&m);
  46.         for(i=1;i<=m;i++)
  47.        {
  48.            printf("Inpute data for node %d : ",i);
  49.            scanf("%d",&n);
  50.            coming = malloc(sizeof(my));
  51.            coming->data = n;
  52.            coming->then = NULL;
  53.            if(head==NULL)
  54.            {
  55.                head = coming;
  56.            }
  57.            else
  58.            {
  59.                coming->then = head;
  60.                head = coming;
  61.            }
  62.        }
  63.         coming=head;
  64.         printf("\nData after inserted in the list are :\n");
  65.         while(coming!=NULL)
  66.         {
  67.         printf(" Data=%d\n",coming->data);
  68.         coming=coming->then;
  69.         j++;
  70.         }
  71.         printf("Total Nodes: %d\n",j);
  72.  
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement