Interface org.as2lib.data.holder.Stack

org.as2lib.core.BasicInterface
   +--org.as2lib.data.holder.Stack

Description

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
 

Method Index

isEmpty(), iterator(), peek(), pop(), push(), size(), toArray()

Inherited from BasicInterface

toString()

Method Detail

push

public function push(value):Void

Pushes the passed-in value to this stack.

Parameters

valuethe value to push to this stack

pop

public function pop(Void)

Removes and returns the lastly pushed value.

Return

the lastly pushed value

Throws

EmptyDataHolderExceptionif this stack is empty

peek

public function peek(Void)

Returns the lastly pushed value without removing it.

Return

the lastly pushed value

Throws

EmptyDataHolderExceptionif this stack is empty

iterator

public function iterator(Void):Iterator

Returns an iterator to iterate over the values of this stack.

Return

an iterator to iterate over this stack's values

isEmpty

public function isEmpty(Void):Boolean

Returns whether this stack is empty.

Return

true if this stack is empty else false

size

public function size(Void):Number

Returns the number of pushed values.

Return

the number of pushed values

toArray

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.

Return

the array representation of this stack