/
theStack.cpp
195 lines (172 loc) · 3.9 KB
/
theStack.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
#include "theStack.h"
theStack::theStack(int s)
{
if(s < 1)
throw sINVALID_SIZE;
cap = s;
anchor = new node();
anchor->next = NULL;
tos = -1;
}
theStack::theStack(const theStack &other)
{
copy(other);
}
theStack::~theStack()
{
if (anchor->next)
nukem(anchor->next);// anchor holds no key, so start from its next
anchor = new node();
anchor->next = NULL;
tos = -1;
}
theStack& theStack::operator=(const theStack &other)
{
if(this != &other)
{
if (anchor->next)
nukem(anchor->next);// anchor holds no key, so start from its next
anchor = new node();
anchor->next = NULL;
copy(other);
}
return *this;
}
bool theStack::empty()
{
// or we could do tos == -1
return anchor->next == NULL;
}
bool theStack::full()
{
return false;
}
void theStack::clear()
{
if (anchor->next)
nukem(anchor->next);// anchor holds no key, so start from its next
tos = -1;
anchor = new node();
anchor->next = NULL;
}
void theStack::push(void *x, const char theType)
{
if(full())
throw sFULL;
tos++; // we successfully are adding something to the stack
// switch here
node *newNext = new node(x, theType, anchor->next); // make a nodepointer with a node with key *x
newNext->next = anchor->next; // put it into the head position and keep the others afterward
anchor->next = newNext; // point anchor to it
}
node* theStack::top()
{
if (anchor->next)
return anchor->next; // we always push from the head
return NULL;
}
node* theStack::pop()
{
if(empty())
throw sEMPTY;
--tos;
nodePtr holder = anchor->next;
anchor->next = holder->next;
holder->next = NULL;
return holder;
}
int theStack::size()
{
return tos + 1;
}
int theStack::capacity()
{
return cap;
}
void theStack::resize(int s)
{
cap = s;
if(s < 1)
throw sINVALID_SIZE;
if (anchor->next)
nukem(anchor->next);// anchor holds no key, so start from its next
tos = -1;
}
void theStack::bye(node *top)
{
delete top;
top = NULL;
}
void theStack::nukem(node* top)
{
if (top->next)
nukem(top->next);
bye(top); // changed from !top->next
}
void theStack::copy(const theStack &other)
{
cap = other.cap;
tos = other.tos;
node* otherPtr = other.anchor->next;
node* myPtr = anchor;
while (otherPtr)
{
switch(otherPtr->data_type){
case 'N':
myPtr->next=new node(otherPtr->key.mPtr, otherPtr->data_type);
break;
case 'D':
myPtr->next=new node(otherPtr->key.dPtr, otherPtr->data_type);
break;
case 'O':
case 'P':
myPtr->next=new node(otherPtr->key.opPtr, otherPtr->data_type);
break;
default:
break;
}
otherPtr = otherPtr->next;
myPtr = myPtr->next;
}
}
theStack& theStack::operator<<(const theStack &other)
{
copy(other);
return *this;
}
theStack& theStack::operator>>(theStack &other)
{
other.copy(*this);
return *this;
}
ostream& operator<<(ostream &out, const theStack &s)
{
if (s.tos != -1)
{
if(out == cout)
out<<"The stack contains "<<s.tos+1<<" items. They are: "<<endl;
else
out<<"Capacity:"<<s.cap<<endl
<<"TOS:"<<s.tos<<endl;
node* Pointer = s.anchor->next;
while (Pointer){
switch(Pointer->data_type)
{
case 'N':
out << *(Pointer->key.mPtr) << endl;
break;
case 'D':
out << *(Pointer->key.dPtr) << endl;
break;
case 'O':
case 'P':
out << (Pointer->key.opPtr->theOp) << endl;
break;
default:
break;
}
Pointer = Pointer->next;
}
}
return out;
}