πŸ“š Introduction to Queue in DSA: πŸŽŸοΈπŸšΆβ€β™‚οΈπŸ§΅

Β·

7 min read

Table of contents

No heading

No headings in the article.

Hey there! πŸ‘‹ Today, we're going to learn about something called a Queue. 🎟️ Imagine you're standing in line to buy tickets for your favorite movie or ride. A Queue is just like that line! It's a special way of organizing data, where the first person to join the line is the first person to be served. It follows a rule called "First In First Out" (FIFO). Let's dive in and understand more about it! πŸšΆβ€β™‚οΈπŸ§΅

🍿 What is a Queue? 🎟️

A Queue is a special way of arranging data in a specific order. It's like a line, where the first person to join is the first to be served. In a Queue, new elements are added to one end, called the rear πŸ‘, and existing elements are removed from the other end, called the front πŸ₯‡. This way, the order in which elements enter the Queue is preserved, just like in a real-life line.

🎟️ How does a Queue work? 🎒

Imagine a Queue as a line of people waiting for a ride. When a new person joins the line, they stand at the end, and when the ride starts, the person at the front gets on first. So, the first person in the Queue gets served first, and the last person has to wait their turn patiently. That's why it's called "First In First Out" (FIFO)!

🧡 Representing a Queue: πŸ“š

We can represent a Queue using an array, just like a line of people. Let's understand the terms we'll use:

  • Queue: It's the name we give to our array that stores the elements in the Queue.

  • Front: It's the position in the array where the first element of the Queue is stored.

  • Rear: It's the position in the array where the last element of the Queue is stored.

✨ Why do we use Queues? πŸ€”

Queues are useful in many situations. They help us manage things that need to be done in a particular order. For example:

  • Print jobs waiting in a printer's queue.

  • Handling requests in a computer network.

  • Managing tasks in an operating system.

  • Processing messages in a messaging app.

πŸ‘¨β€πŸ’» Code Time! πŸš€

Let's take a look at a simple implementation of a Queue using code snippets in various programming languages. Here are a few examples:

  • Queue in Python 🐍:
# Creating an empty Queue
queue = []

# Adding elements to the Queue
queue.append(1)
queue.append(2)
queue.append(3)

# Removing elements from the Queue
front_element = queue.pop(0)
  • Queue in Java β˜•:
import java.util.Queue;
import java.util.LinkedList;

// Creating a Queue
Queue<Integer> queue = new LinkedList<>();

// Adding elements to the Queue
queue.add(1);
queue.add(2);
queue.add(3);

// Removing elements from the Queue
int frontElement = queue.remove();
  • Queue in C++ 🧩:
#include <iostream>
#include <queue>

using namespace std;

int main() {
    // Creating a Queue
    queue<int> queue;

    // Adding elements to the Queue
    queue.push(1);
    queue.push(2);
    queue.push(3);

    // Removing elements from the Queue
    int frontElement = queue.front();
    queue.pop();

    return 0;
}

Another Example :

πŸ“š Introduction to Queue - Explained for 5-Year-Olds! 🚢🎟️

Imagine you're standing in line with your friends to buy ice cream 🍦. You all want to get your ice cream as soon as possible, right? But how do you decide who gets their ice cream first? That's where a queue comes in!

A queue is like a line where the person who arrives first gets served first. Just like in the ice cream shop, the first person in the queue gets their ice cream before the others. This is called "First In, First Out" or FIFO.

πŸšΆβ€β™‚οΈπŸ¦ <- Front of the queue (First In)

πŸšΆβ€β™€οΈπŸ¦

πŸšΆβ€β™‚οΈπŸ¦

πŸšΆβ€β™€οΈπŸ¦ <- Rear of the queue (Last In)

In a queue, we have two important positions: the front and the rear. The person at the front is the one who will be served next, while the person at the rear is the last one who joined the queue.

Just like in the ice cream line, there are a few things we can do with a queue. We can add a new person to the rear of the queue, and we can remove the person at the front of the queue when they get served. This way, everyone gets their turn!

πŸŽŸοΈπŸšΆβ€β™‚οΈπŸ¦ <- New person joins the queue (Enqueue)

πŸšΆβ€β™‚οΈπŸ¦

πŸšΆβ€β™€οΈπŸ¦

πŸšΆβ€β™‚οΈπŸ¦ <- Person at the front gets served and leaves the queue (Dequeue)

One cool thing about queues is that we can represent them using an array. Imagine having a row of chairs where people sit while waiting for their turn. The first chair represents the front of the queue, and the last chair represents the rear.

We can also use some special functions with queues. For example, we can check if the queue is empty (no one is waiting) or if it's full (all chairs are occupied). These functions help us manage the queue better.

So, to summarize, a queue is like a line where people wait for their turn, just like in the ice cream shop. The first person who arrives gets served first, and everyone else follows in the order they joined. We can represent a queue using an array, and we have special functions to manage it.

πŸ“š Let's Dive into Different Types of Queues in C++ πŸ“š

Hey there! Let's dive into the details of different types of queues in C++. We'll explore each type and provide code examples to help you understand better.

1️⃣ Simple Queue: A simple queue follows the FIFO (First In, First Out) rule. It means the first element added to the queue will be the first one to be removed. Here's an example of a simple queue implemented in C++:

#include <iostream>
#include <queue>

int main() {
    std::queue<int> myQueue;

    myQueue.push(10);  // Adding elements to the queue
    myQueue.push(20);
    myQueue.push(30);

    while (!myQueue.empty()) {
        std::cout << myQueue.front() << " ";  // Accessing the front element
        myQueue.pop();  // Removing the front element
    }

    return 0;
}

2️⃣ Circular Queue: A circular queue is implemented using an array or a linked list where the last element is connected to the first element, forming a circle. It allows efficient memory utilization. Here's an example of a circular queue implemented in C++:

#include <iostream>

const int MAX_SIZE = 5;

class CircularQueue {
private:
    int arr[MAX_SIZE];
    int front, rear;

public:
    CircularQueue() {
        front = rear = -1;
    }

    void enqueue(int element) {
        if ((rear + 1) % MAX_SIZE == front) {
            std::cout << "Queue is full!" << std::endl;
        } else {
            if (front == -1)
                front = 0;
            rear = (rear + 1) % MAX_SIZE;
            arr[rear] = element;
            std::cout << "Element enqueued: " << element << std::endl;
        }
    }

    void dequeue() {
        if (front == -1) {
            std::cout << "Queue is empty!" << std::endl;
        } else {
            std::cout << "Element dequeued: " << arr[front] << std::endl;
            if (front == rear)
                front = rear = -1;
            else
                front = (front + 1) % MAX_SIZE;
        }
    }
};

int main() {
    CircularQueue myQueue;

    myQueue.enqueue(10);
    myQueue.enqueue(20);
    myQueue.enqueue(30);
    myQueue.dequeue();

    return 0;
}

3️⃣ Priority Queue: A priority queue assigns a priority to each element. The element with the highest priority is dequeued first. If multiple elements have the same priority, they are served based on their order in the queue. Here's an example of a priority queue implemented in C++:

#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> myQueue;

    myQueue.push(30);  // Adding elements to the priority queue
    myQueue.push(10);
    myQueue.push(20);

    while (!myQueue.empty()) {
        std::cout << myQueue.top() << " ";  // Accessing the highest priority element
        myQueue.pop();  // Removing the highest priority element
    }

    return 0;
}

4️⃣ Double Ended Queue (Deque): A deque allows insertion and removal of elements from both ends. It doesn't strictly follow the FIFO rule. Here's an example of a deque implemented in C++:



#include <iostream>
#include <deque>

int main() {
    std::deque<int> myDeque;

    myDeque.push_front(10);  // Adding elements to the front of the deque
    myDeque.push_back(20);   // Adding elements to the back of the deque
    myDeque.push_back(30);

    while (!myDeque.empty()) {
        std::cout << myDeque.front() << " ";  // Accessing and printing the front element
        myDeque.pop_front();  // Removing the front element
    }

    return 0;
}

I hope these explanations and code examples help you understand the different types of queues in C++. Have fun experimenting and learning more about queues and their applications in programming! πŸš€πŸ’»

Now, if you ever find yourself in a queue, you'll know how it works! Enjoy your ice cream! πŸ¦πŸ˜„