Template
Chargement...
Recherche...
Aucune correspondance
QList.h
1#ifndef LIB_SCP_QLIST
2#define LIB_SCP_QLIST
3
4#ifndef NULL
5 #define NULL 0
6#endif
7
8template<class T>
9class QList
10{
11private:
12 typedef struct node
13 {
14 T item;
15 node *next,*prev;
16 }node;
17
18 int len; // Size of list
19 node *start,*end; // Pointers to start and end
20
21public:
22 QList(); // Class constructor
23 ~QList(); // Class destructor
24
25 void push_back(const T i); // Push item at the back of list
26 void push_front(const T i);// Push item at the front of the list
27 void pop_back(); // Pops item from back
28 void pop_front(); // Pops item from front
29 T front(); // get item from front
30 T back(); // get item from back
31 int size(); // Returns size of list
32 void clear(); // Clears list
33 void clear(unsigned int index); // Clears list
34 T get(unsigned int index); // Get item at given index
35 T& at(unsigned int index); // Get item at given index
36
37 // Array operator
38 T& operator[](unsigned int index);
39 const T& operator[](unsigned int index) const; // Not realy needed
40
41 // Non - critical functions
42 int length();
43 int indexOf(T val);
44
45};
46
47
48
49// Constructor
50template<class T>
52{
53 len = 0;
54 start = NULL;
55 end = NULL;
56}
57
58// Destructor
59template<class T>
61{
62 clear();
63}
64
65// Push at front
66template<class T>
67void QList<T>::push_front(const T i)
68{
69 node *tmp = new node;
70 tmp->item = i;
71 tmp->next = NULL;
72 tmp->prev = NULL;
73
74 if(start==NULL) // If list is empty
75 {
76 start = tmp;
77 end = tmp;
78 }
79 else // Insert at start
80 {
81 tmp->next = start;
82 start->prev = tmp;
83 start = tmp;
84 }
85 len++; // Increase size counter
86}
87
88// Push at back
89template<class T>
90void QList<T>::push_back(const T i)
91{
92 node *tmp = new node;
93 tmp->item = i;
94 tmp->next = NULL;
95 tmp->prev = NULL;
96
97 if(end==NULL) // If list is empty
98 {
99 start = tmp;
100 end = tmp;
101 }
102 else // Insert at the end
103 {
104 tmp->prev = end;
105 end->next = tmp;
106 end = tmp;
107 }
108 len++; // Increase size counter
109}
110
111// Pop from front
112template<class T>
114{
115 if(start!=NULL)
116 {
117 node *tmp = start;
118 start = start->next;
119 if(start!=NULL) // Re-link next item to NULL
120 start->prev = NULL;
121 else // List became empty so we need to clear end
122 end = NULL;
123 delete tmp;
124 len--; // Decrease counter
125 }
126}
127
128// Pop from back
129template<class T>
131{
132 if(end!=NULL)
133 {
134 node *tmp = end;
135 end = end->prev;
136 if(end!=NULL) //Re-link previous item to NULL
137 end->next = NULL;
138 else // List became empty so we need to clear start
139 start = NULL;
140 delete tmp;
141 len--; // Decrease counter
142 }
143}
144
145// Get item from front
146template<class T>
148{
149 if(start!=NULL)
150 return start->item;
151 //TODO: Catch error when list is empty
152}
153
154//Get item from back
155template<class T>
157{
158 if(end!=NULL)
159 return end->item;
160 //TODO: Catch error when list is empty
161}
162
163// Get size
164template<class T>
166{
167 return this->len;
168}
169
170// Clear list
171template<class T>
173{
174 node *tmp = start;
175 while(start!=NULL)
176 {
177 tmp = start;
178 start = start->next;
179 delete tmp; // Delete item
180 len--; // Decrease counter
181 }
182 end = NULL;
183}
184template<class T>
185void QList<T>::clear(unsigned int index)
186{
187 node *tmp = start;
188 for(int i=0;i<=index&&tmp!=NULL;i++)
189 {
190 if(i==index)
191 {
192 if(tmp->prev!=NULL)
193 tmp->prev->next = tmp->next;
194 else
195 start = tmp->next;
196
197 if(tmp->next!=NULL)
198 tmp->next->prev = tmp->prev;
199 else
200 end = tmp->prev;
201
202 len--; // Decrease counter
203 delete tmp; // Delete item
204 break;
205 }
206 else
207 tmp=tmp->next;
208 }
209}
210
211// Get at index
212template<class T>
213T QList<T>::get(unsigned int index)
214{
215 node *tmp = start;
216 for(int i=0;i<=index&&tmp!=NULL;i++)
217 {
218 if(i==index)
219 return tmp->item;
220 else
221 tmp=tmp->next;
222 }
223 //TODO: Catch error when index is out of range
224}
225
226template<class T>
227T& QList<T>::at(unsigned int index)
228{
229 node *tmp = start;
230 for(int i=0;i<=index&&tmp!=NULL;i++)
231 {
232 if(i==index)
233 return tmp->item;
234 else
235 tmp=tmp->next;
236 }
237 //TODO: Catch error when index is out of range
238}
239
240// Get length
241template<class T>
243{
244 return this->len;
245}
246
247// Get index of value
248template<class T>
250{
251 for(int i=0;i<this->size();i++)
252 if(this->at(i) == val)
253 return i;
254 return -1;
255}
256
257// Array operators
258template<class T>
259T& QList<T>::operator[](unsigned int index)
260{
261 node *tmp = start;
262 for(int i=0;i<=index&&tmp!=NULL;i++)
263 {
264 if(i==index)
265 return tmp->item;
266 else
267 tmp=tmp->next;
268 }
269 //TODO: Catch error when index is out of range
270}
271
272
273template<class T>
274const T& QList<T>::operator[](unsigned int index) const
275{
276 node *tmp = start;
277 for(int i=0;i<=index&&tmp!=NULL;i++)
278 {
279 if(i==index)
280 return tmp->item;
281 else
282 tmp=tmp->next;
283 }
284 //TODO: Catch error when index is out of range
285}
286
287#endif
Definition: QList.h:10
void push_back(const T i)
Definition: QList.h:90
void clear(unsigned int index)
Definition: QList.h:185
int size()
Definition: QList.h:165
T get(unsigned int index)
Definition: QList.h:213
QList()
Definition: QList.h:51
T front()
Definition: QList.h:147
T back()
Definition: QList.h:156
~QList()
Definition: QList.h:60
void pop_front()
Definition: QList.h:113
int length()
Definition: QList.h:242
void push_front(const T i)
Definition: QList.h:67
const T & operator[](unsigned int index) const
Definition: QList.h:274
T & operator[](unsigned int index)
Definition: QList.h:259
int indexOf(T val)
Definition: QList.h:249
void pop_back()
Definition: QList.h:130
T & at(unsigned int index)
Definition: QList.h:227
void clear()
Definition: QList.h:172