Queue – Data Structures and Algorithms using C#

A queue is a First In First Out (FIFO) data structure. A queue is the most common which you encounter in daily-life as well.

For example : 

Standing in line for money withdraw at an ATM where the first person in the line is the person to be served first; the last one to be served last. In this kind of arrangement, all additions are made at one end and the deletions made at the other. The end where all additions are made is called the rear end. The other end from where the deletions are made is called the front

Queue can be visualize as shown in the below picture.

Queue

 

Applications of Queue

There are many applications of the queue but some of them are given below :

  • Printing documents
  • Client server network mechanism
  • Breadth First Search
  • Process Scheduling and many more

 

Different Types of Queue

There are two types of queue

  • Linear Queue
  • Circular Queue
Queue Operations

Many operations can be implemented in this data structure but some of the most useful operations are given below

  • Enqueue –  To add an element at the end of the queue.
  • Dequeue –  To remove an element from the beginning and return it.
What is Linear Queue?

Linear queue is a special type of queue where insertion is done at one end and deletion is done at another end called rear and front respectively. This can be understood by the below figure.

linear-queue

Queue Implementation

Before starting its implementation, we should understand some terminologies used in queue.

 rear denotes the top of the list and is used to insert the new element into list.
 front denotes the bottom of the list and is used to remove the elements from the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Queue
    {
        private T[] arr = null;
 
        public int Size { get; set; }
        public int Rear { get; set; }
        public int Front { get; set; }
 
        public Queue(int Size)
        {
            this.Size = Size;
            this.arr = new T[this.Size];
            this.Rear = -1;
            this.Front = -1;
        }
 
        public bool IsFull()
        {
            return (Rear == (Size - 1));
        }
 
        public bool IsEmpty()
        {
            return (Front == -1 || Front > Rear);
        }
 
        public void EnQueue(T newItem)
        {
            if (IsFull())
            {
                Console.WriteLine("Queue is Full");
                return;
            }
 
            Rear = Rear + 1;
            arr[Rear] = newItem;
            if(Front == -1)
            {
                Front = 0;
            }
        }
 
        public T DeQueue(T newItem)
        {
            if (IsEmpty())
            {
                Console.WriteLine("Queue is Empty");
                return default(T) ;
            }
 
            T ObjectOut = (T)arr[Front];
            Front = Front + 1;
            return ObjectOut;
        }
    }

Let’s use the above Queue data structure to enqueue and dequeue operation from it.

1
2
3
4
5
6
7
8
9
10
11
Queue q = new Queue(5);
            q.EnQueue(2);
            q.EnQueue(5);
            q.EnQueue(29);
            q.EnQueue(8);
            q.EnQueue(99);
 
Console.WriteLine("DeQueue : {0}", q.DeQueue());
Console.WriteLine("DeQueue : {0}", q.DeQueue());
 
Console.ReadKey();

So, it was all about Queues Data Structure, if you have any query then please comment below and let us know. If you liked this article, then please follow us on Facebook to get notification for new posts.

Happy Learning 🙂

Rahul is a Data Geek, technology enthusiast, a passionate writer, thinker with passion for computer programming.  He loves to explore technology and finds ultimate joy when writing about trending technology, geek stuff and web development.