Link list AS2 implementation

xiaoxiao2021-03-06  77

Most of these content related to the data structure are examples I wrote in the AS2 book written by Flash last year. Unfortunately, many examples and documents of this book have been lost. This is an example to find an earlier version. Later I organize these related to data structures, including Collection Framework, including common ArrayList, LinkedList, Tree, HashTable, etc. I hope that friends who get my first-hand information will send back to me, thank you first.

Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}

. galan {color: # 000000;

. readyword {color: # 993300;

.IDENTIFIER {color: # 000087;

.properties {color: # 000087;

{Color: # 000087;

.LINECMMMENT, .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 what 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 RemovedremoveItem = LastNode.Data;

// reset firstnode and lastnode references

IF (firstnode == lastnode)

FirstNode = lastnode = null;

Else {

// Locate New Last Node

Var currentnode: 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 currentnode: listNode = firstNode;

// While Not At End of List, Output Current Node's Data

While (current! = null) {

Trarent.Data.toString () "");

Current = CURRENT.NEXTNODE;

}

Trace ("/ ==========) " 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 message (): 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: moviClip = 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 outcome: / ======= Begin 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] Removed

/ ======= begin of the list table =========== / hello true 10 / ========= end of the list table ======= ==== / Hello Removed

/ ======= Begin of the list table ======================== end of the list table ======== === /

10 Removed

/ ======= Begin of the list table =========== / true / ========= end of the list table ========= == /

True Removed

EMPTY LISTAN EMPTYLISTERROR: THE LIST IS EMPTY

转载请注明原文地址:https://www.9cbs.com/read-107488.html

New Post(0)