Files
hello-algo/ja/codes/ruby/chapter_stack_and_queue/queue.rb
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

39 lines
1.0 KiB
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
### 実行用コード ###
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