The main difficulty of the ascending and descending function of the data selection in the list of C language list

Source: Internet
Author: User

List of linked lists explained:

The head pointer points to the first node of the linked list, which is the only basis for finding the entire list, and if the head pointer is missing, the entire list will not be found.

The head stores the address of the first node, Head->next stores the address of the second node, and the address of any node p can only be obtained by the next of its previous node.

One-way linked list selection sort diagram:---->[1]---->[3]---->[2] ...---->[n]---->[null] (original linked list)

Head 1->next 3->next 2->next N->next

Select sort (Selection sort) is a simple and intuitive sorting algorithm.

The smallest element is first found in the unordered sequence, placed at the start of the sort sequence, and then continues to look for the smallest element from the remaining unsorted elements and then to the end of the sort sequence. And so on until all elements are sorted.

Animated Demo: http://www.nowamagic.net/librarys/veda/detail/1849

Select sort

Defined structure body

struct student{  char id[// Student study number char name[];  // Student Name struct student *next;  // the next pointer points  to a variable of struct student type

The variables inside are both arrays

So how do you implement an array variable defined (have) in the struct, and the list traverses the struct and sorts it according to the variables in the structure?

The function in which the size of the array is compared and the size of the two strings is compared is: strcmp ()

The assignment function between arrays is strcpy ()

Ascending:

/*************** Function: Ascending rank attendance student returns: pointer to the list header of the list/***************/structStudent *sort_message_order (structstudent* head)//Ascending by ID order{       structStudent *back,*pointer;//the P pointer points to the new node the back pointer points to the tail node of the list    structStudent temp;//defines struct-body student aliases, typedef can also define struct-body aliasesBack=head->Next; Pointer=head->next;//Skip over node point to the next node Nod node no student information     while(Back!=null)//If the tail node is not empty, go through it .    {         while(Pointer->next!=null)//If you point to a newly opened node, you're going to go through it.{pointer=pointer->next;//pointing down a newly opened knot .            if(strcmp (Back->id,pointer->id) >0)//returns a value greater than 0 if the back->id is greater than Pointer->id, and later than the previous{strcpy (temp.id,back-ID); strcpy (Temp.name,back->name);//assigns the tail node value to the temp temp struct variablestrcpy ( back->id,pointer->ID); strcpy ( back->name,pointer->name);//switch the point to the new node and the tail node Exchange locationstrcpy (Pointer-id,temp.id); strcpy (Pointer->name,temp.name);//Assigning a temporary temp struct variable to a pointer to a struct variable}} back=back->next;//refers to the next tail nodePointer=back;//point to tail node    }    returnHead//returns the head node.    }

Descending:

/*************** Function: Descending rank attendance student returns: pointer to the list header of the list/***************/structStudent * SORT_MESSAGE_DESC (structstudent* head)//Descending Descending{    structStudent *back,*pointer;//p always points to the new application node back always points to the tail node of the list    structStudent temp; back=head->Next; Pointer=head->next;//Skip the node, there is no student information in the head node .     while(back!=NULL) {         while(pointer->next!=NULL) {pointer=pointer->Next; if(strcmp (Back->id,pointer->id) <0)//Back->id Less than pointer->id returns a negative number to put the smallest backward descending{strcpy (temp.id,back-ID); strcpy (Temp.name,back->name);//assigns the tail node value to the temp temp struct variablestrcpy ( back->id,pointer->ID); strcpy ( back->name,pointer->name);//point to the new node and the tail node swap locationstrcpy (Pointer-id,temp.id); strcpy (Pointer->name,temp.name);//Assigning a temporary temp struct variable to a pointer to a struct variable}} back=back->next;//refers to the next tail nodePointer=back;//point to tail node    }    returnHead//returns the head node.}

Output Print List contents:

void Print_list (struct student *head) {    struct student* pointer;     Pointer=head->Next; Skip data-Free header node     while (pointer!=NULL)        {              printf ("",  pointer->ID);           printf ("",pointer->name);                          pointer=pointer->next;//refers to the next node        }     }

The main difficulty of the ascending and descending function of the data selection in the list of C language list

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.