-
Notifications
You must be signed in to change notification settings - Fork 22
List Implementations; #8
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?
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 |
|---|---|---|
|
|
@@ -51,25 +51,52 @@ def delete(val) | |
| end | ||
|
|
||
| def display | ||
| # Traverse through the list till you hit the "nil" at the end | ||
| current = @head | ||
| full_list = [] | ||
| while current.next_node != nil | ||
| full_list += [current.value.to_s] | ||
| current = current.next_node | ||
| end | ||
| full_list += [current.value.to_s] | ||
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def include?(key) | ||
| # Traverse through the list till you hit the "nil" at the end | ||
| current = @head | ||
| full_list = [] | ||
| while current.next_node != nil | ||
| full_list += [current.value.to_s] | ||
| current = current.next_node | ||
| end | ||
| full_list += [current.value.to_s] | ||
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def size | ||
| end | ||
|
|
||
| def max | ||
| end | ||
| def include?(key) | ||
| current = @head | ||
| # breaks out when we reach last instead of check last | ||
| while current.next_node != nil | ||
| return true if current.value == key | ||
| current = current.next_node | ||
| end | ||
| return true if current.value == key | ||
| return false | ||
| end | ||
|
|
||
| def size | ||
| current = @head | ||
| size = 1 | ||
| while current.next_node != nil | ||
| size += 1 | ||
| current = current.next_node | ||
| end | ||
| size | ||
| end | ||
|
|
||
| def max | ||
| current = @head | ||
| max = [current] | ||
| while current.next_node != nil | ||
| current = current.next_node | ||
| if current.value > max[0].value | ||
| max[0] = current | ||
| end | ||
| end | ||
| max[0] = current if current.value > max[0].value | ||
| # i decided to return the node itself, and you can | ||
| # call value on it to see its value | ||
| max[0] | ||
| 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. This looks pretty good, but I don't understand why max is an array of a single node. I think it would have made more sense to say |
||
|
|
||
| end | ||
|
|
||
|
|
@@ -84,17 +111,73 @@ def max | |
| puts "Displaying Linked List:" | ||
| ll.display | ||
|
|
||
| puts "Shows the max:" | ||
| puts ll.max.value | ||
|
|
||
| puts "Delete 10 and then display the linked list:" | ||
| ll.delete(10) | ||
| ll.display | ||
|
|
||
| puts "Shows the size:" | ||
| puts ll.size | ||
|
|
||
| puts "Shows the max:" | ||
| puts ll.max.value | ||
|
|
||
| ll.add(35) | ||
| ll.add(15) | ||
|
|
||
| puts "Displaying Linked List:" | ||
| ll.display | ||
|
|
||
| puts "Shows the max:" | ||
| puts ll.max.value | ||
|
|
||
| puts "Does it include 20?" | ||
| puts ll.include?(20) | ||
|
|
||
| puts "Does it include 35?" | ||
| puts ll.include?(35) | ||
|
|
||
| puts "Does it include 10?" | ||
| puts ll.include?(10) | ||
|
|
||
| puts "Does it include 5?" | ||
| puts ll.include?(5) | ||
|
|
||
| puts "Shows the size:" | ||
| puts ll.size | ||
|
|
||
|
|
||
|
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. yay more tests! |
||
| =begin | ||
| Output: | ||
| Initialized a Node with value: 5 | ||
| Initialized a Node with value: 10 | ||
| Initialized a Node with value: 20 | ||
| Displaying Linked List: | ||
| 5->10->20 | ||
| Shows the max: | ||
| 20 | ||
| Delete 10 and then display the linked list: | ||
| 5->20 | ||
| Shows the size: | ||
| 2 | ||
| Shows the max: | ||
| 20 | ||
| Initialized a Node with value: 35 | ||
| Initialized a Node with value: 15 | ||
| Displaying Linked List: | ||
| 5->20->35->15 | ||
| Shows the max: | ||
| 35 | ||
| Does it include 20? | ||
| true | ||
| Does it include 35? | ||
| true | ||
| Does it include 10? | ||
| false | ||
| Does it include 5? | ||
| true | ||
| Shows the size: | ||
| 4 | ||
| =end | ||
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.
I know ruby says it's ok not to put the return in front of this, but I think it's weird :)