Queue Implementation in C#

Queue

It represent a FIFO (First In First Out) collection of object. Queue is used when you need first-in, first-out access of object that means accessing the first inserting item. Queue basically consist of two operation Enqueue and Dequeue. When you insert an element in Queue , it is called Enqueue, and when you extract the item from Queue , it is called Dequeue. Enqueue operation is perform at the end of queue and Dequeue operation take place at front of the queue. To use Queue data type in c# first you should need to use System.Collections namespace.

Properties of Queue

  1. Count return the number of elements present in the Queue. 

Method of Queue:
  1.  public virtual void Enqueue(object obj); It simply insert an object at the end of queue.

  2.  public virtual object Dequeue(object obj); It simply remove and return the object from front of the queue.

  3. public virtual void Clear(); It clears the queue means this method remove all the element from queue.

  4. public virtual object Peek(); This method return the object from front of the queue (without Removing). 

  5. public virtual object[] ToArray(); This method copy the queue into a object array.

  6. public virtual bool Contains(object obj); This method is used to check whether an element is exist in the queue. It return True when item exist in the queue otherwise it return False. 

  7. public virtual void TrimToSize(); This method sets the capacity to the actual number of elements present in the Queue. Basically the capacity is not increased one by one. Capacity is just doubled each time whenever the size reaches the threshold. So TrimToSize() method sets the capacity to exact the size of queue. 

Here is the example:

  1. using System;  
  2. using System.Collections;  
  3. namespace Teq_Cdac   
  4. {  
  5.     class Program   
  6.     {  
  7.         static void Main(string[] args)   
  8.         {  
  9.             Queue q = new Queue();  
  10.             q.Enqueue("Pankaj");  
  11.             q.Enqueue(1);  
  12.             q.Enqueue(10.5);  
  13.             q.Enqueue(true);  
  14.             q.Enqueue('A');  
  15.             Console.WriteLine("Count : {0}", q.Count);  
  16.             Console.WriteLine();  
  17.             Console.WriteLine("Element in Queue : ");  
  18.             foreach(object obj in q)  
  19.             Console.WriteLine(obj);  
  20.             Console.WriteLine();  
  21.             Console.WriteLine("End element of Queue : {0}", q.Peek());  
  22.             Console.WriteLine();  
  23.             object TopElement = q.Dequeue();  
  24.             Console.WriteLine("Removing End element of Queue = {0}\nNow End element of Queue = {1}\n", TopElement, q.Peek());  
  25.             if (q.Contains("Pankaj")) Console.WriteLine("Pankaj Found");  
  26.             else Console.WriteLine("Pankaj Not found");  
  27.             Object[] ob = q.ToArray();  
  28.             Console.WriteLine();  
  29.             foreach(object obj in ob)  
  30.             Console.WriteLine(obj);  
  31.             q.TrimToSize();  
  32.             q.Clear();  
  33.             Console.WriteLine();  
  34.             Console.WriteLine("Count : {0}", q.Count);  
  35.             Console.ReadKey();  
  36.         }  
  37.     }  
  38. }  
Output

 
Ebook Download
View all
Learn
View all