Friday, October 14, 2011

Playing with LinkList (2)

I had introduced the use of LinkList in my previous post. So what can you do with a LinkList? And how can it help you?

Let us take a look at a simple example.
If you are staying in a house with many rooms and each room have a few cupboards or shelves fully occupied with books, you probably will have a list like the following.
Click here for the list.

I have created a simple flex application to sort all the data in a orderly manner.
Buy let us take a look at some parts of the codes first.

On top of the CustomLinkList class that I had introduced in the previous post, I had build a room class on top of the CustomLinkList class.
package List
 public class RoomsList extends CustomLinkList
  public function RoomsList()
  //Override the add Value function in the CustomLinkList class
  override public function addValue(tempValue:*):void{
   var newNode:Node = new RoomNode(tempValue);
   var baseNode:Node = head;
   var tempNode:Node;
   if(baseNode == null){
    head = newNode;
    var hasCheck:Boolean = false;
    while(hasCheck == false){
     //While moving from one node to the next,
     //do a comparison between the nodes
     //and find the best place to place the new node
     if(compareRoomName(baseNode, newNode)){
      hasCheck = true;
      if(baseNode.prev == null){
       head = newNode;
      tempNode = baseNode.prev;
      if(tempNode != null){ = newNode;
      newNode.prev = baseNode.prev; = baseNode;
      baseNode.prev = newNode;
      if( == null){ = newNode;
       newNode.prev = baseNode;
       hasCheck = true;
       baseNode =;
   count ++;
  //Function to compare 2 node and if the new node that was pass into
  //this function had a smaller value, it will return true, else false 
  //We are going to sort the room by their room_name value
  public function compareRoomName(tempNode1:Node, tempNode2:Node):Boolean{
   var name1:String = String(tempNode1.value.room_name).toLowerCase();
   var name2:String = String(tempNode2.value.room_name).toLowerCase();
   var tempArray:Array = new Array();
   if(name2 == tempArray[0]){
    return true;
    return false;
   return false;

And a RoomNode class that builds on top of the Node class.
package List
 public class RoomNode extends Node
  private var shelvesList:ShelvesList = new ShelvesList();
  //After passing in the value of this node for room,
  //Pass in all the relevant data for creating the 
  //respective shelves
  public function RoomNode(tempValue:*):void {
   // constructor code
   for(var i:int = 0; i < this.value.shelfs.shelf.length(); i ++){
  public function get roomContents():ShelvesList{
   return shelvesList;
And this would be my main application class.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" 
   import List.*;
   //Embed a xml file that contents data
   private var dataXml:Class;
   //Create a list for rooms
   private var tempRoomList:RoomsList = new RoomsList();
   protected function creationCompleteHandler(event:FlexEvent):void{
    var strOutput:String = "";
    var tempDataXML:XML = as XML;
    //Pass all the data into the list for rooms
    for(var i:int = 0; i <; i ++){
    var roomObject:RoomNode;
    var shelfObject:ShelfNode;
    //Display all the contents in the list for rooms
    for(i = 0; i < tempRoomList.length(); i ++){
     roomObject = tempRoomList.getNodeAt(i);
     strOutput += "Room Name = " + 
     strOutput += "\n";
     for(var j:int = 0; j < roomObject.roomContents.length(); j ++){
      shelfObject = roomObject.roomContents.getNodeAt(j);
      strOutput += "-> Shelf Name = " + 
      strOutput += "\n";
      for(var k:int = 0; k < shelfObject.shelfContents.length(); k ++){
       strOutput += "   -> Book Title = " + 
       strOutput += ", Written By " + 
       strOutput += "\n";
    txtMessage.text = strOutput;
 <mx:VBox width="100%" height="100%">
  <mx:TextArea id="txtMessage" width="100%" height="100%"/>
* Click here to view the demo in this post.
^ Click here for the source files of the demo
` Click here to follow the other post on LinkList.

No comments:

Post a Comment