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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

pre {font-family:"Courier New", Courier, Arial; font-size: 12px;}

.operator {color: #000000;}

.keyword {color: #993300;}

.identifier {color: #000087;}

.properties {color: #000087;}

.identifier2 {color : #000087;}

.linecomment, .blockcomment {color: #808080;}

.string {color: #0000FF;}

//****************************************************************************

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航