Book Image

Learning JavaScript Data Structures and Algorithms - Third Edition

Book Image

Learning JavaScript Data Structures and Algorithms - Third Edition

Overview of this book

A data structure is a particular way of organizing data in a computer to utilize resources efficiently. Data structures and algorithms are the base of every solution to any programming problem. With this book, you will learn to write complex and powerful code using the latest ES 2017 features. Learning JavaScript Data Structures and Algorithms begins by covering the basics of JavaScript and introduces you to ECMAScript 2017, before gradually moving on to the most important data structures such as arrays, queues, stacks, and linked lists. You will gain in-depth knowledge of how hash tables and set data structures function as well as how trees and hash maps can be used to search files in an HD or represent a database. This book serves as a route to take you deeper into JavaScript. You’ll also get a greater understanding of why and how graphs, one of the most complex data structures, are largely used in GPS navigation systems in social networks. Toward the end of the book, you’ll discover how all the theories presented in this book can be applied to solve real-world problems while working on your own computer networks and Facebook searches.
Table of Contents (22 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Circular linked lists


A circular linked list can have only one reference direction (as with a linked list) or a double reference (as with a doubly linked list). The only difference between a circular linked list and a linked list is that the last element's next (tail.next) pointer does not make a reference to undefined, but to the first element (head), as we can see in the following diagram:

A doubly circular linked list has tail.next pointing to the head element, and head.prev pointing to the tail element:

Let's check the code to create the CircularLinkedList class:

CircularLinkedList extends LinkedList {
  constructor(equalsFn = defaultEquals) {
    super(equalsFn);
  }
}

The CircularLinkedList class does not need any additional properties, so we can simply extend the LinkedList class and overwrite the required methods to apply the special behavior.

We will overwrite the implementation of the insert and removeAt methods in the following topics.

Inserting a new element at any position

The logic...