From 8a1a025d300b77f93c7460767a01b951c8bf7aff Mon Sep 17 00:00:00 2001 From: Xinran Che Date: Tue, 18 Feb 2020 00:04:21 -0800 Subject: [PATCH 1/4] Completed --- lib/linked_list.rb | 303 ++++++++++++++++++++++++++++----------------- 1 file changed, 190 insertions(+), 113 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..034624dc 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -3,7 +3,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_node = nil) @data = value @next = next_node @@ -12,120 +12,197 @@ def initialize(value, next_node = nil) # 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 - 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 - def search(value) - raise NotImplementedError - end - - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError - end - - # method to return the min value in the linked list - # returns the data value and not the node - def find_min - raise NotImplementedError - end - - - # method that returns the length of the singly linked list - 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 - def get_at_index(index) - raise NotImplementedError - end - - # method to print all the values in the linked list - def visit - raise NotImplementedError - end - - # method to delete the first node found with specified value - 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 - def reverse - raise NotImplementedError - end - - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - 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 - 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. - def has_cycle - raise NotImplementedError - end - - - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - def get_first - raise NotImplementedError - end - - # method that inserts a given value as a new last node in the linked list - 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 - def get_last - raise NotImplementedError + 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 + def add_first(value) + @head = Node.new(value, @head) + end + + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + def search(value) + return false if @head.nil? + curr = @head + until curr.next.nil? + return true if curr.data == value + curr = curr.next + end + return curr.data == value + end + + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + return nil if @head.nil? + max = @head.data + curr = @head.next + until curr.nil? + max = curr.data if max < curr.data + curr = curr.next + end + return max + end + + # method to return the min value in the linked list + # returns the data value and not the node + def find_min + return nil if @head.nil? + min = @head.data + curr = @head.next + until curr.nil? + min = curr.data if min > curr.data + curr = curr.next + end + return min + end + + + # method that returns the length of the singly linked list + def length + count = 0 + return count if @head.nil? + curr = @head + until curr.nil? + count += 1 + curr = curr.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 + def get_at_index(index) + return nil if index < 0 || index >= self.length + curr = @head + index.times do + curr = curr.next + end + return curr.data + end + + # method to print all the values in the linked list + def visit + return nil if @head.nil? + curr = @head + until curr.nil? + p curr.data + curr = curr.next 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 + end + + # method to delete the first node found with specified value + def delete(value) + return nil if @head.nil? + if value == @head.data + @head = @head.next + else + prev = @head + curr = @head.next + until curr.data == value || curr.next.nil? + prev = curr + curr = curr.next + end + if curr.data == value + prev.next = curr.next + end 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 + + # method to reverse the singly linked list + # note: the nodes should be moved and not just the values in the nodes + def reverse + return nil if @head.nil? + curr = @head + prev = nil + until curr.nil? + temp = curr.next + curr.next = prev + prev = curr + curr = temp + end + @head = prev + end + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + def find_middle_value + return nil if @head.nil? + return self.get_at_index(self.length/2) + 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) + return nil if @head.nil? || n > self.length - 1 + curr = @head + res = @head + n.times do + curr = curr.next + end + until curr.next.nil? + curr = curr.next + res = res.next + end + return res.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 + end + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + def get_first + return @head.nil? ? nil : @head.data + end + + # method that inserts a given value as a new last node in the linked list + def add_last(value) + if @head.nil? + self.add_first(value) + else + curr = @head + until curr.next.nil? + curr = curr.next end - - current.next = @head # make the last node link to first node + curr.next = Node.new(value) 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 + return nil if @head.nil? + self.get_at_index(self.length - 1) + 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 + 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 From 75653afd6be24e42335abe48a1f6ce45e9153061 Mon Sep 17 00:00:00 2001 From: Xinran Che Date: Tue, 18 Feb 2020 00:06:57 -0800 Subject: [PATCH 2/4] Fixed indentation --- lib/linked_list.rb | 400 ++++++++++++++++++++++----------------------- 1 file changed, 200 insertions(+), 200 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 034624dc..61bc0c8a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,208 +1,208 @@ # 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 + 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 - def add_first(value) - @head = Node.new(value, @head) - end - - # method to find if the linked list contains a node with specified value - # returns true if found, false otherwise - def search(value) - return false if @head.nil? - curr = @head - until curr.next.nil? - return true if curr.data == value - curr = curr.next - end - return curr.data == value - end - - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - return nil if @head.nil? - max = @head.data - curr = @head.next - until curr.nil? - max = curr.data if max < curr.data - curr = curr.next - end - return max - end - - # method to return the min value in the linked list - # returns the data value and not the node - def find_min - return nil if @head.nil? - min = @head.data - curr = @head.next - until curr.nil? - min = curr.data if min > curr.data - curr = curr.next - end - return min - end - - - # method that returns the length of the singly linked list - def length - count = 0 - return count if @head.nil? - curr = @head - until curr.nil? - count += 1 - curr = curr.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 - def get_at_index(index) - return nil if index < 0 || index >= self.length - curr = @head - index.times do - curr = curr.next - end - return curr.data - end - - # method to print all the values in the linked list - def visit - return nil if @head.nil? - curr = @head - until curr.nil? - p curr.data - curr = curr.next - end - end - - # method to delete the first node found with specified value - def delete(value) - return nil if @head.nil? - if value == @head.data - @head = @head.next - else - prev = @head - curr = @head.next - until curr.data == value || curr.next.nil? - prev = curr - curr = curr.next - end - if curr.data == value - prev.next = curr.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 - return nil if @head.nil? - curr = @head - prev = nil - until curr.nil? - temp = curr.next - curr.next = prev - prev = curr - curr = temp - end - @head = prev - end - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - def find_middle_value - return nil if @head.nil? - return self.get_at_index(self.length/2) - 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) - return nil if @head.nil? || n > self.length - 1 - curr = @head - res = @head - n.times do - curr = curr.next - end - until curr.next.nil? - curr = curr.next - res = res.next - end - return res.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 - end - - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - def get_first - return @head.nil? ? nil : @head.data - end - - # method that inserts a given value as a new last node in the linked list - def add_last(value) - if @head.nil? - self.add_first(value) - else - curr = @head - until curr.next.nil? - curr = curr.next - end - curr.next = Node.new(value) - 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 - return nil if @head.nil? - self.get_at_index(self.length - 1) - 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 - 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 + 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 + def add_first(value) + @head = Node.new(value, @head) + end + + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + def search(value) + return false if @head.nil? + curr = @head + until curr.next.nil? + return true if curr.data == value + curr = curr.next + end + return curr.data == value + end + + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + return nil if @head.nil? + max = @head.data + curr = @head.next + until curr.nil? + max = curr.data if max < curr.data + curr = curr.next + end + return max + end + + # method to return the min value in the linked list + # returns the data value and not the node + def find_min + return nil if @head.nil? + min = @head.data + curr = @head.next + until curr.nil? + min = curr.data if min > curr.data + curr = curr.next + end + return min + end + + + # method that returns the length of the singly linked list + def length + count = 0 + return count if @head.nil? + curr = @head + until curr.nil? + count += 1 + curr = curr.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 + def get_at_index(index) + return nil if index < 0 || index >= self.length + curr = @head + index.times do + curr = curr.next + end + return curr.data + end + + # method to print all the values in the linked list + def visit + return nil if @head.nil? + curr = @head + until curr.nil? + p curr.data + curr = curr.next + end + end + + # method to delete the first node found with specified value + def delete(value) + return nil if @head.nil? + if value == @head.data + @head = @head.next + else + prev = @head + curr = @head.next + until curr.data == value || curr.next.nil? + prev = curr + curr = curr.next + end + if curr.data == value + prev.next = curr.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 + return nil if @head.nil? + curr = @head + prev = nil + until curr.nil? + temp = curr.next + curr.next = prev + prev = curr + curr = temp + end + @head = prev + end + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + def find_middle_value + return nil if @head.nil? + return self.get_at_index(self.length/2) + 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) + return nil if @head.nil? || n > self.length - 1 + curr = @head + res = @head + n.times do + curr = curr.next + end + until curr.next.nil? + curr = curr.next + res = res.next + end + return res.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 + end + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + def get_first + return @head.nil? ? nil : @head.data + end + + # method that inserts a given value as a new last node in the linked list + def add_last(value) + if @head.nil? + self.add_first(value) + else + curr = @head + until curr.next.nil? + curr = curr.next + end + curr.next = Node.new(value) + 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 + return nil if @head.nil? + self.get_at_index(self.length - 1) + 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 + 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 From 867d2cfaa54fd52414172a2e7554cbba69da3e5a Mon Sep 17 00:00:00 2001 From: Xinran Che Date: Tue, 18 Feb 2020 00:16:35 -0800 Subject: [PATCH 3/4] Fixed indentation --- test/linked_list_test.rb | 434 +++++++++++++++++++-------------------- 1 file changed, 217 insertions(+), 217 deletions(-) diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..e9d7f266 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -8,221 +8,221 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe LinkedList do - # Arrange - before do - @list = LinkedList.new - end - - describe 'initialize' do - it 'can be created' do - - # Assert - expect(@list).must_be_kind_of LinkedList - end - end - - describe 'add_first & get_first' do - it 'can add values to an empty list' do - # Act - @list.add_first(3) - - # Assert - expect(@list.get_first).must_equal 3 - end - - it 'will put the last added item to the front of the list' do - # Act - @list.add_first(1) - @list.add_first(2) - - # Assert - expect(@list.get_first).must_equal 2 - - # Act again - @list.add_first(3) - - # Assert - expect(@list.get_first).must_equal 3 - end - - it 'will return `nil` for `getFirst` if the list is empty' do - - expect(@list.get_first).must_be_nil - end - end - - describe "search" do - it "can find an element" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - - expect(@list.search(3)).must_equal true - - expect(@list.search(2)).must_equal true - end - - it "returns false if the element is not in the list" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - expect(@list.search("pasta")).must_equal false - end - - it "returns false for an empty list" do - expect(@list.search(3)).must_equal false - end - end - - describe "length" do - it "will return 0 for an empty list" do - expect(@list.length).must_equal 0 - end - - it "will return the length for nonempty lists" do - count = 0 - while count < 5 - @list.add_first(count) - count += 1 - expect(@list.length).must_equal count - end - end - end - - describe "addLast & getLast" do - it "will add to the front if the list is empty" do - @list.add_last(1) - expect(@list.get_first).must_equal 1 - end - - it "will put new items to the rear of the list" do - @list.add_last(2) - expect(@list.length).must_equal 1 - expect(@list.get_last).must_equal 2 - - @list.add_last(3) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 3 - expect(@list.length).must_equal 2 - - @list.add_last(4) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 4 - expect(@list.length).must_equal 3 - end - end - - describe 'get_at_index' do - it 'returns nil if the index is outside the bounds of the list' do - expect(@list.get_at_index(3)).must_be_nil - end - - it 'can retrieve an item at an index in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - expect(@list.get_at_index(0)).must_equal 4 - expect(@list.get_at_index(1)).must_equal 3 - expect(@list.get_at_index(2)).must_equal 2 - expect(@list.get_at_index(3)).must_equal 1 - end - end - - describe 'max and min values' do - it 'returns nil if the list is empty' do - expect(@list.find_max()).must_be_nil - expect(@list.find_min()).must_be_nil - end - - it 'can retrieve the max and min values in the list' do - count = 0 - while count < 5 - @list.add_first(count) - expect(@list.find_max).must_equal count - expect(@list.find_min).must_equal 0 - count += 1 - end - - @list.add_last(100) - @list.add_first(-12) - expect(@list.find_max).must_equal 100 - expect(@list.find_min).must_equal(-12) - end - end - - describe "delete" do - it "delete from empty linked list is a no-op" do - expect(@list.length).must_equal 0 - @list.delete(4) - expect(@list.length).must_equal 0 - end - - it "can delete valid values from list" do - @list.add_last(9) - @list.add_last(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - - # delete fist node (requires updating head) - @list.delete(2) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 4 - expect(@list.get_last).must_equal 10 - expect(@list.find_max).must_equal 10 - expect(@list.find_min).must_equal 3 - - # delete last node - @list.delete(10) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 3 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - - # delete fist node (requires updating head) - @list.delete(4) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 2 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - end - end - - describe "nth_from_the_end" do - it 'returns nil if n is outside the bounds of the list' do - expect(@list.find_nth_from_end(3)).must_be_nil - end - - it 'can retrieve an item at index n from the end in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - expect(@list.find_nth_from_end(4)).must_be_nil - end - end - - describe "reverse" do - it 'can retrieve an item at index n from the end in the list' do - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - @list.add_first(1) - @list.reverse - - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - end - end + # Arrange + before do + @list = LinkedList.new + end + + describe 'initialize' do + it 'can be created' do + + # Assert + expect(@list).must_be_kind_of LinkedList + end + end + + describe 'add_first & get_first' do + it 'can add values to an empty list' do + # Act + @list.add_first(3) + + # Assert + expect(@list.get_first).must_equal 3 + end + + it 'will put the last added item to the front of the list' do + # Act + @list.add_first(1) + @list.add_first(2) + + # Assert + expect(@list.get_first).must_equal 2 + + # Act again + @list.add_first(3) + + # Assert + expect(@list.get_first).must_equal 3 + end + + it 'will return `nil` for `getFirst` if the list is empty' do + + expect(@list.get_first).must_be_nil + end + end + + describe "search" do + it "can find an element" do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + + expect(@list.search(3)).must_equal true + + expect(@list.search(2)).must_equal true + end + + it "returns false if the element is not in the list" do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + expect(@list.search("pasta")).must_equal false + end + + it "returns false for an empty list" do + expect(@list.search(3)).must_equal false + end + end + + describe "length" do + it "will return 0 for an empty list" do + expect(@list.length).must_equal 0 + end + + it "will return the length for nonempty lists" do + count = 0 + while count < 5 + @list.add_first(count) + count += 1 + expect(@list.length).must_equal count + end + end + end + + describe "addLast & getLast" do + it "will add to the front if the list is empty" do + @list.add_last(1) + expect(@list.get_first).must_equal 1 + end + + it "will put new items to the rear of the list" do + @list.add_last(2) + expect(@list.length).must_equal 1 + expect(@list.get_last).must_equal 2 + + @list.add_last(3) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 3 + expect(@list.length).must_equal 2 + + @list.add_last(4) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 4 + expect(@list.length).must_equal 3 + end + end + + describe 'get_at_index' do + it 'returns nil if the index is outside the bounds of the list' do + expect(@list.get_at_index(3)).must_be_nil + end + + it 'can retrieve an item at an index in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + expect(@list.get_at_index(0)).must_equal 4 + expect(@list.get_at_index(1)).must_equal 3 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(3)).must_equal 1 + end + end + + describe 'max and min values' do + it 'returns nil if the list is empty' do + expect(@list.find_max()).must_be_nil + expect(@list.find_min()).must_be_nil + end + + it 'can retrieve the max and min values in the list' do + count = 0 + while count < 5 + @list.add_first(count) + expect(@list.find_max).must_equal count + expect(@list.find_min).must_equal 0 + count += 1 + end + + @list.add_last(100) + @list.add_first(-12) + expect(@list.find_max).must_equal 100 + expect(@list.find_min).must_equal(-12) + end + end + + describe "delete" do + it "delete from empty linked list is a no-op" do + expect(@list.length).must_equal 0 + @list.delete(4) + expect(@list.length).must_equal 0 + end + + it "can delete valid values from list" do + @list.add_last(9) + @list.add_last(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + # delete fist node (requires updating head) + @list.delete(2) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 4 + expect(@list.get_last).must_equal 10 + expect(@list.find_max).must_equal 10 + expect(@list.find_min).must_equal 3 + + # delete last node + @list.delete(10) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 3 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + + # delete fist node (requires updating head) + @list.delete(4) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 2 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + end + end + + describe "nth_from_the_end" do + it 'returns nil if n is outside the bounds of the list' do + expect(@list.find_nth_from_end(3)).must_be_nil + end + + it 'can retrieve an item at index n from the end in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + expect(@list.find_nth_from_end(4)).must_be_nil + end + end + + describe "reverse" do + it 'can retrieve an item at index n from the end in the list' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.reverse + + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + end + end end From 25afe8fb1f4cd235e49f1ba2f92a5f7a68a7eb1b Mon Sep 17 00:00:00 2001 From: Xinran Che Date: Tue, 18 Feb 2020 00:25:04 -0800 Subject: [PATCH 4/4] Fixed indentation --- lib/linked_list.rb | 400 ++++++++++++++++++------------------ test/linked_list_test.rb | 434 +++++++++++++++++++-------------------- 2 files changed, 417 insertions(+), 417 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 61bc0c8a..034624dc 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,208 +1,208 @@ # 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 + 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 - def add_first(value) - @head = Node.new(value, @head) - end - - # method to find if the linked list contains a node with specified value - # returns true if found, false otherwise - def search(value) - return false if @head.nil? - curr = @head - until curr.next.nil? - return true if curr.data == value - curr = curr.next - end - return curr.data == value - end - - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - return nil if @head.nil? - max = @head.data - curr = @head.next - until curr.nil? - max = curr.data if max < curr.data - curr = curr.next - end - return max - end - - # method to return the min value in the linked list - # returns the data value and not the node - def find_min - return nil if @head.nil? - min = @head.data - curr = @head.next - until curr.nil? - min = curr.data if min > curr.data - curr = curr.next - end - return min - end - - - # method that returns the length of the singly linked list - def length - count = 0 - return count if @head.nil? - curr = @head - until curr.nil? - count += 1 - curr = curr.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 - def get_at_index(index) - return nil if index < 0 || index >= self.length - curr = @head - index.times do - curr = curr.next - end - return curr.data - end - - # method to print all the values in the linked list - def visit - return nil if @head.nil? - curr = @head - until curr.nil? - p curr.data - curr = curr.next - end - end - - # method to delete the first node found with specified value - def delete(value) - return nil if @head.nil? - if value == @head.data - @head = @head.next - else - prev = @head - curr = @head.next - until curr.data == value || curr.next.nil? - prev = curr - curr = curr.next - end - if curr.data == value - prev.next = curr.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 - return nil if @head.nil? - curr = @head - prev = nil - until curr.nil? - temp = curr.next - curr.next = prev - prev = curr - curr = temp - end - @head = prev - end - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - def find_middle_value - return nil if @head.nil? - return self.get_at_index(self.length/2) - 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) - return nil if @head.nil? || n > self.length - 1 - curr = @head - res = @head - n.times do - curr = curr.next - end - until curr.next.nil? - curr = curr.next - res = res.next - end - return res.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 - end - - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - def get_first - return @head.nil? ? nil : @head.data - end - - # method that inserts a given value as a new last node in the linked list - def add_last(value) - if @head.nil? - self.add_first(value) - else - curr = @head - until curr.next.nil? - curr = curr.next - end - curr.next = Node.new(value) - 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 - return nil if @head.nil? - self.get_at_index(self.length - 1) - 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 - 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 + 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 + def add_first(value) + @head = Node.new(value, @head) + end + + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + def search(value) + return false if @head.nil? + curr = @head + until curr.next.nil? + return true if curr.data == value + curr = curr.next + end + return curr.data == value + end + + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + return nil if @head.nil? + max = @head.data + curr = @head.next + until curr.nil? + max = curr.data if max < curr.data + curr = curr.next + end + return max + end + + # method to return the min value in the linked list + # returns the data value and not the node + def find_min + return nil if @head.nil? + min = @head.data + curr = @head.next + until curr.nil? + min = curr.data if min > curr.data + curr = curr.next + end + return min + end + + + # method that returns the length of the singly linked list + def length + count = 0 + return count if @head.nil? + curr = @head + until curr.nil? + count += 1 + curr = curr.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 + def get_at_index(index) + return nil if index < 0 || index >= self.length + curr = @head + index.times do + curr = curr.next + end + return curr.data + end + + # method to print all the values in the linked list + def visit + return nil if @head.nil? + curr = @head + until curr.nil? + p curr.data + curr = curr.next + end + end + + # method to delete the first node found with specified value + def delete(value) + return nil if @head.nil? + if value == @head.data + @head = @head.next + else + prev = @head + curr = @head.next + until curr.data == value || curr.next.nil? + prev = curr + curr = curr.next + end + if curr.data == value + prev.next = curr.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 + return nil if @head.nil? + curr = @head + prev = nil + until curr.nil? + temp = curr.next + curr.next = prev + prev = curr + curr = temp + end + @head = prev + end + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + def find_middle_value + return nil if @head.nil? + return self.get_at_index(self.length/2) + 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) + return nil if @head.nil? || n > self.length - 1 + curr = @head + res = @head + n.times do + curr = curr.next + end + until curr.next.nil? + curr = curr.next + res = res.next + end + return res.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 + end + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + def get_first + return @head.nil? ? nil : @head.data + end + + # method that inserts a given value as a new last node in the linked list + def add_last(value) + if @head.nil? + self.add_first(value) + else + curr = @head + until curr.next.nil? + curr = curr.next + end + curr.next = Node.new(value) + 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 + return nil if @head.nil? + self.get_at_index(self.length - 1) + 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 + 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/test/linked_list_test.rb b/test/linked_list_test.rb index e9d7f266..bac48b55 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -8,221 +8,221 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe LinkedList do - # Arrange - before do - @list = LinkedList.new - end - - describe 'initialize' do - it 'can be created' do - - # Assert - expect(@list).must_be_kind_of LinkedList - end - end - - describe 'add_first & get_first' do - it 'can add values to an empty list' do - # Act - @list.add_first(3) - - # Assert - expect(@list.get_first).must_equal 3 - end - - it 'will put the last added item to the front of the list' do - # Act - @list.add_first(1) - @list.add_first(2) - - # Assert - expect(@list.get_first).must_equal 2 - - # Act again - @list.add_first(3) - - # Assert - expect(@list.get_first).must_equal 3 - end - - it 'will return `nil` for `getFirst` if the list is empty' do - - expect(@list.get_first).must_be_nil - end - end - - describe "search" do - it "can find an element" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - - expect(@list.search(3)).must_equal true - - expect(@list.search(2)).must_equal true - end - - it "returns false if the element is not in the list" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - expect(@list.search("pasta")).must_equal false - end - - it "returns false for an empty list" do - expect(@list.search(3)).must_equal false - end - end - - describe "length" do - it "will return 0 for an empty list" do - expect(@list.length).must_equal 0 - end - - it "will return the length for nonempty lists" do - count = 0 - while count < 5 - @list.add_first(count) - count += 1 - expect(@list.length).must_equal count - end - end - end - - describe "addLast & getLast" do - it "will add to the front if the list is empty" do - @list.add_last(1) - expect(@list.get_first).must_equal 1 - end - - it "will put new items to the rear of the list" do - @list.add_last(2) - expect(@list.length).must_equal 1 - expect(@list.get_last).must_equal 2 - - @list.add_last(3) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 3 - expect(@list.length).must_equal 2 - - @list.add_last(4) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 4 - expect(@list.length).must_equal 3 - end - end - - describe 'get_at_index' do - it 'returns nil if the index is outside the bounds of the list' do - expect(@list.get_at_index(3)).must_be_nil - end - - it 'can retrieve an item at an index in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - expect(@list.get_at_index(0)).must_equal 4 - expect(@list.get_at_index(1)).must_equal 3 - expect(@list.get_at_index(2)).must_equal 2 - expect(@list.get_at_index(3)).must_equal 1 - end - end - - describe 'max and min values' do - it 'returns nil if the list is empty' do - expect(@list.find_max()).must_be_nil - expect(@list.find_min()).must_be_nil - end - - it 'can retrieve the max and min values in the list' do - count = 0 - while count < 5 - @list.add_first(count) - expect(@list.find_max).must_equal count - expect(@list.find_min).must_equal 0 - count += 1 - end - - @list.add_last(100) - @list.add_first(-12) - expect(@list.find_max).must_equal 100 - expect(@list.find_min).must_equal(-12) - end - end - - describe "delete" do - it "delete from empty linked list is a no-op" do - expect(@list.length).must_equal 0 - @list.delete(4) - expect(@list.length).must_equal 0 - end - - it "can delete valid values from list" do - @list.add_last(9) - @list.add_last(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - - # delete fist node (requires updating head) - @list.delete(2) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 4 - expect(@list.get_last).must_equal 10 - expect(@list.find_max).must_equal 10 - expect(@list.find_min).must_equal 3 - - # delete last node - @list.delete(10) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 3 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - - # delete fist node (requires updating head) - @list.delete(4) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 2 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - end - end - - describe "nth_from_the_end" do - it 'returns nil if n is outside the bounds of the list' do - expect(@list.find_nth_from_end(3)).must_be_nil - end - - it 'can retrieve an item at index n from the end in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - expect(@list.find_nth_from_end(4)).must_be_nil - end - end - - describe "reverse" do - it 'can retrieve an item at index n from the end in the list' do - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - @list.add_first(1) - @list.reverse - - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - end - end + # Arrange + before do + @list = LinkedList.new + end + + describe 'initialize' do + it 'can be created' do + + # Assert + expect(@list).must_be_kind_of LinkedList + end + end + + describe 'add_first & get_first' do + it 'can add values to an empty list' do + # Act + @list.add_first(3) + + # Assert + expect(@list.get_first).must_equal 3 + end + + it 'will put the last added item to the front of the list' do + # Act + @list.add_first(1) + @list.add_first(2) + + # Assert + expect(@list.get_first).must_equal 2 + + # Act again + @list.add_first(3) + + # Assert + expect(@list.get_first).must_equal 3 + end + + it 'will return `nil` for `getFirst` if the list is empty' do + + expect(@list.get_first).must_be_nil + end + end + + describe "search" do + it "can find an element" do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + + expect(@list.search(3)).must_equal true + + expect(@list.search(2)).must_equal true + end + + it "returns false if the element is not in the list" do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + expect(@list.search("pasta")).must_equal false + end + + it "returns false for an empty list" do + expect(@list.search(3)).must_equal false + end + end + + describe "length" do + it "will return 0 for an empty list" do + expect(@list.length).must_equal 0 + end + + it "will return the length for nonempty lists" do + count = 0 + while count < 5 + @list.add_first(count) + count += 1 + expect(@list.length).must_equal count + end + end + end + + describe "addLast & getLast" do + it "will add to the front if the list is empty" do + @list.add_last(1) + expect(@list.get_first).must_equal 1 + end + + it "will put new items to the rear of the list" do + @list.add_last(2) + expect(@list.length).must_equal 1 + expect(@list.get_last).must_equal 2 + + @list.add_last(3) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 3 + expect(@list.length).must_equal 2 + + @list.add_last(4) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 4 + expect(@list.length).must_equal 3 + end + end + + describe 'get_at_index' do + it 'returns nil if the index is outside the bounds of the list' do + expect(@list.get_at_index(3)).must_be_nil + end + + it 'can retrieve an item at an index in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + expect(@list.get_at_index(0)).must_equal 4 + expect(@list.get_at_index(1)).must_equal 3 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(3)).must_equal 1 + end + end + + describe 'max and min values' do + it 'returns nil if the list is empty' do + expect(@list.find_max()).must_be_nil + expect(@list.find_min()).must_be_nil + end + + it 'can retrieve the max and min values in the list' do + count = 0 + while count < 5 + @list.add_first(count) + expect(@list.find_max).must_equal count + expect(@list.find_min).must_equal 0 + count += 1 + end + + @list.add_last(100) + @list.add_first(-12) + expect(@list.find_max).must_equal 100 + expect(@list.find_min).must_equal(-12) + end + end + + describe "delete" do + it "delete from empty linked list is a no-op" do + expect(@list.length).must_equal 0 + @list.delete(4) + expect(@list.length).must_equal 0 + end + + it "can delete valid values from list" do + @list.add_last(9) + @list.add_last(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + # delete fist node (requires updating head) + @list.delete(2) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 4 + expect(@list.get_last).must_equal 10 + expect(@list.find_max).must_equal 10 + expect(@list.find_min).must_equal 3 + + # delete last node + @list.delete(10) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 3 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + + # delete fist node (requires updating head) + @list.delete(4) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 2 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + end + end + + describe "nth_from_the_end" do + it 'returns nil if n is outside the bounds of the list' do + expect(@list.find_nth_from_end(3)).must_be_nil + end + + it 'can retrieve an item at index n from the end in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + expect(@list.find_nth_from_end(4)).must_be_nil + end + end + + describe "reverse" do + it 'can retrieve an item at index n from the end in the list' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.reverse + + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + end + end end