From 30fae6310a413784b4812600c34c99131a7b9db8 Mon Sep 17 00:00:00 2001 From: Morgan Schuler Date: Wed, 19 Feb 2020 19:52:16 -0800 Subject: [PATCH 1/3] Linked list exercises --- lib/linked_list.rb | 340 ++++++++++++++++++++++++++++++++------------- 1 file changed, 241 insertions(+), 99 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..6e51904e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -3,7 +3,7 @@ class Node attr_reader :data # allow external entities to read value but not write attr_accessor :next # allow external entities to read or write next node - + def initialize(value, next_node = nil) @data = value @next = next_node @@ -12,120 +12,262 @@ def initialize(value, next_node = nil) # Defines the singly linked list class LinkedList - def initialize - @head = nil # keep the head private. Not accessible outside this class + def initialize + @head = nil # keep the head private. Not accessible outside this class + end + + # 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) + @head = Node.new(value) + end + + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + def search(value) + current = @head + + until current.nil? + return true if current.data == value + current = current.next end - - # 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 + return false + end + + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + if @head == nil + return nil 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 + max = @head.data + + until current == nil + if current.data > max + max = current.data + end + current = current.next 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 max + end + + # method to return the min value in the linked list + # returns the data value and not the node + def find_min + if @head == nil + return nil 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 = @head.data + + until current == nil + if current.data < min + min = current.data + end + current = current.next end - - - # method that returns the length of the singly linked list - def length - raise NotImplementedError + return min + end + + + # method that returns the length of the singly linked list + def length + if @head.nil? + return 0 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 + count = 0 + + until current.nil? + count += 1 + current = current.next end - - # method to print all the values in the linked list - def visit - raise NotImplementedError + 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) + count = 0 + current = @head + until count == index + return nil if current.nil? + current = current.next + count += 1 end - - # method to delete the first node found with specified value - def delete(value) - raise NotImplementedError + return current.data + end + + # method to print all the values in the linked list + def visit + current = @head + + until current.nil? + print current.data 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 + end + + # method to delete the first node found with specified value + def delete(value) + current = @head + previous = nil + + return nil if current.nil? + + until current.data == value + return nil if current.nil? + previous = current + current = current.next end - - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - def find_middle_value - raise NotImplementedError + + if previous.nil? + @head = current.next + else + previous.next = current.next 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 + end + + # method to reverse the singly linked list + # note: the nodes should be moved and not just the values in the nodes + def reverse + return if @head.nil? + current = @head + previous = nil + + until current.next.nil? + temp = current.next + current.next = previous + previous = current + current = temp end - - # checks if the linked list has a cycle. A cycle exists if any node in the - # linked list links to a node already visited. - # returns true if a cycle is found, false otherwise. - def has_cycle - raise NotImplementedError + + current.next = previous + @head = current + end + + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + def find_middle_value + length = self.length + + return nil if @head.nil? + + if length % 2 == 0 + value = (length / 2) - 1 + else + value = (length / 2) end - - - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - def get_first - raise NotImplementedError + return self.get_at_index(value) + 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) + current = @head + n_ahead = @head + + return nil if current.nil? + + n.times do + return nil if n_ahead.next.nil? + n_ahead = n_ahead.next end - - # method that inserts a given value as a new last node in the linked list - def add_last(value) - raise NotImplementedError + + until n_ahead.next.nil? + return current.data if current.next.nil? + current = current.next + n_ahead = n_ahead.next 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 current.data + end + + # checks if the linked list has a cycle. A cycle exists if any node in the + # linked list links to a node already visited. + # returns true if a cycle is found, false otherwise. + def has_cycle + raise NotImplementedError + end + + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + def get_first + if @head.nil? + return nil + else + return @head.data end - - # 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 + end + + # method that inserts a given value as a new last node in the linked list + def add_last(value) + current = @head + + if current.nil? + @head = Node.new(value) + return @head end - - # Helper method for tests - # Creates a cycle in the linked list for testing purposes - # Assumes the linked list has at least one node - def create_cycle - return if @head == nil # don't do anything if the linked list is empty - - # navigate to last node - current = @head - while current.next != nil - current = current.next + + 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 + 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 + # list is sorted in ascending order + def insert_ascending(value) + current = @head + + if current.nil? + return @head = Node.new(value) + end + + until current.nil? + if value >= current.data && value < current.next.data + new_node = Node.new(value, current.next) + current.next = new_node + return + else + current = current.next end - - current.next = @head # make the last node link to first node end -end + current = Node.new(value) + end + + # Helper method for tests + # Creates a cycle in the linked list for testing purposes + # Assumes the linked list has at least one node + def create_cycle + return if @head == nil # don't do anything if the linked list is empty + + # navigate to last node + current = @head + while current.next != nil + current = current.next + end + + current.next = @head # make the last node link to first node + end +end \ No newline at end of file From 4aa856883ec89a7d2f14774b3d664ea0c0d394eb Mon Sep 17 00:00:00 2001 From: Morgan Schuler Date: Wed, 19 Feb 2020 20:18:14 -0800 Subject: [PATCH 2/3] Updates --- lib/linked_list.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 6e51904e..e76d21b1 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -93,13 +93,13 @@ def length # 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) - count = 0 current = @head - until count == index + + index.times do return nil if current.nil? current = current.next - count += 1 end + return current.data end @@ -117,7 +117,9 @@ def delete(value) current = @head previous = nil - return nil if current.nil? + if current == nil + return nil + end until current.data == value return nil if current.nil? @@ -125,7 +127,7 @@ def delete(value) current = current.next end - if previous.nil? + if previous == nil @head = current.next else previous.next = current.next @@ -170,19 +172,19 @@ def find_middle_value # assume indexing starts at 0 while counting to n def find_nth_from_end(n) current = @head - n_ahead = @head + n_from_end = @head return nil if current.nil? n.times do - return nil if n_ahead.next.nil? - n_ahead = n_ahead.next + return nil if n_from_end.next.nil? + n_from_end = n_from_end.next end - until n_ahead.next.nil? + until n_from_end.next.nil? return current.data if current.next.nil? current = current.next - n_ahead = n_ahead.next + n_from_end = n_from_end.next end return current.data end From b77e5e02c389c388ee8d4b9ef39e48561f48b641 Mon Sep 17 00:00:00 2001 From: Morgan Schuler Date: Thu, 20 Feb 2020 08:04:22 -0800 Subject: [PATCH 3/3] Fixes --- 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 e76d21b1..5733636b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -140,6 +140,7 @@ def reverse return if @head.nil? current = @head previous = nil + temp = nil until current.next.nil? temp = current.next @@ -168,7 +169,7 @@ def find_middle_value return self.get_at_index(value) end - # find the nth node from the end and return its value + # find the nth node from the end and return it s value # assume indexing starts at 0 while counting to n def find_nth_from_end(n) current = @head