SHARE
TWEET

Unqueue Dequeue

ahsanhabib1080 Dec 14th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct queue
  4. {
  5.     int data;
  6.     struct queue* next;
  7. } node;
  8.  
  9. node* front = NULL;
  10. node* rear = NULL;
  11.  
  12. void Enqueue(int x)
  13. {
  14.     node* temp =
  15.         (node*)malloc(sizeof(node));
  16.     temp->data =x;
  17.     temp->next = NULL;
  18.     printf("%d is added to the Queue list\n\n",x);
  19.     if(front == NULL && rear == NULL)
  20.     {
  21.         front = rear = temp;
  22.         return;
  23.     }
  24.     rear->next = temp;
  25.     rear = temp;
  26. }
  27.  
  28. void Dequeue()
  29. {
  30.     node* temp = front;
  31.     if(front == NULL)
  32.     {
  33.         printf("Queue is Empty\n");
  34.         return;
  35.     }
  36.     if(front == rear)
  37.     {
  38.         front = rear = NULL;
  39.     }
  40.     else
  41.     {
  42.         front = front->next;
  43.     }
  44.     free(temp);
  45.     printf("Element is deleted from Queue\n");
  46.  
  47. }
  48.  
  49. void Print()
  50. {
  51.     node* temp = front;
  52.     while(temp != NULL)
  53.     {
  54.         printf("%d ",temp->data);
  55.         temp = temp->next;
  56.     }
  57.     printf("\n");
  58. }
  59. int main()
  60. {
  61.     printf("Enter 1 to enqueue\nEnter 2 to Dequeue\nEnter 3 to print\n");
  62.     int a,b;
  63.     scanf("%d",&a);
  64.     switch(a)
  65.     {
  66.     case 1:
  67.         printf("Enter value to add to the queue\n");
  68.         scanf("%d",&b);
  69.  
  70.         Enqueue(b);
  71.         main();
  72.     case 2:
  73.         Dequeue();
  74.         main();
  75.     case  3:
  76.         Print();
  77.         main();
  78.     default:
  79.         printf("Wrong Keyword!\n");
  80.         main();
  81.  
  82.     }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top