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 Push and Pop values in stack and getting peek value of stack in VB and C#.

Author: Zunnair
Download Source Code : 917_CONTROL.zip

In this simple article you will learn that how we can push and pop values in stack and how to get peek value of stack using system.collection.stack in VB and C#.

 

Stack:
Stack is a data structure which is used to store multiple values of same type and to do different operations. Stack uses a LIFO (last in first out) policy. This policy means that the value which will come at the end in the stack will go out at first. Push and pop are the terms which are used to store and remove values from stack. Dot net provides us a class named stack through which we can perform different stack operations.
Create Object:
C#
System.Collections.Stack obj = new System.Collections.Stack(5);
VB
Dim obj As New System.Collections.Stack(5)
Push:
Push means to push something in stack or store value in stack. Same type of values can be pushed in stack. The value which is pushed in the last is store at the top of the stack. To push value stack class provides us a function named push (). The function takes an object as a value and returns nothing. Null value also can be pushed. Any type of value which is pushed to stack should convert to object before pushing in stack 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 push value.
Now write the following code on Button click event:
C#
private void btn_Push_Click(object sender, EventArgs e)
        {
            if (obj.Count != 5)
            {
                string str = string.Empty;
                str = txt_Push.Text;
                object ob = (object)str;
                obj.Push(ob);
                MessageBox.Show("value pushed in stack");
            }
            else
                MessageBox.Show("stack is full");
        }
VB
Private Sub btn_Push_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 5 Then
            Dim str As String = String.Empty
            str = txt_Push.Text
            Dim ob As Object = DirectCast(str, Object)
            obj.Push(ob)
            MessageBox.Show("value pushed in stack")
            Else
            MessageBox.Show("stack is full")
        End If
    End Sub
This simple code pushes the value in stack.
Pop:
Pop means to remove something from stack. When we want to remove the value from stack only that value which is at top is removed because as I discussed stack uses a LIFO policy. So the value pushed at end will remove at first. To pop value from stack, stack class provides us a function named pop (). The function takes nothing and returns object type value. When the value is pop it is removed from the stack and the next value becomes the peek value.
 To demonstrate make a window application. Drag button on form. Press button to pop value.
Now write the following code on Button click event:
C#
private void btn_Pop_Click(object sender, EventArgs e)
        {
            if (obj.Count != 0)
            {
                string str = string.Empty;
                object ob;
                ob=obj.Pop();
                str = (string)ob;
                MessageBox.Show("value pop from stack is " + str);
            }
            else
                MessageBox.Show("stack is empty");
        }
VB
Private Sub btn_Pop_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.Pop()
            str = DirectCast(ob, String)
            MessageBox.Show("value pop from stack is " & str)
        Else
            MessageBox.Show("stack is empty")
        End If
    End Sub
This simple code pops the value from stack.
 
Peek:
When we do pop the value at top removes form the stack. But sometimes we do not know that what value is at top in stack and we do not want to pop 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 pop 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("stack 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("stack is empty")
        End If
    End Sub
This simple code gets the peek value from stack.
 
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Stack Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasps Stack Application"
    End Sub
This simple article tells that how we can push and pop values in stack and how to get peek value of stack using system.collection.stack in VB and C#.
Article Comments
This is really helpful for me. Nice work.

Posted on 11/6/2009 6:04:28 AM by Ravi Verma

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to check syntax (Machine name, path, rooted path) using syntax class in VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net