Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Geon

큐[BOJ] 본문

코딩테스트

큐[BOJ]

jgkim1008 2022. 3. 25. 14:14

코드


import Foundation

struct Queue {
    private var list: [Int] = []

    mutating func push(value: Int) {
        list.append(value)
    }

    mutating func pop() -> Int{
        list.isEmpty ? -1 : list.removeFirst()
    }

    func size() -> Int {
        return list.count
    }

    func empty() -> Int {
        return list.isEmpty ? 1 : 0
    }

    func front() -> Int {
        return list.first ?? -1
    }

    func back() -> Int {
        return list.last ?? -1
    }
}


var queue = Queue()

for _ in 0..<Int(readLine()!)! {
    let commnad = readLine()!.split(separator: " ")
    switch commnad[0] {
    case "push":
        queue.push(value: Int(commnad[1])!)  
    case "front":
        print(queue.front())
    case "back":
        print(queue.back())
    case "size":
        print(queue.size())
    case "pop":
        print(queue.pop())
    case "empty":
        print(queue.empty())
    default:
        break
    }
}

'코딩테스트' 카테고리의 다른 글

OddOccurrencesInArray  (0) 2022.05.13
요세푸스 문제[BOJ]  (0) 2022.03.26
괄호[BOJ]  (0) 2022.03.22
단어뒤집기[BOJ]  (0) 2022.03.22
스택[BOJ]  (0) 2022.03.22