为什么我写的就不行呢?
求两个链表的差
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define SIZE sizeof(snode)
typedef struct student
{
int num;
struct student *next;
}snode;
snode *create1();
snode *create2();
//创建链表
void print(snode *head);
//打印链表
snode *difference(snode *A,snode *B);
void main(void)
{
snode *head2,*head1,*head3;
head1=create1();
head2=create2();
printf("\n链表1为:\n");
print(head1);
printf("\n链表2为:\n");
print(head2);
printf("\n");
printf("两链表的差为:\n");
head3=difference(head1,head2);
print(head3);
printf("\n");
}
snode *create1()
{
snode *head,*p,*q;
int num1;
head=(snode *)malloc(SIZE);
head->next=NULL;
p=head;
FILE *fp;
fp=fopen("D:\\qq.txt","r");
while(!feof(fp))
{
if((fscanf(fp,"%d",&num1))==-1)
break;
q=(snode *)malloc(SIZE);
q->num=num1;
q->next=NULL;
p->next=q;
p=q;
}
return (head);
}
snode *create2()
{
snode *head,*p,*q;
int num1;
head=(snode *)malloc(SIZE);
head->next=NULL;
p=head;
FILE *fp;
fp=fopen("D:\\aa.txt","r");
while(!feof(fp))
{
if((fscanf(fp,"%d",&num1))==-1)
break;
q=(snode *)malloc(SIZE);
q->num=num1;
q->next=NULL;
p->next=q;
p=q;
}
return (head);
}
void print(snode *head)
{
snode *p;
p=head->next;
//带头结点
while(p!=NULL)
{
printf("%6d",p->num);
p=p->next;
}
}
snode *difference(snode *A,snode *B)
//
依次查看A中的节点中是否有值与B中相同,若相同, 进入链表C中
{
snode *pa=A->next,*pb,*s,*r,*c;
c=(snode *)malloc(SIZE);
c->next=NULL;
r=c;
while(pa!=NULL)
{
pb=B->next;
while((pb->num!=pa->num )&& (pb!=NULL))
{
pb=pb->next;
}
if(pb==NULL)
//
有值与之相同
{
s=(snode *)malloc(SIZE);
s->num=pa->num;s->next=NULL;
r->next=s;
r=s;
}
pa=pa->next;
}
return(c);
}