C++)
#include <iostream>
#include <string>
#include <queue>
#define endl "\n"
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string str;
queue<int> q;
for(int i = 0; i < n; i++) {
cin >> str;
switch(str[1]) {
case 'u':
int temp;
cin >> temp;
q.push(temp);
break;
case 'o':
if(q.empty()) cout << "-1" << endl;
else
{
cout << q.front() << endl;
q.pop();
}
break;
case 'i':
cout << q.size() << endl;
break;
case 'm':
if(q.empty()) cout << 1 << endl;
else cout << 0 << endl;
break;
case 'r':
if(q.empty()) cout << -1 << endl;
else cout << q.front() << endl;
break;
case 'a':
if(q.empty()) cout << -1 << endl;
else cout << q.back() << endl;
break;
}
}
return 0;
}
Python)
import sys
from collections import deque
read = sys.stdin.readline
n = int(read().rstrip())
q = deque()
for _ in range(n):
arr = list(read().rstrip().split())
if arr[0] == 'push':
q.append(int(arr[1]))
elif arr[0] == 'pop':
if q:
print(q[0])
q.popleft()
else:
print(-1)
elif arr[0] == 'size':
print(len(q))
elif arr[0] == 'empty':
print(0 if q else 1)
elif arr[0] == 'front':
print(q[0] if q else -1)
elif arr[0] == 'back':
print(q[-1] if q else -1)'백준 1 > 자료구조' 카테고리의 다른 글
| [백준 1158] 요세푸스 문제 (C++/Python) (0) | 2020.12.06 |
|---|---|
| [백준 2161] 카드1 (C++/Python) (0) | 2020.12.06 |
| [백준 9012] 괄호 (C++/Python) (0) | 2020.12.06 |
| [백준 10828] 스택 (C++/Python) (0) | 2020.12.06 |
| [백준 10773] 제로 (C++/Python) (0) | 2020.12.06 |