hello-algo/codes/swift/chapter_stack_and_queue/deque.swift

44 lines
1.3 KiB
Swift
Raw Permalink 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: 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)")
}
}