From 888b936b5ce1084f43633d2fb3a1421ebeeaf3cd Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 16 Feb 2020 22:14:29 -0800 Subject: [PATCH 1/4] Some exercises --- lib/linked_list.rb | 114 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 12 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..8eab8632 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,54 +19,129 @@ 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 + new_node = Node.new(value, @head) + @head = new_node 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 + + current = @head + found = false + until current == nil + if current.data == value + found = true + break + end + + current = current.next + end + return found 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 = 0 + until current == nil + if current.data > max + max = current.data + end + current = current.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 + + current = @head + min = 0 + until current == nil + if current.data < min + min = current.data + end + current = current.next + end + return min end # method that returns the length of the singly linked list def length - raise NotImplementedError + return 0 if @head == nil + + current = @head + length = 0 + 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 + length = 0 + current = @head + value_to_find = nil + + until current == nil + if index == length + value_to_find = current.data + current = nil + end + length += 1 + current == current.next + end + + return value_to_find end # method to print all the values in the linked list def visit - raise NotImplementedError + return nil if @head == nil + 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 + return nil if @head == nil + current = @head + previous = current + + until current == nil || previous.next == nil + if current.data == value + previous.next == current.next + current.next = nil + end + 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 + previous = nil + current = @head + + until current == nil + current = current.next + current.next = previous + end @@ -94,12 +169,27 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + if @head == nil + return @head + else + return @head.data + end 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 + add_first(value) + else + current = @head + previous = current + new_last_node = Node.new(value, nil) + + until @current.next == nil + current = current.next + end + current.next == new_last_node + end end # method that returns the value of the last node in the linked list @@ -123,7 +213,7 @@ def create_cycle # navigate to last node current = @head while current.next != nil - current = current.next + current = current.next end current.next = @head # make the last node link to first node From af24c7a61281dcf9ba32c0698dda11a16012a691 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Mon, 17 Feb 2020 22:53:51 -0800 Subject: [PATCH 2/4] Only delete and nth from end are missing --- lib/linked_list.rb | 69 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8eab8632..71ce311e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -47,7 +47,8 @@ def find_max return nil if @head == nil current = @head - max = 0 + max = current.data + until current == nil if current.data > max max = current.data @@ -91,20 +92,19 @@ 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) - length = 0 current = @head - value_to_find = nil - until current == nil - if index == length - value_to_find = current.data - current = nil + index.times do + if current != nil + current = current.next end - length += 1 - current == current.next end - return value_to_find + if current != nil + return current.data + else + return nil + end end # method to print all the values in the linked list @@ -125,8 +125,9 @@ def delete(value) previous = current until current == nil || previous.next == nil + if current.data == value - previous.next == current.next + previous.next == nil current.next = nil end end @@ -135,13 +136,20 @@ 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 - previous = nil + return if @head.nil? + current = @head + previous = nil - until current == nil - current = current.next + until current.next.nil? + temp = current.next current.next = previous + previous = current + current = temp + end + current.next = previous + @head = current end @@ -153,9 +161,22 @@ 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 - end + # def find_nth_from_end(n) + # return nil if @head == nil + + # current = @head + # nth_previous = current + + # until current.next == nil + # (n - 1).times do + # nth_previous = nth_previous.next + # end + + # n.times do + # current = current.next + # end + # end + # 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. @@ -185,17 +206,25 @@ def add_last(value) previous = current new_last_node = Node.new(value, nil) - until @current.next == nil + until current.next == nil current = current.next end - current.next == new_last_node + current.next = new_last_node 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 + return nil if @head == nil + + current = @head + + 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 ba9689f96a8ccfbee3952680bf60bd1e8132a94c Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Tue, 18 Feb 2020 23:26:12 -0800 Subject: [PATCH 3/4] Delete method missing to pass tests --- lib/linked_list.rb | 61 ++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 71ce311e..b20ad57a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -121,35 +121,23 @@ def visit # method to delete the first node found with specified value def delete(value) return nil if @head == nil - current = @head - previous = current - - until current == nil || previous.next == nil - - if current.data == value - previous.next == nil - current.next = nil - end - 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 - return if @head.nil? - + return nil if @head == nil current = @head previous = nil - - until current.next.nil? - temp = current.next + + while current != nil + next_value = current.next current.next = previous previous = current - current = temp + current = next_value end - current.next = previous - @head = current + @head = previous end @@ -161,22 +149,27 @@ 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) - # return nil if @head == nil - - # current = @head - # nth_previous = current - - # until current.next == nil - # (n - 1).times do - # nth_previous = nth_previous.next - # end - - # n.times do - # current = current.next - # end - # end - # end + def find_nth_from_end(n) + return nil if @head == nil + + answer_pointer = @head + scanning_pointer = answer_pointer + + n.times do + if scanning_pointer.next != nil + scanning_pointer = scanning_pointer.next + else + return nil + end + end + + until scanning_pointer.next == nil + answer_pointer = answer_pointer.next + scanning_pointer = scanning_pointer.next + end + + return answer_pointer.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. From d3d225e2007840dbe65d2de0f82e423a4f101754 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Tue, 16 Jun 2020 18:23:42 -0700 Subject: [PATCH 4/4] delete method --- lib/linked_list.rb | 20 ++++++++++++++++++-- test/linked_list_test.rb | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b20ad57a..7838be56 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -64,8 +64,8 @@ def find_min return nil if @head == nil current = @head - min = 0 - until current == nil + min = @head.data + while current != nil if current.data < min min = current.data end @@ -121,6 +121,22 @@ def visit # method to delete the first node found with specified value def delete(value) return nil if @head == nil + + if @head.data == value + @head = @head.next + end + + curr = @head + prev = nil + + while curr != nil + if curr.data == value + prev.next = curr.next + end + prev = curr + curr = curr.next + end + return @head end # method to reverse the singly linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..7d9173f9 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -174,7 +174,7 @@ expect(@list.find_max).must_equal 10 expect(@list.find_min).must_equal 3 - # delete last node + # # delete last node @list.delete(10) expect(@list.get_first).must_equal 3 expect(@list.length).must_equal 3 @@ -182,7 +182,7 @@ expect(@list.find_max).must_equal 9 expect(@list.find_min).must_equal 3 - # delete fist node (requires updating head) + # # delete fist node (requires updating head) @list.delete(4) expect(@list.get_first).must_equal 3 expect(@list.length).must_equal 2