-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment2_answer.cpp
More file actions
365 lines (361 loc) · 8.8 KB
/
assignment2_answer.cpp
File metadata and controls
365 lines (361 loc) · 8.8 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
#include <iostream>
#include <queue>
#include<string>
#include <vector>
#include<cstring>
#include<string.h>
#include <algorithm>
#include <cstdlib>
#define MAX 1000
using namespace std;
//---------------------------------------- Stack using Linked List. --------------------------------------------
// Class of the node .
template <class T> class Node
{
public:
T data;
Node *next;
};
// Creating a class STACK
template <class T> class TStackList
{
Node <T> *top;
public:
TStackList()
{
top=NULL;
}
void Push(); // to insert an element
void Pop(); // to delete an element
void Show(); // to show the stack
};
//----------------------------------------
// PUSH Operation
template <class T> void TStackList <T> ::Push()
{
T value;
Node <T> *ptr;
cout<<"\nPUSH Operation\n";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new Node <T>;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nYeah! New item inserted :)";
}
//----------------------------------------
// POP Operation
template <class T> void TStackList <T> ::Pop()
{
Node <T> *temp;
if(top==NULL)
{
cout<<"\nOhh!! The stack is empty :(";
return;
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
}
//----------------------------------------
// Show stack
template <class T> void TStackList <T> ::Show()
{
Node <T> *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}
//---------------------------------------- Reverse a string using stacks. --------------------------------------
class node
{
public:
char data;
node *next;
};
// Creating a class STACK
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void Push(char value); // to insert an element
char Pop(); // to delete an element
string reverseArray();
};
// PUSH Operation
void stack::Push(char value)
{
node *ptr;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
}
// POP Operation
char stack::Pop()
{
node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
return NULL;
}
temp=top;
char x = temp->data;
top=top->next;
delete temp;
return x;
}
string reverseArray(){
stack s;
char chararray[100] = {'H','e','l','l','o',' ','W','o','r','l','e','d'};
for (int i=0;i<strlen(chararray);i++){
s.Push(*(chararray+i));
}
cout<<"\nString is inserted to the stack!!!\n";
string reverseWord;
for (int i =0; i<strlen(chararray);i++){
reverseWord.push_back(s.Pop());
}
cout << "String after reversing is : ";
return reverseWord;
}
//---------------------------------------- Palindromes. --------------------------------------------------------
class strStack {
int top1;
int top2;
public:
char x[MAX]; // Maximum size
char y[MAX]; // Maximum size
strStack() { top1 = -1;top2 = -1; }
bool Push1 (char value);
bool Push2 (char value);
char Pop1();
char Pop2();
bool isEmpty_1();
bool isEmpty_2();
bool checkPalindrome(int len);
};
//----------------------------------------
bool strStack::Push1(char value)
{
if (top1 >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
x[++top1] = value;
cout << value << " pushed into stack\n";
return true;
}
}
//----------------------------------------
bool strStack::Push2(char value)
{
if (top2 >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
y[++top2] = value;
cout << value << " pushed into stack\n";
return true;
}
}
//----------------------------------------
char strStack::Pop1()
{
if (top1 < 0) {
cout << "Stack Underflow";
return 0;
}
else {
char value = x[top1--];
return value;
}
}
//----------------------------------------
char strStack::Pop2()
{
if (top2 < 0) {
cout << "Stack Underflow";
return 0;
}
else {
char value = y[top2--];
return value;
}
}
//----------------------------------------
bool strStack :: checkPalindrome(int len){
for (int i = 0; i< len/2; i++){
if (tolower(Pop1()) != tolower(Pop2())){
return false;
}
}
cout << "String is Palindrome . . . .";
return true;
}
//----------------------------------------
static string removeSpaces(string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}
// ******* CALLING OF THE FUNCTION *******
//---------------------------------------- Implement a stack using a singly linked list. ----------------------------------------
//===============================================================================================================================
/*
int main()
{
TStackList <int> s;
int choice;
while(1)
{
cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\t STACK USING LINKED LIST \n\n";
cout<<"1:PUSH \n2:POP \n3:DISPLAY STACK \n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.Push();
break;
case 2:
s.Pop();
break;
case 3:
s.Show();
break;
case 4:
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!!";
break;
}
}
return 0;
}
*/
//---------------------------------------- Reverse a string using stacks. ----------------------------------------
//================================================================================================================
/*
int main(){
cout << reverseArray();
}
*/
//---------------------------------------- Check Palindromes using 2 stacks. -------------------------------------
//================================================================================================================
/*
int main()
{
class strStack s;
string str = "Murder for a jar of redrum";
str = removeSpaces(str);
int len = removeSpaces(str).length();
if (len%2==0){
for (int i = len/2; i < len ; i++){
s.Push1(str[i]);
}
for (int i = len/2; i >= 0 ; i--){
s.Push2(str[i]);
}
}
else{
for (int i = len/2; i < len ; i++){
s.Push1(str[i]);
}
for (int i = len/2; i >= 0 ; i--){
s.Push2(str[i]);
}
}
s.checkPalindrome(len);
return 0;
}
*/
//---------------------------------------- Implement a stack using a queue -------------------------------------
//==============================================================================================================
/*
int main()
{
queue <int> q;
int count;
int choice;
cout << "1. Push into a stack .\n";
cout << "2. Pop into a stack .\n";
cout << "3. Print the stack. \n";
cout << "4. Exit the program. \n";
do{
cout << "Enter the choice ? \n";
cin >> choice;
switch (choice){
case 1:
{
int data;
cout << "Enter the item to push ? \n";
cin >> data;
q.push(data);
count++;
break;
}
case 2:
{
if (q.empty()){
cout << "Queue is empty !";
}
else{
for (int i=0;i<count-1;i++){
int k = q.front();
q.pop();
q.push(k);
}
count--;
cout << "Popped item is " << q.front() << "\n";
q.pop();
}
break;
}
case 3:
{
if (q.empty()){
cout << "Stack is empty !! /n";
}
else{
for(int i=0;i<count;i++){
cout << q.front() << " ";
q.push(q.front());
q.pop();
}
}
break;
}
case 4:
{
return 0;
}
default:
{
cout << "please Enter a valid choice !! ";
}
}
}while(choice != 4);
return 0;
}
*/
//=======================================================================================