xxxxxxxxxx
void insertion_sort_list(listint_t **list)
{
listint_t *sorted, *curr, *next, *aux, *temp;
sorted = NULL;
curr = *list;
while (curr != NULL)
{
next = curr->next;
if (sorted == NULL || sorted->n > curr->n)
{
curr->next = sorted;
sorted = curr;
} else
{
aux = sorted;
while (aux->next != NULL && aux->next->n <= curr->n)
aux = aux->next;
if (aux->next == NULL)
{
aux->next = curr;
curr->prev = aux;
curr->next = NULL;
} else
{
temp = aux->next;
aux->next = curr;
curr->prev = aux;
curr->next = temp;
temp->prev = curr;
}
}
curr = next;
}
*list = sorted;
}
xxxxxxxxxx
void sort(node **q)
{
int n;
node *cur;
cur=*q;
if (cur->next==NULL)
return;
node *ptr,*tmp;
cur=cur->next;
while(cur!=NULL)
{
n=0;
ptr=cur;
tmp=cur->prev;
cur=cur->next;
while (tmp!=NULL && tmp->data>ptr->data)
{
n++;
tmp=tmp->prev;
}
if (n)
{
ptr->prev->next=ptr->next;
if (ptr->next!=NULL)
ptr->next->prev=ptr->prev;
if (tmp==NULL)
{
tmp=*q;
ptr->prev=NULL;
ptr->next=tmp;
ptr->next->prev=ptr;
*q=ptr;
}
else
{
tmp=tmp->next;
tmp->prev->next=ptr;
ptr->prev=tmp->prev;
tmp->prev=ptr;
ptr->next=tmp;
}
}
}
}