这些与数据结构相关的大部分内容,都是我去年在为闪吧写的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