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 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
142 changes: 130 additions & 12 deletions linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,182 @@ 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
puts "Not implemented"
current = @head
min = current.data
while current.next
current = current.next
if current.data < min
min = current.data
end
end
return min
end

# method that returns the length of the singly linked list
def length
puts "Not implemented"
counter = 1
current = @head
while current.next
current = current.next
counter += 1
end
return counter
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
prevVal = current
current.next ? current = current.next : break
end

new_node = Node.new(value)

if value > current.data && !current.next
current.next = new_node
else # no next
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
def delete(value)
puts "Not implemented"
current = @head
prevNode = nil
if current.data == value
@head = current.next
return
end
while current.data != value
prevNode = current
current.next ? current = current.next : break
Copy link
Collaborator

@shrutivanw shrutivanw Oct 31, 2017

Choose a reason for hiding this comment

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

Should this be current.next ? current = current.next : return If you just break, you'll have a nil dereference exception on line 142.

end

if current.data == value
if current.next
prevNode.next = current.next
else
prevNode.next = nil
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optimization: you could replace lines 143 through 147 with just line 144, i.e. prevNode.next = current.next. If current.next is nil, line 144 will just set prevNode.next to nil.

else # not found
return false
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
puts "Not implemented"
puts "THIS MAKES MY HEAD HURT"
Copy link
Collaborator

Choose a reason for hiding this comment

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

:-)

current = @head
previous = nil
temp = current.next
Copy link
Collaborator

@shrutivanw shrutivanw Oct 31, 2017

Choose a reason for hiding this comment

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

Check for empty linked list case. Don't access current.next without verifying that current exists (and is not 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
puts "Not implemented"
counter = 0
current = @head
while current
current = current.next
counter += 1
end

middle = nil

if counter % 2 == 0
middle = counter / 2
else
middle = (counter + 1 ) / 2
end

newCounter = 0

current = @head

until newCounter = middle
current = current.next
counter += 1
end

return current.data
end

# find the nth node from the end and return its value
Expand Down Expand Up @@ -148,7 +266,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