class JSStack

Attributes

top[RW]

Public Class Methods

new(data=nil) click to toggle source

Creates a new stack with an optional first item data.

# File lib/js_stack.rb, line 8
def initialize(data=nil)
  @top = StackNode.new data unless data.nil?
  @top ||= nil
end

Public Instance Methods

empty?() click to toggle source

Returns a Boolean indicating whether the stack is empty.

# File lib/js_stack.rb, line 38
def empty?
  @top.nil?
end
peek() click to toggle source

Returns the top value off of the stack without removing it.

# File lib/js_stack.rb, line 32
def peek
  @top.nil? ? nil : @top.data
end
pop() click to toggle source

Pops the top value off of the stack and returns it.

# File lib/js_stack.rb, line 23
def pop
  return if @top.nil?
  popped_top = @top.data
  @top = @top.next
  popped_top
end
push(data) click to toggle source

Pushes the data value provided on to the top of the stack.

# File lib/js_stack.rb, line 15
def push(data)
  node = StackNode.new data
  node.next = @top
  @top = node
end