-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.h
More file actions
148 lines (129 loc) · 4.52 KB
/
StackArray.h
File metadata and controls
148 lines (129 loc) · 4.52 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
#include <iostream>
using namespace std;
// StackException class for handling errors
class StackException
{
private:
string msg;
public:
StackException(string str) : msg(str) {}
string getMessage()
{
return msg;
}
};
// Template class for StackArray
template <class T>
class StackArray
{
private:
T *stack;
int top, size;
public:
// StackArray constructor: create an empty stack with a given size
StackArray(int n = 0) : stack(new T[n]), top(-1), size(n) {}
// StackArray Destructor: delete the stack
~StackArray() { delete[] stack; }
// Check if the stack is empty
const bool empty() const { return top == -1; }
// Push an element onto the stack
void push(const T &val);
// Pop an element from the stack
T pop();
void stackArrayMenu();
};
// StackArray push operation
template <class T>
void StackArray<T>::push(const T &val)
{
if (top == size - 1)
{
throw StackException(
"\n===============================================\n"
"| ⚠️ Error: Overflow |\n"
"===============================================\n"
"| Stack is full. Cannot push any more elements |\n"
"===============================================\n");
}
top = top + 1;
stack[top] = val;
}
// StackArray pop operation
template <class T>
T StackArray<T>::pop()
{
if (top == -1)
{
throw StackException(
"\n===============================================\n"
"| ⚠️ Error: Underflow |\n"
"===============================================\n"
"| Stack is empty. Cannot pop any elements. |\n"
"===============================================\n");
}
T val = stack[top];
top = top - 1;
return val;
}
// Template function to handle stack menu
template <class T> void StackArray<T>::stackArrayMenu(){
int choice;
do
{
cout << "\n==========================================\n";
cout << "| Stack Menu |\n";
cout << "==========================================\n";
cout << "| 1. Push element onto the stack |\n";
cout << "| 2. Pop element from the stack |\n";
cout << "| 3. Exit |\n";
cout << "==========================================\n";
cout << "Enter your choice: ";
cin >> choice;
try
{
switch (choice)
{
case 1:
cout << "\n-----------------------------------------------\n";
cout << "| ➕ Push Element |\n";
cout << "-----------------------------------------------\n";
cout << "Enter value to push: ";
T value;
cin >> value;
push(value);
cout << "✅ Successfully pushed " << value << " onto the stack.\n";
cout << "-----------------------------------------------\n";
break;
case 2:
cout << "\n-----------------------------------------------\n";
cout << "| ➖ Pop Element |\n";
cout << "-----------------------------------------------\n";
cout << "✅ Successfully popped " << pop() << " from the stack.\n";
cout << "-----------------------------------------------\n";
break;
case 3:
cout << "\n-----------------------------------------------\n";
cout << "| 🚪 Exit Selected |\n";
cout << "-----------------------------------------------\n";
break;
default:
cout << "\n-----------------------------------------------\n";
cout << "| ❌ Invalid Choice |\n";
cout << "-----------------------------------------------\n";
cout << "| Please enter a valid option (1-3). |\n";
cout << "-----------------------------------------------\n";
}
}
catch (StackException &e)
{
cout << e.getMessage();
}
} while (choice != 3);
}
// Main function
// int main()
// {
// StackArray<int> stack(5); // Create a stack with size 5
// stackMenu(stack); // Call the menu template function
// return 0;
// }