From 8cbcf9977e3690c6dd95d631f763fc58745a9a73 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Tue, 23 Mar 2021 12:15:25 -0700 Subject: [PATCH 1/3] core methods written --- lib/linked_list.rb | 71 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..36d03cb6 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,24 +18,60 @@ 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) - raise NotImplementedError + # raise NotImplementedError + + new_node = Node.new(value) + new_node.next = @head + @head = new_node + + end # 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(n) def search(value) - raise NotImplementedError + # raise NotImplementedError + current_node = @head + + while current_node + if current_node.data == value + return true + else + current_node = current_node.next + end + end + + return false + end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + # raise NotImplementedError + if @head == nil + return nil + end + + max_value = @head.data + current_node = @head + + while current_node + if current_node.data > max_value + max_value = current_node.data + current_node = current_node.next + else + current_node = current_node.next + end + end + + + return max_value end # method to return the min value in the linked list @@ -43,7 +79,26 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + # raise NotImplementedError + + if @head == nil + return nil + end + + min_value = @head.data + current_node = @head + + while current_node + if current_node.data < min_value + min_value = current_node.data + current_node = current_node.next + else + current_node = current_node.next + end + end + + + return min_value end From 786c26363fea872d1d9d89e23868008c067f1481 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Tue, 23 Mar 2021 12:52:48 -0700 Subject: [PATCH 2/3] through length passing, to turn in --- lib/linked_list.rb | 60 +++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 36d03cb6..0a79f1dd 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -105,33 +105,61 @@ def find_min # 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 + # raise NotImplementedError + 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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def add_last(value) - raise NotImplementedError + # raise NotImplementedError + new_node = Node.new(value) + + if @head == nil + @head = new_node + else + current_node = @head + until current_node.next == nil do + current_node = current_node.next + end + current_node.next = new_node + end end # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def length - raise NotImplementedError + # raise NotImplementedError + length = 0 + current_node = @head + + while current_node + 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(n) def get_at_index(index) - raise NotImplementedError + # raise NotImplementedError end # method to print all the values in the linked list @@ -142,16 +170,16 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) 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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def reverse raise NotImplementedError end From 1c1e50915fdbf4453133128491bed154afd8eb9b Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Tue, 23 Mar 2021 12:56:26 -0700 Subject: [PATCH 3/3] un-commenting NotImplementError for incomplete method --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0a79f1dd..324a6f4d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -159,7 +159,7 @@ def length # Time Complexity: O(n) # Space Complexity: O(n) def get_at_index(index) - # raise NotImplementedError + raise NotImplementedError end # method to print all the values in the linked list