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

Linked List - Gessica #24

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
6 changes: 6 additions & 0 deletions .idea/.gitignore

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

27 changes: 27 additions & 0 deletions .idea/linked-list.iml

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

4 changes: 4 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .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 .idea/vcs.xml

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

177 changes: 143 additions & 34 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,95 +18,204 @@ 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)
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
# raise NotImplementedError
@head = Node.new(value, @head)

Choose a reason for hiding this comment

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

nice

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 +30 to 32

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
current = @head

until current == nil
if current.data == value
return true
else
current = current.next
end
end

return false
end

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_max
Comment on lines +49 to 51

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

max_value = @head.data
current = @head

until current == nil
if current.data > max_value
max_value = current.data
end
current = current.next
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 +71 to 73

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

min_value = @head.data
current = @head

until current == nil
if current.data < min_value
min_value = current.data
end
current = current.next
end

return min_value

end


# 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 +95 to 97

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return @head.nil? ? nil : @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(n)
# Space Complexity: O(1)
def add_last(value)
Comment on lines +103 to 105

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
if @head.nil?
@head = Node.new(value)
else
current = @head
until current.next.nil?
current = current.next
end
current.next = Node.new(value)
end
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 +119 to 121

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
count = 0
current = @head
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 +135 to 137

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
counter = 0
current = @head
until current.nil?
if index == counter
return current.data
else
counter += 1
current = current.next
end
end
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 +152 to 154

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

current = @head
until current.nil?
puts current.data
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 +166 to 168

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

if @head.data == value
@head = @head.next
else
current = @head
until current.next.data == value
current = current.next
end
current.next = current.next.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 +186 to 188

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

current = @head
prev = nil

while current
temp = current.next
current.next = prev
prev = current
current = temp
end
@head = prev

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(n)
# Space Complexity: O(1)
def get_last
Comment on lines +207 to 209

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

current = @head
until current.next.nil?
current = current.next
end

return current.data
end

## Advanced Exercises
Expand Down
2 changes: 1 addition & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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