-
Notifications
You must be signed in to change notification settings - Fork 22
Suzanne-finished assignment #7
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 | ||
|
|
@@ -63,14 +63,45 @@ def display | |
| end | ||
|
|
||
| def include?(key) | ||
| current = @head | ||
| #while we have nodes to traverse | ||
| while current != nil | ||
| #when we find the key | ||
| return true if current.value == key | ||
| #reassign currrent | ||
| current = current.next_node | ||
| end | ||
| puts "The list does not include #{key}" | ||
| return false | ||
| end | ||
|
|
||
| def size | ||
| @size = 0 | ||
| current = @head | ||
| while current != nil | ||
| #increase size | ||
| @size += 1 | ||
| #reassign current | ||
| current = current.next_node | ||
| end | ||
| puts "This linked list has #{@size} elements." | ||
| return @size | ||
| 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 is good too! Same comment as above, though. |
||
|
|
||
| def max | ||
| current = @head | ||
| max_value = current.value | ||
| while current != nil | ||
| #if the current node has a larger value than the max gets reasigned | ||
| if current.value > max_value | ||
| max_value = current.value | ||
| end | ||
| #if max_value is larger than keep moving to check next node | ||
| current = current.next_node | ||
| end | ||
| puts "#{max_value} is the max value element in the list." | ||
| return max_value | ||
| 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 good! Though I'm not sure what Ruby does in the case that you call |
||
|
|
||
| end | ||
|
|
||
| # Initializing a Linked List with a node containing value (5) | ||
|
|
@@ -79,14 +110,28 @@ def max | |
| # Adding Elements to Linked List | ||
| ll.add(10) | ||
| ll.add(20) | ||
| # add more to list | ||
| ll.add(11) | ||
| ll.add(99) | ||
|
|
||
| # Display the Linked List | ||
| puts "Displaying Linked List:" | ||
| ll.display | ||
| ll.include?(5) | ||
| # add more to test include? | ||
| ll.include?(11) | ||
| ll.include?(98) | ||
| ll.size | ||
| ll.max | ||
|
|
||
|
|
||
| puts "Delete 10 and then display the linked list:" | ||
| ll.delete(10) | ||
| ll.display | ||
| ll.size | ||
| ll.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. yay! more tests! |
||
|
|
||
| =begin | ||
| Output: | ||
|
|
||
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.
yes! this looks nice! the only thing is that you shouldn't "puts" anything because I did not ask for that, which means that you are creating unintended side-affects for the method that may not be expected.