Dequeue Operation in Queues

by Jasleen Chhabra | Updated on 24 August 2024

The dequeue operation is a fundamental process in queue management, essential for removing elements from the data structure. Operating on the First In, First Out (FIFO) principle, the dequeue operation ensures that elements are removed from the front of the queue, maintaining an orderly sequence where the oldest elements are processed first.

What is Dequeue?

Dequeue is the operation used to remove an element from the front of the queue. This operation ensures that the FIFO order is maintained by always removing the element that has been in the queue the longest. The process is straightforward but must handle cases where the queue might be empty.

How Dequeue Works

When performing a dequeue operation, the following steps are executed:

  1. Check for Underflow: Verify if the queue is empty. If the front index has surpassed the rear index, an underflow condition occurs, and there are no elements to remove.
  2. Remove Element: Retrieve and remove the element at the front index.
  3. Increment Front: Move the front index to the next position to point to the new front of the queue.
  4. End: The process concludes with the element successfully removed from the queue.

Algorithm for Dequeue Operation

The dequeue algorithm can be broken down into clear, concise steps:

  1. Start
  2. Check Underflow: If the queue is empty, print "Queue Underflow" and exit.
  3. Retrieve Element: Get the element at the front position.
  4. Increment Front: Move the front index to the next position.
  5. End

Pseudocode for Dequeue Operation

Here is the pseudocode for the dequeue operation:

Algorithm Dequeue(queue, front, rear):
    if front > rear:
        print "Queue Underflow"
        return
    else:
        element = queue[front]
        front = front + 1
        return element

 

Example Implementation in C++

To better understand the dequeue operation, let’s look at a simple C++ implementation using an array:

#include <iostream>
#define MAX 1000

class Queue {
    int front, rear;
    int arr[MAX]; // Maximum size of Queue

public:
    Queue() { front = 0; rear = -1; }
    bool enqueue(int x);
    int dequeue();
    bool isEmpty();
    void printQueue();
};

bool Queue::enqueue(int x) {
    if (rear == MAX - 1) {
        std::cout << "Queue Overflow" << std::endl;
        return false;
    } else {
        arr[++rear] = x;
        std::cout << x << " added to queue" << std::endl;
        return true;
    }
}

int Queue::dequeue() {
    if (isEmpty()) {
        std::cout << "Queue Underflow" << std::endl;
        return -1;
    } else {
        int x = arr[front++];
        std::cout << x << " removed from queue" << std::endl;
        return x;
    }
}

bool Queue::isEmpty() {
    return (front > rear);
}

void Queue::printQueue() {
    for (int i = front; i <= rear; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
}

int main() {
    Queue queue;
    queue.enqueue(10);
    queue.enqueue(20);
    queue.enqueue(30);
    queue.printQueue();
    queue.dequeue();
    queue.printQueue();

    return 0;
}

 

Practical Applications

The dequeue operation is vital in many real-world scenarios:

  1. Job Scheduling: Removing completed jobs from a job queue in operating systems.
  2. Print Queue Management: Dequeueing print tasks once they are printed.
  3. Data Packet Handling: Dequeueing data packets from network routers for processing.
  4. Task Management: Removing completed tasks from task queues in various applications.

Conclusion

The dequeue operation is a core aspect of queue management, ensuring that elements are systematically removed from the front of the queue, maintaining the FIFO order. Understanding and implementing the dequeue operation is crucial for effectively utilizing queues in various applications. Whether managing tasks, scheduling jobs, or handling data packets, the dequeue operation plays a pivotal role in maintaining order and efficiency.

FAQ

Any Questions?
Look Here.

Related Articles

Circular Queues in Data Structures

Enqueue Operation in Queues

Introduction to Queues