How to VB.Net Stack

The Stack is an effortlessly accessible VB.NET collection, known for its user-friendly nature. It strictly adheres to the push-pop operations, allowing the addition of items to the Stack through the Push method and subsequent retrieval through the Pop method. Moreover, the Stack adheres to the Last In First Out (LIFO) principle, enabling the retrieval of items in reverse order. Hence, the Stack conveniently returns the most recently added item as the first one to be retrieved.

Commonly used methods :

Stack class provides several commonly used methods for managing and manipulating stacks. Here are a few examples of these methods:

Push Method

The Push method is used to add an item to the top of the stack. It takes an argument representing the item to be added.

Dim stack As New Stack() stack.Push("Apple") stack.Push("Banana") stack.Push("Orange")

Pop Method

The Pop method is used to remove and retrieve the item at the top of the stack. It does not take any arguments.

Dim stack As New Stack() stack.Push("Apple") stack.Push("Banana") stack.Push("Orange") Dim topItem As String = stack.Pop() Console.WriteLine(topItem) ' Output: Orange

Peek Method

The Peek method is used to retrieve the item at the top of the stack without removing it. It does not take any arguments.

Dim stack As New Stack() stack.Push("Apple") stack.Push("Banana") stack.Push("Orange") Dim topItem As String = stack.Peek() Console.WriteLine(topItem) ' Output: Orange

Count Property

The Count property returns the number of items in the stack.

Dim stack As New Stack() stack.Push("Apple") stack.Push("Banana") stack.Push("Orange") Dim itemCount As Integer = stack.Count Console.WriteLine(itemCount) ' Output: 3

The provided VB.NET source code showcases a selection of frequently utilized functions.

Full Source VB.NET
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("Sun") stackTable.Push("Mon") stackTable.Push("Tue") stackTable.Push("Wed") stackTable.Push("Thu") stackTable.Push("Fri") stackTable.Push("Sat") If stackTable.Contains("Wed") Then MsgBox(stackTable.Pop()) Else MsgBox("not exist") End If End Sub End Class

Conclusion

These are just a few examples of commonly used methods in the VB.NET Stack class. There are other methods available as well, such as Clear to remove all items from the stack and Contains to check if a specific item exists in the stack.