9.10にはa,bの2つのチェーンテーブルがあり、各チェーンテーブルのノードには学号、成績が含まれている.2つのチェーンテーブルを結合し、学号昇順に並べることを要求する.
1633 ワード
//C ( )
// :
// :9.10
// : a,b , 、 。 ,
#include
#include
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
struct student *create() //
{
struct student *head=NULL,*p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(LEN);
printf("input num,score(if num=0 stop):
");
scanf("%ld %f",&p1->num,&p1->score);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld %f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head) //
{
struct student *p;
p=head;
printf("output list:
");
while(p!=NULL)
{
printf("%ld\t%.2f
",p->num,p->score);
p=p->next;
}
printf("
");
}
int main()
{
struct student *head_a,*head_b,*p1,*p2,*p3;
long num0;
float score0;
printf("input list a:
");
head_a=create();
printf("input list b:
");
head_b=create();
p1=head_a;
while(p1->next!=NULL)
p1=p1->next;
p1->next=head_b; // a,b
for(p1=head_a;p1->next!=NULL;p1=p1->next) //
{
p3=p1;
for(p2=p1->next;p2!=NULL;p2=p2->next)
if(p3->num>p2->num)
p3=p2;
if(p1!=p3)
{
num0=p1->num;
p1->num=p3->num;
p3->num=num0;
score0=p1->score;
p1->score=p3->score;
p3->score=score0;
}
}
print(head_a);
return 0;
}