Created
June 11, 2025 18:01
-
-
Save jcasimir/61709275c2a7d1a8cf3b41afba3e99c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList | |
attr_reader :head | |
def initialize | |
@head = RecursiveNode.new(nil) | |
end | |
def count | |
head.count | |
end | |
def insert(input) | |
head.insert(RecursiveNode.new(input)) | |
end | |
def include?(target) | |
head.include?(target) | |
end | |
end | |
class RecursiveNode | |
attr_reader :data, :link | |
def initialize(input) | |
@data = input | |
end | |
def insert(node) | |
if link.nil? | |
@link = node | |
else | |
link.insert(node) | |
end | |
end | |
def count | |
if link.nil? | |
0 # Count from 0 because of null object pattern with Head | |
else | |
1 + link.count | |
end | |
end | |
def include?(target) | |
(data == target) || (link && link.include?(target)) | |
end | |
end | |
class Node | |
attr_reader :data | |
attr_accessor :link | |
def initialize(input) | |
@data = input | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IterativeLinkedList | |
def include?(target) | |
current = head | |
while current | |
if current.nil? | |
return false | |
elsif current.data == target | |
return true | |
else | |
current = current.link | |
end | |
end | |
end | |
def insert(input) | |
node = Node.new(input) | |
current = head | |
if current.nil? | |
@head = node | |
else | |
until current.link.nil? | |
current = current.link | |
end | |
current.link = node | |
end | |
end | |
def count | |
output = 0 | |
data.each do |d| | |
output += 1 | |
end | |
list_count = 0 | |
current = head | |
until current.nil? | |
list_count += 1 | |
current = current.link | |
end | |
return list_count | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest' | |
require 'minitest/autorun' | |
require './list' | |
class ListTest < Minitest::Test | |
def test_it_inserts_a_number | |
list = LinkedList.new | |
assert_equal 0, list.count | |
list.insert(5) | |
assert_equal 1, list.count | |
end | |
def test_it_inserts_two_numbers | |
list = LinkedList.new | |
assert_equal 0, list.count | |
list.insert(5) | |
list.insert(10) | |
assert_equal 2, list.count | |
end | |
def test_it_inserts_five_numbers | |
list = LinkedList.new | |
assert_equal 0, list.count | |
list.insert(5) | |
list.insert(10) | |
list.insert(15) | |
list.insert(20) | |
list.insert(25) | |
assert_equal 5, list.count | |
end | |
def test_it_confirms_membership | |
list = LinkedList.new | |
assert_equal 0, list.count | |
list.insert(5) | |
list.insert(10) | |
list.insert(15) | |
list.insert(20) | |
list.insert(25) | |
assert list.include?(20) | |
refute list.include?(30) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I generally don't love ternaries, but the
count
method in the recursive version is somewhere I would consider it like...Or, flipping the logic around:
And then I probably wouldn't use it.