BOJ 18258 큐 2

1 개요[ | ]

BOJ 18258 큐 2


2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int T;
    cin >> T;
    string command;
    int num;
    queue<int> q;
    while(T--) {
        cin >> command;
        if(command=="push") {
            cin >> num;
            q.push(num);
            continue;
        }
        if(command=="pop") {
            if(q.empty()) {
                cout << "-1\n";
                continue;
            }
            cout << q.front() << "\n";
            q.pop();
            continue;
        }
        if(command=="size") {
            cout << q.size() << "\n";
            continue;
        }
        if(command=="empty") {
            cout << q.empty() << "\n";
            continue;
        }
        if(command=="front") {
            if(q.empty()) {
                cout << "-1\n";
                continue;
            }
            cout << q.front() << "\n";
            continue;
        }
        if(command=="back") {
            if(q.empty()) {
                cout << "-1\n";
                continue;
            }
            cout << q.back() << "\n";
            continue;
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}