Circular Queues in Data Structures
Dequeue Operation in Queues
Enqueue Operation in Queues
Queues are a fundamental data structure in computer science, widely used for handling tasks in various applications. They operate on a First In, First Out (FIFO) principle, meaning the first element added to the queue will be the first one to be removed. This principle is akin to a real-world queue, such as a line of people waiting for a service, where the person who arrives first is served first.
.png)
A queue is an abstract data type that manages a collection of elements, providing two main operations:
These operations ensure that the oldest elements are removed first, maintaining the FIFO order.
Queues come in various types, each suited to different scenarios:
Queues are versatile and can be found in various applications, including:
Let's look at the step-by-step algorithm for adding an element to the queue (Enqueue):
Similarly, here’s the algorithm for removing an element from the queue (Dequeue):
Enqueue Operation:
Algorithm Enqueue(queue, element, rear, size):
    if rear == size - 1:
        print "Queue Overflow"
        return
    else:
        rear = rear + 1
        queue[rear] = element
Dequeue Operation:
Algorithm Dequeue(queue, front, rear):
    if front > rear:
        print "Queue Underflow"
        return None
    else:
        element = queue[front]
        front = front + 1
        return element
Here’s a simple implementation of a queue in C++ 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();
    bool isFull();
    void printQueue();
};
bool Queue::isEmpty() {
    return (front > rear);
}
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;
    }
}
int Queue::dequeue() {
    if (isEmpty()) {
        std::cout << "Queue Underflow" << std::endl;
        return -1;
    } else {
        return arr[front++];
    }
}
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();
    std::cout << "Dequeued from queue: " << queue.dequeue() << std::endl;
    queue.printQueue();
    return 0;
}
Queues are a powerful data structure that efficiently handles tasks in various applications by maintaining a FIFO order. Understanding and implementing queue operations like enqueue and dequeue are fundamental to utilizing queues effectively. With their diverse types and applications, queues play a critical role in data structure management and algorithm design.
