帮我看下这个链表程序,哪里错了

fxq1029 2010-10-19 11:44:31
在VC里运行,查找的时候需要输入两次学号,删除结点的时候提示“内存不能为"read"”, 不知道什么原因,烦请各位帮忙看下。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct student
{
int num;
float score;
struct student *next;
};
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
if(head==NULL)
{
printf("申请头结点失败!\n");
return NULL;
}
else
head->next=NULL;
}
struct student *Insert(struct student *head, struct student *s)
{
struct student *p=head;
while(p->next!=NULL&&s->score>p->next->score)
p=p->next;
if(p==NULL)
{
p->next=s;
s->next=NULL;
}
else
{
s->next=p->next;
p->next=s;
}
return head;
}

struct student *search(struct student *head)
{
int num;
struct student *p=head->next;
printf("请输入要查找结点的学号:\n");
scanf("%d\n",&num);
while(p!=NULL&&p->num!=num)
p=p->next;
if(p=NULL)
{
printf("没有找到学号为%d的结点!\n",num);
return NULL;
}
else
{
printf("找到学号为%d的结点\n该结点为%d\t%f\n",num,p->num,p->score);
return p;
}
}

void print(struct student *head)
{
struct student *p=head->next;
while(p!=NULL)
{
printf("%d\t%.1f\n",p->num,p->score);
p=p->next;
}
}

void free_list(struct student *head)
{
struct student *p=head;
while(p!=NULL)
{
head=head->next;
free(p);
p=head;
}
printf("释放链表成功!\n");
}

struct student *delenote(struct student *head, int num_x)//删除学号为num_x的结点
{
struct student *p1=head,*p2=head->next;
while(p2!=NULL&&p2->num!=num_x)
{
p2=p2->next;
p1=p1->next;
}
if(p2==NULL)
{
printf("没有找到要删除的结点!\n");
}
else
{
p1->next=p2->next;
free(p2);
printf("删除结点成功!\n");
}
return head;
}

void main()
{
struct student *head,*p;
int num;
float score;
char c;
printf("有头结点链表操作程序:\n");
printf("I:插入结点(自动升序) P:打印链表 S:查找结点 D:删除结点 F:释放链表并退出程序");
head=create_head();
while(1)
{
c=getchar();
switch(c)
{
case 'I': printf("请输入要插入学生的学号和分数:\n");
scanf("%d%f\n",&num,&score);
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{
printf("申请结点失败!\n");
exit(0);
}
else
{
p->num=num;
p->score=score;
Insert(head,p);
printf("插入成功\n");
}
break;
case 'P': print(head);
break;
case 'S': search(head);
break;
case 'D':printf("请输入要删除学生的学号:\n");
scanf("%d\n",&num);
delenote(head,num);
break;
case 'F':free_list(head);
exit(0);
}
}
}
...全文
104 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxq1029 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 liberty724048 的回复:]
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
if(head==NULL)
{
printf("申请头结点失败!\n");
return NULL;
}
……
[/Quote]
是有return head;的,我漏打了~~
fxq1029 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 q342210738 的回复:]
建议你:

所有的if判断语句写成if(NULL==p)

这是一种好的编程习惯,凡是判断语句,都写成if(常量==变量)

这样的话,可以避免#10L的错误,因为只要代码一编译,就会报错,很容易发现错误
[/Quote]
恩 这个孙鑫老师的视频上提到过 很好的建议 谢谢你~
雪影 2010-10-19
  • 打赏
  • 举报
回复
自己先调试一下,自己找找问题
jyh_baoding 2010-10-19
  • 打赏
  • 举报
回复
有什么错误码
nangelaile 2010-10-19
  • 打赏
  • 举报
回复
太长了,看着头疼。。在运行里调试一下。看看红字在哪里。
Liberty-Bird 2010-10-19
  • 打赏
  • 举报
回复
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
if(head==NULL)
{
printf("申请头结点失败!\n");
return NULL;
}
else
head->next=NULL;
//这里为啥没有返回值啊?
//应该加上:
//return head;

}
lyingbo 2010-10-19
  • 打赏
  • 举报
回复
建议你:

所有的if判断语句写成if(NULL==p)

这是一种好的编程习惯,凡是判断语句,都写成if(常量==变量)

这样的话,可以避免#10L的错误,因为只要代码一编译,就会报错,很容易发现错误
fxq1029 2010-10-19
  • 打赏
  • 举报
回复
实在不好意思,犯了一些低级错误,删除结点时报“内存不能为"read"”的原因是if(p2=NULL),应为if(p2==NULL), 查找结点时必须要输入两次学号是因为scanf("%d\n",&num);语句里多了\n .以下是正确正序:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct student
{
int num;
float score;
struct student *next;
};
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
if(head==NULL)
{
printf("申请头结点失败!\n");
return NULL;
}
else
head->next=NULL;
return head;
}
struct student *Insert(struct student *head, struct student *s)
{
struct student *p=head;
while(p->next!=NULL&&s->score>p->next->score)
p=p->next;
if(p->next==NULL)
{
p->next=s;
s->next=NULL;
}
else
{
s->next=p->next;
p->next=s;
}
return head;
}

struct student *search(struct student *head)
{
int num;
struct student *p=head->next;
printf("请输入要查找结点的学号:\n");
scanf("%d",&num);
while(p!=NULL&&p->num!=num)
p=p->next;
if(p==NULL)
{
printf("没有找到学号为%d的结点!\n",num);
return NULL;
}
else
{
printf("找到学号为%d的结点\n该结点为%d\t%f\n",num,p->num,p->score);
return p;
}
}

void print(struct student *head)
{
struct student *p=head->next;
printf("链表如下:\n");
while(p!=NULL)
{
printf("%d\t%.1f\n",p->num,p->score);
p=p->next;
}
}

void free_list(struct student *head)
{
struct student *p=head;
printf("释放链表\n");
while(p!=NULL)
{
head=head->next;
free(p);
p=head;
}
printf("释放链表成功!\n");
}

struct student *delenote(struct student *head, int num_x)//删除学号为num_x的结点
{
struct student *p1=head,*p2=head->next;
while(p2!=NULL&&p2->num!=num_x)
{
p2=p2->next;
p1=p1->next;
}
if(p2==NULL)
{
printf("没有找到要删除的结点!\n");
}
else
{
p1->next=p2->next;
free(p2);
printf("删除结点成功!\n");
}
return head;
}

void main()
{
struct student *head,*p;
int num;
float score;
char c;
printf("有头结点链表操作程序:\n");
printf("I:插入结点(自动升序) P:打印链表 S:查找结点 D:删除结点 F:释放链表并退出程序");
head=create_head();
while(1)
{
c=getchar();
switch(c)
{
case 'I': printf("请输入要插入学生的学号和分数:\n");
scanf("%d%f",&num,&score);
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{
printf("申请结点失败!\n");
exit(0);
}
else
{
p->num=num;
p->score=score;
Insert(head,p);
printf("插入成功\n");
}
break;
case 'P': print(head);
break;
case 'S': search(head);
break;
case 'D':printf("请输入要删除学生的学号:\n");
scanf("%d",&num);
delenote(head,num);
break;
case 'F':free_list(head);
exit(0);
}
}
}
fxq1029 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tiger9991 的回复:]
问题不是一点点的多啊,我拷贝进偶机器,那个插入都错误,楼主这不叫改错了,整段代码都有问题.好好自己想想.
[/Quote]
我是一个字母一个字母敲进去的,电脑有加密软件,拷不进去,可能漏了。我再检查一遍。
傻X 2010-10-19
  • 打赏
  • 举报
回复
问题不是一点点的多啊,我拷贝进偶机器,那个插入都错误,楼主这不叫改错了,整段代码都有问题.好好自己想想.
wateryh 2010-10-19
  • 打赏
  • 举报
回复
看了第一个函数create_head()
正常的情况没有返回值
fxq1029 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mayudong1 的回复:]
太长,不看
[/Quote]
我可是一个一个敲进去的诶
fxq1029 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jyh_baoding 的回复:]
有什么错误码
[/Quote]
不是白说嘛 我已经调试过了
acrifyh 2010-10-19
  • 打赏
  • 举报
回复
自己调试一下

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧