src/llist.c: Use stable insertion algorithm
Ensure the relative order of elements with equal keys is maintained when inserting into a sorted list. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
parent
b2645847a0
commit
9c3a8a5a49
@ -162,7 +162,7 @@ llist_add_sorted (llist_t *l, void *data, llist_fn_cmp_t fn_cmp)
|
|||||||
|
|
||||||
if (!l->head)
|
if (!l->head)
|
||||||
l->head = o;
|
l->head = o;
|
||||||
else if (fn_cmp(o->data, l->head->data) <= 0)
|
else if (fn_cmp(o->data, l->head->data) < 0)
|
||||||
{
|
{
|
||||||
o->next = l->head;
|
o->next = l->head;
|
||||||
l->head = o;
|
l->head = o;
|
||||||
@ -170,7 +170,7 @@ llist_add_sorted (llist_t *l, void *data, llist_fn_cmp_t fn_cmp)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
i = l->head;
|
i = l->head;
|
||||||
while (i->next && fn_cmp(o->data, i->next->data) > 0)
|
while (i->next && fn_cmp(o->data, i->next->data) >= 0)
|
||||||
i = i->next;
|
i = i->next;
|
||||||
o->next = i->next;
|
o->next = i->next;
|
||||||
i->next = o;
|
i->next = o;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user