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