1. Stack
1. 스택 라이브러리 사용
2. 직접 구현
일반적으로 알고리즘 대회에서는 스택 라이브러리 쓰는게 빠름
사용 가능 명령어:
- stack<자료형> 이름; (선언 방식)
- push()
- pop()
- empty()
- top()
#include <iostream>
#include <stack>
using namespace std;
int main(void){
stack<int> s;
s.push(7);
s.push(3);
s.push(4);
s.pop();
s.push(6);
while(!s.empty()){
cout << s.top() << ' ';
s.pop();
}
return 0;
}
2. Queue
operations:
- push()
- pop()
- front()
- empty()
#include <iostream>
#include <queue>
using namespace std;
int main(void){
queue<int> q;
q.push(7);
q.push(6);
q.push(4);
q.pop();
q.push(2);
while(!q.empty()){
cout << q.front() << ' ';
q.pop();
}
return 0;
}
'컴퓨터기본 > 알고리즘' 카테고리의 다른 글
8. Dynamic Programming (0) | 2021.07.06 |
---|---|
7. 너비 우선 탐색(BFS), 깊이 우선 탐색(DFS) (0) | 2021.06.14 |
5. 계수 정렬 (Counting Sort) (0) | 2021.06.14 |
4. Heap Sort (힙 정렬) (0) | 2021.06.14 |
3. 퀵 정렬 (Quick Sort), 병합 정렬 (Merge Sort) (0) | 2021.06.14 |