From 55b14379b1cd8b63a2a551ae5c449bc20144fe3c Mon Sep 17 00:00:00 2001 From: Nara Date: Mon, 26 Aug 2019 23:03:40 -0700 Subject: [PATCH] Partially complete methods --- lib/linked_list.rb | 132 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 100 insertions(+), 32 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..bd847077 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,3 @@ - # Defines a node in the singly linked list class Node attr_reader :data # allow external entities to read value but not write @@ -18,71 +17,122 @@ 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 + new_node = Node.new(value, @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(1) 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 # returns the data value and not the node - # Time Complexity: - # Space Complexity + # Time Complexity: o(n) + # Space Complexity: O(1) def find_max - raise NotImplementedError + if @head == nil + return + end + current = @head + max = current.data + 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 # returns the data value and not the node - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(1) def find_min - raise NotImplementedError + if @head == nil + return + end + current = @head + min = current.data + while current + if current.data < min + min = current.data + end + current = current.next + end + return min end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(1) def length - raise NotImplementedError + current = @head + count = 0 + while current + count += 1 + current = current.next + end + return count 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(1) def get_at_index(index) - raise NotImplementedError + current = @head + count = 0 + while current + if count == index + return current.data + elsif count > index + return + end + count += 1 + current = current.next + end end - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: o(n) def visit - raise NotImplementedError + current = @head + linked_list_values = [] + while current + linked_list_values.push(current.data) + current = current.next + end + return linked_list_values end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(1) 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(1) def reverse raise NotImplementedError end @@ -117,17 +167,31 @@ def has_cycle # 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 + if @head == nil + return + 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 def add_last(value) - raise NotImplementedError + new_node = Node.new(value) + if @head == nil + @head = new_node + return + else + current = @head + while current.next != nil + current = current.next + end + current.next = new_node + end end # method that returns the value of the last node in the linked list @@ -135,7 +199,11 @@ def add_last(value) # Time Complexity: # Space Complexity def get_last - raise NotImplementedError + current = @head + while current + current = current.next + end + return current.data end # method to insert a new node with specific data value, assuming the linked