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

请高手帮帮忙!谢谢了啊
#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; }
else
while( ( 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);
}

慕丝7291255
浏览 205回答 3
3回答

慕码人2483693

这个改过了,保证好用。#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;}

慕妹3146593

如果我没有看错的话,你应该是尾巴少掉了一点。我用的是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;&nbsp;

喵喔喔

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 = nullhead2=NULL;在下去 , 你又 if ( p0 -> num <= p1 -> num )p1 本来= null, 何来的num.
打开App,查看更多内容
随时随地看视频慕课网APP