Implementing a stack using array Learn programming Visual Basic (VB.net)

Lesson:

Dynamic Memory Management


Exercise:

Implementing a stack using array


Objetive:

Implementing a stack


Code:

Imports System
Namespace StackInt
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim depurando As Boolean = True
            Dim miPila As Pila = New Pila(5)
            miPila.Apilar(4)
            miPila.Apilar(5)
            Console.WriteLine(miPila.Desapilar())
            Console.WriteLine(miPila.Desapilar())
            If depurando Then Console.ReadLine()
        End Sub
    End Class

    Class Pila
        Private valor_actual As Integer
        Private cantidad_valores As Integer
        Private valores_pila As Integer()

        Public Sub New(ByVal cantidad_valores As Integer)
            valor_actual = 0
            Me.cantidad_valores = cantidad_valores
            valores_pila = New Integer(cantidad_valores - 1) {}
        End Sub

        Public Sub Apilar(ByVal valor As Integer)
            If valor_actual < cantidad_valores Then
                valores_pila(valor_actual) = valor
                valor_actual += 1
            End If
        End Sub

        Public Function Desapilar() As Integer
            If valor_actual > 0 Then
                valor_actual -= 1
                Return valores_pila(valor_actual)
            End If

            Return 0
        End Function
    End Class
End Namespace

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.