mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 22:06:27 +08:00
b2f0d4603d
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
37 lines
763 B
Ruby
37 lines
763 B
Ruby
=begin
|
|
File: stack.rb
|
|
Created Time: 2024-04-06
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
=end
|
|
|
|
### Driver Code ###
|
|
if __FILE__ == $0
|
|
# 初始化堆疊
|
|
# Ruby 沒有內建的堆疊類別,可以把 Array 當作堆疊來使用
|
|
stack = []
|
|
|
|
# 元素入堆疊
|
|
stack << 1
|
|
stack << 3
|
|
stack << 2
|
|
stack << 5
|
|
stack << 4
|
|
puts "堆疊 stack = #{stack}"
|
|
|
|
# 訪問堆疊頂元素
|
|
peek = stack.last
|
|
puts "堆疊頂元素 peek = #{peek}"
|
|
|
|
# 元素出堆疊
|
|
pop = stack.pop
|
|
puts "出堆疊元素 pop = #{pop}"
|
|
puts "出堆疊後 stack = #{stack}"
|
|
|
|
# 獲取堆疊的長度
|
|
size = stack.length
|
|
puts "堆疊的長度 size = #{size}"
|
|
|
|
# 判斷是否為空
|
|
is_empty = stack.empty?
|
|
puts "堆疊是否為空 = #{is_empty}"
|
|
end
|