foros de software libre :: chat #softwarelibre :: socios :: siguiente web/blog»

13th Sep 2007

Pila en javascript

JAVASCRIPT:
  1. //creando la clase pila
  2.       var Stack = function(size){
  3.           this.Size=size;
  4.           this.Stacker = new Array(size);
  5.           this.pointer=0;
  6.  
  7.           //funciones
  8.  
  9.           //inserta a la pila
  10.           this.Push = function(cadena){
  11.               if(this.IsFull()==false){
  12.                   this.Stacker[this.pointer]=cadena;
  13.                   this.pointer++;
  14.                   return true;
  15.               }
  16.               else{
  17.                   return false;
  18.               }
  19.           }
  20.  
  21.  
  22.           //saca de la pila
  23.           this.Pop = function(){
  24.               if(this.IsEmpty!=true){
  25.                   this.pointer--;
  26.                   return this.Stacker[this.pointer];
  27.               }
  28.               else{
  29.                   return false;
  30.               }
  31.           }
  32.  
  33.           //esta vacia?
  34.           this.IsEmpty = function(){
  35.               if(this.pointer==0) return true;
  36.               else return false;
  37.           }
  38.  
  39.           //esta llena?
  40.           this.IsFull = function(){
  41.               if(this.pointer==this.Size) return true;
  42.               else return false;
  43.           }
  44.       }

2 Responses to “Pila en javascript”

  1. lesthack Says:

    Gracias, pero, pues, si el Array ya contiene un push y actúa como pila, entonces, ahorraríamos mas código. Esta era solo una manera de explicar los multi-usus que tiene javascript.

  2. jose Says:

    muy bien pero seria mejor usar prototype para reducir codigo

Leave a Reply