-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathList.cpp
224 lines (186 loc) · 4.32 KB
/
List.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
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
#include <iostream>
#include <cstdlib>
#include "List.h"
#include "Node.h"
//dn xreiazontai an kanw include to .cpp file
#include "dataTypes.h"
#include "euclideanNode.h"
#include "clusterNode.h"
#include "kMedoids.h"
using namespace std;
template <class T>
List<T>::List()
{
listSize = 0;
start = NULL;
last = NULL;
}
template <class T>
List<T>::~List()
{
Node<T>* tmp = start;
Node<T>* del;
while(tmp != NULL)//diagrafoume enan enan tous komvous
{
del = tmp;
tmp = tmp->get_next();
listSize--;
delete del;
}
}
template <class T>
bool List<T>::checkEmpty()
{
return(listSize == 0);
}
template <class T>
int List<T>::getSize()
{
return listSize;
}
template <class T>
Node<T>* List<T>::get_begin()
{
return start;
}
template <class T>
Node<T>* List<T>::get_last()
{
return last;
}
template <class T>
Node<T>* List<T>::insertBeginning(T item) //epistrefei to node pou dhmiourghthike
{
Node<T>* node = new Node<T>(item);
if(checkEmpty()) //an einai to monadiko stoixeio prepei na allaksw ton deikti start kai last
{
start = node;
last = node;
listSize++;
return node;
}
//alliws eisodos stin arxi
node->set_next(start);
start->set_prev(node);
start = node;
listSize++;
return node;
}
template <class T>
Node<T>* List<T>::insertEnd(T item)
{
Node<T>* node = new Node<T>(item);
if(checkEmpty()) //an einai to monadiko stoixeio prepei na allaksw ton deikti start kai last
{
start = node;
last = node;
listSize++;
return node;
}
//alliws eisodos sto telos ths listas
last->set_next(node);
node->set_next(NULL);
node->set_prev(last);
last = node;
listSize++;
return node;
}
template <class T>
T List<T>::deleteFirstNode()
{
Node<T>* tmp;
T tmpAccount;
if(checkEmpty())
{
return 0;
}
tmp = start;
start = start->get_next();
if(listSize == 1) last = NULL; //arxi kai telos deixnoun sto idio, epomenos i lista exei mono ena stoixeio
else start->set_prev(NULL);
tmpAccount = tmp->get_data();
delete tmp;
listSize--;
return tmpAccount;
}
template <class T>
T List<T>::deleteLastNode()
{
Node<T>* tmp;
T tmpAccount;
if(checkEmpty())
{
return 0;
}
if(listSize == 1) //an exei mono ena stoixeio tote prepei na allaksei kai o deiktis start
{
tmpAccount = start->get_data();
delete start;
start = NULL;
last = NULL;
listSize--;
return tmpAccount;
}
tmpAccount = last->get_data();
tmp = last;
last = last->get_prev();
last->set_next(NULL);
delete tmp;
listSize--;
return tmpAccount;
}
template <class T>
T List<T>::deleteNode(Node<T>* node) //diagrafei tou sigekrimenou node
{
Node<T>* tmp;
T data;
if(checkEmpty() == true)
{
return 0;
}
if(start == node) // an einai o prwtos 'h o monadikos komvos
{
return this->deleteFirstNode();
}
if(last == node) //an o teleutaios
{
return this->deleteLastNode();
}
//elenxos gia sfalma
if(node->get_next() == NULL || node->get_prev()== NULL) return 0; //afou dn einai to prwto 'h teleutaio stoixeio dn prepei na einai null oi deiktes
tmp = node->get_prev();
tmp->set_next(node->get_next());
node->get_next()->set_prev(tmp);
data = node->get_data();
listSize--;
delete node;
return data;
}
template <class T>
T List<T>::getPoint(int pos) //epistrefei to tyxaio simeio
{
int count = 0;
for(Node<T>* i = start; i != NULL; i = i->get_next())
{
if(count == pos)
{
return i->get_data();
}
count++;
}
}
template class List<int>;
template class List<double>;
template class List<string>;
template class List<Vector* >;
template class List<Hamming* >;
template class List<EuclideanNode* >;
template class List<MatrixPoint* >;
template class List<ClusterNode<Vector*>*>;
template class List<ClusterNode<Hamming*>*>;
template class List<ClusterNode<EuclideanNode*>*>;
template class List<ClusterNode<MatrixPoint*>*>;
template class List<LshAssingNode<Vector*>*>;
template class List<LshAssingNode<Hamming*>*>;
template class List<LshAssingNode<EuclideanNode*>*>;
template class List<LshAssingNode<MatrixPoint*>*>;