sortlist.bdy 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Module: Sort Linked Lists
  3. Author: [email protected] (Dick Grune @ Vrije Universiteit, Amsterdam)
  4. Version: Tue Sep 17 17:32:33 1991
  5. Description:
  6. This is the implementation part of a generic routine that sorts
  7. linked lists.
  8. Instantiation:
  9. See sortlist.spc
  10. */
  11. #ifndef _SORT_EXTERN_DEFINED
  12. static
  13. #endif
  14. void
  15. SORT_NAME(struct SORT_STRUCT **lh) {
  16. /* I've never known that sorting a linked list was this
  17. complicated; what am I missing?
  18. */
  19. register struct SORT_STRUCT **listhook = lh;
  20. while (*listhook) {
  21. /* 0. the list is not empty -> there must be a smallest one */
  22. register struct SORT_STRUCT **hsmall;
  23. /* 1. find (the pointer to) the smallest element */
  24. {
  25. register struct SORT_STRUCT **hook = listhook;
  26. /* assume initially that first element is smallest */
  27. hsmall = hook;
  28. while (*hook) {
  29. if (SORT_BEFORE(*hook, *hsmall)) {
  30. /* revise opinion */
  31. hsmall = hook;
  32. }
  33. hook = &(*hook)->SORT_NEXT;
  34. }
  35. }
  36. /* 2. move the smallest element to front */
  37. {
  38. register struct SORT_STRUCT *smallest = *hsmall;
  39. /* remove it from the chain */
  40. *hsmall = smallest->SORT_NEXT;
  41. /* and insert it before the first element */
  42. smallest->SORT_NEXT = *listhook;
  43. *listhook = smallest;
  44. }
  45. /* 3. skip over smallest element */
  46. listhook = &(*listhook)->SORT_NEXT;
  47. }
  48. }