Algorithm

[BOJ] 10845 : 큐

rudgh99_algo 2024. 8. 7. 12:31

1. problem : 

https://www.acmicpc.net/problem/10845

 

2. solution 1 :

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

const int MX = 100001;
int dat[MX];
int fir = 0, fin = 0;

void push(int x) {
    dat[fin++] = x;
}

int pop() {
    if (fir == fin) return -1;
    return dat[fir++];
}

int empty() {
    return (fir == fin) ? 1 : 0;
}

int size() {
    return fin - fir;
}

int front() {
    if (fir == fin) return -1;
    return dat[fir];
}

int back() {
    if (fir == fin) return -1;
    return dat[fin - 1];
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    while (N--) {
        string op;
        cin >> op;
        if (op == "push") {
            int x;
            cin >> x;
            push(x);
        }
        else if (op == "pop") {
            cout << pop() << '\n';
        }
        else if (op == "size") {
            cout << size() << '\n';
        }
        else if (op == "front") {
            cout << front() << '\n';
        }
        else if (op == "back") {
            cout << back() << '\n';
        }
        else if (op == "empty") {
            cout << empty() << '\n';
        }
    }
    return 0;
}