Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
JavaScript Queue Implantation
//create a Node class that will represent elements/nodes in the Queue
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}
//creates a Queue class to store the elements/nodes of the Queue
class Queue {
  constructor() {
    this.first = null;
    this.last = null;
    this.size = 0;
  }
  //enqueues a node
  enqueue(val) {
    let node = new Node(val);
    if (this.size === 0) {
      this.first = node;
      this.last = node;
    } else {
      this.last.next = node;
      this.last = node;
    }
    return this.size++;
  }
  //dequeues a node 
  dequeue() {
    if (!this.first) {
      return null;
    }
    let temp = this.first;
    if (this.first === this.last) {
      this.last = null;
    }
    this.first = this.first.next;
    this.size--;
    return temp.value;
  }
}

I was following an article on medium on how to implement stacks and queues with JavaScript. I was lost when it came to queues specifically the way the author implemented enqueing. On the last bits of the method "enqueue", he writes

{
   this.last.next = node;
   this.last = node;
}

Wouldn't the previous item be overwritten by this implementation? What Im i missing?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

this.last.next will be null, there is nothing after the last item.

this.last.next = node; creates a link from the current last node to the new item, then this.last = node; sets the current last node to the node that was just inserted. The previous last node is still in the linked list though.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!