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