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

Space - Nikki V #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
200 changes: 162 additions & 38 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,185 @@
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 :previous

def initialize(value, next_node = nil)
def initialize(value, next_node = nil, previous = nil)
@data = value
@next = next_node
@previous = previous
end
end

# Defines the singly linked list
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
@tail = nil
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)
# Space Complexity: O(1)
def add_first(value)
Comment on lines +24 to 26

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @head == nil
@head = @tail = Node.new(value)
else
new_node = Node.new(value, @head)
@head.previous = new_node
@head = new_node
end
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 +38 to 40

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return false if @head == nil

current = @head
while current != nil
return true if current.data == value
current = current.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 52 to 54

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head == nil
max = @head.data
current = @head
until current == nil
max = current.data if current.data > max
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)
# Space Complexity: O(1)
def find_min
Comment on lines +67 to 69

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head == nil
min = @head.data
current = @head
until current == nil
min = current.data if current.data < min
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)
# Space Complexity: O(1)
def length
Comment on lines +82 to 84

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return 0 if @head == nil

count = 0
if @head.next
current = @head
while current != nil
count += 1
current = current.next
end
else
current = @tail
while current != nil
count += 1
current = current.previous
end
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 +108 to 110

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head.nil?
count = 0
current = @head
while count < index
if current.nil?

Choose a reason for hiding this comment

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

Nice that you're checking to see if you're off the list.

return nil
else
current = current.next
count += 1
end
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 +126 to 128

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @head.nil?
print "nil"
return
else
current = @head
while current
print current.data
current = current.next
end
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 +142 to 144

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
while current != nil
if current.data == value
if current.next && current.previous

previous = current.previous
next_node = current.next

previous.next = next_node
next_node.previous = previous

elsif current.previous.nil?
@head = current.next
@head.previous = nil
else
@tail = current.previous
@tail.next = nil
end
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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head.nil?
current = @head
previous = nil

while current != nil
next_value = current.next
current.next = previous
previous = current
current = next_value
end

@head = previous
end


Expand All @@ -96,10 +195,28 @@ def find_middle_value

# 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)
Comment on lines +198 to 200

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head.nil?

value = @head
counter = value

n.times do
if counter.next != nil
counter = counter.next
else
return nil
end
end

until counter.next == nil
value = value.next
counter = counter.next
end

return value.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -115,25 +232,32 @@ def has_cycle
# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first
Comment on lines +235 to 237

Choose a reason for hiding this comment

The 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
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def add_last(value)
Comment on lines +243 to 245

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @head.nil?
@head = @tail = Node.new(value)
else
new_node = Node.new(value, nil, @tail)
@tail.next = new_node
@tail = 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: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_last
Comment on lines +257 to 259

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @tail.data
end

# method to insert a new node with specific data value, assuming the linked
Expand Down