本帖最后由 jackz007 于 2019-4-28 10:24 编辑
代码过于复杂,简单修改了一下,请楼主参考#include<stdio.h>
#include<stdlib.h>
typedef struct M{
int num ;
struct M * next ;
} node ;
node * link()
{
node * p1 , * p2 , * head ;
int num ;
head = NULL ;
for(;;) {
scanf("%d" , & num) ;
if(num >= 0) {
if((p2 = (node *) malloc(sizeof(node))) != NULL) {
p2 -> num = num ;
p2 -> next = NULL ;
if(head == NULL) head = p2 ;
else p1 -> next = p2 ;
p1 = p2 ;
} else {
fprintf(stderr , "error: failure of malloc()\n") ;
head = NULL ;
break ;
}
} else {
break ;
}
}
return head ;
}
int main()
{
node * ph , * p , * qh , * q ;
int c ;
ph = link() ;
qh = link() ;
c = 0 ;
for(p = ph ; p != NULL ; p = p -> next) {
for(q = qh ; q != NULL ; q = q -> next) {
if (p -> num == q -> num) {
if(c) printf(" ") ;
printf("%d" , p -> num) ;
c ++ ;
}
}
}
if(! c) printf("no match data found!\n") ;
free(ph) ;
free(qh) ;
}
|