To sort the list, remember that the last time you sorted the structure, you created the structure array so that the space is contiguous.
But the list is discrete and cannot be directly qsort.
So allocate a temporary space to store the address, then sort the address and re-establish it.
This is recorded here because when you write the CMP function, you find that the formal parameters are somewhat exquisite, so they are placed here for easy viewing.
//--------------------------------struct part
typedef struct LINKEDLISTNODE//Linked list node
{
int data;
Linkedlistnode* Next;
node;
struct LinkInfo//to record the link header node, the last node, the number of elements
{
int listlen;
LinkedListNode *head,*tail;
};
//--------------------------qsort part
Cmpincrease (const void *left,const void *right)
//node **p= (node * *) left;
Node *p=* (node * *) LEFT;//CMP functions two ways of accessing
node **q= right;
if (p->data> (*q)->data) return
1;
else return
p->data== (*q)->data?0:-1;
}
int cmpdecrease (const void *left,const void *right)
{
//node **p= (node * *) left;
Node *p=* (node * *) LEFT;//CMP functions two ways of accessing
node **q= right;
if (p->data< (*q)->data) return
1;
else return
p->data== (*q)->data?0:-1;
}
void Linksortq (linkinfo *l,bool Increase)
{
if (l->listlen<=1) return
;
node* (*all) =new node* [l->listlen];
all[0]=l->head->next;
for (int i=1;i<l->listlen;i++)
all[i]= (all[i-1])->next;
Qsort (node *) (All), l->listlen,sizeof (node *), increase?cmpincrease:cmpdecrease);
Node *start=l->head;
for (int i=0;i<l->listlen;i++)
{
start->next=all[i];
start=start->next;
}
l->tail=all[l->listlen-1];
Delete (all);
}