From e92720b01060caa8903ac5db5c659076b0a322d6 Mon Sep 17 00:00:00 2001 From: Jessica Date: Mon, 26 Aug 2019 00:23:39 -0700 Subject: [PATCH] implement methods --- lib/linked_list.rb | 352 ++++++++++++++++++++++++++------------- test/linked_list_test.rb | 9 +- 2 files changed, 245 insertions(+), 116 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..fd7f0398 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,152 +12,276 @@ 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 + 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 - # Time Complexity: - # Space Complexity - def add_first(value) - raise NotImplementedError - 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 + # Time Complexity: O(1) + # Space Complexity O(1) + def add_first(value) + @head = Node.new(value, @head) # i don't fully understand why next_node is @head - # method to find if the linked list contains a node with specified value - # returns true if found, false otherwise - # Time Complexity: - # Space Complexity - def search(value) - raise NotImplementedError - end + return @head.data + end - # method to return the max value in the linked list - # returns the data value and not the node - # Time Complexity: - # Space Complexity - def find_max - raise NotImplementedError - end + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) + def search(value) + current = @head - # method to return the min value in the linked list - # returns the data value and not the node - # Time Complexity: - # Space Complexity - def find_min - raise NotImplementedError + while current + return true if current.data == value + current = current.next end + return false + end - # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity - def length - raise NotImplementedError - end + # method to return the max value in the linked list + # returns the data value and not the node + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) + def find_max + current = @head - # 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 - def get_at_index(index) - raise NotImplementedError - end + return nil unless current - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity - def visit - raise NotImplementedError - end + max_value = current.data - # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity - def delete(value) - raise NotImplementedError + while current + max_value = current.data > max_value ? current.data : max_value + current = current.next 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 - def reverse - raise NotImplementedError - 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: O(n), where n is the length of the list + # Space Complexity: O(1) + def find_min + current = @head - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - # Time Complexity: - # Space Complexity - def find_middle_value - raise NotImplementedError + return nil unless current + + min_value = current.data + + while current + min_value = current.data < min_value ? current.data : min_value + current = current.next end - # find the nth node from the end and return its value - # assume indexing starts at 0 while counting to n - # Time Complexity: - # Space Complexity - def find_nth_from_end(n) - raise NotImplementedError + return min_value + end + + # method that returns the length of the singly linked list + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) + def length + return 0 unless @head + + length = 0 + + current = @head + while current + current = current.next + length += 1 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. - # Time Complexity: - # Space Complexity - def has_cycle - raise NotImplementedError + 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: O(n), where n is the value of the index + # Space Complexity: O(1) + def get_at_index(index) + return nil unless @head + + current = @head + + index.times do + current = current.next + + return nil unless current end + return current.data + end + + # method to print all the values in the linked list + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1), unless printing takes space? + def visit + return nil unless @head - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - # Time Complexity: - # Space Complexity - def get_first - raise NotImplementedError + current = @head + + while current + puts current.data + current = current.next end + end - # method that inserts a given value as a new last node in the linked list - # Time Complexity: - # Space Complexity - def add_last(value) - raise NotImplementedError + # method to delete the first node found with specified value + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) + def delete(value) + # if the value doesn't exist, just return the list + return self unless self.search(value) + + # delete the value if at the head + if @head.data == value + @head = @head.next ? @head.next : nil + return 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 - def get_last - raise NotImplementedError + # delete value somewhere else + previous = @head + current = @head.next + while current + if current.data == value + previous.next = current.next ? current.next : nil + end + current = current.next + previous = previous.next end - # method to insert a new node with specific data value, assuming the linked - # list is sorted in ascending order - # Time Complexity: - # Space Complexity - def insert_ascending(value) - raise NotImplementedError + return self + end + + # method to reverse the singly linked list + # note: the nodes should be moved and not just the values in the nodes + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1)? + def reverse + return nil unless @head + return self if self.length < 2 + + temporary = nil + previous = nil + current = @head + puts self.visit + + # printing things to try to trace what's happening + while current + puts "------------" + puts "temporary: #{temporary.data if temporary}" + puts "previous: #{previous.data if previous}" + puts "current: #{current.data}" + puts "next: #{current.next.data if current.next}" + puts "list: #{self.visit}" + temporary = current.next + current.next = previous + previous = current + current = temporary + + # puts "temporary: #{temporary.data if temporary}" + # puts "previous: #{previous.data}" + # puts "current: #{current.data if current}" + # puts "next: #{current.next.data if current && current.next}" end + + @head = previous + + puts self.visit + return self + end + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: + # Space Complexity + 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 + # Time Complexity: O(n-m), where n is the length of the list and m is the index + # Space Complexity: O(1) + def find_nth_from_end(n) + return nil if self.length - 1 < n + + self.get_at_index(self.length - 1 - n) + 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 + # 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. + # Time Complexity: + # Space Complexity + def has_cycle + raise NotImplementedError + end + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: O(1) + # Space Complexity: O(1) + def get_first + return @head ? @head.data : nil + end - # navigate to last node + # method that inserts a given value as a new last node in the linked list + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) + def add_last(value) + if @head current = @head - while current.next != nil - current = current.next + + while current.next + current = current.next end - current.next = @head # make the last node link to first node + return current.next = Node.new(value) + else + self.add_first(value) + + return @head + 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: O(n), where n is the length of the list + # Space Complexity: O(1) + def get_last + return nil unless @head + + current = @head + current = current.next while current.next + + return current.data + end + + # method to insert a new node with specific data value, assuming the linked + # list is sorted in ascending order + # Time Complexity: + # Space Complexity + 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 8311c439..86338027 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -96,6 +96,11 @@ expect(@list.get_last).must_equal 4 expect(@list.length).must_equal 3 end + + it "will return `nil` for `get_last` if the list is empty" do + # Act-Assert + expect(@list.get_last).must_be_nil + end end describe 'get_at_index' do @@ -137,7 +142,7 @@ end @list.add_last(100) @list.add_first(-12) - + # Act-Assert expect(@list.find_max).must_equal 100 expect(@list.find_min).must_equal(-12) @@ -172,7 +177,7 @@ expect(@list.get_last).must_equal 10 expect(@list.find_max).must_equal 10 expect(@list.find_min).must_equal 3 - + # Act (again) # delete last node @list.delete(10)