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