hello-algo/zh-hant/codes/swift/chapter_stack_and_queue/stack.swift

40 lines
1000 B
Swift
Raw 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: stack.swift
* Created Time: 2023-01-09
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Stack {
/* Driver Code */
static func main() {
/* */
// Swift Array 使
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)")
}
}