In the C-language linked list, arrays are used to select and sort data. The ascending and descending functions are mainly difficult.

Source: Internet
Author: User

In the C-language linked list, arrays are used to select and sort data. The ascending and descending functions are mainly difficult.

 

Sort linked lists:

The head Pointer Points to the head node of the linked list, which is the only basis for finding the entire linked list. If the head pointer is lost, the entire linked list cannot be found.

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

 

Figure ----> [1] ----> [3] ----> [2]... ----> [n] ----> [NULL] (original linked list)

Head 1-> next 3-> next 2-> next n-> next

Selection sort is a simple and intuitive sorting algorithm.

First, find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence. Then, find the minimum element from the remaining unordered elements and put it at the end of the sorting sequence. And so on until all elements are sorted.

Animation demo: http://www.nowamagic.net/librarys/veda/detail/1849

 

Select sort

Struct defined

Struct student
{
Char ID [11]; // student id
Char name [20]. // student name

Struct student * next; //next pointer to variable of struct student type
} stu.

 

All the variables are arrays.

So how can we implement the definition (with) array variables in the struct, traverse the struct through the linked list, and sort the struct by the variables in the struct?

The strcmp () function is used to compare the size of an array and compare the size of two strings ()

The value assignment function between arrays is strcpy ()

 

 

Ascending Order:

/ * * * * * * * * * * * * * * *

Function functions:
Students present in ascending order
Return: pointer to the list header of the linked table

/ * * * * * * * * * * * * * * * /


Struct student* sort_message_order(struct student* head) // in ascending order by ID

{
Back struct student * and * pointer; //p pointer to the new node the back pointer to the tail node of the list
Struct student temp. // defines the struct student alias. Typedef can also define struct alias
The Back = head - > next;
Pointer = head - > next; // there is no student information in the head node of the node pointing to the next node
While (the Back! =NULL) // if the tail node is not NULL, it keeps traversing
{
While (pointer - > next! =NULL) // if pointing to a newly created node is not NULL, it keeps traversing
{
Pointer = pointer - > next; // points to the next new node
If (STRCMP (back->ID)>0) The back is bigger than the front and the back
{
Strcpy (temp. ID, Back - > ID);
Strcpy (temp. The name, the Back - > name); // assigns the tail node value to the temp structure variable


Strcpy (Back - > ID, pointer - > ID);
Strcpy (Back - > name, pointer - > name); // switches the new node to the tail node


Strcpy (pointer - > ID, temp. ID);
Strcpy (pointer - > name, temp. Name); // assigns the temporary temp structure variable to the pointed structure variable

}
}
The Back = Back - > next; // points to the next tail node
Pointer = Back; // points to the tail node
}
Return the head; // returns the header node

}

 

 

Descending order:

/ * * * * * * * * * * * * * * *

Function functions:
Students are listed in descending order
Return: pointer to the list header of the linked table

/ * * * * * * * * * * * * * * * /

Struct student* sort_message_Desc(struct student* head)//Descending order
{
Back struct student * and * pointer; //p always points to the newly applied node. Back always points to the tail node of the list
Struct student temp.
The Back = head - > next;
Pointer = head - > next; // skip the overhead node, there is no student information in the head node
While (the Back! = NULL)
{
While (pointer - > next! = NULL)
{
Pointer = pointer - > next;

If (STRCMP (back->ID)<0
{
Strcpy (temp. ID, Back - > ID);
Strcpy (temp. The name, the Back - > name); // assigns the tail node value to the temp structure variable

Strcpy (Back - > ID, pointer - > ID);
Strcpy (Back - > name, pointer - > name); // points to a new node that switches places with the tail node

Strcpy (pointer - > ID, temp. ID);
Strcpy (pointer - > name, temp. Name); // assigns the temporary temp structure variable to the pointed structure variable
}
}
The Back = Back - > next; // points to the next tail node
Pointer = Back; // points to the tail node
}
Return the head; // returns the header node
}

 

 

Output the printed content of the linked list:

Void Print_List (struct student * head)
{
Struct student * pointer;
Pointer = head - > next; // skip the header of no data
While (pointer! = NULL)
{
Printf (" ", pointer - > ID);
Printf (" ", pointer - > name);

Pointer = pointer - > next; // points to the next node
}
} 

 

 

 


Related Article

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.