分享
 
 
 

链表AS2实现

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

这些与数据结构相关的大部分内容,都是我去年在为闪吧写的AS2书中所写的例子。不幸的是,这本书的很多示例和文档我都已经遗失。这是示例是找出的早期版本。后面我把这些与数据结构相关的内容整理了Collection Framework,包括常用的ArrayList,LinkedList,Tree,HashTable等。希望当时拿到我第一手资料的朋友能发回我一份,谢谢先。

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: ListNode.as

// Description:class for cell of a List

// Author: AOL

// Last Modified:13/10/2003

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

class net.flash8.ds.ListNode

{

// list node members

public var data:Object;

public var nextNode:ListNode;

// constructor to create ListNode that refers to Object

// and to next ListNode in List

public function ListNode(object:Object,node:ListNode)

{

data = object;

nextNode = node;

}

// return Object in this node

public function getObject():Object

{

return data;

}

// get next node

public function getNext():ListNode

{

return nextNode;

}

}

// end class ListNode

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

// FileName: LinkedList.as

// Description:List class

// Author: AOL

// Last Modified:13/10/2003

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

import net.flash8.ds.ListNode;

import net.flash8.ds.EmptyListError;

class net.flash8.ds.LinkedList

{

private var firstNode:ListNode;

private var lastNode:ListNode;

private var name:String;// String like "list" used in printing

// construct an empty List with a name

public function LinkedList(string:String)

{

if(arguments.length==0)

{

//default name

name="list";

}

else

{

name = string;

}

firstNode = lastNode = null;

}

// Insert Object at front of List. If List is empty,

// firstNode and lastNode will refer to same object.

// Otherwise, firstNode refers to new node.

public function insertAtFront(insertItem:Object):Void

{

if ( isEmpty() )

firstNode = lastNode = new ListNode( insertItem,null );

else

firstNode = new ListNode( insertItem, firstNode );

}

// Insert Object at end of List. If List is empty,

// firstNode and lastNode will refer to same Object.

// Otherwise, lastNode's nextNode refers to new node.

public function insertAtBack(insertItem:Object):Void

{

if ( isEmpty() )

firstNode = lastNode = new ListNode(insertItem,null);

else

lastNode = lastNode.nextNode = new ListNode( insertItem,null);

}

// remove first node from List

public function removeFromFront():Object

{

var removeItem:Object = null;

// throw exception if List is empty

if ( isEmpty() )

throw new EmptyListError(name);

// retrieve data being removed

removeItem = firstNode.data;

// reset the firstNode and lastNode references

if ( firstNode == lastNode )

firstNode = lastNode = null;

else

firstNode = firstNode.nextNode;

// return removed node data

return removeItem;

}

// Remove last node from List

public function removeFromBack():Object

{

var removeItem:Object = null;

// throw exception if List is empty

if ( isEmpty() )

throw new EmptyListError(name);

// retrieve data being removed

removeItem = lastNode.data;

// reset firstNode and lastNode references

if ( firstNode == lastNode )

firstNode = lastNode = null;

else {

// locate new last node

var current:ListNode= firstNode;

// loop while current node does not refer to lastNode

while ( current.nextNode != lastNode )

current = current.nextNode;

// current is new lastNode

lastNode = current;

current.nextNode = null;

}

// return removed node data

return removeItem;

}

// return true if List is empty

public function isEmpty():Boolean

{

return firstNode == null;

}

// output List contents

public function print():Void

{

if ( isEmpty() ) {

trace("Empty "+name);

return;

}

trace( "/=======begin of the " + name + " table ===========\\" );

var current:ListNode = firstNode;

// while not at end of list, output current node's data

while ( current != null ) {

trace( current.data.toString() + " " );

current = current.nextNode;

}

trace( "\==========end of the " + name + " table ===========/\n" );

}

}

// end class LinkedList

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

// FileName: EmptyListError.as

// Description:Error class for while the list is empty.

// Author: AOL

// Last Modified:13/10/2003

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

class net.flash8.ds.EmptyListError extends Error

{

// initialize an EmptyListError

public function EmptyListError(name:String)

{

super("The " + name + " is empty");

}

public function messageTrace():Void

{

trace("An EmptyListError:"+message);

}

}

// end class EmptyListError

//ListTest.fla

import net.flash8.ds.LinkedList;

import net.flash8.ds.EmptyListError;

//===============================================================

var list:LinkedList = new LinkedList();

var bool:Boolean = true;

var number:Number = 10;

var string:String = "hello";

var movieClip:MovieClip = new MovieClip();

list.insertAtFront(bool);

list.print();

list.insertAtFront(string);

list.print();

list.insertAtBack(number);

list.print();

list.insertAtFront(movieClip);

list.print();

//=================================================================

var removedObject:Object;

// remove objects from list; print after each removal

try

{

removedObject = list.removeFromFront();

trace(removedObject.toString()+" removed\n");

list.print();

removedObject = list.removeFromFront();

trace(removedObject.toString()+" removed\n");

list.print();

removedObject = list.removeFromBack();

trace(removedObject.toString()+" removed\n");

list.print();

removedObject = list.removeFromBack();

trace(removedObject.toString()+" removed\n");

list.print();

removedObject = list.removeFromBack();

trace(removedObject.toString()+" removed\n");

list.print();

}

// process exception if List is empty when attempt is

// made to remove an item

catch (emptyListError)

{

emptyListError.messageTrace();

}

output出来的结果:

/=======begin of the list table ===========true

\=========end of the list table ===========/

/=======begin of the list table ===========hello

true

\=========end of the list table ===========/

/=======begin of the list table ===========hello

true

10

\=========end of the list table ===========/

/=======begin of the list table ===========[object Object]

hello

true

10

\=========end of the list table ===========/

[object Object] removed

/=======begin of the list table ===========hello

true

10

\=========end of the list table ===========/

hello removed

/=======begin of the list table ===========true

10

\=========end of the list table ===========/

10 removed

/=======begin of the list table ===========true

\=========end of the list table ===========/

true removed

Empty list

An EmptyListError:The list is empty

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有