Skip to content

Leaves - Mariya #27

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
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
173 changes: 158 additions & 15 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,190 @@ 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
return nil if current.nil?
max = current.data

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

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

until 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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
length = 0

until current.nil?
length += 1
current = current.next
end
Comment on lines +90 to +95

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 find the length?


return nil if length < index

current = @head
index_value = current.data

(index + 1).times do
index_value = current.data
current = current.next
end

return index_value
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
return nil if head.nil?

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

if current.data == value
return @head = current.next
end

previous = nil
continuing_node = nil

until current.next.nil? || current.next.data == value
current = current.next
end

return nil if current.next.nil?
previous = current
continuing_node = previous.next.next
previous.next = continuing_node

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


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

@head = current

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.

👍 , just remember to re-use methods

raise NotImplementedError
current = @head
temp = current
length = 0

until current.nil?
length += 1
temp = temp.next
end

middle_node = nil
(length / 2).times do
middle_node = current.data
current = current.next
end

return middle_node
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
nth_node = nil
current = @head
temp = @head
length = 0

until temp.nil?
length += 1
temp = temp.next
end

(length - n).times do
nth_node = current.data
current = current.next
end

return nth_node
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -94,24 +217,44 @@ 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
current = @head
return nil if current.nil?
return current.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
@head = Node.new(value)
return @head
else
current = @head
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)
raise NotImplementedError
end

# Helper method for tests
Expand Down
Loading