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

Dani - Leaves #33

Open
wants to merge 4 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
156 changes: 142 additions & 14 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,141 @@ 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
def add_first(value)
raise NotImplementedError
new_node = Node.new(value, @head)
@head = new_node
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)

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
found = false
until current == nil
if current.data == value
found = true
break
end

current = current.next
end
return found
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max

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
max = current.data

until current == nil
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
def find_min
raise NotImplementedError
return nil if @head == nil

current = @head
min = @head.data
while current != nil
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
def length

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

current = @head
length = 0
until current == nil
length += 1
current = current.next
end
return length
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
def get_at_index(index)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head

index.times do
if current != nil
current = current.next
end
end

if current != nil
return current.data
else
return nil
end
end

# method to print all the values in the linked list
def visit

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

until current == nil
print current.data
current = current.next
end
end

# method to delete the first node found with specified value
def delete(value)
raise NotImplementedError
return nil if @head == nil

Choose a reason for hiding this comment

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

Just noting this method isn't finished.


if @head.data == value
@head = @head.next
end

curr = @head
prev = nil

while curr != nil
if curr.data == value
prev.next = curr.next
end
prev = curr
curr = curr.next
end
return @head
end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
def reverse

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 @@ -79,7 +166,25 @@ def find_middle_value
# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
def find_nth_from_end(n)

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

answer_pointer = @head
scanning_pointer = answer_pointer

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

until scanning_pointer.next == nil
answer_pointer = answer_pointer.next
scanning_pointer = scanning_pointer.next
end

return answer_pointer.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -94,18 +199,41 @@ def has_cycle
# returns the value in the first node
# returns nil if the list is empty
def get_first

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
return @head
else
return @head.data
end
end

# method that inserts a given value as a new last node in the linked list
def add_last(value)

Choose a reason for hiding this comment

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

👍 Good use of add_first

raise NotImplementedError
if @head == nil
add_first(value)
else
current = @head
previous = current
new_last_node = Node.new(value, nil)

until current.next == nil
current = current.next
end
current.next = new_last_node
end
end

# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
def get_last

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

until current.next == nil
current = current.next
end

return current.data
end

# method to insert a new node with specific data value, assuming the linked
Expand All @@ -123,7 +251,7 @@ def create_cycle
# navigate to last node
current = @head
while current.next != nil
current = current.next
current = current.next
end

current.next = @head # make the last node link to first node
Expand Down
4 changes: 2 additions & 2 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@
expect(@list.find_max).must_equal 10
expect(@list.find_min).must_equal 3

# delete last node
# # delete last node
@list.delete(10)
expect(@list.get_first).must_equal 3
expect(@list.length).must_equal 3
expect(@list.get_last).must_equal 9
expect(@list.find_max).must_equal 9
expect(@list.find_min).must_equal 3

# delete fist node (requires updating head)
# # delete fist node (requires updating head)
@list.delete(4)
expect(@list.get_first).must_equal 3
expect(@list.length).must_equal 2
Expand Down