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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java