[바킹독의 실전 알고리즘] 0x05강 - 스택
[바킹독의 실전 알고리즘] 0x06강 - 큐
[바킹독의 실전 알고리즘] 0x07강 - 덱
const int MX = 1000005;
int dat[MX];
int pos = 0;
void push(int x) {
dat[pos++] = x;
}
void pop() {
pos--;
}
int top() {
return dat[pos - 1];
}
#include <bits/stdc++.h>
using namespace std;
int main(void) {
stack<int> S;
S.push(10); // 10
S.push(20); // 10, 20
S.push(30); // 10, 20, 30
cout << S.size() << '\n'; // 3
if (S.empty()) cout << "S is empty\n";
else cout << "S is not empty\n"; // S is not empty
S.pop(); // 10, 20
cout << S.top() << '\n'; // 20
S.pop(); // 10
cout << S.top() << '\n'; // 10
S.pop(); // empty
if (S.empty()) cout << "S is empty\n"; // S is empty
cout << S.top() << '\n'; // runtime error 발생
}
const int MX = 1000005;
int dat[MX];
int head = 0, tail = 0;
void push(int x) {
dat[tail++] = x;
}
void pop() {
head++;
}
int front() {
return dat[head];
}
int back() {
return dat[tail - 1];
}
#include <bits/stdc++.h>
using namespace std;
int main(void) {
queue<int> Q;
Q.push(10); // 10
Q.push(20); // 10 20
Q.push(30); // 10 20 30
cout << Q.size() << '\n'; // 3
if (Q.empty()) cout << "Q is empty\n";
else cout << "Q is not empty\n"; // Q is not empty
Q.pop(); // 20 30
cout << Q.front() << '\n'; // 20
cout << Q.back() << '\n'; // 30
Q.push(40); // 20 30 40
Q.pop(); // 30 40
cout << Q.front() << '\n'; // 30
}
const int MX = 1000005;
int dat[2*MX+1];
int head = MX, tail = MX;
void push_front(int x) {
dat[--head] = x;
}
void push_back(int x) {
dat[tail++] = x;
}
void pop_front(int x) {
head++;
}
void pop_back(int x) {
tail--;
}
int front(int x) {
return dat[head];
}
int back(int x) {
return dat[tail-1];
}
#include <bits/stdc++.h>
using namespace std;
int main(void) {
deque<int> DQ;
DQ.push_front(10); // 10
DQ.push_back(50); // 10 50
DQ.push_front(24); // 24 10 50
for (auto x : DQ) cout << x;
cout << DQ.size() << '\n'; // 3
if (DQ.empty()) cout << "DQ is empty\n";
else cout << "DQ is not empty\n" // DQ is not empty
DQ.pop_front(); // 10 50
DQ.pop_back(); // 10
cout << DQ.back() << '\n'; // 10
DQ.push_back(72); // 10 72
cout << DQ.front() << '\n'; // 10
DQ.push_back(12); // 10 72 12
DQ[2] = 17; // 10 72 17
DQ.insert(DQ.begin()+1, 33); // 10 33 72 17
DQ.insert(DQ.begin()+4, 60); // 10 33 72 17 60
for (auto x : DQ) cout << x << ' ' ;
cout << '\n';
DQ.erase(DQ.begin()+3); // 10 33 72 60
cout << DQ[3] << '\n'; // 60
DQ.clear(); // DQ의 모든 원소 제거
}
끄읏