注册 登录
编程论坛 C++教室

链表的合并(1)

未未来 发布于 2013-07-11 19:27, 518 次点击
先生成两个链表。在对两个链表进行合并,并从小到大排序
代码一:
程序代码:
#include<iostream>
using namespace std;
struct student{

 int num;

 int score;

 student* next;

 };
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
    p0=p1;
    p1=p1->next;
}
if(ne->num<=p1->num){
    if(head==p1)head=ne;
    else p0->next=ne;
    ne->next=p1;
}else{
    p1->next=ne;
    ne->next=NULL;
}
}


return (head);
}
   
student *hebi(student* head,student *h2)
{
while(h2!=NULL){
student *p1,*ne,*p0;
ne=new student;
ne=h2;
p1=head;   
while((ne->num>p1->num)&&(p1->next!=NULL)){
    p0=p1;
    p1=p1->next;
}
if(ne->num<=p1->num){
    if(head==p1)head=ne;
    else p0->next=ne;
    ne->next=p1;
}else{
    p1->next=ne;
    ne->next=NULL;
}
h2=h2->next;


}
return head;

}   
   
   
        
void print(student * head){
    student *p;
    p=head;
    do{
        cout<<p->num<<" "<<p->score<<endl;
        p=p->next;
    }while(p!=NULL);
}            
int main(){
    int N,M;
    cin>>N>>M;
    student *head1,*head2;
    head1=creat(N);
   head2=creat(M);
hebi(head1,head2);
print(head1);

return 0;
   
   
}


自己感觉这样也是对的,但是输入数据之后,无法达到合并并排序的效果,
代码二
程序代码:
#include<iostream>
using namespace std;
struct student{

 int num;

 int score;

 student* next;

 };
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
    p0=p1;
    p1=p1->next;
}
if(ne->num<=p1->num){
    if(head==p1)head=ne;
    else p0->next=ne;
    ne->next=p1;
}else{
    p1->next=ne;
    ne->next=NULL;
}
}


return (head);
}
   
student *hebi(student* head,student *h2)
{student *p1,*ne,*p0;
while(h2!=NULL){

ne=new student;
ne->num=h2->num;
ne->score=h2->score;
h2=h2->next;
p1=head;   
while((ne->num>p1->num)&&(p1->next!=NULL)){
    p0=p1;
    p1=p1->next;
}
if(ne->num<=p1->num){
    if(head==p1)head=ne;
    else p0->next=ne;
    ne->next=p1;
}else{
    p1->next=ne;
    ne->next=NULL;
}
}
return (head);

}   
   
   
        
void print(student * head){
    student *p;
    p=head;
    do{
        cout<<p->num<<" "<<p->score<<endl;
        p=p->next;
    }while(p!=NULL);
}            
int main(){
    int N,M;
    cin>>N>>M;
    student *head1,*head2;
    head1=creat(N);
   head2=creat(M);
   cout<<endl;
   print(head1);
   print(head2);
   cout<<endl;
hebi(head1,head2);
print(head1);

return 0;
   
   
}

我把指针的数据先提出来,然后再插入,但在测试数据的时候
输入
2 3
2 4
1 2
3 2
4 1
5 2

输出
1 2
2 4
3 2
4 1
5 2

但输入
2 3
4 3
3 2
2 5
1 3
3 2

输出
3 2
4 3
以及其他数据都无法输出正确结果;
有的程序还会崩溃;
有谁能帮我看看问题出在哪;
8 回复
#2
peach54602013-07-11 20:56
好长啊...
#3
未未来2013-07-11 21:51
回复 2楼 peach5460
作为程序员!怎么可以说程序长呢!
这几天打算自己写课设,问题会很多的
#4
yuccn2013-07-11 22:58
回复 3楼 未未来
缩进~
#5
说1234543212013-07-12 00:07
头晕。。。
#6
peach54602013-07-12 14:08
楼主有没有兴趣一起用GIT玩c++撒
我看oschina上免费提供Git库,我们可以新建一个项目一起玩嘛
#7
未未来2013-07-12 16:02
有兴趣,但是怕玩不动,,摸不进去门,
#8
peach54602013-07-12 17:05
有自己的电脑,有网,有兴趣就可以玩嘛
我晚上回去建一个,链接发你QQ
其实就是哥GITHUB的国内版
git这东西还蛮好玩的

[ 本帖最后由 peach5460 于 2013-7-12 17:11 编辑 ]
#9
未未来2013-07-12 19:29
回复 8楼 peach5460
好的。
1