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

Erika - Branches #45

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
99 changes: 87 additions & 12 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,132 @@ 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)
raise NotImplementedError
if @head.nil?
return nil
end

until @head.nil?

Choose a reason for hiding this comment

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

This is going to lose all subsequent elements of the linked list.

You need to have a current element to traverse the list.

  return nil if @head.nil?

  current = @head
  until current.next.nil?
    return true if current.data == value
    current = current.next
  end
return false

if @head.data == value
return true
else
@head = @head.next
end
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?
current = @head
max = current

until current.nil?
if max.data < current.data
max = current
current = current.next

Choose a reason for hiding this comment

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

Suggested change
current = current.next
end
current = current.next

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

See above for suggestions on how to fix this.

raise NotImplementedError

return nil if @head.nil?

current = @head
min = current

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

current = @head
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)
raise NotImplementedError
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?
current = @head

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

end

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

return nil if @head.nil?
current = @head
previous = nil

until current.nil?
if current.data == value
previous.next = current.next
exit

Choose a reason for hiding this comment

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

exit exits the program, you probably meant return

else
previous = current
current = current.next
end
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.next
current.next = previous
previous = current
current = temp
end

@head = previous
end


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