알고리즘/BOJ

백준 10828번 스택, 10845번 큐, 10866번 덱 stl 사용하지 않고 구현

꾸준함. 2019. 7. 9. 00:06

stl을 사용한 코드는 아래 링크를 참고하시면 됩니다.

 

https://jaimemin.tistory.com/687

 

백준 10828번 스택

문제 링크입니다: https://www.acmicpc.net/problem/10828 백준 10845번 큐(http://jaimemin.tistory.com/661)처럼 자료구조를 복습하는 문제였습니다. 스택에 대한 개념을 안다면 어렵지 않게 풀 수 있는 문제였..

jaimemin.tistory.com

https://jaimemin.tistory.com/661

 

백준 10845번 큐

문제 링크입니다: https://www.acmicpc.net/problem/10845 오랜만에 자료구조 큐를 복습할 수 있던 문제였습니다. 사실, 덱(deque)을 쓰면 훨씬 쉬운 문제였지만 문제의 의도대로 큐를 사용해서 풀었습니다. 덱을..

jaimemin.tistory.com

https://jaimemin.tistory.com/837

 

백준 10866번 덱

문제 링크입니다: https://www.acmicpc.net/problem/10866 STL 덱 활용을 연습하는 문제였습니다. #include #include #include using namespace std; int main(void) { ios_base::syn..

jaimemin.tistory.com

 

백준 10828번 스택

백준 10845번 큐

백준 10866번 덱

#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;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

 

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

 

 

 

 

 

 

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 15649번 N과 M (1)  (0) 2019.07.14
백준 1926번 그림  (0) 2019.07.10
백준 3187번 양치기 꿍  (7) 2019.06.20
백준 1938번 통나무 옮기기  (0) 2019.06.19
백준 2346번 풍선 터뜨리기  (0) 2019.05.31