-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram6.cpp
More file actions
104 lines (79 loc) · 1.96 KB
/
Program6.cpp
File metadata and controls
104 lines (79 loc) · 1.96 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Program to create a stack and implement push and pop operations on it.
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Stack {
private:
int arr[MAX_SIZE];
int top;
public:
Stack() : top(-1) {}
bool isEmpty() {
return (top == -1);
}
bool isFull() {
return (top == MAX_SIZE - 1);
}
void push(int data) {
if (isFull()) {
cout << "Stack Overflow" << endl;
return;
}
top++;
arr[top] = data;
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow" << endl;
return -1; // Return an error value
}
int data = arr[top];
top--;
return data;
}
void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack elements:" << endl;
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stack;
int choice, data;
cout << "Name: Umesh Patel\n";
cout << "Enrollment No: 0126AL231140\n";
while (true) {
cout << "\n1. Push\n";
cout << "2. Pop\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data to push: ";
cin >> data;
stack.push(data);
break;
case 2:
data = stack.pop();
if (data != -1) { // Check for error
cout << "Popped element: " << data << endl;
}
break;
case 3:
stack.display();
break;
case 4:
return 0;
default:
cout << "Invalid choice" << endl;
}
}
}