-
Notifications
You must be signed in to change notification settings - Fork 22
List implementations #3
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 |
|---|---|---|
|
|
@@ -3,73 +3,101 @@ | |
| # LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list | ||
|
|
||
| class Node | ||
| attr_accessor :value, :next_node | ||
| attr_accessor :value, :next_node | ||
|
|
||
| def initialize(val,next_in_line=null) | ||
| @value = val | ||
| @next_nodex = next_in_line | ||
| puts "Initialized a Node with value: " + value.to_s | ||
| end | ||
| def initialize(val,next_in_line=nil) | ||
| @value = val | ||
| @next_node = next_in_line | ||
| puts "Initialized a Node with value: " + value.to_s | ||
| end | ||
| end | ||
|
|
||
| class LinkedList | ||
| def initialize(val) | ||
| # Initialize a new node at the head | ||
| @head = Node.new(val,nil) | ||
| end | ||
|
|
||
| def add(value) | ||
| # Traverse to the end of the list | ||
| # And insert a new node over there with the specified value | ||
| current = @head | ||
| while current.next_node != nil | ||
| current = current.next_node | ||
| end | ||
| current.next_node = Node.new(value,nil) | ||
| self | ||
| end | ||
|
|
||
| def delete(val) | ||
| current = @head | ||
| if current.value == val | ||
| # If the head is the element to be delete, the head needs to be updated | ||
| @head = @head.next_node | ||
| else | ||
| # ... x -> y -> z | ||
| # Suppose y is the value to be deleted, you need to reshape the above list to : | ||
| # ... x->z | ||
| # ( and z is basically y.next_node ) | ||
| current = @head | ||
| while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val) | ||
| current = current.next_node | ||
| end | ||
|
|
||
| if (current != nil) && (current.next_node != nil) | ||
| current.next_node = (current.next_node).next_node | ||
| end | ||
| end | ||
| 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) | ||
| end | ||
|
|
||
| def size | ||
| end | ||
|
|
||
| def max | ||
| end | ||
| def initialize(val) | ||
| # Initialize a new node at the head | ||
| @head = Node.new(val,nil) | ||
| end | ||
|
|
||
| def add(value) | ||
| # Traverse to the end of the list | ||
| # And insert a new node over there with the specified value | ||
| current = @head | ||
| while current.next_node != nil | ||
| current = current.next_node | ||
| end | ||
| current.next_node = Node.new(value,nil) | ||
| self | ||
| end | ||
|
|
||
| def delete(val) | ||
| current = @head | ||
| if current.value == val | ||
| # If the head is the element to be delete, the head needs to be updated | ||
| @head = @head.next_node | ||
| else | ||
| # ... x -> y -> z | ||
| # Suppose y is the value to be deleted, you need to reshape the above list to : | ||
| # ... x->z | ||
| # ( and z is basically y.next_node ) | ||
| current = @head | ||
| while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val) | ||
| current = current.next_node | ||
| end | ||
|
|
||
| if (current != nil) && (current.next_node != nil) | ||
| current.next_node = (current.next_node).next_node | ||
| end | ||
| end | ||
| 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 | ||
|
|
||
| # HOMEWORK: next 3 methods | ||
| def include?(key) | ||
| current = @head | ||
| while current.next_node != nil | ||
| return true if current.value == key | ||
| current = current.next_node | ||
| end | ||
|
|
||
| return true if current.value == key | ||
|
|
||
| false | ||
| end | ||
|
|
||
| def size | ||
| current = @head | ||
| list_size = 0 | ||
| while current.next_node != nil | ||
| list_size += 1 | ||
| current = current.next_node | ||
| end | ||
|
|
||
| list_size += 1 | ||
| 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 |
||
|
|
||
| # optional | ||
| def max | ||
| current = @head | ||
| max = 0 | ||
| 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 | ||
|
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. ditto here |
||
| end | ||
|
|
||
| end | ||
|
|
||
|
|
@@ -88,6 +116,23 @@ def max | |
| ll.delete(10) | ||
| ll.display | ||
|
|
||
| print "Checking if 5 is included in the linked list: " | ||
| puts ll.include?(5) | ||
|
|
||
| print "Checking if 10 is included in the linked list: " | ||
| puts ll.include?(10) | ||
|
|
||
| print "Checking the size of the linked list: " | ||
| puts ll.size | ||
|
|
||
| ll.add(12) | ||
| puts "New size: #{ll.size}" | ||
|
|
||
| ll.display | ||
|
|
||
| print "Max value of any node is: " | ||
| puts 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. yaya for adding more tests! |
||
| =begin | ||
| Output: | ||
| Initialized a Node with value: 5 | ||
|
|
@@ -97,4 +142,11 @@ def max | |
| 5->10->20 | ||
| Delete 10 and then display the linked list: | ||
| 5->20 | ||
| Checking if 5 is included in the linked list: true | ||
| Checking if 10 is included in the linked list: false | ||
| Checking the size of the linked list: 2 | ||
| Initialized a Node with value: 12 | ||
| New size: 3 | ||
| 5->20->12 | ||
| Max value of any node is: 20 | ||
| =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 think non-Ruby-ers will find this strange. I think you should explicitly say
return falseeven if in Ruby this works.