//**************************************************************************** // FileName: Stack.as // Description:Stack // Author: AOL // Last Modified:14/10/2003 //**************************************************************************** import net.flash8.ds.LinkedList; class net.flash8.ds.Stack { private var stackList:LinkedList; // constructor public function Stack() { stackList = new LinkedList("stack"); } // add object to stack public function push(object:Object):Void { stackList.insertAtFront(object); } // remove object from stack public function pop():Object { return stackList.removeFromFront(); } // determine if stack is empty public function isEmpty():Boolean { return stackList.isEmpty(); } // output stack contents public function print():Void { stackList.print(); } } // end class Stack //SatckTest.fla import net.flash8.ds.Stack; import net.flash8.ds.EmptyListError; var stack = new Stack(); // create objects to store in the stack var bool:Boolean = true; var integer:Number = 12324; var string:String = "hello"; var movieClip:MovieClip = new MovieCip("movie clip"); // use push method stack.push(bool); stack.print(); stack.push(integer); stack.print(); stack.push(string); stack.print(); stack.push(movieClip); stack.print(); // remove items from stack try { var removedObject:Object = null; for(var i:Number=1;i<6;i++) { removedObject = stack.pop(); trace(removedObject.toString()+" popped"); stack.print(); } } // catch exception if stack empty when item popped 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
本文地址:http://com.8s8s.com/it/it35069.htm