Deletion in a Queue in C

Deletion in a queue in c programming

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.

for deleting an element from 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

for deletion from a Queue in C

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

for deleting an element in a queue in C (Dequeuing)

#include<stdio.h>
#include<stdlib.h>
#define MAX 50//defining the size of queue
int queue[MAX],front=-1,rear=-1;

void enqueue(int data) //function to enqueue data
{
	if(rear==MAX-1)
		printf("\nQueue is Full!");
	else
	{
		rear=rear+1;
		queue[rear]=data;
		if(front==-1)
		front=0;
	}
	
}
void disp() //function to display the relents of the queue
{
	printf("\nThe elements in the queue are:");
	int i;
	if(front==-1)
		printf("\nQueue 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(front==-1||front==rear+1)
		printf("\nQueue is Empty");
	else
	{
		queue[front]=0;
		front=front+1;
	}
	disp();
}
int main() //main function for all input and output statements
{
	int data,size;
	printf("Enter the size of queue: ");
	scanf("%d",&size);
	printf("\nEnter Data to Enqueue:\n");
    for(int i=0;i<size;i++)
    {
      scanf("%d",&data);
      enqueue(data);
    }
    disp();
    printf("\nDequeuing elements:\n");
    for(int i=0;i<size;i++)
    {
        dequeue();
    }
    
}
Output:
Enter the size of queue: 5

Enter Data to Enqueue:
11
21
31
41
51

The elements in the queue are:11  21  31  41  51

Dequeuing elements:
 
The elements in the queue are:21  31  41  51  

The elements in the queue are:31  41  51  

The elements in the queue are:41  51  

The elements in the queue are:51

The elements in the queue are: