链表AS2实现

博主整理了与数据结构相关的内容到Collection Framework,包含ArrayList、LinkedList等。这些内容多源于去年为闪吧写的AS2书的例子,但很多示例和文档已遗失,现找出早期版本,还希望拿到第一手资料的朋友能发回一份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

//****************************************************************************
// 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值