#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct Queue {

int data[10000];
int begin,end;

Queue() { //따로 큐 안에 비긴 엔드 선언
begin = 0, end = 0;
}

void push(int x) {
data[end] = x;
end += 1;

}

int pop() {
if (empty()) { return -1; }
//data[begin] = 0;
begin += 1;
return data[begin - 1];
//왜 return[begin] 하고 begin+=1하면 안되는지 헷갈림
}

int size() {
return end-begin;
}
bool empty() {

if (size() == 0) { return 1; }
else { return 0; }
}

int front() {
if (empty()) {
return -1;
}
else {
return data[begin];
}
}

int back() {
if (empty()) {
return -1;
}
else {
return data[end-1];
}
}

};



int main() {

int num;
cin >> num; //처음 입력 받는 줄 수

Queue q; //Queue 사용

while (num--) {
string s;
cin >> s;
if (s == "push") {
int n;
cin >> n; //push할 숫자 입력
q.push(n);
}

else if (s == "size") {
cout << q.size() << endl;
}
else if (s == "empty") {
cout << q.empty() << endl;


}
else if (s == "front") {
cout << ((q.empty()) ? -1 : q.front()) << endl;
}
else if (s == "back") {
cout << ((q.empty()) ? -1 : q.back()) << endl;
}

else if (s == "pop") {
cout << ((q.empty()) ? -1 : q.front()) << endl;
if (!q.empty()) {
q.pop();
}
}
}
}

+ Recent posts