堆栈可以用链表和数组两种方式实现,这里分别给出这两种实现方式。
//数组实现 function Stack(){    this.items = [];    this.size = 0; } Stack.prototype = {    constructor:Stack,    push:function(data){        this.items[this.size++] = data;    },    pop:function(){        return this.items[--this.size];    },    clear:function(){        this.size = 0;        this.items = [];    },    perk:function(){        return this.items[this.size-1];    } } 
//链表实现     function Stack(){         this.top = null;         this.size = 0;     }     Stack.prototype = {         constructor:Stack,         push:function(data){             var node = {                 data:data,                 next:null             };             node.next = this.top;             this.top = node;             this.size++;         },         pop:function(){             if(this.top === null){                 return null;             }             var out = this.top;             this.top = this.top.next;             if(this.size > 0){                 this.size--;                 }             return out.data;         },         perk:function(){             return this.top === null ? null:this.top.data;          },         clear:function(){             this.top = null;             this.size = 0;         } 
测试:
var stack = new Stack(); stack.push('k'); stack.push('b'); console.log(stack.perk());//输出b stack.pop(); console.log(stack.perk());//输出k 
栈的应用 例子:数值进制转换
具体代码实现:
function mulBase(num,base){     var stack = new Stack();     do{         stack.push(num % base);          num = Math.floor(num / base);     }while(num>0);     var result = '';     while(stack.size > 0){         result += stack.pop();     }     return result; } console.log(mulBase(234,2)); //输出11101010 
初学者学习笔记,如有不对,还希望高手指点。如有造成误解,还希望多多谅解。