큐란 자료구조 중 하나로 Swift에는 존재하지 않으므로 직접 구현해야 합니다

 

이번에는 6등 달성하였지만 그 이상으로는 더 줄일 수 없었습니다.

 

Queue란

Queue는 FIFO 의 특징을 가지고 있는 자료 구조입니다. 즉 가장 먼저 들어왔으면 가장 먼저 나가는 특징입니다.

예를 들어 1,2,3,4,5 가 순서대로 들어왔을 때 pop을 한다면 1,2,3,4,5 순서대로 pop 한다는 의미입니다.

var arr: [Int] = []

for _ in 1...Int(readLine()!)! {
    let tmp = readLine()!.split(separator: " ")
    let command = String(tmp[0])
    
    if command == "push" {
        let value = Int(tmp[1])
        arr.append(value!)
    } else if command == "pop" {
        print(arr.isEmpty ? -1 : arr.first!)
        if !arr.isEmpty { arr.removeFirst() }
    } else if command == "size" {
        print(arr.count)
    } else if command == "empty" {
        print(arr.isEmpty ? 1 : 0)
    } else if command == "front" {
        print(arr.isEmpty ? -1 : arr.first!)
    } else if command == "back" {
        print(arr.isEmpty ? -1 : arr.last!)
    }
}
ytw_developer