Friday, December 18, 2015

C# : Implementation of Queue using C#

How to implement Queue using C#


Queue is one of the most used data structures in Computer programming and understanding how queues work is essential for learning Data Structures thoroughly. Let us see the definition first :

 Definition : According to Wikipedia, 


" In Computer Science, a queue is a particular kind of abstract data type or collection in which the     entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the Queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. " 


The following image illustrates the concept of a Queue:

Image Source : http://jcsites.juniata.edu/

Image showing a typical Queue (real world example)

Typical use of Queues is in Operating Systems, some of the applications of Queue are listed below:


  • Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.


  • Computer systems must often provide a “holding area” for messages between two processes, two programs, or even two systems. This holding area is usually called a “buffer” and is often implemented as a queue.



  • Now let us go to the step by step implementation of the Queue Data Structure.We are using Visual Studio 2015 for the implementation.

    Step 1:  Open Visual Studio Professional 2015




    Step 2:  Click on New Project. Select Console Application and give desired Name to the application.




    Step 3:  Click on OK and you will get the following Screen



    Step 4The overall implementation or the code should like the following.



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace QueueConsoleExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Main Method
                // In this example we will see basic implementation of Data Structure: Queue 
                // using the language C#.
    
                //Declaring a Queue of integers 
                //(You can make Queues of other Data Types as you want)
                Queue<int> testQueue = new Queue<int>();
    
                //Enqueing elements to the Queue
                Console.WriteLine("Enqueing elements to the Queue");
                for (int i = 0; i < 10; i++)
                {
                    testQueue.Enqueue(i);
                }
                Console.WriteLine("Enqueing of Elements to the Queue is finished");
    
                // Dequing elements from the Queue
                // Please note stack is FIFO (First In First Out), Hence 0 should be 
                // dequed first, then 1, then 2, nd so on ... 
    
                Console.WriteLine("Dequing elements from the Queue started");
    
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Dequed element is : " + testQueue.Dequeue());
                }
                Console.WriteLine("Dequeing of Elements from the Queue is finished");
    
                // Waiting for enter to be pressed by user to prevent the Console Screen from 
                // closing automatically.
                Console.ReadLine();
    
            }
        }
    }
    
    
    Output: The output would look like this: 


    Hope you liked the article. Let us know your feedback through comments. Cheers!!

    No comments:

    Post a Comment

    Thanks for your valuable comment