mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* 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
		
			
				
	
	
		
			39 lines
		
	
	
		
			878 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			878 B
		
	
	
	
		
			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
 |