#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Deque
{
private:
int deque[10001] = { 0, };
int first = 0, end = 0;
public:
Deque()
{
}
~Deque()
{
}
public:
void Push_front(int num)
{
if (end != 0)
{
for (int i = end; i > 0; --i)
{
deque[i] = deque[i - 1];
}
}
deque[first] = num;
end++;
}
void Push_back(int num)
{
deque[end] = num;
end++;
}
int Pop_front()
{
if (empty())
{
return -1;
}
int output = deque[first];
for (int i = 0; i <= end; ++i)
{
deque[i] = deque[i + 1];
}
end--;
return output;
}
int Pop_back()
{
if (empty())
{
return -1;
}
end--;
int output = deque[end];
return output;
}
int Size()
{
return end;
}
bool empty()
{
if (Size() == 0)
{
return true;
}
else
{
return false;
}
}
int front()
{
if (empty())
{
return -1;
}
return deque[first];
}
int back()
{
if (empty())
{
return -1;
}
int a = end - 1;
return deque[a];
}
};
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int input = 0;
cin >> input;
Deque q;
for (int i = 0; i < input; ++i)
{
string order;
cin >> order;
if (order == "push_front")
{
int num = 0;
cin >> num;
q.Push_front(num);
}
else if (order == "push_back")
{
int num = 0;
cin >> num;
q.Push_back(num);
}
else if (order == "pop_front")
{
cout << q.Pop_front() << "\n";
}
else if (order == "pop_back")
{
cout << q.Pop_back() << "\n";
}
else if (order == "size")
{
cout << q.Size() << "\n";
}
else if (order == "empty")
{
cout << q.empty() << "\n";
}
else if (order == "front")
{
cout << q.front() << "\n";
}
else if (order == "back")
{
cout << q.back() << "\n";
}
}
return 0;
}
코딩 테스트