建立动态链表时出错

jianchidaodihahaha 2006-03-09 01:15:44
写了一个动态链表,但运行时显示vc遇错,需要关闭。
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld %f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
p1=head;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
}
p2->next=NULL;
return(head);
}
void print (struct student *head)
{
struct student *p;
printf("\nThe records are:\n");
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
main()
{
struct student *head;
printf("Input the records:\n");
head=creat();
print(head);
}
不明白错在哪里,请教!
...全文
160 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangmuqq88 2006-03-12
  • 打赏
  • 举报
回复
欢迎加群20641933,欢迎大家共同交流.提高!(不怕不会,就怕不学.)
jixingzhong 2006-03-12
  • 打赏
  • 举报
回复
typedef enum
{
FALSE = 0,
TRUE = !FALSE
} bool;
是什么意思啊?还有可不可以不释放啊?
-------------------
前面是定义结构体,
typedef 自定义类型 ...

不释放 ...
不好,
最好还是释放吧 ..
否则...
内存泄漏鸟 ...
mp6 2006-03-12
  • 打赏
  • 举报
回复
hi goodluckyxl,
pAddPos = head;
while(head->next!=NULL)
{
pAddPos = head;
head = head->next;
}
这一段也太马虎了
mp6 2006-03-12
  • 打赏
  • 举报
回复
搂主代码的风格需要注意,指针类型最好马上赋初值(例如NULL),函数里面需要首先检查参数等等。
jianchidaodihahaha 2006-03-09
  • 打赏
  • 举报
回复
谢谢哦!你们都好强!我没完全看懂。
typedef enum
{
FALSE = 0,
TRUE = !FALSE
} bool;
是什么意思啊?还有可不可以不释放啊?
goodluckyxl 2006-03-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>
typedef enum
{
FALSE = 0,
TRUE = !FALSE
} bool;

typedef struct student
{
long num;
float score;
struct student *next;
}student;
//create的功能明确只是返回一个有意义的头接点
//建立起链表最基本的起始结构

struct student* creat()
{
struct student* head = NULL;
head = (struct student*)malloc(sizeof(struct student));
scanf("%ld %f",&head->num,&head->score);
head->next = NULL;
return head;
}
//如需要另外添加节点那么重新分配空间在构造链进链表

bool addtail(struct student* head)
{
struct student* pAddPos = NULL, *pNew = NULL;
if( (pNew = (struct student*)malloc(sizeof(struct student)))==NULL )
return FALSE;
scanf("%ld %f",&pNew->num,&pNew->score);
pNew->next = NULL;

pAddPos = head;
while(head->next!=NULL)
{
pAddPos = head;
head = head->next;
}
pAddPos->next = pNew;
return TRUE;
}

void destroy(struct student* head)
{
struct student* pDestroy = NULL;
pDestroy = head;
while (head->next!=NULL)
{
head = head->next;
free(pDestroy);
}
free(head);

}

void print (struct student *head)
{
struct student *p;
printf("\nThe records are:\n");
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
//你看下我刚刚大概写的东西
//弄个简单的模型构造 添加屁股后面一个节点 还有释放
//你的显示是可以用的没有改 不懂可以问我
void main()
{
struct student *head;
printf("Input the records:\n");
head=creat();
addtail(head);
print(head);
destroy(head);
}
adintr 2006-03-09
  • 打赏
  • 举报
回复
看 create 函数中的这部分:
while(p1->num!=0)
{
n=n+1;
if(n==1)
p1=head;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
}
有两个问题:
1. 循环不知道什么时候退出,因为 p1=(struct student *)malloc(LEN); 后没有初始化,p1->num 的值随机的。
2. if(n == 1) p1 = head, 由于之前 head = NULL, 所以 p1 = NULL, 随后又有 p2 = p1, 所以 p2 = NULL, 到下一循环 p2 没有改变的情况下使用了 p2->next, 就出现了 NULL->next 的空指针引用。
修改如下:
while(p1->num!=0)
{
n=n+1;
if(n==1)
head = p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld %f",&p1->num,&p1->score);
}
newpine 2006-03-09
  • 打赏
  • 举报
回复
写代码加点注释拉,让人想帮你看一下算法都觉得浪费
lbing7 2006-03-09
  • 打赏
  • 举报
回复
head=NULL;

一系列的没有头

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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