-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12week_6.cpp
59 lines (58 loc) · 1.14 KB
/
12week_6.cpp
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
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
class BaseArray{
private:
int capacity;
int *mem;
protected:
BaseArray(int capacity = 100){
this->capacity = capacity;
mem = new int[capacity];
}
~BaseArray(){ delete[] mem; }
void put(int index, int val){
mem[index] = val;
}
int get(int index){
return mem[index];
}
void set(int index , int n){
mem[index] = n;
}
int getCapacity(){
return capacity;
}
};
class MyStack : public BaseArray{
int index;
public:
MyStack(int size) { index = 0; }
void push(int n){
set(index, n);
index++;
}
int capacity(){
return getCapacity();
}
int length(){
return index;
}
int pop(){
return get(--index);
}
};
int main(){
MyStack mStack(100);
int n;
cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
for (int i = 0; i < 5; i++){
cin >> n;
mStack.push(n);
}
cout << "스택용량:" << mStack.capacity() << ", 스택크기:" << mStack.length() << endl;
cout << "스택의 모든 원소를 팝하여 출력한다>> ";
while (mStack.length() != 0){
cout << mStack.pop() << ' ';
}
cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
}