Advertisement
afrinahoque

Insert at last singly (Hoyna)

Dec 8th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     char ch;
  6.     float a;
  7.     struct node *next;
  8. }node;
  9.  
  10. node *head=NULL, *list=NULL;
  11.  
  12. void insert_at_last()
  13. {
  14.     node *N=(node*)malloc(sizeof(node));
  15.     scanf("%c %f",&N->ch, &N->a);
  16.     N->next=NULL;
  17.  
  18.     if(head==NULL)
  19.     {
  20.         head=N;
  21.         return;
  22.     }
  23.     else
  24.     {
  25.         node *list=head;
  26.         while(list->next!=NULL)
  27.         {
  28.             list=list->next;
  29.         }
  30.         N->next=list->next;
  31.         list->next=N;
  32.     }
  33. }
  34.  
  35. void display()
  36. {
  37.     node *list=head;
  38.     while(list!=NULL)
  39.     {
  40.         printf("Data: %c %.2f\n", list->ch, list->a);
  41.         list=list->next;
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     int i;
  48.     for(i=0; i<3; i++)
  49.     {
  50.         insert_at_last();
  51.     }
  52.     display();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement