org.as2lib.core.BasicInterface +--org.as2lib.data.holder.Stack
Stack
is the base interface for data holders that follow the 'last-in,
first-out' policy.
It offers the opposite functionality of a queue, which follows the 'first-in, first-out' policy.
'last-in, first-out' means that the last value that has been pushed to the stack is the first that is popped from the stack.
The usage of a stack is quite simple. You have one method to push values, push, and one method to pop values, pop. You can also peek at the top of the stack to see what's the last value that has been pushed to the stack without removing it peek.
If you want to iterate over the values of the stack you can either use the iterator returned by the iterator method or the array that contains the stack's values returned by the toArray method.
The two methods isEmpty and size let you find out whether the stack contains values and how many values it contains.
Example:
// the stack gets set up
var stack:Stack = new MyStack();
stack.push("value1");
stack.push("value2");
stack.push("value3");
// the stack gets used somewhere in your application
trace(stack.peek()); // traces the last element without removing it
while (!stack.isEmpty()) {
trace(stack.pop());
}
Output:
value3 value3 value2 value1
public function push(value):Void
Pushes the passed-in value
to this stack.
value | the value to push to this stack |
public function pop(Void)
Removes and returns the lastly pushed value.
the lastly pushed value
EmptyDataHolderException | if this stack is empty |
public function peek(Void)
Returns the lastly pushed value without removing it.
the lastly pushed value
EmptyDataHolderException | if this stack is empty |
public function iterator(Void):Iterator
Returns an iterator to iterate over the values of this stack.
an iterator to iterate over this stack's values
public function isEmpty(Void):Boolean
Returns whether this stack is empty.
true
if this stack is empty else false
public function size(Void):Number
Returns the number of pushed values.
the number of pushed values
public function toArray(Void):Array
Returns the array representation of this stack.
The elements are copied onto the array in a 'last-in, first-out' order, similar to the order of the elements returned by a succession of calls to the pop method.
the array representation of this stack