Stack: Push

Push

Inserts an item at the top of the Stack.



 Public Sub Push(
	  ByRef Value As Variant )

Parameters

Value
[ByRef] Variant. The item to push onto the Stack.

Remarks

If Count already equals the capacity, the capacity of the Stack is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count.

Examples

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

Public Sub Main()
    ' Creates an initializes a new Stack.
    Dim MyStack As New Stack
    MyStack.Push "The"
    MyStack.Push "quick"
    MyStack.Push "brown"
    MyStack.Push "fox"
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Removes an element from the Stack.
    Debug.Print t("(Pop)\t\t") & MyStack.Pop
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Removes another element from the Stack.
    Debug.Print t("(Pop)\t\t") & MyStack.Pop
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Views the first element in the Stack but does not remove it.
    Debug.Print t("(Peek)\t\t") & MyStack.Peek
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
End Sub

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

' This example code produce the following output.
'
'    Stack values:    fox    brown    quick    The
'    (Pop)       fox
'    Stack values:    brown    quick    The
'    (Pop)       brown
'    Stack Values:    quick The
'    (Peek)      quick
'    Stack Values:    quick The

See Also

Project CorLib Overview

Class Stack Overview

Pop

Peek