主题:求助 关于线性表
我写了下面的程序,希望实现两个线性表的连接,可是尽管能够将两个线性表连接在一起,每当程序执行到main函数的最后的‘ } '总是出错。请各位大侠指点。
#include<iostream.h>
#include<stdio.h>
typedef struct node //定义结点类型
{
node(){link=NULL;}
node(char e,node *next)
{
elem=e;link=next;
}
char elem;
node *link;
}node;
typedef class linklist //定义线性表
{
public:
linklist(){first=NULL;}
~linklist(){clear();}
void clear();
void create();
void remove(char &x); //删除线性表中的X元素
private:
void output(ostream &out)const;
node *first;
friend ostream &operator<<(ostream &out,linklist &s);
friend void connectstr(linklist &s1,linklist &s2); //连接两个线性表
}linklist;
void linklist::clear()
{
node *p;
while(first){
p=first->link;
delete first;
first=p;
}
}
void linklist::create()
{
char c;
node *p,*r=NULL;
first=NULL;
while((c=getchar())!='\n'){
p=new node(c,NULL);
if(first!=NULL)r->link=p;
else first=p;
r=p;
}
}
void linklist::remove(char &x)
{
node *p=first,*q;
while(p){
if(p->elem==x){
if(p==first){q=p;first=p=p->link;delete q;}
else {q->link=p->link;delete p;p=q->link;}
}
else {q=p;p=p->link;}
}
}
void linklist::output(ostream &out)const
{
node *p=first;
while(p){
out<<p->elem;
p=p->link;
}
out<<endl;
}
ostream &operator<<(ostream &out,linklist &s)
{
s.output(out);
return out;
}
void connectstr(linklist &s1,linklist &s2)
{
node *r1=s1.first->link,*r3=s1.first;
while(r1)
{
r3->link =r1;
r3=r1;
r1=r1->link;
}
r1=s2.first;
while(r1){
r3->link =r1;
r3=r1;
r1=r1->link;
}
}
void main()
{
linklist link1=linklist();
linklist link2=linklist();
link1.create();
link2.create();
connectstr(link1,link2);
cout<<link1;
} // ------此处出错
例如:我输入
link1:ghj
link2:kfd
它输出:
ghjkfd
然后说程序出错
#include<iostream.h>
#include<stdio.h>
typedef struct node //定义结点类型
{
node(){link=NULL;}
node(char e,node *next)
{
elem=e;link=next;
}
char elem;
node *link;
}node;
typedef class linklist //定义线性表
{
public:
linklist(){first=NULL;}
~linklist(){clear();}
void clear();
void create();
void remove(char &x); //删除线性表中的X元素
private:
void output(ostream &out)const;
node *first;
friend ostream &operator<<(ostream &out,linklist &s);
friend void connectstr(linklist &s1,linklist &s2); //连接两个线性表
}linklist;
void linklist::clear()
{
node *p;
while(first){
p=first->link;
delete first;
first=p;
}
}
void linklist::create()
{
char c;
node *p,*r=NULL;
first=NULL;
while((c=getchar())!='\n'){
p=new node(c,NULL);
if(first!=NULL)r->link=p;
else first=p;
r=p;
}
}
void linklist::remove(char &x)
{
node *p=first,*q;
while(p){
if(p->elem==x){
if(p==first){q=p;first=p=p->link;delete q;}
else {q->link=p->link;delete p;p=q->link;}
}
else {q=p;p=p->link;}
}
}
void linklist::output(ostream &out)const
{
node *p=first;
while(p){
out<<p->elem;
p=p->link;
}
out<<endl;
}
ostream &operator<<(ostream &out,linklist &s)
{
s.output(out);
return out;
}
void connectstr(linklist &s1,linklist &s2)
{
node *r1=s1.first->link,*r3=s1.first;
while(r1)
{
r3->link =r1;
r3=r1;
r1=r1->link;
}
r1=s2.first;
while(r1){
r3->link =r1;
r3=r1;
r1=r1->link;
}
}
void main()
{
linklist link1=linklist();
linklist link2=linklist();
link1.create();
link2.create();
connectstr(link1,link2);
cout<<link1;
} // ------此处出错
例如:我输入
link1:ghj
link2:kfd
它输出:
ghjkfd
然后说程序出错