From 1dcd03cd719bdddae68e70f006d15f0128928553 Mon Sep 17 00:00:00 2001 From: Sharon Cheung Date: Sun, 23 Aug 2020 00:57:02 -0700 Subject: [PATCH 1/2] all tests passed --- lib/linked_list.rb | 120 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 105 insertions(+), 15 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..447fc31e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,27 +1,38 @@ -# Defines a node in the singly linked list +# Defines a node in the doubly 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 :previous - def initialize(value, next_node = nil) + + def initialize(value, next_node = nil, previous = nil) @data = value @next = next_node + @previous = previous end end -# Defines the singly linked list +# Defines the doubly linked list class LinkedList def initialize @head = nil # keep the head private. Not accessible outside this class + @tail = nil 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: ? + # Time Complexity: 0(1) + # Space Complexity: 0(1) def add_first(value) - raise NotImplementedError + if @head.nil? + @head = @tail = Node.new(value) + else + new_node = Node.new(value) + new_node.next = @head + @head.previous = new_node + @head = new_node + end end # method to find if the linked list contains a node with specified value @@ -29,13 +40,32 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + return false if @head.nil? + current = @head + until current == nil + if current.data == value + return true + end + current = current.next + 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 + return nil if @head.nil? + current = @head + 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 @@ -43,7 +73,17 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + return nil if @head.nil? + current = @head + min = current.data + until current == nil + if current.data < min + min = current.data + end + current = current.next + end + + return min end @@ -51,7 +91,15 @@ def find_min # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + return 0 if @head.nil? + count = 0 + current = @head + until current == nil + current = current.next + count += 1 + end + + return count end # method that returns the value at a given index in the linked list @@ -60,21 +108,49 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + return nil if @head.nil? + current = @head + index.times do + if current.next.nil? + return nil + end + current = current.next + end + return current.data end # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + curernt = @head + until current == nil + puts current.data + current = current.next + end end # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(value) - raise NotImplementedError + return nil if @head.nil? + if @head.data == value + @head = @head.next + @head.previous = nil + elsif @tail.data == value + @tail = @tail.previous + @tail.next = nil + else + current = @head + until current == nil + if current.data == value + current.previous.next = current.next + current.next.previous = current.previous + end + current = current.next + end + end end # method to reverse the singly linked list @@ -82,7 +158,20 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse - raise NotImplementedError + return nil if @head.nil? + current = @head + previous_node = nil + + until current == nil + temp = current.previous + current.previous = current.next + current.next = temp + current = current.previous + end + + if temp + @head = temp.previous + end end @@ -118,7 +207,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 From 5ce5b9d98719bfb4735ff3320b30273e65489618 Mon Sep 17 00:00:00 2001 From: Sharon Cheung Date: Mon, 24 Aug 2020 21:26:33 -0700 Subject: [PATCH 2/2] test passed --- lib/linked_list.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 447fc31e..1524f485 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -37,8 +37,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(1) def search(value) return false if @head.nil? current = @head @@ -70,8 +70,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(1) def find_min return nil if @head.nil? current = @head @@ -88,8 +88,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(1) def length return 0 if @head.nil? count = 0 @@ -105,8 +105,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: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_at_index(index) return nil if @head.nil? current = @head @@ -120,8 +120,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(1) def visit curernt = @head until current == nil @@ -155,8 +155,8 @@ 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(1) def reverse return nil if @head.nil? current = @head @@ -168,7 +168,7 @@ def reverse current.next = temp current = current.previous end - + if temp @head = temp.previous end