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

Aurora Anderson Linked List #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
82 changes: 75 additions & 7 deletions linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,115 @@ 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 insert(value)
puts "Not implemented"
newNode = Node.new(value)
if @head == nil
@head = newNode
else
newNode.next = @head
@head = newNode
end
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)
puts "Not implemented"
current = @head
while current.next != 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
puts "Not implemented"
current = @head
max = current.data
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check to ensure that head is not nil before accessing data. In other words, update your method to work for empty linked list scenario.

while current.next
current = current.next
if current.data > max
max = current.data
end
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
current = @head
min = current.data
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as with find_max, check for null before accessing member variables in the node.

while current.next
current = current.next
if current.data < min
min = current.data
end
end
return min
puts "Not implemented"
end

# method that returns the length of the singly linked list
def length
counter = 1
current = @head
while current.next
Copy link
Collaborator

Choose a reason for hiding this comment

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

Always check for null. :-) What if the linked list is empty? In that case, the length returned should be 0.

current = current.next
counter += 1
end
return counter
puts "Not implemented"
end

# method to return the value of the nth element from the beginning
# assume indexing starts at 0 while counting to n
def find_nth_from_beginning(n)
puts "Not implemented"
index = 0
current = @head
if current
while index < n
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if there are less than n+1 number of nodes in the list?
To fix for that scenario, Reverse the order of if and while statements and if current become null sooner than the n, return null.

current = current.next
index += 1
end
p current.data
end
# puts "Not implemented"
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
def insert_ascending(value)
puts "Not implemented"
current = @head
prevVal = nil
# nextVal = nil
until value < current.data
Copy link
Collaborator

Choose a reason for hiding this comment

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

check if current is nil (or @Head is nil) before accessing current.data

puts "current.data is #{current.data}"
prevVal = current
current.next ? current = current.next : break
end

new_node = Node.new(value)

if value > current.data && !current.next
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check if current is nil first. :-)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Scenarios to ensure your code works for: empty linked list, linked list has all values smaller than the value to insert, linked list with all values greater than the value to insert. In the empty linked list case and all values greater than value to insert case, head will need to get updated.

current.next = new_node
else # no next
puts "current.data is #{current.data}"
prevVal.next = new_node
new_node.next = current

end
# # puts "Not implemented"
end

# method to print all the values in the linked list
def visit
puts "Not implemented"
current = @head
while current
puts current.data
current = current.next
end
# puts "Not implemented"
end

# method to delete the first node found with specified value
Expand Down Expand Up @@ -148,7 +216,7 @@ def create_cycle
puts "BUG: Value at index 1 should be 3 and is #{value}" if value != 3

# Insert ascending
puts "Adding 6 in ascening order."
puts "Adding 6 in ascending order."
my_linked_list.insert_ascending(6)

# print all elements
Expand Down