#include
using namespace std;
typedef int ElemType ;
struct LNode{
ElemType data;
LNode* next;
};
void createlistR(LNode* &l, int a[], int n){//尾插法
LNode* r = (LNode*)malloc(sizeof(LNode));//创建头结点
r->next = NULL;
l = r;
for(int i = 0;i<n;i++)
{
LNode* t = (LNode*)malloc(sizeof(LNode));//创建插入节点
t->data = a[i];
t->next = NULL;
r->next = t;//插入
r = t;
}
r->next = NULL;//别漏了
}
void mergeAsc(LNode* A, LNode* B, LNode* &C){
LNode* p = A->next;//p跟踪A的最小值节点
LNode* q = B->next;//q跟踪A的最小值节点
C = A;//C获取头节点
LNode* r = A;//尾插法使用节点
r->next = NULL;
while(p!=NULL&&q!=NULL){//直至一方链表耗尽
if(p->data < q->data){//小的插入
r->next = p;
r = r->next;
p = p->next;
}
else{
r->next = q;
r = r->next;
q = q->next;
}
}
if(p!=NULL){//接上剩余链表
r->next = p;
}
if(q!=NULL){
r->next = q;
}
}
void mergeDesc(LNode* A, LNode* B, LNode* &C){//有意思的是,头插法即实现逆序排列
LNode* p = A->next;//p跟踪A的最小值节点
LNode* q = B->next;//q跟踪A的最小值节点
C = A;//C获取头节点
LNode* r = A;//头插法暂存节点
r->next = NULL;
while(p!=NULL&&q!=NULL){//直至一方链表耗尽
if(p->data < q->data){//小的插入
r = p;
p = p->next;
r->next = C->next;
C->next = r;
}
else{
r = q;
q = q->next;
r->next = C->next;
C->next = r;
}
}
while(p!=NULL){//接上剩余链表
r = p;
p = p->next;
r->next = C->next;
C->next = r;
}
while(q!=NULL){
r = q;
q = q->next;
r->next = C->next;
C->next = r;
}
}
void main(){
int a[5] = {1,3,5,7,9};
int aLength = sizeof(a)/sizeof(a[0]);
LNode* l;
createlistR(l,a,aLength);
int b[5] = {2,4,26,28,210};
int bLength = sizeof(b)/sizeof(b[0]);
LNode* n;
createlistR(n,b,bLength);
LNode* c;
mergeDesc(l,n,c);
cout<<' ';
}