Data | Structure in the game we often need to store some discrete object data, such as prop box props, often need to perform insert and delete operations, and there is no link between the items are unordered arrangement. Some people would say that just use an array. However, the efficiency of an array deletion insert operation is very low when there is a large amount of data storage.
So we need a hash table that provides a quick insert and delete, a data structure for the lookup operation, no matter how much data is in the Hashtable, the insert and delete operations only need to be close to the constant time: that is, the time level of O (1).
Since it is so good, can our as be realized? Of course! As developed into AS2.0, it has become an object-oriented language that is syntactically closer to Java + Pascal. Now, with the release of Flash8, this scripting language has been added faster, Background technology to connect the advantages of more convenient. So we can use as to implement some of the commonly used data structures.
First we have to implement an orderly linked list, as the name implies, the chain is a chain-like data structure, similar to the array, but the difference is that each node of the linked list will generally include at least one data item and a pointer to the next node, and ordered list of data is arranged in order.
It is recommended that you put all the classes in your package so that you can find and use them later, and I put these classes in a package called Util.
The realization of ordered linked list:
Linked list Node class:
Class util. Link
{
public Var idata; Data item (key)
public Var Ddata;
public var next:util. Link; Next link in list
// ------------------------------------------------------------
Public Function Link (ID,DD)//constructor
{
Idata = ID; (' Next ' is automatically
Ddata = DD;
}//set to NULL)
// ------------------------------------------------------------
Public Function DisplayLink (): Void//Display ourself
{
Trace ("{" + Idata + "," +ddata+ "}");
}
}//End Class Link ordered list class:
Import util. link;//Note To import our Front link Class!
Class util. SortedList
{
private Var First:link; Ref to list item
// ------------------------------------------------------------
Public Function SortedList ()//constructor
{i = null;}
// ------------------------------------------------------------
Public Function Insert (thelink:link): Void//Insert Link, in order
{
var key = Thelink.idata;
var previous:link = null; Start at
var current:link = i;
Until end of list,
while (current!= null && key > Current.idata)
{//or current > key,
previous = current;
current = Current.next; Go to next item
}
if (previous==null)//If beginning of list,
i = Thelink; The--> new link
else//Not at beginning,
Previous.next = Thelink; Prev--> New link
Thelink.next = current; New link--> Current
}//End insert ()
Public function DeleteD (key): Void//Delete link
{//(assumes non-emptylist)
var previous:link = null; Start at
var current:link = i;
Until end of list,
While (the current!= null && key!= current.idata)
{//or key = = Current,
previous = current;
current = Current.next; Go to Next link
}
Disconnect Link
if (previous==null)//If beginning of list
i = First.next; Delete the
else//Not at beginning
Previous.next = Current.next; Delete Currentlink
}//End Delete ()
// ------------------------------------------------------------
Public function Find (key): Link//Find link
{
var current:link = i; Start at
Until end of list,
While (the current!= null && current.idata <= key)
{//or key too small,
if (Current.idata = = key)//Is this the link?
return to current; Found it, return link
current = Current.next; Go to next item
}
return null; Didn ' t find it
}//End Find ()
// ------------------------------------------------------------
Public Function Displaylist (): Void
{
Trace ("List (first-->last):");
var current:link = i; Start at beginning of list
While (the current!= null)//until end of list,
{
Current.displaylink (); Print data
current = Current.next; Move to Next link
}
Trace ("");
}
The implementation of the//End Class SortedList hash table:
Hash Table class:
Import util. link;//Note To import our Front link Class!
Import util. sortedlist;//Note To import our front SortedList Class!
Class util. HashTable
{
private Var Hasharray:array; Array of lists
private Var Arraysize:number;
// ------------------------------------------------------------
Public Function HashTable (size:number)//constructor
{
ArraySize = size;
Hasharray = new Array (arraysize); Create array
for (Var j=0 j<arraysize; j + +)//fill array
HASHARRAY[J] = new SortedList (); With lists
}
// ------------------------------------------------------------
Public Function displaytable (): Void
{
for (Var j=0 j<arraysize; j + +)//For each cell,
{
Trace (j + "."); Display cell number
Hasharray[j].displaylist (); Display list
}
}
// ------------------------------------------------------------
Public function Hashfunc (key): number//hash function
{
Return key% ArraySize;
}
// ------------------------------------------------------------
Public Function Insert (thelink:link): Void//Insert a Link
{
var key = Thelink.idata;
var hashval = Hashfunc (key); Hash the key
Hasharray[hashval].insert (Thelink); Insert at Hashval
}//End insert ()
// ------------------------------------------------------------
Public function DeleteD (key): Void//Delete a link
{
var hashval = Hashfunc (key); Hash the key
Hasharray[hashval].deleted (key); Delete link
}//End Delete ()
// ------------------------------------------------------------
Public function Find (key): Link//Find link
{
var hashval = Hashfunc (key); Hash the key
var thelink:link = Hasharray[hashval].find (key); Get link
return thelink; Return link
}
// ------------------------------------------------------------
}//End Class HashTable hash function Hashfunc (key) We can design, I use the simplest method of modulo operation. The design of hash function determines the efficiency of each operation of hash table.
Use of a hash table:
var itemTable = new HashTable (6);//declares a hash table object and defines a space size of 6;
var item:array = new Array ("Cupper", "Herb", "knife", "tinymedal", "cloth", "milk");
for (var i = 0;i<6;i++) {
var tlink = new link (6*i,item[i])//Declare a linked list node, the first parameter is the key value, the second is the object that you want to deposit, here is the name of the prop;
Itemtable.insert (Tlink);//Insert node into hash table;
}
Itemtable.displaytable ();//Show data in the hash table;
You can try other find and delete operations; All right, here we go. Welcome to the discussion!
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.