A Method for merging linked lists to implement Yan Weimin's Data Structure

Source: Internet
Author: User

I personally think this is easier to understand by referring to others' and modifying the Merged Code.

# Include <iostream>
Using namespace STD;
Typedef int elemtype;

// Combine two incremental linked lists into an incremental linked list.
Typedef struct lnode
{
Elemtype data;
Struct lnode * next;
}
Lnode;
Typedef lnode * linklist;
Void creatlist (linklist & L, int N)
{
Linklist p, q;
L = new lnode; // the header node is empty.
L-> next = NULL;
Q = L;
Cout <"Please input the elements of the linked list from small to large :";
For (INT I = 1; I <= N; I ++)
{
P = new lnode;
Cin> P-> data;
Q-> next = P;
P-> next = NULL;
Q = P;
// P-> next = Q-> next;
// Q-> next = P;
// Q = Q-> next;

}
Cout <"the incremental ordered linked list created is :";
P = L-> next;
For (Int J = 1; j <= N; j ++)
{
Cout <p-> data <"";
P = p-> next;
}
Cout <Endl;
}
Void creatc (linklist & A, linklist & B, linklist & C, int N)
{
Linklist Pa, Pb, pre = NULL/* Previous node of the C node */, Q/* T */;
Pa = A-> next;
PB = B-> next;
Free ();

C = B;
C-> next = NULL;
While (PA & Pb)
{
If (Pa-> data <= Pb-> data)
{
Q = PA;
Pa = pa-> next;
}
Else
{
Q = Pb;
PB = Pb-> next;
}
Q-> next = C-> next;
C-> next = Q;
}
While (PA)
{
Q = PA; Pa = pa-> next;
Q-> next = C-> next;
C-> next = Q;
}
While (PB)
{
Q = Pb; Pb = Pb-> next;
Q-> next = C-> next;
C-> next = Q;
}
Cout <"the combined descending ordered linked list is :";
Pa = C;
For (Int J = 1; j <= N; j ++)
{
Cout <pa-> next-> data <"";
Pa = pa-> next;
}
Cout <Endl;
// Getchar ();
}
Void main ()
{
Linklist A, B, C;
Int n, m, K;
Cout <"Enter the length of the chain table :";
Cin> N;
Creatlist (A, N );
Cout <"Enter the length of the chain table *** B :";
Cin> m;
Creatlist (B, M );
K = m + N;
Creatc (a, B, c, k );

}

A Method for merging linked lists to implement Yan Weimin's Data Structure

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.