In Part 1 we discussed the rationale behind building a queue class. We
discussed what methods and properties would be needed, and looked at how the entire design process could be
simplified by reusing the WeakList class presented in the stack
class article. In this part, we'll look at the code for the queue class's three methods and single property
as well as how to use the queue class in an ASP page!
The queue class acts like an ordered list. The first element placed on the queue is the first to be removed.
Compare this to the functionality of the stack class, where the first
element added was the last element to be removed. A queue is referred to as being FIFO (First In, First
Out), while a stack is referred to as being LIFO (Last In, First
Out). As with the stack class our queue class's methods and
properties are each only one line long, thanks to the WeakList class!
Here, now, is an example of using the Queue class! Note that this example assumes that the queue class is placed
within its own file, Queue.Class.asp. (A live demo is available for
this example.)
|
In the above example a queue instance is created and four words in a sentence are enqueued. The head of the queue is peeked at, and then the entire queue is dequeued and outputted. Note that the output of the four words would be the exact same order that they were inputed: "This is a test!" This is the nature of a queue, or any FIFO data structure.
I hope this article has proved useful. Feel free to cut and paste the queue class into your ASP applications. In a future article I plan on tying together all three data structures that we've examined so far and look at several real-world applications in which these data structures are invaluable.
Until then, happy programming!
Attachments:
WeakList class



