From 08f9a1437f242f4c926f4f987ae5d2e916042139 Mon Sep 17 00:00:00 2001 From: Georgina Sanchez Date: Mon, 17 Feb 2020 21:31:38 -0800 Subject: [PATCH 1/2] linked list algorithms --- .DS_Store | Bin 0 -> 6148 bytes lib/linked_list.rb | 256 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 240 insertions(+), 16 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ef44739970d016d21e752001b7bbd0daec114aa7 GIT binary patch literal 6148 zcmeHKU5nE|6ur~dHL)NO1Xu7O;DbICsn&G`Ut(sMA4(wz zzWM|7)erm|>W}b`_^f9>6x-E(6x6vebMD+Z_og{hW->&?`jg~5kw-)hn#6{S?iVKK zW!q;B6IlT=S)+IyXGM&WENAU+P(ZF-lQK#vzL71g-xbW9{i;m8h+)OkYS*mtmv}cz zE79$KW{qa+(8^)QJ<@jDN89d$a9m775s69FkIIpEDohg|N99(Ui18-RhZ*1F`R(#% zBzSpGk~fPY13~*yd^hFOk$3!Bk&7tJd8z8@8uz!-JSnFAVv;A7;+x{izY~cu>Nux^ z$X7qxb=;A+I-B*@);fOg!sU6#pRKQVJO0JqrTN@-oRep+Z#o;%TefX%d zh~W>d?3lst@ClBQVlOQ7tjM1tBH0dYP(<6r=?Ub_h%X8H9-N;Ab-^FfRT|JO;5pi% zWHE!CSjuRTK=q2m67TsE4?}BK0jq#j;2;Ie7U#4NBH1~sfK}kXR6yl8g)7`^<>mBPiE$Z z!pzl!`HD^~&}e(BfK^~ofu 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 # returns the data value and not the node def find_min - raise NotImplementedError + current = @head + + if current.nil? + return nil + end + + min_value = @head.data + until current.nil? + if current.data < min_value + min_value = current.data + end + current = current.next + end + + return min_value + end # method that returns the length of the singly linked list def length - raise NotImplementedError + current = @head + + len = 0 + until current.nil? + len += 1 + current = current.next + end + + return len 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 def get_at_index(index) - raise NotImplementedError + if @head == nil + return nil + end + + current = @head + result = @head + length = 0 + until current.nil? + length += 1 + current = current.next + end + + if index > (length - 1) + return nil + else + (index).times do + result = result.next + end + return result.data + end + + end # method to print all the values in the linked list def visit - raise NotImplementedError + current = @head + + until current.nil? + print current.data + current = current.next + end end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + current = @head + + if current.nil? + return nil + end + + if current.data == value + @head = @head.next + else + previous_node = current + next_node = current.next + until next_node.nil? + if next_node.data == value + previous_node.next = next_node.next + return + end + previous_node = next_node + next_node = next_node.next + end + end end # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes def reverse - raise NotImplementedError + + return if @head.nil? + + current = @head + next_node = current.next + + @head.next = nil + + until next_node.nil? + temp = next_node.next + next_node.next = current + current = next_node + next_node = temp + end + + @head = current + + end ## Advanced Exercises # returns the value at the middle element in the singly linked list def find_middle_value - raise NotImplementedError + current = @head + result = @head + + if current.nil? + return nil + end + + length = 0 + until current.nil? + length += 1 + current = current.next + end + + mid_el = length/2 + + mid_el.times do + result = result.next + end + + return result.data + end # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n def find_nth_from_end(n) - raise NotImplementedError + i = @head + j = @head + + outside_bound = false + n.times do + if j != nil + j = j.next + else + outside_bound = true + end + end + + if outside_bound == true || j == nil + return nil + end + + until j.next == nil + i = i.next + j = j.next + end + + return i.data 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. def has_cycle - raise NotImplementedError + return if @head == nil + + i = @head + j = @head.next + + until j.nil? + i = i.next + j = j.next + + if j == i || j.next == i + return true + else + j = j.next + end + end + + return false + end @@ -94,24 +269,73 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + current = @head + + 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 def add_last(value) - raise NotImplementedError + if @head == nil + @head = Node.new(value) + else + current = @head + while current.next != nil + current = current.next + end + + last_node = Node.new(value) + current.next = last_node + end end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty def get_last - raise NotImplementedError + if @head.nil? + return nil + end + + current = @head + while current.next != nil + current = current.next + end + + return current.data + end # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order def insert_ascending(value) - raise NotImplementedError + return if @head == nil + + current = @head + next_node = current.next + + new_node = Node.new(value) + + if new_node.data < current.data + new_node.next = current + end + + while next_node != nil + if (current < new_node.data) && (next_node.data > new_node.data) + current.next = new_node + new_node.next = next_node + end + current = current.next + next_node = next_node.next + end + + if current.data < new_node.data + current.next = current + end + end # Helper method for tests From 2164648d4d9d8e2fc5b9bc0745e3c4f64a71e770 Mon Sep 17 00:00:00 2001 From: geomsb Date: Thu, 20 Feb 2020 13:05:35 -0800 Subject: [PATCH 2/2] bug fixed --- lib/linked_list.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index ce081369..70933347 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -205,6 +205,10 @@ def find_middle_value mid_el = length/2 + if length.even? + mid_el += 1 + end + mid_el.times do result = result.next end