Using Qsort sort list to record __ data structure

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.