How To Use Queues In C# – Data Structures Made Easy
Queues are a limited access data structure, which works similarly to stacks. The difference between the two are their logistic principles.
Filter by Category
Queues are a limited access data structure, which works similarly to stacks. The difference between the two are their logistic principles.
Linked lists are a type of list that allow us to traverse its elements without using their index positions.
Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.
In this tutorial we will be talking about intensity level slicing in image processing. What is Intensity Level Slicing It’s a process that highlights pixels in an arbitrary...
This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...
What is Log Transformation Anyway? Log transformation in image processing is a part of gray level transformations. It works by transforming each pixel individually. It is usually...
Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen...
A negative image is a complete inversion of an image, we would say to be normal. In other words, dark areas will appear light, and light areas will become dark. More in detail, a...
Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...
Erosion is a morphological process, where pixels at an object boundaries are removed. Erosion and dilation are a pair of basic morphological transformations, which are completely...
Queues are another type of limited access data structures like stacks. However, the difference between a stack and a queue is the object position we can access. To elaborate, a queue works with first in first out principle, while a stack works with first in last out principle.
For example, imagine you’re standing in a line at the bank to pay some bills. In order to get to the counter, you need to wait for the people that came before you.
This is basically how it works in data structures as well. Moreover, before you can access an object in the middle, you need to dequeue all other objects that you enqueued before it.
Fortunately, we don’t have to code it ourselves, since generic class Queue already exists in System.Collections.Generic namespace.
I think it’s best we take a look at how it works in code and which methods we can use with it.
Queue<string> line = new Queue<string>();
line.Enqueue("Bob");
line.Enqueue("Drew");
line.Enqueue("Lara");
line.Enqueue("Faith");
Console.WriteLine("Queue length: " + line.Count);
string first = line.Dequeue();
Console.WriteLine("Dequeued " + first);
Console.WriteLine("Queue length: " + line.Count);
string name = line.Peek();
Console.WriteLine("Currently it's " + name + "s turn.");
Console.WriteLine("Queue length: " + line.Count);
string answer = line.Contains("Lara") ? "Yes" : "No";
Console.WriteLine("Is Lara standing in queue? " + answer);
Console.WriteLine("Clearing queue...");
line.Clear();
Console.WriteLine("Queue length: " + line.Count);
In this simple example I declared a queue, which holds string data type objects. However, you’re not limited to that, as you can queue various different data types, even custom ones. In order to add new objects, we need to use Enqueue method.
Throughout the example I used Count property to display how many objects are still inside the queue. Furthermore, a queue wouldn’t do us much good if we couldn’t remove objects from it. For this purpose, we can use Dequeue method, which will remove the first object in the queue.
As you can see from the example, Dequeue method not only removes the object, but also returns its value. However, if you just wanted to return the first object in line, we can use Peek method, which does exactly that.
I’d like to mention that limited access data structures don’t hide the objects we can’t access. You can still use Contains method to check whether a certain object lies in a queue or not.
And finally, if you want to completely clear out the queue, you can just use Clear method.
I hope this short tutorial helped you gain a better understanding about queues in c#.
You can also download the project I put together, if you want to try it out yourself.