Created
September 18, 2018 08:12
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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