这些与数据结构相关的大部分内容,都是我去年在为闪吧写的AS2书中所写的例子。不幸的是,这本书的很多示例和文档我都已经遗失。这是示例是找出的早期版本。后面我把这些与数据结构相关的内容整理了Collection Framework,包括常用的ArrayList,LinkedList,Tree,HashTable等。希望当时拿到我第一手资料的朋友能发回我一份,谢谢先。
class net.flash8.ds.ListNode
{
public var data:Object;
public var nextNode:ListNode;
public function ListNode(object:Object,node:ListNode)
{
data = object;
nextNode = node;
}
public function getObject():Object
{
return data;
}
public function getNext():ListNode
{
return nextNode;
}
}
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;
public function LinkedList(string:String)
{
if(arguments.length==0)
{
name="list";
}
else
{
name = string;
}
firstNode = lastNode = null;
}
public function insertAtFront(insertItem:Object):Void
{
if ( isEmpty() )
firstNode = lastNode = new ListNode( insertItem,null );
else
firstNode = new ListNode( insertItem, firstNode );
}
public function insertAtBack(insertItem:Object):Void
{
if ( isEmpty() )
firstNode = lastNode = new ListNode(insertItem,null);
else
lastNode = lastNode.nextNode = new ListNode( insertItem,null);
}
public function removeFromFront():Object
{
var removeItem:Object = null;
if ( isEmpty() )
throw new EmptyListError(name);
removeItem = firstNode.data;
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removeItem;
}
public function removeFromBack():Object
{
var removeItem:Object = null;
if ( isEmpty() )
throw new EmptyListError(name);
removeItem = lastNode.data;
if ( firstNode == lastNode )
firstNode = lastNode = null;
else {
var current:ListNode= firstNode;
while ( current.nextNode != lastNode )
current = current.nextNode;
lastNode = current;
current.nextNode = null;
}
return removeItem;
}
public function isEmpty():Boolean
{
return firstNode == null;
}
public function print():Void
{
if ( isEmpty() ) {
trace("Empty "+name);
return;
}
trace( "/=======begin of the " + name + " table ===========\\" );
var current:ListNode = firstNode;
while ( current != null ) {
trace( current.data.toString() + " " );
current = current.nextNode;
}
trace( "\==========end of the " + name + " table ===========/\n" );
}
}
class net.flash8.ds.EmptyListError extends Error
{
public function EmptyListError(name:String)
{
super("The " + name + " is empty");
}
public function messageTrace():Void
{
trace("An EmptyListError:"+message);
}
}
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;
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();
}
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

|