-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13week_6.cpp
62 lines (54 loc) · 1007 Bytes
/
13week_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
60
61
62
#include <iostream>
using namespace std;
class AbstractStack {
public:
virtual bool push(int n) = 0;
virtual bool pop(int& n) = 0;
virtual int size() = 0;
};
class IntStack : public AbstractStack {
int stack[5] = { 0 };
int top = -1;
public:
bool push(int n) {
if (size() + 1 >= 5) {
cout << "Stack is full" << endl;
return false;
}
stack[++top] = n;
return true;
}
bool pop(int& n) {
if (size() < 0) {
cout << "Stack is empty" << endl;
return false;
}
n = stack[top--];
return true;
}
int size() {
return top;
}
void show() {
cout << "| ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << ' ';
}
cout << "|" << endl;
}
};
int main() {
IntStack intStack;
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.push(4);
intStack.push(5);
intStack.push(6);
intStack.show();
int n;
intStack.pop(n);
cout << n << " is popped" << endl;
intStack.show();
return 0;
}