-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-string-using-stack.cpp
278 lines (195 loc) · 5.7 KB
/
reverse-string-using-stack.cpp
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
/*
GEEKSFORGEEKS
DATA STRUCTURES
STACKS
REVERSE A STRING USING STACK
coded by Fatih Cinar
on March 11th, 2020
*/
#include <iostream>
using namespace std;
class Stack
{
private:
const int NOWHERE = -1; // EMPTY STACK POINTER
char* Container; // The container(array) that contains characters
int Top; // showing the top of the stack
int Capacity; // capacity of the stack
void privateConstructor(int stackCapacity); // Private initializations
public:
Stack(int stackCapacity); // Create stack with capacity
Stack(); // Assign a capacity to the stack
void push(char value); // push a char element to the stack
char pop(); // pop a char from the stack
void printStack(); // prints the elements in the stack
bool isEmpty(); // figures out whether the stack is empty or not
bool isFull(); // figures out whether the is full or not
static Stack* createRandomStack(); // create random valued stack for testing
};
class Reverser
{
/*
This class reverses strings using Stack data structure
*/
public:
static void reverseString(string &oneString); // reverses a string
};
Stack* Stack::createRandomStack(){
/*
This method creates a stack with random length
and random values in it
for testing purposes
*/
// Factory Design Pattern
Stack* myNewStack = new Stack();
const int minimumNumberElements = 3;
const int maximumNumberElements = 7;
const int randomNumberofElements = rand() % maximumNumberElements + minimumNumberElements;
const int range = 'z' - 'a' + 1; // number of characters in the range
char randomCharacter; // randomly chosen charater to push to the stack
for(int counter = 0; counter < randomNumberofElements; counter++){
// push the elements to the stack
// randomly chose characters to push
randomCharacter = 'a' + rand() % range;
myNewStack->push(randomCharacter);
}
return myNewStack;
}
bool Stack::isFull(){
/*
This method figures out whether the stack is FULL or not
*/
enum Values
{
FULL = true,
NOT_FULL = false
};
if(Top == Capacity - 1){
return FULL;
}
else{
return NOT_FULL;
}
}
bool Stack::isEmpty(){
/*
This method figures out whether the stack is empty or not
*/
enum Values{
EMPTY = true,
NON_EMPTY = false
};
if(Top == NOWHERE){
return EMPTY;
}
else{
return NON_EMPTY;
}
}
void Stack::printStack(){
/*
This function prints all the elements inside the stack
*/
if(isEmpty()){
cout << "Empty Stack!" << endl;
return;
}
else{
// Start from the bottom of the stack
// and iterate till the top
const int bottom = 0; // bottom of the stack
for(int iterator = bottom; iterator <= Top; iterator++){
cout << Container[iterator] << " ";
}
cout << endl;
return;
}
}
void Stack::privateConstructor(int stackCapacity){
/*
This private constructor makes all the job
Public constructors are only interfaces
*/
this->Top = NOWHERE; // Pointing nowhere
// Allocate the memory for stack
this->Container = new char[stackCapacity];
this->Capacity = stackCapacity;
return;
}
Stack::Stack(){
/*
This public constructor creates stack with the
assigned capacity
*/
const int assignedCapacity = 1000;
this->privateConstructor(assignedCapacity);
return;
}
Stack::Stack(int stackCapacity){
/*
This public constructor creates the stack with
the desired capacity
*/
if(stackCapacity <= 0){
throw "Not able to initialize the stack!\nError Code: 1 - Invalid capacity";
}
this->privateConstructor(stackCapacity);
return;
}
void Stack::push(char value){
/*
This method gets a character values
and pushes it to the top of the stack
*/
if(isFull()){
throw "Error Code 2 - Stack is FULL!";
}
Top++;
// Increment the top to the new area to push
Container[Top] = value;
// push the element to the new area
return;
}
char Stack::pop(){
/*
This method pops the most-recently-added element
from the top of the stack
*/
if(isEmpty()){
// throw "Error Code 3: Stack is empty! Nothing to pop!";
return '\0';
}
else{
char theTopElement = Container[Top];
Top--; // Decrement the top
// so that the last element is removed
return theTopElement;
}
}
void Reverser::reverseString(string& oneString){
/*
This method gets a string and reverses it
and saves the reversed sitauation to the string itself
*/
// Create new stack for operations
Stack* stack = new Stack();
const char TERMINATION = '\0';
// Add all the characters on the string to the stack
for(int iterator = 0 ; oneString[iterator] != TERMINATION; iterator++){
// add each one to the stack
stack->push(oneString[iterator]);
}
char popped;
// Now start popping from the stack
// and replace the characters in the string
// one by one
for(int iterator = 0; !stack->isEmpty(); iterator++ ){
// POP FROM THE STACK
// AS LONG AS IT IS NOT EMPTY
popped = stack->pop();
// and replace the string using iteator
// and increase the iterator later one
oneString[iterator] = popped;
}
return; // all configurations saved in the string
}