-
Notifications
You must be signed in to change notification settings - Fork 48
C-14 Fire Alice D #46
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
2482707
e7728af
f1771a5
3dec5ef
4cb62db
49c2176
6583329
de3b664
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,71 +18,163 @@ 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 | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(1) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def add_first(value) | ||||||||||||
| raise NotImplementedError | ||||||||||||
|
|
||||||||||||
| new_node = Node.new(value, nil) | ||||||||||||
| new_node.next = @head | ||||||||||||
| @head = new_node | ||||||||||||
| return @head.data | ||||||||||||
|
Comment on lines
+25
to
+28
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 a little simpler
Suggested change
|
||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to find if the linked list contains a node with specified value | ||||||||||||
| # returns true if found, false otherwise | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def search(value) | ||||||||||||
|
Comment on lines
+33
to
35
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 | ||||||||||||
| current = @head | ||||||||||||
| while current != nil | ||||||||||||
| if current.data == value | ||||||||||||
| return true | ||||||||||||
| else | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
| return false | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to return the max value in the linked list | ||||||||||||
| # returns the data value and not the node | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def find_max | ||||||||||||
|
Comment on lines
+49
to
51
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_value = @head.data | ||||||||||||
| while current != nil | ||||||||||||
| if current.data > max_value | ||||||||||||
| max_value = current.data | ||||||||||||
| end | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| return max_value | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to return the min value in the linked list | ||||||||||||
| # returns the data value and not the node | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def find_min | ||||||||||||
|
Comment on lines
+66
to
68
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_value = current.data | ||||||||||||
| while current != nil | ||||||||||||
| if current.data < min_value | ||||||||||||
| min_value = current.data | ||||||||||||
| end | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| return min_value | ||||||||||||
|
|
||||||||||||
| end | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # method that returns the length of the singly linked list | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def length | ||||||||||||
|
Comment on lines
+84
to
86
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 0 if @head.nil? | ||||||||||||
| current = @head | ||||||||||||
| length = 0 | ||||||||||||
| while current != nil | ||||||||||||
| length += 1 | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| return length | ||||||||||||
| 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 | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def get_at_index(index) | ||||||||||||
|
Comment on lines
+100
to
102
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 | ||||||||||||
| # traverse the list, | ||||||||||||
| # index, number of times or | ||||||||||||
| # until the end is reach | ||||||||||||
| # Then return the current node's value | ||||||||||||
| i = 0 | ||||||||||||
| current = @head | ||||||||||||
| while current != nil | ||||||||||||
| if i == index | ||||||||||||
| return current.data | ||||||||||||
| else | ||||||||||||
| i += 1 | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| return nil | ||||||||||||
|
|
||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to print all the values in the linked list | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def visit | ||||||||||||
|
Comment on lines
+123
to
125
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 | ||||||||||||
| current = @head | ||||||||||||
| while current !=nil | ||||||||||||
| puts current.data | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to delete the first node found with specified value | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def delete(value) | ||||||||||||
|
Comment on lines
+134
to
136
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 | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| if @head.data == value | ||||||||||||
| @head = @head.next | ||||||||||||
| return | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| current = @head | ||||||||||||
| previous = nil | ||||||||||||
|
|
||||||||||||
| while current != nil | ||||||||||||
| if current.data == value | ||||||||||||
| previous.next = current.next | ||||||||||||
| end | ||||||||||||
| previous = current | ||||||||||||
| current = current.next | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method to reverse the singly linked list | ||||||||||||
| # note: the nodes should be moved and not just the values in the nodes | ||||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| # Time Complexity: O(n) | ||||||||||||
| # Space Complexity: O(1) | ||||||||||||
| def reverse | ||||||||||||
|
Comment on lines
+161
to
163
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? || @head.next.nil? | ||||||||||||
| return @head | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| current = @head | ||||||||||||
| previous = nil | ||||||||||||
|
|
||||||||||||
| while current != nil | ||||||||||||
| temp = current.next | ||||||||||||
| current.next = previous | ||||||||||||
| previous = current | ||||||||||||
| current = temp | ||||||||||||
| end | ||||||||||||
| @head = previous | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -118,7 +210,12 @@ def has_cycle | |||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| def get_first | ||||||||||||
|
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 the value of the 1st node in the list | ||||||||||||
| if @head.nil? | ||||||||||||
| return nil | ||||||||||||
| else | ||||||||||||
| return @head.data | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| # method that inserts a given value as a new last node in the linked list | ||||||||||||
|
|
@@ -133,6 +230,9 @@ def add_last(value) | |||||||||||
| # Time Complexity: ? | ||||||||||||
| # Space Complexity: ? | ||||||||||||
| def get_last | ||||||||||||
| # return nil if the list is empty | ||||||||||||
| # otherwise traverse the list to the end | ||||||||||||
| # Then return the last node's value | ||||||||||||
| raise NotImplementedError | ||||||||||||
| 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.
👍