Skip to content

Branches - Monick #24

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
135 changes: 123 additions & 12 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,88 @@ 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)

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
raise NotImplementedError
return nil if @head.nil?
max = 0

Choose a reason for hiding this comment

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

What if it's a linked list of negative values, or non-integers?

Suggested change
max = 0
max = @head.data

current = @head

until current.nil?
if current.data > max
max = current.data
end
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

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

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


current = @head
until current.nil?
current = current.next
count += 1
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
return nil if @head.nil?

current = @head
count = 0

until count == index
current = current.next

Choose a reason for hiding this comment

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

What about if the length is less than index?

Suggested change
current = current.next
return nil if current.nil?
current = current.next

count += 1
end

return current.data
end

# method to print all the values in the linked list
Expand All @@ -60,13 +110,40 @@ def visit

# 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?

current = @head
prev = nil

until current.data == value
prev = current
current = current.next
end

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


Expand All @@ -79,7 +156,23 @@ def find_middle_value
# 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 nil if @head.nil?
current = @head

length = 0
until current.next.nil?
length += 1
current = current.next
end
Comment on lines +163 to +166

Choose a reason for hiding this comment

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

Why not use the length method to calculate the length of the list?


nth = @head
nth_length = length - n
return nil if nth_length < 0
nth_length.times do
nth = nth.next
end

return nth.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -94,18 +187,36 @@ 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?
@head = Node.new(value, nil)
else
current = @head
while !current.next.nil?
current = current.next
end

current.next = Node.new(value, nil)
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
return nil if @head.nil?

current = @head

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
Expand Down