VBScript only provides a single built-in data type: the array. From that data type you can do a lot, though. Sometimes it's nice to have other data types, such as a linked list, stack, or queue. For those who are unfamiliar with these terms, let me take a moment to explain:
List: - An ordered collection of items. Each item in a list is "connected" to
one another through some means, and a global reference exists to the starting item in
the list. That way, to visit any other item in the list, the user can sequentially step
through the list.
Queue: - An ordered collection of items that follows the following semantic: first in, first out. A queue has the following operations: enqueue and dequeue. When an item is enqueued, it is added to the queue, while a dequeue removes the item. The items are removed in the order they are added to the queue. So, if a queue had three items enqueued - A, B, and C - it would first dequeue A, then B, and then C.
Stack: - A stack is similar to a queue except it follows a first in, last out semantic. A stack, like a queue, has two operations: push, which adds an item to the top of the stack, and pop, which removes an item from the top of the stack. A stack is like a stack of cafeteria trays. When taking a tray, you are removing the tray that was last put on the stack. Therefore, if we pushed A, B, and C onto a stack, when we popped the elements off, we'd get C, B, and A, in that order.
This article focuses on creating an easy-to-use stack class using VBScript. If you are new to classes or unfamiliar with classes, let me strongly suggest that you read the following articles before continuing:
(To use classes in VBScript, you must have the VBScript Scripting Engine Version 5.0 or greater. Check out Determining the Server-Side Scripting Language and Version to see what version of the VBScript you have and for instructions on downloading the latest version!)
As explained earlier in this article, a stack is a data structure that has a FILO (First
In Last Out) semantic and methods to permit the user to add elements
to the stack (
push) and remove elements from the stack (
To help implement this, we will first create a more generic class, a weak list class,
WeakList class is called weak because of two reasons:
1.) It really isn't a list
2.) It only supports a small subset of methods and properties a true list should support.
A "real" list data structure contains a number of elements linked together to form a list.
Rather than use this approach, we will use an array. To make things painfully easy, I will
be reusing some code presented in an earlier article, Dynamic Arrays.
Dynamic Arrays presented a class named
DynamicArray that encapsulated the complexity of constantly
ever changing array. If you have not read the Dynamic Arrays
article yet, I strongly encourage you to do so now...
WeakLink class contains a single member variable, an instance of the
DynamicArray class. Therefore, the file that contains the class definition
WeakLink must also include the class definition for
(I recommend you do this via a server-side include).
WeakList class contains only one property,
returns the number of elements in the list. The methods of the
can be seen in the table below:
||Adds the |
||Removes the head element of the list, and returns its value.|
||Adds the |
||Removes the tail element of the list, and returns its value.|
||Returns the value at the head of the list, without altering the list at all.|
||Returns the value at the tail of the list, without altering the list at all.|
The code for the
WeakClass class is a little too long to post. It is
available for downloading,
WeakList class should be placed in its own file
(I chose to use
WeakList.Class.asp and should contain a server-side
include to import the contents of the file that contains the
Now that we have our list class, we are ready to create our stack class. As we'll
see in Part 2, creating the stack class is
insanely easy when using the
WeakList class. In fact, our entire stack class
is only ~35 lines long (including blank lines to enhance readability)!