题目是:已有两个链表a和b,每个链表中的结点包含学号,成绩。要求把两个链表合并,按学号生序排列。
我用的是VC++6.0.再次感谢大家。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int id;
char name[10];
float math,english,computer;
struct node *next;
};
struct node *ahead,*bhead,*chead;
void jianliAbiao()
{
float a;
struct node *p,*q;
if(ahead==NULL)
{
ahead=(struct node *) malloc(sizeof(struct node));
ahead->next=NULL;
}
p=(struct node *) malloc(sizeof(struct node));
printf(" qingshuruxuehao:");
scanf("%d",&p->id);
printf(" qingshuruxingming:");
scanf("%s",&p->name);
for(;;)
{
printf(" qingshuruyingyu(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shuruyingyuyouwu!\n");
continue;
}
else break;
}
p->english=a;
for(;;)
{
printf(" qingshurushuxue(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shurushuxueyouwu!\n");
continue;
}
else break;
}
p->math=a;
for(;;)
{
printf(" qingshurujisuanji(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shurujisuanjiyouwu!\n");
continue;
}
else break;
}
p->computer=a;
p->next=NULL;
q=ahead;
if(ahead->next==NULL)
{
ahead->next=p;
}
else
{
while(q->next!=NULL)
q=q->next;q->next=p;
}
}
void jianliBbiao()
{
float a;
struct node *p,*q;
if(bhead==NULL)
{
bhead=(struct node *) malloc(sizeof(struct node));
bhead->next=NULL;
}
p=(struct node *) malloc(sizeof(struct node));
printf(" qingshuruxuehao:");
scanf("%d",&p->id);
printf(" qingshuruxingming:");
scanf("%s",&p->name);
for(;;)
{
printf(" qingshuruyingyu(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shuruyingyuyouwu!\n");
continue;
}
else break;
}
p->english=a;
for(;;)
{
printf(" qingshurushuxue(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shurushuxueyouwu!\n");
continue;
}
else break;
}
p->math=a;
for(;;)
{
printf(" qingshurujisuanji(0~100):");
scanf("%f",&a);
if(a<0||a>100)
{
printf(" shurujisuanjiyouwu!\n");
continue;
}
else break;
}
p->computer=a;
p->next=NULL;
q=bhead;
if(bhead->next==NULL)
{
bhead->next=p;
}
else
{
while(q->next!=NULL)
q=q->next;q->next=p;
}
}
void xianshi(struct node *y)
{
struct node *x;
x=y;
if(x->next==NULL)
{
printf(" chengjibiaowujilu!\n");
}
else
{
printf(" xuehao xingming yingyu shuxue jisuanji\n");
while(x->next!=NULL)
{
printf(" %d %s %3.2f %3.2f %3.2f\n",x->next->id,x->next->name,x->next->english,x->next->math,x->next->computer);
x=x->next;
}
}
}
void hebingCbiao()
{
struct node *p,*q,*z;
chead=ahead;
p=chead;
q=bhead->next;
while(p->next!=NULL)
p=p->next;
while(q!=NULL)
{
z=chead;
while(z!=NULL)
{
if(q->id==z->id)
{
q=q->next;break;
}
z=z->next;
}
if(z==NULL)
{
p->next=q;
p=q;
q=q->next;
p->next=NULL;
}
}
}
main()
{
int x;
for(;;)
{
printf(" chengjiguanli[hebing] \n");
printf(" ----------------------------------------------\n");
printf(" | 1.jianliAbiao |\n");
printf(" | 2.jianliBbiao |\n");
printf(" | 3.xianshiAbiao |\n");
printf(" | 4.xianshiBbiao |\n");
printf(" | 5.hebingCbiao |\n");
printf(" | 6.xianshiCbiao |\n");
printf(" | 0.tuichu! |\n");
printf(" ----------------------------------------------\n");
printf(" qingshugongnenghao:");
scanf("%d",&x);
if(x==1) jianliAbiao();
if(x==2) jianliBbiao();
if(x==3) xianshi(ahead);
if(x==4) xianshi(bhead);
if(x==5) hebingCbiao();
if(x==6) xianshi(chead);
if(x==0) break;
}
}