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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| Sponsored Links |
| |||
| 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... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |