Skip to content

Instantly share code, notes, and snippets.

@descartez
Last active May 16, 2016 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save descartez/51d7d05589a1fe63eb10e15ae92d6606 to your computer and use it in GitHub Desktop.
Save descartez/51d7d05589a1fe63eb10e15ae92d6606 to your computer and use it in GitHub Desktop.
require "test/unit"
class ListTester < Test::Unit::TestCase
def test_node_integrity
node = Node.new(1)
assert_equal(1,node.value)
assert_equal(nil,node.next_node)
assert_equal(nil,node.previous_node)
end
def test_list_creation
list = LinkedList.new()
assert_equal(list.root,nil)
end
def test_list_integrity
list = LinkedList.new
# tests a blank list
assert_equal(list.root,nil)
assert_equal(list.current_node,nil)
# tests list with one integer element
list.push_to(1)
assert_equal(list.root.value,1)
assert_equal(list.current_node.value,1)
# tests list with object as a value
list = LinkedList.new
node = Node.new(1)
list.push_to(node)
assert_equal(list.root.value,node)
assert_equal(list.current_node.value,node)
end
def test_adds_node_with_root
node1 = Node.new(1)
list = LinkedList.new(node1)
assert_equal(list.root.value,1)
list.push_to(2)
assert_equal(list.root.next_node.value, 2)
list.push_to(3)
assert_equal(list.current_node.value, 3)
list.push_to(4)
assert_equal(list.current_node.value, 4)
assert_equal(list.length, 4)
end
def test_adds_node_without_root
list = LinkedList.new()
list.push_to(1)
assert_equal(list.root.value,1)
list.push_to(2)
assert_equal(list.root.next_node.value, 2)
list.push_to(3)
assert_equal(list.current_node.value, 3)
list.push_to(4)
assert_equal(list.current_node.value, 4)
assert_equal(list.length, 4)
end
# def test_present?
# list = LinkedList.new()
# list.push_to(1)
# list.push_to(2)
# list.push_to(3)
# list.push_to(4)
# assert_equal(list.present?(1, list.current_node), true)
# assert_equal(list.present?(2, list.current_node), true)
# assert_equal(list.present?(3, list.current_node), true)
# assert_equal(list.present?(4, list.current_node), true)
# end
# def test_delete
# list = LinkedList.new()
# list.push_to(1)
# list.push_to(2)
# list.push_to(3)
# list.push_to(4)
# assert_equal(list.present?(2),true)
# list.delete(2)
# assert_equal(list.present?(2),false)
# end
end
class LinkedList
attr_accessor :root, :length, :current_node
def initialize(root=nil)
@root = root
@root.nil? ? @length = 0 : @length = 1
@current_node = root
end
def progress_node
@current_node = @current_node.next_node
end
def push_to(value)
if @root==nil
@root = Node.new(value)
@current_node = root
else
new_node = Node.new(value)
@current_node.next_node = new_node
progress_node
end
@length += 1
end
def present?(value, node)
if node.value == value && node.next_node != nil
return true
else
progress_node
present?(value, @current_node)
end
end
def go_to(value)
place = 0
until place == value
progress_node
place += 1
end
return @current_node
end
def delete(value)
end
def list
if @root.next_node != nil
p @root.value
@root = @root.next_node
else
p "end"
end
end
end
class Node
attr_accessor :value, :next_node, :previous_node
def initialize(value=nil)
@value = value
@next_node = nil
@previous_node = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment