Pages

Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Thursday, December 30, 2010

List In Javascript

/**
 * A List of ADT. This ADT works well like list in other library.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * Constructor service
 */
function List(){
    this.data = new Array();
}

/**
 * method to add an element to list
 *
 * @param item string,number,boolean value. element to be added
 */
List.prototype.add = function(item){
    var len = this.data.length;
    this.data[len] = item;
};

/**
 * method to remove an element from the list
 *
 * @param item string, number or boolean as what the list contains.
 */
List.prototype.remove = function(item){
    var newData = new Array();
    var len = this.data.length;
    var k = 0;
    for(i=0;i<len;i++){
        if(this.data[i]!=item){
            newData[k] = this.data[i];
            k++;
        }
    }
    this.data = newData;
};


/**
 * method to know how many elements in the list
 *
 * @return number number of elements
 */
List.prototype.size = function(){
    return this.data.length;
};

/**
 * method to give this list as an array
 *
 * @return an array
 */
List.prototype.toArray = function(){
    return this.data;
};
      

Queue In Javascript

/**
 * A Queue ADT. This ADT provide functionality for FIFO 
 * data structure.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Queue(){
    this.data = new Array();
}

/**
 * method to insert an element to the Queue
 *
 * @param item string, number, boolean element to be inserted.
 */
Queue.prototype.push = function(item){
    var len = this.data.length;
    var newData = new Array();
    newData[0] = item;
    for(i=0;i<len;i++){
        newData[i+1]=this.data[i];
    }
    this.data = newData;
};

/**
 * A method to remove the first item inserted.
 *
 * @return first element of the Queue.
 */
Queue.prototype.pop = function(){
    var len = this.data.length;
    var first = this.data[len-1];
    var newData = new Array();
    for(i=1;i<len;i++){
        newData[i-1]=this.data[i];
    }
    this.data = newData;
    return first;
};

/**
 * A method to get how many elements in the Queue
 *
 * @return number number of elements.
 */
Queue.prototype.size = function(){
    return this.data.length;
};

/**
 * A method to represent this queue as an array data structure.
 *
 * @return array of data
 */
Queue.prototype.toArray = function(){
    return this.data;
}

Stack In Javascript

/**
 * An ADT of Stack. This is an implementation of LIFO 
 * data structure.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Stack(){
    this.data = new Array();
}

/**
 * A method to insert an element to the top position.
 *
 * @param item string, number or boolean value to be inserted
 */
Stack.prototype.push = function(item){
    var len = this.data.length;
    this.data[len]=item;
};

/**
 * A method to get the top position from the stack. As well as
 * to remove it.
 *
 * @return the value of the top position.
 */
Stack.prototype.pop = function(){
    var len = this.data.length;
    var last = this.data[len-1];
    var newStack = new Array();
    for(i=0;i<len-1;i++){
        newStack[i]=this.data[i];
    }
    this.data = newStack;
    return last;
};

/**
 * A method to get the size of elements in the Stack.
 *
 * @return number number of elements in Stack
 */
Stack.prototype.size = function(){
     return this.data.length;
};

/**
 * A method to convert the stack to be an array representation.
 *
 * @return an array of data.
 */
Stack.prototype.toArray = function(){
     return this.data;
}
         

Map In Javascript

/**
 * An ADT of Map. This ADT will do functionality as 
 * the key value ADT.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Map(){
    this.key = new Array();
    this.value = new Array();
}

/**
 * A method to add an elements to the Map
 *
 * @param k string, number key element
 * @param v string, number, boolean value element
 */
Map.prototype.add = function(k,v){
    var len = this.key.length;
    this.key[len] = k;
    this.value[len] = v;
};

/**
 * A method to get an element as suitable for the key
 *
 * @param k key of the element
 * @return value as suitable for the key.
 */
Map.prototype.get = function(k){
    var index = 0;
    for(i=0;i<k.length;i++){
        if(this.key[i]==k)
        index = i;
    }
    return this.value[index];
};
     

Thursday, December 23, 2010

Simple Stack Data Structure

/**
 * Simple List
 * @author : irfanudin ridho
 * @email  : irfan.ub@gmail.com
 * @date   : December 24, 2010
 * @version: 1.0
 *
 * This class provide a stack equivalent data structure
 */


function SimpleStack(){
    this.data = new Array();
}


/*
 * @param string,number,boolean data item in data structure
 */
SimpleStack.prototype.push = function(data){
    var len = this.data.length;
    this.data[len] = data;
}


/*
 * @return string,number,boolean last item
 */
SimpleStack.prototype.pop = function(){
    var len = this.data.length;
    return this.data[len-1];
}