繁体   English   中英

我正在尝试从堆栈创建队列,但出现错误“找不到所需的变量”

[英]I m trying to create a queue from stacks but I get the error “Can't find the variable require”

我是编程新手,我有一个练习来为堆栈创建一个队列,但是当我验证它时给我一个错误:

"队列是一个 class

TypeError: undefined is not an object(评估“Queue.prototype”)

const Stack = require('./stack'); 

class Queue {
    constructor(){ 
this.first = new Stack();
this.second = new Stack();
    }

    add(record) {
        this.first.push(record);
    }

    remove() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }

        const record = this.second.pop();

        while (this.second.peek()) {
            this.first.push(this.second.pop());

        }
        return(record); 
    }

    peek() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }

        const record = this.second.peek();

        while (this.second.peek()) {
            this.first.push(this.second.pop()); 
        }

        return record; 
    }

}
module.exports = Queue;

一个简单的例子


class Animal{
  species;
  constructor(){}
}

class Dog extends Animal{
  name;
  constructor(){
    super();
  }
}

let max = new Dog();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM