2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Insert a node into a list.
8 #include <aros/debug.h>
10 /*****************************************************************************
13 #include <exec/lists.h>
14 #include <proto/exec.h>
16 AROS_LH3I(void, Insert
,
19 AROS_LHA(struct List
*, list
, A0
),
20 AROS_LHA(struct Node
*, node
, A1
),
21 AROS_LHA(struct Node
*, pred
, A2
),
24 struct ExecBase
*, SysBase
, 39, Exec
)
27 Insert Node node after pred in list.
30 list - The list to insert the node into
31 node - This node is to be inserted
32 pred - Insert after this node. If this is NULL, node is inserted
33 as the first node (same as AddHead()).
41 struct Node * pred, * node;
43 // Insert Node node as second node in list
44 pred = GetHead (list);
45 Insert (list, node, pred);
50 AddHead(), AddTail(), Enqueue(), RemHead(), Remove(), RemTail(),
55 ******************************************************************************/
62 /* If we have a node to insert behind... */
65 /* Is this the last node in the list ? */
66 if (pred
->ln_Succ
) /* Normal node ? */
69 Our successor is the successor of the node we add ourselves
70 behind and our predecessor is just the node itself.
72 node
->ln_Succ
= pred
->ln_Succ
;
76 We are the predecessor of the successor of our predecessor
77 (What ? blblblb... ;) and of our predecessor itself.
78 Note that here the sequence is quite important since
79 we need ln_Succ in the first expression and change it in
82 pred
->ln_Succ
->ln_Pred
= node
;
88 Add the node at the end of the list.
89 Make the node point to the head of the list. Our
90 predecessor is the previous last node of the list.
92 node
->ln_Succ
= (struct Node
*)&list
->lh_Tail
;
93 node
->ln_Pred
= list
->lh_TailPred
;
96 Now we are the last now. Make the old last node point to us
97 and the pointer to the last node, too.
99 list
->lh_TailPred
->ln_Succ
= node
;
100 list
->lh_TailPred
= node
;
106 add at the top of the list. I do not use AddHead() here but
107 write the code twice for two reasons: 1. The code is small and
108 quite prone to errors and 2. If I would call AddHead(), it
109 would take almost as long to call the function as the execution
110 would take yielding 100% overhead.
112 node
->ln_Succ
= list
->lh_Head
;
113 node
->ln_Pred
= (struct Node
*)&list
->lh_Head
;
114 list
->lh_Head
->ln_Pred
= node
;
115 list
->lh_Head
= node
;