#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct grades{
char num[100];
char name[100];
int math,china,english;
struct grades *next;
}join,teample;
typedef struct grades Grades,*GRADES;
int main()
{
int n=0,i=0;
int china=0,english=0,math=0;
char num[100],name[100];
GRADES head,p,rear,join;
head=(GRADES)malloc(sizeof(Grades));
head->next=NULL;
rear=head;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",num);
scanf("%s",name);
scanf("%d%d%d",&math,&china,&english);
p=(GRADES)malloc(sizeof(Grades));
strcpy(p->num,num);
strcpy(p->name,name);
p->math=math;
p->china=china;
p->english=english;
rear->next=p;
rear=p;
}
rear->next=NULL;
join=(GRADES)malloc(sizeof(Grades));
scanf("%s",join->num);
scanf("%s",join->name);
scanf("%d%d%d",&join->math,&join->china,&join->english);
p=head->next;
while(p){
if(strcmp(p->num,join->num)==0){
printf("error!");
return 0;
}
else
p=p->next;
}
p=head->next;
n=0;
while(p){
n++;
if(strcmp(p->num,join->num)>0)
break;
else
p=p->next;}
p=head->next;
n-=2;
while(n--){
p=p->next;
}
if(p!=NULL){
join->next=p->next;
p->next=join;}
else{
join=NULL;
p->next=join;
}
p=head->next;
while(p!=NULL)
{
printf("%s ",p->num);
printf("%s ",p->name);
printf("%d ",p->math);
printf("%d ",p->china);
printf("%d\n",p->english);
p=p->next;}
return 0;
}
地址网页链接
在while(p->next)中如果p->num比join->大不是应该插入到p的前面吗?这是不是放后面了
答案我采纳了能告诉我我代码为啥出现
段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域
吗?