VB.Net program to peek an item from the stack using the collection

Here, we are going to learn how to peek an item from the stack using the collection in VB.Net?
Submitted by Nidhi, on January 15, 2021 [Last updated : March 08, 2023]

Peek an item from stack using the collection in VB.Net

Here, we will use Peek() method of the Stack collection class to get the item from the stack without removing it.

Program/Source Code:

The source code to peek an item from the stack using collection is given below. The given program is compiled and executed successfully.

VB.Net code to peek an item from the stack using the collection

'VB.Net program to peek an item from 
'the stack using the collection.

Imports System
Imports System.Collections

Module Module1
    Sub Main()
        Dim stk As New Stack(5)

        stk.Push(40)
        stk.Push(30)
        stk.Push(20)
        stk.Push(10)

        Console.WriteLine("Peeked Element: " & stk.Peek())
        Console.WriteLine("Peeked Element: " & stk.Peek())
        Console.WriteLine("Peeked Element: " & stk.Peek())
        Console.WriteLine("Peeked Element: " & stk.Peek())
    End Sub
End Module

Output

Peeked Element: 10
Peeked Element: 10
Peeked Element: 10
Peeked Element: 10
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, Here, we created an object of Stack collection class for 5 elements. After that we inserted 4 items using the Push() method into the stack and then call the Peek() method 4 items but it will return the same item every time because we can get the item from the stack by removing it using Peek() method.

VB.Net Data Structure Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.