Code Monkey

于工开始移山

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/* queue FIFO

        */
        (function () {
            var queue = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
                iHead = 0, iTail = queue.length,
                result = [];

            while (iHead !== iTail) {
                result.push(queue[iHead]);
                queue[iHead] = null;
                iHead++;
                //if (iHead === iTail) {
                //    break;
                //}
                queue[iTail] = (queue[iHead]);
                iHead++;
                iTail++;
            }
            console.log(result.join(", "));

        }());
/* stack LIFO

        */
        (function () {
            var stack1 = [], stack2 = [], top1 = -1, top2 = -1,
                hash = { "{": "}", "[": "]", "(": ")" }, result = [], vl, vr;

            stack1 = ["{", "[", "(", ")", "(", ")", "]", "[", "(", ")", "]", "(", ")", "}"];
            top1 = stack1.length - 1;
            while (top1 >= 0) {
                vl = stack1[top1--];
                switch (vl) {
                    case "{":
                    case "[":
                    case "(":
                        vr = top2 >= 0 ? stack2[top2--] : "undefined";
                        if (hash[vl] !== vr) {
                            console.log("Error: " + vl + " !== " + vr);
                            return;
                        }
                        else {
                            result.push([vl, vr]);
                        }
                        break;
                    case "}":
                    case "]":
                    case ")":
                        stack2[++top2] = vl;
                        break;
                }
            }
            if (top2 >= 0) {
                console.log("Error: Don't find out the same sign: " + stack2.slice(0, top2 + 1).join(", "));
            } else {
                console.log(result.join(", "));
            }
        }());
/* chain table
           数组模拟链表:两个数组,一个数组存相应值,一个数组存相对应索引处值得下一个值得索引,即索引表
        */
        (function () {
            var Node = function (value, nextNode) {
                this.value = value;
                this.nextNode = nextNode;
            };

            var chain = new Node(1, new Node(3, new Node(5, new Node(7, new Node(9, null))))),
                fnInsert = function (value, node) {
                    if (!node.nextNode || node.nextNode.value >= value) {
                        node.nextNode = new Node(value, node.nextNode);
                        return;
                    }
                    else {
                        fnInsert(value, node.nextNode);
                    }
                },
                fnPrint = function (node) {
                    return [node.value].concat(!!node.nextNode ? fnPrint(node.nextNode) : []);
                };

            //insert
            console.log(fnPrint(chain));
            var insertValue = 6;
            fnInsert(6, chain);

            console.log(fnPrint(chain));
        }());

 

posted on 2015-02-02 15:45  MSchina  阅读(135)  评论(0编辑  收藏  举报