Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
1.7k views
in Blog Post by 66 69 85
edited by

In this post, we will discuss What's Queue in Data Structure and How to use it using C#. But before moving towards Queue. I would recommend having a look at What is Data Structure?.

What is Queue in data structure?

A queue is the data structure that is similar to a Queue in the real world. In which whatever comes first will go out first, and it follows the FIFO (First-In-First-Out) policy.

The representation of the queue is shown in the below image -

queue representation

Key points of Queue

  1. The Queue is a FIFO( First In First Out ) structure.
  2. Once a new element is inserted into the Queue, all the elements inserted before the new element in the Queue must be removed, to remove the new element.
  3. peek( ) function is often used to return the value of the first element without dequeuing it.

Type of Queues:

Four types of Queues are listed as follows:

  • Simple Queue.
  • Circular Queue.
  • Priority Queue.
  • Double Ended Queue.

Let’s take a brief on each type of queue

1. Simple Queue

Insertion takes place from one end while the deletion occurs from another end.

  • The end at which the insertion takes place is known as the rear end.
  • And the end at which the deletion takes place is known as the front end.

2. Circular Queue

All the nodes are represented as circular. It is similar to the Simple Queue except that the last element of the queue is connected to the first element.

3. Priority Queue

From its name, the elements are arranged based on priority. It’s a special type of Queue in which every element has a priority associated with it. And if some elements have the same priority, in this case, we will arrange with the FIFO principle.

4. Double Ended Queue

Insertion and deletion can be done from both ends of the Queue either from the front or rear. each element can be added to or removed from either the front or rear.

How does Queue work in Data Structure?

Like stack, the Queue is also an ordered list of similar data types.

  • The operation to add an element into the queue is called Enqueue.
  • The operation of removal of an element from the queue is called Dequeue.

We will dig deep into these two operations.

1. Enqueue operation

  • The Enqueue operation is used to insert the element at the rear of the Queue.
  • Queues maintain two data pointers, front, and rear. Therefore, its operations are comparatively difficult to implement than stacks.
  • The image below explains who the Enqueue operation works -

Enqueue operation

2. Dequeue operation

  • It performs the deletion from the front of the Queue.
  • To access data from the queue there are two tasks.
  1. Access the data where the front is pointing.
  2. Remove the data after accessing.

The image below explains who the Dequeue operation works -

Dequeue operation

This animation explains how dose Queue works:

Queue animation


Application of queue in Data Structure 

Here are some common applications in Queue is used:

  1. Serving requests on a single shared resource, like a printer.
  2. Call Center phone systems.
  3. Routers and switches in networking.

Example: Implementing Code of the Queue in C#

In this example, you will learn how to create a Queue, how to Enqueue an element and Dequeue it and search the last element inserted in the Queue.

Queue<int> MyQueue = new Queue<int>(); //initialize a new Queue
 
MyQueue.Enqueue(1);// insert element in the Queue
MyQueue.Enqueue(2);
MyQueue.Enqueue(3);
MyQueue.Enqueue(4);
 
 
Console.WriteLine("this is all the elmenet in the Queue...");
foreach(int item in MyQueue) // print all the elements
    Console.WriteLine(item);
Console.WriteLine("\ntotal number of queue is: " + MyQueue.Count); //return total number of elements
Console.WriteLine("\nthe first element in the queue : " + MyQueue.Peek()); //return the first element
Console.WriteLine("\nthis element is removed! :" + MyQueue.Dequeue()); //remove the first element
Console.WriteLine("\ntotal number of queue is (updated): " + MyQueue.Count); //return total number of elements
Console.WriteLine("\nthis is all the elmenet in the Queue (Updated)");

foreach (int item in MyQueue) // print all the elements 
    Console.WriteLine(item);

Output

this is all the elmenet in the Queue...
1
2
3
4

total number of queue is: 4

the first element in the queue : 1

this element is removed! : 1

total number of queue is (updated): 3

this is all the elmenet in the Queue (Updated) 
2
3
4

You can find the source code for this example at GitHub


References:

See Also 


1.1k questions

673 answers

454 comments

193k users

تعلم بالعربي PowerApps

تعلم بالعربي Power Automate

If you don’t ask, the answer is always NO!
...