两个链表合并以及排序的问题~~
#include <stdio.h>#include <math.h>
#include <string.h>
#define LEN sizeof(struct lb)
struct lb
{
int no;
char name[15];
struct lb *next;
}a[3]={{6,"chen",&a[1]},{5,"huan",&a[2]},{4,"li",NULL}},b[3]={{3,"ou",&b[1]},{2,"guo",&b[2]},{1,"he",NULL}};
void main()
{
struct lb *px(struct lb *head);
struct lb (*head1)[3],(*head2)[3],*p1,*p2;
head1=&a;
head2=&b;
p1=head1;
p2=head2;
while(p1->next!=NULL) \*合并链表*\
{
p1=p1->next;
}
while(p2->next!=NULL)
{
p1->next=p2;
p1=p1->next;
p2=p2->next;
}
p1=head1;
while(p1!=NULL)
{
printf("%5d%10s\n",p1->no,p1->name);
p1=p1->next;
}
px(head1);
}
struct lb *px(struct lb *head) \*排序*\
{
struct lb *p1,*p2;
int k;
p1=head;
p2=head;
while(p1!=NULL)
{
while(p2!=NULL)
{
if((p1->no)<(p2->no))
{
k=p1->no;
p1->no=p2->no;
p2->no=k;
}
p2=p2->next;
}
p2=head;
p1=p1->next;
}
p1=head;
while(p1!=NULL)
{
printf("%5d%10s\n",p1->no,p1->name);
p1=p1->next;
}
return(head);
}
本来是想用改next指针来实现排序的(用合并后的表状态),这样就不用改号码直接改指针就可以了,但是想了很久没想出好的办法来实现它。只有
暂时先用改号码来实现排序了,求高人指点不改号码只改指针NEXT实现排序!!