Creation of new data types in AS3: Linked List

In computer programming, a data type (or datatype) is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, stating the possible values for that type, the operations that can be done on that type, and the way the values of that type are stored. (source: Wikipedia).

Obviously any language is capable to create new data structures. I did not find any complete AS3 complete tutorial to create a new data type from scratch with AS3, so I am writing it by myself.

We are going to define a simple linked list data type.

A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

(source and image: Wikipedia. Yes, I made a donation so I feel free to copy/paste :)).

In this example, we are going to reproduce the same linked list you see in the picture. Usually the “records” Wikipedia is talking about are called “nodes”, so we’ll define a node datatype.

This is the class, in a file called node.as:

package triqui{
	public class node {
		public var node_data:*;
		public var next_node:node;
		public function node(node_content:*) {
			node_data=node_content;
			next_node=null;
		}
		public function get_node_data():* {
			return node_data;
		}
		public function insert_prev(n:node):void {
			n.next_node=this;
		}
		public function insert_next(n:node):void {
			next_node=n;
		}
		public function get_next_node():node {
			return next_node;
		}
	}
}

Line 1: package definition. Notice there is a name after the package to preserve class name uniqueness. Whithout boring you with useless theory, just remember that since package name is triqui, you will need to save it in a triqui folder in the same path of you main .fla file. Refer to the downloadable source code available at the end of the post for a concrete example.

Line 2: definition of the class. It’s called node.

Line 3: node_data is the container of the information we need to store. Referring to Wikipedia example, the numbers 12, 99 and 37. Giving * as data type makes it capable of having any data type, such as strings, arrays, integers or even other nodes.

Line 4: next_node is the link to the next node in the list and obviously is a node type.

Line 5: the main function, or constructor. It wants the content of the node (12, 99 or37 in the example) as argument. Notice the type is * because we said it will accept any kind of data types.

Line 6: assigning node_content (the constructor argument) value to node_data (the information container of the node).

Line 7: assigning null to next_node. The node will not link any other node.

Line 9: function to retrieve the information stored in a node (notice the *: you should know why).

Line 10: simply returning the content of node_data variable.

Line 12: function to insert the previous node to a node, that is the node in the argument will link the current node.

Line 13: assigning the current node to next_node variable of the node passed in the argument.

Line 15: function to insert the next node to a node, that is the node in the argument will be linked by the current node.

Line 16: assigning the node passed as argument to next_node variable

Line 18: function to get the next node of a node. Returns (obviously) a node.

Line 19: returning next_node content

And this is an example of the class at work… in a file called datatype.as:

package {
	import flash.display.Sprite;
	import triqui.node;
	public class datatype extends Sprite {
		public function datatype() {
			var n1:node=new node(12);
			var n2:node=new node(99);
			var n3:node=new node(37);
			n2.insert_prev(n1);
			n2.insert_next(n3);
			trace(n1.get_next_node().get_next_node().get_node_data());
		}
	}
}

Line 3: importing node library

Lines 6-8: creating the three nodes as in the example

Line 9: setting n1 node as the previous of n2

Line 10: setting n3 node as the next of n2

Line 11: This will output the content of n3 node starting from n137.

Download the source code.

During next days we’ll see various more complex data types such as double linked lists and trees.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers