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
- 토마토
- Spring
- TCP/IP
- C++
- 스프링 핵심 원리
- BOJ
- Four Squares
- 제프리리처
- 열혈 TCP/IP 소켓 프로그래밍
- redis
- 김영한
- OS
- 10026번
- 열혈 tcp/ip 프로그래밍
- inflearn
- Operating System.
- 2475번
- Operating System
- Window-Via-c/c++
- HTTP
- C#
- 윤성우 저자
- n타일링2
- 우아한 테크 세미나
- 스프링 입문
- 운영체제
- 에러핸들링
Archives
- Today
- Total
나의 브을로오그으
[c++] 10828번 : 스택 본문
https://www.acmicpc.net/problem/10828
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
#define PUSH 0
#define POP 1
#define SIZE 2
#define TOP 3
#define EMPTY 4
template <typename T>
struct Node
{
T data;
Node<T>* next;
Node<T>* prev;
Node()
{
data = -1;
next = nullptr;
prev = nullptr;
}
Node(T data)
{
this->data = data;
next = nullptr;
prev = nullptr;
}
};
template <typename T>
class Stack
{
public:
Stack()
{
this->current = nullptr;
this->front = new Node<T>();
this->end = new Node<T>();
this->length = 0;
this->capacity = 1;
front->prev = nullptr;
front->next = end;
end->prev = front;
end->next = nullptr;
}
~Stack()
{
Node<T>* node = front;
while (node != nullptr)
{
Node<T>* next = node->next;
delete node;
node = next;
}
}
void push(T data)
{
if (length == capacity)
{
capacity *= 2;
}
Node<T>* newNode = new Node<T>(data);
Node<T>* prevNode = end->prev;
end->prev = newNode;
prevNode->next = newNode;
newNode->prev = prevNode;
newNode->next = end;
current = newNode;
++length;
return;
}
T pop()
{
if (length == 0)
{
return -1;
}
T data = current->data;
Node<T>* prevNode = current->prev;
prevNode->next = end;
end->prev = prevNode;
delete current;
current = prevNode;
--length;
return data;
}
int size() const
{
return length;
}
int empty() const
{
return length == 0;
}
T top()
{
if (length == 0)
{
return -1;
}
return current->data;
}
private:
Node<T>* current;
Node<T>* front;
Node<T>* end;
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[5] = { "push", "pop", "size", "top", "empty" };
Stack<int> st;
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 < 5; ++j)
{
if (output[0] == inst[j])
{
switch (j)
{
case PUSH:
st.push(stoi(output[1]));
break;
case POP:
cout << st.pop() << '\n';
break;
case SIZE:
cout << st.size() << '\n';
break;
case TOP:
cout << st.top() << '\n';
break;
case EMPTY:
cout << st.empty() << '\n';
break;
default:
break;
}
break;
}
}
}
return 0;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 10866번 : 덱 (0) | 2022.05.10 |
---|---|
[c++] 10845번 : 큐 (0) | 2022.05.10 |
[c++] 10816번 : 숫자 카드2 (0) | 2022.05.05 |
[c++] 10814번 : 나이순 정렬 (0) | 2022.05.05 |
[c++] 10773번 : 제로 (0) | 2022.05.05 |