Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 우아한 테크 세미나
- BOJ
- FIFO paging
- 스프링 입문
- 열혈 tcp/ip 프로그래밍
- 열혈 TCP/IP 소켓 프로그래밍
- Window-Via-c/c++
- n타일링2
- 스프링 핵심 원리
- 이펙티브코틀린
- 윤성우 저자
- 2475번
- Operating System.
- Spring
- inflearn
- 에러핸들링
- OS
- 10026번
- 토마토
- 운영체제
- Four Squares
- HTTP
- TCP/IP
- 우아한레디스
- redis
- 김영한
- Operating System
- C++
- C#
- 제프리리처
Archives
- Today
- Total
나의 브을로오그으
[c++] 10866번 : 덱 본문
https://www.acmicpc.net/problem/10866
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
enum Instructor
{
PushFront,
PushBack,
PopFront,
PopBack,
Size,
Empty,
Front,
Back
};
template <typename T>
struct Node {
T data;
Node<T>* next;
Node<T>* prev;
Node(T data) {
this->data = data;
next = nullptr;
prev = nullptr;
}
};
template <typename T>
class Deque
{
public:
Deque()
{
left = new Node<T>(-1);
right = new Node<T>(-1);
length = 0;
capacity = 1;
left->prev = nullptr;
left->next = right;
right->prev = left;
right->next = nullptr;
}
~Deque()
{
while (left != nullptr) {
Node<T>* nextNode = left->next;
delete left;
left = nextNode;
}
}
void push_front(T data) {
if (full())
{
capacity *= 2;
}
Node<T>* newNode = new Node<T>(data);
Node<T>* nextNode = left->next;
newNode->next = nextNode;
nextNode->prev = newNode;
newNode->prev = left;
left->next = newNode;
++length;
}
void push_back(T data)
{
if (full())
{
capacity *= 2;
}
Node<T>* newNode = new Node<T>(data);
Node<T>* prevNode = right->prev;
prevNode->next = newNode;
newNode->prev = prevNode;
newNode->next = right;
right->prev = newNode;
++length;
}
T pop_front()
{
if (empty()) {
return -1;
}
Node<T>* delNode = left->next;
Node<T>* nextNode = delNode->next;
T data = delNode->data;
left->next = nextNode;
nextNode->prev = left;
--length;
return data;
}
T pop_back()
{
if (empty())
{
return -1;
}
Node<T>* delNode = right->prev;
T data = delNode->data;
Node<T>* prevNode = delNode->prev;
prevNode->next = right;
right->prev = prevNode;
--length;
return data;
}
int size() const
{
return length;
}
int full() const
{
return length == capacity;
}
int empty() const
{
return length == 0;
}
T front()
{
if (empty())
{
return -1;
}
else
{
return left->next->data;
}
}
T back()
{
if (empty())
{
return -1;
}
else
{
return right->prev->data;
}
}
private:
Node<T>* left;
Node<T>* right;
int length;
int capacity;
};
vector<string> split(string input, char delim)
{
stringstream ss(input);
vector<string> instructor;
string temp;
while (getline(ss, temp, delim))
{
instructor.push_back(temp);
}
return instructor;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
string inst[8] = {
"push_front", "push_back",
"pop_front", "pop_back",
"size", "empty",
"front", "back"
};
Deque<int> dq;
string input = "";
int N = 0;
cin >> N;
cin.ignore();
for (int i = 0; i < N; ++i)
{
getline(cin, input);
vector<string> output = split(input, ' ');
for (int j = 0; j < 8; ++j)
{
if (output[0] == inst[j])
{
switch (j)
{
case Instructor::PushFront:
dq.push_front(atoi(output[1].c_str()));
break;
case Instructor::PushBack:
dq.push_back(atoi(output[1].c_str()));
break;
case Instructor::PopFront:
cout << dq.pop_front() << '\n';
break;
case Instructor::PopBack:
cout << dq.pop_back() << '\n';
break;
case Instructor::Size:
cout << dq.size() << '\n';
break;
case Instructor::Empty:
cout << dq.empty() << '\n';
break;
case Instructor::Front:
cout << dq.front() << '\n';
break;
case Instructor::Back:
cout << dq.back() << '\n';
break;
default:
break;
}
}
}
}
return 0;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 11050번 : 이항 계수 1 (0) | 2022.05.10 |
---|---|
[c++] 10989번 : 수 정렬하기3 (0) | 2022.05.10 |
[c++] 10845번 : 큐 (0) | 2022.05.10 |
[c++] 10828번 : 스택 (0) | 2022.05.08 |
[c++] 10816번 : 숫자 카드2 (0) | 2022.05.05 |