From 2482707c729a79a0d820530e0e92bac1dba62e15 Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 15:16:27 -0500 Subject: [PATCH 1/8] add_first, get_first, and get_at_index methods implemented --- .idea/.gitignore | 8 +++++ .idea/.rakeTasks | 7 ++++ .idea/inspectionProfiles/Project_Default.xml | 6 ++++ .idea/linked-list.iml | 27 +++++++++++++++ .idea/misc.xml | 4 +++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ lib/linked_list.rb | 35 ++++++++++++++++++-- 8 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.rakeTasks create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/linked-list.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 00000000..e409da2a --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..b0db9b0f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/linked-list.iml b/.idea/linked-list.iml new file mode 100644 index 00000000..28179284 --- /dev/null +++ b/.idea/linked-list.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..15ffe978 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..a096c061 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..66bee330 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,12 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add_first(value) - raise NotImplementedError + # if the head is nil new_node becomes head + # Then head becomes the new node. + new_node = Node.new(value, nil) + new_node.next = @head + @head = new_node + return @head.data; end # method to find if the linked list contains a node with specified value @@ -60,7 +65,23 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + # traverse the list, + # index, number of times or + # until the end is reach + # Then return the current node's value + i = 0 + current = @head + while current != nil + if i == index + return current.data + else + i += 1 + current = current.next + end + end + + return nil + end # method to print all the values in the linked list @@ -118,7 +139,12 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + # return the value of the 1st node in the list + if @head.nil? + return nil + else + return @head.data + end end # method that inserts a given value as a new last node in the linked list @@ -133,6 +159,9 @@ def add_last(value) # Time Complexity: ? # Space Complexity: ? def get_last + # return nil if the list is empty + # otherwise traverse the list to the end + # Then return the last node's value raise NotImplementedError end From e7728af6e60730a82884d4d400693b8406d057dd Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 15:22:39 -0500 Subject: [PATCH 2/8] search method implemented --- lib/linked_list.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 66bee330..d4c7d8b5 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -34,7 +34,15 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + current = @head + while current != nil + if current.data == value + return true + else + current = current.next + end + end + return false end # method to return the max value in the linked list From f1771a5153b7ed2e1b45aaff21c854ba4fb79bea Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 16:22:21 -0500 Subject: [PATCH 3/8] find_max, find_min methods implemented --- lib/linked_list.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d4c7d8b5..e1eee804 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -48,7 +48,16 @@ def search(value) # 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_value = @head.data + while current != nil + if current.data > max_value + max_value = current.data + end + current = current.next + end + return max_value end # method to return the min value in the linked list @@ -56,7 +65,17 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + return nil if @head.nil? + current = @head + min_value = current.data + while current != nil + if current.data < min_value + min_value = current.data + end + current = current.next + end + return min_value + end From 3dec5ef3d3d928174aa70a0974a7bfdc3b8dd962 Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 16:25:22 -0500 Subject: [PATCH 4/8] length method implemented --- lib/linked_list.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e1eee804..9b4ebbb6 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -83,7 +83,14 @@ def find_min # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + return 0 if @head.nil? + current = @head + length = 0 + while current != nil + length += 1 + current = current.next + end + return length end # method that returns the value at a given index in the linked list From 4cb62db9ee0d3542150833c7704b49bfe7b989ef Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 17:29:12 -0500 Subject: [PATCH 5/8] visit method implemented --- lib/linked_list.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9b4ebbb6..9b8d03b2 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -122,14 +122,28 @@ def get_at_index(index) # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + 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: ? def delete(value) - raise NotImplementedError + current = @head + return if current.nil? + + while current != nil + if current.next.nil? + + end + if current.next.value == value + + end + end end # method to reverse the singly linked list @@ -137,7 +151,7 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse - raise NotImplementedError + end From 49c2176cf59d4349c6968721bd89ee389e7aea46 Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 18:23:50 -0500 Subject: [PATCH 6/8] delete method implemented --- lib/linked_list.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9b8d03b2..44172361 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -133,16 +133,25 @@ def visit # Time Complexity: ? # Space Complexity: ? def delete(value) - current = @head - return if current.nil? - while current != nil - if current.next.nil? + if @head.nil? + return + end - end - if current.next.value == value + if @head.data == value + @head = @head.next + return + end + current = @head + previous = nil + + while current != nil + if current.data == value + previous.next = current.next end + previous = current + current = current.next end end From 6583329baab809951704fcc9400d97b9955636bc Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 19:03:03 -0500 Subject: [PATCH 7/8] reverse method implemented --- lib/linked_list.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 44172361..0ee7e1a5 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,12 +21,11 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add_first(value) - # if the head is nil new_node becomes head - # Then head becomes the new node. + new_node = Node.new(value, nil) new_node.next = @head @head = new_node - return @head.data; + return @head.data end # method to find if the linked list contains a node with specified value @@ -160,7 +159,20 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse + if @head.nil? || @head.next.nil? + return @head + end + + current = @head + previous = nil + while current != nil + temp = current.next + current.next = previous + previous = current + current = temp + end + @head = previous end From de3b664784affacd941c8247e5d901e660e55aba Mon Sep 17 00:00:00 2001 From: Alice D Date: Fri, 2 Apr 2021 19:06:45 -0500 Subject: [PATCH 8/8] time and space complexities updated --- lib/linked_list.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0ee7e1a5..fd01dbbb 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,8 +18,8 @@ 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) new_node = Node.new(value, nil) @@ -30,8 +30,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) current = @head while current != nil @@ -46,6 +46,8 @@ def search(value) # 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(1) def find_max return nil if @head.nil? current = @head @@ -61,8 +63,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 @@ -79,8 +81,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? current = @head @@ -95,8 +97,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) # traverse the list, # index, number of times or @@ -118,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 current = @head while current !=nil @@ -129,8 +131,8 @@ def visit 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) if @head.nil? @@ -156,8 +158,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 if @head.nil? || @head.next.nil? return @head