Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches - Angele #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 130 additions & 14 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,166 @@ 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @head.nil?
@head = Node.new(value)
else
next_node = @head
@head = Node.new(value, next_node)
end
return @head
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return false if @head.nil?
return true if @head.data == value
if @head.next
checking_node = @head
found = false
end
until checking_node.next == nil || found == true
checking_node = checking_node.next
if checking_node.data == value
found = true
end
end
return found
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head.nil?
max = @head.data
checking_node = @head
until checking_node.next.nil?
checking_node = checking_node.next
if checking_node.data > max
max = checking_node.data
end
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head.nil?
min = @head.data
checking_node = @head
until checking_node.next.nil?
checking_node = checking_node.next
if checking_node.data < min
min = checking_node.data
end
end
return min
end


# method that returns the length of the singly linked list
def length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return 0 if @head.nil?
node_count = 1
checking_node = @head
until checking_node.next == nil
node_count += 1
checking_node = checking_node.next
end
return node_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
if index > self.length - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good use of the existing length method.

return nil
else
checking_node = @head
count = 0
until count == index
count += 1
checking_node = checking_node.next
end
end
return checking_node.data
end

# method to print all the values in the linked list
def visit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head.nil?
checking_node = @head
until checking_node.next == nil
puts checking_node.data
checking_node = checking_node.next
end
puts checking_node.data
end

# method to delete the first node found with specified value
def delete(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head.nil?
checking_node = @head
previous_node = nil
until checking_node.nil?
if checking_node.data == value
if checking_node == @head
@head = checking_node.next
else
previous_node.next = checking_node.next
end
end
previous_node = checking_node
checking_node = checking_node.next
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
current_node = @head
previous_node = nil
until current_node.next == nil
next_node = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next_node
end
@head = current_node
@head.next = previous_node
end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
def find_middle_value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good use of the existing length method.

raise NotImplementedError
return nil if @head.nil?
times_to_go = self.length / 2
current_node = @head
times_to_go.times do
current_node = current_node.next
end
return current_node.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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if n > self.length - 1
main_walker = @head
secondary_walker = @head
main_walker_counter = 0
until main_walker_counter == n
main_walker = main_walker.next
main_walker_counter += 1
end
until main_walker.next == nil
main_walker = main_walker.next
secondary_walker = secondary_walker.next
end
return secondary_walker.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -94,18 +193,35 @@ def has_cycle
# returns the value in the first node
# returns nil if the list is empty
def get_first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head.nil?
return @head.data
end

# method that inserts a given value as a new last node in the linked list
def add_last(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @head.nil?
add_first(value)
else
checking_node = @head
until checking_node.next.nil?
checking_node = checking_node.next
end
checking_node.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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @head.nil?
return nil
else
checking_node = @head
until checking_node.next.nil?
checking_node = checking_node.next
end
end
return checking_node.data
end

# method to insert a new node with specific data value, assuming the linked
Expand Down
14 changes: 13 additions & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'minitest/reporters'

require_relative 'test_helper'

Expand Down Expand Up @@ -225,4 +225,16 @@
expect(@list.find_nth_from_end(3)).must_equal 4
end
end

describe "find_middle_value" do
it 'can return the 5th item in a list of 10' do
count = 1
until count > 10
@list.add_first(count)
count += 1
end

expect(@list.find_middle_value()).must_equal 5
end
end
end