求大神帮忙看看我的连接函数connectList()哪里有错
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct Lnode
{
int data;
Lnode *next;
};
class LinkList
{
//private:
//Lnode *head;
public:
Lnode *head;
void CreatList()
{
Lnode *s;
Lnode *r;
head=new Lnode;
head->data=0;
head->next=NULL;
r=head;
cout<<"Please input the data of your linkList and when you input zero the pro end."<<endl;
int n;
int i=0;
cin>>n;
while(n!=0)
{
s=new Lnode;
s->data=n;
r->next=s;
r=s;
cin>>n;
}
r->next=NULL;
}
void dispList()
{
Lnode *p=head->next;
while(p!=NULL)
{
cout<<setw(3)<<p->data;
p=p->next;
}
cout<<endl;
}
int LocateList(int i)
{
int e=0;
int j=0;
Lnode *p=head;
while(j<i&&p!=NULL)
{
p=p->next;
j++;
}
if(p==NULL)
return 0;
else
{
e=p->data;
cout<<e<<endl;
return 1;
}
}
int length()
{
Lnode *p=head;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
};
LinkList connectList(LinkList a,LinkList b)//总是中断在这
{
Lnode *p=a.head->next;
Lnode *q=b.head->next;
while(p!=NULL)
p=p->next;
/*for (int i=0;i<a.length();i++)
{
p=p->next;
}*/
p->next=q;
free(b.head);
return a;
}
int main()
{
LinkList h,a,b;
/*h.CreatList();
h.dispList();
h.LocateList(1);
h.length();*/
a.CreatList();
b.CreatList();
connectList(a,b);
a.dispList();
return 0;
}