Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Imports System.

IO

Module Module1
Class Stack
Private ele() As Integer
Private top As Integer
Private max As Integer

Public Sub New(ByVal size As Integer)


ReDim ele(size)
top = -1
max = size
End Sub

Public Sub push(ByVal item As Integer)


If (top = max - 1) Then
Console.WriteLine("Stack Overflow")
Else
top = top + 1
ele(top) = item
End If
End Sub

Public Function pop() As Integer


Dim item As Integer = 0
If (top = -1) Then
Console.WriteLine("Stack Underflow")
Return -1
Else
item = ele(top)
top = top - 1
Console.WriteLine("Poped element is: " & item)
Return item
End If
End Function

Public Sub printStack()


If (top = -1) Then
Console.WriteLine("Stack is Empty")
Else
For i = 0 To top Step 1
Console.WriteLine("Item(" & i + 1 & "): " & ele(i))
Next
End If
End Sub

End Class

Sub Main()
Dim S As New Stack(5)

S.push(10)
S.push(20)
S.push(30)
S.push(40)
S.push(50)

Console.WriteLine("Items are : ")


S.printStack()

S.pop()
S.pop()
S.pop()
Console.ReadKey()
End Sub
End Module

You might also like