Sunday, October 9, 2011

Playing with LinkList

In most of the older Programming language that I had used before or played before requires the developer to enter a value for the number of items that you are going to store in a Array. This can be quite a nuisance sometimes, but luckily there is something known as a Link List in the programming world. :D With the help of a Link List, it had definitely solved a lot of length issues in various Programming Language.

This is a simple example that I had created for Flex but the classes were also usable in Flash Professional Projects.
This is the source file for the main Application - LinkList.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" width="100%" height="100%"
    creationComplete="creationCompleteHandler(event)">
 <mx:Style>
  VBox{
   paddingTop:5;
   paddingBottom:5;
   paddingLeft:5;
   paddingRight:5;
  }
 </mx:Style>
 <mx:Script>
  <![CDATA[
   import List.*;
   
   import mx.events.FlexEvent;
   
   private var tempObject:Object = {x:100,y:100};
   private var dataArray:Array = new Array("Data",12345,3.14,tempObject);
   
   private var tempList:CustomLinkList = new CustomLinkList();
   
   protected function creationCompleteHandler(event:FlexEvent):void
   {
    var strOutput:String = "";
    //Adding values to the List
    for(var i:int = 0; i < dataArray.length; i ++)
    {
     tempList.addValue(dataArray[i]);
    }
    strOutput = "Show Contents of List";
    strOutput += "\nList has " + tempList.length() + " items.";
    strOutput += "\n";
    //Display the objects in the List
    for(i = 0; i < tempList.length(); i ++)
    {
     strOutput += String(tempList.getValueAt(i)) + " ";
    }
    strOutput += "\nRemove Item number 3";
    //Method to remove an object of a particular position
    strOutput += "\n" + String(tempList.removeValueAt(2)) +
     " have been removed.";
    strOutput += "\nShow Contents of List Again";
    strOutput += "\nList has " + tempList.length() + " items.";
    strOutput += "\n";
    //Display the objects in the List
    for(i = 0; i < tempList.length(); i ++)
    {
     strOutput += String(tempList.getValueAt(i)) + " ";
    }
    strOutput += "\nRemove All Items";
    while(tempList.length() != 0)
    {
     strOutput += "\n" + String(tempList.removeValueAt(0)) +
      " have been removed.";
    }
    strOutput += "\nShow Contents of List Again";
    strOutput += "\nList has " + tempList.length() + " items.";
    strOutput += "\n";
    for(i = 0; i < tempList.length(); i ++)
    {
     strOutput += String(tempList.getValueAt(i)) + " ";
    }
    txtMessage.text = strOutput;
   }
   
  ]]>
 </mx:Script>
 <mx:VBox width="100%" height="100%">
  <mx:TextArea id="txtMessage" width="100%" height="100%"/>
 </mx:VBox> 
</mx:Application>

This is a basic Link List class that I had written
package List{
 public class CustomLinkList {
  private var _head:Node;
  private var _count:int;
  public function CustomLinkList() {
   // constructor code
   head = null;
   count = 0;
  }
  
  //count = number of items Getters and Setters
  protected function get count():int
  {
   return _count;
  }

  protected function set count(value:int):void
  {
   _count = value;
  }

  //Start of the Node
  protected function get head():Node
  {
   return _head;
  }

  protected function set head(value:Node):void
  {
   _head = value;
  }

  //Creating and adding a new Node
  //And connect the 2 way Link List properly
  public function addValue(tempValue:*):void{
   var newNode:Node = new Node(tempValue);
   var currNode:Node = head;
   if(currNode == null){
    head = newNode;
   }else{
    while(currNode.next != null){
     currNode = currNode.next;
    }
    currNode.next = newNode;
    newNode.prev = currNode;
   }
   count ++;
  }
  
  //Removing a node and returning 
  //the value of the Node at the same time
  //And connect the 2 way Link List properly
  public function removeValueAt(tempInt:int):*
  {
   var i:int = 0;
   var currNode:Node = head;
   if(tempInt < count)
   {
    var nextNode:Node;
    var prevNode:Node;
    while(i != tempInt){
     currNode = currNode.next;
     i ++;
    }
    if(currNode)
    {
     nextNode = currNode.next;
     prevNode = currNode.prev;
     if(nextNode)
      nextNode.prev = prevNode;
     if(prevNode)
      prevNode.next = nextNode;
     if(currNode == head)
     {
      head = nextNode;
     }
    }
    if(count > 0)
     count --;
    return currNode.value;
   }else{
    return null;
   }
  }
  
  //Get the value of a Node of a particular position
  public function getValueAt(tempInt:int):*{
   var i:int = 0;
   var currNode:Node = head;
   if(tempInt < count)
   {
    while(i != tempInt){
     currNode = currNode.next;
     i ++;
    }
    return currNode.value;
   }else{
    return null;
   }
  }
  
  //Returns the length of the LinkList
  public function length():int{
   return count;
  }
 }
}
This is a simple Node class
package List{
 public class Node {
  protected var _value:* = null;
  protected var _next:Node;
  protected var _prev:Node;
  public function Node(tempValue:*) {
   // constructor code
   _value = tempValue;
   _next = null;
   _prev = null;
  }
  
  //Previous Node Getters and Setters
  public function get prev():Node
  {
   return _prev;
  }

  public function set prev(value:Node):void
  {
   _prev = value;
  }
  
  //Next Node Getters and Setters
  public function get next():Node
  {
   return _next;
  }

  public function set next(value:Node):void
  {
   _next = value;
  }
  
  //Previous Content of the Node
  public function set value(tempValue:*):void{
   _value = tempValue;
  }
  
  public function get value():*{
   return _value;
  }
 }
}
* Click here to view the demo in this post. ^ Click here for the source files of the demo

No comments:

Post a Comment