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

Lesson:

Dynamic Memory Management


Exercise:

Implementing a queue using array


Objetive:

Implementing a queue


Code:

Imports System
Class Cola
    Private pila As Integer()
    Private posicion As Integer
    Private tamanyo As Integer

    Public Sub New(ByVal tamanyo As Integer)
        posicion = -1
        Me.tamanyo = tamanyo
        pila = New Integer(Me.tamanyo - 1) {}
    End Sub

    Public Sub Encolar(ByVal valor As Integer)
        posicion += 1
        pila(posicion) = valor
    End Sub

    Public Function Desencolar() As Integer
        Dim aux As Integer = pila(0)

        For i As Integer = 0 To posicion - 1
            pila(i) = pila(i + 1)
        Next

        pila(posicion) = aux
        posicion -= 1
        Return pila(posicion + 1)
    End Function
End Class

Class TestCola
    Private Shared Sub Main()
        Dim depurando As Boolean = False
        Dim cola As Cola = New Cola(2)
        cola.Encolar(1)
        cola.Encolar(2)
        Console.WriteLine(cola.Desencolar())
        Console.WriteLine(cola.Desencolar())
        If depurando Then Console.ReadLine()
    End Sub
End Class

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