Skip to content
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

Ports - Ari #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/.idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/.idea/lib.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

242 changes: 185 additions & 57 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(value, next_node = nil)
@data = value
@next = next_node
end

end

# Defines the singly linked list
Expand All @@ -18,129 +19,255 @@ 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) (constant), because the length of the list doesn't matter
# Space Complexity: O(n), where n is the length of the list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the method itself doesn't add more space to the list, it only adds 1 element.

def add_first(value)
raise NotImplementedError
new_node = Node.new(value, nil)
new_node.next = @head
@head = new_node

return new_node.data
end

# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first
return nil if @head.nil?

return @head.data
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)
raise NotImplementedError
node = @head
return false if !node.next

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if @Head is nil?
What if the value is in the head and the list only has 1 element?

return node if node.value == value
while (node = node.next)
return node if node.value == value
end
end

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
# Time Complexity: O(n), where n is the length of the list
# Space Complexity: O(1)
def find_max
raise NotImplementedError
if @head == nil
return nil
end
current = @head
max = current.data
while current
if current.data > max
max = current.data
end
current = current.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), where n is the length of the list
# Space Complexity: O(1)
def find_min
raise NotImplementedError
if @head == nil
return nil
end
current = @head
min = current.data
while current
if current.data < min
min = current.data
end
current = current.next
end
return min
end


# method that returns the length of the singly linked list
# Time Complexity:
# Space Complexity
# Time Complexity: O(n), where n is the length of the list
# Space Complexity: O(1)
def length
raise NotImplementedError
current = @head

return 0 if current.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this return?

length = 0

until current.nil?
current = current.next
length += 1
end

return length
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: O(n), where n is the length of the list
# Space Complexity: unsure. O(1) because we're not creating a new data structure, we're just adding to
# an existing one?
def add_last(value)
new_node = Node.new(value)
if @head
current = @head
until current.next.nil?
current = current.next
end
current.next = new_node
else
@head = new_node
end
end

# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# Time Complexity: O(n), where n is the length of the list
# Space Complexity: O(1)
def get_last
node = @head

return nil if node.nil?

return node.data if !node.next
return node.data if !node.next while (node = node.next)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at this line. I strongly suggest you revise it so it's more readable.


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)
raise NotImplementedError
return nil if @head.nil?

# I feel like I could do something like use the length method from before and compare the result to the index

count = 0
current = @head
while count != index && !current.next.nil?
current = current.next
count += 1
end
current.data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the index is past the end of the list?

end

# method to print all the values in the linked list
# Time Complexity:
# Space Complexity
# Time Complexity: O(n)
# Space Complexity: O(n)
def visit
raise NotImplementedError
end

# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
def delete(value)
raise NotImplementedError
visit_array = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have some weird indentation here.

if self.length == 0
puts "empty"
else
node = @head
until node.nil?
visit_array << node.data
node = node.next
end
end

return visit_array
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
raise NotImplementedError
return @head if @head.nil? || @head.next.nil?
current_node = @head
previous_node = nil


while current_node

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

temp = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = temp
end
@head = previous_node

end

# method to delete the first node found with specified value
# Time Complexity: O(n)
# Space Complexity: O(1)
def delete(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't working.

current = @head
before = nil
while current
if current.data == value
if !before && !current.next
@head = nil
elsif !before
@head = current.next
else
before.next = current.next
end
return "deleted"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

end
before = current
current = current.next
end
return nil
end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity:
# Time Complexity:
# Space Complexity
def find_middle_value
raise NotImplementedError
end

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity:
# Space Complexity
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(n)
raise NotImplementedError
selected_index = length - n - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you have also used the method to get an element at an index?


if n >= length
return nil
end
current = @head
i = 0
while i < selected_index && current.next
current = current.next
i += 1
end
return current.data


end

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity:
# Time Complexity:
# Space Complexity
def has_cycle
raise NotImplementedError
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity:
# Space Complexity
def get_first
raise NotImplementedError
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:
# Space Complexity
def add_last(value)
raise NotImplementedError
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
raise NotImplementedError
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity:
# Time Complexity:
# Space Complexity
def insert_ascending(value)
raise NotImplementedError
Expand All @@ -161,3 +288,4 @@ def create_cycle
current.next = @head # make the last node link to first node
end
end