-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-class.cpp
More file actions
51 lines (43 loc) · 885 Bytes
/
stack-class.cpp
File metadata and controls
51 lines (43 loc) · 885 Bytes
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
// implementing stack with class
#include <iostream>
using namespace std;
class stack {
private:
int arr[20] = {0};
int pointer = 0;
public:
void push(int x) {
if (pointer < 20) {
arr[pointer++] = x;
}
else {
cout << "stack overflow!" << endl;
}
}
void pop() {
if (pointer > 0) {
arr[--pointer] = 0;
}
else {
cout << "Stack underflow!" << endl;
}
}
void showdata() {
for (int x : arr) {
cout << x << "\t";
}
}
};
int main() {
stack s1;
s1.push(10);
s1.push(20);
s1.pop();
s1.push(10);
s1.push(30);
s1.push(60);
s1.push(80);
s1.pop();
s1.showdata();
return 0;
}