Advertisement
Arnab_Manna

Queue SINGLE LINK LIST

Nov 6th, 2022
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int data;
  7.     struct node *link;
  8. }N;
  9.  
  10. N *f;
  11. N *r;
  12.  
  13.  
  14. N *getnode()
  15. {
  16.     N *New;
  17.     New=(N*)malloc(sizeof(N*));
  18.     New->data=0;
  19.     New->link=NULL;
  20.     return New;
  21. }
  22.  
  23. void Insert(int x)
  24. {
  25.     N *New;
  26.     New=getnode();
  27.     New->data=x;
  28.    
  29.     if(f==NULL)
  30.     {
  31.         f=New;
  32.         r=f;
  33.     }
  34.     else
  35.     {
  36.         r->link=New;
  37.         r=New;
  38.     }
  39. }
  40. void Delete()
  41. {
  42.     N *x,*y;
  43.     x=f;
  44.     if(f==NULL)
  45.     {
  46.         printf("underfow\n");
  47.     }
  48.     else
  49.     {
  50.         if(f==r)
  51.         {
  52.             f=NULL;
  53.             r=NULL;
  54.         }
  55.         else
  56.         {
  57.             while(1)
  58.             {
  59.                 y=x;
  60.                 x=x->link;
  61.                 if(x==r)
  62.                 {
  63.                     break;
  64.                 }
  65.             }
  66.             r=y;
  67.             r->link=NULL;
  68.             free(x);
  69.         }
  70.     }
  71. }
  72.  
  73. void Display()
  74. {
  75.     N *x;
  76.     x=f;
  77.     if(f==NULL)
  78.     {
  79.         printf("underfow\n");
  80.     }
  81.     else
  82.     {
  83.         while(x!=NULL)
  84.         {
  85.             printf("%d\n",x->data);
  86.             x=x->link;
  87.         }
  88.     }
  89. }
  90.  
  91. int main()
  92. {
  93.     int n,ch;
  94.     f=NULL;
  95.     r=NULL;
  96.     while(1)
  97.     {
  98.         printf("1>Insert\n2>Delete\n3>display\n4>exit\nenter the number :");
  99.         scanf("%d",&ch);
  100.         switch(ch)
  101.         {
  102.             case 1:
  103.                 printf("enter the data :");
  104.                 scanf("%d",&n);
  105.                 Insert(n);
  106.                     Display();
  107.                 break;
  108.             case 2:        
  109.                 Delete();
  110.                 printf("deleted\n");
  111.                     Display();
  112.                 break;
  113.             case 3:
  114.                 Display();
  115.                 break;
  116.             case 4:
  117.                 exit(0);
  118.             default:
  119.                 printf("invaid choice");
  120.         }
  121.     }
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement