From bc46b852b84328e7a6208a2429a392e4f7200470 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 20 Feb 2020 08:49:42 -0800 Subject: [PATCH 1/3] first commit --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..09f67962 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,4 @@ - +#first commit # Defines a node in the singly linked list class Node attr_reader :data # allow external entities to read value but not write From b584b77dccb1bc8ac9850a834993c76e0a2168b8 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 20 Feb 2020 09:07:37 -0800 Subject: [PATCH 2/3] will need to return later for failed tests --- lib/linked_list.rb | 69 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 09f67962..101a76f5 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,4 @@ -#first commit + # Defines a node in the singly linked list class Node attr_reader :data # allow external entities to read value but not write @@ -19,43 +19,96 @@ 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 + current = @head + @head = Node.new(value, current) 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 + if @head.nil? + return false + else + current = @head + end + + while current + if current.data == value + return true + else + current = current.next + end + end 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? + current = @head + max = @head.data + + while current + max = current.data if current.data > max + current = current.next + end 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? + current = @head + min = @head.data + + while current + min = current.data if current.data < min + current = current.next + end end # method that returns the length of the singly linked list def length - raise NotImplementedError + count = 1 + node = @head + + return 0 if @head.nil? + + until node == nil + count += 1 + node = node.next + end 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 current.nil? + return current.data if count == index + count += 1 + current = current.next + end + + return nil end # method to print all the values in the linked list def visit - raise NotImplementedError + return nil if @head.nil? + current = @head + linked_list = "" + + while current + linked_list += "#{current.data}" + current = current.next + end end # method to delete the first node found with specified value From faa44570ccffcb186325507e5823ccd852d380c6 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 20 Feb 2020 14:13:39 -0800 Subject: [PATCH 3/3] fixed length method --- lib/linked_list.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 101a76f5..730daa25 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -70,7 +70,7 @@ def find_min # method that returns the length of the singly linked list def length - count = 1 + count = 0 node = @head return 0 if @head.nil? @@ -79,6 +79,7 @@ def length count += 1 node = node.next end + return count end # method that returns the value at a given index in the linked list