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/zh-hant/codes/swift/chapter_stack_and_queue/deque.swift

45 lines
1.4 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: deque.swift
* Created Time: 2023-01-14
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Deque {
/* Driver Code */
static func main() {
/* */
// Swift Array 使
var deque: [Int] = []
/* */
deque.append(2)
deque.append(5)
deque.append(4)
deque.insert(3, at: 0)
deque.insert(1, at: 0)
print("雙向佇列 deque = \(deque)")
/* */
let peekFirst = deque.first!
print("佇列首元素 peekFirst = \(peekFirst)")
let peekLast = deque.last!
print("佇列尾元素 peekLast = \(peekLast)")
/* */
// 使 Array popFirst O(n)
let popFirst = deque.removeFirst()
print("佇列首出列元素 popFirst = \(popFirst),佇列首出列後 deque = \(deque)")
let popLast = deque.removeLast()
print("佇列尾出列元素 popLast = \(popLast),佇列尾出列後 deque = \(deque)")
/* */
let size = deque.count
print("雙向佇列長度 size = \(size)")
/* */
let isEmpty = deque.isEmpty
print("雙向佇列是否為空 = \(isEmpty)")
}
}