Skip to content

Instantly share code, notes, and snippets.

@iandouglas
Created January 13, 2021 04:45
Show Gist options
  • Save iandouglas/21479de34f46fc8955f7a7340b0187dc to your computer and use it in GitHub Desktop.
Save iandouglas/21479de34f46fc8955f7a7340b0187dc to your computer and use it in GitHub Desktop.
Linked List Starter Kit
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
require './linkedlist'
require './node'
class NodeTest < Minitest::Test
def test_it_exists
node = Node.new('Alan Turing')
assert_instance_of Node, node
end
def test_it_has_a_value
node = Node.new('Alan Turing')
assert_equal 'Alan Turing', node.data
end
def test_it_has_a_next_attribute_set_to_nil
node = Node.new('Alan Turing')
assert_nil node.next
end
end
class LinkedListTest < Minitest::Test
def test_it_exists
list = LinkedList.new
assert_instance_of LinkedList, list
end
def test_it_has_a_head_node_set_to_nil
list = LinkedList.new
assert_nil list.head
end
def test_it_can_append_a_node
list = LinkedList.new
list.append('hello')
refute_nil list.head
assert_equal 'hello', list.head.data
end
def test_it_can_append_several_nodes_in_the_right_order
list = LinkedList.new
list.append('hello')
list.append('world')
list.append('!')
refute_nil list.head
assert_equal 'hello', list.head.data
assert_equal 'world', list.head.next.data
assert_equal '!', list.head.next.next.data
end
def test_it_can_prepend_several_nodes_in_the_right_order
list = LinkedList.new
list.prepend('!')
list.prepend('world')
list.prepend('hello')
refute_nil list.head
assert_equal 'hello', list.head.data
assert_equal 'world', list.head.next.data
assert_equal '!', list.head.next.next.data
end
def test_it_can_count_nodes_in_list
list = LinkedList.new
assert_equal 0, list.count
list.append('hello')
assert_equal 1, list.count
list.append('world')
assert_equal 2, list.count
list.append('!')
assert_equal 3, list.count
end
def test_it_can_join_list_as_a_string
list = LinkedList.new
assert_equal '', list.generate_string
list.append('hello')
assert_equal 'hello', list.generate_string
list.append('world')
assert_equal 'hello world', list.generate_string
list.append('!')
assert_equal 'hello world !', list.generate_string
end
def test_it_can_insert_data_partway_through_the_list
# build a method that skips "n" elements before inserting our value
list = LinkedList.new
# we cannot set a value at a position if our list is empty
refute list.insert(1, 'second')
refute list.insert(5, 'sixth')
list.append('first')
assert list.insert(1, 'second')
assert_equal 'second', list.head.next.data
# starting over
list = LinkedList.new
list.append('first')
list.append('third')
list.append('fifth')
list.insert(2, 'fourth')
list.insert(1, 'second')
assert_equal 'first second third fourth fifth', list.generate_string
end
def test_it_can_find_the_nth_node
list = LinkedList.new
assert_nil list.find(0)
assert_nil list.find(-5)
list.append('hello')
assert_equal 'hello', list.find(1).data
list.append('world')
list.append('!')
assert_equal 'world', list.find(2).data
assert_nil list.find(99)
end
def test_it_can_search_for_data
list = LinkedList.new
list.append('hello')
list.append('world')
list.append('!')
assert list.includes?('!')
refute list.includes?('Alan Turing')
end
def test_it_can_pop_last_item_from_list
list = LinkedList.new
assert_nil list.pop
list.append('hello')
assert_equal 'hello', list.pop.data
assert_nil list.head
assert_equal 0, list.count
list = LinkedList.new
list.append('hello')
list.append('world')
list.append('!')
list.append('oops, remove this')
node = list.pop
assert_equal 'oops, remove this', node.data
assert_equal 'hello world !', list.generate_string
end
end
class LinkedList
attr_reader :head
def initialize
@head = nil
end
def append(value)
# add our value as a node at the very end of our linked list
end
def prepend(value)
# add a value at the start of our linked list, like "unshift" would do to an array
end
def insert(skip_count, value)
# return a 'false' value if our @head node is nil
# return a false value if we try to skip too many things
# how will we "skip" through the linked list to insert things in the right spot?
# insert our new node into the linked list
# return a true value
end
def count
# return 0 of our list is empty
# otherwise, count every node in our list
# and return that count
end
def generate_string
# hint: do not use an array here, build the string as you navigate the linked list
# return '' if our linked list is empty
# otherwise, compose a string, letter by letter as you traverse through the linked list
# and return the string
end
def find(position)
# here, we want to find the "n-th" node in our linked list
# ie, if our linked list was h->e->l->l->o ("hello") and we called find(2), we would return "e"
# return nil if you can't find what you're looking for
end
def includes?(value)
# return true if our linked list contains a node with a given value
# return false otherwise
end
def pop
# remove the last item from our linked list, but "return" that data to use, too
# we want this to act just like an array "pop" where it is destructive, but gives us the old value
end
end
class Node
attr_reader :data
attr_accessor :next
def initialize(payload)
@data = payload
@next = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment