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/codes/ruby/chapter_stack_and_queue/queue.rb

38 lines
806 B

=begin
File: queue.rb
Created Time: 2024-04-06
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Driver Code ###
# 初始化队列
# 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}"