美文网首页
《数据结构与算法Javascript描述》第五章 队列 习题答案

《数据结构与算法Javascript描述》第五章 队列 习题答案

作者: 月半女那 | 来源:发表于2018-12-29 15:12 被阅读0次
       function Queue() {
                this.dataStore = [];
                this.enqueue = enqueue;
                this.dequeue = dequeue;
                this.fornt = front;
                this.back = back;
                this.toString = toString;
                this.empty = empty;
                this.count = count;
            }
    
            function enqueue(element) {
                this.dataStore.push(element);
            }
    
            function dequeue() {
                return this.dataStore.shift();
            }
    
            function front() {
                return this.dataStore[0];
            }
    
            function back() {
                return this.dataStore[this.dataStore.length - 1]
            }
    
            function toString() {
                var reStr = '';
                for (let i = 0; i < this.dataStore.length; i++) {
                    reStr += this.dataStore[i]
                }
    
                return reStr
            }
    
            function empty() {
                if (this.dataStore.length == 0) {
                    return true
                } else {
                    return false
                }
            }
    
            function count() {
                return this.dataStore.length;
            }
    
    1. 修改queue类,形成Deque类,允许队列两端添加和删除元素,也叫双向队列
      2.使Deque类来判定一个给定的单词是否为回文
     function Deque() {
            }
    
            Deque.prototype = new Queue();
    
            // l:left r:right
            Deque.prototype.enqueue = function (element, direction) {
                var _this = this;
                switch (direction) {
                    case 'l':
                        _this.dataStore.unshift(element)
                        break;
                    case 'r':
                        _this.dataStore.push(element)
                        break;
                    default:
                        _this.dataStore.push(element);
                }
            }
    
    
            Deque.prototype.dequeue = function (direction) {
                var _this = this;
                switch (direction) {
                    case 'l':
                        _this.dataStore.shift()
                        break;
                    case 'r':
                        _this.dataStore.pop()
                        break;
                }
            }
    
    
            function test(word) {
                var l = new Deque();
                l.dataStore = [];
                var r = new Deque();
                r.dataStore = [];
                for (let i = 0; i < word.length; i++) {
                    l.enqueue(word[i], 'l')
                    r.enqueue(word[i], 'r')
    
                }
                if (l.toString() == r.toString()) {
                    return true
                } else {
                    return false
                }
            }
            console.log(test('hahaa')) //false
            console.log(test('haah')) //true
    
    
    1. 优先级高的优先码也大
     function Patient(name, code) {
                this.name = name;
                this.code = code;
            }
    
            var PriorQueue = function () { }
            PriorQueue.prototype = new Queue();
            PriorQueue.prototype.dequeue = function () {
                var max = this.dataStore[0].code;
                var index = 0;
                for (let i = 0; i < this.dataStore.length; i++) {
                    if (max < this.dataStore[i].code) {
                        max = this.dataStore[i].code;
                        index = i
                    }
                }
                return this.dataStore.splice(index, 1)
            }
            var priorQueue = new PriorQueue();
            priorQueue.enqueue(new Patient('张三', 10));
            priorQueue.enqueue(new Patient('张三1', 3));
            priorQueue.enqueue(new Patient('张三2', 11));
            priorQueue.enqueue(new Patient('张三3', 23));
            priorQueue.enqueue(new Patient('张三4', 1));
            console.log(priorQueue.dequeue());
    
    
    1. 控制候诊室内活动 a).患者进入候诊室 b).患者就诊 c.)显示等待就诊患者名单
     var visits = function () {
            }
            visits.prototype = new PriorQueue();
            visits.prototype.in = function (p) {
                this.enqueue(p)
            }
    
            visits.prototype.vist = function () {
                this.dequeue()
            }
    
            visits.prototype.show = function () {
                return this.dataStore
            }
            var visites = new visits();
            visites.in(new Patient('张三', 10));
            visites.in(new Patient('张三1', 3));
            visites.in(new Patient('张三2', 11));
            visites.in(new Patient('张三3', 23));
            visites.in(new Patient('张三4', 1));
    
    
            console.log(visites.dequeue())
            console.log(visites.show())
    

    相关文章

      网友评论

          本文标题:《数据结构与算法Javascript描述》第五章 队列 习题答案

          本文链接:https://www.haomeiwen.com/subject/fyldlqtx.html