IT Community - Software Programming, Web Development and Technical Support

Using Queue in C#

This is a discussion on Using Queue in C# within the C# Programming forums, part of the Software Development category; Adding Items to the Queue : To add items to the Queue you use Enqueue method. This method takes an object ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-26-2007, 05:54 AM
SaravananJ SaravananJ is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 79
SaravananJ is on a distinguished road
Thumbs up Using Queue in C#

Adding Items to the Queue :

To add items to the Queue you use Enqueue method. This method takes an object of any type but to keep things simple lets just give it a string object:

Queue q = new Queue();
q.Enqueue("am");
q.Enqueue("inserting");
q.Enqueue("value");
q.Enqueue("to");
q.Enqueue("Queues");
q.Enqueue("List");

Viewing a Single Object Without Removing it From the Queue :

You can use the Peak method to get the topmost object in the queue without removing it from the queue:

Response.Write(q.Peek());

Viewing All the Objects in the Queue Without Removing Them :

You can read any of the data in the Queue by using an enumerator:

System.Collections.IEnumerator en = q.GetEnumerator();
while (en.MoveNext())
{
Response.Write(en.Current +" ");
}

Removing Items from the Queue :

To pop an item from the Queue you can use the Dequeue statement. This returns the topmost object of the queue.

while( q.Count > 0)
{
Response.Write(q.Dequeue +" ");
}
__________________
J.Saravanan

Last edited by SaravananJ : 12-26-2007 at 05:58 AM. Reason: Modified code font color
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-29-2007, 01:21 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Using Queue in C#

What if i need to just see the Top element without removing it from the Queue
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-29-2007, 01:22 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Using Queue in C#

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());
}
}
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a queue unavailable in SQL server Sundaram Database Support 1 03-13-2008 09:12 AM
How would you implement a queue from a stack? sundarraja C and C++ Programming 1 07-31-2007 03:49 AM
What is the minimum number of queues needed to implement the priority queue? sundarraja C and C++ Programming 1 07-31-2007 02:34 AM
Local Delivery queue vigneshgets Operating Systems 1 07-18-2007 04:48 AM
What would a rise in remote queue length generally indicate? karunagaran Server Management 0 07-17-2007 12:25 AM


All times are GMT -7. The time now is 08:41 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0