求助

来源:互联网 发布:淘宝宣传视频收费 编辑:程序博客网 时间:2024/05/29 17:21

我是一名c++初学者,这学期学习数据结构,有一个上机题是创建两个链表,并合并为一个,我创建的时候默认为升序排列。虽然编译通过了,但是无法运行处结果,求好心的高手帮忙指点迷津,在下感激不尽。下面是我的源程序。

#include<iostream>
using namespace std;
struct NodeTp{int data;struct NodeTp * next;};
NodeTp * Create()
{int x;
NodeTp * h=new NodeTp;
NodeTp * p;
NodeTp * last=h;
while(1)
{
 cin>>x;
if(x==0)break;
p=new NodeTp;
p->data=x;
last->next=p;
last=p;
}
last->next=NULL;
p=h->next;
while(p)
{cout<<p->data<<endl;
p=p->next;
}
return h;
}
void main()
{NodeTp * La;
NodeTp * Lb;
NodeTp * Lc,*last,*p;
Lc=new NodeTp;
last=Lc;
La=Create();
Lb=Create();
while(1)
{if(La->data==0||Lb->data==0)break;
p=new NodeTp;
if(La->data<Lb->data)
{p->data=La->data;
La=La->next;
last->next=p;
last=p;}
else
{p->data=Lb->data;Lb=Lb->next;
last->next=p;
last=p;}
}
while(La)
{p=new NodeTp;
 p->data=La->data;
 last->next=p;
 last=p;
 La=La->next;
}
while(Lb)
{p=new NodeTp;
p->data=Lb->data;
last->next=p;
last=p;
Lb=Lb->next;
}
p->next=NULL;
cout<<Lc->data<<endl;
}

原创粉丝点击