hello-algo/zh-hant/codes/ruby/chapter_stack_and_queue/deque.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

42 lines
1.2 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: deque.rb
Created Time: 2024-04-06
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Driver Code ###
if __FILE__ == $0
# 初始化雙向佇列
# Ruby 沒有內直的雙端佇列,只能把 Array 當作雙端佇列來使用
deque = []
# 元素如隊
deque << 2
deque << 5
deque << 4
# 請注意由於是陣列Array#unshift 方法的時間複雜度為 O(n)
deque.unshift(3)
deque.unshift(1)
puts "雙向佇列 deque = #{deque}"
# 訪問元素
peek_first = deque.first
puts "佇列首元素 peek_first = #{peek_first}"
peek_last = deque.last
puts "佇列尾元素 peek_last = #{peek_last}"
# 元素出列
# 請注意,由於是陣列, Array#shift 方法的時間複雜度為 O(n)
pop_front = deque.shift
puts "佇列首出列元素 pop_front = #{pop_front},佇列首出列後 deque = #{deque}"
pop_back = deque.pop
puts "佇列尾出列元素 pop_back = #{pop_back}, 佇列尾出列後 deque = #{deque}"
# 獲取雙向佇列的長度
size = deque.length
puts "雙向佇列長度 size = #{size}"
# 判斷雙向佇列是否為空
is_empty = size.zero?
puts "雙向佇列是否為空 = #{is_empty}"
end