软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
链表AS2实现

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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



相关文章

相关软件