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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值