Using Queue class in C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Using Queue class in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Using Stack class in C# [next Lesson]  Comparison Between Properties and Indexers
Using Queue class in C#
  by: amsprasad

Using Queue class in C#

by: amsprasad

Summary

The Source code of the Queue Class usage in C#. WinForms App which visually shows the Queue. Below is the Doc to show the usage of Queue Class. Just compile the QueueDemo.cs file using csc QueueDemo.cs and run QueueDemo.exe to see the sample.

Queue is a Simple DataStucture which allows Insert/Remove of Items at one of the ends only. It is basically called as FIFO(First In First Out) data structure. ie The item which is added first is the first one to be removed. .NET has a built in class for Queue. It is foundin System.Collections namespace.

1. How to Create a Queue?
Queue q = new Queue(32);
32 is the intial size of the Queue. It is always better to give intially size so that the Insert/Remove operations are performed efficiently with out re-arranging the elements.

2. How to add an element to the Queue?
  q.Enqueue(12);
Enqueue() will add an element to the Queue. The item is inserted at the last available position of the queue.

3. How to Remove an Element?
  object ob = q.Dequeue()
Dequeue() will return the element which is added very old and also removes the item from the Queue. It throws Exception when the Queue is Empty.

4. What if i need to just see the Top element without removing it from the Queue?

Queue class has a method called Peek() which return the top element of the Queue. It is very similar to Dequeue() except that it would not remove the element from the Queue.

Prasad

The Code:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;

class QueueDemo : Form
{
    static Queue simpleQueue;
    Button btnEnQueue,btnPeek,btnDeQueue;
    TextBox txtItem;
    Label lblItem,lblQueue;
    ListBox lstQueueUI;
    
    public QueueDemo()
    {
        simpleQueue = new Queue(32);
        lblItem = new Label();
        lblItem.Text = "Item To Add";
        lblItem.Location = new Point(16,16);
        lblItem.AutoSize = true;
        
        txtItem = new TextBox();
        txtItem.Location = new Point(lblItem.Right + 8 ,lblItem.Top);
        txtItem.Size = new Size(256,27);
        
        btnEnQueue = new Button();
        btnEnQueue.Size = btnEnQueue.Size;
        btnEnQueue.Location = new Point(txtItem.Right + 8,lblItem.Top);
        btnEnQueue.Text = "EnQueue";
        btnEnQueue.Click += new EventHandler(btnEnQueue_Click);
        
        btnPeek = new Button();
        btnPeek.Size = btnEnQueue.Size;
        btnPeek.Location = new Point(btnEnQueue.Right + 8,lblItem.Top);
        btnPeek.Text = "Peek";
        btnPeek.Click += new EventHandler(btnPeek_Click);
        
        btnDeQueue = new Button();
        btnDeQueue.Size = btnEnQueue.Size;
        btnDeQueue.Location = new Point(btnPeek.Right + 8,lblItem.Top);
        btnDeQueue.Text = "DeQueue";
        btnDeQueue.Click += new EventHandler(btnDeQueue_Click);
        
        lblQueue = new Label();
        lblQueue.Location = new Point(lblItem.Left,lblItem.Bottom + 32);
        lblQueue.Text = "Visual Queue";
        
        lstQueueUI = new ListBox();
        lstQueueUI.Location = new Point(lblItem.Left,lblQueue.Bottom + 16);
        lstQueueUI.Size = new Size(512,256);
        
        this.Text = "Queue Demo";        
        this.Controls.AddRange( new Control[]
            {
                lblItem,txtItem,
                btnEnQueue,btnPeek,btnDeQueue,
                lblQueue,lstQueueUI
            });
    }
    
    void btnEnQueue_Click(object sender,EventArgs e)
    {
        if(txtItem.Text.Trim() != String.Empty)
        {
            simpleQueue.Enqueue(txtItem.Text.Trim());
            lstQueueUI.Items.Insert(lstQueueUI.Items.Count,
"Added Element: " + txtItem.Text.Trim() + " At " + DateTime.Now.ToString());
        }
        else
        {
            MessageBox.Show("Empty Value Cannot be Added","QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
    }
    
    void btnPeek_Click(object sender,EventArgs e)
    {
        try
        {
            MessageBox.Show("Peek Element: "  
+ simpleQueue.Peek().ToString());
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message,"QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Error); 
        }
    }
    
    void btnDeQueue_Click(object sender,EventArgs e)
    {
        try
        {
            MessageBox.Show("Removed Element: "  + 

simpleQueue.Dequeue().ToString());
            lstQueueUI.Items.RemoveAt(0);
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message,"QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Error); 
        }
    }
    
    static void Main()
    {
        Application.Run(new QueueDemo());
    }
}

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Using Queue class in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Using Stack class in C# [next Lesson]  Comparison Between Properties and Indexers


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
sksandz 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center