forked from NiharRanjan53/HacktoberFest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_implementation_using_array
68 lines (54 loc) · 1.21 KB
/
Stack_implementation_using_array
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
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
class stack_using_array {
int *data, nextindex , capicity; // This is private data
public:
stack_using_array(int totalsize) { //Constructor call for intitilize
data = new int [totalsize];
nextindex = 0;
capicity = totalsize;
}
int size() {
return nextindex; // For check Size
}
bool isempty() {
if (nextindex == 0) {
return true; // For size is empty or not
} else {
return false;
}
}
void push(int element) {
if (nextindex == capicity) {
return; // for push element in a stack
} else {
data[nextindex++] = element;
}
}
int pop() {
if (!isempty()) {
cout << "stack is empty" << endl; // Pop element from stack
return 0;
}
nextindex--;
return nextindex;
}
int top() {
if (!isempty()) {
cout << "stack is empty" << endl; // excess top element in stack
return 0;
}
return (nextindex - 1);
}
};
int main() {
int n; // main function in satck
cin >> n;
stack_using_array s(n);
s.push(10);
s.push(10);
s.push(10);
s.push(10);
s.push(10);
cout << s.top() << endl;
}