JavaScript中栈
function Stack() {
this.items = [];
Stack.prototype.push = function(element) {
this.items.push(element);
}
Stack.prototype.pop = function() {
return this.items.pop();
}
Stack.prototype.peek = function() {
return this.items[this.items.length - 1];
}
Stack.prototype.isEmpty = function() {
return this.items.length == 0;
}
Stack.prototype.size = function(){
return this.items.length;
}
Stack.prototype.toString = function() {
var result = "";
for(var i =0;i<this.items.length;i++)
{
result+=this.items[i]+" "
}
return result;
}
}
// 十进制转换成二进制
function dec2bin(decNumber){
var stack = new Stack();
while(decNumber > 0){
stack.push(decNumber % 2); // 获取余数
decNumber = Math.floor(decNumber/2); // 向下去整
}
var binaryString = "";
while(!stack.isEmpty()){
binaryString += stack.pop();
}
return binaryString;
}

浙公网安备 33010602011771号