The two-way linked list in the data structure is written as a class in AS3, which is very useful. Some non-program flasher may not understand the data structure. If you think of arrays as queues of data, the linked list queues hand in hand, but each person only knows himself and his/her front and back. If you use the idea of classes, you can understand the following: objects with three data attributes: 1. Oneself; 2. People behind oneself; 3. People before oneself; and methods for operating these attributes. We hope this structure will be useful to you in the frequent deletion and insertion of data, which will be much more effective than the array.
Package {public class DoubleNode {
// Two-Way linked list, double list node
Private var _ data :*;
Private var _ prev: DoubleNode;
Private var _ next: DoubleNode;
// Indicates whether it is already in the linked list
Private var _ hasIN: Boolean = false;
// @ InitData: initial value of the node, the node 'data;
Public function DoubleNode (initData :*){
_ Data = initData;
_ Prev = _ next = null;
}
// This node has in link
Public function get hasIN (): Boolean {
Return _ hasIN;
}
Public function set hasIN (boolean: Boolean): void {
_ HasIN = boolean;
}
// Successor node
Public function get next (): DoubleNode {
Return _ next;
}
// Precursor node
Public function get prev (): DoubleNode {
Return _ prev;
}
// Set the successor
Public function set next (newNode: DoubleNode): void {
_ Next = newNode;
}
// Set the precursor
Public function set prev (newNode: DoubleNode): void {
_ Prev = newNode;
}
// Get the data of the current node, return the node's data
Public function get nodeData ():*{
Return _ data;
}
// Set the current node data, set the node's data
Public function set nodeData (newData: *): void {
_ Data = newData;
}
// Insert the node after the current node and append a new node after current node
Public function addAfter (newNode: DoubleNode): void {
NewNode. next = _ next;
NewNode. prev = this;
If (_ next! = Null ){
_ Next. prev = newNode;
}
_ Next = newNode;
NewNode. hasIN = true;
}
// Insert the node before the current node and append a new node before current node
Public function addBefore (newNode: DoubleNode): void {
NewNode. next = this;
NewNode. prev = _ prev;
If (_ prev! = Null ){
_ Prev. next = newNode;
}
_ Prev = newNode;
NewNode. hasIN = true;
}
// Returns the length of the current array, get the length of the node
Public function get nodeLength (): uint {
Var cursor: DoubleNode;
Var length: uint = 1;
For (cursor = _ prev; cursor! = Null; cursor = cursor. prev ){
Length ++;
}
Return length;
}
// Remove from the two-way linked list. Get out of link to the double-node
Public function unlink (): void {
If (_ prev! = Null ){
_ Prev. next = _ next;
}
If (_ next! = Null ){
_ Next. prev = _ prev;
}
_ Next = _ prev = null;
_ HasIN = false;
}
// Description of the current double-node, rerurn a string represent the double-node
Public function toString (): String {
Return "[DoubleNode, data =" + _ data + "]";
}
}
}