Skip to content

Instantly share code, notes, and snippets.

@woodRock
Created September 18, 2018 08:12
Show Gist options
  • Save woodRock/c8d0a68e496c9999668f1d569d7c1a84 to your computer and use it in GitHub Desktop.
Save woodRock/c8d0a68e496c9999668f1d569d7c1a84 to your computer and use it in GitHub Desktop.
Each method should run in constant time. Implement a stack that has the following methods:
#!/usr/bin/env ruby
class Stack
def initialize(init=nil)
@stack = []
@stack << init if !init.nil?
end
def push val
@stack << val
end
alias << push
def pop
return nil if @stack.empty?
return @stack.pop()
end
def max
return nil if @stack.empty?
return @stack.max()
end
def to_s
return @stack.to_s
end
end
stack = Stack.new()
stack << '1'
stack << '2'
puts stack.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment