已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
我的代码
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int Data;
struct Node *Next;
}node,*List;
List read();
int main(){
List L1,L2,L3,h;
L1=read();
L2=read();
L3=h=(List)malloc(sizeof(struct Node));
while(L1!=NULL){
L3->Next=(List)malloc(sizeof(struct Node));
while(L2!=NULL){
if(L1->Data==L2->Data){
L3->Next->Data=L1->Data;
L3=L3->Next;
}
L2=L2->Next;
}
L1=L1->Next;
}
L3->Next=NULL;
L3=h->Next;
free(h);
if(L3==NULL){
printf("NULL");
}else{
while(L3!=NULL){
printf("%d",L3->Data);
L3=L3->Next;
}
}
return 0;
}
List read(){
List L,h;
int n;
L=h=(List)malloc(sizeof(struct Node));
scanf("%d",&n);
while(n!=-1){
L->Next=(List)malloc(sizeof(struct Node));
L->Next->Data=n;
L=L->Next;
scanf("%d",&n);
}
L->Next=NULL;
L=h->Next;
free(h);
h=L;
return h;
不是很能理解错哪儿了,有人可以具体解惑一下吗?要怎么改呢?希望可以讲的简单易懂一点,先谢谢了(^~^)