programming-examples/ruby/Data_Structures/linked_stack.rb

27 lines
396 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
require_relative 'singly_linked_list'
class LinkedStack < LinkedList
def empty?
@length == 0
end
def push(data)
new_head = Node.new(data)
new_head.next = @head
@length += 1
@head = new_head
end
def pop
return nil if empty?
data = @head.data
@head = @head.next
@length -= 1
data
end
def peek
self[0]
end
end