33,311
社区成员
发帖
与我相关
我的任务
分享
struct stu * bond ( struct stu *head1, struct stu *head2 )
{
struct stu *p1, *p2, *q1, *q2, *head;
p1=p2=head1;
q1=q2=head2;
if ( head1==NULL ) head=head2;
else head=head1;
for ( ; q1 != NULL; q2=q1, q1=q1->next )
{
p1=p2=head;
while ( q1->num>p1->num && p1 != NULL )
{
p2=p1;
p1=p1->next;
}
if ( q1->num <= p1->num )
{
if ( p1==head1 ) head=head1;
else p2->next=q1;
q1->next=p1;
}
p2=q2;
}
return head;
}
struct stu * bond ( struct stu *head1, struct stu *head2 )
{
...
if ( q1->num <= p1->num )
{
if ( p1==head1 )
{
head=head1;
}
else // 链表插入应该是这样的,注意看你最外层的for循环逻辑,当你找到一个节点插入后,下一次for循环q1变成什么了
{
p2->next=q1;
q1->next=p1;
}
}
p2=q2;
}
return head;
}
int main ()
{
struct stu * creat ( void );
void print ( struct stu *head );
struct stu * bond ( struct stu *head1, struct stu *head2 );
struct stu *head1, *head2, *head;
printf("Creat the first list please:\n");
head1=creat ();
printf("The list you inputed is :\n");
print ( head1 );
printf("Creat the second list please:\n");
head2=creat ();
printf("The second list you inputed is:\n");
print( head2 );
head=bond ( head1, head2 );
printf ( "Now the list is :\n");
print ( head );
system("pause");
return 0;
}