题目如下
代码如下
#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; //尾插法 LinkList List_TailInsert(LinkList &L) { ElemType x; L=(LinkList)malloc(sizeof(LNode)); LNode *s,*r=L; printf("请输入单链表各个节点,以9999结束!\n"); scanf("%d",&x); while(x!=9999) { s=(LNode*)malloc(sizeof(LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; } int Length(LinkList L) { LNode *p=L; int count=0; while(p->next!=NULL) { p=p->next; count++; } return count; } void MergeList(LinkList &La,LinkList &Lb){ LNode *r,*pa=La->next,*pb=Lb->next; La->next=NULL; while(pa&&pb) { if(pa->data<pb->data) { r=pa->next; pa->next=La->next; La->next=pa; pa=r; } else { r=pb->next; pb->next=La->next; La->next=pb; pb=r; } } if(pa) pb=pa; while(pb) { r=pb->next; pb->next=La->next; La->next=pb; pb=r; } free(Lb); } int main(){ LinkList L1,L2; LinkList R,S; R=List_TailInsert(L1); S=List_TailInsert(L2); MergeList(R,S); LNode *p=R; while(p->next!=NULL){ p=p->next; printf("->%d",p->data); } }
效果展示