-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathList.cpp
189 lines (164 loc) · 3.44 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
#include <cstdlib>
#include "List.h"
#include "Node.h"
#include "bankAccount.h"
#include "transaction.h"
#include <iostream>
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>
void List<T>::insertBeginning(T* account)
{
Node<T>* node = new Node<T>(account);
if(checkEmpty()) //an einai to monadiko stoixeio prepei na allaksw ton deikti start kai last
{
start = node;
last = node;
listSize++;
return;
}
//alliws eisodos stin arxi
node->set_next(start);
start = node;
listSize++;
}
template <class T>
void List<T>::insertEnd(T* account)
{
Node<T>* node = new Node<T>(account);
if(checkEmpty()) //an einai to monadiko stoixeio prepei na allaksw ton deikti start kai last
{
start = node;
last = node;
listSize++;
return;
}
//alliws eisodos sto telos ths listas
last->set_next(node);
node->set_next(NULL);
last = node;
listSize++;
}
template <class T>
T* List<T>::deleteFirstNode()
{
Node<T>* tmp;
T* tmpAccount;
if(checkEmpty())
{
return NULL;
}
tmp = start;
start = start->get_next();
if(listSize == 1) last = NULL; //arxi kai telos deixnoun sto idio, epomenos i lista exei mono ena stoixeio
tmpAccount = tmp->get_data();
delete tmp;
listSize--;
return tmpAccount;
}
template <class T>
T* List<T>::deleteLastNode()
{
Node<T>* tmp = start;
T* tmpAccount;
if(checkEmpty())
{
return NULL;
}
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;
}
while(tmp->get_next() != last) //pairnw to proteleutaio stoixeio
{
tmp = tmp->get_next();
}
tmpAccount = last->get_data();
delete last;
last = tmp;
tmp->set_next(NULL);
listSize--;
return tmpAccount;
}
template <class T>
T* List<T>::deleteNode(Node<T>* node) //diagrafei tou sigekrimenou node
{
Node<T>* tmp = start;
Node<T>* del = NULL;
T* data = NULL;
if(checkEmpty() == true)
{
return NULL;
}
if(start == node) // an einai o prwtos 'h o monadikos komvos
{
return this->deleteFirstNode();
}
if(last == node) //an o teleutaios
{
return this->deleteLastNode();
}
while(tmp->get_next() != node)
{
if(tmp->get_next() == NULL)
{
return NULL;
}
tmp = tmp->get_next();
}
del = tmp->get_next();
tmp->set_next(del->get_next());
data = del->get_data();
listSize--;
delete del;
return data;
}
template class List<int>;
template class List<double>;
template class List<std::string>;
template class List<bankAccount>;
template class List<Transaction>;