C语言单链表题目段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域,为什么啊!
#include<stdio.h>#include<stdlib.h>#include<string.h>structgrades{charnum[100];charna...
#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;
} 展开
#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;
} 展开
展开全部
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct grades {
char num[100];
char name[100];
int math,chinese,english;
struct grades *next;
}join,teample;
typedef struct grades Grades,*GRADES;
int main() {
int n = 0,i = 0,res,flag = 0;
int chinese = 0,english = 0,math = 0;
char num[100],name[100];
GRADES head,p,join;
head = p = (GRADES)malloc(sizeof(Grades));
scanf("%d",&n);
for(i = 0;i < n;i++) {
scanf("%s",num);
scanf("%s",name);
scanf("%d%d%d",&math,&chinese,&english);
p->next = (GRADES)malloc(sizeof(Grades));
strcpy(p->next->num,num);
strcpy(p->next->name,name);
p->next->math = math;
p->next->chinese = chinese;
p->next->english = english;
p = p->next;
}
p->next = NULL;
//至此,链表创建完毕
join = (GRADES)malloc(sizeof(Grades));
scanf("%s",join->num);
scanf("%s",join->name);
scanf("%d%d%d",&join->math,&join->chinese,&join->english);
p = head;
while(p->next) {
res = strcmp(p->next->num,join->next->num);
if(res == 0) {
printf("error(重复的学号)!\n");
return 1;
}
else if(res > 0) {
join->next = p->next;
p->next = join;
flag = 1;
break;
}
else p = p->next;
}
if(flag == 0) {//插到链表尾部
join->next= NULL;
p->next = join;
flag = 1;
}
//if(flag == 0) printf("SOORY,Insert过程未完成!\n");
p = head->next;
while(p) {
printf("%s ",p->num);
printf("%s ",p->name);
printf("%d ",p->math);
printf("%d ",p->chinese);
printf("%d\n",p->english);
p = p->next;
}
return 0;
}
追问
地址网页链接
在while(p->next)中如果p->num比join->大不是应该插入到p的前面吗?这是不是放后面了
答案我采纳了能告诉我我代码为啥出现
段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域
吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询