Chapter 14

These are the solutions to the exercises found in the section, Exercises. The solutions provided here are in Ruby, but you can find the solutions in JavaScript and Python in the code download.[14]

  1. One way we can do this is with a simple while loop:

     def​ ​print
      current_node = first_node
     
     while​ current_node
      puts current_node.​data
      current_node = current_node.​next_node
     end
     end
  2. 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 simply the inverse of the previous exercise:

     def​ ​print_in_reverse
      current_node = last_node
     
     while​ current_node
      puts current_node.​data
      current_node = current_node.​ ...

Get A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd 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.