Linked List to String

This time, I want to focus on printing out the data from each node. I usually call this to_string, but I saw it named stringify recently, so I might just borrow that. This will be handy with some of the other methods coming up, like prepend and insert, to more clearly check if the tests are working. As with the last few, I will start with a test.

RSpec.describe LinkedList do
  describe 'stringify' do    
    it 'can be turned into a string' do 
      list = LinkedList.new
      list.append("A")
      expect(list.stringify).to eq("A")
    end
  end
end

Time to build out the method

def stringify
  new_string = []
  current_node = head
  new_string << current_node.data
  new_string.join
end

I will put the data into the array new_string so that it stays in order. I will use logic similar to that of other things I’ve written in prior weeks. First, temporarily assigning current_node to the head, taking the data from current_node, and sticking it into the new_string array. I will use join to make it return as a string and not an array. This should pass the test. But what if it’s longer than one data point? Probably not going to work. So another test

it 'can turn multiple data points into a string' do 
  list = LinkedList.new
  list.append("A")
  list.append("B")
  list.append("C")
  list.append("D")
  expect(list.stringify).to eq("A, B, C, D")
end

Now, there are a few modifications to the method. If a conditional is added to check if that is the last thing in the list, it can work its way through, slowly adding more to the array until it is through the entire list, as opposed to just returning the first thing, as it did above.

def stringify
  new_string = []
  current_node = head
    while current_node.next_node != nil
      new_string &lt;&lt; current_node.data
      current_node = current_node.next_node
    end
  new_string &lt;&lt; current_node.data
  new_string.join
end

With this modification, it checks to see if something comes next. If that is the case, it passes the current data into the array, reassigns current_node, and continues until it is done. However, I didn’t want it to return "ABCD", I wanted "A, B, C, D", which requires a slight change to join. Add this (", ") to the end of the join method, so it inserts a comma and space, and then it will return the way I wanted it to.

def stringify
  new_string = []
  current_node = head
    while current_node.next_node != nil
      new_string << current_node.data
      current_node = current_node.next_node
    end
  new_string << current_node.data
  new_string.join(", ")
end

As mentioned in last week’s blog about the count, it’s probably a good idea to add something in case the list is empty.

def stringify
  new_string = []
  current_node = head
  return new_string if empty?
    while current_node.next_node != nil
      new_string << current_node.data
      current_node = current_node.next_node
    end
  new_string << current_node.data
  new_string.join(", ")
end

If the list is empty, it will return an empty array. This test can be used to check it.

it 'returns 0 if count is 0' do
  list = LinkedList.new
  expect(list.stringify).to eq([])
end

Comments

One response to “Linked List to String”

  1. […] stringify […]

Leave a Reply

Discover more from Brendan Bondurant

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

Continue reading