VB.Net program to implement Double Stack using Structure

Here, we are going to learn how to implement Double Stack using Structure in VB.Net?
Submitted by Nidhi, on January 12, 2021

Here, we will implement a double-stack using structure; we can implement two stacks inside a single array using structure.

Program/Source Code:

The source code to implement Double Stack using Structure is given below. The given program is compiled and executed successfully.

'VB.Net program to implement Double Stack using Structure.

Imports System.IO

Module Module1
    Structure DoubleStack
        Private ele() As Integer
        Private top1 As Integer
        Private top2 As Integer
        Private max As Integer

        Public Sub New(ByVal size As Integer)
            ReDim ele(size)
            top1 = -1
            top2 = size
            max = size
        End Sub

        Public Sub PushStack1(ByVal item As Integer)
            If (top2 = top1 + 1) Then
                Console.WriteLine("Stack Overflow Stack1")
                Return
            End If

            top1 = top1 + 1
            ele(top1) = item
            Console.WriteLine("Inserted item in Stack1 : {0}", item)
        End Sub

        Public Sub PushStack2(ByVal item As Integer)

            If (top2 = top1 + 1) Then
                Console.WriteLine("Stack Overflow Stack2")
                Return
            End If

            top2 = top2 - 1
            ele(top2) = item
            Console.WriteLine("Inserted item in Stack2 : {0}", item)
        End Sub

        'Pop Operation on Stack1
        Public Sub PopStack1()
            Dim item As Integer
            If (top1 = -1) Then
                Console.WriteLine("Stack Underflow Stack1")
                Return
            End If
            item = ele(top1)
            top1 = top1 - 1
            Console.WriteLine("Poped Item: " & item)
        End Sub

        'Pop Operation on Stack2
        Public Sub PopStack2()
            Dim item As Integer = 0
            If (top2 = max) Then
                Console.WriteLine("Stack Underflow Stack2")
                Return
            End If
            item = ele(top2)
            top2 = top2 + 1
            Console.WriteLine("Poped Item: " & item)
        End Sub
    End Structure

    Sub Main()
        Dim DB As New DoubleStack(5)

        DB.PushStack1(10)
        DB.PushStack1(20)
        DB.PushStack1(30)
        DB.PushStack2(40)
        DB.PushStack2(50)
        DB.PushStack2(60)

        DB.PopStack1()
        DB.PopStack1()
        DB.PopStack1()

        DB.PopStack2()
        DB.PopStack2()
        DB.PopStack2()

        Console.WriteLine()
    End Sub
End Module

Output:

Inserted item in Stack1 : 10
Inserted item in Stack1 : 20
Inserted item in Stack1 : 30
Inserted item in Stack2 : 40
Inserted item in Stack2 : 50
Stack Overflow Stack2
Poped Item: 30
Poped Item: 20
Poped Item: 10
Poped Item: 50
Poped Item: 40
Stack Underflow Stack2

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 a structure DoubleStack that contains PushStack1(), PushStack2(), PopStack1(), and PopStack2() functions.

The PushStack1() and PushStack2() functions are used to store data elements into stack1 and stack2 respectively. The PopStack1() and PopStack2() functions are used to get elements from stack1 and stack2 respectively and print the items on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.