You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/zh-hant/codes/ruby/chapter_stack_and_queue/queue.rb

39 lines
878 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

=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