-
Notifications
You must be signed in to change notification settings - Fork 48
Diana - Space #39
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?
Diana - Space #39
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 |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
|
|
||
| # Defines a node in the singly linked list | ||
| class Node | ||
| attr_reader :data # allow external entities to read value but not write | ||
|
|
@@ -18,71 +17,173 @@ 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 | ||
| @head = Node.new(value, @head) | ||
| 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
+28
to
30
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 false if @head.nil? | ||
|
|
||
| current = @head | ||
| current_index = 0 | ||
| current_value = @head.data | ||
| while (current_index < (self.length) && !current.nil?) | ||
| if current_value == value | ||
| return true | ||
| elsif current_index != (self.length - 1) | ||
| current = current.next | ||
| current_value = current.data | ||
| current_index += 1 | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_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. 👍 |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| current = @head | ||
| current_index = 0 | ||
| max_value = @head.data | ||
| current_value = @head.data | ||
| while (current_index < (self.length) && !current.nil?) | ||
| if current.data > max_value | ||
| max_value = current_value | ||
| end | ||
| if current.next.nil? | ||
| return max_value | ||
| end | ||
| current = current.next | ||
| current_value = current.data | ||
| current_index += 1 | ||
| 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
+74
to
76
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 | ||
| current_index = 0 | ||
| min_value = @head.data | ||
| current_value = @head.data | ||
| while (current_index < (self.length) && !current.nil?) | ||
| if current.data < min_value | ||
| min_value = current_value | ||
| end | ||
| if current.next.nil? | ||
| return min_value | ||
| end | ||
| current = current.next | ||
| current_value = current.data | ||
| current_index += 1 | ||
| 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
+99
to
101
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 | ||
| count = 0 | ||
| until current.nil? | ||
| current = current.next | ||
| count += 1 | ||
| end | ||
| return count | ||
| 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
+116
to
118
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 | ||
| end | ||
| return nil if @head.nil? | ||
| return nil if index > self.length | ||
|
|
||
| current = @head | ||
| current_index = 0 | ||
| while (current_index < index && !current.nil?) | ||
| current_index += 1 | ||
| current = current.next | ||
| end | ||
|
|
||
| return current.data | ||
| 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
+133
to
135
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 | ||
| current_index = 0 | ||
| while (current_index < self.length && !current.nil?) | ||
| print current.data | ||
| current_index += 1 | ||
| 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
+148
to
150
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? | ||
|
|
||
| # if head node is node to be deleted, change head | ||
| if @head.data == value | ||
| @head = @head.next | ||
| end | ||
|
|
||
| current = @head | ||
| while !current.next.nil? | ||
| if current.next.data == value | ||
| current.next = current.next.next | ||
| return | ||
| end | ||
| 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
+170
to
172
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 | ||
| previous = nil | ||
| temp = current.next | ||
|
|
||
| until current.nil? | ||
| temp = current.next | ||
|
|
||
| current.next = previous | ||
|
|
||
| previous = current | ||
| current = temp | ||
| end | ||
|
|
||
| @head = previous | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -118,7 +219,8 @@ def has_cycle | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_first | ||
|
Comment on lines
219
to
221
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? | ||
| return @head.data | ||
| end | ||
|
|
||
| # method that inserts a given value as a new last node in the linked list | ||
|
|
||
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.
👍