From 1ee8d7313d4b5b4524b47918ac2ab368c2da5fbc Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 20 Aug 2020 14:49:11 -0700 Subject: [PATCH 1/5] Filled out search method --- lib/linked_list.rb | 24 +++++++++++++++++++++--- test/linked_list_test.rb | 16 ++++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..97d5e848 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -3,10 +3,12 @@ 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 + attr_accessor :previous - def initialize(value, next_node = nil) + def initialize(value, next_node = nil, prev_node = nil) @data = value @next = next_node + @previous = prev_node end end @@ -21,7 +23,15 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add_first(value) - raise NotImplementedError + if @head.nil? + @head = Node.new(value) + @tail = nil + else + new_node = Node.new(value) + new_node.next = @head + @head.previous = new_node + @head = new_node + end end # method to find if the linked list contains a node with specified value @@ -29,7 +39,15 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + current_node = @head + + until current_node.nil? + return true if current_node.data == value + + current_node = current_node.next + end + + return false end # method to return the max value in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..ee588568 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -21,7 +21,7 @@ end end - describe 'add_first & get_first' do + xdescribe 'add_first & get_first' do it 'can add values to an empty list' do # Act @list.add_first(3) @@ -74,7 +74,7 @@ end end - describe "length" do + xdescribe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -89,7 +89,7 @@ end end - describe "addLast & getLast" do + xdescribe "addLast & getLast" do it "will add to the front if the list is empty" do @list.add_last(1) expect(@list.get_first).must_equal 1 @@ -112,7 +112,7 @@ end end - describe 'get_at_index' do + xdescribe 'get_at_index' do it 'returns nil if the index is outside the bounds of the list' do expect(@list.get_at_index(3)).must_be_nil end @@ -130,7 +130,7 @@ end end - describe 'max and min values' do + xdescribe 'max and min values' do it 'returns nil if the list is empty' do expect(@list.find_max()).must_be_nil expect(@list.find_min()).must_be_nil @@ -152,7 +152,7 @@ end end - describe "delete" do + xdescribe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) @@ -192,7 +192,7 @@ end end - describe "nth_from_the_end" do + xdescribe "nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end @@ -211,7 +211,7 @@ end end - describe "reverse" do + xdescribe "reverse" do it 'can retrieve an item at index n from the end in the list' do @list.add_first(4) @list.add_first(3) From cacf5a703bfc1dbed0949652fada5c25969a5590 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 20 Aug 2020 15:56:30 -0700 Subject: [PATCH 2/5] Pass tests for add_first, find_max, find_min, length, get_at_index --- lib/linked_list.rb | 133 ++++++++++++++++++++++++++++++--------- test/linked_list_test.rb | 12 ++-- 2 files changed, 108 insertions(+), 37 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 97d5e848..ec64db06 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -20,12 +20,11 @@ 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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def add_first(value) if @head.nil? - @head = Node.new(value) - @tail = nil + @head = @tail = Node.new(value) else new_node = Node.new(value) new_node.next = @head @@ -36,8 +35,8 @@ def add_first(value) # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def search(value) current_node = @head @@ -52,33 +51,74 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) def find_max - raise NotImplementedError + current_node = @head + max_value = current_node.data unless current_node.nil? + + until current_node.nil? + max_value = current_node.data if current_node.data > max_value + + current_node = current_node.next + end + + return max_value end # method to return the min value in the linked list # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_min - raise NotImplementedError + current_node = @head + min_value = current_node.data unless current_node.nil? + + until current_node.nil? + min_value = current_node.data if current_node.data < min_value + + current_node = current_node.next + end + + return min_value end # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length - raise NotImplementedError + current_node = @head + length = 0 + + until current_node.nil? + length += 1 + + current_node = current_node.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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + length = self.length + return nil if length <= index + return @head.data if index == 0 + + current_index = 0 + current_node = @head + + until current_index == index + current_index += 1 + current_node = current_node.next + end + + return current_node.data end # method to print all the values in the linked list @@ -89,18 +129,42 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(value) - raise NotImplementedError + return if @head.nil? + if @head.data == value + @head = nil + return + end + + current_node = @head + previous_node = @head.previous + + until current_node.nil? + next_node = current_node.next + + if current_node.data == value + next_node.previous = previous_node + previous_node.next = current_node.next unless previous_node.nil? + @tail = next_node if next_node.next.nil? + return + end + + previous_node = current_node + current_node = current_node.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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O() + # Space Complexity: O(n) def reverse - raise NotImplementedError + #TODO: use recursion + return end @@ -133,25 +197,32 @@ def has_cycle # Additional Exercises # returns the value in the first node # returns nil if the list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def get_first - raise NotImplementedError + return @head.data unless @head.nil? end # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def add_last(value) - raise NotImplementedError + if @head.nil? + @head = @tail = Node.new(value) + else + new_node = Node.new(value) + @tail.next = new_node + new_node.previous = @tail + @tail = new_node + end end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def get_last - raise NotImplementedError + return @tail.data unless @tail.nil? end # method to insert a new node with specific data value, assuming the linked diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index ee588568..001da4b3 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -21,7 +21,7 @@ end end - xdescribe 'add_first & get_first' do + describe 'add_first & get_first' do it 'can add values to an empty list' do # Act @list.add_first(3) @@ -74,7 +74,7 @@ end end - xdescribe "length" do + describe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -89,7 +89,7 @@ end end - xdescribe "addLast & getLast" do + describe "addLast & getLast" do it "will add to the front if the list is empty" do @list.add_last(1) expect(@list.get_first).must_equal 1 @@ -112,7 +112,7 @@ end end - xdescribe 'get_at_index' do + describe 'get_at_index' do it 'returns nil if the index is outside the bounds of the list' do expect(@list.get_at_index(3)).must_be_nil end @@ -130,7 +130,7 @@ end end - xdescribe 'max and min values' do + describe 'max and min values' do it 'returns nil if the list is empty' do expect(@list.find_max()).must_be_nil expect(@list.find_min()).must_be_nil @@ -152,7 +152,7 @@ end end - xdescribe "delete" do + describe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) From ab9e27c295f3afebfff3f14a447157d4ded6af7e Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 20 Aug 2020 16:06:22 -0700 Subject: [PATCH 3/5] Complete delete function --- 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 ec64db06..4dff8142 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -122,10 +122,19 @@ def get_at_index(index) end # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def visit - raise NotImplementedError + return nil if @head.nil? + current_node = @head + + until current_node.nil? + puts current_node.data + + current_node = current_node.next + end + + return end # method to delete the first node found with specified value @@ -133,8 +142,10 @@ def visit # Space Complexity: O(1) def delete(value) return if @head.nil? + if @head.data == value - @head = nil + @head = nil if @head.next.nil? + @head = @head.next unless @head.nil? return end @@ -142,11 +153,18 @@ def delete(value) previous_node = @head.previous until current_node.nil? + if current_node.next.nil? + @tail = previous_node + previous_node.next = nil + return + end + next_node = current_node.next if current_node.data == value + next_node.previous = previous_node - previous_node.next = current_node.next unless previous_node.nil? + previous_node.next = next_node @tail = next_node if next_node.next.nil? return end From 270e41aa28329fd5491fd1c6f91e4f62e7ec03c8 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Thu, 20 Aug 2020 17:26:41 -0700 Subject: [PATCH 4/5] Almost there --- lib/linked_list.rb | 27 +++++++++++++++++++++++---- test/linked_list_test.rb | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 4dff8142..3668b722 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -158,7 +158,7 @@ def delete(value) previous_node.next = nil return end - + next_node = current_node.next if current_node.data == value @@ -181,6 +181,12 @@ def delete(value) # Time Complexity: O() # Space Complexity: O(n) def reverse + return if @head.nil? + return if @head.next.nil? + temp = @tail + @tail = @head + @head = temp + #TODO: use recursion return end @@ -196,10 +202,23 @@ def find_middle_value # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_nth_from_end(n) - raise NotImplementedError + length = self.length + return nil if length <= n + return @head.data if n == 0 + return @tail.data if n == length - 1 + + current_index = length - 1 + current_node = @tail + + until current_index == n + current_index -= 1 + current_node = current_node.previous + end + + return current_node.data end # checks if the linked list has a cycle. A cycle exists if any node in the diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 001da4b3..35a7b85d 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -192,7 +192,7 @@ end end - xdescribe "nth_from_the_end" do + describe "nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end From 1a9222191bb0dd6302229e471309c1744b713a85 Mon Sep 17 00:00:00 2001 From: Alicia Combs Date: Mon, 24 Aug 2020 22:34:27 -0700 Subject: [PATCH 5/5] Attempted to refine reverse --- lib/linked_list.rb | 16 +++++++++++++--- test/linked_list_test.rb | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 3668b722..34ddb493 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -183,11 +183,21 @@ def delete(value) def reverse return if @head.nil? return if @head.next.nil? - temp = @tail - @tail = @head + + temp = @head @head = temp + @tail = @head + + current = @head + + until current == nil + temp = current.next + current.next = current.previous + current.previous = temp + + current = current.previous + end - #TODO: use recursion return end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index c1328395..c1756935 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -211,7 +211,7 @@ end end - xdescribe "reverse" do + describe "reverse" do it 'can retrieve an item at index n from the end in the list' do @list.add_first(4) @list.add_first(3)