From 0c351b68d5c90461e2c3c5ccb70dbd476c0f3552 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 17 Feb 2020 21:39:19 -0800 Subject: [PATCH 1/2] this won't be fully finished until wednesday night --- lib/linked_list.rb | 78 ++++++++++++++++++++++++++++++++++------ test/linked_list_test.rb | 20 ++++++----- 2 files changed, 79 insertions(+), 19 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8dee5e8d..9ff17bb6 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,4 @@ - +require 'pry' # Defines a node in the singly linked list class Node attr_reader :data # allow external entities to read value but not write @@ -19,38 +19,82 @@ 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 + new_node = Node.new(value) + + new_node.next = @head + + @head = new_node + # head is not a node, it is just a variable that refers to the first node 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 + current = @head + + until current == nil + return true if current.data == value + 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 def find_max - raise NotImplementedError + return nil if @head == nil + + current = @head + max = current.data + + + until current == nil + max = current.data if current.data > max + current = current.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 - raise NotImplementedError - end + return nil if @head == nil + + current = @head + min = current.data + + + until current == nil + min = current.data if current.data < min + current = current.next + end + return min + end # method that returns the length of the singly linked list def length - raise NotImplementedError + + count = 0 + current = @head + # current + + until current == nil + count += 1 + current = current.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) - raise NotImplementedError + + end # method to print all the values in the linked list @@ -94,12 +138,26 @@ 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 == nil + + @head.data end # method that inserts a given value as a new last node in the linked list def add_last(value) - raise NotImplementedError + return nil if @head == nil + + current = @head + + last_node = '' + new_node = Node.new(value) + + until current == nil + last_node = current + current = current.next + end + + last_node.next = new_node end # method that returns the value of the last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index d169c9a0..93b1f9a5 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -5,6 +5,7 @@ require_relative 'test_helper' + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe LinkedList do @@ -13,7 +14,7 @@ @list = LinkedList.new end - describe 'initialize' do + xdescribe 'initialize' do it 'can be created' do # Assert @@ -21,11 +22,11 @@ end end - describe 'add_first & get_first' do + xdescribe '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 @@ -51,7 +52,7 @@ end end - describe "search" do + xdescribe "search" do it "can find an element" do @list = LinkedList.new @list.add_first(3) @@ -60,6 +61,7 @@ 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 @@ -74,7 +76,7 @@ end end - describe "length" do + xdescribe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -89,7 +91,7 @@ end end - describe "addLast & getLast" do + xdescribe "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 @@ -152,7 +154,7 @@ end end - describe "delete" do + xdescribe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) @@ -192,7 +194,7 @@ end end - describe "nth_from_the_end" do + xdescribe "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 @@ -211,7 +213,7 @@ end end - describe "reverse" do + xdescribe "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) From 69e2d9cb2a0e4de630bad519e17d35f34c29fd14 Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 19 Feb 2020 20:57:16 -0800 Subject: [PATCH 2/2] latest commit --- lib/linked_list.rb | 101 +++++++++++++++++++++++++++++++-------- test/linked_list_test.rb | 25 +++++++--- 2 files changed, 100 insertions(+), 26 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9ff17bb6..bb87cd13 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -76,41 +76,97 @@ def find_min # method that returns the length of the singly linked list def length + return 0 if @head == nil - count = 0 + length = 0 current = @head # current until current == nil - count += 1 + length += 1 current = current.next end - return count + 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) - + + current = @head + value = '' + + return current.data if index == 0 + new_index = index + 1 + + new_index.times do |count| + return nil if current == nil + value = current.data + current = current.next + end + + return value end # method to print all the values in the linked list def visit - raise NotImplementedError + linked_list_array = [] + + current = @head + + until current == nil + linked_list_array.push(current.data) + current = current.next + end + + return linked_list_array end # method to delete the first node found with specified value def delete(value) - raise NotImplementedError + + + return nil if @head == nil + + current = @head + + if current.data == value + @head = current.next + return + end + + until current == nil + if current.next.data == value + current.next == current.next.next + return + end + + current = current.next + end + + return 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 + previous = nil + + until current.next.nil? + temp = current.next + current.next = previous + previous = current + current = temp + end + + current.next = previous + @head = current end @@ -140,30 +196,37 @@ def has_cycle def get_first return nil if @head == nil - @head.data + return @head.data end # method that inserts a given value as a new last node in the linked list def add_last(value) - return nil if @head == nil - - current = @head - last_node = '' new_node = Node.new(value) - until current == nil - last_node = current - current = current.next - end - - last_node.next = new_node + if @head.nil? + add_first(value) + else + current = @head + while current.next != nil + current = current.next + end + current.next = new_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 + return nil if @head.nil? + + 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 diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 93b1f9a5..c30d9475 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -14,7 +14,7 @@ @list = LinkedList.new end - xdescribe 'initialize' do + describe 'initialize' do it 'can be created' do # Assert @@ -22,7 +22,7 @@ end end - xdescribe 'add_first & get_first' do + describe 'add_first & get_first' do it 'can add values to an empty list' do # Act @list.add_first(3) @@ -52,7 +52,7 @@ end end - xdescribe "search" do + describe "search" do it "can find an element" do @list = LinkedList.new @list.add_first(3) @@ -76,7 +76,7 @@ end end - xdescribe "length" do + describe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -91,7 +91,7 @@ end end - xdescribe "addLast & getLast" do + 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 @@ -154,7 +154,7 @@ end end - xdescribe "delete" do + describe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) @@ -213,7 +213,18 @@ end end - xdescribe "reverse" do + describe "visit" do + it 'can print all the values in the linked list' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + expect(@list.visit).must_equal [1, 2, 3, 4] + 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)