hello-algo/zh-hant/codes/ruby/chapter_stack_and_queue/queue.rb
Yudong Jin b2f0d4603d
Many bug fixes and improvements (#1270)
* 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
2024-04-11 20:18:19 +08:00

38 lines
878 B
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

=begin
File: queue.rb
Created Time: 2024-04-06
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Driver Code ###
if __FILE__ == $0
# 初始化佇列
# Ruby 內建的佇列Thread::Queue) 沒有 peek 和走訪方法,可以把 Array 當作佇列來使用
queue = []
# 元素入列
queue.push(1)
queue.push(3)
queue.push(2)
queue.push(5)
queue.push(4)
puts "佇列 queue = #{queue}"
# 訪問佇列元素
peek = queue.first
puts "佇列首元素 peek = #{peek}"
# 元素出列
# 清注意由於是陣列Array#shift 方法時間複雜度為 O(n)
pop = queue.shift
puts "出列元素 pop = #{pop}"
puts "出列後 queue = #{queue}"
# 獲取佇列的長度
size = queue.length
puts "佇列長度 size = #{size}"
# 判斷佇列是否為空
is_empty = queue.empty?
puts "佇列是否為空 = #{is_empty}"
end