Working on Queue Using C#

Introduction

A Queue represents a first-in, first-out collection of objects. An example of a queue is a line of people waiting. Queue<T> is the class in the System.Collection.Generic Namespace, where T specifies the type of elements in the queue. In this article I explain the constructor, properties and methods of the Queue class.

Constructors

  1. Queue<T> Constructor - Initializes a new instance of the Queue<T> class that is empty. The next lines shows how we create the empty queue.

    Queue
    <string> queue = new Queue<string>();
     
  2. Queue<T> Constructor (IEnumerable<T>) - Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection.

    string
    [] courses = { "MCA","MBA", "BCA","BBA", "BTech","MTech" };
    Queue<string> queue = new Queue<string>(courses);
     
  3. Queue<T> Constructor (Int32) - Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.

    Queue
    <string> queue = new Queue<string>(4);

Properties

Count - Gets the number of elements contained in the Queue<T>. For example:

namespace Queue

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] courses = { "MCA", "MBA", "BCA", "BBA", "BTech", "MTech" };

            Queue<string> queue1 = new Queue<string>();

            Queue<string> queue2 = new Queue<string>(courses);

            Queue<string> queue3 = new Queue<string>(4);

            Console.WriteLine("Number of elements in queue1:" + queue1.Count());

            Console.WriteLine("Number of elements in queue2:" + queue2.Count());

            Console.WriteLine("Number of elements in queue3:" + queue3.Count());

        }

    }

}

OUTPUT

pic1.jpg

Methods

  1. Enqueue - Adds an object to the end of the Queue<T>. For example:
     

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("The elements in the queue are:");

               foreach (string s in queue1)

                {

                   Console.WriteLine(s);

                }

            }

        }

    }

    OUTPUT

    pic2.jpg
     

  2. Dequeue - Removes and returns the object at the beginning of the Queue<T>. For example:

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("The elements in the queue are:");

               foreach (string s in queue1)

                {

                   Console.WriteLine(s);

                }

                queue1.Dequeue();//Removes the first element that enter in the queue here the first element is MCA

                queue1.Dequeue();//Removes MBA

               Console.WriteLine("After removal the elements in the queue are:");

               foreach (string s in queue1)

                {

                   Console.WriteLine(s);

                }

            }

        }

    }

    OUTPUT

    pic3.jpg

     

  3. Contain - Determines whether an element is in the Queue<T>. For example:
     

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("The elements in the queue are:");

               foreach (string s in queue1)

                {

                   Console.WriteLine(s);

                }

               Console.WriteLine("The element MCA is contain in the queue:" + queue1.Contains("MCA"));

               Console.WriteLine("The element BCA is contain in the queue:" + queue1.Contains("BCA"));

               Console.WriteLine("The element MTech is contain in the queue:" + queue1.Contains("MTech"));

            }

        }

    }

    OUTPUT

    pic4.jpg
     

  4. Clear - Removes all objects from theQueue<T>. For example:

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("The elements in the queue are:"+queue1.Count());

                queue1.Clear();

               Console.WriteLine("The elements in the queue are after the clear method:" + queue1.Count());           

            }

        }

    }

    OUTPUT

    pic5.jpg

  5. Peek - Returns the object at the beginning of the Queue<T> without removing it. For example:
     

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("Peek the first item from the queue is:" + queue1.Peek());

                queue1.Dequeue();

               Console.WriteLine("Peek the next item from the queue is:" + queue1.Peek());        

            }

        }

    }

    OUTPUT

    pic6.jpg
     

  6. ToArray - Copies the Queue<T> elements to a new array. For example:

    namespace Queue

    {

       class Program

        {

           static void Main(string[] args)

            {

               Queue<string> queue1 = new Queue<string>();

                queue1.Enqueue("MCA");

                queue1.Enqueue("MBA");

                queue1.Enqueue("BCA");

                queue1.Enqueue("BBA");

               Console.WriteLine("The queue elements are:");

               foreach (string s in queue1)

                {

                   Console.WriteLine(s);

                }

               Queue<string> queue2 = new Queue<string>(queue1.ToArray());

               Console.WriteLine("\nContents of the copy");

               foreach (string n in queue2)

                {

                   Console.WriteLine(n);

                }

            }

        }

    }

  7. OUTPUT

  8. pic7.jpg

    Summary

    In this article I explained the Queue<> class and the various methods and properties of it. 

Up Next
    Ebook Download
    View all
    Learn
    View all