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.

Where Next?

Popular Pragmatic Bookshelf topics Top

Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
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

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
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
New
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: