Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5254 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Introducing Queue in C#

Introducing Queue in C#

1 vote(s)
Rating: 3 out of 5
Article posted by Gajananbok on 12/1/2011 | Views: 2910 | Category: C# | Level: Beginner | Points: 250 red flag


This article demonstrates how to implement queue in C# with the help of easy examples. Queue is a data structure element where elements are in form FIFO.

Queue:

Queues are dynamic collections which have some concept of order. A Queue is a collection where elements are processed first in, first out (FIFO). The item is put first in the queue is read first. The item in the end is read last.

Priority queue : A queuing policy in which each member has a priority determined by external factors. The member with the highest priority is the first to be removed.

 Real life Example of queues –

1) Patients waiting outside the doctor's clinic: The patient who comes first visits the doctor first, and the patient who comes last visits the doctor last. Therefore, it follows the first-in-first-out (FIFO) strategy of queue.

2) Queue in the Bank

3) Queue at the bus stand

4) Queue at the Ration shop

 

Examples of queues in Software systems :

1)      1) Print jobs waiting to be processed in a print queue

2)      2) A thread waiting for the CPU in a round-robin fashion.

 Often there are queues where the elements processed differ in their priority. For example, in the queue at the Bank, Gold card customers are processed before common customers. Here, multiple queues can be used, one queue for every priority. In the bank this can easily be found out, because there are separate queues for Gold card and common customers. You can have an array or a list of queues where on item in the array stands for a priority. Within every array item there is a queue, where processing happens with the FIFO principle.

 

Implementing classes and Interfaces :

In C# queue is implemented with Queue<T> class in the namespace System.Collections.Generic. It implements the interfaces IEnumerable & ICollection.

 

 Some useful methods of queue :

1) To create a queue:

Queue<int> p = new Queue<int>();

2) To add element to it :

p.Enqueue(i * 10);

3) To remove element from queue :

p.Dequeue( );

 

4) To count total elements in queue:

p.Count;

using System;
using System.Collections.Generic;

namespace ConsoleApplication17
{
  class Class1
  {
    public static void Main()
    {
      int i;
      Queue<int> p = new Queue<int>();
      for (i = 0; i < 5; i++)
      {
        p.Enqueue(i * 10);
      }
      Console.WriteLine("Total elements : " + p.Count);

      Console.WriteLine("Elements is queue are :");
      for (i = 0; i < 5; i++)
      {
        Console.WriteLine("Element {0}: {1} ", i, p.Dequeue());
      }
      Console.WriteLine("Total elements : "+ p.Count);
    }
  }
}

Output :

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Gajananbok
Experience:3 year(s)
Home page:
Member since:Thursday, December 01, 2011
Level:Starter
Status: [Member]
Biography:Working as a Trainer for .NET for 3 years .
>> Write Response - Respond to this post and get points
Related Posts

This article describes how can we use a delegate in a real time application to build dynamic components. I will write this article in two phases and the first phase goes here.

This article is inspired to present the easiest way of building your own speech to text based call recording system.

Generally in many websites after you get logged in you will be redirected to home page. While this page is running if the user selects the new window and open the Login page again and if he provides the same credentials which was provided earlier then he will be automatically redirected to home page. But in this article I am going to explain how to restrict this if you consider the examples of gmail.com or yahoo.com once in a particular user is logged in they won’t restrict the user but here I will provide that feature and check whether if a particular user is logged in I will display it as already logged in.

In this example i am explaining how to create AutoComplete TextBox or ComboBox in winforms using C#

This article describes about most of the Value Types variables, its uses and how to use them.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/20/2013 8:00:30 AM