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 - Julia Bouvier #35

Open
wants to merge 8 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
163 changes: 147 additions & 16 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,99 +19,229 @@ 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
@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)

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
max = current
return nil if current.nil?

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

until current.nil?
min = current if current.data < min.data
current = current.next
end
return min.data
end


# method that returns the length of the singly linked list
def length
raise NotImplementedError
current = @head
length = 0
return 0 if current.nil?

Choose a reason for hiding this comment

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

Do you need this line?


until current.nil?
current = current.next
length += 1
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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
current_index = 0

index.times do |i|
return nil if current.nil?
current = current.next
end

return current.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
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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
previous = nil

return nil if current.nil?
until current.data == value
previous = current
current = current.next
end

if previous.nil?
@head = current.next
else
previous.next = current.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 = @head
previous = nil

until current.nil?
temp = current
current = current.next
temp.next = previous
previous = temp
end
@head = previous
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.

👍

raise NotImplementedError
current = @head
middle = current

return nil if current.nil?
until current.nil?
middle.next = middle
current.next.next = current
end
return middle
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
current = @head
from_end = current

return nil if current.nil?

n.times do
return nil if current.next.nil?
current = current.next
end

until current.next.nil?
return from_end.data if from_end.next.nil?
from_end = from_end.next
current = current.next
end
return from_end.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
slow = @head
fast = @head

until fast.nil?
slow = slow.next
fast = fast.next.next

Choose a reason for hiding this comment

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

Just in case fast.next is nil.

Suggested change
fast = fast.next.next
fast = fast.next.nil? ? fast.next : fast.next.next

return true if slow == fast
end
return false
end


# Additional Exercises
# 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
current = @head

return nil if current.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
current = @head

if current.nil?
@head = Node.new(value)
return
end

until current.next.nil?
current = current.next
end

current.next = Node.new(value)
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
current = @head

return nil if current.nil?
until 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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head

if current.nil?
@head = Node.new(value)
end

until current.nil?
if value.data > last.data && value.data < current.data
new_node = Node.new(value, current.next)
current.next = new_node
return
else
current = current.next
end
end
end

# Helper method for tests
Expand All @@ -129,3 +259,4 @@ def create_cycle
current.next = @head # make the last node link to first node
end
end

1 change: 0 additions & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative 'test_helper'

Expand Down