Combine Two ordered loops linearly into a cyclical Table Algorithm (C language)

Source: Internet
Author: User

Void merge (slnodetype * headA, slnodestype * headB)
{
Slnodetype * tempA, * tempB, * temp;
TempA = headA;
TempB = headB;
// Loop the elements in each A and put the elements in B that are smaller than the elements in A before them.
While (tempA-> next! = HeadA)
{
Temp = tempA;

// Loop the elements in B and place the elements in B smaller than the elements in A before the elements in
While (tempA-> next-> data> tempB-> next-> data)
{
Temp-> next = tempB-> next;
TempB-> next = tempA-> next;
Temp = tempB-next;
// Elements in the next B;
TempB = tempB-> next;
If (tempB-> next = headB & tempA-next = headA)
{
// When the end of a or B is reached, the combination is completed and the header node of B is released.
Free (headB)
Return;
}
}
// ELEMENTS IN THE NEXT
TempA = temp-> next;

}
// Find the tail element of B
While (tempB-> next! = HeadB)
{
TempB = tempB-> next;
}
// Add the elements at the end of B to the front of A, release the header node of B, and complete the combination.
TempB-> next = headA;
Free (headB );

}

Typedef struct slnode
{
Elemtype data;
Struct slnode * next;

} Slnodetype;

/* Think that a good algorithm is very important to improve the program performance. Please give me more advice for the first time */

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.