Stack data structure in VB.net

By: Issac Viewed: 153305 times  Printer Friendly Format    


Stack is another useful data structure in VB.net which is based upon push-pop method. It works on LIFO mechanism, the last value inserted will be the first one to be removed.

Syntax of Stack push and pop operations
Push: to add an item in the stack datastructure 
Syntax: Stack.Push (Object)
Pop: to return the item last inserted item in the stack 
Syntax: Stack.Pop ()
Return: the last object in the Stack 
Contains: to Check whether an object is in the stack or not
Syntax: Stack.Contains (Object)

VB.NET SourceCode 

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles Button1.Click
Dim stackTable As New Stack
stackTable.Push("1")
stackTable.Push("2")
stackTable.Push("3")
stackTable.Push("4")
stackTable.Push("5")
stackTable.Push("6")
stackTable.Push("7")
If stackTable.Contains("4") Then
MsgBox(stackTable.Pop())
Else
MsgBox("not exist")
End If
End Sub
End Class

When you run this program it numbers 1 to 7 (items) into the sack and then checks whether item “4” exists in the stack or not if it exists then the last item added is popped out(7 is popped out) if you check contains with 8 (i.e If stacktable.contains(“8”) ) then it will say not exist.

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial