Files
hello-algo/ko/codes/swift/chapter_stack_and_queue/array_stack.swift
T
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

86 lines
1.9 KiB
Swift

/**
* File: array_stack.swift
* Created Time: 2023-01-09
* Author: nuomi1 ([email protected])
*/
/* 배열로 구현한 스택 */
class ArrayStack {
private var stack: [Int]
init() {
// 리스트(동적 배열)를 초기화한다
stack = []
}
/* 스택의 길이 반환 */
func size() -> Int {
stack.count
}
/* 스택이 비어 있는지 판단 */
func isEmpty() -> Bool {
stack.isEmpty
}
/* 스택에 삽입 */
func push(num: Int) {
stack.append(num)
}
/* 스택에서 꺼내기 */
@discardableResult
func pop() -> Int {
if isEmpty() {
fatalError("스택이 비어 있다")
}
return stack.removeLast()
}
/* 스택 맨 위 원소 접근 */
func peek() -> Int {
if isEmpty() {
fatalError("스택이 비어 있다")
}
return stack.last!
}
/* List를 Array로 변환하여 반환 */
func toArray() -> [Int] {
stack
}
}
@main
enum _ArrayStack {
/* 실행 코드 */
static func main() {
/* 스택 초기화 */
let stack = ArrayStack()
/* 스택에 원소 삽입 */
stack.push(num: 1)
stack.push(num: 3)
stack.push(num: 2)
stack.push(num: 5)
stack.push(num: 4)
print("스택 stack = \(stack.toArray())")
/* 스택 맨 위 원소 접근 */
let peek = stack.peek()
print("스택의 맨 위 원소 peek = \(peek)")
/* 스택에서 원소 꺼내기 */
let pop = stack.pop()
print("스택에서 꺼낸 원소 pop = \(pop), 꺼낸 후 stack = \(stack.toArray())")
/* 스택의 길이 반환 */
let size = stack.size()
print("스택의 길이 size = \(size)")
/* 비어 있는지 판단 */
let isEmpty = stack.isEmpty()
print("스택이 비어 있는가 = \(isEmpty)")
}
}