Skip to content

Instantly share code, notes, and snippets.

@idoleat
Last active March 21, 2024 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idoleat/2483c3f904bfd6953d9a5e1396e9da3f to your computer and use it in GitHub Desktop.
Save idoleat/2483c3f904bfd6953d9a5e1396e9da3f to your computer and use it in GitHub Desktop.
Sort linked list with size at most 16 and limited to numeric element value.
/* Concate lists in arr. No dummy head required for the list */
struct list_head *b16sort(struct list_head *arr[])
{
struct list_head *begin = 0, *end = 0, *tmp = 0;
int size = 0;
for (int i = 0; i < 15; i++) {
if (arr[i] != 0) {
tmp = arr[i]->prev;
if (size != 0) {
end->next = arr[i];
arr[i]->prev = end;
}
end = tmp;
size += 1;
if (size == 1)
begin = arr[i];
}
}
if (size != 0) {
begin->prev = end;
end->next = begin;
}
return begin;
}
void q_vbsort(struct list_head *head)
{
struct list_head *arr[16], *itr, *safe;
memset(arr, 0, sizeof(struct list_head *) * 16);
list_for_each_safe (itr, safe, head) {
INIT_LIST_HEAD(itr);
element_t *entry = list_entry(itr, element_t, list);
int value = atoi(entry->value);
if (arr[value] != 0) {
list_add(itr, arr[value]);
} else {
arr[value] = itr;
}
}
list_add_tail(head, b16sort(arr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment