Home »
        VB.Net »
        VB.Net Programs
    
    VB.Net program to implement Double Stack
    
    
    
    
	    
		    By Nidhi Last Updated : November 15, 2024
	    
    
    
    VB.Net – Double Stack
    Here, we will implement a double-stack using class; we can implement two stacks inside a single array using class.
    
    Program/Source Code:
    The source code to implement Double Stack is given below. The given program is compiled and executed successfully.
    VB.Net code to implement Double Stack
'VB.Net program to implement Double Stack.
Imports System.IO
Module Module1
    Class 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 Class
    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 class 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.
    VB.Net Data Structure Programs »
	
    
    
    
   
    
  
    Advertisement
    
    
    
  
  
    Advertisement