mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
772183705e
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
39 lines
1.3 KiB
Ruby
39 lines
1.3 KiB
Ruby
=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
|