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.
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.
/* *
* F i l e : d e q u e . s w i f t
* C r e a t e d T i m e : 2 0 2 3 - 0 1 - 1 4
* A u t h o r : n u o m i 1 ( n u o m i 1 @ q q . c o m )
*/
@ main
enum Deque {
/* D r i v e r C o d e */
static func main ( ) {
/* 初 始 化 双 向 队 列 */
// S w i f t 没 有 内 置 的 双 向 队 列 类 , 可 以 把 A r r a y 当 作 双 向 队 列 来 使 用
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 ) " )
/* 元 素 出 队 */
// 使 用 A r r a y 模 拟 时 p o p F i r s t 的 复 杂 度 为 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 ) " )
}
}