-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Array.cpp
More file actions
57 lines (46 loc) · 1.12 KB
/
Copy pathStack_Array.cpp
File metadata and controls
57 lines (46 loc) · 1.12 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
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
const int MAX_SIZE = 100; // Maximum size of the stack
class Stack {
private:
int arr[MAX_SIZE]; // Array to store stack elements
int top; // Index of the top element
public:
Stack() {
top = -1; // Initialize the top index
}
void push(int value) {
if (top >= MAX_SIZE - 1) {
cout << "Stack Overflow! Cannot push more elements." << endl;
return;
}
arr[++top] = value;
}
void pop() {
if (top < 0) {
cout << "Stack Underflow! Cannot pop." << endl;
return;
}
cout << "Popped element: " << arr[top--] << endl;
}
void display() {
if (top >= 0) {
cout << "Stack elements:";
for (int i = top; i >= 0; --i)
cout << " " << arr[i];
cout << endl;
} else {
cout << "Stack is empty." << endl;
}
}
};
int main() {
Stack myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.display();
myStack.pop();
myStack.display();
return 0;
}