Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Dequeuing elements from the queue

Next, we are going to implement the dequeue method. This method is responsible for removing items from the queue. As the queue uses the FIFO principle, the first item that we added in the queue is the one that is removed:

dequeue() {  if (this.isEmpty()) {    return undefined;  }  const result = this.items[this.lowestCount]; // {1}  delete this.items[this.lowestCount]; // {2}  this.lowestCount++; // {3}  return result; // {4}}

First, we need to verify whether the queue is empty and, if so, we return the value undefined. If the queue is not empty, we will store the value from the front of the queue ({1}so we can return it ({4}) after the element has been removed ({2}). We also need to increase the lowestCount property ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content