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

40 lines
958 B
Swift
Raw Normal View History

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