SDJ( 수돈재 아님 ㅎ )

[C++] 10845 - 큐 본문

알고리즘/Backjoon

[C++] 10845 - 큐

ShinDongJun 2020. 1. 12. 17:01

문제 링크 : https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

www.acmicpc.net

 

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<string>
#include<queue>
 
#define endl '\n'
 
using namespace std;
 
queue<int> Q;
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    string str;
    int t;
    cin >> t;
 
    while(t--)
    {
        cin >> str;
        if(!str.compare("push"))
        {
            int tmp;
            cin >> tmp;
            Q.push(tmp);
        }
        else if(!str.compare("front"))
        {
            if(Q.empty())
                cout << -1 << endl;
            else
                cout << Q.front() << endl;
        }
        else if(!str.compare("back"))
        {
            if(Q.empty())
                cout << -1 << endl;
            else
                cout << Q.back() << endl;
        }
        else if(!str.compare("size"))
        {
            cout << Q.size() << endl;
        }
        else if(!str.compare("empty"))
        {
            cout << Q.empty() << endl;
        }
        else if(!str.compare("pop"))
        {
            if(Q.empty())
                cout << -1 << endl;
            else
            {
                cout << Q.front() << endl;
                Q.pop();
            }
        }
    }
 
    return 0;
}
Comments