This repository has been archived by the owner on May 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CList.h
279 lines (211 loc) · 7.27 KB
/
CList.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
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
273
274
275
276
277
278
279
//Header for Lists with various functions
// @Author = Jan van Dick
#ifndef CLIST_H
#define CLIST_H
#include "CFunctions.h"
//Class for lists with various function
template < typename T1>
class CList
{
private:
T1 *m_cur; //current element
CList *m_next; //current node
unsigned int m_numElements; //Number of elemnts in the list
public:
//Constructor
CList()
{
m_cur = NULL;
m_next = NULL;
m_numElements = 0;
}
void set(T1* element) {
m_cur = element; //Set cur element
}
//Add: add new element to list
void add(T1* element)
{
m_numElements++;
//if there is a next node, call "add" with next node
if(m_next)
m_next->add(element);
//if there is no next node, create new node and call "set" from this node
else
{
m_next = new CList;
m_next->set(element);
}
return;
}
//Out: print element(Name) of each element in list
void out()
{
//Check for empty list
if(!m_next)
return;
CList* temp = this->m_next; //Create temp
//Print name of current element
cout << m_next->m_numElements << ": " << temp->m_cur->getName() << endl;
//if there is a next node, call "out" with next node
if(temp->m_next)
temp->out();
return;
}
//Out: print element(Name) of each element in list
void outNum()
{
//Check for empty list
if(!m_next)
return;
CList* temp = this->m_next; //Create temp
//Print name of current element
cout << m_next->m_numElements << endl;
//if there is a next node, call "out" with next node
if(temp->m_next)
temp->outNum();
return;
}
//Out2: print element(name) of each element in list, without free lines
void out2()
{
//Check for empty list
if(!m_next)
return;
CList* temp = this->m_next; //Create temp
//Print name of current element
cout << temp->m_cur->getName();
//if there is a next node, call "out2" with next node
if(temp->m_next)
{
cout << ", ";
temp->out2();
}
else
cout << endl;
return;
}
//CheckElementByName: check if the given name matches the name of an
//element in the list
bool checkElementByName(char* chElementName)
{
//Check for empty list
if(!m_next)
return false;
CList* temp = this->m_next; //Create temp
//Variables
CFunctions F; //Suplies various functions
//Name of the current element matches given name: return true
if(F.compare(chElementName, temp->m_cur->getName()) == true)
return true;
//Name doesn't match, but next node exists: return function-value with via node
else if(temp->m_next)
return temp->checkElementByName(chElementName);
//Name doesn't matcha and no next node exists: return false
else
return false;
}
//GetElement (ByName): check if the given name matches the name of an
//element in the list, if yes, then return that element
T1* getElement(char* chElementName)
{
//Check for empty list
if(!m_next)
return NULL;
CList* temp = this->m_next; //Create temp
//Variables
CFunctions F; //Suplies various functions
//Name of the current element matches given name: return the current element
if(F.compare(chElementName, temp->m_cur->getName()) == true)
return temp->m_cur;
//Name doesn't match, but next node exists: return function-value via next node
else if(temp->m_next)
return temp->getElement(chElementName);
//Name doesn't match and no next node exists: return "NULL"
else
return NULL;
}
//GetElementByName: check if the given name CONTAINS the name of an
//element in the list
T1* getElementByNameContains(char* chElementName)
{
//Check for empty list
if(!m_next)
return NULL;
CList* temp = this->m_next; //Create temp
//Variables
CFunctions F; //Suplies the "contains"-function"
//given name contains name of current element: return current element
if(F.contains(chElementName, temp->m_cur->getName()) == true)
return temp->m_cur;
//given name doesn't contain name of current element, but next node exists:
//return function-value via next node
else if(temp->m_next)
return temp->getElementByNameContains(chElementName);
//given name doesn't contain name of current element, and no next node exists:
//return "NULL"
else
return NULL;
}
//GetElement (ByNum): check if the given number matches the number of the number
//of the element in the list
T1* getElement(int num)
{
//check for empty list
if(!m_next)
return NULL;
CList* temp = this->m_next; //Create temp
//Given number matches the number of the current element: return current element
if(num == temp->m_cur->getNum())
return temp->m_cur;
//Given number does not match the number of the current element,
//a next node exists: return function-value via next node
else if(temp->m_next)
return temp->getElement(num);
//Given number does not match the number of the current element,
//and no next node exists: return "NULL"
else
return NULL;
}
//getElement by node number
T1* iterateElements(unsigned int num)
{
//Given number matches m_numElements of current element: return current element
if(num == m_numElements)
return m_cur;
//Given number does not match m_numElements of current element,
//a next node exists: return function-value via next node
else if(m_next)
return m_next->iterateElements(num);
//Given number does not match m_numElements of current element,
//a next node does not exist: return NULL
else
return NULL;
}
//getFirstElement: simply retruns m_cur
T1* getFirstElement() {
return m_cur;
}
//getNumElements: return the number of elements
unsigned int getNumElements() {
return m_numElements;
}
//deleteNode: delete a given node
void deleteNode(int num)
{
//check for empty list
if(!m_next)
return;
CList* temp = this->m_next; //Create temp
m_numElements--; //Reduse number of elements in the list
//check whether number of current node matches given number
if(temp->m_cur->getNum() == num)
{
m_next = temp->m_next; //relink
delete temp; //delete
}
//if not, then check, whether there is a next node and call delete via next node
else if(temp->m_next)
m_next->deleteNode(num);
}
};
#endif