From 25cab4f3fb13ff55561fbcdde57884226d3fd718 Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 16 Feb 2020 18:49:20 -0800 Subject: [PATCH 1/8] Implemented add_first and search method --- lib/linked_list.rb | 12 ++++++++++-- test/linked_list_test.rb | 1 - 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..168f58a4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,13 +19,20 @@ 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 @@ -129,3 +136,4 @@ def create_cycle current.next = @head # make the last node link to first node end end + diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 22b55ef0..4b84cd0d 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -1,6 +1,5 @@ require 'minitest/autorun' require 'minitest/reporters' -require 'minitest/skip_dsl' require_relative 'test_helper' From 4b093bcc0b3a7d865cf9d9ff9cfea7dd560ed76f Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 16 Feb 2020 19:07:52 -0800 Subject: [PATCH 2/8] Implemented find_min and find_max methods --- lib/linked_list.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 168f58a4..2dae7fea 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -29,7 +29,6 @@ def search(value) until current.nil? return true if current.data == value - current = current.next end return false @@ -38,19 +37,35 @@ 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 + current = @head + max = current + return nil if current.nil? + + until current.nil? + max = current if current.data > max.data + current = current.next + end + return max.data end # method to return the min value in the linked list # returns the data value and not the node def find_min - raise NotImplementedError + current = @head + min = current + return nil if current.nil? + + until current.nil? + min = current if current.data < min.data + current = current.next + end + return min.data end # method that returns the length of the singly linked list def length - raise NotImplementedError + end # method that returns the value at a given index in the linked list From de033ca9f42c68c9e7cca37b20b52988566b949b Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 16 Feb 2020 19:25:14 -0800 Subject: [PATCH 3/8] Implemented length and get_at_index methods --- lib/linked_list.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2dae7fea..046f3b81 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -65,14 +65,30 @@ def find_min # method that returns the length of the singly linked list def length - + current = @head + length = 0 + return 0 if current.nil? + + until current.nil? + current = current.next + length += 1 + end + return length 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 + current = @head + current_index = 0 + + index.times do |i| + return nil if current.nil? + current = current.next + end + + return current.data end # method to print all the values in the linked list From 9d860c74473378adbcf71a7d278f11c49d0a4bac Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 16 Feb 2020 20:14:44 -0800 Subject: [PATCH 4/8] Implemented get_first, get_last and add_last methods --- lib/linked_list.rb | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 046f3b81..e93c5ae3 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -93,12 +93,26 @@ def get_at_index(index) # method to print all the values in the linked list def visit - raise NotImplementedError + current = @head + + until current.nil? + print current.data + current = current.next + end end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + current = @head + + return nil if current.nil? + until current.nil? + to_delete = current if current.data == value + current = current.next + end + + to_delete.previous.next = to_delete.next + to_delete.next.previous = to_delete.previous end # method to reverse the singly linked list @@ -132,18 +146,38 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + current = @head + + return nil if current.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 + current = @head + + if current.nil? + @head = Node.new(value) + return + end + + until current.next.nil? + current = current.next + end + + current.next = Node.new(value) 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 + current = @head + + return nil if current.nil? + 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 From 6a064efb76ac920a33e8126ce5f30a1e6087a391 Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Sun, 16 Feb 2020 20:38:30 -0800 Subject: [PATCH 5/8] Implemented delete method --- lib/linked_list.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e93c5ae3..388901d0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -104,15 +104,19 @@ def visit # method to delete the first node found with specified value def delete(value) current = @head + previous = nil return nil if current.nil? - until current.nil? - to_delete = current if current.data == value + until current.data == value + previous = current current = current.next end - to_delete.previous.next = to_delete.next - to_delete.next.previous = to_delete.previous + if previous.nil? + @head = current.next + else + previous.next = current.next + end end # method to reverse the singly linked list @@ -183,7 +187,21 @@ def get_last # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order def insert_ascending(value) - raise NotImplementedError + current = @head + + if current.nil? + @head = Node.new(value) + end + + until current.nil? + if value.data > last.data && value.data < current.data + new_node = Node.new(value, current.next) + current.next = new_node + return + else + current = current.next + end + end end # Helper method for tests From 2a0c65230ea1548cc97063c744595272cdb5f310 Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Wed, 19 Feb 2020 13:06:22 -0800 Subject: [PATCH 6/8] Impletemented reverse method --- lib/linked_list.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 388901d0..134e174b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -122,7 +122,17 @@ 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 = @head + previous = nil + + return nil if current.nil? + until current.nil? + temp = current + current.next = previous + current = current.next + previous = temp + end + @head = previous end From e7e6fc3edfad159e55f67e4e7146fd1878fabfa7 Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Wed, 19 Feb 2020 17:58:10 -0800 Subject: [PATCH 7/8] Fixed reverse method --- lib/linked_list.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 134e174b..3efc9fbe 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -125,11 +125,10 @@ def reverse current = @head previous = nil - return nil if current.nil? until current.nil? temp = current - current.next = previous current = current.next + temp.next = previous previous = temp end @head = previous @@ -139,13 +138,36 @@ def reverse ## Advanced Exercises # returns the value at the middle element in the singly linked list def find_middle_value - raise NotImplementedError + current = @head + middle = current + + return nil if current.nil? + until current.nil? + middle.next = middle + current.next.next = current + end + return middle end # 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 + current = @head + from_end = current + + return nil if current.nil? + + n.times do + return nil if current.next.nil? + current = current.next + end + + until current.next.nil? + return from_end.data if from_end.next.nil? + from_end = from_end.next + current = current.next + end + return from_end.data end # checks if the linked list has a cycle. A cycle exists if any node in the From b04e642f969ffb0422fac143575cd98e0790e60b Mon Sep 17 00:00:00 2001 From: Julia Bouvier Date: Wed, 19 Feb 2020 18:01:12 -0800 Subject: [PATCH 8/8] Implemented has_cycle --- lib/linked_list.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 3efc9fbe..cfcff9af 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -174,7 +174,15 @@ def find_nth_from_end(n) # linked list links to a node already visited. # returns true if a cycle is found, false otherwise. def has_cycle - raise NotImplementedError + slow = @head + fast = @head + + until fast.nil? + slow = slow.next + fast = fast.next.next + return true if slow == fast + end + return false end