import net.flash8.ds.LinkedList;
class net.flash8.ds.Stack
{
private var stackList:LinkedList;
public function Stack()
{
stackList = new LinkedList("stack");
}
public function push(object:Object):Void
{
stackList.insertAtFront(object);
}
public function pop():Object
{
return stackList.removeFromFront();
}
public function isEmpty():Boolean
{
return stackList.isEmpty();
}
public function print():Void
{
stackList.print();
}
}
import net.flash8.ds.Stack;
import net.flash8.ds.EmptyListError;
var stack = new Stack();
var bool:Boolean = true;
var integer:Number = 12324;
var string:String = "hello";
var movieClip:MovieClip = new MovieCip("movie clip");
stack.push(bool);
stack.print();
stack.push(integer);
stack.print();
stack.push(string);
stack.print();
stack.push(movieClip);
stack.print();
try
{
var removedObject:Object = null;
for(var i:Number=1;i<6;i++)
{
removedObject = stack.pop();
trace(removedObject.toString()+" popped");
stack.print();
}
}
catch (emptyListError)
{
emptyListError.messageTrace();
}
output出的结果:
/=======begin of the stack table ===========\ true \=========end of the stack table ===========/
/=======begin of the stack table ===========\ 12324 true \=========end of the stack table ===========/
/=======begin of the stack table ===========\ hello 12324 true \=========end of the stack table ===========/
/=======begin of the stack table ===========\ undefined hello 12324 true \=========end of the stack table ===========/
undefined popped /=======begin of the stack table ===========\ hello 12324 true \=========end of the stack table ===========/
hello popped /=======begin of the stack table ===========\ 12324 true \=========end of the stack table ===========/
12324 popped /=======begin of the stack table ===========\ true \=========end of the stack table ===========/
true popped Empty stack An EmptyListError:The stack is empty 
|