forked from MAYANK25402/Hacktober-Fest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackUsingArray.cpp
More file actions
50 lines (43 loc) · 1.06 KB
/
Copy pathstackUsingArray.cpp
File metadata and controls
50 lines (43 loc) · 1.06 KB
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
class stackUsingArray {
// member functions
int *data;
int nextIndex;
int capacity;
public:
// constructor
stackUsingArray(int totalSize) {
data = new int[totalSize];
nextIndex = 0;
capacity = totalSize;
}
// operation 1: Size of the stack
int size() {
return nextIndex;
}
// operation 2: checking if the stack is empty
bool isEmpty() {
return nextIndex == 0;
}
// operation 3: pushing/inserting the element
void push(int element) {
if(nextIndex == capacity) {
cout << "Stack is Full" << endl;
return;
}
data[nextIndex] = element;
nextIndex++;
}
// operation 4: Popping/Deleteing the element
int pop() {
if(isEmpty()) {
cout << "Stack is Empty" << endl;
return INT_MIN;
}
nextIndex--;
return data[nextIndex];
}
// operation 5: Seeing the top most element
int top() {
return data[nextIndex-1];
}
};