From 9b37eb28acbf25458e114b01b142d95006e01aa4 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Sun, 16 Feb 2020 18:30:58 -0800 Subject: [PATCH 1/7] yay did the first one --- lib/linked_list.rb | 5 +++-- test/linked_list_test.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..e8892a57 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,7 +19,8 @@ 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) + return @head end # method to find if the linked list contains a node with specified value @@ -94,7 +95,7 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + return @head.data end # method that inserts a given value as a new last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..76587d8f 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -1,6 +1,6 @@ require 'minitest/autorun' -require 'minitest/reporters' require 'minitest/skip_dsl' +require 'minitest/reporters' require_relative 'test_helper' From 71ff9037274237800e364b4897500ba3029696b5 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Sun, 16 Feb 2020 19:24:40 -0800 Subject: [PATCH 2/7] search, get_first --- lib/linked_list.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e8892a57..d0874375 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,14 +19,31 @@ 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) + if @head.nil? @head = Node.new(value) + else + next_node = @head + @head = Node.new(value, next_node) + end return @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 + return false if @head.nil? + return true if @head.data == value + if @head.next + checking_node = @head + found = false + end + until checking_node.next == nil || found == true + checking_node = checking_node.next + if checking_node.data == value + found = true + end + end + return found end # method to return the max value in the linked list @@ -95,6 +112,7 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first + return nil if @head.nil? return @head.data end From 1c0c5a8d28ae5d3a05dbd94484520d7c4d2349b3 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Sun, 16 Feb 2020 19:39:36 -0800 Subject: [PATCH 3/7] length, get at index, lasts --- lib/linked_list.rb | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d0874375..a5d23838 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -61,14 +61,31 @@ def find_min # method that returns the length of the singly linked list def length - raise NotImplementedError + return 0 if @head.nil? + node_count = 1 + checking_node = @head + until checking_node.next == nil + node_count += 1 + checking_node = checking_node.next + end + return node_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 + if index > self.length - 1 + return nil + else + checking_node = @head + count = 0 + until count == index + count += 1 + checking_node = checking_node.next + end + end + return checking_node.data end # method to print all the values in the linked list @@ -118,13 +135,29 @@ def get_first # method that inserts a given value as a new last node in the linked list def add_last(value) - raise NotImplementedError + if @head.nil? + add_first(value) + else + checking_node = @head + until checking_node.next.nil? + checking_node = checking_node.next + end + checking_node.next = Node.new(value) + 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 + if @head.nil? + return nil + else + checking_node = @head + until checking_node.next.nil? + checking_node = checking_node.next + end + end + return checking_node.data end # method to insert a new node with specific data value, assuming the linked From da51e7f0f4d6f129fcdc12b76c2ba68a13694ed7 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Tue, 18 Feb 2020 22:28:52 -0800 Subject: [PATCH 4/7] max and min why so difficult --- lib/linked_list.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index a5d23838..b809291b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -49,13 +49,31 @@ def search(value) # 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 = @head.data + checking_node = @head + until checking_node.next.nil? + checking_node = checking_node.next + if checking_node.data > max + max = checking_node.data + end + 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 = @head.data + checking_node = @head + until checking_node.next.nil? + checking_node = checking_node.next + if checking_node.data < min + min = checking_node.data + end + end + return min end From 270b36309f91093d31959856e6631451c6fc8080 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Tue, 18 Feb 2020 23:01:17 -0800 Subject: [PATCH 5/7] delete why u so difficult --- lib/linked_list.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b809291b..25eab4c0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -108,12 +108,30 @@ def get_at_index(index) # method to print all the values in the linked list def visit - raise NotImplementedError + return nil if @head.nil? + checking_node = @head + until checking_node.next == nil + puts checking_node.data + checking_node = checking_node.next + end end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + return nil if @head.nil? + checking_node = @head + previous_node = nil + until checking_node.nil? + if checking_node.data == value + if checking_node == @head + @head = checking_node.next + else + previous_node.next = checking_node.next + end + end + previous_node = checking_node + checking_node = checking_node.next + end end # method to reverse the singly linked list From 3c71d10004c953ac3e8346231290caad3d2ba40b Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Tue, 18 Feb 2020 23:07:19 -0800 Subject: [PATCH 6/7] find the middler --- lib/linked_list.rb | 8 +++++++- test/linked_list_test.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 25eab4c0..35e251fe 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -144,7 +144,13 @@ def reverse ## Advanced Exercises # returns the value at the middle element in the singly linked list def find_middle_value - raise NotImplementedError + return nil if @head.nil? + times_to_go = self.length / 2 + current_node = @head + times_to_go.times do + current_node = current_node.next + end + return current_node.data end # find the nth node from the end and return its value diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 76587d8f..97de1dff 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -225,4 +225,16 @@ expect(@list.find_nth_from_end(3)).must_equal 4 end end + + describe "find_middle_value" do + it 'can return the 5th item in a list of 10' do + count = 1 + until count > 10 + @list.add_first(count) + count += 1 + end + + expect(@list.find_middle_value()).must_equal 5 + end + end end From f61ee5db9064c275de59e84ba3028514b475cc90 Mon Sep 17 00:00:00 2001 From: Angele Zamarron Date: Tue, 18 Feb 2020 23:36:53 -0800 Subject: [PATCH 7/7] reverse done, all tests passing --- lib/linked_list.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 35e251fe..0c01cc4a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -114,6 +114,7 @@ def visit puts checking_node.data checking_node = checking_node.next end + puts checking_node.data end # method to delete the first node found with specified value @@ -137,7 +138,16 @@ def delete(value) # 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 + current_node = @head + previous_node = nil + until current_node.next == nil + next_node = current_node.next + current_node.next = previous_node + previous_node = current_node + current_node = next_node + end + @head = current_node + @head.next = previous_node end @@ -156,7 +166,19 @@ 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 n > self.length - 1 + main_walker = @head + secondary_walker = @head + main_walker_counter = 0 + until main_walker_counter == n + main_walker = main_walker.next + main_walker_counter += 1 + end + until main_walker.next == nil + main_walker = main_walker.next + secondary_walker = secondary_walker.next + end + return secondary_walker.data end # checks if the linked list has a cycle. A cycle exists if any node in the