建立一个单链表,并使用该链表存储所有学生的信息。假设最初建立的链表名为ListA,你要将链表ListA中成成绩在1400分以上(含1400分)的节点从ListA中删去,并把这些节点插入到新链表ListB中,最后输出链表B
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char name[20];
char num[20];
int score;
struct student *next;
};
main(){
//构建链表A
struct student *head1,*p1,*p2;
int m=0,n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%s %s %d",p1->name,p1->num,&p1->score);
head1=NULL;
while(strcmp(p1->name,"#####")!=0)
{ n++;
if(n==1) head1=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%s %s %d",p1->name,p1->num,&p1->score);
}
p2->next=NULL;
//链表ListA中成成绩在1400分以上(含1400分)的节点从ListA中删去,并把这些节点插入到新链表ListB中
struct student *head2,*q1,*q2;
struct student *m1,*m2;
m1=head1;
m2=head1;
while(m2!=NULL)
{
if(m1->score>=1400){
m++;
q1=m1;
if(m==1) { head2=m1; q2=m1;}
else { q2->next=q1; q2=q1; }
m2->next=m1->next;
}
m2=m1;
m1=m1->next;
}
q2->next=NULL;//提取1400以上的数据
//打印1400以上的数据
struct student *node;
node=head2;
while(node!=NULL)
{
printf("%s %s %d\n",node->name,node->num,node->score);
node=node->next;
}
}
相关分类