-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if current is nil first. :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.