-
Notifications
You must be signed in to change notification settings - Fork 22
homework for native arrays and linked lists #5
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 |
|---|---|---|
|
|
@@ -5,9 +5,9 @@ | |
| class Node | ||
| attr_accessor :value, :next_node | ||
|
|
||
| def initialize(val,next_in_line=null) | ||
| def initialize(val,next_in_line=nil) | ||
| @value = val | ||
| @next_nodex = next_in_line | ||
| @next_node = next_in_line | ||
| puts "Initialized a Node with value: " + value.to_s | ||
| end | ||
| end | ||
|
|
@@ -62,13 +62,35 @@ def display | |
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def include?(key) | ||
| def include?(value) | ||
| current = @head | ||
| while !(current.next_node == nil) | ||
| return true if current.value == value | ||
| current = current.next_node | ||
| end | ||
| return true if current.value == value | ||
| return false | ||
| end | ||
|
|
||
| def size | ||
| current = @head | ||
| size = 0 | ||
| while !(current.next_node == nil) | ||
| size += 1 | ||
| current = current.next_node | ||
| end | ||
| size += 1 | ||
| end | ||
|
|
||
| def max | ||
| current = @head | ||
| max = current.value | ||
| while !(current.next_node == nil) | ||
| max = current.value if current.value > max | ||
| current = current.next_node | ||
| end | ||
| max = current.value if current.value > max | ||
| max | ||
| 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 great and works, but in all of these (include?, size, and max) you had to account for that last node separately -- if you change your loop to while current != nil instead of while current.next_node != nil then you wouldn't need that! I told you to model after display which does it the latter way -- because sometimes you need to do something different with the last case and I wanted that to be the pattern I showed -- but now I want to point out how we could have optimized the pattern just slightly. Hopefully that makes sense!
Author
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. Mostly understand, but honestly can't completely remember this from two weeks ago. I'll review it again tonight. Thanks! |
||
|
|
||
| end | ||
|
|
@@ -87,7 +109,11 @@ def max | |
| puts "Delete 10 and then display the linked list:" | ||
| ll.delete(10) | ||
| ll.display | ||
|
|
||
| puts ll.size | ||
| puts ll.include?(5) | ||
| puts ll.include?(10) | ||
| puts ll.include?(20) | ||
| puts ll.max | ||
| =begin | ||
|
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 for adding more tests! |
||
| Output: | ||
| Initialized a Node with value: 5 | ||
|
|
@@ -98,3 +124,5 @@ def max | |
| Delete 10 and then display the linked list: | ||
| 5->20 | ||
| =end | ||
|
|
||
| # Homework: write include/size/and max | ||
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 find this strange notation to say
rather than saying