#include<stdio.h>#include<stdlib.h>typedef struct LNode{?int data;?struct LNode *next;}LNode,*LinkList;int main(){?void MergeList(LinkList La,LinkList Lb,LinkList Lc);?LNode a,b;?LinkList p,q,w,s,head1,head2;?int alength=0,blength=0,i,j;?p=&a;?q=&b;??? w=NULL;?head1=p;?head2=q;??? //p=(LinkList)malloc(sizeof(LNode));??printf("請輸入第一個線性表的長度:");?scanf("%d",&alength);?printf("請輸入第一個線性表的數據:");?for(i=1; i<=alength;i++)?{??s=(LinkList)malloc(sizeof(LNode));??p->next=s;??p=p->next;??scanf("%d",&p->data);?}?p->next=NULL;?printf("請輸入第二個線性表的長度:");?scanf("%d",&blength);?printf("請輸入第二個線性表的數據:");?for(j=1; j<=blength;j++)?{??s=(LinkList)malloc(sizeof(LNode));??q->next=s;??q=q->next;??scanf("%d",&q->data);?}?q->next=NULL;??? MergeList(head1,head2,w);?while(head1->next!=NULL)?{??printf("%d",head1->next->data);??head1=head1->next;?}?return 0;}void MergeList(LinkList La,LinkList Lb,LinkList Lc){?LinkList pa,pb,pc;?pa=La->next;pb=Lb->next;?Lc=pc=La;?while(pa&&pb)?{??if(pa->data<=pb->data)??{???pc->next=pa;???pc=pa;???pa=pa->next;??}??else??{???pc->next=pb;???pc=pb;???pb=pb->next;??}?}?pc->next=pa?pa:pb;?free(Lb);?}
- 1 回答
- 0 關注
- 1351 瀏覽
添加回答
舉報
0/150
提交
取消