-
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
Branches - Diana Han #40
base: master
Are you sure you want to change the base?
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,43 +19,97 @@ 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 = @head | ||||||||
@head = Node.new(value, current) | ||||||||
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 | ||||||||
if @head.nil? | ||||||||
return false | ||||||||
else | ||||||||
current = @head | ||||||||
end | ||||||||
|
||||||||
while current | ||||||||
if current.data == value | ||||||||
return true | ||||||||
else | ||||||||
current = current.next | ||||||||
end | ||||||||
end | ||||||||
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. Just for readability I suggest returning false when you can't find it.
Suggested change
|
||||||||
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 | ||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
max = @head.data | ||||||||
|
||||||||
while current | ||||||||
max = current.data if current.data > max | ||||||||
current = current.next | ||||||||
end | ||||||||
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 | ||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
min = @head.data | ||||||||
|
||||||||
while current | ||||||||
min = current.data if current.data < min | ||||||||
current = current.next | ||||||||
end | ||||||||
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 | ||||||||
node = @head | ||||||||
|
||||||||
return 0 if @head.nil? | ||||||||
|
||||||||
until node == nil | ||||||||
count += 1 | ||||||||
node = node.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 | ||||||||
return nil if @head.nil? | ||||||||
|
||||||||
current = @head | ||||||||
count = 0 | ||||||||
|
||||||||
until current.nil? | ||||||||
return current.data if count == index | ||||||||
count += 1 | ||||||||
current = current.next | ||||||||
end | ||||||||
|
||||||||
return nil | ||||||||
end | ||||||||
|
||||||||
# method to print all the values in the linked list | ||||||||
def visit | ||||||||
raise NotImplementedError | ||||||||
return nil if @head.nil? | ||||||||
current = @head | ||||||||
linked_list = "" | ||||||||
|
||||||||
while current | ||||||||
linked_list += "#{current.data}" | ||||||||
current = current.next | ||||||||
end | ||||||||
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 | ||||||||
|
||||||||
# method to delete the first node found with specified 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.
👍