注册 登录
编程论坛 C++教室

顺序表错误

peswe 发布于 2007-04-15 14:12, 782 次点击

刚做了个顺序表,程序编译的时候没错,当运行的时候就有错,我估计是结构体指针出了问题,但就是不知道错在哪里,望各位大侠指点!
#include <stdio.h>
#define maxnum 20
typedef int elemtype;
struct qltype
{
elemtype list[maxnum];
int num;
};

void creat(qltype *la) /*建立顺序表*/
{
int i,n;
printf("\n please input the number of node:");
scanf("%d",&n);
la->num=n; //运行到这步就出错
printf("\n Input the content:");
for(i=0;i<n;i++)
{
printf("\n please input the ");
printf("%d",i);
printf("th number:");
scanf("%d",&la->list[i]);
}
for(i=0;i<n;i++)
printf("%d",la->list[i]);
}
int main()
{
struct qltype *la=0;
creat(la);
return 0;
}

[此贴子已经被作者于2007-4-15 14:13:56编辑过]

6 回复
#2
xvholly2007-04-15 16:23
在Create()开始要新建一个结点 la = new qltype;
函数最后再释放该结点 delete la;
#3
peswe2007-04-15 16:53
这里la是指针,并不需要分配空间!~
不过还是谢谢了!我已经找到了错误!~
#4
xvholly2007-04-16 13:14
但是我的理解是 la 初始化为 NULL , 并为指向任何位置.
LZ可否告诉我错误是哪里?
#5
Jackzdong2007-04-16 16:18
以下是引用peswe在2007-4-15 14:12:34的发言:

刚做了个顺序表,程序编译的时候没错,当运行的时候就有错,我估计是结构体指针出了问题,但就是不知道错在哪里,望各位大侠指点!
#include <stdio.h>
#define maxnum 20
typedef int elemtype;
struct qltype
{
elemtype list[maxnum];
int num;
};

void creat(qltype *la) /*建立顺序表*/
{
int i,n;
printf("\n please input the number of node:");
scanf("%d",&(la->num));
// la->num=n; //运行到这步就出错
printf("\n Input the content:");
for(i=0;i<la->num;i++)
{
printf("\n please input the ");
printf("%d",i);
printf("th number:");
scanf("%d",&la->list[i]);
}
for(i=0;i<la->num;i++)
printf("%d",la->list[i]);
}
int main()
{
struct qltype *la=0;
creat(la);
return 0;
}

在运行看看

[此贴子已经被作者于2007-4-16 16:19:57编辑过]

#6
peswe2007-04-16 16:24

给指针la初始化个结构体变量的值就可以了,如在main函数中输入:
struct qltype p1;
la=&p1;

还有楼上的意见是行不通的,就算你注释了这一行,但是下面还有调用指la的代码,所以运行到下面的时候还是有错误的。

[此贴子已经被作者于2007-4-16 16:28:32编辑过]

#7
ycxlove2008-04-06 01:11
struct qltype *la=0;这是对*a赋值吗?我认为这里有错误!不知道对不
我认为 struct qltype *la=(qltype)0;
1