Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (196 loc) · 5.44 KB
/
Copy pathmain.cpp
File metadata and controls
228 lines (196 loc) · 5.44 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* ============================================================
* For questions, feedback, or support:
*
* Mohammed Abdulrahman Alalyani
*
* Email : malredfan@gmail.com
* Instagram : @malredfan
* X : @malredfan
* LinkedIn : malredfan
* ============================================================
*/
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 100
struct Stack {
char stk[MAX];
int top;
} s = { .top = -1 };
bool isEmpty() {
return s.top == -1;
}
bool isFull() {
return s.top == MAX - 1;
}
void push(char c) {
if (isFull()) {
cout << "Stack is Full\n";
return;
}
s.stk[++s.top] = c;
}
char pop() {
if (isEmpty()) {
cout << "Stack is Empty\n";
return '\0';
}
char popped = s.stk[s.top--];
return popped;
}
// Display operation
void display() {
if (isEmpty()) {
cout << "Stack is empty\n";
return;
}
cout << "Current Stack (Top to Bottom): ";
for (int i = s.top; i >= 0; i--)
cout << s.stk[i] << " ";
cout << "\n";
}
void peek() {
if (isEmpty()) {
cout << "Stack is empty\n";
return;
}
cout << "Top element: " << s.stk[s.top] << "\n";
}
// Reverse Word Function
string reverseWord(string str) {
string reversedWord;
s.top = -1;
for (int i = 0; i < str.length(); i++) {
push(str[i]);
}
while (!isEmpty()) {
reversedWord += pop();
}
return reversedWord;
}
// Palindrome Checker Function
bool isPalindrome(string str) {
s.top = -1;
for (int i = 0; i < str.length(); i++) {
push(str[i]);
}
for (int i = 0; i < str.length(); i++) {
if (str[i] != pop()) {
return false;
}
}
return true;
}
// Braces Balancer Function
bool isBalanced(string expr) {
s.top = -1;
for (int i = 0; i < expr.length(); i++) {
char c = expr[i];
if (c == '(' || c == '{' || c == '[') {
push(c);
} else if (c == ')' || c == '}' || c == ']') {
if (isEmpty()) {
return false;
}
char topChar = pop();
// Check for matching pairs
if ((c == ')' && topChar != '(') ||
(c == '}' && topChar != '{') ||
(c == ']' && topChar != '[')) {
return false;
}
}
}
return isEmpty();
}
int main() {
int choice;
cout << "\n*** Data Structure Practicing Project ***\n";
while (true) {
cout <<"\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Display\n";
cout << "4. Peek\n";
cout << "5. IsEmpty\n";
cout << "6. IsFull\n";
cout << "7. Reverse Word\n";
cout << "8. Palindrome Check\n";
cout << "9. Braces Balancer\n";
cout << "10. Exit\n";
cout << "------------------------------------------------------------------------------------\n";
cout << "Please enter the number of the required operation (1-10) from the above menu: ";
cin >> choice;
cout << "------------------------------------------------------------------------------------\n";
switch (choice) {
case 1: {
char c;
cout << "Please enter a character to push: ";
cin >> c;
push(c);
break;
}
case 2: {
cout << "Popped character: " << pop() << "\n";
break;
}
case 3: {
display();
break;
}
case 4: {
peek();
break;
}
case 5: {
cout << (isEmpty() ? "Stack is empty\n" : "Stack is not empty\n");
break;
}
case 6: {
cout << (isFull() ? "Stack is full\n" : "Stack is not full\n");
break;
}
case 7: {
string word;
cout << "Please enter a word to reverse: ";
cin >> word;
string reversed = reverseWord(word);
cout << "Reversed word: " << reversed << "\n";
break;
}
case 8: {
string str;
cout << "Please enter a string to check if it is a palindrome or not: ";
cin >> str;
bool result = isPalindrome(str);
if (result)
cout << str << " is a palindrome string.\n";
else
cout << str << " is not a palindrome string.\n";
break;
}
case 9: {
string expr;
cout << "Please enter an expression to check braces balance: ";
cin >> expr;
bool balanced = isBalanced(expr);
if (balanced)
cout << "The expression is balanced.\n";
else
cout << "The expression is not balanced.\n";
break;
}
case 10: {
cout << "Exiting program.\n";
return 0;
}
default: {
cout << "Invalid choice. Please try again.\n";
break;
}
}
cout << "Operation executed successfully.\n";
}
return 0;
}