Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Created October 2, 2015 22:59
Show Gist options
  • Save cacheflow/b762683d06eb9446ea59 to your computer and use it in GitHub Desktop.
Save cacheflow/b762683d06eb9446ea59 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :next_node_in_stack
def initialize(value, next_node_in_stack)
@value = value
@next_node_in_stack = next_node_in_stack
end
end
class LinkedList
attr_accessor :head, :tail
def initialize(head, tail)
@head = nil
@tail = head
end
def add(value)
@added = Node.new(value, @head)
@added.next = @head
@head = @added
end
def remove(value)
return nil if empty?
value = @head.value
@head = @head.next
puts value
end
def find(value)
return self if @value == value
end
def tail
if empty?
return nil
end
end
def length
end
end
=begin
############################
RSPEC CONFIG AND TESTS BELOW
############################
=end
RSpec.configure do |config|
config.failure_color = :magenta
config.tty = true
config.color = true
config.default_formatter = 'doc'
end
describe LinkedList do
it "has a head" do
ll = LinkedList.new
expect(ll.head).to be_nil
ll.add(1)
expect(ll.head.value).to eql(1)
end
it "has a tail" do
ll = LinkedList.new
ll.add(1)
expect(ll.tail.value).to eql(1)
ll.add(2)
expect(ll.tail.value).to eql(2)
expect(ll.tail.next_node).to be_nil
end
it "adds a value" do
ll = LinkedList.new
ll.add(1)
ll.add(2)
ll.add(3)
expect(ll.head.value).to eql(1)
expect(ll.tail.value).to eql(3)
end
it "finds a node" do
ll = LinkedList.new
10.times do |i|
ll.add(i)
end
node_4 = ll.find(4)
expect(node_4.value).to eql(4)
node_5 = ll.find(5)
expect(node_5.value).to eql(5)
end
it "removes a node" do
ll = LinkedList.new
10.times do |i|
ll.add(i)
end
node_4 = ll.find(4)
expect(node_4.next_node.value).to eql(5)
ll.remove(5)
expect(node_4.next_node.value).to eql(6)
end
it "has a length" do
ll = LinkedList.new
expect(ll.length).to eql(0)
ll.add("thefirst")
expect(ll.length).to eql(1)
10.times do |i|
ll.add(i)
end
expect(ll.length).to eql(11)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment