LinkedList

From ActionScriptWiki

Jump to: navigation, search

A LinkedList is the fastest way to store and iterate over a set of data elements if the content of the list is static. Inserting and removing elements of a LinkedList can be a painful task. Therefore it is really important to decide whether or not to use such a structure at all. In this snippet the elements are type safe and form a LinkedList on their own. It is also possible to build a box around any object for a LinkedList class. In that case multiple objects could exist in different LinkedList instances.

Contents

[edit] Simple LinkedList Implementation

[edit] The LinkedList Element

package
{
	public final class NumberBox
	{
		public var value: Number = 0.0;
		public var next: NumberBox = null;
	}
}

[edit] Creating A LinkedList

function create( numValues: int ): NumberBox
{
	if( 1 > numValues )
		return null;

	const start: NumberBox = new NumberBox();

	var current: NumberBox = start;

	while( --numValues != 0 )
		current = current.next = new NumberBox();

	return start;
}

[edit] Iterating Over A LinkedList

const list: NumberBox = create( 1000 );
var current: NumberBox = list;

while( current )
{
	trace( current.value );
	current = current.next;
}

[edit] See Also

Personal tools