insert into linked list

After writing about node_at last week, I think today I’ll cover inserting something into the list.

I am going to write the test to make sure it works, and then another test to catch the error right at the top, instead of doing the error testing at the end. This is a habit I’m trying to reinforce for myself.

describe 'insert' do 
  it "can insert at a specific point" do
    list = LinkedList.new
    list.append('a')
    list.append('b')
    list.append('c')
    list.append('e')
    list.append('f')
    expect(list.stringify).to eq("a, b, c, e, f")  
    list.insert(3, 'd')
    expect(list.stringify).to eq("a, b, c, d, e, f")      
  end
  it 'returns a message if you can not insert it' do
    list = LinkedList.new
    expect { list.insert(15, 'd') }.to raise_error(RuntimeError, "Invalid location")
  end
end

Notice in the second test we’re expecting to raise_error, which is something I haven’t mentioned before.

Time to write the method

First, I want to add something to catch it if something is written to be inserted at 0. That is the prepend method, so we can take care of that quickly,

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
end

This is taking two arguments, the location that we want the data to be inserted, and the data itself. In this case, if the location is 0, it’s getting sent to the prepend method that already exists

Because we are inserting new data, it’s going to need to be added to a new node.

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
  new_node = Node.new(data)
end

Now it’s time to figure out where that new_node is going to go. First, I want to find the node that is before the location we’re looking for. I’m going to label that prior_node

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
  new_node = Node.new(data)
  prior_node = node_at(location - 1)
end

Now that I have that, I am going to take the node that is at the location, and name it next_node.

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
  new_node = Node.new(data)
  prior_node = node_at(location - 1)
  next_node = node_at(location)
end

Finally, to insert it in, I need to make the new_node come after prior_node and before next_node

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
  new_node = Node.new(data)
  prior_node = node_at(location - 1)
  next_node = node_at(location)
  prior_node.next_node = new_node
  new_node.next_node = next_node
end

And that should take care of the first test. We do need to add something to take care of the error though. So if prior_node.nil? is going to be added in to raise an error,

def insert(location, data)
  if location == 0
    prepend(data)
    return
  end
  new_node = Node.new(data)
  prior_node = node_at(location - 1)
  raise "Invalid location" if prior_node.nil?
  next_node = node_at(location)
  prior_node.next_node = new_node
  new_node.next_node = next_node
end

And now you can insert something into your Linked List


Comments

One response to “insert into linked list”

Leave a Reply to Linked List – Brendan BondurantCancel reply

Discover more from Brendan Bondurant

Subscribe now to keep reading and get access to the full archive.

Continue reading