-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackjw.h
executable file
·237 lines (206 loc) · 4.59 KB
/
stackjw.h
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
#ifndef STACKJW
#define STACKJW
//#include <iostream>
//#include <cstdlib>
//#include <fstream>
//#include <string>
//#include <sstream>
#include "linkedlistjw.h"
#include "nodejw.h"
#include "token.h"
using std::ostream;
using std::istream;
using std::endl;
enum STACK_ERRORS_LINK_JW {STACK_EMPTY_LINK_JW, STACK_FULL_LINK_JW, INVALID_MAXSIZE_LINK_JW};
template<typename T>
class stackJW:public linkedlist_jw
{
public:
stackJW(unsigned int maxSize = 10);
~stackJW();
stackJW(const stackJW<T> &other);
stackJW<T>& operator=(const stackJW<T> &other);
unsigned int size();
unsigned int max_size();
void clear();
void resize(unsigned int newSize);
void push(const T &data);
T pop();
T top();
T operator[](const size_t index);//done
stackJW<T>& operator<<(const T &data); //Chainable push
stackJW<T>& operator>>(T &data); // Chainable pop
void print();
template<typename R>
friend
ostream& operator<<(ostream& out, const stackJW<R> &s);
template<typename R>
friend
istream& operator>>(istream& in, stackJW<R> &s);
private:
void copy(const stackJW
<T> &other);
void nukem();
};
template<typename T>
T stackJW<T>::operator[](const size_t index){
unsigned int i = 0;
if(index>qty-1)
throw STACK_EMPTY_LINK_JW;
for(baseNodeJW * ptr = tail;ptr && ptr!=head && i<=index; ptr = ptr->prevNode(), i++);
T data = *(T*)tail->prevNode()->getValue();
return data;
}
template<typename T>
void stackJW<T>::print(){
for(baseNodeJW * ptr = head; ptr && ptr!=tail; ptr = ptr->nextNode()){
std::cout<<*(T*)ptr->getValue();
}
}
template<>
void stackJW<Token *>::print(){
for(baseNodeJW * ptr = head;ptr && ptr!=tail; ptr = ptr->nextNode()){
std::cout<<*(*(Token **)ptr->getValue());
}
}
template<typename T>
stackJW<T>::stackJW(unsigned int maxSize)
{
if(maxSize < 2)
throw INVALID_MAXSIZE_LINK_JW;
qty = 0;
maxQty= maxSize;
}
template<typename T>
stackJW<T>::~stackJW()
{
nukem();
maxQty = 0;
}
template<typename T>
stackJW<T>::stackJW(const stackJW<T> &other)
{
copy(other);
}
template<typename T>
stackJW<T>& stackJW<T>::operator=(const stackJW<T> &other)
{
if(this != &other)
{
nukem();
copy(other);
}
return *this;
}
template<typename T>
unsigned int stackJW<T>::size()
{
return qty;
}
template<typename T>
unsigned int stackJW<T>::max_size()
{
return maxQty;
}
template<typename T>
void stackJW<T>::clear()
{
nukem();
}
template<typename T>
void stackJW<T>::resize(unsigned int newSize)
{
nukem();
if(newSize < 2)
throw INVALID_MAXSIZE_LINK_JW;
maxQty = newSize;
}
template<typename T>
void stackJW<T>::push(const T &data)
{
//insert at the tail
if(full())
throw STACK_FULL_LINK_JW;
baseNodeJW* whom = new nodeJW<T>(data);
this->insert(whom);
}
template<typename T>
T stackJW<T>::pop()
{
//delate at the tail;
if(qty==0)
throw STACK_EMPTY_LINK_JW;
baseNodeJW *ptr = tail->prevNode();
baseNodeJW *ptrP = ptr->prevNode();
qty--;
T data = *(T*)tail->prevNode()->getValue();
if(qty==0){
// head->nextNode() = tail;
// tail->prevNode() = head;
head = tail = NULL;
}else{
ptrP->nextNode() = tail;
tail->prevNode() = ptrP;
}
delete ptr;
return data;
}
template<typename T>
T stackJW<T>::top()
{
if(!head)
throw STACK_EMPTY_LINK_JW;
return *(T*)(tail->prevNode())->getValue();
}
template<typename T>
stackJW<T>& stackJW<T>::operator<<(const T &data) //Chainable push
{
push(data);
return *this;
}
template<typename T>
stackJW<T>& stackJW<T>::operator>>(T &data) // Chainable pop
{
data = pop();
return *this;
}
template<typename T>
void stackJW<T>::copy(const stackJW<T> &other)
{
baseNodeJW *ptr = other.head;
for(; ptr; ptr = ptr->nextNode()){
linkedlist_jw::insert(ptr);
}
qty = other.qty;
maxQty = other.maxQty;
}
template<typename T>
void stackJW<T>::nukem()
{
baseNodeJW *ptr;
qty = 0;
while(head)
{
ptr = head;
head = head->nextNode();
delete ptr;
}
head = tail = NULL;
}
template<typename R>
ostream& operator<<(ostream& out, const stackJW<R> &s)
{
baseNodeJW *ptr = s.head;
for(; ptr; ptr = ptr->nextNode())
out<<*(R*)ptr->getValue()<<endl;
return out;
}
template<typename R>
istream& operator>>(istream& in, stackJW<R> &s)
{
nodeJW<R> newNode;
while(in>>newNode)
s<<newNode.getData();
return in;
}
#endif // STACKJW