编程语言

img flashvan

使用链表(LinkedList) mix-in 到其中的栈(Stack)

发表于2004/10/12 14:07:00  1381人阅读

分类: flash

//****************************************************************************
// 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

阅读全文
0 0

相关文章推荐

img
取 消
img