Implementation of linked list as2

Source: Internet
Author: User

Most of the content related to the data structure is an example I wrote in as2 for flash last year. Unfortunately, I have lost many examples and documents in this book. This is an example of an earlier version. Later, I sorted out the collection framework related to the data structure, including the commonly used arraylist, sorted list, tree, hashtable and so on. I hope a friend who obtained my first-hand information can send me a copy. Thank you.

//****************************************************************************// 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.flaimport 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 removaltry{        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 itemcatch (emptyListError){                emptyListError.messageTrace();}
Output result:
/======= 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.