编程,根据输入的数建立两个递增排序的单链表A,B,用两个链表中相同的元素按递减有序生成一个新的链表,要求新的链表中没有重复元素。
有谁可以帮我新手一下,谢谢了。
我是用的成绩管理做的实验,你可以参考一下,当合并两个成绩表时,相同的学号就只输出第一个成绩表的纪录。
程序如下:
#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;
}
}