Enqueue Operation in Queues

by Jasleen Chhabra | Updated on 24 August 2024

The enqueue operation is a fundamental process in the management of queues, essential for adding elements to the data structure. Operating on the First In, First Out (FIFO) principle, the enqueue operation ensures that new elements are always added to the rear (end) of the queue. This guarantees that the oldest elements are processed first, maintaining an orderly sequence.

What is Enqueue?

Enqueue is the operation used to insert an element into the queue. It adds the new element at the rear of the queue, ensuring that the FIFO order is maintained. The process is straightforward but must handle cases where the queue might be full, especially in fixed-size implementations.

How Enqueue Works

When performing an enqueue operation, the following steps are executed:

  1. Check for Overflow: Verify if the queue is full. In a fixed-size queue, if the rear index has reached the maximum size, an overflow condition occurs, and the element cannot be added.
  2. Increment Rear: Move the rear index to the next position to point to the location where the new element will be inserted.
  3. Insert Element: Place the new element at the position indicated by the rear index.
  4. End: The process concludes with the new element successfully added to the queue.

Algorithm for Enqueue Operation

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

  1. Start
  2. Check Overflow: If the queue is full, print "Queue Overflow" and exit.
  3. Increment Rear: Move the rear index to the next position.
  4. Insert Element: Place the new element at the rear position.
  5. End

Pseudocode for Enqueue Operation

Here is the pseudocode for the enqueue operation:

Algorithm Enqueue(queue, element, rear, size):
    if rear == size - 1:
        print "Queue Overflow"
        return
    else:
        rear = rear + 1
        queue[rear] = element

 

Example Implementation in C++

To better understand the enqueue 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);
    bool isFull();
    void printQueue();
};

bool Queue::isFull() {
    return (rear == MAX - 1);
}

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

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();

    return 0;
}

 

Practical Applications

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

  1. Job Scheduling: Adding new jobs to a job queue in operating systems.
  2. Print Queue Management: Enqueueing print tasks in a printer’s queue.
  3. Data Packet Handling: Enqueueing data packets in network routers.
  4. Task Management: Adding tasks to task queues in various applications.

Conclusion

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

FAQ

Any Questions?
Look Here.

Related Articles

Circular Queues in Data Structures

Dequeue Operation in Queues

Introduction to Queues