The StackItem Class

Started by sukishan, Sep 04, 2009, 05:01 PM

Previous topic - Next topic

sukishan

The StackItem Class
It doesn't get much simpler than this. The StackItem class maintains a data item, as well as a pointer to the next item in the structure, as shown in Listing 6.1.

Code for the StackItem Class

' Keep track of the next stack item,
' and the value of this item.
Public Value As Variant
Public NextItem As StackItemThe Stack Class
The StackItem class contains a single item, a pointer to the first item in the stack (the stack top). That pointer always points to the top of the stack, and it's at this location that you'll add (push) and delete (pop) items from the stack. The Stack class module implements the two methods (Push and Pop), as well as two read-only properties, StackTop (which returns the value of the element at the top of the stack, without popping the item) and StackEmpty (which returns a Boolean value indicating the status of the stack–True if there are no items in the stack and False if there are items).

Pushing Items onto the Stack
To add an item to the stack, you "push" it to the top of the stack. This is similar to pushing a new cafeteria tray to the top of the tray stack. When you push the new tray, each of the other trays moves down one position in the stack. Using linked lists, the code must follow these steps:

Create the new node.


Place the value to be stored in the new node.


Make the new node point to whatever the current stack top pointer refers to.


Make the stack top point to this new node.
The code in Listing 6.2 shows the Push method of the stack class. The four lines of code correspond to the four steps listed above.

Listing 6.2: Use the Push Method to Add a New Item to the Stack

Public Sub Push(ByVal varText As Variant)
    ' Add a new item to the top of the stack.

    Dim siNewTop As New StackItem

    siNewTop.Value = varText
    Set siNewTop.NextItem = siTop
    Set siTop = siNewTop
End Sub
A good beginning makes a good ending

emmamartin

Step by step description and short on supplies so easy to understand The StackItem Class and becomes an excellent guide for those who are not aware of this and who are new to this area.

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview