-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
272 lines (225 loc) · 4.93 KB
/
stack.cpp
File metadata and controls
272 lines (225 loc) · 4.93 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
#include <iostream>
#include <limits>
#include <sstream>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
class Node
{
public:
string value;
Node *next;
Node(const string &val) : value(val), next(nullptr) {}
};
class Stack
{
private:
int size = 0;
Node *top = nullptr;
public:
void push(const string &s)
{
Node *node = new Node(s);
if (isEmpty())
{
top = node;
}
else
{
node->next = top;
top = node;
}
size++;
}
string pop()
{
if (isEmpty())
return "";
Node *node = top;
top = top->next;
string value = node->value;
delete node;
size--;
return value;
}
string peek() const
{
if (!isEmpty())
return top->value;
return "";
}
bool isEmpty() const { return !size; }
int getSize() const { return size; }
void clear()
{
while (!isEmpty())
{
pop();
}
}
Node *getTop() const { return top; }
};
Stack stack;
void init();
void printHeading()
{
cout << "\t ╔═══════════════════════════════════╗\n";
cout << "\t ║ ║\n";
cout << "\t ║ Stack Demo Program ║\n";
cout << "\t ║ ║\n";
cout << "\t ║ Copyright © 2025 Ullas Shome. ║\n";
cout << "\t ║ All Rights Reserved. ║\n";
cout << "\t ╚═══════════════════════════════════╝\n\n";
}
bool isInteger(string &s);
void showMenu();
inline int checkCommand(int command);
int main()
{
init();
printHeading();
while (true)
{
string input;
int command;
showMenu();
getline(cin, input);
cout << endl;
if (isInteger(input))
{
command = stoi(input);
}
else
{
cout << "-> That's not a valid input.\n\n";
continue;
}
checkCommand(command);
}
return 0;
}
bool isInteger(string &s)
{
stringstream ss(s);
int x;
char c;
return ss >> x && !(ss >> c);
}
void init()
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
system("cls");
system("title Stack Demo Program");
#else
system("clear");
#endif
}
void showMenu()
{
cout << "Select an option:\n";
cout << "\t1. Add an Item to the Stack\n";
cout << "\t2. Show Top Item\n";
cout << "\t3. Show Full Stack\n";
cout << "\t4. Take Out Top Item\n";
cout << "\t5. Check Stack Size\n";
cout << "\t6. Clear Stack\n";
cout << "\t7. Show the Banner\n";
cout << "\t8. End Program\n\n";
cout << "Command (n): ";
}
void addItem();
void top();
void showFull();
void pop();
void stackSize();
void clearStack();
inline int checkCommand(int command)
{
switch (command)
{
case 1:
addItem();
break;
case 2:
top();
break;
case 3:
showFull();
break;
case 4:
pop();
break;
case 5:
stackSize();
break;
case 6:
clearStack();
break;
case 7:
printHeading();
break;
case 8:
cout << "-> Program Ended.\n\n";
cout << "Copyright © 2025 Ullas Shome. All rights reserved.\n";
exit(0);
default:
cout << "-> Not a valid command\n\n";
}
return 0;
}
void addItem()
{
string item;
cout << "Give us an item: ";
getline(cin, item);
stack.push(item);
cout << "-> Item added\n\n";
}
void top()
{
if (stack.isEmpty())
{
cout << "-> No item to show.\n\n";
return;
}
cout << "-> Top Item is: " << stack.peek() << "\n\n";
}
void showFull()
{
if (stack.isEmpty())
{
cout << "-> No items to show. :(\n\n";
return;
}
cout << "-> Items (from top to bottom):\n";
Node *current = stack.getTop();
for (int i = 1; current; i++)
{
cout << "\t" << i << ". " << current->value << endl;
current = current->next;
}
cout << endl;
}
void pop()
{
if (stack.isEmpty())
{
cout << "-> No item to take out! :(\n\n";
return;
}
cout << "-> Item taken out is: " << stack.pop() << "\n\n";
}
void stackSize()
{
cout << "-> Stack size: " << stack.getSize();
if (stack.isEmpty())
cout << ". No item found! :(";
cout << "\n\n";
}
void clearStack()
{
stack.clear();
cout << "-> Stack memory erased!\n\n";
}