You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/codes/swift/chapter_stack_and_queue/queue.swift

41 lines
1.0 KiB

/**
* 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 poll O(n)
let pool = queue.removeFirst()
print("出队元素 poll = \(pool),出队后 queue = \(queue)")
/* */
let size = queue.count
print("队列长度 size = \(size)")
/* */
let isEmpty = queue.isEmpty
print("队列是否为空 = \(isEmpty)")
}
}