From c39aff54454e2589fb1677884c1b7319eb30831d Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 20 Feb 2020 13:55:46 -0800 Subject: [PATCH 1/6] completed add_first and get_first methods --- lib/linked_list.rb | 237 ++++++++++++++++++++------------------- test/linked_list_test.rb | 100 ++++++++--------- 2 files changed, 171 insertions(+), 166 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..8b439d1d 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,125 @@ 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 - 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 - 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 - end - - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError - end - - # method to return the min value in the linked list - # returns the data value and not the node - def find_min - raise NotImplementedError - end - - - # method that returns the length of the singly linked list - def length - raise NotImplementedError - 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 - end - - # method to print all the values in the linked list - def visit - raise NotImplementedError - end - - # method to delete the first node found with specified value - def delete(value) - 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 - raise NotImplementedError - end - - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - def find_middle_value - raise NotImplementedError - 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 - - # 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 - raise NotImplementedError - end - - # method that inserts a given value as a new last node in the linked list - def add_last(value) - raise NotImplementedError - 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 - 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 - - # 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 + 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) + 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 + end + + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + raise NotImplementedError + end + + # method to return the min value in the linked list + # returns the data value and not the node + def find_min + raise NotImplementedError + end + + + # method that returns the length of the singly linked list + def length + raise NotImplementedError + 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 + end + + # method to print all the values in the linked list + def visit + raise NotImplementedError + end + + # method to delete the first node found with specified value + def delete(value) + 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 + raise NotImplementedError + end + + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + def find_middle_value + raise NotImplementedError + 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 + + # 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 + end + + # method that inserts a given value as a new last node in the linked list + def add_last(value) + raise NotImplementedError + 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 + 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 + + # 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 diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..33ea6ca9 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -12,73 +12,73 @@ before do @list = LinkedList.new end - + describe 'initialize' do it 'can be created' do - + # Assert expect(@list).must_be_kind_of LinkedList end end - + describe 'add_first & get_first' do it 'can add values to an empty list' do # Act @list.add_first(3) - + # Assert expect(@list.get_first).must_equal 3 end - + it 'will put the last added item to the front of the list' do # Act @list.add_first(1) @list.add_first(2) - + # Assert expect(@list.get_first).must_equal 2 - + # Act again @list.add_first(3) - + # Assert expect(@list.get_first).must_equal 3 end - + it 'will return `nil` for `getFirst` if the list is empty' do - + expect(@list.get_first).must_be_nil end end - - describe "search" do + + xdescribe "search" do it "can find an element" do @list = LinkedList.new @list.add_first(3) @list.add_first(2) - + expect(@list.search(3)).must_equal true - + expect(@list.search(2)).must_equal true end - + it "returns false if the element is not in the list" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - expect(@list.search("pasta")).must_equal false + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + expect(@list.search("pasta")).must_equal false end - + it "returns false for an empty list" do - expect(@list.search(3)).must_equal false + expect(@list.search(3)).must_equal false end end - - describe "length" do + + xdescribe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end - + it "will return the length for nonempty lists" do count = 0 while count < 5 @@ -88,54 +88,54 @@ end 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 end - + it "will put new items to the rear of the list" do @list.add_last(2) expect(@list.length).must_equal 1 expect(@list.get_last).must_equal 2 - + @list.add_last(3) expect(@list.get_first).must_equal 2 expect(@list.get_last).must_equal 3 expect(@list.length).must_equal 2 - + @list.add_last(4) expect(@list.get_first).must_equal 2 expect(@list.get_last).must_equal 4 expect(@list.length).must_equal 3 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 - + it 'can retrieve an item at an index in the list' do @list.add_first(1) @list.add_first(2) @list.add_first(3) @list.add_first(4) - + expect(@list.get_at_index(0)).must_equal 4 expect(@list.get_at_index(1)).must_equal 3 expect(@list.get_at_index(2)).must_equal 2 expect(@list.get_at_index(3)).must_equal 1 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 end - + it 'can retrieve the max and min values in the list' do count = 0 while count < 5 @@ -144,28 +144,28 @@ expect(@list.find_min).must_equal 0 count += 1 end - + @list.add_last(100) @list.add_first(-12) expect(@list.find_max).must_equal 100 expect(@list.find_min).must_equal(-12) 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) expect(@list.length).must_equal 0 end - + it "can delete valid values from list" do @list.add_last(9) @list.add_last(10) @list.add_first(4) @list.add_first(3) @list.add_first(2) - + # delete fist node (requires updating head) @list.delete(2) expect(@list.get_first).must_equal 3 @@ -173,7 +173,7 @@ expect(@list.get_last).must_equal 10 expect(@list.find_max).must_equal 10 expect(@list.find_min).must_equal 3 - + # delete last node @list.delete(10) expect(@list.get_first).must_equal 3 @@ -181,7 +181,7 @@ expect(@list.get_last).must_equal 9 expect(@list.find_max).must_equal 9 expect(@list.find_min).must_equal 3 - + # delete fist node (requires updating head) @list.delete(4) expect(@list.get_first).must_equal 3 @@ -191,18 +191,18 @@ expect(@list.find_min).must_equal 3 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 - + it 'can retrieve an item at index n from the end in the list' do @list.add_first(1) @list.add_first(2) @list.add_first(3) @list.add_first(4) - + expect(@list.find_nth_from_end(0)).must_equal 1 expect(@list.find_nth_from_end(1)).must_equal 2 expect(@list.find_nth_from_end(2)).must_equal 3 @@ -210,15 +210,15 @@ expect(@list.find_nth_from_end(4)).must_be_nil 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) @list.add_first(2) @list.add_first(1) @list.reverse - + expect(@list.find_nth_from_end(0)).must_equal 1 expect(@list.find_nth_from_end(1)).must_equal 2 expect(@list.find_nth_from_end(2)).must_equal 3 From 201e22d3db9c9528663828f17e522e4c3658f526 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 20 Feb 2020 14:19:55 -0800 Subject: [PATCH 2/6] completed search and cycle method --- lib/linked_list.rb | 27 +++++++++++++++++++++++++-- test/linked_list_test.rb | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8b439d1d..37ba1f75 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -26,7 +26,17 @@ def add_first(value) # 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 + + until current == nil + 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 @@ -87,7 +97,20 @@ def find_nth_from_end(n) # linked list links to a node already visited. # returns true if a cycle is found, false otherwise. def has_cycle - raise NotImplementedError + fast = @head.next + slow = @head + + until fast.nil? || slow.nil? + if fast == slow + return true + end + + fast = fast.next + fast = fast.next unless fast.nil? + slow = slow.next + end + + return false end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 33ea6ca9..606ab668 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -51,7 +51,7 @@ end end - xdescribe "search" do + describe "search" do it "can find an element" do @list = LinkedList.new @list.add_first(3) From b446fb55dc372de3ebcf73711b84b74643b45d79 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sun, 1 Mar 2020 21:16:09 -0800 Subject: [PATCH 3/6] completed length --- lib/linked_list.rb | 36 +++++++++++++++++++++++++++++++++--- test/linked_list_test.rb | 4 ++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 37ba1f75..8441274d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -54,7 +54,18 @@ def find_min # method that returns the length of the singly linked list def length - raise NotImplementedError + current = @head + length = 0 + + if @head == nil + return length + else + until current == nil + current = current.next + length += 1 + end + return length + end end # method that returns the value at a given index in the linked list @@ -127,13 +138,32 @@ def get_first # method that inserts a given value as a new last node in the linked list def add_last(value) - raise NotImplementedError + new_node = Node.new(value, nil) + current = @head + + if current == nil + @head = new_node + else + until current.next == nil + current = current.next + end + current.next = 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 def get_last - raise NotImplementedError + current = @head + + if current == nil + return nil + else + until current.next == nil + current = current.next + end + return current.data + end 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 606ab668..6a9d30e4 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 From 6a92e6429ce071683ff709f68958ff71003feb4f Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sun, 1 Mar 2020 22:56:00 -0800 Subject: [PATCH 4/6] completed getatindex and find min, max --- lib/linked_list.rb | 52 +++++++++++++++++++++++++++++++++++++--- test/linked_list_test.rb | 4 ++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8441274d..05da4e56 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -42,13 +42,43 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + current = @head + + if current == nil + return nil + end + + max = current.data + + 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 + current = @head + + if current == nil + return nil + end + + min = current.data + + until current == nil + if current.data < min + min = current.data + end + current = current.next + end + + return min end @@ -72,7 +102,23 @@ 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) - raise NotImplementedError + current = @head + counter = 0 + + if current == nil + return nil + end + + while counter <= index + if current == nil + return nil + elsif counter == index + return current.data + end + + current = current.next + counter += 1 + end end # method to print all the values in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 6a9d30e4..947ba14e 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 From cd7c13ddaa52dd080d1ee22752f10400cb26100f Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Sun, 1 Mar 2020 23:48:20 -0800 Subject: [PATCH 5/6] completed delete and nthfromend --- lib/linked_list.rb | 36 ++++++++++++++++++++++++++++++++++-- test/linked_list_test.rb | 4 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 05da4e56..cf2d9edb 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -128,7 +128,21 @@ def visit # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + current = @head + previous = nil + + until current == nil + if current.data == value + if previous == nil + @head = current.next + else + previous.next = current.next + end + end + + previous = current + current = current.next + end end # method to reverse the singly linked list @@ -147,7 +161,25 @@ 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 + current = @head + counter = 0 + all_data = [] + + if current == nil + return nil + end + + until current == nil + all_data << current.data + current = current.next + counter += 1 + end + + if n >= all_data.length + return nil + end + + return all_data[all_data.length - (n + 1)] 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 947ba14e..c779cb7a 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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) @@ -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 0855b8a26620d5a5c5bf46a15c5d1ea553637215 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Mon, 2 Mar 2020 00:18:25 -0800 Subject: [PATCH 6/6] completed reverse --- lib/linked_list.rb | 21 ++++++++++++++++++++- test/linked_list_test.rb | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index cf2d9edb..f30391ae 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -148,7 +148,26 @@ 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 - raise NotImplementedError + current_node = @head + previous_node = nil + next_node = nil + + if current_node == nil + return nil + end + + until current_node == nil + next_node = current_node.next + if previous_node == nil + current_node.next = nil + else + current_node.next = previous_node + end + previous_node = current_node + current_node = next_node + end + + @head = previous_node end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index c779cb7a..f8c5c49b 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)