hello-algo/zh-hant/codes/swift/chapter_stack_and_queue/queue.swift

41 lines
1.0 KiB
Swift
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.

/**
* File: queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Queue {
/* Driver Code */
static func main() {
/* */
// Swift Array 使
var queue: [Int] = []
/* */
queue.append(1)
queue.append(3)
queue.append(2)
queue.append(5)
queue.append(4)
print("佇列 queue = \(queue)")
/* */
let peek = queue.first!
print("佇列首元素 peek = \(peek)")
/* */
// 使 Array pop O(n)
let pool = queue.removeFirst()
print("出列元素 pop = \(pool),出列後 queue = \(queue)")
/* */
let size = queue.count
print("佇列長度 size = \(size)")
/* */
let isEmpty = queue.isEmpty
print("佇列是否為空 = \(isEmpty)")
}
}