javasript_数据结构和算法_栈

 //-----------------------------------存储结构为数组--------------------------------------------
function Stack(){
this.store = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.display = display;
this.length = length; this.binary_10 = binary_10;
} function push(ele){
this.store[this.top] = ele;
this.top++; //top指向下一个元素
}
function pop(){
return this.store[--this.top];
}
function peek(){
return this.store[this.top-1];
}
function length(){
return this.top;
}
function display(){
for(var i=this.length()-1; i>-1; i--){
console.log(this.store[i]);
}
}
function binary_10(){
var sum = 0;
var j = 0;
for(var i=this.top-1; i>-1; i--){
sum += this.store[i] * Math.pow(2,j);
j++;
}
return sum;
}
var stack = new Stack();
var num = '11001001';
for(var i=0; i<num.length; i++){
stack.push(num[i]);
}
stack.display();
console.log(stack.binary_10());
上一篇:DRF(2) - 解析器,序列化组件使用(GET/POST接口设计)


下一篇:php笔记01:php基本语法格式