Stack Code

Here you can see a VB.NET implementation of a stack, including the Push and Pop operations.

The code is driven from a form like this:

stackdemoform

This stack has been implemented using an array variable, aStackData. The Integer variable iMaxSize defines how big the stack can grow. This has been initialised to a value of 4 (5 items) to match the upper bounds of the array. The Integer variable iTop is used as a pointer to the top of the stack. It has an initial default value of 0. iTop is incremented each time a new item is pushed onto the stack, and decremented when an item is popped off it. Notice that when data is pushed, a test is performed for a stack overflow. Also notice that when data is popped in this example, nothing is done with other than to display it in a message box. In reality, it would be used somehow.

In some circumstances, it might be more appropriate to implement the Push and Pop operations as functions, rather than procedures as shown here. The Push function would have one parameter to accept the value to the pushed onto the stack, and it might return a Boolean or a string to indicate success or failure of the Push operation. The Pop function would not need to accept any parameters at all, but it would return the value that was popped.

Public Class Form1

    Dim aStackData(4) As Integer
    Dim iTop As Integer
    Dim iMaxSize As Integer = 4

    Private Sub btnPush_Click(sender As Object, e As EventArgs) Handles btnPush.Click
        Dim iDataItem As Integer
        iDataItem = CInt(Me.TextBox1.Text)
        Call Push(Me.TextBox1.Text)
    End Sub

    Private Sub btnPop_Click(sender As Object, e As EventArgs) Handles btnPop.Click
        Call Pop()
    End Sub

    Sub Push(NewItem As Integer)
        If iTop = iMaxSize Then
            MsgBox("stack overflow")
        Else
            iTop = iTop + 1
            aStackData(iTop) = NewItem
        End If
    End Sub

    Sub Pop()
        If iTop = 0 Then
            MsgBox("stack is empty")
        Else
            MsgBox("removing " & aStackData(iTop))
            iTop = iTop - 1
        End If
    End Sub

End Class

The following example takes an object oriented approach to the implementation of a stack. The Stack has been defined as a new class, in order to hide the implementation code (encapsulate) from any programmer who might make use of the stack. The form code creates a new instance of the class (Dim s As New Stack) and the buttons call the push and pop methods of the stack object.

Public Class Form1
    Dim s As New Stack

    Private Sub btnPush_Click(sender As Object, e As EventArgs) Handles btnPush.Click
        Dim iDataItem As Integer
        iDataItem = CInt(Me.TextBox1.Text)
        s.Push(iDataItem)
    End Sub

    Private Sub btnPop_Click(sender As Object, e As EventArgs) Handles btnPop.Click
        s.Pop()
    End Sub

End Class

Class Stack

    Dim aStackData(4) As Integer
    Dim iTop As Integer
    Dim iMaxSize As Integer = 4

    Sub Push(NewItem As Integer)
        If iTop = iMaxSize Then
            MsgBox("stack overflow")
        Else
            iTop = iTop + 1
            aStackData(iTop) = NewItem
        End If
    End Sub

    Sub Pop()
        If iTop = 0 Then
            MsgBox("stack is empty")
        Else
            MsgBox("removing " & aStackData(iTop))
            iTop = iTop - 1
        End If
    End Sub

End Class