-
Notifications
You must be signed in to change notification settings - Fork 49
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
Erika - Branches #45
base: master
Are you sure you want to change the base?
Erika - Branches #45
Changes from 1 commit
bbfa3eb
4bb0f68
f0ebb6c
74c37ca
5c7cb88
147f4db
f754161
85b2746
2861349
41f0be8
229d2c3
74d51d7
c0556a4
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,57 +19,132 @@ 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 | ||||||||
@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) | ||||||||
raise NotImplementedError | ||||||||
if @head.nil? | ||||||||
return nil | ||||||||
end | ||||||||
|
||||||||
until @head.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. This is going to lose all subsequent elements of the linked list. You need to have a current element to traverse the list. return nil if @head.nil?
current = @head
until current.next.nil?
return true if current.data == value
current = current.next
end
return false |
||||||||
if @head.data == value | ||||||||
return true | ||||||||
else | ||||||||
@head = @head.next | ||||||||
end | ||||||||
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 | ||||||||
raise NotImplementedError | ||||||||
|
||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
max = current | ||||||||
|
||||||||
until current.nil? | ||||||||
if max.data < current.data | ||||||||
max = current | ||||||||
current = 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.
Suggested change
|
||||||||
end | ||||||||
end | ||||||||
return max.data | ||||||||
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. See above for suggestions on how to fix this. |
||||||||
raise NotImplementedError | ||||||||
|
||||||||
return nil if @head.nil? | ||||||||
|
||||||||
current = @head | ||||||||
min = current | ||||||||
|
||||||||
until current.nil? | ||||||||
if min.data > current.data | ||||||||
min.data = current.data | ||||||||
current = current.next | ||||||||
end | ||||||||
end | ||||||||
return min.data | ||||||||
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 | ||||||||
if @head.nil? | ||||||||
return 0 | ||||||||
end | ||||||||
|
||||||||
current = @head | ||||||||
length = 0 | ||||||||
|
||||||||
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) | ||||||||
raise NotImplementedError | ||||||||
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 | ||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
|
||||||||
until current.nil? | ||||||||
print current.value | ||||||||
current = current.next | ||||||||
end | ||||||||
|
||||||||
end | ||||||||
|
||||||||
# method to delete the first node found with specified value | ||||||||
def delete(value) | ||||||||
raise NotImplementedError | ||||||||
|
||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
previous = nil | ||||||||
|
||||||||
until current.nil? | ||||||||
if current.data == value | ||||||||
previous.next = current.next | ||||||||
exit | ||||||||
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. exit exits the program, you probably meant |
||||||||
else | ||||||||
previous = current | ||||||||
current = current.next | ||||||||
end | ||||||||
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 | ||||||||
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 | ||||||||
current = @head | ||||||||
previous = 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 | ||||||||
|
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.
👍