DEV Community

Cover image for In Line with Data: Queue 🎒
Shrutik
Shrutik

Posted on

In Line with Data: Queue 🎒

What is a Queue πŸ€”?
A queue is a useful data structure in programming. Using a Queue Keeps Things in Order.
It is similar to the ticket queue outside a cinema hall, where the first person entering the queue is the first person who gets the ticket.

Queues follow the 'First-In, First-Out' (FIFO) principle, ensuring that data or tasks are processed in the order they are added. This is crucial for maintaining the sequence of operations.

Usage in C.S Field:

  • Buffering and Flow Control: They help regulate the flow of data in systems, preventing overload and ensuring smooth operation.
  • Task Scheduling: Queues are used for task scheduling and prioritization, ensuring that high-priority tasks are handled promptly while lower-priority tasks wait their turn.
  • Resource Allocation: In scenarios with limited resources (e.g., CPU time), queues help allocate resources fairly among competing processes or tasks.
  • Message Passing: Message queues are used in distributed systems to enable communication between different components or nodes asynchronously, improving system responsiveness. & More....

Queue follows theΒ First In First Out (FIFO)Β rule - the item that goes in first is the item that comes out first.
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

Basic Operations:
A queue is an object (an abstract data structure - ADT) that allows the following operations :

  • enqueue() βˆ’ add (store) an item to the queue.
  • dequeue() βˆ’ remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation efficient.
These are βˆ’

  • peek() βˆ’ Gets the element at the front of the queue without removing it.
  • isfull() βˆ’ Checks if the queue is full.
  • isempty() βˆ’ Checks if the queue is empty. In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer.

Enqueue Operation:

  • check if the queue is full
  • for the first element, set the value to FRONT
  • increase theΒ REAR index by 1
  • add the new element in the position pointed to by REAR

Dequeue Operation:

  • check if the queue is empty
  • return the value pointed by FRONT
  • increase theΒ Β index by 1 FRONT
  • for the last element, reset the values ofΒ FRONTΒ andΒ REARΒ to -1

Mechanism

Types of Queues:

  • Linear Queue: Imagine people waiting in a single-file line. The items are added to one end (the rear) and removed from the other end (the front). It follows the "first-in, first-out" (FIFO) principle, just like people waiting in a single-file line.
Pseudocode:
Queue linearQueue
Enqueue(linearQueue, item1)  // Add item1 to the rear
Enqueue(linearQueue, item2)  // Add item2 to the rear
Dequeue(linearQueue)        // Remove item1 from the front
Enter fullscreen mode Exit fullscreen mode
  • Circular Queue: It's like a line of people in a circle. When you reach the end, you go back to the beginning. Circular queues are like linear queues but with a twist. When you reach the end, you wrap around to the beginning, creating a circular data structure. This allows efficient use of space and is often used in situations where the queue size is fixed.
Pseudocode:
Queue circularQueue
Enqueue(circularQueue, item1)  // Add item1 to the rear
Enqueue(circularQueue, item2)  // Add item2 to the rear
Dequeue(circularQueue)        // Remove item1 from the front

Enter fullscreen mode Exit fullscreen mode
  • Priority Queue: Think of it as a line where people with higher priority get to go ahead of others. For example, in a hospital, emergencies have high priority. Priority queues assign a priority level to each item. Items with higher priority are dequeued first. It's like a queue where important tasks get processed before less important ones.
Pseudocode:
Priority_Queue priorityQueue
Enqueue(priorityQueue, item1, priority1)  // Add item1 with priority1
Enqueue(priorityQueue, item2, priority2)  // Add item2 with priority2
Dequeue(priorityQueue)                   // Remove item1 if priority1 > priority2
Enter fullscreen mode Exit fullscreen mode
  • Double-ended Queue (Deque): It's a versatile queue that allows items to be added or removed from both ends. It can be used as a queue or a stack. It's like having doors at both ends of the line.
Pseudocode:
Deque deque
Enqueue_Front(deque, item1)  // Add item1 to the front
Enqueue_Rear(deque, item2)   // Add item2 to the rear
Dequeue_Front(deque)         // Remove item1 from the front
Dequeue_Rear(deque)          // Remove item2 from the rear
Enter fullscreen mode Exit fullscreen mode

Be sure to explore the actual code for these implementations to gain a deeper understanding of how queues work in practice.
Today, we delved into queues, covering their operations, mechanisms, and the numerous benefits they offer in computer science and real-world applications.

I hope this post was informative and helpful.
If you have any questions, please feel free to leave a comment below.

Happy Coding πŸ‘πŸ»!
Thank You

Top comments (0)