0

我正在从他的 udemy 课程中查看 Colt Steele 的 enqueue 方法。当我根据他的代码和console.log 创建一个新队列时,它显示第一个属性正在不断更新,但是从代码看来,它似乎应该只在 first 等于 null 时才更新。那么它不应该只更新一次吗?

然后,last 属性的 next 属性更新为 newNode,但之后,last 属性又更新为 newNode,这样不就取消了之前的命令吗?它不会将新节点添加到最后一个属性的下一个属性,并且当我们使最后一个属性等于新节点时,下一个命令不会将下一个属性重新分配为 null 吗?

function Queue(){
this.first = null;
this.last = null;
this.size = 0;
}

function Node(value){
  this.value = value;
  this.next = null;
}

Queue.prototype.enqueue = function(value){
  let newNode = new Node(value);
  if(!this.first){
    this.first = newNode;
    this.last = newNode;
  }
  else {
    this.last.next = newNode;
    this.last = newNode;
  }
4

0 回答 0