📄 link.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;p0=stud;
if(head==NULL) {head=p0;p0->next=NULL;}
else {
while((p0->num>=p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;}
else {p1->next=p0;p0->next=NULL;}
}
return(head);
}
struct student *creat(int n)
{
struct student *head;
struct student *stu;int k=1;
stu=(struct student *)malloc(LEN);
scanf("%ld,%d",&stu->num,&stu->score);
head=NULL;
while(k<=n)
{k=k+1; head=insert(head,stu);
stu=(struct student *)malloc(LEN);
scanf("%ld,%d",&stu->num,&stu->score);
}free(stu);
return(head);
}
struct student *merge(struct student *ahead,struct student *bhead)
{
struct student *pa1,*pa2,*pb1;
pa2=pa1=ahead;pb1=bhead;
do{ pa2=pa1=ahead;
while((pb1->num>pa1->num)&&(pa1->next!=NULL))
{pa2=pa1;pa1=pa1->next;}
if(pb1->num<pa1->num)
{if(ahead==pa1) ahead=pb1;
else pa2->next=pb1;
pa1=pb1->next;}
else if(pb1->num==pa1->num)
{if(pb1->score>pa1->score) pa2->next=pb1; }
else {pa1->next=pb1;pb1->next=NULL;}
pb1=pb1->next;
}while(pb1!=NULL);
return(ahead);
}
struct student *del(struct student *ahead,struct student *bhead)
{
struct student *p1,*p2,*p;
p1=p=ahead;
while(p1!=NULL)
{p2=bhead;
while((p1->num!=p2->num)&&(p2->next!=NULL))
{p=p1;p2=p2->next;}
if(p1->num==p2->num)
if(p1==ahead) ahead=p1->next;
else {p->next=p1->next;}
p1=p1->next;
} return(ahead);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do{printf("%ld,%d\n",p->num,p->score);
p=p->next;}while(p!=NULL);
}
void main()
{
struct student *a,*b,*ahead,*bhead,*chead,*dhead;int n,m;
scanf("%d",&n);
printf("input records:\n");
ahead=creat(n);
print(ahead);
scanf("%d",&m);
bhead=creat(m);
print(bhead);
printf("\nmerge the two records:\n");
chead=merge(ahead,bhead);
print(chead);
printf("\ndel the same score of b from a:\n");
dhead=del(ahead,bhead);
print(dhead);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -