hayashi

hayashi

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow. Linked List Insertion method

@jaywengrow

A Common-Sense Guide to Data Structures and Algorithms - insertion at wrong index (page 140)

I tested the example of a linked list insertion method on page 140 (2017 edition):

class Node
  attr_accessor :data, :next_node
  def initialize(data) 
    @data = data
  end 
end

class LinkedList
  attr_accessor :first_node
  # rest of code omitted here...

  def insert_at_index(index, value) 
    current_node = first_node 
    current_index = 0

    # First, we find the index immediately before where the new node will go:
     while current_index < index do
       current_node = current_node.next_node
       current_index += 1
     end

     # We create the new node:
     new_node = Node.new(value)
     new_node.next_node = current_node.next_node
     # We modify the link of the previous node to point to our new node:
     current_node.next_node = new_node
  end
end

The method seems to be inserting a new node after the provided index, e.g.


node_1 = Node.new("once") 
node_2 = Node.new("upon") 
node_1.next_node = node_2
node_3 = Node.new("a") 
node_2.next_node = node_3
node_4 = Node.new("time") 
node_3.next_node = node_4


list = LinkedList.new(node_1)  # 0. "once" 1. "upon" 2. "a" 3. "time"
list.insert_at_index(3, "beautiful") # 0 "once" 1. "upon" 2. "a" 3. "time" 4. "beautiful"

Shouldn’t the while loop be while current_index < index-1 instead?

Thank you for the brilliant book anyway @jaywengrow . It’s a great learning resource!

First Post!

jaywengrow

jaywengrow

Author of A Common-Sense Guide to Data Structures and Algorithms

Thanks for this! You are correct, and this has indeed been fixed in the Second Edition of the book.

Popular Pragmatic topics Top

New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Latest in PragProg

View all threads ❯