zoukankan      html  css  js  c++  java
  • 算法导论(第三版)练习 10.1-1 ~ 10.1-7

    栈与队列js实现版本:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <script type="text/javascript">
                let log = console.log.bind(console);
                let x = [1, 2, 3];
                x.isEmpty = function() {
                    if (this.length == 0) {
                        return true;
                    } else {
                        return false;
                    }
                };
                x.push(4);
                log("isEmpty", x.isEmpty()); // => isEmpty false
                log(x); // => [1, 2, 3, 4]
                log(x.pop()); // => 4
                log(x.pop()); // => 3
                log(x.pop()); // => 2
                log(x.pop()); // => 1
                log(x.pop(), "??"); // => undefined ??
                log("isEmpty", x.isEmpty()); // => isEmpty true
            </script>
            
            <script type="text/javascript">
                // 利用数组Q[0..n](有效下标是0~n-1)实现一个最多容纳n-1个元素的队列
                
                let queue = [];
                queue.size = 12;
                queue.tail = queue.head = 0;
                function enqueue(q, x) {
                    let temp = (q.tail == q.size - 1) ? 0 : q.tail + 1;
                    if (temp == q.head) { // 有效下标是12个的数组,只能存11个数
                        throw new Error("overflow");
                    } else {
                        q[q.tail] = x;
                        q.tail = temp;
                    }
                }
                
                function dequeue(q) {
                    if (q.head == q.tail) {
                        throw new Error("underflow");
                    } else {
                        let x = q[q.head];
                        if (q.head == q.size - 1) {
                            q.head = 1;
                        } else {
                            ++q.head;
                        }
                        return x;
                    }            
                }
                
                log(queue);
                for (let i = 0; i != queue.size - 1; ++i) {
                    enqueue(queue, i);
                }
                log(queue);
                // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, size: 12, head: 0, tail: 11]
                for (let i = 0; i != queue.size - 1; ++i) {
                    log(dequeue(queue));
                }
                dequeue(queue); // => Uncaught Error: underflow
            </script>
        </body>
    </html>
    View Code

    10.1-1

    10.1-2

    初始化:两个指针分别指向0和x.length-1

    push操作:首先检查两个是否相等,相等就抛出overflow,不相等就赋值加1或者减1

    10.1-3

    10.1-4

    10.1-5

    调个api。。

            <script type="text/javascript">
                let arr = [1, 2, 3, 4];
                arr.push_back = function(x) {
                    this.push(x);
                }
                arr.push_front = function(x) {
                    this.unshift(x)
                }
                arr.pop_back = function() {
                    return this.pop();
                }
                arr.pop_front = function() {
                    return this.shift();
                }
                log(arr); // => [1, 2, 3, 4]
                arr.push_back(5);
                arr.push_front(0);
                log(arr); // => [0, 1, 2, 3, 4, 5]
                arr.pop_back();
                arr.pop_front();
                log(arr); // => [1, 2, 3, 4]
            </script>

    10.1-6 & 10.1-7

    两个栈实现队列 两个队列实现栈

  • 相关阅读:
    Code Forces Gym 100886J Sockets(二分)
    CSU 1092 Barricade
    CodeChef Mahesh and his lost array
    CodeChef Gcd Queries
    CodeChef GCD2
    CodeChef Sereja and LCM(矩阵快速幂)
    CodeChef Sereja and GCD
    CodeChef Little Elephant and Balance
    CodeChef Count Substrings
    hdu 4001 To Miss Our Children Time( sort + DP )
  • 原文地址:https://www.cnblogs.com/xkxf/p/9972896.html
Copyright © 2011-2022 走看看