You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
396 B
Ruby

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