728x90
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
스택과 반대의 개념인 큐다.
코드 목록은 비슷하게 하되 순서에 주의하여 코드를 작성하면 된다.
import Foundation
let N = Int(readLine()!)!
var queue : [Int] = []
for _ in 0..<N {
//명령어 입력.
let command = readLine()!.split(separator: " ").map{ String($0)}
switch command[0]{
case "push":
push(Int(command[1])!) // 띄어진 정수 부분은 아직 문자열이니 정수형으로 강제할당.
break
case "pop":
print(pop())
break
case "size":
print(size())
break
case "empty":
print(empty())
break
case "front":
print(front())
break
case "back":
print(back())
break
default:
break
}
}
func push(_ element: Int){
queue.append(element)
}
func pop() -> Int {
if queue.isEmpty == true {
return -1
}else {
return queue.removeFirst()
}
}
func size() -> Int {
return queue.count
}
func empty() -> Int {
if queue.isEmpty {
return 1
}else {
return 0
}
}
func front() -> Int{
if queue.isEmpty {
return -1
}else {
return queue.first!
}
}
func back() -> Int{
if queue.isEmpty {
return -1
}else {
return queue.last!
}
}728x90
'PS' 카테고리의 다른 글
| 11866번 요세푸스 문제 : SWIFT (1) | 2022.10.03 |
|---|---|
| 10866번 덱 / swift (1) | 2022.10.03 |
| 11050번 이항계수 1 / swift (0) | 2022.10.03 |
| 10828번 스택 / swift (0) | 2022.10.03 |
| 9012번 괄호 / swift (0) | 2022.10.02 |