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 - Stephanie #31

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
158 changes: 131 additions & 27 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Comment on lines +21 to 23

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 = Node.new(value)
else
@head = Node.new(value, @head)
end
Comment on lines +24 to +28

Choose a reason for hiding this comment

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

Could be simplified as:

Suggested change
if @head.nil?
@head = Node.new(value)
else
@head = Node.new(value, @head)
end
@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) 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

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?

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

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?

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

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?

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

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 = 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

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?

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

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?

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

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?

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
Expand Down Expand Up @@ -118,22 +203,41 @@ def has_cycle
# Time Complexity: ?
# Space Complexity: ?
def get_first
Comment on lines 203 to 205

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.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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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
Expand Down
14 changes: 7 additions & 7 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
end

it "returns false if the element is not in the list" do
@list = LinkedList.new
@list.add_first(3)
@list.add_first(2)
expect(@list.search("pasta")).must_equal false
@list = LinkedList.new
@list.add_first(3)
@list.add_first(2)
expect(@list.search("pasta")).must_equal false
end

it "returns false for an empty list" do
expect(@list.search(3)).must_equal false
expect(@list.search(3)).must_equal false
end
end

Expand Down Expand Up @@ -107,7 +107,7 @@
end
end

xdescribe "Optional addLast & getLast" do
describe "Optional addLast & getLast" do
it "will add to the front if the list is empty" do
@list.add_last(1)
expect(@list.get_at_index(0)).must_equal 1
Expand Down Expand Up @@ -211,7 +211,7 @@
end
end

describe "reverse" do
xdescribe "reverse" do
it 'can retrieve an item at index n from the end in the list' do
@list.add_first(4)
@list.add_first(3)
Expand Down