Deletion in a Queue in C

Deletion in a Queue in C

How to delete an element in a Queue in C programming?

Deletion in a Queue in C is a process of removing an element from the queue . Queue data structures work on the FIFO architecture so the element that has entered first in the list will go out from the list first. Deletion not possible if the queue is empty so you can learn how to insert elements in a queue by clicking the button below. As if there are no elements present in the queue then it will arise the condition of underflow.

Deleting an element in a Queue in C

Steps for dequeuing in C

  1. Create  a main function for the program.
  2. In this main function accept the size and data of the queue by the user.
  3. After that enqueue these element in the queue using an enqueue() function
  4. Now to dequeue, we will be using an function that will remove elements from the queue one by one.
  5. In this dequeue function first we will check if there are elements present in the queue or not.
  6. If there are elements present in the queue then we will move the front of the queue by one step each time ,that is every time when an element is removed from the list.
Deletion in a queue in C 1

Algorithm for dequeuing in C

As we know that Queue is a FIFO type data structure so we will be using following algorithm for deletion of an element from a Queue :-
  • IF(FRONT==-1||FRONT==REAR+1)
  • RETURN
  • ELSE
  • QUEUE[FRONT]=0
  • FRONT=FRONT+1

Code for dequeuing an element in a queue in C

Run
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>

#define MAX 50			//defining the size of queue

int queue[MAX], front = 0, rear = -1, itemCount = 0;

bool isEmpty ()
{
  return itemCount == 0;
}

bool isFull ()
{
  return itemCount == MAX;
}

void enqueue (int data)
{

  if (!isFull ())
    {

      if (rear == MAX - 1)
	{
	  rear = -1;
	}

      queue[++rear] = data;
      itemCount++;
    }
}

void display ()			//function to display the relents of the queue
{
  printf ("The elements in the queue are:");
  int i;
  if (isEmpty ())
    printf (" Queue is Empty");
  else
    for (i = front; i <= rear; i++)
      {

	printf ("%d ", queue[i]);
      }
  printf ("\n");
}

void dequeue ()			//function to delete elements from the queue
{
  if (isEmpty ())
    printf (" Queue is Empty");
  else
    {
      queue[front] = 0;
      front = front + 1;
      itemCount--;
    }
  display ();
}

int queuesize ()
{
  return itemCount;
}

int main ()				
{

  enqueue (3);
  enqueue (5);
  enqueue (9);
  enqueue (1);
  enqueue (12);
  enqueue (15);
  
  int size = queuesize ();

  display ();
  printf ("\nDequeuing elements:\n");
  
  for (int i = 0; i < size; i++)
    {
      dequeue ();
    }

}
The elements in the queue are:3 5 9 1 12 15 

Dequeuing elements:
The elements in the queue are:5 9 1 12 15 
The elements in the queue are:9 1 12 15 
The elements in the queue are:1 12 15 
The elements in the queue are:12 15 
The elements in the queue are:15 
The elements in the queue are: Queue is Empty