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

Leaves - Dominique #25

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
149 changes: 135 additions & 14 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

require 'pry'
# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
Expand All @@ -19,54 +19,154 @@ 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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
new_node = Node.new(value)

new_node.next = @head

@head = new_node
# head is not a node, it is just a variable that refers to the first 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
current = @head

until 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

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
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
def find_min

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
end
return nil if @head == nil

current = @head
min = current.data


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

length = 0
current = @head
# current

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)
raise NotImplementedError

Choose a reason for hiding this comment

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

Suggested change
return nil if @head.nil?

current = @head
value = ''

return current.data if index == 0

new_index = index + 1

new_index.times do |count|
return nil if current == nil
value = current.data
current = current.next
end

return value
end

# method to print all the values in the linked list
def visit
raise NotImplementedError
linked_list_array = []

current = @head

until current == nil
linked_list_array.push(current.data)
current = current.next
end

return linked_list_array
end

# method to delete the first node found with specified value
def delete(value)

Choose a reason for hiding this comment

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

⚠️ One small correction to make.

raise NotImplementedError


return nil if @head == nil

current = @head

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

until current == nil
if current.next.data == value

Choose a reason for hiding this comment

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

Suggested change
if current.next.data == value
if !current.next.nil? && current.next.data == value

current.next == current.next.next

Choose a reason for hiding this comment

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

Small bug here

Suggested change
current.next == current.next.next
current.next = current.next.next

return
end

current = current.next
end

return
end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
def reverse
raise NotImplementedError
return if @head.nil?

current = @head
previous = nil

until current.next.nil?
temp = current.next
current.next = previous
previous = current
current = temp
end

current.next = previous
@head = current
end


Expand Down Expand Up @@ -94,18 +194,39 @@ def has_cycle
# returns the value in the first node
# returns nil if the list is empty
def get_first
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
def add_last(value)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

new_node = Node.new(value)

if @head.nil?
add_first(value)
else
current = @head
while current.next != nil
current = current.next
end
current.next = 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
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

while 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 Down
19 changes: 16 additions & 3 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative 'test_helper'



Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe LinkedList do
Expand All @@ -25,7 +26,7 @@
it 'can add values to an empty list' do
# Act
@list.add_first(3)

# Assert
expect(@list.get_first).must_equal 3
end
Expand Down Expand Up @@ -60,6 +61,7 @@
expect(@list.search(3)).must_equal true

expect(@list.search(2)).must_equal true

end

it "returns false if the element is not in the list" do
Expand All @@ -74,7 +76,7 @@
end
end

describe "length" do
describe "length" do
it "will return 0 for an empty list" do
expect(@list.length).must_equal 0
end
Expand Down Expand Up @@ -192,7 +194,7 @@
end
end

describe "nth_from_the_end" do
xdescribe "nth_from_the_end" do
it 'returns nil if n is outside the bounds of the list' do
expect(@list.find_nth_from_end(3)).must_be_nil
end
Expand All @@ -211,6 +213,17 @@
end
end

describe "visit" do
it 'can print all the values in the linked list' do
@list.add_first(4)
@list.add_first(3)
@list.add_first(2)
@list.add_first(1)

expect(@list.visit).must_equal [1, 2, 3, 4]
end
end

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