Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Queue Data Structure using Csharp and ASP.NET

This article we will cover almost all common features of queue, very useful when you have to serve longest weighted object first.

Introduction:

Queue data structure work First in First out basis (FIFO). Its means that the first inserted item will come out first. The Queue is very useful when you have to serve longest weighted object first.

This article will cover the following topics:


  1. Queue - Enqueue Item
  2. Queue - Dequeue Item
  3. Queue - Copy to Array
  4. Queue - Search Item
  5. Queue - DisplayQueueData
  6. Queue - Other Functionality

Initial UI and Queue Data

Figure 1: Initial UI and Queue Data

ASP.NET Code:

The below ASP.NET code is used in this tutorial to demonstrate the queue functionality. This script includes some textboxes , labels and buttons.

Listing 1: ASP.NET Script for Queue Demonstrate

<head runat="server">
    <title>MrBool.com Tutorial - Queue</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Item:
        <asp:TextBox ID="txtItem" runat="server"></asp:TextBox>
        <br />
        <br />
        Message:
        <asp:Label ID="txtMSG" runat="server" Text=""></asp:Label>
        <br />
        <br />
        Queue State:
        <asp:Label ID="txtQueue" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Button ID="btnEnqueue" runat="server" Text="Enqueue" OnClick="btnEnqueue_Click" />
         <asp:Button ID="btnDequeue" runat="server" Text="Dequeue" OnClick="btnDequeue_Click" />
         <asp:Button ID="btnCopytoArray" runat="server" Text="Copy to Array" OnClick="btnCopytoArray_Click" />
         <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
         </div>
    </form>
</body>

C# Code:

Lets start with C# code, You have to include the following namespaces to properly use the Queue collections.

Listing 2: Including the namespaces

using System;
using System.Collections.Generic;

Now you have to create Queue object and initialize it . The below code snippet will create a QueueData object and then initialize it.

Listing 3: Creating Queue Object

static Queue<string> QueueData; 
QueueData = new Queue<string>();

Queue - Enqueue Item:

The Enqueue means add the item in queue. The enqueue operation add element at the end of queue.

This tutorial is using a string queue, so you can only insert string values

Listing 4 : Example of Insert String Values

Queue < string> 

You can also have a string which work on class objects.

Enqueue Operation - 499 Inserted

Figure 2: Enqueue Operation - 499 Inserted

Listing 5: Queue - Enqueue Code

protected void btnEnqueue_Click(object sender, EventArgs e)
    {
        if (txtItem.Text.Trim() != "")
        {
            string QueueItem = txtItem.Text.Trim();
            QueueData.Enqueue(QueueItem);

            txtMSG.Text = QueueItem + " - Enqueue Complete.";
        }
        else
        {
            txtMSG.Text = "Enter an Item.";
        }
        DisplayQueueData();
    } 

Queue - Dequeue Item:

The Queue collection class provide a method Dequeue to remove item from front. Just use this method and it will remove the item and return it.

Dequeue Operation - 299 removed from Queue

Figure 3: Dequeue Operation - 299 removed from Queue

Listing 6: Queue - Dequeue Code

protected void btnDequeue_Click(object sender, EventArgs e)
    {
        if (QueueData.Count > 0)
        {
            string QueueItem = QueueData.Dequeue();
            txtMSG.Text = QueueItem + " - Dequeue Complete.";

        }
        else
        {
            txtMSG.Text = "Queue is Empty.";
        }

        DisplayQueueData();
    } 

Queue - Copy to Array:

You can copy the Queue data to an Array. This method copy all the element in array from given index.

Copy Operation - 399 and 499 copied to Array

Figure 4: Copy Operation - 399 and 499 copied to Array

Listing 7: Queue - Copy Operation

protected void btnCopytoArray_Click(object sender, EventArgs e)
    {
        string[] strArray = new string[QueueData.Count];
        QueueData.CopyTo(strArray, 0);

        foreach (string ArrayItem in strArray)
        {
            txtMSG.Text += ", " + ArrayItem;
        }

        txtMSG.Text = "Item Copied- " + txtMSG.Text.TrimStart(',');

    } 

Queue - Search Item:

To Search an item in Queue use the Contains method or use for loop to find a particular item. Note that Queue is also collection class ,so you can use foreach loop.

Search Operation - 499 found in Queue

Figure 5: Search Operation - 499 found in Queue

Listing 8: Queue - Search Operation

protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool blnFlag = false;
        foreach (string QueueItem in QueueData)
        {
            if (QueueItem == txtItem.Text.Trim())
            {
                blnFlag = true;
                break;
            }
        }

        if (blnFlag)
        {
            txtMSG.Text = txtItem.Text.Trim() + " Found in Queue.";
        }
        else
        {
            txtMSG.Text = txtItem.Text.Trim() + " Not Found in Queue.";
        }

        //Inbuilt method for search
        // QueueData.Contains(txtItem.Text.Trim());

        DisplayQueueData();
    }
 

Display Queue Data:

Use foreach loop to display data in Queue. We are using a label to display data of queue.

Listing 9: Queue - Loop through items and display data

  private void DisplayQueueData()
    {
        foreach (string QueueItem in QueueData)
        {
            txtQueue.Text += ", " + QueueItem;
        }

        txtQueue.Text = txtQueue.Text.TrimStart(',');
    } 

Queue -Other Functionality:

Queue provide some other functionalities by providing functions and properties. Like Clear to clear all data from Queue. Count to find number of items in Queue. Peek to get the first element of queue without removing it from queue.

Listing 10: Queue - Other operation

private void OtherFunctionality()
    {
        ////Clear all the items of Queue
        QueueData.Clear();

        txtMSG.Text = " Count of All the Items of Queue: " + QueueData.Count;

        //Check first element of Queue without removing it.
        txtMSG.Text = " First element of Queue: " + QueueData.Peek();

    } 

Conclusion:

This article covered almost all common features of queue and now you can make a application use it. I hope this tutorial is useful for you. Your comments and suggestions are always welcomed.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login