Data Structures — Queue
Queue is a data structure that follows the First-In-First-Out (FIFO) principle. This means that the first item added to the queue will be the first item to leave the queue. In this article, I present how to use this data structure in .NET.
To illustrate how a queue works, think about a line of people waiting for a service, where the first person to join the line is the first person to be served. In similar way, when working with queues, the elements are added at the rear (enqueue) and removed from the front (dequeue) of the queue.
For demonstration purposes, I created a console application using .NET. The complete code can be found on my GitHub.
The Queue data structure provides a series of methods that you can use to work with it. These are the main methods:
- Enqueue: this adds elements to the rear/back of the stack.
- Dequeue: this removes and returns the element at the front of the queue.
- Peek: this returns the element at the front of the queue (without removing it).
- Count: this returns the number of elements in the queue.
- Contains: this returns whether an element is in the queue.
In the image below you can see what happens when the Enqueue
and Dequeue
operations are executed:
- The element 1 was at the front of the queue, and when the
Dequeue
operation was executed, the element 1 was removed from the queue. - The last element on this queue is element 6, and when the
Enqueue
operation is executed, the element 7 will be added to the end of the queue.
Creating a Queue
To create a Stack in C#, you can declare it by using the generic class Queue<T>
, where T
is the type of elements that will be stored in the queue. For example, below I’m creating a Queue of string elements:
Queue<string> queueDemo = new Queue<string>();
// or
var queueDemo = new Queue<string>();
Adding elements to the Queue
In the code below I’m adding three elements to the queueDemo
:
queueDemo.Enqueue("Julie");
queueDemo.Enqueue("Ana");
queueDemo.Enqueue("Bob");
For each Enqueue
operation, the element will be added at the back of the queue:
When printing the values of this stack, this is the output:
Julie
Ana
Bob
Removing the first element from the Queue
The Dequeue
operation can be used to remove the first element from the queue. For example:
queueDemo.Dequeue();
When executing the Dequeue
operation, the first element that was added to the queue (“Julie”) will be removed:
When printing the elements of the queue after executing the Dequeue method, this is the output:
Ana
Bob
Getting the first element from the Queue
To retrieve the first element from the Queue, you can use the Peek
method:
var firstElement = queueDemo.Peek();
Now when printing the firstElement
, this is the output:
Ana
Contains operation
To check if an element exists in the Queue, you can use the Contains
method, and pass as a parameter the value that you want to search:
var containsBob = queueDemo.Contains("Bob");
var containsSofia = queueDemo.Contains("Sofia");
When printing containsBob
and containsSofia
, this is the output:
Queue contains Bob: True
Queue contains Sofia: False
Retrieving the number of elements in a Queue
To get the amount of elements that exist in a Queue, or to check if the Queue is empty or not, you can use the Count
method:
var numberOfElements = stackDemo.Count;
When printing the amountOfElements
, the output is:
numberOfElements: 2
Useful cases for using a Queue
Some common scenarios where the Queue data structure is useful are:
- Task or Job Scheduling: Queues can be used to schedule and process tasks or jobs in a first-in-first-out (FIFO) manner, ensuring fair and ordered execution, for example, printing queues, where documents are added to the queue and printed in the order they were received.
- Breadth-First Search (BFS): queues are commonly used in BFS algorithms for traversing graphs or trees level by level.
- Multithreading and Task Management: queues can be used in multithreaded applications to communicate between different threads or manage tasks in a thread pool.
- Call center systems: where incoming calls are added to a queue and are answered by agents in the order they were received.
Conclusion
The Queue is a linear data structure that follows the FIFO (First-In-First-Out) principle, and can be useful in scenarios where the first element added to the queue must be the first one to be processed.
This is the link for the project in GitHub: https://github.com/henriquesd/queuedemo
If you like this demo, I kindly ask you to give a ⭐️ in the repository.
Thanks for reading!
References