-
Notifications
You must be signed in to change notification settings - Fork 49
Leaves - Bri #43
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
base: master
Are you sure you want to change the base?
Leaves - Bri #43
Changes from all 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,48 +19,105 @@ 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) | ||
raise NotImplementedError | ||
current_node = Node.new(value, @head) | ||
@head = current_node | ||
end | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
def search(value) | ||
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. 👍 |
||
raise NotImplementedError | ||
end | ||
temp = @head | ||
until temp.nil? | ||
if temp.data == value | ||
return true | ||
end | ||
temp = temp.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 | ||
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. 👍 |
||
raise NotImplementedError | ||
max = nil | ||
current = @head | ||
until current.nil? | ||
if max.nil? || current.data > max | ||
max = current.data | ||
end | ||
current = current.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 | ||
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. 👍 |
||
raise NotImplementedError | ||
min = nil | ||
current = @head | ||
until current.nil? | ||
if min.nil? || current.data < min | ||
min = current.data | ||
end | ||
current = current.next | ||
end | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
def length | ||
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. 👍 |
||
raise NotImplementedError | ||
count = 0 | ||
current = @head | ||
until current.nil? | ||
count += 1 | ||
current = current.next | ||
end | ||
return count | ||
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) | ||
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. 👍 |
||
raise NotImplementedError | ||
count = 0 | ||
current = @head | ||
|
||
until current.nil? | ||
count += 1 | ||
if index == count | ||
return current.data | ||
end | ||
|
||
current = current.next | ||
end | ||
return nil | ||
end | ||
# end | ||
|
||
# method to print all the values in the linked list | ||
def visit | ||
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. 👍 |
||
raise NotImplementedError | ||
print_all = nil | ||
current = @head | ||
until current.nil? | ||
print current.data | ||
current = current.next | ||
end | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
def delete(value) | ||
raise NotImplementedError | ||
if @head.nil? | ||
return nil | ||
end | ||
|
||
previous = @head | ||
current = @head | ||
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. You should also put in a check to make sure that the node we're deleting is not the head. |
||
until current.nil? || current.data == value | ||
previous = current | ||
current = current.next | ||
end | ||
previous.next = current.next | ||
end | ||
|
||
# method to reverse the singly linked list | ||
|
@@ -94,19 +151,44 @@ def has_cycle | |
# returns the value in the first node | ||
# returns nil if the list is empty | ||
def get_first | ||
raise NotImplementedError | ||
if @head.nil? | ||
return nil | ||
else | ||
return @head.data | ||
end | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
def add_last(value) | ||
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. 👍 |
||
raise NotImplementedError | ||
if @head.nil? | ||
@head = Node.new(value, nil) | ||
return | ||
end | ||
current = @head | ||
until current.next.nil? | ||
current = current.next | ||
end | ||
current.next = Node.new(value, nil) | ||
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 | ||
def get_last(value) | ||
previous = @head | ||
current = @head | ||
|
||
until current.nil? | ||
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. I'd suggest going |
||
current = current.next | ||
end | ||
|
||
previous = current | ||
current = current.next | ||
end | ||
previous.next = current.next.data | ||
return previous.next.data | ||
end | ||
# got stuck here | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
|
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.
👍