《數(shù)據(jù)結(jié)構(gòu)與算法Javascript描述》第四章 棧 習(xí)題答案

  function Stack() {
            this.dataSource = [];
            this.top = 0;
            this.push = push;
            this.pop = pop;
            this.peek = peek;
            this.length = length;
            this.clear = clear
        }

        function push(element) {
            this.dataSource[this.top++] = element;
        }

        function pop(element) {
            return this.dataSource[--this.top]
        }

        function peek() {
            return this.dataSource[this.top - 1]
        }


        function clear() {
            this.top = 0;
        }

        function length() {
            return this.top
        }
  1. ??梢杂脕砼袛嘁粋€(gè)算術(shù)表達(dá)式中的括號(hào)是否匹配。編寫一個(gè)函數(shù),該函數(shù)接受一個(gè)算術(shù)表達(dá)式作為參數(shù),返回括號(hào)缺失的位置。下面是一個(gè)括號(hào)不匹配的算術(shù)表達(dá)式的例子:2.3 + 23 / 12 + (3.14159×0.24。
 function panduan(shizi) {
            var s = new Stack();
            var left = ['(', '{', '['];
            var right = [')', '}', ']'];
            for (let i = 0; i < shizi.length; i++) {
                if (left.indexOf(shizi[i]) > -1) {
                    s.push(shizi[i])
                } else if (right.indexOf(shizi[i]) > -1) {
                    var c = s.peek();
                    switch (shizi[i]) {
                        case '}':
                            if (c == '{') {
                                s.pop();
                                break;
                            }
                            return i + 1;
                        case ']':
                            if (c == '[') {
                                s.pop();
                                break;
                            }
                            return i + 1;
                        case ')':
                            if (c == '(') {
                                s.pop();
                                break;
                            }
                            return i + 1;

                        default: break;
                    }
                }
            }
            if (s.length() > 0) {
                return shizi.length+ 1;
            }
            return -1
        }
        console.log(panduan('2+3/1+(3*2')) //11
        console.log(panduan('2+(3/1)+(3*2')) //13

2.使用兩個(gè)棧,一個(gè)用來存儲(chǔ)操作數(shù),一個(gè)用來存儲(chǔ)操作符,設(shè)計(jì)并實(shí)現(xiàn)一個(gè)js函數(shù),該函數(shù)可以將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式,然后利用棧對(duì)該表達(dá)式求值。


        function infixToPostfix(exp) {
            var operatorStack = new Stack();
            var postfixExp = [];

            exp.split('').forEach(function (char) {

                if (isOperator(char)) {
                    // 比較優(yōu)先級(jí)
                    while (comparePriority(operatorStack.peek(), char)) {
                        var tmp = operatorStack.pop();
                        if (tmp !== '(' && tmp !== ')') {
                            postfixExp.push(tmp);
                        }
                        if (tmp === '(' && char === ')') { // 匹配到左括號(hào)的時(shí)候,結(jié)束循環(huán)。
                            break;
                        }
                    }
                    if (char !== ')') {
                        operatorStack.push(char);
                    }
                } else {
                    postfixExp.push(char);
                }
            });
            while (operatorStack.length()) {
                postfixExp.push(operatorStack.pop());
            }
            return postfixExp.join('');
        }

        function computePostfix(exp) {
            var numStack = new Stack();
            exp.split('').forEach(function (char) {
                if (char.trim()) {
                    if (!isOperator(char)) {
                        numStack.push(char);
                    } else {
                        var tmp = numStack.pop();
                        numStack.push(eval(numStack.pop() + char + tmp));
                    }
                }
            });
            return numStack.pop();
        }

        var postfixExp = infixToPostfix('(8-2)/(3-1)*(9-6)');
        var postfixExpValue = computePostfix(postfixExp);

        console.log(postfixExp); // 82-31-/96-*
        console.log(postfixExpValue); // 9
  1. 佩茲糖果盒,不改變其他顏色糖果將黃色糖果移出
// 1:紅 2:黃 3:白
var s = [1, 1, 1, 1, 3, 3, 2, 2, 2, 2, 1, 1, 3, 3, 1, 2, 3, 1, 3, 2]
        function yichusugger(arr) {
            var s = new Stack();
            for (let i = 0; i < arr.length; i++) {
                s.push(arr[i])
            }
            var newArr = [];
            while (s.length() > 0) {
                var color = s.pop();
                if (color != 2) {
                    newArr.push(color)
                }
            }
            return newArr
        }
        console.log(yichusugger(s))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容