From 1e0d25d6349130cb4c59175e656d98c8cc08b63e Mon Sep 17 00:00:00 2001 From: Eve Le Date: Mon, 17 Feb 2020 21:57:15 -0800 Subject: [PATCH 1/4] Implemented singly linked list methods and added more tests --- lib/linked_list.rb | 157 +++++++++++++++++++++++++++++++++++---- test/linked_list_test.rb | 84 +++++++++++++++++++++ 2 files changed, 225 insertions(+), 16 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..7485d324 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,74 +19,142 @@ 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 def add_first(value) - raise NotImplementedError + @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) - raise NotImplementedError + return find_node_and_previous_node(value).first end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + current = @head + if current.nil? + return nil + else + max = current.data + while !current.next.nil? + current = current.next + max = current.data if (current.data > max) + end + return max + end 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 + else + min = current.data + while !current.next.nil? + current = current.next + min = current.data if (current.data < min) + end + return min + end end # method that returns the length of the singly linked list def length - raise NotImplementedError + length = 0 + current = @head + 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 # 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 + return nil if index > (length() - 1) || index < 0 + + current = @head + index.times do + current = current.next + end + return current.data end # method to print all the values in the linked list def visit - raise NotImplementedError + current = @head + while current + puts current.data + current = current.next + end end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + node_and_previous_node_pair = find_node_and_previous_node(value) + if node_and_previous_node_pair.first + previous = node_and_previous_node_pair[1] + current = node_and_previous_node_pair[0] + + if !previous + @head = current.next + else + previous.next = current.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 + previous = nil + current = @head + while current + next_node = current.next + + current.next = previous + previous = current + current = next_node + end + + @head = previous end ## Advanced Exercises # returns the value at the middle element in the singly linked list def find_middle_value - raise NotImplementedError + return get_at_index((length() - 1 )/ 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) - raise NotImplementedError + return get_at_index(length() - 1 - n) 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 + single = @head + if single && single.next && single.next.next + double = single.next.next + while single && double + return true if single == double + 2.times do + double = double.next + return false if !double + end + single = single.next + end + end + return false end @@ -94,24 +162,54 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty def get_first - raise NotImplementedError + return nil if !@head + return @head.data end # method that inserts a given value as a new last node in the linked list def add_last(value) - raise NotImplementedError + last_node = find_last_node + if last_node + last_node.next = Node.new(value) + else + @head = 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 - raise NotImplementedError + last_node = find_last_node() + if last_node + return last_node.data + else + return last_node + end 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 + new_node = Node.new(value) + if @head + current = @head + previous = nil + + while current + break if current.data >= value + previous = current + current = current.next + end + + if previous + previous.next = new_node + else + @head = new_node + end + new_node.next = current + else + @head = new_node + end end # Helper method for tests @@ -128,4 +226,31 @@ def create_cycle current.next = @head # make the last node link to first node end + + private + def find_last_node + current = @head + while current && current.next + current = current.next + end + return current + end + + def find_node_and_previous_node(value) + current = @head + + if current && current.data == value + return [current, nil] + end + + while current && current.next + previous = current + current = current.next + if current.data == value + return [current, previous] + end + end + + return [nil, nil] + end end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 22b55ef0..2a55d725 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -202,4 +202,88 @@ expect(@list.find_nth_from_end(3)).must_equal 4 end end + + describe "has cycle" do + it "returns false if list doesn't have cycle for list with even number of nodes" do + @list.add_first(2) + @list.add_first(1) + + expect(@list.has_cycle).must_equal false + end + + it "returns false if list doesn't have cycle for list with odd number of nodes" do + @list.add_first(2) + @list.add_first(1) + @list.add_first(0) + + expect(@list.has_cycle).must_equal false + end + + it "returns false if list is empty" do + expect(@list.has_cycle).must_equal false + end + + it "returns true if list has cycle for list with even number of nodes" do + @list.add_first(2) + @list.add_first(1) + @list.create_cycle() + + expect(@list.has_cycle).must_equal true + end + + it "returns true if list has cycle for list with odd number of nodes" do + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.create_cycle() + + expect(@list.has_cycle).must_equal true + end + end + + describe "insert_ascending" do + it "can insert to an empty list" do + @list.insert_ascending(1) + + expect(@list.get_first).must_equal 1 + end + + it "can insert to the fron of a sorted list" do + @list.add_first(2) + @list.insert_ascending(1) + + expect(@list.get_at_index(0)).must_equal 1 + expect(@list.get_at_index(1)).must_equal 2 + end + + it "can insert to the end of a sorted list" do + @list.add_first(1) + @list.insert_ascending(2) + + expect(@list.get_at_index(0)).must_equal 1 + expect(@list.get_at_index(1)).must_equal 2 + end + + it "can insert to the middle of a sorted list" do + @list.add_first(3) + @list.add_first(1) + @list.insert_ascending(2) + + expect(@list.get_at_index(0)).must_equal 1 + expect(@list.get_at_index(1)).must_equal 2 + expect(@list.get_at_index(2)).must_equal 3 + end + + it "can insert a duplicate value to a sorted list" do + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.insert_ascending(2) + + expect(@list.get_at_index(0)).must_equal 1 + expect(@list.get_at_index(1)).must_equal 2 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(3)).must_equal 3 + end + end end From 295e40569fc41bbbadf83a7f264b2d2aba2d6a72 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Mon, 17 Feb 2020 22:03:16 -0800 Subject: [PATCH 2/4] Fixed bug in search method --- 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 7485d324..4527391c 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -25,7 +25,7 @@ def add_first(value) # method to find if the linked list contains a node with specified value # returns true if found, false otherwise def search(value) - return find_node_and_previous_node(value).first + return find_node_and_previous_node(value).first != nil end # method to return the max value in the linked list From 46474f86b14a64eea4e5ab58199f6693a84a8b09 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Thu, 20 Feb 2020 12:00:15 -0800 Subject: [PATCH 3/4] Added tests for doubly linked list --- test/doubly_linked_list_test.rb | 158 ++++++++++++++++++++++++++++++++ test/test_helper.rb | 1 + 2 files changed, 159 insertions(+) create mode 100644 test/doubly_linked_list_test.rb diff --git a/test/doubly_linked_list_test.rb b/test/doubly_linked_list_test.rb new file mode 100644 index 00000000..200daf3b --- /dev/null +++ b/test/doubly_linked_list_test.rb @@ -0,0 +1,158 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative 'test_helper' + + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe DoublyLinkedList do + # Arrange + before do + @list = DoublyLinkedList.new + end + + describe 'initialize' do + it 'can be created' do + + # Assert + expect(@list).must_be_kind_of DoublyLinkedList + 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 "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 "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 + + # 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 + + # 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 + 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.get_at_index(3)).must_equal 1 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(1)).must_equal 3 + expect(@list.get_at_index(0)).must_equal 4 + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 039f0234..759aa4d7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,4 +5,5 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative '../lib/linked_list' +require_relative '../lib/doubly_linked_list' From 188aaea08d50d7c338154902b4dcaf84463b2c87 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Thu, 20 Feb 2020 12:00:51 -0800 Subject: [PATCH 4/4] Implemented doubly linked list --- lib/doubly_linked_list.rb | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 lib/doubly_linked_list.rb diff --git a/lib/doubly_linked_list.rb b/lib/doubly_linked_list.rb new file mode 100644 index 00000000..26b6fa5d --- /dev/null +++ b/lib/doubly_linked_list.rb @@ -0,0 +1,113 @@ +class DoublyNode + attr_reader :data + attr_accessor :next, :previous + + def initialize(value) + @data = value + @next = nil + @previous = nil + end +end + +class DoublyLinkedList + def initialize + @head = nil + @tail = nil + @size = 0 + end + + def length + return @size + # raise NotImplementedError + end + + def add_first(value) + new_node = DoublyNode.new(value) + + if @head + @head.previous = new_node + new_node.next = @head + else + @tail = new_node + end + @head = new_node + @size += 1 + end + + def add_last(value) + new_node = DoublyNode.new(value) + + if @tail + @tail.next = new_node + new_node.previous = @tail + else + @head = new_node + end + @tail = new_node + @size += 1 + end + + def get_first + return @head.data if @head + return @head + end + + def get_last + return @tail.data if @tail + return @tail + end + + def get_at_index(index) + if @head && index < @size && index >= 0 + current = @head + count = 0 + while count < index + current = current.next + count += 1 + end + return current.data + end + return nil + end + + def reverse + if @head + @tail = @head + current = @head + while current + previous_node = current.previous + current.previous = current.next + current.next = previous_node + @head = current + current = current.previous + end + end + end + + def delete(value) + current = @head + while current + if current.data == value + previous_node = current.previous + next_node = current.next + + if previous_node + previous_node.next = next_node + else + @head = next_node + end + + if next_node + next_node.previous = previous_node + else + @tail = previous_node + end + + @size -= 1 + break + end + current = current.next + end + end + +end \ No newline at end of file