Skip to content

Commit

Permalink
boj: 스택
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrescent61 committed Dec 9, 2021
1 parent 79d86ad commit 75685cf
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Boj_ellen/10828_Ellen.swift
@@ -0,0 +1,38 @@
//
// 10828_Ellen.swift
// Boj_Ellen
//
// Created by Ellen on 2021/12/10.
//

let userInput = Int(readLine()!)!
let params = ["push", "pop", "size", "empty", "top"]
var stack: [Int] = []

for _ in 1...userInput {
let userInput = readLine()!
let splitedInput = userInput.split(separator: " ")
if params.contains(String(splitedInput[0])) {
switch splitedInput[0] {
case "push":
stack.append(Int(splitedInput[1])!)
case "pop":
if stack.isEmpty == true {
print("-1")
} else {
print(stack.popLast()!)
}
case "size":
print(stack.count)
case "empty":
if stack.isEmpty == true {
print("1")
} else {
print("0")
}
case "top":
print(stack.last!)
default: break
}
}
}

0 comments on commit 75685cf

Please sign in to comment.