-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackM.cpp
More file actions
245 lines (207 loc) · 3.91 KB
/
stackM.cpp
File metadata and controls
245 lines (207 loc) · 3.91 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
/**
* @file stackM.cpp
* @author Yupeng Zhao
* @Date 2018-06-27
* @version 1.0
* @section DESCRIPTION:
This program uses a linked list to define stack elements and
uses class for stack manipulation. The size of the stack is
determined by the user as an input. Two exceptions for manipulating
class are pushing data into a full class, and popping data from an
empty class.
*/
#include<iostream>
#include<string>
using namespace std;
struct Node
{
char a;
Node* next;
};
typedef Node* NodePtr;
class Stack
{
public:
Stack();
Stack(int a);
int getMaxSize();
int getCurrentSize();
void dispStack();
void pushNext(char c);
// Insert the new node at the head
char popNext();
// Remove and return a node from the head
private:
int size;
// Input size for stack
int currentSize;
// Current size for stack
NodePtr head;
// Head pointer for stack
};
class StackOverflowException
{
public:
StackOverflowException(string a);
string getMessageE();
private:
string messageE;
};
class StackEmptyException
{
public:
StackEmptyException(string a);
string getMessageE();
private:
string messageE;
};
int main()
{
Stack stack1;
int size = 0;
// int currentSize = 0;
char ans = '\0';
bool loopCond = false;
char pushChar = '\0';
char poppedChar = '\0';
cout << "Enter the size of the stack: ";
cin >> size;
if(cin.fail())
{
cerr << "Error: Wrong entry type." << endl
<< "Terminating program." << endl;
exit(1);
}
else if(size < 0)
{
cerr << "Error: Stack size cannot be negative." << endl
<< "Terminating program." << endl;
exit(1);
}
cin.ignore(100, '\n');
stack1 = Stack(size);
do
{
cout << "Press '1' to push a new element into the stack." << endl
<< "Press '2' to pop an element from the stack." << endl
<< "Press '3' to exit." << endl;
cin >> ans;
try
{
switch(ans)
{
case '1':
cout << "Enter character to be pushed in: ";
cin >> pushChar;
cin.ignore(100, '\n');
stack1.pushNext(pushChar);
loopCond = true;
break;
case '2':
poppedChar = stack1.popNext();
cout << "Character '" << poppedChar << "' has been popped out." << endl;
loopCond = true;
break;
case '3':
cout << "Exiting simulation." << endl;
loopCond = false;
break;
default:
cout << "Your entry could not be recognized." << endl
<< "Try agian." << endl;
loopCond = true;
}
cout << "Max stack size " << stack1.getMaxSize()
<< ", current stack size " << stack1.getCurrentSize() << endl;
cout << "The current stack is ";
stack1.dispStack();
cout << endl << endl;
}
catch(StackOverflowException e)
{
cout << e.getMessageE() << endl;
cout << "Ending silumation." << endl;
loopCond = false;
}
catch(StackEmptyException e)
{
cout << e.getMessageE() << endl;
cout << "Ending simulatoin." << endl;
loopCond = false;
}
}while(loopCond);
return 0;
}
Stack::Stack()
{
size = 0;
currentSize = 0;
head = nullptr;
}
Stack::Stack(int a)
{
size = a;
currentSize = 0;
head = nullptr;
}
int Stack::getMaxSize()
{
return(size);
}
int Stack::getCurrentSize()
{
return(currentSize);
}
void Stack::dispStack()
{
NodePtr temp = head;
while(temp != nullptr)
{
cout << temp->a << " ";
temp = temp->next;
}
}
void Stack::pushNext(char c)
{
if(currentSize >= size)
{
throw StackOverflowException("Error: Stack is full.");
}
NodePtr temp;
temp = new Node;
temp->a = c;
temp->next = head;
head = temp;
currentSize++;
}
char Stack::popNext()
{
if(currentSize <= 0)
{
throw StackEmptyException("Error: Stack is empty.");
}
char c;
NodePtr temp;
temp = head;
c = head->a;
head = head->next;
delete temp;
currentSize--;
return(c);
}
StackOverflowException::StackOverflowException(string a)
{
messageE = a;
}
string StackOverflowException::getMessageE()
{
return messageE;
}
StackEmptyException::StackEmptyException(string a)
{
messageE = a;
}
string StackEmptyException::getMessageE()
{
return messageE;
}