1. 程式人生 > 實用技巧 >資料結構與演算法JavaScript描述 第四章課後習題

資料結構與演算法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);
}