A Simple Stack Class

This class module functions as a simple stack that can be dropped straight into any project requiring a stack object.

The class contains an internal collection of objects (variants) - you can simply change the data types passed in through the Push method and returned from the Pop method to use any data type you like.

screen-shot

The Push / Pop functionality comes from using the internal Collections Add method with the underused 'Before' parameter set as 1.

Simply paste the code into a class module with a relevant name, eg. cStack and you can use it straight away in your projects :


option Explicit
'
' cStack.cls
'
' This class implements a simple stack structure -
' at the moment it only uses a variant item in the
' collection, but of course you can use any
' variable / object type you like!


'
' Our Internal Collection
'
private mStackCollection as Collection

private Sub Class_Initialize()
'
' Create the internal collection
'
    set mStackCollection = new Collection
End Sub

private Sub Class_Terminate()
'
' Kill off the internal collection
'
    set mStackCollection = nothing
End Sub

public Sub Push(byval vData as Variant)
'
' Push an item onto the stack
'

'
' If there's stuff already on the stack, then
' insert the new item before the first item
'
    If mStackCollection.Count > 0 then
        mStackCollection.Add vData, , 1
    else
'
' Otherwise, just add it to the stack as the first item
'
        mStackCollection.Add vData
    End If
End Sub

public Function Pop() as Variant
'
' 'Pop' an item off the stack
'
    If mStackCollection.Count > 0 then
        If IsObject(mStackCollection.Item(1)) then
            set Pop = mStackCollection.Item(1)
        else
            Pop = mStackCollection.Item(1)
        End If
'
' Don't forget to remove the item from the stack
' once it's been popped !
'
        mStackCollection.Remove 1
    else
        Err.Raise vbObjectError + 4999, "cStack::Pop", _
                  "Stack is empty"
    End If
    
End Function

public property get StackCount() as Long
'
' Number of items in stack
'
    StackCount = mStackCollection.Count
End property
'
'

Download Zipped Project File containing class and sample(5k)



Comments

  • STACK CLASS GIVES AN ERROR !!!

    Posted by Legacy on 05/09/2001 12:00am

    Originally posted by: Siddharth.S.Pawar


    Hello,
    I downloaded your stack class and found some error in it.If the stack is totally empty and still u perform the pop operation,it gives an Run-Time error.I have fixed that error at my place,if u want the code,please contact me...

    Anyway,
    THANKS FOR YOUR STACK CLASS......????

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds