Chapter 14
These are the solutions to the exercises found in the section Exercises.
-
One way we can do this is with a simple while loop:
printList() { let currentNode = this.firstNode; while (currentNode) { console.log(currentNode.data); currentNode = currentNode.nextNode; } } -
With a doubly linked list, we have immediate access to the last nodes and can follow their “previous node” links to access the previous nodes. This code is basically the inverse of the previous exercise:
reversePrint() { let currentNode = this.lastNode; while (currentNode) { console.log(currentNode.data); currentNode = currentNode.previousNode; } } -
Here, we use a while loop to move through each node. However, before we move forward, ...
Get A Common-Sense Guide to Data Structures and Algorithms in JavaScript, Volume 1 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.