[BOJ] 10845 : 큐
2024. 8. 7. 12:31ㆍAlgorithm
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;
}
'Algorithm' 카테고리의 다른 글
| [BOJ] 10866번 : 덱 (0) | 2024.08.07 |
|---|---|
| [BOJ] 2164번 : 카드2 (0) | 2024.08.07 |
| [BOJ] 2493: 탑 (0) | 2024.08.07 |
| [BOJ]1874번 : 스택 수열 (0) | 2024.08.06 |
| [BOJ]10773번 : 제로 (0) | 2024.08.06 |