VB.Net program to get the element from the Queue without removing it

Here, we are going to learn how to get the element from the Queue without removing it in VB.Net?
Submitted by Nidhi, on January 17, 2021 [Last updated : March 08, 2023]

Get a Queue element without removing it in VB.Net

Here, we will use the Peek() method of Queue collection to get the item from the Queue without removing it, and print the item on the console screen.

Program/Source Code:

The source code to get the element from the Queue without removing it is given below. The given program is compiled and executed successfully.

VB.Net code to get a Queue element without removing it

'VB.Net program to get the element from 
'the Queue without removing it.

Imports System
Imports System.Collections

Module Module1
    Sub Main()
        Dim queue As New Queue(5)

        queue.Enqueue("India")
        queue.Enqueue("USA")
        queue.Enqueue("UK")
        queue.Enqueue("CHINA")

        Console.WriteLine("Peeked item: " + queue.Peek())
        Console.WriteLine("Peeked item: " + queue.Peek())
        Console.WriteLine("Peeked item: " + queue.Peek())
        Console.WriteLine("Peeked item: " + queue.Peek())
    End Sub
End Module

Output

Peeked item: India
Peeked item: India
Peeked item: India
Peeked item: India
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, And, we created an object of the Queue collection class for 5 elements. After that, we inserted 4 items using the Enqueue() method and then get the item from the Queue collection without removing it using the Peek() method of Queue collection.

VB.Net Data Structure Programs »




Comments and Discussions!

Load comments ↻





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