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

Leaves - Yitgop #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "1234",
"remoteWorkspaceRoot": "${workspaceRoot}"
}
]
}
161 changes: 145 additions & 16 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,99 +19,228 @@ 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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
checker = @head

until checker.nil?
return true if checker.data == value
checker = checker.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

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?
max = @head.data
marker = @head.next
until marker.nil?
max = marker.data if marker.data > max
marker = marker.next
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

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

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 index > self.length || index < 0
curr = @head

index.times do
curr = curr.next
end

return curr.data
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?
curr = @head

until curr.nil?
p curr.data
curr = curr.next
end
end

# method to delete the first node found with specified value
def delete(value)
raise NotImplementedError
curr = @head
previous = nil

return nil if curr.nil?

until curr.data == value

Choose a reason for hiding this comment

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

This could crash if curr is nil.

Suggested change
until curr.data == value
until curr.nil?

Then inside the loop if you find the item to delete, make the deletion and return.

if curr.nil?
return nil
else
previous = curr
curr = curr.next
end
end

if previous == nil
@head = curr.next
else
previous.next = curr.next
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.

This doesn't seem to work as you never change @head

raise NotImplementedError
return if @head.nil? || @head.next.nil?

curr = @head
prev = nil

until curr.nil?
temp = curr.next
curr.next = prev
prev = curr
curr = temp
end
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?

slow = @head
fast = @head.next

until fast.nil?
slow = slow.next
fast = fast.next.next
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.

👍

raise NotImplementedError
return nil if @head.nil? || self.length < n

slow = @head
fast = @head

(n - 1).times do
fast = fast.next
end

until fast.next.nil?
slow = slow.next
fast = fast.next
end

return slow.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

Choose a reason for hiding this comment

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

This doesn't quite work as the cycle doesn't have to connect the head to the tail, it could be a circle starting toward the end. Like if the last node points to the 2nd to last node.

raise NotImplementedError
return false if @head.nil?
marker = @head.next

until marker.nil?
return true if marker == @head
marker = marker.next
end
return false
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
def get_first
raise NotImplementedError
return nil if @head.nil?
return @head.data
end

# method that inserts a given value as a new last node in the linked list
def add_last(value)
raise NotImplementedError
if @head.nil?
@head = Node.new(value)
end

curr = @head
self.(length-1).times do

Choose a reason for hiding this comment

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

This has a typo

Suggested change
self.(length-1).times do
(self.length - 1).times do

curr = curr.next
end
curr.next = Node.new(value)
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
return nil if @head.nil?

curr = @head
until curr.next.nil?
curr = curr.next
end

return curr.data
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
@head = Node.new(value) if @head.nil?

if @head.data > value
temp = @head
@head = Node.new(value)
@head.next = temp
else
curr = @head
until curr.nil?
if value >= curr.data
new = Node.new(value,curr.next.next)
curr.next = new

Choose a reason for hiding this comment

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

You'll want to leave the loop when you add the new node.

Suggested change
curr.next = new
curr.next = new
return

end
curr = curr.next
end
end
end

# Helper method for tests
Expand Down