From 2a8a5f46ebcb38d22142e70f6a200e567d538403 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Thu, 20 Feb 2020 12:05:48 -0800 Subject: [PATCH 1/2] finish --- .vscode/launch.json | 16 +++++ lib/linked_list.rb | 161 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..a5dbfbb6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for rdebug-ide", + "type": "Ruby", + "request": "attach", + "remoteHost": "127.0.0.1", + "remotePort": "1234", + "remoteWorkspaceRoot": "${workspaceRoot}" + } + ] +} \ No newline at end of file diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..f0870788 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,74 +19,172 @@ 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 + checker = @head + + until checker.nil? + return true if checker.data == value + checker = checker.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 = @head.data + marker = @head.next + until marker.nil? + max = marker.data if marker.data > max + marker = marker.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 = @head.data + marker = @head.next + until marker.nil? + min = marker.data if marker.data < min + marker = marker.next + end + return min end # method that returns the length of the singly linked list def length - raise NotImplementedError + length = 0 + current = @head + until current.nil? + length += 1 + current = current.next + 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 + return nil if index > self.length || index < 0 + curr = @head + + index.times do + curr = curr.next + end + + return curr.data end # method to print all the values in the linked list def visit - raise NotImplementedError + return nil if @head.nil? + curr = @head + + until curr.nil? + p curr.data + curr = curr.next + end end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + curr = @head + previous = nil + + return nil if curr.nil? + + until curr.data == value + if curr.nil? + return nil + else + previous = curr + curr = curr.next + end + end + + if previous == nil + @head = curr.next + else + previous.next = curr.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? || @head.next.nil? + + curr = @head + prev = nil + + until curr.nil? + temp = curr.next + curr.next = prev + prev = curr + curr = temp + end end ## 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? + + slow = @head + fast = @head.next + + until fast.nil? + slow = slow.next + fast = fast.next.next + end + + return slow.data 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 + return nil if @head.nil? || self.length < n + + slow = @head + fast = @head + + (n - 1).times do + fast = fast.next + end + + until fast.next.nil? + slow = slow.next + fast = fast.next + end + + return slow.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 + return false if @head.nil? + marker = @head.next + + until marker.nil? + return true if marker == @head + marker = marker.next + end + return false end @@ -94,24 +192,55 @@ 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) + end + + curr = @head + self.(length-1).times do + curr = curr.next + end + curr.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 + return nil if @head.nil? + + curr = @head + until curr.next.nil? + curr = curr.next + end + + return curr.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 + @head = Node.new(value) if @head.nil? + + if @head.data > value + temp = @head + @head = Node.new(value) + @head.next = temp + else + curr = @head + until curr.nil? + if value >= curr.data + new = Node.new(value,curr.next.next) + curr.next = new + end + curr = curr.next + end + end end # Helper method for tests From e901c90be8dfd23f289307e9545282b0cd3651d5 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Fri, 15 May 2020 16:24:23 -0700 Subject: [PATCH 2/2] refactor based on feedback --- lib/linked_list.rb | 126 ++++++++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index f0870788..170addf4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,44 +19,56 @@ 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) + # circular linked list, therefore @head point to itself @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) - checker = @head + current = @head - until checker.nil? - return true if checker.data == value - checker = checker.next + 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 + # 1. if linked list is empty: return nil if @head.nil? + + # 2. if not empty: max = @head.data - marker = @head.next - until marker.nil? - max = marker.data if marker.data > max - marker = marker.next + upcoming = @head.next + + until upcoming.nil? + max = upcoming.data if upcoming.data > max + upcoming = upcoming.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 + # 1. if linked list is empty: return nil if @head.nil? + + # 2. if not empty: min = @head.data - marker = @head.next - until marker.nil? - min = marker.data if marker.data < min - marker = marker.next + upcoming = @head.next + + until upcoming.nil? + min = upcoming.data if upcoming.data < min + upcoming = upcoming.next end + return min end @@ -64,11 +76,13 @@ def find_min # method that returns the length of the singly linked list def length length = 0 - current = @head + current = @head + until current.nil? length += 1 current = current.next end + return length end @@ -76,64 +90,66 @@ 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) + # rule out input error: index > length || index < 0 return nil if index > self.length || index < 0 - curr = @head - + + current = @head index.times do - curr = curr.next + current = current.next end - return curr.data + return current.data end # method to print all the values in the linked list def visit + # if linked list is empty: return nil if @head.nil? - curr = @head - - until curr.nil? - p curr.data - curr = curr.next + + current = @head + until current.nil? + # p instead of return to continue the loop + p current.data + current = current.next end end # method to delete the first node found with specified value def delete(value) - curr = @head - previous = nil - - return nil if curr.nil? + # if linked list is empty: + return nil if @head.nil? + + current = @head + @head = @head.next if current.data == value - until curr.data == value - if curr.nil? - return nil - else - previous = curr - curr = curr.next + until current.next.nil? + if current.next.data == value + current.next = current.next.next + return end + current = current.next end - if previous == nil - @head = curr.next - else - previous.next = curr.next - end + return end # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes def reverse + # nothing to be reversed if list is empty or only has one node: return if @head.nil? || @head.next.nil? - curr = @head + current = @head prev = nil - until curr.nil? - temp = curr.next - curr.next = prev - prev = curr - curr = temp + until current.nil? + temp = current.next + current.next = prev + prev = current + current = temp end + + @head = prev end @@ -174,20 +190,23 @@ def find_nth_from_end(n) 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. + # linked list links to a node already visited < different def than circular LI > # returns true if a cycle is found, false otherwise. def has_cycle return false if @head.nil? - marker = @head.next + + slow = @head.next + fast = @head.next.next - until marker.nil? - return true if marker == @head - marker = marker.next + until fast.nil? + return true if fast == slow + fast = fast.next + slow = slow.next end + return false end - # Additional Exercises # returns the value in the first node # returns nil if the list is empty @@ -198,12 +217,12 @@ def get_first # method that inserts a given value as a new last node in the linked list def add_last(value) - if @head.nil? - @head = Node.new(value) - end + # 1. if list is empty: + self.add_first(value) if @head.nil? + # 2. if not empty: curr = @head - self.(length-1).times do + (self.length - 1).times do curr = curr.next end curr.next = Node.new(value) @@ -237,6 +256,7 @@ def insert_ascending(value) if value >= curr.data new = Node.new(value,curr.next.next) curr.next = new + return end curr = curr.next end