mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 10:26:48 +08:00 
			
		
		
		
	 7359a7cb4b
			
		
	
	7359a7cb4b
	
	
	
		
			
			* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
		
			
				
	
	
		
			114 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
| /**
 | ||
|  * File: array_queue.swift
 | ||
|  * Created Time: 2023-01-11
 | ||
|  * Author: nuomi1 (nuomi1@qq.com)
 | ||
|  */
 | ||
| 
 | ||
| /* 基于环形数组实现的队列 */
 | ||
| class ArrayQueue {
 | ||
|     private var nums: [Int] // 用于存储队列元素的数组
 | ||
|     private var front: Int // 队首指针,指向队首元素
 | ||
|     private var _size: Int // 队列长度
 | ||
| 
 | ||
|     init(capacity: Int) {
 | ||
|         // 初始化数组
 | ||
|         nums = Array(repeating: 0, count: capacity)
 | ||
|         front = 0
 | ||
|         _size = 0
 | ||
|     }
 | ||
| 
 | ||
|     /* 获取队列的容量 */
 | ||
|     func capacity() -> Int {
 | ||
|         nums.count
 | ||
|     }
 | ||
| 
 | ||
|     /* 获取队列的长度 */
 | ||
|     func size() -> Int {
 | ||
|         _size
 | ||
|     }
 | ||
| 
 | ||
|     /* 判断队列是否为空 */
 | ||
|     func isEmpty() -> Bool {
 | ||
|         size() == 0
 | ||
|     }
 | ||
| 
 | ||
|     /* 入队 */
 | ||
|     func push(num: Int) {
 | ||
|         if size() == capacity() {
 | ||
|             print("队列已满")
 | ||
|             return
 | ||
|         }
 | ||
|         // 计算队尾指针,指向队尾索引 + 1
 | ||
|         // 通过取余操作实现 rear 越过数组尾部后回到头部
 | ||
|         let rear = (front + size()) % capacity()
 | ||
|         // 将 num 添加至队尾
 | ||
|         nums[rear] = num
 | ||
|         _size += 1
 | ||
|     }
 | ||
| 
 | ||
|     /* 出队 */
 | ||
|     @discardableResult
 | ||
|     func pop() -> Int {
 | ||
|         let num = peek()
 | ||
|         // 队首指针向后移动一位,若越过尾部,则返回到数组头部
 | ||
|         front = (front + 1) % capacity()
 | ||
|         _size -= 1
 | ||
|         return num
 | ||
|     }
 | ||
| 
 | ||
|     /* 访问队首元素 */
 | ||
|     func peek() -> Int {
 | ||
|         if isEmpty() {
 | ||
|             fatalError("队列为空")
 | ||
|         }
 | ||
|         return nums[front]
 | ||
|     }
 | ||
| 
 | ||
|     /* 返回数组 */
 | ||
|     func toArray() -> [Int] {
 | ||
|         // 仅转换有效长度范围内的列表元素
 | ||
|         (front ..< front + size()).map { nums[$0 % capacity()] }
 | ||
|     }
 | ||
| }
 | ||
| 
 | ||
| @main
 | ||
| enum _ArrayQueue {
 | ||
|     /* Driver Code */
 | ||
|     static func main() {
 | ||
|         /* 初始化队列 */
 | ||
|         let capacity = 10
 | ||
|         let queue = ArrayQueue(capacity: capacity)
 | ||
| 
 | ||
|         /* 元素入队 */
 | ||
|         queue.push(num: 1)
 | ||
|         queue.push(num: 3)
 | ||
|         queue.push(num: 2)
 | ||
|         queue.push(num: 5)
 | ||
|         queue.push(num: 4)
 | ||
|         print("队列 queue = \(queue.toArray())")
 | ||
| 
 | ||
|         /* 访问队首元素 */
 | ||
|         let peek = queue.peek()
 | ||
|         print("队首元素 peek = \(peek)")
 | ||
| 
 | ||
|         /* 元素出队 */
 | ||
|         let pop = queue.pop()
 | ||
|         print("出队元素 pop = \(pop),出队后 queue = \(queue.toArray())")
 | ||
| 
 | ||
|         /* 获取队列的长度 */
 | ||
|         let size = queue.size()
 | ||
|         print("队列长度 size = \(size)")
 | ||
| 
 | ||
|         /* 判断队列是否为空 */
 | ||
|         let isEmpty = queue.isEmpty()
 | ||
|         print("队列是否为空 = \(isEmpty)")
 | ||
| 
 | ||
|         /* 测试环形数组 */
 | ||
|         for i in 0 ..< 10 {
 | ||
|             queue.push(num: i)
 | ||
|             queue.pop()
 | ||
|             print("第 \(i) 轮入队 + 出队后 queue = \(queue.toArray())")
 | ||
|         }
 | ||
|     }
 | ||
| }
 |