-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack and queue.cpp
More file actions
434 lines (346 loc) · 7.73 KB
/
Copy pathstack and queue.cpp
File metadata and controls
434 lines (346 loc) · 7.73 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//STACK:::::::::::::::::::::
//Stack implementation Using Array:::::::::::::::::::::::
/*
#include<iostream>
using namespace std;
class Stack {
int *arr;
int top;
int size;
public:
Stack(int size) {
this->size=size;
arr = new int[size];
top = -1;
}
void push(int element) {
if(size-top>1){
top++;
arr[top] = element;
}
else{
cout<<"Stack overflow"<<endl;
}
}
void pop() {
if(top>=0) {
top--;
} else{
cout<<"Stack Underflow"<<endl;
}
}
int peek(){
if(top>=0){
return arr[top];
}
else{
cout<<"Stack is empty"<<endl;
return -1;
}
}
bool isempty(){
if(top==-1) {
return true;
} else{
return false;
}
}
};
int main() {
Stack st(5);
st.push(22);
st.push(43);
st.push(44);
st.push(76);
cout<<st.peek()<<endl;
st.pop();
cout<<st.peek()<<endl;
if(st.isempty()) {
cout<<"Stack is empty"<<endl;
}
else{
cout<<"Stack is not empty"<<endl;
}
}*/
//Implement 2 Stacks in an array::::::::::::::::::
/*
#include<iostream>
using namespace std;
class TwoStack {
int *arr;
int top1;
int top2;
int size;
public:
TwoStack(int s) {
this->size=s;
arr = new int[s];
top1 = -1;
top2 = s;
}
void push1(int num) {
if(top2-top1>1){
top1++;
arr[top1] = num;
}
else{
cout<<"Stack overflow"<<endl;
}
}
void push2(int num) {
if(top2-top1>1){
top2--;
arr[top2] = num;
}
else{
cout<<"Stack overflow"<<endl;
}
}
int pop1() {
if(top1>=0) {
int ans = arr[top1];
top1--;
return ans;
}else{
return -1;
}
}
int pop2() {
if(top2<size) {
int ans = arr[top2];
top2++;
return ans;
}else{
return -1;
}
}
//int peek(){
// if(top>=0){
// return arr[top];
// }
// else{
// cout<<"Stack is empty"<<endl;
// return -1;
// }
//}
//bool isempty(){
// if(top==-1) {
// return true;
// } else{
// return false;
// }
//
//}
};
int main() {
TwoStack st(5);
st.push1(22);
st.push1(43);
st.push2(44);
st.push2(76);
//cout<<st.peek()<<endl;
st.pop1();
st.pop2();
//cout<<st.peek()<<endl;
// if(st.isempty()) {
// cout<<"Stack is empty"<<endl;
// }
// else{
// cout<<"Stack is not empty"<<endl;
// }
} */
//Reverse a string using stack::::::::::::::::::::::
/*
#include<iostream>
#include<stack>
using namespace std;
int main () {
string str = "daddu";
stack<char> s;
for(int i=0; i<str.length(); i++) {
char ch = str[i];
s.push(ch);
}
string ans ="";
while(!s.empty()) {
char ch = s.top();
ans.push_back(ch);
s.pop();
}
cout<<"Reverse of daddu is "<<ans<<endl;
} */
//Delete middle element from stack::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/*
#include <iostream>
#include <stack>
using namespace std;
// Function to delete the middle element of the stack
void deleteMiddle(stack<int>& s, int current, int size) {
// Base case: If we've reached the middle
if (current == size / 2) {
s.pop();
return;
}
// Remove the top element and hold it in a temporary variable
int temp = s.top();
s.pop();
// Recursive call to delete the middle element
deleteMiddle(s, current + 1, size);
// Push the elements back except for the middle one
s.push(temp);
}
// Helper function to delete the middle element
void deleteMiddleElement(stack<int>& s) {
int size = s.size();
if (size == 0)
return; // If the stack is empty, nothing to delete
deleteMiddle(s, 0, size);
}
// Function to print the stack (in reverse order, to display as a stack)
void printStack(stack<int> s) {
if (s.empty())
return;
int temp = s.top();
s.pop();
printStack(s);
cout << temp << " ";
}
int main() {
stack<int> s;
// Push elements into the stack
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << "Original stack: ";
printStack(s);
cout << endl;
// Deleting the middle element
deleteMiddleElement(s);
cout << "Stack after deleting middle element: ";
printStack(s);
cout << endl;
return 0;
} */
// valid parenthesis::::::::::::::::::::::::::::::::::::::::::
//by babbar
/*
#include<iostream>
#include<stack>
using namespace std;
int main () {
string exp;
stack<char> s;
for(int i=0; i<exp.length(); i++) {
char ch = exp[i];
if(ch =='(' || ch =='{' || ch == '[') {
s.push(ch);
}
else{
if(!s.empty()) {
char top = s.top();
if((ch ==')' && top =='(' ) ||
( ch == '}' && top == '{') ||
( ch == ']' && top == '[') ) {
s.pop();
}
else {
return false;
}
}
else {
return false;
}
}
}
}*/
//chatgpt::
/*
#include <iostream>
#include <stack>
using namespace std;
bool isValidParenthesesStack(const string& s) {
stack<char> st;
// Iterate through each character in the string using a traditional for loop
for (int i = 0; i < s.length(); i++) {
char ch = s[i];
// Push the opening brackets onto the stack
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
}
// When encountering a closing bracket
else if (ch == ')' || ch == '}' || ch == ']') {
// If stack is empty or top of the stack doesn't match, return false
if (st.empty()) return false;
char top = st.top();
if ((ch == ')' && top == '(') ||
(ch == '}' && top == '{') ||
(ch == ']' && top == '[')) {
st.pop(); // Pop the matched opening bracket
} else {
return false; // Invalid closing bracket
}
}
}
// If the stack is empty, all brackets were matched
return st.empty();
}
int main() {
string input;
cout << "Enter parentheses string to check: ";
cin >> input;
if (isValidParenthesesStack(input)) {
cout << "Valid parentheses using stack!" << endl;
} else {
cout << "Invalid parentheses using stack!" << endl;
}
return 0;
}*/
// insert an element in the bottom of a stack:::::::::::
/*
#include <iostream>
#include <stack>
using namespace std;
// Function to insert an element at the bottom of the stack
void insertAtBottom(stack<int>& s, int x) {
// Base case: If the stack is empty, push the element
if (s.empty()) {
s.push(x);
return;
}
// Hold the top element and pop it
int topElement = s.top();
s.pop();
// Recursive call to insert the element at the bottom
insertAtBottom(s, x);
// Push the top element back onto the stack
s.push(topElement);
}
int main() {
stack<int> s;
// Pushing elements into the stack
s.push(1);
s.push(2);
s.push(3);
s.push(4);
cout << "Original stack (top to bottom): 4 3 2 1" << endl;
int elementToInsert = 0;
insertAtBottom(s, elementToInsert);
// Printing the updated stack
cout << "Stack after inserting " << elementToInsert << " at the bottom: ";
// Print the stack (temporary stack to reverse order)
stack<int> temp;
while (!s.empty()) {
temp.push(s.top());
s.pop();
}
// Display the stack in correct order
while (!temp.empty()) {
cout << temp.top() << " ";
temp.pop();
}
cout << endl;
return 0;
}*/
//Insert the element in the middle of stack: