为了账号安全,请及时绑定邮箱和手机立即绑定

关于C++新建新链表,对已有链表进行排序,连接翻译都没错,可是运行却有问题?为什么

关于C++新建新链表,对已有链表进行排序,连接翻译都没错,可是运行却有问题?为什么

慕丝7291255 2022-04-22 15:15:01
请高手帮帮忙!谢谢了啊#include<iostream.h>struct student{ int num; //学号int score;student *next;};student *creat( ) //创建链表{ student *head = NULL, *p1, *p2;p1 = new student;cin >> p1->num >> p1->score ;head = p1;while( p1 -> num != 0 ){ p2 = p1;p1 = new student;cin >> p1->num >> p1->score ;p2 -> next = p1;}p2 -> next = NULL;return ( head );}void print ( student *head ){ student *p;p = head;if ( head != NULL )do{ cout << p->num <<' '<< p->score<< endl;p = p->next; //后移一个结点} while ( p!=NULL );else cout<<"head is NULL !";}student *insert( student *head, student *stud)//插入链表{ student *p0,*p1,*p2;p1= head; p0 = stud;if ( head == NULL ) //空链表处理,插入头结点{ head = p0; p0 -> next = NULL; }elsewhile( ( p0 -> num > p1 -> num) && (p1->next!= NULL ) ){ p2 = p1; p1 = p1 -> next; } //查找插入位置if ( p0 -> num <= p1 -> num )if ( head == p1 ) { head = p0, p0 -> next = p1;}else { p2 -> next = p0; p0 -> next = p1; }else{ p1 -> next = p0; p0 -> next = NULL; }return ( head );}student *creat2( student *head, student *head2) //重建新链表,按照num排序{ student *stu,*p1,*p2;p1=head;head2=NULL;do{ stu=new student;(*stu).num=(*p1).num,(*stu).score=(*p1).score;insert( head2, stu);p2 = p1;p1 = p1 -> next;}while(p1!=NULL);return head2;}void main(){ student *head,*head2;head=creat();print(head);head=creat2(head,head2);print(head2);}
查看完整描述

3 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

这个改过了,保证好用。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(int,string);
int No;
string name;
Person *next; /*指向下一个元素的指针*/
};
Person::Person(int inputNumber,string inputName) /*构造函数*/
{
next=NULL;
No=inputNumber;
name=inputName;
}
void create(Person **phead,Person **ptail,int inputNumber,string inputName)
/*如果phead指针不为空,说明链表已存在,退出
如果为空,则创建第一个元素,
将表首表尾指针同时指向该元素
*/
{
if((*phead)!=NULL)
{
cout<<"链表已存在,无法重新建立,退出"<<endl;
return;
}
else
{
(*phead)=new Person(inputNumber,inputName);
(*ptail)=(*phead);
cout<<"表创建成功,请继续输入!"<<endl;
}
}
void add(Person **ptail,int inputNumber,string inputName) /*向链表尾添加元素,并将表尾指针指向该元素*/
{
(*ptail)->next=new Person(inputNumber,inputName);
(*ptail)=(*ptail)->next;
}
void display(Person **phead) /*由首元素开始逐个元素显示,直到尾元素*/
{
while((*phead)!=NULL)
{
cout<<"No:"<<(*phead)->No<<" "<<"Name:"<<(*phead)->name<<endl;
(*phead)=(*phead)->next;
}
}
int main()
{
Person *phead=NULL;
Person *ptail=NULL;
int inputNumber;
string inputName;
cout<<"请输入第一个人的编号及姓名:"<<endl;
cin>>inputNumber>>inputName;
create(&phead,&ptail,inputNumber,inputName);
bool flag=true;
while(flag)
{
cin>>inputNumber;
if(inputNumber==0)
{
flag=false;
cout<<"输入为0,终止输入"<<endl;
}
else
{
cin>>inputName;
add(&ptail,inputNumber,inputName);
}
}
display(&phead);
cout<<"按任意数字键退出..."<<endl;
cin>>inputName;
return 0;
}



查看完整回答
反对 回复 2022-04-24
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

如果我没有看错的话,你应该是尾巴少掉了一点。
我用的是win-tc,就没有对你的程序调试。
我看了下,可能是下面这段程序出了问题
while( p1 -> num != 0 )
{ p2 = p1;
p1 = new student;
cin >> p1->num >> p1->score ;
p2 -> next = p1;
}
p2 -> next = NULL;
return ( head );
}
或许,你可以将p2 -> next = NULL;写成p1 -> next = NULL;

 


查看完整回答
反对 回复 2022-04-24
?
喵喔喔

TA贡献1735条经验 获得超5个赞

1 ,head=creat2(head,head2);
此处的head 2 没有赋值 head2 = nullptr;
2 在create2里面
(*stu).num=(*p1).num,(*stu).score=(*p1).score;
insert( head2, stu);
p2 = p1;
此处的head2 = null,
而在insert() 里面
p1=head;也就是说p1 = null
head2=NULL;
在下去 , 你又 if ( p0 -> num <= p1 -> num )
p1 本来= null, 何来的num.



查看完整回答
反对 回复 2022-04-24
  • 3 回答
  • 0 关注
  • 204 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号