[BOJ / C++] 18258 큐2

Seulguo·2022년 7월 14일
0

Algorithm

목록 보기
64/185
post-thumbnail

🐣 문제

링크 : https://www.acmicpc.net/problem/18258


🐥 코드

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main(){
    ios::sync_with_stdio(false); 
    cin.tie(0);
    int n = 0;
    cin >> n;
    queue<int> q;

    for(int i = 0; i < n; i ++){
        string s;
        cin >> s;

        if(s == "push"){
            int num = 0;
            cin >> num;
            q.push(num);
        }
       
        else if(s == "pop"){
            if(q.empty()) cout << "-1" << "\n";
            else {
                cout << q.front() << "\n";
                q.pop();
            }
        }
        
        else if(s == "size") cout << q.size() << "\n";
        
        else if(s == "empty"){
            if(q.empty()) cout << "1" << "\n";
            else cout << "0" << "\n";
        }

        else if(s == "front"){
            if(q.empty()) cout << "-1" << "\n";
            else cout << q.front() << "\n";
        }

        else if(s == "back"){
            if(q.empty()) cout << "-1" << "\n";
            else cout << q.back() << "\n";
        }
    }

    return 0;
}

0개의 댓글