请高手帮帮忙!谢谢了啊
#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);
}
慕码人2483693
慕妹3146593
喵喔喔