主题:数据结构初学
#include <iostream>
using namespace std;
typedef struct node{
int data;
struct node *next;
}Lnode,*Linklist;
Linklist init(Linklist A);
Linklist combine(Linklist A,Linklist B);
int main()
{Linklist l1,l2,l3;
l1=init(l1);
l2=init(l2);
l3=combine(l1,l2);
while(l3->next)
{l3=l3->next;
cout<<l3->data;
}
cout<<endl;
delete l1;
delete l2;
delete l3;
return 0;
}
Linklist init(Linklist A)
{Lnode *s;
s=A;
int i,num;
cout<<"enter the number:\n";
cin>>num;
for(i=0;i<num;i++)
{s=s->next;
cin>>s->data;
return A;
}
}
Linklist combine(Linklist A,Linklist B)
{Linklist C;
Lnode *pa,*pb,*rc;
C=A;
rc=C;
pa=A->next;
pb=B->next;
delete B;
while(pa&&pb)
{if(pa->data<pb->data)
{rc->next=pa;
rc=pa;
pa=pa->next;
}
else
{ rc->next=pb;
rc=pb;
pb=pb->next;
}
if(pa)
rc->next=pa;
else
rc->next=pb;
return C;
}
}
我想初始化2个递增的单链表,并把它们合并成一个递增的单链表,哪里出错了?
using namespace std;
typedef struct node{
int data;
struct node *next;
}Lnode,*Linklist;
Linklist init(Linklist A);
Linklist combine(Linklist A,Linklist B);
int main()
{Linklist l1,l2,l3;
l1=init(l1);
l2=init(l2);
l3=combine(l1,l2);
while(l3->next)
{l3=l3->next;
cout<<l3->data;
}
cout<<endl;
delete l1;
delete l2;
delete l3;
return 0;
}
Linklist init(Linklist A)
{Lnode *s;
s=A;
int i,num;
cout<<"enter the number:\n";
cin>>num;
for(i=0;i<num;i++)
{s=s->next;
cin>>s->data;
return A;
}
}
Linklist combine(Linklist A,Linklist B)
{Linklist C;
Lnode *pa,*pb,*rc;
C=A;
rc=C;
pa=A->next;
pb=B->next;
delete B;
while(pa&&pb)
{if(pa->data<pb->data)
{rc->next=pa;
rc=pa;
pa=pa->next;
}
else
{ rc->next=pb;
rc=pb;
pb=pb->next;
}
if(pa)
rc->next=pa;
else
rc->next=pb;
return C;
}
}
我想初始化2个递增的单链表,并把它们合并成一个递增的单链表,哪里出错了?