× back Queue definition Operations associated with queue Working of queue Applications of queue Linear queue Circular queue Double Ended queue Priority queue Queue Implementation using linked list Circular Queue Implementation using linked list
Next Topic → ← Previous Topic

Queue

Operations associated with a Queue in C

Working of Queue Data Structure

Applications of Queue Data Structure

Implementation of Linear Queue using array in C

                  
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int rear = -1;
int front = -1;
void enqueue()
{
    if (rear == MAX - 1)
    {
        printf("\nQueue is full");
        return;
    }
    if (front == -1 && rear == -1)
        front = rear = 0;
    else
        rear++;
    printf("\nEnter the element : ");
    scanf("%d", &queue[rear]);
}
void dequeue()
{
    if (front == -1 && rear == -1)
    {
        printf("\nQueue is empty");
        return;
    }
    int val = queue[front];
    if (front == rear)
        front = rear = -1;
    else
        front++;
    printf("\n%d is deleted", val);
}
void display()
{
    int i;
    if (front == -1 && rear == -1)
    {
        printf("\nQueue is empty\n");
        return;
    }
    for (i = front; i <= rear; i++)
    {
        printf("\n%d", queue[i]);
    }
}
int main()
{
    int ch;
    while (1)
    {
        printf("\n------- MENU -------");
        printf("\n1.Enqueue Operation\n");
        printf("2.Dequeue Operation\n");
        printf("3.Display the Queue\n");
        printf("4.Exit\n");
        printf("Enter your choice of operations : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
        default:
            printf("\nIncorrect choice \n");
        }
    }
    return 0;
}
                  
               

Circular queue

Problems with linear queue:

To Save us from this problem we use circular queue.

                  
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int rear = -1;
int front = -1;
void enqueue()
{
    if ((rear + 1) % MAX == front)
    {
        printf("\nQueue is full");
        return;
    }
    if (front == -1 && rear == -1)
        front = rear = 0;
    else
        rear = (rear + 1) % MAX;
    printf("\nEnter the element : ");
    scanf("%d", &queue[rear]);
}
void dequeue()
{
    if (front == -1 && rear == -1)
    {
        printf("\nQueue is empty");
        return
    }
    int val = queue[front];
    if (front == rear)
        front = rear = -1;
    else
        front = (front + 1) % MAX;
    printf("\n%d is deleted", val);
}
void display()
{
    int i;
    if (front == -1 && rear == -1)
    {
        printf("\nQueue is empty\n");
        return;
    }
    if (front <= rear)
    {
        for (i = front; i <= rear; i++)
            printf("%d\n", queue[i]);
    }
    else
    {
        for (i = front; i < MAX; i++)
            printf("%d\n", queue[i]);
        for (i = 0; i <= rear; i++)
            printf("%d\n", queue[i]);
    }
}
int main()
{
    int ch;
    while (1)
    {
        printf("\n------- MENU FOR CIRCULAR QUEUE -------");
        printf("\n1.Enqueue Operation\n");
        printf("2.Dequeue Operation\n");
        printf("3.Display the Queue\n");
        printf("4.Exit\n");
        printf("Enter your choice of operations : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
        default:
            printf("\nIncorrect choice \n");
        }
    }
    return 0;
}
                  
               

Deque (Double Ended Queue)

Types of Deque

  • Input restricted: Here insertion is allowed only from one end but deletion is allowed from both the ends.
  • Output restricted: Here deletion is allowed only from one end but insertion is allowed from both the ends.

Types of operation performed on deque

  1. Insert at front
  2. Delete from front
  3. Insert at rear
  4. Delete from rear

Application of deque

  • It is used to perform redo and undo operation.
  • It can also be used as a palindrome checker.
  • It is used in multi-processor scheduling.
                  
#include <stdio.h>
#include <stdlib.h>
#define N 5
int deque[N];
int F = -1, R = -1;
void display();
void enqueueFront();
void enqueueRear();
void dequeueFront();
void dequeueRear();
int main()
{
   int ch;
   while (1)
   {
      printf("\n------- MENU FOR DEQUE -------");
      printf("\n1.Enqueue Front Operation\n");
      printf("2.Enqueue Rear Operation\n");
      printf("3.Dequeue  Front Operation\n");
      printf("4.Dequeue  Rear Operation\n");
      printf("5.Display the Queue\n");
      printf("6.Exit\n");
      printf("Enter your choice of operations : ");
      scanf("%d", &ch);
      switch (ch)
      {
      case 1:
         enqueueFront();
         break;
      case 2:
         enqueueRear();
         break;
      case 3:
         dequeueFront();
         break;
      case 4:
         dequeueRear();
         break;
      case 5:
         display();
         break;
      case 6:
         exit(0);
      default:
         printf("\nIncorrect choice \n");
      }
   }
   return 0;
}

void display()
{
   int i;
   if (F == -1 && R == -1)
      printf("\nQueue is empty\n");
   else
   {
      if (F <= R)
      {
         for (i = F; i <= R; i++)
            printf("%d\n", deque[i]);
      }
      else
      {
         for (i = F; i < N; i++)
            printf("%d\n", deque[i]);
         for (i = 0; i <= R; i++)
            printf("%d\n", deque[i]);
      }
   }
}

void enqueueFront()
{
   if ((F == 0 && R == N - 1) || (F == R + 1))
   {
      printf("\nDeque is Full\n");
      return;
   }
   else if (F == -1 && R == -1)
      F = R = 0;
   else if (F == 0)
      F = N - 1;
   else
      F--;
   printf("\nEnter the value you want to enter : ");
   scanf("%d", &deque[F]);
}

void enqueueRear()
{
   if ((F == 0 && R == N - 1) || (F == R + 1))
   {
      printf("\nDeque is Full\n");
      return;
   }
   else if (F == -1 && R == -1)
      F = R = 0;
   else if (R == N - 1)
      R = 0;
   else
      R++;
   printf("\nEnter the value you want to enter : ");
   scanf("%d", &deque[R]);
}

void dequeueFront()
{
   int val;
   if (F == -1 && R == -1)
   {
      printf("\nDeque is Empty\n");
      return;
   }
   else if (F == R)
   {
      val = deque[F];
      F = R = -1;
   }
   else if (F == N - 1)
   {
      val = deque[F];
      F = 0;
   }
   else
   {
      val = deque[F];
      F++;
   }
   printf("\n%d is deleted", val);
}
void dequeueRear()
{
   int val;
   if (F == -1 && R == -1)
   {
      printf("\nDeque is Empty\n");
      return;
   }
   else if (F == R)
   {
      val = deque[R];
      F = R = -1;
   }
   else if (R == 0)
   {
      val = deque[R];
      R = N - 1;
   }
   else
   {
      val = deque[R];
      R--;
   }
   printf("\n%d is deleted", val);
}
                  
              

Priority queue

Types of Priority Queue:

  1. Ascending Order Priority Queue:
    • Here the element with a lower priority value is given a higher priority in the priority list.
    • For example, if we have the following elements in a priority queue arranged in ascending order like 4,6,8,9,10. Here, 4 is the smallest number, therefore, it will get the highest priority in a priority queue and so when we dequeue from this type of priority queue, 4 will remove from the queue and dequeue returns 4.
  2. Descending Oder Priority Queue:
    • Here the element with higher priority value is given a higher priority in the priority list.

Difference between Priority queue and normal queue?

  • There is no priority attached to elements in a queue, the rule of first-in-first-out(FIFO) is implemented whereas, in a priority queue, the elements have a priority. The elements with higher priority are served first.

Queue implementation using linked list

                                    
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
   int data;
   struct node *next;
} node;
node *front = NULL;
node *rear = NULL;
void enqueue();
void dequeue();
void display();

int main()
{
   int choice;
   while (1)
   {
         printf("\n\n-!-!-!-!-!- MENU -!-!-!-!-!-");
         printf("\n1. Enqueue");
         printf("\n2. Dequeue");
         printf("\n3. DISPLAY");
         printf("\n4. EXIT");
         printf("\n Enter your choice : ");
         scanf("%d", &choice);
         switch (choice)
         {
         case 1:
            enqueue();
            break;
         case 2:
            dequeue();
            break;
         case 3:
            display();
            break;
         case 4:
            exit(0);
         }
   }
   return 0;
}

void enqueue()
{
   node *temp = (node *)malloc(sizeof(node));
   if (temp == NULL)
   {
         printf("\nOverflow");
         return;
   }
   printf("\nEnter a digit : ");
   scanf("%d", &temp->data);
   temp->next = NULL;
   if (front == NULL)
   {
         front = rear = temp;
   }
   else
   {
         rear->next = temp;
         rear = temp;
   }
}

void dequeue()
{
   if (front == NULL)
   {
         printf("\nUnderflow");
         return;
   }
   node *temp = front;
   if (front == rear)
   {
         front = rear = NULL;
   }
   else
   {
         front = front->next;
   }
   printf("\n%d is deleted", temp->data);
   free(temp);
}

void display()
{
   if (front == NULL)
   {
         printf("\nUnderflow");
         return;
   }
   node *temp = front;
   while (temp != NULL)
   {
         printf("%d ---> ", temp->data);
         temp = temp->next;
   }
}
                  
              

Circular Queue implementation using linked list

                  
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
      int data;
      struct node *next;
} node;
node *front = NULL;
node *rear = NULL;
void enqueue();
void dequeue();
void display();

int main()
{
      int choice;
      while (1)
      {
         printf("\n\n-!-!-!-!-!- MENU -!-!-!-!-!-");
         printf("\n1. Enqueue");
         printf("\n2. Dequeue");
         printf("\n3. DISPLAY");
         printf("\n4. EXIT");
         printf("\n Enter your choice : ");
         scanf("%d", &choice);
         switch (choice)
         {
         case 1:
            enqueue();
            break;
         case 2:
            dequeue();
            break;
         case 3:
            display();
            break;
         case 4:
            exit(0);
         }
      }
      return 0;
}

void enqueue()
{
      node *temp = (node *)malloc(sizeof(node));
      if (temp == NULL)
      {
         printf("\nOverflow");
         return;
      }
      printf("\nEnter a digit : ");
      scanf("%d", &temp->data);
      temp->next = front;
      if (front == NULL)
      {
         front = rear = temp;
      }
      else
      {
         rear->next = temp;
         rear = temp;
      }
}

void dequeue()
{
      if (front == NULL)
      {
         printf("\nUnderflow");
         return;
      }
      node *temp = front;
      if (front == rear)
      {
         free(front);
         front = rear = NULL;
      }
      else
      {
         front = front->next;
         rear->next = front;
      }
      printf("\n%d is deleted", temp->data);
      free(temp);
}

void display()
{
      if (front == NULL)
      {
         printf("\nUnderflow");
         return;
      }
      node *temp = front;
      while (temp->next != front)
      {
         printf("%d ---> ", temp->data);
         temp = temp->next;
      }
      printf("%d", temp->data);
}
                  
              

References ↓