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
Filter by Author
Queues are a limited access data structure, which works similarly to stacks. The difference between the two are their logistic principles.
Posted by Andraz Krzisnik
Stacks are limited access data structures, which means we can't access every element. It only allows us to access the last object we added.
Posted by Andraz Krzisnik
Circular linked lists are an upgrade to generic double linked lists. They can loop around when navigating to adjacent elements.
Posted by Andraz Krzisnik
Linked lists are a type of list that allow us to traverse its elements without using their index positions.
Posted by Andraz Krzisnik
Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.
Posted by Andraz Krzisnik
Generic lists in C# are a data structures that allow us to add and remove objects to store inside without declaring its size.
Posted by Andraz Krzisnik
Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.
Posted by Andraz Krzisnik
Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.
Posted by Andraz Krzisnik
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Posted by Andraz Krzisnik
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Posted by Andraz Krzisnik
Queues are a limited access data structure, which works similarly to stacks. The difference between the two are their logistic principles.
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.
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...
Skeletonization is a morphological process in image processing, which extracts the center lines of all shapes, which look like their skeleton