编泽成功但不能运行
输入两个单链表A,B用新链表C输出他们的集合
程序编出来了编译成功但运行时却出现错误
错在哪里呢?
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0 //宏定义NULL为0
#define SIZ sizeof(struct stu) //宏定义 SIZ为sizeof(struct stu)
struct stu //结构体
{
int num;
stu *next;
};
stu *r,*s; //定义全局指针变量
void main()
{
stu *Ha=NULL;stu *Hb=NULL;stu *Hc=NULL; //定义头指针
int x;
cout<<"in put A:"<<endl; //建立单链表A
cin>>x;
r=Ha;
while(x!=NULL)
{
s=(stu*) malloc(SIZ);
s->num=x;
if(Ha==NULL)
Ha->next=s;
else
r->next=s;
r=s;
cin>>x;
}
if(r!=NULL)
r->next=NULL;
cout<<"in put B:"<<endl; //建立单链表B
cin>>x;
r=Hb;
while(x!=NULL)
{
s=(stu*)malloc(SIZ);
s->num=x;
if(Hb==NULL)
Hb->next=s;
else
r->next=s;
r=s;
cin>>x;
}
if(r!=NULL)
r->next=NULL;
stu *p,*q; //定义指针p,q
r=Hc;p=Ha->next;q=Hb->next;
while(p!=NULL&&q!=NULL) //生成单链表C
{
while(p->num!=q->num)
{
if(Hc=NULL)
{
s=(stu*)malloc(SIZ);
s->num=p->num;
Hc->next=s;
}
else
{
s=(stu*)malloc(SIZ);
s->num=p->num;
r->next=s;
}
r=s;
}
q=q->next;
p=p->next;
}
r=Hc->next;
while(r!=NULL) //输出C
{
cout<<r->num<<" ";
r=r->next;
}
cout<<endl;
free(s);
}