Skip to content

Leaves-Yasmin #29

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 2 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
347 changes: 264 additions & 83 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,120 +12,301 @@ def initialize(value, next_node = nil)

# Defines the singly linked list
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
end
def initialize
@head = nil # keep the head private. Not accessible outside this class
end

# 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
end
# 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.

👍

@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)
raise NotImplementedError
end
# new_node = Node.new(value)
# new_node.next = @head
# @head = new_node
# end

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

# method to return the min value in the linked list
# returns the data value and not the node
def find_min
raise NotImplementedError
# 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_node = @head
until current_node == nil
if current_node.data == value
return true
end
current_node = current_node.next
end
return false
end

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

# method that returns the length of the singly linked list
def length
raise NotImplementedError
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_node = @head
if current_node == nil
return nil
else
max = current_node.data
until current_node == nil
if current_node.data > max
max = current_node.data
end
current_node = current_node.next
end
return max
end
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
def visit
raise NotImplementedError
# 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_node = @head
if current_node == nil
return nil
else
min = current_node.data
until current_node == nil
if current_node.data < min
min = current_node.data
end
current_node = current_node.next
end
return min
end
end

# method to delete the first node found with specified value
def delete(value)
raise NotImplementedError
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
# 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?
current_node = @head
length = 0
until current_node.nil?
length += 1
current_node = current_node.next
end
return length
end
# return 0 if @head.nil?
# current_node = @head
# length = 0
# while current_node != nil
# length += 1
# current_node = current_node.next
# end
# return length
# end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
def find_middle_value
raise NotImplementedError
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)
raise NotImplementedError
# 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.

what about if index is past the end of the list?

# raise NotImplementedError
current_node = @head
return nil if current_node.nil?
index.times do
current_node = current_node.next

Choose a reason for hiding this comment

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

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

end
return current_node.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
# 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_node = @head
while current_node = current_node.next

Choose a reason for hiding this comment

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

Clever

print current_node.data
end
end
# current_node = @head
# until current_node == nil
# print current_node.data
# current_node = current_node.next
# end
# end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
def get_first
raise NotImplementedError
# 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_node = @head
if current_node.data == value
@head = current_node.next
return
end
until current_node == nil
if current_node.next.data == value
current_node.next = current_node.next.next
return
end
current_node = current_node.next
end
end

# method that inserts a given value as a new last node in the linked list
def add_last(value)
raise NotImplementedError
# 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 nil if @head.nil?
current_node = @head
# next_node = current_node.next
# current_node.next = nil
previous_node = nil
until current_node == nil
temp = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = temp
end
@head = 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.

👍

# raise NotImplementedError
return nil if @head.nil?
return @head.data if @head.next.nil?

slow = @head
fast = @head.next
while fast!= nil
solw = slow.next
fast = fast.next
if fast != nil
fast = fast.next
end
end
return slow.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.

Very good reuse of existing methods!

return nil if @head == nil
length = self.length
return nil if n >= length
i = length - n - 1
return get_at_index(i)
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

Choose a reason for hiding this comment

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

👍 Great work here!

# raise NotImplementedError
return false if @head.nil?
return false if @head.next.nil?
slow = @head
fast = @head
while fast != nil
slow = slow.next
fast = fast.next
if fast != nil
fast = fast.next
end
if fast == slow
return true #cycle detected
end
end
return false
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
# 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
return nil if @head.nil?
return @head.data
end
# if @head != nil
# return @head.data
# else
# return nil
# 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
new_node = Node.new(value)
if @head.nil?
add_first(value)
else
current_node = @head
while current_node.next != nil
current_node = current_node.next
end
current_node.next = new_node
end
end
# if @head == nil
# @head = Node.new(value)
# return
# end
# current_node = @head
# until current_node.next.nil?
# current_node = current_node.next
# end
# current_node.next = Node.new(value)
# 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
# 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_node = @head
return nil if current_node.nil?
while current_node.next != nil
current_node = current_node.next
end
return current_node.data
end
# current_node = @head
# return nil if current_node == nil
# until current_node.next.nil?
# current_node = current_node.next
# end
# return current_node.data
# end

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty

# navigate to last node
current = @head
while current.next != nil
current = current.next
end
# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
def insert_ascending(value)
end



# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty

current.next = @head # make the last node link to first node
# navigate to last node
current = @head
while current.next != nil
current = current.next
end

current.next = @head # make the last node link to first node
end
end
Loading