From 124386d2ca1257dff8065699ee9a301cc590b4e1 Mon Sep 17 00:00:00 2001 From: Sandler Date: Thu, 22 Aug 2019 11:21:58 -0700 Subject: [PATCH 1/2] Implement add_first, visit, search, min/max --- lib/#linked_list.rb# | 163 +++++++++++++++++++++++++++++++++++++++++++ lib/linked_list.rb | 81 ++++++++++++++++++--- 2 files changed, 236 insertions(+), 8 deletions(-) create mode 100644 lib/#linked_list.rb# diff --git a/lib/#linked_list.rb# b/lib/#linked_list.rb# new file mode 100644 index 00000000..51660267 --- /dev/null +++ b/lib/#linked_list.rb# @@ -0,0 +1,163 @@ + +# 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 + + def initialize(value, next_node = nil) + @data = value + @next = next_node + end +end + +# 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 + # Time Complexity: + # Space Complexity + 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 + # Time Complexity: + # Space Complexity + def search(value) + raise NotImplementedError + 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 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 + end + + + # method that returns the length of the singly linked list + # Time Complexity: + # Space Complexity + 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 + # Time Complexity: + # Space Complexity + def get_at_index(index) + raise NotImplementedError + end + + # method to print all the values in the linked list + # Time Complexity: + # Space Complexity + def visit + raise NotImplementedError + end + + # method to delete the first node found with specified value + # Time Complexity: + # Space Complexity + 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 + def reverse + raise NotImplementedError + 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: + # Space Complexity + 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. + # 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: + # Space Complexity + def get_first + raise NotImplementedError + 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 + 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 + 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/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..2b49d5ba 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,13 @@ def initialize # Time Complexity: # Space Complexity def add_first(value) - raise NotImplementedError + new_node = Node.new(value) + + if @head + new_node.next = @head + end + + @head = new_node end # method to find if the linked list contains a node with specified value @@ -29,7 +35,14 @@ def add_first(value) # Time Complexity: # Space Complexity def search(value) - raise NotImplementedError + current = @head + while current + if current.data == value + return true + end + current = current.next + end + return false end # method to return the max value in the linked list @@ -37,7 +50,20 @@ def search(value) # Time Complexity: # Space Complexity def find_max - raise NotImplementedError + if !@head + return nil + end + + current = @head + max = current.data + current = current.next + while current + 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 @@ -45,7 +71,20 @@ def find_max # Time Complexity: # Space Complexity def find_min - raise NotImplementedError + if !@head + return nil + end + + current = @head + min = current.data + current = current.next + while current + if current.data < min + min = current.data + end + current = current.next + end + return min end @@ -53,7 +92,14 @@ def find_min # Time Complexity: # Space Complexity def length - raise NotImplementedError + i = 0 + current = @head + + while current + i++ + current = current.next + end + return i end # method that returns the value at a given index in the linked list @@ -69,14 +115,32 @@ def get_at_index(index) # Time Complexity: # Space Complexity def visit - raise NotImplementedError + current = @head + while current + print "#{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 + if !@head + return + end + if @head.data == value + @head = @head.next + return + end + current = @head + while current.next + if current.next.data == value + current.next = current.next.next + return + end + current = current.next + end end # method to reverse the singly linked list @@ -120,7 +184,8 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + return nil if !@head + return @head.data end # method that inserts a given value as a new last node in the linked list From 3e9d99b4316371a0bce36be20f07ce45f99e9258 Mon Sep 17 00:00:00 2001 From: Kate Sandler Date: Thu, 17 Oct 2019 16:53:08 -0700 Subject: [PATCH 2/2] Delete #linked_list.rb# --- lib/#linked_list.rb# | 163 ------------------------------------------- 1 file changed, 163 deletions(-) delete mode 100644 lib/#linked_list.rb# diff --git a/lib/#linked_list.rb# b/lib/#linked_list.rb# deleted file mode 100644 index 51660267..00000000 --- a/lib/#linked_list.rb# +++ /dev/null @@ -1,163 +0,0 @@ - -# 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 - - def initialize(value, next_node = nil) - @data = value - @next = next_node - end -end - -# 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 - # Time Complexity: - # Space Complexity - 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 - # Time Complexity: - # Space Complexity - def search(value) - raise NotImplementedError - 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 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 - end - - - # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity - 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 - # Time Complexity: - # Space Complexity - def get_at_index(index) - raise NotImplementedError - end - - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity - def visit - raise NotImplementedError - end - - # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity - 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 - def reverse - raise NotImplementedError - 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: - # Space Complexity - 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. - # 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: - # Space Complexity - def get_first - raise NotImplementedError - 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 - 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 - 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