스택과 큐는 선형구조로 분류되는 자료구조 중 대표적인 예시이다.
- 스택(Stack)
- push(element) : top에 원소를 추가
- pop() : top에 있는 원소를 삭제
- top() : top에 있는 원소를 반환
- empty() : 스택이 비어있으면 true 아니면 false를 반환
- size() : 스택 사이즈를 반환

- 큐(Queue)
- push(element) : 큐 뒤에 원소를 추가
- pop() : 큐 앞에 있는 원소를 삭제
- front() : 큐 제일 앞에 있는 원소를 반환
- back() : 큐 제일 뒤에 있는 원소를 반환
- empty() : 큐가 비어있으면 true 아니면 false를 반환
- size() : 큐 사이즈를 반환

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
| #include <iostream> #include <stack> #include <queue> using namespace std;
int main () { stack<int> s; queue<int> q;
s.push(1); s.push(2); s.push(3); s.pop(); cout << s.top() << endl; cout << s.size() << endl; cout << (s.empty() ? "empty" : "!empty") << endl << endl;
q.push(1); q.push(2); q.push(3); q.pop(); cout << q.front() << endl; cout << q.back() << endl; cout << q.size() << endl; cout << (q.empty() ? "empty" : "!empty") << endl << endl;
}
|