What Is Queue in Data Structure With Example?

//

Scott Campbell

In data structure, a queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It is an abstract data type that represents a collection of elements in which an element is added to the end of the queue and removed from the front of the queue.

How Does a Queue Work?

A queue works on two fundamental operations:

  • Enqueue: This operation adds an element to the end of the queue.
  • Dequeue: This operation removes an element from the front of the queue.

The enqueue operation allows you to add elements to the rear or end of the queue, whereas the dequeue operation allows you to remove elements from the front or head of the queue. This behavior ensures that elements are processed in a sequential order, similar to how people stand in a line and are served one by one.

Example:

To better understand queues, let’s consider an example where we have a queue for processing customer support tickets. Each ticket represents a task that needs to be addressed by customer support representatives.

We can represent this queue using an array or linked list. Let’s consider an array-based implementation for simplicity.


// Initializing an empty queue
let ticketQueue = [];

// Enqueuing (Adding) tickets
ticketQueue.push("Ticket 1");
ticketQueue.push("Ticket 2");
ticketQueue.push("Ticket 3");

// Dequeuing (Processing) tickets
let processedTicket = ticketQueue.shift();
console.log("Processed Ticket:", processedTicket);

processedTicket = ticketQueue.log("Processed Ticket:", processedTicket);

Output:


Processed Ticket: Ticket 1
Processed Ticket: Ticket 2
Processed Ticket: Ticket 3

In the example above, we start with an empty queue and enqueue three tickets. The dequeue operation is then performed three times to process each ticket in the order they were enqueued.

Applications of Queue Data Structure

Queues are extensively used in various real-life scenarios and computer science applications. Some common applications of queue data structure include:

  • Job Scheduling: In operating systems, queues are used for scheduling tasks or processes based on priority or arrival time.
  • Breadth-First Search (BFS): Queues are used to traverse graphs or trees in a breadth-first manner, exploring all neighboring nodes at the current level before moving to the next level.
  • Printer Spooler: Queues are used to manage print jobs in order of their submission, ensuring that each job is printed in sequence.
  • Waiting Lists: Queues are used to manage waiting lists for various services like restaurants, movie theaters, and amusement parks.

These examples highlight how queues play a crucial role in organizing and processing data efficiently across multiple domains.

Conclusion

A queue is a fundamental data structure that follows the First-In-First-Out principle. It allows elements to be added at one end and removed from another end, ensuring sequential processing.

Understanding queues is essential for efficient problem-solving and designing algorithms that require ordering and organizing elements based on their arrival or priority. Remember to utilize enqueue and dequeue operations to manipulate the elements in a queue as needed.

Now that you have a solid understanding of queues, you can confidently incorporate them into your programming projects and algorithms.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy