Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- const int MAX = (10000 + 1) * 2;
- int head = MAX / 2, tail = MAX / 2;
- int arr[MAX];
- // stl 사용하지 않고 구현
- void push_front(int num)
- {
- arr[--head] = num;
- }
- void push_back(int num)
- {
- arr[tail++] = num;
- }
- void pop_front(void)
- {
- head++;
- }
- void pop_back(void)
- {
- tail--;
- }
- int front(void)
- {
- return arr[head];
- }
- int back(void)
- {
- return arr[tail - 1];
- }
- int main(void)
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int N;
- cin >> N;
- for (int n = 0; n < N; n++)
- {
- string s;
- cin >> s;
- if (s == "push_front")
- {
- int num;
- cin >> num;
- push_front(num);
- }
- else if (s == "push_back")
- {
- int num;
- cin >> num;
- push_back(num);
- }
- else if (s == "pop_front")
- {
- if (head == tail)
- cout << -1 << "\n";
- else
- {
- cout << front() << "\n";
- pop_front();
- }
- }
- else if (s == "pop_back")
- {
- if (head == tail)
- cout << -1 << "\n";
- else
- {
- cout << back() << "\n";
- pop_back();
- }
- }
- else if (s == "size")
- {
- cout << tail - head << "\n";
- }
- else if (s == "empty")
- {
- bool flag = (head == tail);
- cout << flag << "\n";
- }
- else if (s == "front")
- {
- if (head == tail)
- cout << -1 << "\n";
- else
- cout << front() << "\n";
- }
- else
- {
- if (head == tail)
- cout << -1 << "\n";
- else
- cout << back() << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement