Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Enqueue and Dequeue values in queue, getting peek value of queue and converting queue in to array in VB and C#.

Author: Zunnair
Download Source Code : 913_Queue.zip

In this simple article you will learn that how we can Enqueue and Dequeue values in queue, how we can get peek value of queue and how to convert queue in an array using system.collection.queue in VB a

 

Queue:
Queue is a data structure which is used to store multiple values of same type and to do different operations. Queue uses a FIFO (first in first out) policy. This policy means that the value which will come first in the queue will go out at first. Enqueue and Dequeue are the terms which are used to store and remove values from queue. Dot net provides us a class named queue through which we can perform different queue operations.
Create Object:
C#
System.Collections.Queue obj = new System.Collections.Queue(5);
VB
Dim obj As New System.Collections.Queue(5)
Enqueue:
Enqueue means to store something in queue or store value in queue. Same type of values can enqueue in queue. The value which is enqueue in the first is store at the top of the queue. To enqueue value queue class provides us a function named Enqueue (). The function takes an object as a value and returns nothing. Null value also can be Enqueue. Any type of value which is enqueueed to queue should convert to object before enqueue in queue because the function takes object type value as a parameter.
 To demonstrate make a window application. Drag one text box and button on form. Enter value in text box and press button to enqueue value.
Now write the following code on Button click event:
C#
private void btn_Enqueue_Click(object sender, EventArgs e)
        {
            if (obj.Count != 5)
            {
                string str = string.Empty;
                str = txt_Enqueue.Text;
                object ob = (object)str;
                obj.Enqueue(ob);
                MessageBox.Show("value stored in queue");
            }
            else
                MessageBox.Show("queue is full");
        }
VB
Private Sub btn_Enqueue_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 5 Then
            Dim str As String = String.Empty
            str = txt_Enqueue.Text
            Dim ob As Object = DirectCast(str, Object)
            obj.Enqueue(ob)
            MessageBox.Show("value stored in queue")
        Else
            MessageBox.Show("queue is full")
        End If
    End Sub
This simple code enqueue the value in queue.
Dequeue:
Dequeue means to remove something from queue. When we want to remove the value from queue only that value which is at top is removed because as I discussed queue uses a LIFO policy. So the value enqueue first will remove at first. To dequeue value from queue, queue class provides us a function named dequeue (). The function takes nothing and returns object type value. When the value is dequeue it is removed from the queue and the next value becomes the peek value.
 To demonstrate make a window application. Drag button on form. Press button to dequeue value.
Now write the following code on Button click event:
C#
private void btn_Dequeue_Click(object sender, EventArgs e)
        {
            if (obj.Count != 0)
            {
                string str = string.Empty;
                object ob;
                ob = obj.Dequeue();
                str = (string)ob;
                MessageBox.Show("value remove from queue is " + str);
            }
            else
                MessageBox.Show("queue is empty");
        }
VB
Private Sub btn_Dequeue_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 0 Then
            Dim str As String = String.Empty
            Dim ob As Object
            ob = obj.Dequeue()
            str = DirectCast(ob, String)
            MessageBox.Show("value remove from queue is " & str)
        Else
            MessageBox.Show("queue is empty")
        End If
    End Sub
This simple code dequeue the value from queue.
 
Peek:
When we do dequeue the value at top removes form the queue. But sometimes we do not know that what value is at top in queue and we do not want to dequeue it without knowing. So for this purpose we have a function named peek (). The function takes nothing but it returns object value which is at peek but does not remove that value like dequeue function.
To demonstrate make a window application. Drag button on form. Press button to get peek value.
Now write the following code on Button click event:
C#
private void btn_Peek_Click(object sender, EventArgs e)
        {
            if (obj.Count != 0)
            {
                string str = string.Empty;
                object ob;
                ob=obj.Peek();
                str = (string)ob;
                MessageBox.Show("value at peek is " + str);
            }
            else
                MessageBox.Show("queue is empty");
        }
VB
Private Sub btn_Peek_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 0 Then
            Dim str As String = String.Empty
            Dim ob As Object
            ob = obj.Peek()
            str = DirectCast(ob, String)
            MessageBox.Show("value at peek is " & str)
        Else
            MessageBox.Show("queue is empty")
        End If
    End Sub
This simple code get peek the value from queue.
To Array:
As I discussed that queue stores object type values. So when we want to convert queue in array we will get an array of object type. For this purpose we have a function named to array (). Function takes nothing but returns the object array. Only an array of values is returned, values do not remove from queue.
To demonstrate make a window application. Drag button and list box on form. Press button.
Now write the following code on Button click event:
C#
private void btn_ToArray_Click(object sender, EventArgs e)
        {
            if (obj.Count != 0)
            {
                object[] arr;
                arr = obj.ToArray();
                for (int i = 0; i < obj.Count; i++)
                    listBox1.Items.Add(arr[i]);
            }
            else
                MessageBox.Show("queue is empty");
       }
VB
Private Sub btn_ToArray_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 0 Then
            Dim arr As Object()
            arr = obj.ToArray()
            For i As Integer = 0 To obj.Count - 1
                listBox1.Items.Add(arr(i))
            Next
        Else
            MessageBox.Show("queue is empty")
        End If
    End Sub
This is simple code to convert queue into an array.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Queue Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasps Queue Application"
    End Sub
 
This simple article tells how we can Enqueue and Dequeue values in queue, how we can get peek value of queue and how to convert queue in an array using system.collection.queue in VB and C#.
 
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Bit Array Class Operations (Not, XOR, Is synchronized, Set All) in VB and C# (.net 2.0).Part2

Disclaimer - Privacy
© 2002-2017 DevASP.net