数据结构与算法JavaScript描述 第四章课后习题
1.栈可以用来判断一个算术表达式中的括号是否匹配。编写一个函数,该函数接受一个算术表达式作为参数,返回括号缺失的位置。下面是一个不匹配的算术表达式的例子:2.3+23/12+(3.14159*0.24。
// 栈的基本特性实现
function Stack(){ this.dataStore = []; this.top = 0; this.push = push; this.pop = pop; this.peek = peek; this.clear = clear; this.length = length; } function push(element){ this.dataStore[this.top++] = element; } function peek(){ return this.dataStore[this.top - 1]; } function pop(){ return this.dataStore[--this.top]; } function clear(){ this.top = 0; } function length(){ return this.top; } // 检查括号匹配 function checkBracket(expression){ var s = new Stack(); for (var i = 0; i < expression.length; ++i){ if (expression[i] == "("){ s.push(i+1); } else if(expression[i] == ")"){ s.pop(); } } if (s.length() > 0){ return s.pop(); } else { return -1; } } var ans = checkBracket("2.3+((((23/12+(3.14)159*0.24"); print("num:" + ans);
2.一个算术表达式的后缀表达式形式如下:
op1 op2 operator
使用两个栈,一个用来存储操作数,另一个用来存储操作符,设计并实现一个JavaScript函数,该函数可以将中缀表达式转换为后缀表达式,然后利用栈对该表达式求值。
function Stack(){ this.dataStore = []; this.top = 0; this.push = push; this.pop = pop; this.peek = peek; this.clear = clear; this.length = length; } function push(element){ this.dataStore[this.top++] = element; } function peek(){ return this.dataStore[this.top - 1]; } function pop(){ return this.dataStore[--this.top]; } function clear(){ this.top = 0; } function length(){ return this.top; } // 计算输入表达式 function cal(expression){ var nums = new Stack(); var opreators = new Stack(); var exp = expression.split(" "); for (var i = 0; i < exp.length ; ++i){ if (i < exp.length - 1){ nums.push(exp[i]); } else{ opreators.push(exp[i]); } } var num1 = nums.pop(); // print(num1); var num2 = nums.pop(); // print(num2); var op = opreators.pop(); // print(op); var res = 0; switch (op){ case "+": res = parseFloat(num1) + parseFloat(num2); break; case "-": res = parseFloat(num1) - parseFloat(num2); break; case "*": res = parseFloat(num1) * parseFloat(num2); break; case "/": res = parseFloat(num1) / parseFloat(num2); break; } return { expStr:num1 + op + num2 + "=" + res.toString(), res: res }; } var expression = "12 15 +"; print(cal(expression).expStr);
3.现实生活中栈的一个例子是佩兹糖果盒。想象一下你有一盒佩兹糖果,里面塞满了红色、黄色和白色的糖果盒,但你不喜欢黄色的糖果。使用栈(优肯用到多个栈)写一段程序,在不改变盒内其他糖果叠放顺序的基础上,将黄色糖果移出。
function Stack(){ this.dataStore = []; this.top = 0; this.push = push; this.pop = pop; this.peek = peek; this.clear = clear; this.length = length; } function push(element){ this.dataStore[this.top++] = element; } function peek(){ return this.dataStore[this.top - 1]; } function pop(){ return this.dataStore[--this.top]; } function clear(){ this.top = 0; } function length(){ return this.top; } var candies = new Stack(); candies.push("red"); candies.push("red1"); candies.push("yellow"); candies.push("yellow"); candies.push("blue"); candies.push("red"); candies.push("yellow"); candies.push("red"); var temp = new Stack(); while(candies.length()>0){ var candle = candies.pop(); if (candle != "yellow"){ temp.push(candle); } } while(temp.length()>0){ var candle = temp.pop(); candies.push(candle); } while(candies.length()>0){ var candle = candies.pop(); print(candle); }

浙公网安备 33010602011771号