From 12db63fce14b051b2b51dfd9fa4d739297292ba6 Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 19:47:15 -0700 Subject: [PATCH 1/6] add_first and get_first are passing --- lib/linked_list.rb | 10 +++++++--- test/linked_list_test.rb | 14 +++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..c11e9d0a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -4,7 +4,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) + def initialize(value, next_node=nil) @data = value @next = next_node end @@ -21,7 +21,8 @@ def initialize # Time Complexity: # Space Complexity def add_first(value) - raise NotImplementedError + new_node = Node.new(value) + @head = new_node end # method to find if the linked list contains a node with specified value @@ -120,7 +121,10 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + return @head if @head.nil? + + first_node = @head + return first_node.data end # method that inserts a given value as a new last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index a7ee3769..166a25f6 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -51,7 +51,7 @@ end end - describe "length" do + xdescribe "length" do it "will return 0 for an empty list" do # Act-Assert expect(@list.length).must_equal 0 @@ -69,7 +69,7 @@ end end - describe "addLast & getLast" do + xdescribe "addLast & getLast" do it "will add to the front if the list is empty" do # Arrange @list.add_last(1) @@ -103,7 +103,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 # Act-Assert expect(@list.get_at_index(3)).must_be_nil @@ -124,7 +124,7 @@ end end - describe 'max and min values' do + xdescribe 'max and min values' do it 'returns nil if the list is empty' do # Act-Assert expect(@list.find_max()).must_be_nil @@ -149,7 +149,7 @@ end end - describe "delete" do + xdescribe "delete" do it "delete from empty linked list is a no-op" do # Assert expect(@list.length).must_equal 0 @@ -198,7 +198,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 # Act-Assert expect(@list.find_nth_from_end(3)).must_be_nil @@ -220,7 +220,7 @@ end end - describe "reverse" do + xdescribe "reverse" do it 'can retrieve an item at index n from the end in the list' do # Arrange @list.add_first(4) From 4aeae14cf544c85f2be99464251a204bc1b0be07 Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 20:24:56 -0700 Subject: [PATCH 2/6] max and min, length tests passing --- lib/linked_list.rb | 70 +++++++++++++++++++++++++++++++++++----- test/linked_list_test.rb | 6 ++-- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index c11e9d0a..8037d54e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,7 @@ def initialize # Time Complexity: # Space Complexity def add_first(value) - new_node = Node.new(value) + new_node = Node.new(value, @head) @head = new_node end @@ -30,7 +30,16 @@ def add_first(value) # Time Complexity: # Space Complexity def search(value) - raise NotImplementedError + current = @head + + while current + if current.data == value + return true + else + current = current.next + end + end + return false end # method to return the max value in the linked list @@ -38,7 +47,18 @@ def search(value) # Time Complexity: # Space Complexity def find_max - raise NotImplementedError + visited_node = @head + return nil if visited_node == nil + + max = visited_node.data + + while visited_node != nil + if visited_node.data > max + max = visited_node.data + end + visited_node = visited_node.next + end + return max end # method to return the min value in the linked list @@ -46,7 +66,18 @@ def find_max # Time Complexity: # Space Complexity def find_min - raise NotImplementedError + return nil if @head == nil + + visited_node = @head + min = visited_node.data + + while visited_node != nil + if visited_node.data < min + min = visited_node.data + end + visited_node = visited_node.next + end + return min end @@ -54,7 +85,18 @@ def find_min # Time Complexity: # Space Complexity def length - raise NotImplementedError + count = 0 + current = @head + + if @head == nil + return 0 + else + until current == nil + current = current.next + count += 1 + end + end + return count end # method that returns the value at a given index in the linked list @@ -63,7 +105,7 @@ def length # Time Complexity: # Space Complexity def get_at_index(index) - raise NotImplementedError + end # method to print all the values in the linked list @@ -128,10 +170,22 @@ def get_first end # method that inserts a given value as a new last node in the linked list - # Time Complexity: + # Time Complexity:O(n) # Space Complexity def add_last(value) - raise NotImplementedError + current = @head + count = 0 + + if current == nil + new_node = Node.new(value) + @head = new_node + else + while current.next != nil + current = current.next + end + last_node = Node.new(value) + current.next = last_node + end end # method that returns the value of the last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 166a25f6..d82a3f4e 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -51,7 +51,7 @@ end end - xdescribe "length" do + describe "length" do it "will return 0 for an empty list" do # Act-Assert expect(@list.length).must_equal 0 @@ -103,7 +103,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 # Act-Assert expect(@list.get_at_index(3)).must_be_nil @@ -124,7 +124,7 @@ end end - xdescribe 'max and min values' do + describe 'max and min values' do it 'returns nil if the list is empty' do # Act-Assert expect(@list.find_max()).must_be_nil From 86e015ee1acbbff8ea47fd7dee8f7faecb270e4b Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 22:08:45 -0700 Subject: [PATCH 3/6] currently working on delete method --- lib/linked_list.rb | 87 +++++++++++++++++++++++++++++----------- test/linked_list_test.rb | 6 +-- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8037d54e..ba3d061d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,8 +18,8 @@ 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 + # Space Complexity O def add_first(value) new_node = Node.new(value, @head) @head = new_node @@ -27,8 +27,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 def search(value) current = @head @@ -44,8 +44,8 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O def find_max visited_node = @head return nil if visited_node == nil @@ -63,8 +63,8 @@ def find_max # 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 def find_min return nil if @head == nil @@ -82,8 +82,8 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O def length count = 0 current = @head @@ -102,24 +102,56 @@ def length # 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 def get_at_index(index) - + current = @head + + return nil if @head == nil + count = 0 + + while count < index && current.next != nil + current = current.next + count += 1 + end + + return current.data end # method to print all the values in the linked list # Time Complexity: # Space Complexity def visit - raise NotImplementedError + current = @head + + while current.next != nil + puts @current.data + current = current.next + end + # for last node + puts current.data end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O def delete(value) - raise NotImplementedError + current = @head + unless current + return 0 + end + + # if head node has the value to delete + if current.data == value + @head = current.next + end + + while current.next != nil + if current.next.data == value + current.next = current.next.next + end + current = current.next + end end # method to reverse the singly linked list @@ -173,17 +205,15 @@ def get_first # Time Complexity:O(n) # Space Complexity def add_last(value) + last_node = Node.new(value) current = @head - count = 0 - - if current == nil - new_node = Node.new(value) - @head = new_node + + if @head == nil + @head = last_node else while current.next != nil current = current.next end - last_node = Node.new(value) current.next = last_node end end @@ -193,7 +223,16 @@ def add_last(value) # Time Complexity: # Space Complexity def get_last - raise NotImplementedError + count = 0 + current = @head + if !current + return nil + end + + while current.next != nil + current = current.next + end + return current.data 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 d82a3f4e..49e6c2d3 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -69,7 +69,7 @@ end end - xdescribe "addLast & getLast" do + describe "addLast & getLast" do it "will add to the front if the list is empty" do # Arrange @list.add_last(1) @@ -149,7 +149,7 @@ end end - xdescribe "delete" do + describe "delete" do it "delete from empty linked list is a no-op" do # Assert expect(@list.length).must_equal 0 @@ -168,7 +168,7 @@ @list.add_first(2) # Act - # delete fist node (requires updating head) + # delete first node (requires updating head) @list.delete(2) # Assert From 82d1604da02fe8446a14b92b97b5350351cd5356 Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 22:22:09 -0700 Subject: [PATCH 4/6] delete tests passing --- lib/linked_list.rb | 7 ++++--- test/linked_list_test.rb | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index ba3d061d..d388710f 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -137,7 +137,8 @@ def visit # Space Complexity O def delete(value) current = @head - unless current + + if @head == nil return 0 end @@ -146,8 +147,8 @@ def delete(value) @head = current.next end - while current.next != nil - if current.next.data == value + while current!= nil + if current.next != nil && current.next.data == value current.next = current.next.next end current = current.next diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 49e6c2d3..45863d37 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -188,7 +188,7 @@ expect(@list.find_max).must_equal 9 expect(@list.find_min).must_equal 3 - # delete fist node (requires updating head) + # delete first node (requires updating head) @list.delete(4) expect(@list.get_first).must_equal 3 expect(@list.length).must_equal 2 From e6d3bd0b1858c390b94254012810e61de8280e38 Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 22:33:11 -0700 Subject: [PATCH 5/6] all tests green --- lib/linked_list.rb | 40 ++++++++++++++++++++++++++++------------ test/linked_list_test.rb | 2 +- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d388710f..1068c8a3 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -82,8 +82,8 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: O(n) - # Space Complexity: O + # Time Complexity: O(n) - must count all nodes + # Space Complexity: O - constant value returned def length count = 0 current = @head @@ -102,8 +102,8 @@ def length # 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: O(n) - # Space Complexity: O + # Time Complexity: O(n) - worst case + # Space Complexity: O - constant value returned def get_at_index(index) current = @head @@ -119,8 +119,8 @@ 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 - just printing to console, no new memory needed def visit current = @head @@ -157,10 +157,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 - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O(n) def reverse - raise NotImplementedError + current = @head + reversed_list = LinkedList.new + + while current.next != nil + reversed_list.add_last(current) + current = current.next + end + return reversed_list end @@ -174,10 +181,19 @@ 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 - how is this method different from find_at_index?? def find_nth_from_end(n) - raise NotImplementedError + current = @head + + return nil if @head == nil + count = 0 + + while count < n && current.next != nil + current = current.next + count += 1 + end + return current.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 45863d37..7681a4d3 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -220,7 +220,7 @@ end end - xdescribe "reverse" do + describe "reverse" do it 'can retrieve an item at index n from the end in the list' do # Arrange @list.add_first(4) From 1823e8033e7227a5a204217134a358b90defa301 Mon Sep 17 00:00:00 2001 From: Tatiana Date: Sun, 25 Aug 2019 23:12:37 -0700 Subject: [PATCH 6/6] almost done, last test not passing again --- lib/linked_list.rb | 24 +++++++++++++++++++----- test/linked_list_test.rb | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 1068c8a3..2448b7e0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -182,18 +182,32 @@ 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: O(n) - # Space Complexity O - how is this method different from find_at_index?? + # Space Complexity O def find_nth_from_end(n) current = @head return nil if @head == nil - count = 0 - while count < n && current.next != nil + # need to know length + length = 0 + while current != nil current = current.next - count += 1 + length += 1 + end + + # return position from the end + position = length - n - 1 + return nil if position < 0 + + search_node = @head + starting_index = 0 + while search_node != nil + if starting_index == position + return search_node.data + end + starting_index += 1 + search_node = search_node.next end - return current.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 7681a4d3..8741070b 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -198,7 +198,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 # Act-Assert expect(@list.find_nth_from_end(3)).must_be_nil