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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 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)")
}
}