📄 text.c
字号:
#include <stdio.h>
#include<stdlib.h>
struct student
{
long num;
int score;
struct student *next;
};
struct student * creat(int n);
void print(struct student *head);
struct student * merge(struct student *a,struct student *b);
struct student * del(struct student *a,struct student *b);
void main()
{
struct student *a,*b,*c;
a=creat(5);
printf("**************a****************\n");
print(a);
b=creat(6);
printf("**************b****************\n");
print(b);
c=merge(a,b);
printf("**************c****************\n");
print(c);
a=del(a,b);
printf("**************a****************\n");
print(a);
}
struct student * creat(int n)
{
struct student * head=NULL,*p0,*p1,*p2;
int i=0;
while(n)
{ p0=(struct student * )malloc(sizeof(struct student));
scanf("%ld,%d",&p0->num,&p0->score);
n--;i++;
if(i==1) head=p0,p0->next=NULL;
else{
p1=head;
while(p1->num<=p0->num && p1->next!=NULL)
{ p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num )
{
if( p1==head) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {
p1->next=p0;
p0->next=NULL;
}
}
}
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%ld,%d\n",p->num,p->score);
p=p->next;
}
}
struct student * merge(struct student *a,struct student *b)
{
struct student *pa,*pb,*headc=NULL,*pc,*pc1;
int n=0;
if(a==NULL) return b;
if(b==NULL) return a;
pa=a;pb=b;
while(pa!=NULL&&pb!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
if(pa->num<pb->num)
{ pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;
}
else if(pa->num>pb->num)
{
pc->num=pb->num;
pc->score=pb->score;
pb=pb->next;
}
else if(pa->score>=pb->score)
{
pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;pb=pb->next;
}
else
{
pc->num=pb->num;
pc->score=pb->score;
pa=pa->next;pb=pb->next;
}
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
while(pa!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
while(pb!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
pc->num=pb->num;
pc->score=pb->score;
pb=pb->next;
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
pc1->next=NULL;
return (headc);
}
struct student * del(struct student *a,struct student *b)
{
struct student *pa,*pb,*pa1;
if(a==NULL || b==NULL ) return a;
pa1=pa=a;
while(pa!=NULL)
{
pb=b;
while((pa->num!=pb->num)&& pb->next!=NULL) pb=pb->next;
if(pa->num==pb->num)
{
if(pa==a){ a=pa->next;free(pa);pa1=pa=a;}
else{
pa1->next=pa->next;free(pa);pa=pa1->next;
}
}
else{
pa1=pa;pa=pa->next;
}
}
return (a);
}
二、编程题
结构
struct student
{
long num;
int score;
struct student *next;
};
链表练习:
(1).编写函数struct student * creat(int n),创建一个按学号升序排列的新链表,每个链表中的结点中的学号、成绩由键盘输入,一共n个节点。
(2).编写函数void print(struct student *head),输出链表,格式每行一个结点,包括学号,分数。
(3).编写函数struct student * merge(struct student *a,struct student *b),将已知的a,b两个链表按学号升序合并,若学号相同则保留成绩高的结点。
(4).编写函数struct student * del(struct student *a,struct student *b),从a链表中删除b链表中有相同学号的那些结点。
(5).编写main函数,调用函数creat建立2个链表a,b,用print输出俩个链表;调用函数merge升序合并2个链表,并输出结果;调用函数del实现a-b,并输出结果。
a:
20304,75
20311,89
20303,62
20307,87
20320,79
b:
20302,65
20301,99
20311,87
20323,88
20307,92
20322,83
#include <stdio.h>
#include<stdlib.h>
struct student
{
long num;
int score;
struct student *next;
};
struct student * creat(int n);
void print(struct student *head);
struct student * merge(struct student *a,struct student *b);
struct student * del(struct student *a,struct student *b);
void main()
{
struct student *a,*b,*c;
a=creat(5);
printf("**************a****************\n");
print(a);
b=creat(6);
printf("**************b****************\n");
print(b);
c=merge(a,b);
printf("**************c****************\n");
print(c);
a=del(a,b);
printf("**************a****************\n");
print(a);
}
struct student * creat(int n)
{
struct student * head=NULL,*p0,*p1,*p2;
int i=0;
while(n)
{ p0=(struct student * )malloc(sizeof(struct student));
scanf("%ld,%d",&p0->num,&p0->score);
n--;i++;
if(i==1) head=p0,p0->next=NULL;
else{
p1=head;
while(p1->num<=p0->num && p1->next!=NULL)
{ p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num )
{
if( p1==head) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {
p1->next=p0;
p0->next=NULL;
}
}
}
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%ld,%d\n",p->num,p->score);
p=p->next;
}
}
struct student * merge(struct student *a,struct student *b)
{
struct student *pa,*pb,*headc=NULL,*pc,*pc1;
int n=0;
if(a==NULL) return b;
if(b==NULL) return a;
pa=a;pb=b;
while(pa!=NULL&&pb!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
if(pa->num<pb->num)
{ pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;
}
else if(pa->num>pb->num)
{
pc->num=pb->num;
pc->score=pb->score;
pb=pb->next;
}
else if(pa->score>=pb->score)
{
pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;pb=pb->next;
}
else
{
pc->num=pb->num;
pc->score=pb->score;
pa=pa->next;pb=pb->next;
}
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
while(pa!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
pc->num=pa->num;
pc->score=pa->score;
pa=pa->next;
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
while(pb!=NULL)
{
pc=(struct student * )malloc(sizeof(struct student));
n++;
pc->num=pb->num;
pc->score=pb->score;
pb=pb->next;
if(n==1) headc=pc;
else
pc1->next=pc;
pc1=pc;
}
pc1->next=NULL;
return (headc);
}
struct student * del(struct student *a,struct student *b)
{
struct student *pa,*pb,*pa1;
if(a==NULL || b==NULL ) return a;
pa1=pa=a;
while(pa!=NULL)
{
pb=b;
while((pa->num!=pb->num)&& pb->next!=NULL) pb=pb->next;
if(pa->num==pb->num)
{
if(pa==a){ a=pa->next;free(pa);pa1=pa=a;}
else{
pa1->next=pa->next;free(pa);pa=pa1->next;
}
}
else{
pa1=pa;pa=pa->next;
}
}
return (a);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -