Last active
June 17, 2022 05:20
-
-
Save nikukyugamer/00cb276e3b47fd58417c6099ac1517e0 to your computer and use it in GitHub Desktop.
Stack を簡易的に作る
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
class Stack | |
attr_reader :stack | |
def initialize | |
@stack = [] | |
end | |
def push(element) | |
@stack.push(element) | |
end | |
def pop | |
pop_value = @stack.first | |
@stack.delete_at(0) | |
pop_value | |
end | |
def peek | |
@stack.first | |
end | |
def empty? | |
@stack.empty? | |
end | |
def size | |
@stack.size | |
end | |
end | |
s = Stack.new | |
s.push("a") | |
s.push("b") | |
s.pop #=> a | |
s.stack #=> ["b"] | |
s.peek #=> b | |
s.empty? #=> false | |
s.size #=> 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment