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 : s t a c k . s w i f t
* C r e a t e d T i m e : 2 0 2 3 - 0 1 - 0 9
* 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 Stack {
/* D r i v e r C o d e */
static func main ( ) {
/* 初 始 化 堆 疊 */
// S w i f t 沒 有 內 建 的 堆 疊 類 別 , 可 以 把 A r r a y 當 作 堆 疊 來 使 用
var stack : [ Int ] = [ ]
/* 元 素 入 堆 疊 */
stack . append ( 1 )
stack . append ( 3 )
stack . append ( 2 )
stack . append ( 5 )
stack . append ( 4 )
print ( " 堆疊 stack = \( stack ) " )
/* 訪 問 堆 疊 頂 元 素 */
let peek = stack . last !
print ( " 堆疊頂元素 peek = \( peek ) " )
/* 元 素 出 堆 疊 */
let pop = stack . removeLast ( )
print ( " 出堆疊元素 pop = \( pop ) ,出堆疊後 stack = \( stack ) " )
/* 獲 取 堆 疊 的 長 度 */
let size = stack . count
print ( " 堆疊的長度 size = \( size ) " )
/* 判 斷 是 否 為 空 */
let isEmpty = stack . isEmpty
print ( " 堆疊是否為空 = \( isEmpty ) " )
}
}