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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
 
class MyIntStack {
    int *p;
    int size;
    int tos;
public:
    MyIntStack();
    MyIntStack(int size) { this->size = size; p = new int[size]; tos = 0; }
    MyIntStack(MyIntStack& s) { this->= new int[s.size]; size = s.size; tos = s.tos; for (int i = 0; i < tos; i++) { p[i] = s.p[i]; } }
    ~MyIntStack() { delete[]p; }
    bool push(int n); // 정수 n을 스택에 푸시한다.
            // 스택이 꽉 차 있으면 false를, 아니면 true 리턴
    bool pop(int &n); // 스택의 탑에 있는 값을 n에 팝한다.
            // 만일 스택이 비어 있으면 false를, 아니면 true 리턴
};
 
bool MyIntStack::push(int n) {
    if (tos != size) {
        p[tos] = n;
        tos++;
        return true;
    }
    else return false;
}
 
bool MyIntStack::pop(int &n) {
    if (tos != 0) {
        tos--;
        n = p[tos];
        return true;
    }
    else return false;
}
 
int main() {
    MyIntStack a(10);
    a.push(10);
    a.push(20);
    MyIntStack b = a;
    b.push(30);
 
    int n;
    a.pop(n);
    cout << "스택 a에서 팝한 값 " << n << endl;
    b.pop(n);
    cout << "스택 b에서 팝한 값 " << n << endl;
}
cs


+ Recent posts