Creating the StackLinkedList class

We can also use the LinkedList class and its variations as internal data structures to create other data structures such as stack, queue, and deque. In this topic, we will learn how to create the stack data structure (covered in Chapter 4, Stacks).

The StackLinkedList class structure and the methods push and pop are declared as follows:

class StackLinkedList {  constructor() {    this.items = new DoublyLinkedList(); // {1}  }  push(element) {    this.items.push(element); // {2}  }  pop() {    if (this.isEmpty()) {      return undefined;    }    return this.items.removeAt(this.size() - 1); // {3}  }}

For the StackLinkedList class, instead of using an array or a JavaScript object to store the items, we will use a DoublyLinkedList

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.