From 630eca3c2a67843cfd62f035072233420588f120 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 24 Aug 2020 21:33:23 -0700 Subject: [PATCH 1/4] Adds LL methods. --- lib/linked_list.rb | 145 ++++++++++++++++++++++++++++++++++-------- lib/ll.code-workspace | 10 +++ 2 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 lib/ll.code-workspace diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..814b4e84 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -4,9 +4,10 @@ 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) + def initialize(value) @data = value @next = next_node + @next = nil end end @@ -18,71 +19,161 @@ 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 - end + if @head.nil? + @head = Node.new(value) + else + new_nodelet = Node.new(value, @head) + new_nodelet.next = @head + @head = new_nodelet + end + 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 + return false if @head.nil? + + current = @head + + while current != nil + return true if value == current.data + 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: O(n) + # Space Complexity: O(n) def find_max - raise NotImplementedError + return nil if @head.nil? + + current = @head + highest_value = @head.data + + until current.nil? + if current.data > highest_value + highest_value = current.data + end + current = current.next + end + + return highest_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) + # Space Complexity: O(n) def find_min - raise NotImplementedError + return nil if @head.nil? + + current = @head + lowest_value = @head.data + + until current.nil? + if current.data < lowest_value + lowest_value = current.data + end + current = current.next + end + + return lowest_value 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 + return 0 if @head.nil? + + current = @head + count = 0 + + while current != nil + current = current.next + count += 1 + 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 + return nil if @head.nil? + + current = @head + count = 0 + + until count == index + current = current.next + count += 1 + end + + return current.data 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 - raise NotImplementedError + return nil if @head.nil? + + current = @head + + while current != nil + puts current.data + current = current.next + end 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 + return nil if @head.nil? + + last = @head + current = @head + + until current.data == value || current.nil? + last = current + current = current.next + end + + last.next = current.next 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 + return nil if @head.nil? + + last = nil + current = @head + + while current != nil + queued = current.next + current.next = last + last = current + current = queued + end + + @head = last end diff --git a/lib/ll.code-workspace b/lib/ll.code-workspace new file mode 100644 index 00000000..8884edbe --- /dev/null +++ b/lib/ll.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "../.." + }, + { + "path": ".." + } + ] +} \ No newline at end of file From 86ef57f6fbeb35d7bdeb504637df9555102aa8a6 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 24 Aug 2020 21:39:00 -0700 Subject: [PATCH 2/4] Fix error. --- lib/linked_list.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 814b4e84..09426ec3 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -4,10 +4,9 @@ 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) + def initialize(value, next = nil) @data = value @next = next_node - @next = nil end end From 5b8e07c282c50d21ca87edda86f57ea9e3b72202 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Thu, 27 Aug 2020 17:41:43 -0700 Subject: [PATCH 3/4] Fixes error. --- 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 09426ec3..4b26d9b8 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -4,7 +4,7 @@ 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 = nil) + def initialize(value) @data = value @next = next_node end From 59a7aad79ebb89db960cf60d2301ab78ca95ae5b Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Thu, 27 Aug 2020 17:50:41 -0700 Subject: [PATCH 4/4] Fixes error. Now runs cleanly on my machine. --- lib/linked_list.rb | 1 + lib/tempCodeRunnerFile.rb | 1 + 2 files changed, 2 insertions(+) create mode 100644 lib/tempCodeRunnerFile.rb diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 4b26d9b8..337b3525 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -8,6 +8,7 @@ def initialize(value) @data = value @next = next_node end + end # Defines the singly linked list diff --git a/lib/tempCodeRunnerFile.rb b/lib/tempCodeRunnerFile.rb new file mode 100644 index 00000000..90f1b0f0 --- /dev/null +++ b/lib/tempCodeRunnerFile.rb @@ -0,0 +1 @@ +@next = next_node \ No newline at end of file