-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.h
116 lines (92 loc) · 3.38 KB
/
linkedList.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
/*************************************************************************
linkedList
-------------------
début : 27/11/2023
copyright : (C) 2023 par Jixiang, Adam, Clément, Louis
binome : B3311 et B3309
*************************************************************************/
//---------- Interface de la classe <linkedList> (fichier linkedList.h)
//----------------
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
//------------------------------------------------------------------ Types
template <typename T>
struct Node
// Node of the linkedList
// T : the type of the data that the node will contain
// pdata : the data that the node will contain
// next : the next node of the linkedList
{
T *pdata;
Node<T> *next;
};
//------------------------------------------------------------------------
// Rôle de la classe <linkedList>
//
// This class represents a linkedList.
// It is used to store a linkedList of data which are the journeys
//
//------------------------------------------------------------------------
template <typename T> class LinkedList {
//----------------------------------------------------------------- PUBLIC
public:
//----------------------------------------------------- Méthodes publiques
void Add(T *pdata, bool AddToEnd = true)
// Mode d'emploi :
// This function will add a data to the linkedList.
// pdata : the data to add
// AddToEnd : if AddToEnd is true, the data will be added to the end of the
// linkedList.
// Contrat :
// important:should pass a new Journey* object,
// LinkedList will make the GC for it
// data should have a refCount
// Algorithme :
// If the linkedList is empty, add the data to the linkedList and update
// _first and _last. If the linkedList is not empty, add the data to the
// linkedList and update _first or _last depending on the value of AddToEnd
{
Node<T> *node = new Node<T>{pdata, nullptr};
pdata->refCount++;
if (!_first)
_first = _last = node;
else if (AddToEnd)
_last = _last->next = node;
else {
node->next = _first;
_first = node;
};
}
Node<T> *GetFirst(void) const { return _first; }
Node<T> *GetLast(void) const { return _last; }
//-------------------------------------------- Constructeurs - destructeur
virtual ~LinkedList()
// Algorithme :
// Delete all the nodes of the linkedList
// While there is a first node, delete the first node
// RefCount is used to know if the data is still used or not
// If the data is not used anymore, delete it
{
#ifdef MAP
std::cout << "Destructor called for <LinkedList>" << std::endl;
#endif
while (_first) {
_last = _first; // from here on, _last is only used as a temp var
_first = _first->next;
_last->pdata->refCount--;
if (!_last->pdata->refCount)
delete _last->pdata;
delete _last;
}
}
//----------------------------------------------------- Attributs publics
unsigned int refCount = 0;
// Attribute that is public because it is used by the catalog to know if a
// journey is still used or not
protected:
//------------------------------------------------------------------ PRIVE
//----------------------------------------------------- Attributs protégés
Node<T> *_first = nullptr;
Node<T> *_last = nullptr;
};
#endif