Queue: Enqueue

Enqueue

Adds an item to the tail of the Queue, expanding the queue as necessary.



 Public Sub Enqueue(
	  ByRef Value As Variant )

Parameters

Value
[ByRef] Variant. The item to be placed at the tail of the Queue.

Remarks

The capacity of a Queue is the number of elements the Queue can hold. As elements are added to a Queue, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize.

If Count is less than the capacity of the internal array, this method is an O(1) operation. If the internal array needs to be reallocated to accommodate the new element, this method becomes an O(n) operation, where n is Count.

Examples

The following example shows how to add elements to the Queue, remove elements from the Queue, or view the element at the beginning of the Queue.

Public Sub Main()
    Dim MyQ As New Queue
    
    MyQ.Enqueue "The"
    MyQ.Enqueue "quick"
    MyQ.Enqueue "brown"
    MyQ.Enqueue "fox"
    
    ' Display the Queue
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Removes an element from the queue.
    Debug.Print "(Dequeue)    " & MyQ.Dequeue
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Removes another element from the queue.
    Debug.Print "(Dequeue)    " & MyQ.Dequeue
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Views the first element in the queue but does not remove it.
    Debug.Print "(Peek)       " & MyQ.Peek
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
End Sub

Private Sub PrintValues(ByVal MyCollection As IEnumerable)
    Dim Item As Variant
    
    For Each Item In MyCollection
        Debug.Print "    " & Item;
    Next
    
    Debug.Print
End Sub

' This example code produces the following output.
'
'    Queue values:    The    quick    brown    fox
'    (Dequeue)    The
'    Queue values:    quick    brown    fox
'    (Dequeue)    quick
'    Queue Values:    brown fox
'    (Peek)       brown
'    Queue Values:    brown fox

See Also

Project CorLib Overview

Class Queue Overview

Dequeue

Peek