-
Notifications
You must be signed in to change notification settings - Fork 48
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
Space - Stephanie #31
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||||||||||
# Defines a node in the singly linked list | ||||||||||||||
class Node | ||||||||||||||
attr_reader :data # allow external entities to read value but not write | ||||||||||||||
attr_accessor :next # allow external entities to read or write next node | ||||||||||||||
attr_accessor :next, :data # allow external entities to read or write next node | ||||||||||||||
|
||||||||||||||
def initialize(value, next_node = nil) | ||||||||||||||
@data = value | ||||||||||||||
|
@@ -12,69 +12,154 @@ def initialize(value, next_node = nil) | |||||||||||||
|
||||||||||||||
# Defines the singly linked list | ||||||||||||||
class LinkedList | ||||||||||||||
def initialize | ||||||||||||||
def initialize | ||||||||||||||
@head = nil # keep the head private. Not accessible outside this class | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# 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) bc it only adds one thing regardless of how big the list is | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def add_first(value) | ||||||||||||||
raise NotImplementedError | ||||||||||||||
if @head.nil? | ||||||||||||||
@head = Node.new(value) | ||||||||||||||
else | ||||||||||||||
@head = Node.new(value, @head) | ||||||||||||||
end | ||||||||||||||
Comment on lines
+24
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. Could be simplified as:
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) because worst case it needs to run through all items in list | ||||||||||||||
# 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 | ||||||||||||||
return false if @head.nil? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
|
||||||||||||||
until pointer.nil? | ||||||||||||||
if pointer.data == value | ||||||||||||||
return true | ||||||||||||||
end | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return false | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method to return the max value in the linked list | ||||||||||||||
# returns the data value and not the node | ||||||||||||||
def find_max | ||||||||||||||
Comment on lines
50
to
52
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? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
max = @head.data | ||||||||||||||
|
||||||||||||||
until pointer.nil? | ||||||||||||||
if pointer.data > max | ||||||||||||||
max = pointer.data | ||||||||||||||
end | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return max | ||||||||||||||
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) bc it has to run through every item | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def find_min | ||||||||||||||
Comment on lines
+70
to
72
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? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
min = @head.data | ||||||||||||||
|
||||||||||||||
until pointer.nil? | ||||||||||||||
if pointer.data < min | ||||||||||||||
min = pointer.data | ||||||||||||||
end | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return min | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
# method that returns the length of the singly linked list | ||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
# Time Complexity: O(n) bc it has to run through every item | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def length | ||||||||||||||
Comment on lines
+90
to
92
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? | ||||||||||||||
|
||||||||||||||
count = 1 | ||||||||||||||
pointer = @head | ||||||||||||||
|
||||||||||||||
until pointer.next.nil? | ||||||||||||||
pointer = pointer.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) could possibly have to run through every item in list | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def get_at_index(index) | ||||||||||||||
Comment on lines
+109
to
111
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? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
pointer_index = 0 | ||||||||||||||
until pointer.nil? | ||||||||||||||
if pointer_index == index | ||||||||||||||
return pointer.data | ||||||||||||||
end | ||||||||||||||
pointer_index += 1 | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return nil | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method to print all the values in the linked list | ||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
# Time Complexity: O(n) bc it has to go thru every item on the list | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def visit | ||||||||||||||
Comment on lines
+128
to
130
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? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
|
||||||||||||||
until pointer.nil? | ||||||||||||||
puts pointer.data | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method to delete the first node found with specified value | ||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
# Time Complexity: O(n) because you could look through entire list before you found specified value | ||||||||||||||
# Space Complexity: O(1) | ||||||||||||||
def delete(value) | ||||||||||||||
Comment on lines
+142
to
144
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? | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
prev_node = nil | ||||||||||||||
|
||||||||||||||
until pointer.nil? | ||||||||||||||
if pointer.data == value | ||||||||||||||
if prev_node | ||||||||||||||
prev_node.next = pointer.next | ||||||||||||||
else | ||||||||||||||
@head = pointer.next | ||||||||||||||
end | ||||||||||||||
return | ||||||||||||||
else | ||||||||||||||
prev_node = pointer | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method to reverse the singly linked list | ||||||||||||||
|
@@ -118,22 +203,41 @@ def has_cycle | |||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
def get_first | ||||||||||||||
Comment on lines
203
to
205
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.value | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
def add_last(value) | ||||||||||||||
Comment on lines
211
to
214
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. 👍 , but time/space complexity? |
||||||||||||||
raise NotImplementedError | ||||||||||||||
if @head.nil? | ||||||||||||||
@head = Node.new(value) | ||||||||||||||
return | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
pointer = @head | ||||||||||||||
|
||||||||||||||
until pointer.next.nil? | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
pointer.next = Node.new(value) | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method that returns the value of the last node in the linked list | ||||||||||||||
# returns nil if the linked list is empty | ||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
def get_last | ||||||||||||||
Comment on lines
228
to
232
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. 👍 , but time/space complexity? |
||||||||||||||
raise NotImplementedError | ||||||||||||||
return nil if @head.nil? | ||||||||||||||
pointer = @head | ||||||||||||||
|
||||||||||||||
until pointer.next.nil? | ||||||||||||||
pointer = pointer.next | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return pointer.data | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# method to insert a new node with specific data value, assuming the linked | ||||||||||||||
|
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.
👍