diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..e18afc65 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,38 +19,88 @@ def initialize # method to add a new node with the specific data value in the linked list # insert the new node at the beginning of the linked list def add_first(value) - raise NotImplementedError + @head = Node.new(value, @head) end # method to find if the linked list contains a node with specified value # returns true if found, false otherwise def search(value) - raise NotImplementedError + current = @head + + until current.nil? + return true if current.data == value + current = current.next + end + + return false end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + return nil if @head.nil? + max = 0 + current = @head + + until current.nil? + if current.data > max + max = current.data + end + current = current.next + end + + return max end # method to return the min value in the linked list # returns the data value and not the node def find_min - raise NotImplementedError + return nil if @head.nil? + min = nil + current = @head + + until current.nil? + if min == nil + min = current.data + elsif current.data < min + min = current.data + end + current = current.next + end + + return min end # method that returns the length of the singly linked list def length - raise NotImplementedError + count = 0 + return count if @head.nil? + + current = @head + until current.nil? + current = current.next + count += 1 + end + + return count end # method that returns the value at a given index in the linked list # index count starts at 0 # returns nil if there are fewer nodes in the linked list than the index value def get_at_index(index) - raise NotImplementedError + return nil if @head.nil? + + current = @head + count = 0 + + until count == index + current = current.next + count += 1 + end + + return current.data end # method to print all the values in the linked list @@ -60,13 +110,40 @@ def visit # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + return nil if @head.nil? + + current = @head + prev = nil + + until current.data == value + prev = current + current = current.next + end + + if prev.nil? + @head = current.next + else + prev.next = current.next + end end # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes def reverse - raise NotImplementedError + return if @head.nil? + + current = @head + previous = nil + + until current.next.nil? + temp = current.next + current.next = previous + previous = current + current = temp + end + + current.next = previous + @head = current end @@ -79,7 +156,23 @@ def find_middle_value # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n def find_nth_from_end(n) - raise NotImplementedError + return nil if @head.nil? + current = @head + + length = 0 + until current.next.nil? + length += 1 + current = current.next + end + + nth = @head + nth_length = length - n + return nil if nth_length < 0 + nth_length.times do + nth = nth.next + end + + return nth.data end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -94,18 +187,36 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + return nil if @head.nil? + return @head.data end # method that inserts a given value as a new last node in the linked list def add_last(value) - raise NotImplementedError + if @head.nil? + @head = Node.new(value, nil) + else + current = @head + while !current.next.nil? + current = current.next + end + + current.next = Node.new(value, nil) + end end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty def get_last - raise NotImplementedError + return nil if @head.nil? + + current = @head + + until current.next.nil? + current = current.next + end + + return current.data end # method to insert a new node with specific data value, assuming the linked