首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >这个Queue类有什么问题?Javascript

这个Queue类有什么问题?Javascript
EN

Stack Overflow用户
提问于 2018-09-26 03:45:54
回答 2查看 124关注 0票数 -3

这是我的Que类,但我对对象不是很熟悉,所以获取对象的大小和元素对我不起作用

代码语言:javascript
复制
class Queue{
   // Array is used to implement a Queue
    constructor(){
        this.items = {};
        this.count = 0;
    }

    // enqueue(item)
    enqueue(element){    
        // adding element to the queue
        this.items.push(element);
        this.count++;
    }
    // dequeue()
    dequeue(){
        // removing element from the queue
        // returns underflow when called 
        // on empty queue
        if(this.isEmpty())
            return "Underflow";
        this.count--;
        return this.items.shift();
     }

    // front()
    front(){
        // returns the Front element of 
        // the queue without removing it.
        if(this.isEmpty())
            return "No elements in Queue";
        return this.items[0];
    }

// isEmpty()

代码语言:javascript
复制
    isEmpty(){
        // return true if the queue is empty.
        return this.items.length == 0;
    }

    // peek()
    peek(){
        return this.items[this.count]
    }

    size(element){
        return this.count;
    }

    show(){
        var result = '';
        var i=this.count-1;

        for(i; i>=0; i--){
            result += this.items[i] +' ';
        }

        return result;
    }
}

像size()、show()和isEmpty()这样的方法不能工作吗?它们返回未定义的。

EN

回答 2

Stack Overflow用户

发布于 2018-09-26 03:53:14

代码中的小错误。只需将项的对象文字更改为数组文字即可。this.items = []

代码语言:javascript
复制
class Queue{
   // Array is used to implement a Queue
    constructor(){
        this.items = []; //the problem is resolved
        this.count = 0;
    }

    // enqueue(item)
    enqueue(element){    
        // adding element to the queue
        this.items.push(element);
        this.count++;
    }
    // dequeue()
    dequeue(){
        // removing element from the queue
        // returns underflow when called 
        // on empty queue
        if(this.isEmpty())
            return "Underflow";
        this.count--;
        return this.items.shift();
     }

    // front()
    front(){
        // returns the Front element of 
        // the queue without removing it.
        if(this.isEmpty())
            return "No elements in Queue";
        return this.items[0];
    }
    
    // isEmpty()
    isEmpty(){
        // return true if the queue is empty.
        return this.items.length == 0;
    }

    // peek()
    peek(){
        return this.items[this.count]
    }

    size(element){
        return this.count;
    }

    show(){
        var result = '';
        var i=this.count-1;

        for(i; i>=0; i--){
            result += this.items[i] +' ';
        }

        return result;
    }
}

let q = new Queue();

console.log(q.isEmpty())
console.log(q.size())
console.log(q.show());

票数 1
EN

Stack Overflow用户

发布于 2018-09-26 03:58:08

首先,如果它是一个数组,但你将它声明为任何类型的对象,你就会威胁到它。第二,当你调用size函数时,一个更好的方法是返回: items.length。最后,现在可以从每次使用中删除count变量。

祝好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52505685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档