diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-02 10:58:50 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-02 10:58:50 +0200 |
| commit | cc79ccab56a112b2e928bff63ce1d24cd2a22e22 (patch) | |
| tree | a2be79ebbf71d82a0defea036188c89025c17ae8 /dlist.c | |
| parent | 09a96907962ac2d1115f545e194f06ea4fd3db19 (diff) | |
| download | data-structures-cc79ccab56a112b2e928bff63ce1d24cd2a22e22.tar.gz data-structures-cc79ccab56a112b2e928bff63ce1d24cd2a22e22.tar.bz2 data-structures-cc79ccab56a112b2e928bff63ce1d24cd2a22e22.zip | |
alten, seltsamen code zum Listensortieren entfernt. Eine Kopie davon gibt's in einem separaten Branch...
Diffstat (limited to 'dlist.c')
| -rw-r--r-- | dlist.c | 90 |
1 files changed, 0 insertions, 90 deletions
| @@ -277,96 +277,6 @@ remove_if(struct dlist *list) | |||
| 277 | } | 277 | } |
| 278 | } | 278 | } |
| 279 | 279 | ||
| 280 | void | ||
| 281 | dlist_sort_xxx(struct dlist *list) | ||
| 282 | { | ||
| 283 | if ( list->head == NULL ) | ||
| 284 | return; | ||
| 285 | |||
| 286 | int insize = 1; | ||
| 287 | |||
| 288 | while ( 1 ) { | ||
| 289 | struct dlist_element *p; | ||
| 290 | |||
| 291 | p = list->head; | ||
| 292 | list->head = NULL; | ||
| 293 | list->tail = NULL; | ||
| 294 | |||
| 295 | int nmerges = 0; /* count number of merges we do in this pass */ | ||
| 296 | |||
| 297 | while ( p ) { | ||
| 298 | struct dlist_element *q; | ||
| 299 | |||
| 300 | nmerges++; /* there exists a merge to be done */ | ||
| 301 | /* step `insize' places along from p */ | ||
| 302 | q = p; | ||
| 303 | int psize = 0; | ||
| 304 | for ( int i = 0; i < insize; i++ ) { | ||
| 305 | psize++; | ||
| 306 | q = q->next; | ||
| 307 | if ( !q ) | ||
| 308 | break; | ||
| 309 | } | ||
| 310 | |||
| 311 | /* if q hasn't fallen off end, we have two lists to merge */ | ||
| 312 | int qsize = insize; | ||
| 313 | |||
| 314 | /* now we have two lists; merge them */ | ||
| 315 | while ( psize > 0 || (qsize > 0 && q) ) { | ||
| 316 | struct dlist_element *e; | ||
| 317 | |||
| 318 | /* decide whether next element of merge comes from p or q */ | ||
| 319 | if ( psize == 0 ) { | ||
| 320 | /* p is empty; e must come from q. */ | ||
| 321 | e = q; | ||
| 322 | q = q->next; | ||
| 323 | qsize--; | ||
| 324 | } | ||
| 325 | else if ( qsize == 0 || !q ) { | ||
| 326 | /* q is empty; e must come from p. */ | ||
| 327 | e = p; | ||
| 328 | p = p->next; | ||
| 329 | psize--; | ||
| 330 | } | ||
| 331 | else if ( p->data < q->data ) { | ||
| 332 | /* First element of p is lower ; | ||
| 333 | * e must come from p. */ | ||
| 334 | e = p; | ||
| 335 | p = p->next; | ||
| 336 | psize--; | ||
| 337 | } | ||
| 338 | else { | ||
| 339 | /* First element of q is lower; e must come from q. */ | ||
| 340 | e = q; | ||
| 341 | q = q->next; | ||
| 342 | qsize--; | ||
| 343 | } | ||
| 344 | |||
| 345 | /* add the next element to the merged list */ | ||
| 346 | if ( list->tail ) { | ||
| 347 | list->tail->next = e; | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | list->head = e; | ||
| 351 | } | ||
| 352 | e->prev = list->tail; | ||
| 353 | list->tail = e; | ||
| 354 | } | ||
| 355 | |||
| 356 | /* now p has stepped `insize' places along, and q has too */ | ||
| 357 | p = q; | ||
| 358 | } | ||
| 359 | list->tail->next = NULL; | ||
| 360 | |||
| 361 | /* If we have done only one merge, we're finished. */ | ||
| 362 | if ( nmerges <= 1 ) /* allow for nmerges==0, the empty list case */ | ||
| 363 | return; // list; | ||
| 364 | |||
| 365 | /* Otherwise repeat, merging lists twice the size */ | ||
| 366 | insize *= 2; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | struct dlist * | 280 | struct dlist * |
| 371 | dlist_merge(struct dlist *list1, struct dlist *list2) | 281 | dlist_merge(struct dlist *list1, struct dlist *list2) |
| 372 | { | 282 | { |
