Stack

Stack


Represents a simple last-in-first-out (LIFO) non-generic collection of items.


Implements:

ICloneable 
ICollection 
IEnumerable 
IObject 
IVersionable 

Public:

Properties:

NameDescription
 Count (get) Gets the number of elements contained in the Stack.  

Methods:

NameDescription
 Clear Removes all items from the Stack.  
 Clone Creates a shallow copy of the Stack.  
 Contains Determines whether an element is in the Stack.  
 CopyTo Copies the Stack to an existing one-dimensional array, starting at the specified array index.  
 Equals Determines whether the specified value is equal to the current object.  
 GetEnumerator Returns an enumerator to enumerate the colleciton  
 GetHashCode Returns a pseudo-unique number identifying this instance.  
 Peek Returns the item at the top of the Stack without removing it.  
 Pop Removes and returns the item at the top of the Stack.  
 Push Inserts an item at the top of the Stack.  
 ToArray Copies the Stack to a new array.  
 ToString Returns a string representation of this object instance.  

Remarks

The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.

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. Pop is an O(1) operation.

Examples

The following example shows how to create and add values to a Stack and how to print out its values.

Public Sub Main()
    ' Creates an initializes a new Stack.
    Dim MyStack As New Stack
    MyStack.Push "Hello"
    MyStack.Push "World"
    MyStack.Push "!"
    
    ' Displays the properties and values of the Stack.
    Debug.Print "MyStack"
    Debug.Print vbTab & "Count:    " & MyStack.Count
    Debug.Print vbTab & "Values:";
    PrintValues MyStack
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 produce the following output.
'
'    MyStack
'        Count:    3
'        Values:    !    World    Hello

See Also

Project CorLib Overview

Class Stack Overview

Constructors

ICollection

IEnumerable

ICloneable