-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Implememntation.cpp
More file actions
59 lines (47 loc) · 1.08 KB
/
Copy pathStack_Implememntation.cpp
File metadata and controls
59 lines (47 loc) · 1.08 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
58
59
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Stack {
private:
int arr[MAX_SIZE];
int top;
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 is empty! Cannot pop." << endl;
return;
}
top--;
}
int peek() {
if (top < 0) {
cout << "Stack is empty!" << endl;
return -1;
}
return arr[top];
}
bool isEmpty() {
return top < 0;
}
};
int main() {
Stack myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
cout << "Top element: " << myStack.peek() << endl;
myStack.pop();
cout << "Top element after popping: " << myStack.peek() << endl;
cout << "Is stack empty? " << (myStack.isEmpty() ? "Yes" : "No") << endl;
return 0;
}