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.


Andraz Krzisnik
How To Use Queues In C# – Data...

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.

Queues in C#

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.

Conclusion

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.

Related Articles

C# Tutorial

Intensity Level Slicing With C# – Explore Image Processing

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...

Posted on by Andraz Krzisnik
C# Tutorial

Histogram Equalization And Magic Behind It

This tutorial is meant to demonstrate how histogram equalization works. This is a continuation on contrast stretch articles I’ve made in the past. An article on histogram...

Posted on by Andraz Krzisnik