1. What is a Queue?

β€œQueue” is a data structure that stores items in First-in/First-Out manner.

OR

A β€œQueue” is like a waiting line at a funfair or a line of people waiting for their turn to ride a roller coaster! It follows a simple rule called "First-In-First-Out" (FIFO), which means the person who enters the line first gets to go out first.

Imagine you have a bunch of friends waiting to play a game, and you want to keep track of their order. You can use a queue!

πŸ“₯ It starts with an empty queue, like an empty line. ➑️ When your friend arrives, they join the queue by standing at the back. πŸ“€ When it's time to play, the person at the front of the queue gets their turn, and they leave the queue. ➑️ The next person in line moves to the front, and they get their turn. πŸ“€ This continues until everyone has had their turn and the queue becomes empty again.

That's how a queue works! It's a simple data structure that helps keep track of the order in which things or people enter and leave.

Example:

Why we need a Queue?

Imagine you're at a busy food truck festival with your friends, and you all want to get some yummy treats. But there's a problem: the food truck can only serve one person at a time. This is where a queue comes in handy!

πŸ•°οΈ Time management: A queue helps manage the order in which people arrive and get served. It's like forming a line or queue at the food truck. The first person in line gets served first, followed by the next person, and so on.

πŸ“₯ Enqueue: When someone arrives at the food truck, they join the back of the queue by "enqueuing" themselves. It's like adding their name to a waiting list. They patiently wait for their turn.

πŸ”’ Queue size: You can keep track of how many people are in the queue by counting the number of friends waiting. It's important to know the size of the queue to ensure fairness and efficiency.

πŸ“€ Dequeue: When the food truck finishes serving a person, they leave the queue, and the next person in line gets their turn. This process is called "dequeuing." It's like stepping out of the line after receiving your order.

🚦 Queue operations: You can perform different operations on a queue. For example, you can check if the queue is empty (no one waiting), check the size of the queue, or even peek at the person in the front of the queue without removing them.

In DSA, queues are used to manage tasks or data in a similar way. They help ensure fairness and maintain the order of processing. For example, when processing tasks in a computer system, handling requests in a network, or printing documents from a printer, a queue ensures that things are done in a proper sequence, preventing chaos or conflicts.

Let’s Know About some Operations in Queue?

Here's an explanation of each queue operation using emojis:

  1. Create Queue (✨): This operation creates an empty queue, ready to hold elements.

    Example: Initially, the queue is represented by ❌ (empty).

  2. Enqueue (➑️πŸ“₯): This operation adds an element to the back of the queue.

    Example: If the queue is represented by emojis like πŸŽπŸŠπŸ‡, enqueueing a πŸ‰ would result in πŸŽπŸŠπŸ‡πŸ‰.

  3. Dequeue (β¬…οΈπŸ“€): This operation removes the element from the front of the queue.

    Example: If the queue is πŸŽπŸŠπŸ‡πŸ‰, dequeuing an element would result in πŸŠπŸ‡πŸ‰.

  4. Peek (πŸ‘€): This operation returns the element at the front of the queue without removing it.

    Example: If the queue is πŸŽπŸŠπŸ‡πŸ‰, peeking would show 🍎.

  5. Is Empty (β“πŸ†˜): This operation checks if the queue is empty and has no elements.

    Example: If the queue is empty, it would be represented by ❌. Otherwise, it would have some emojis in it.

  6. Is Full (πŸ“₯πŸ”’πŸ“€): This operation checks if the queue is full and has reached its maximum capacity.

    Example: If the queue is full, it would be represented by a πŸ”’ symbol. Otherwise, it can still accept new elements.

  7. Delete Queue (πŸ—‘οΈ): This operation deletes or clears the entire queue, removing all elements.

    Example: After deleting the queue, it would be represented by ❌ (empty).

These operations allow you to create, add, remove, and check the status of elements in a queue.