Development‎ > ‎VBScript‎ > ‎

LILO queue

Following code provides easy implementation of the last-in-last-out (LILO) queue concept as a VBScript class.

The class has two public methods which add and remove the elements from the queue.
'* Class name: LILO_QUEUE
'* LILO (Last-In-Last-out) Queue
'* method enqueue
'* method dequeue
    Public iCount
    Private aList()
    Private Sub class_Initialize()
        iCount = 0
        ReDim aList(iCount)
    End Sub
    Public Sub enqueue(sElement)
        iCount = iCount + 1
        ReDim preserve aList(iCount)
        aList(iCount-1) = sElement
    End Sub
    Public Function dequeue
        If iCount = 0 Then
            dequeue = -1
                        Exit Function
                End If
                iCount = iCount - 1
                dequeue = aList(iCount)
                ReDim preserve aList(iCount)
    End Function
End Class

Following is a simple example on how the class could be used:

'Declare the variable and create the instance of the LILO_Queue class
Dim myQueue
Set myQueue = New LILO_QUEUE

'Add elements to the end of the queue
myQueue.enqueue "a"
myQueue.enqueue "b"
myQueue.enqueue "c"

MsgBox myQueue.dequeue 'Call returns: a
MsgBox myQueue.dequeue 'Call returns: b
MsgBox myQueue.dequeue 'Call returns: c