Mercurial > hg > th-libs
comparison th_datastruct.c @ 756:4bd82aca5e98
Fix list length when reversing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 01 Feb 2023 14:09:12 +0200 |
parents | 3f84521fdd31 |
children | 2ab2fece83ea |
comparison
equal
deleted
inserted
replaced
755:3f84521fdd31 | 756:4bd82aca5e98 |
---|---|
225 void th_llist_reverse(th_llist_t **list) | 225 void th_llist_reverse(th_llist_t **list) |
226 { | 226 { |
227 th_llist_t | 227 th_llist_t |
228 *f_curr = *list, *f_prev = NULL, | 228 *f_curr = *list, *f_prev = NULL, |
229 *b_curr = *list, *b_prev = NULL; | 229 *b_curr = *list, *b_prev = NULL; |
230 | 230 size_t count = 0; |
231 | |
232 // NULL list? | |
233 if (f_curr == NULL) | |
234 return; | |
235 | |
236 // Set initial previous backward pointer | |
231 if (b_curr != NULL) | 237 if (b_curr != NULL) |
232 b_prev = b_curr->prev; | 238 b_prev = b_curr->prev; |
233 | 239 |
240 // Reverse the list | |
234 while (f_curr != NULL) | 241 while (f_curr != NULL) |
235 { | 242 { |
236 th_llist_t | 243 th_llist_t |
237 *f_next = f_curr->next, | 244 *f_next = f_curr->next, // next = current->next |
238 *b_next = b_curr->prev; | 245 *b_next = b_curr->prev; |
239 | 246 |
240 f_curr->next = f_prev; | 247 count++; |
241 f_prev = f_curr; | 248 f_curr->num = 0; |
242 f_curr = f_next; | 249 |
250 f_curr->next = f_prev; // current->next = previous | |
251 f_prev = f_curr; // previous = current | |
252 f_curr = f_next; // current = next | |
243 | 253 |
244 b_curr->prev = b_prev; | 254 b_curr->prev = b_prev; |
245 b_prev = b_curr; | 255 b_prev = b_curr; |
246 b_curr = b_next; | 256 b_curr = b_next; |
247 } | 257 } |
248 | 258 |
259 // Update count | |
260 f_prev->num = count; | |
261 | |
262 // Set list pointer | |
249 *list = f_prev; | 263 *list = f_prev; |
264 | |
265 // Fix last previous pointer | |
250 if (b_curr != NULL) | 266 if (b_curr != NULL) |
251 b_curr->prev = b_prev; | 267 b_curr->prev = b_prev; |
252 } | 268 } |
253 | 269 |
254 | 270 |