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
- FIFO paging
- C#
- C++
- 열혈 TCP/IP 소켓 프로그래밍
- Spring
- redis
- inflearn
- Operating System
- TCP/IP
- Window-Via-c/c++
- 2475번
- OS
- Four Squares
- 제프리리처
- 10026번
- 윤성우 저자
- n타일링2
- 토마토
- 김영한
- 이펙티브코틀린
- 열혈 tcp/ip 프로그래밍
- 에러핸들링
- Operating System.
- HTTP
- 스프링 입문
- 스프링 핵심 원리
- 우아한 테크 세미나
- 우아한레디스
- BOJ
- 운영체제
Archives
- Today
- Total
나의 브을로오그으
[c++] 10845번 : 큐 본문
https://www.acmicpc.net/problem/10845
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
enum Instructor {
Push,
Pop,
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 Queue {
public:
Queue() {
left = new Node<T>(-1);
right = new Node<T>(-1);
left->prev = nullptr;
left->next = right;
right->prev = left;
right->next = nullptr;
length = 0;
capacity = 1;
}
~Queue() {
while (left != nullptr)
{
Node<T>* nextNode = left->next;
delete left;
left = nextNode;
}
}
void push(T data) {
if (length == capacity) {
capacity *= 2;
}
Node<T>* newNode = new Node<T>(data);
Node<T>* prevNode = right->prev;
prevNode->next = newNode;
right->prev = newNode;
newNode->next = right;
newNode->prev = prevNode;
++length;
return;
}
T pop() {
if (length == 0) {
return -1;
}
Node<T>* delNode = left->next;
T data = delNode->data;
Node<T>* nextNode = delNode->next;
nextNode->prev = left;
left->next = nextNode;
delete delNode;
--length;
return data;
}
int size() const {
return length;
}
int empty() const {
return length == 0;
}
T front() {
if (length == 0) {
return -1;
}
else
{
return left->next->data;
}
}
T back() {
if (length == 0) {
return -1;
}
else
{
return right->prev->data;
}
}
private:
Node<T>* left;
Node<T>* right;
int length;
int capacity;
};
vector<string> split(string input, char delimiter) {
vector<string> instructor;
stringstream ss(input);
string temp;
while (getline(ss, temp, delimiter)) {
instructor.push_back(temp);
}
return instructor;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
string inst[6] = { "push", "pop", "size", "empty", "front", "back"};
Queue<int> q;
string input = "";
int N = 0;
cin >> N;
cin.ignore();
for (int i = 0; i < N; ++i)
{
input.clear();
getline(cin, input);
vector<string> output = split(input, ' ');
for (int j = 0; j < 6; ++j)
{
if (output[0] == inst[j])
{
switch (j)
{
case Instructor::Push:
q.push(atoi(output[1].c_str()));
break;
case Instructor::Pop:
cout << q.pop() << '\n';
break;
case Instructor::Size:
cout << q.size() << '\n';
break;
case Instructor::Empty:
cout << q.empty() << '\n';
break;
case Instructor::Front:
cout << q.front() << '\n';
break;
case Instructor::Back:
cout << q.back() << '\n';
break;
}
}
}
}
return 0;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 10989번 : 수 정렬하기3 (0) | 2022.05.10 |
---|---|
[c++] 10866번 : 덱 (0) | 2022.05.10 |
[c++] 10828번 : 스택 (0) | 2022.05.08 |
[c++] 10816번 : 숫자 카드2 (0) | 2022.05.05 |
[c++] 10814번 : 나이순 정렬 (0) | 2022.05.05 |