Advertisement
Nayeemzaman

queue

Apr 3rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. }*front,*front1,*rear,*temp;
  9. void enq(int data)
  10. {
  11.     if(rear == NULL)
  12.     {
  13.         rear = (struct node*)malloc(sizeof(struct node));
  14.  
  15.         rear->data = data;
  16.         rear->next = NULL;
  17.         front = rear;
  18.     }
  19.     else
  20.     {
  21.         temp = (struct node*)malloc(sizeof(struct node));
  22.  
  23.         rear->next = temp;
  24.         temp->data = data;
  25.         temp->next = NULL;
  26.  
  27.         rear = temp;
  28.     }
  29. }
  30. void deq()
  31. {
  32.     front1 = front;
  33.  
  34.     if(front1 == NULL)
  35.         printf("Your queue is empty\n");
  36.     else
  37.     {
  38.         if(front1->next != NULL)
  39.         {
  40.             front1 = front1->next;
  41.             printf("Dequeued value %d\n",front->data);
  42.             free(front);
  43.             front = front1;
  44.         }
  45.         else
  46.         {
  47.             printf("Dequeued value %d\n",front->data);
  48.             free(front);
  49.             front = NULL;
  50.             rear = NULL;
  51.         }
  52.     }
  53. }
  54. void display()
  55. {
  56.     front1 = front;
  57.     if(front1 == NULL && rear == NULL)
  58.         printf("Queue is empty\n");
  59.  
  60.     while(front1 != rear)
  61.     {
  62.         printf("%d\n",front1->data);
  63.         front1 = front1->next;
  64.     }
  65.     printf("%d\n",front1->data);
  66. }
  67.  
  68. int main()
  69. {
  70.     front = rear = NULL;
  71.     enq(10);
  72.     enq(20);
  73.     enq(30);
  74.     printf("Queue before dequeue:\n");
  75.     display();
  76.     deq();
  77.     printf("Queue after dequeue:\n");
  78.     display();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement