From 021ddcdb2facbd9acdf548b59204487465792c0f Mon Sep 17 00:00:00 2001 From: Jessica Stone Date: Mon, 24 Aug 2020 21:26:55 -0700 Subject: [PATCH] update not implemented functions to pass tests --- .DS_Store | Bin 0 -> 6148 bytes lib/linked_list.rb | 171 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 143 insertions(+), 28 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f9f60d0a8f94f19a9f55d4bcbfeaf7866207ada5 GIT binary patch literal 6148 zcmeH~Jr2S!425mzfW*>~F$)La1`&c2Z~;_UkSZ}C_8gt(pM}DVD)cPbU+mOs`-Y|# z5#2wpJCR;QR&b+iElf<2FJ+XQ4A;kH9PangN^Xm!72v&0_H&z{0#twsPys4H1!kl` z9^|X_jGl>)LItS6JQT3+LxCG>vIYIqf#4$m*rDu(wa*e@u>x3=Er<$CqZN!+^)bZi z-VT<$t|nVB+C_8t(7dzS6a&*}7cEF&S{)2jfC@|$SVrF4`G16eoBt;*OsN1B_%j8x zJDd)Cyi}g8AFpTib5?EL;GkcQ@b(jc#E#+(+ztE17GO=bASy8a2)GOkRN$uyyZ{;| B5o7=W literal 0 HcmV?d00001 diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..f976bc7e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -2,11 +2,12 @@ # Defines a node in the singly linked list 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 :next, :previous, :data # allow external entities to read or write next node - 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 @@ -18,71 +19,184 @@ 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), since it will always have direct access to the front of the list + # Space Complexity: O(1) def add_first(value) - raise NotImplementedError + if @head == nil + newNode = Node.new(value) + @head = newNode + return + end + + current_head = @head + newNode = Node.new(value, next_node = current_head) + current_head.previous = newNode + @head = newNode + return 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), n being up to the length of the linked list + # Space Complexity: O(1) def search(value) - raise NotImplementedError + return false if @head == nil + return true if @head.data == value + + current_node = @head + until current_node == nil + 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 + # Time Complexity: O(n), n being up to the length of the linked list + # Space Complexity: O(1) def find_max - raise NotImplementedError + return nil if @head == nil + + current_node = @head + max_value = current_node.data + until current_node == nil + if current_node.data > max_value + max_value = current_node.data + end + 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), n being up to the length of the linked list + # Space Complexity: O(1) def find_min - raise NotImplementedError + return nil if @head == nil + + current_node = @head + min_value = current_node.data + until current_node == nil + if current_node.data < min_value + min_value = current_node.data + end + 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), n being the length of the linked list + # Space Complexity: O(1) def length - raise NotImplementedError + return 0 if @head == nil + + 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), n being the index to get the value at + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + return nil if @head == nil + + current_node = @head + current_index = 0 + until current_node == nil + if current_index == index + return current_node.data + end + current_index += 1 + current_node = current_node.next + end + + return nil end # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), n being the amount of nodes + # Space Complexity: O(1) def visit - raise NotImplementedError + return nil if @head == nil + + current_node = @head + until current_node == nil + current_node = current_node.next + end end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), n being up to the the amount of nodes in the list + # Space Complexity: O(1) def delete(value) - raise NotImplementedError + return nil if @head == nil + + current_node = @head + previous_node = nil + next_node = current_node.next + until current_node == nil + if current_node.data == value + # if first node is being deleted + if previous_node == nil + next_node.previous = nil + @head = next_node + return + end + + # if last node is deleted + if next_node == nil + previous_node.next = nil + return + end + + next_node.previous = previous_node + previous_node.next = next_node + return + end + current_node = current_node.next + next_node = current_node.next + previous_node = current_node.previous + end 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), n being the number of nodes in the list + # Space Complexity: O(1) def reverse - raise NotImplementedError + return if @head == nil + + length = self.length + current_node = @head + + until length == 0 + next_node = current_node.next + current_node.next = current_node.previous + current_node.previous = next_node + current_node = next_node if next_node != nil + length -= 1 + if next_node == nil + @head = current_node + end + end end @@ -118,7 +232,8 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + return nil if @head == nil + return @head.data end # method that inserts a given value as a new last node in the linked list