struct list

3 posts / 0 new
Last post
Antikken
Antikken's picture
Offline
Last seen: 11 years 9 months ago
Joined: 2011-05-19 22:37
struct list

Have a hopefully small question. ;)

Is there a very easy way of moving a list entry 1 step up or down the list in the listbrowser? Hope someone understand what i meant :P

OldFart
OldFart's picture
Offline
Last seen: 1 day 4 hours ago
Joined: 2010-11-30 14:09
Hi Antikken Try this: void

Hi Antikken

Try this:

  1. void Trade_PositionInList(struct List *L, struct Node *N, BOOL Up)
  2. {
  3. struct Node *n = IExec->GetHead(L),
  4. *x;
  5.  
  6. /*
  7.   ** Walk trough the list:
  8.   */
  9. while (n)
  10. {
  11. /*
  12.   ** If the node you are looking for
  13.   ** is encountered, then...
  14.   */
  15. if (n == N)
  16. {
  17. /*
  18.   ** ...depending on whether
  19.   ** you want it to move...
  20.   */
  21. if (Up == TRUE)
  22. {
  23. /*
  24.   ** ... 1 place upward...
  25.   */
  26. x = IExec->GetPred(n);
  27.  
  28. if (x)
  29. {
  30. /*
  31.   ** ... you have to move 2
  32.   ** nodes upwards, because...
  33.   */
  34. x = IExec->GetPred(x);
  35.  
  36. if (x)
  37. {
  38. /*
  39.   ** ... after removal of the node...
  40.   */
  41. IExec->Remove(n);
  42. /*
  43.   ** ... it is inserted AFTER
  44.   ** the second upward node:
  45.   */
  46. IExec->Insert(x, n);
  47. }
  48. }
  49. }
  50. else
  51. {
  52. /*
  53.   ** ... 1 place downward,
  54.   ** you have to get hold of
  55.   ** the node following it:
  56.   */
  57. x = IExec->GetSucc(n);
  58.  
  59. if (x)
  60. {
  61. /*
  62.   ** after whicht you remove your node:
  63.   */
  64. IExec->Remove(n);
  65. /*
  66.   ** and place AFTER the one you found:
  67.   */
  68. IExec->Insert(x, n);
  69. }
  70. }
  71.  
  72. /*
  73.   ** after which you can stop walking through the list:
  74.   **
  75.   ** ('modern' programming style requires you to
  76.   ** insert here a 'break' statement, instead of
  77.   ** 'n = NULL'but as my nick
  78.   ** may indicate I am a bit longer around in the world
  79.   ** of programming and ADVICE STRONGLY AGAINST IT!
  80.   ** I.e. only use it in a switch ()-environment.)
  81.   */
  82. n = NULL;
  83. }
  84. else
  85. {
  86. /*
  87.   ** Next node please...
  88.   */
  89. n = IExec->GetSucc(n);
  90. }
  91. }
  92. }

Hope you get the gist of it.

OldFart

salass00
salass00's picture
Offline
Last seen: 1 year 1 month ago
Joined: 2011-02-03 11:27
@OldFart Here is a much

@OldFart

Here is a much simpler version with less bugs:

  1. void MoveNodeUpDown (struct List *list, struct Node *node, BOOL up) {
  2. if (up) {
  3. struct Node *pred = IExec->GetPred(node);
  4. if (pred) {
  5. IExec->Remove(node);
  6. IExec->Insert(list, node, IExec->GetPred(pred));
  7. }
  8. } else {
  9. struct Node *succ = IExec->GetSucc(node);
  10. if (succ) {
  11. IExec->Remove(node);
  12. IExec->Insert(list, node, succ);
  13. }
  14. }
  15. }

Note that IExec->Insert() takes three parameters: the list to add to, the node to add and the node to insert after (which may be NULL causing Insert() to add the node to the beginning of the list).

Log in or register to post comments