空指针的问题
Roy T 2006-05-13 08:21:48
我在做数据结构时候遇到一个难题,请好心人看看
//这是头文件的开头 ds.h (那些宏定义都省略了)
//静态顺序表的数据结构:
typedef struct SqList {
ElemType *elem; //Storage place.
int length; //How many elements are there in the SqList.
int size; //How big are this SqList.
} SqList;
//初始化:
void initial(SqList &L) {
L.elem = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
if(!L.elem)
exit(OVERFLOW);
L.length = 0;
L.size = INIT_SIZE;
}
//判空
bool isEmpty(SqList L) {
if(L.length == 0)
return true;
return false;
}
//判满:
bool isFull(SqList L) {
if (L.length >= L.size)
return true;
return false;
}
//添加节点
bool insertElem(SqList &L, int position, ElemType insr_e) {
if(position<0 || position>L.length)
return ERROR;
if(isFull(L)) {
ElemType *newbase;
newbase = ( (ElemType*)realloc(L.elem, (L.size+INCREMENT)*sizeof(ElemType)) );
if(!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.size += INCREMENT;
}
for(int i = L.length; i >= position; i--)
assign(L.elem[i+1], L.elem[i]);
assign(L.elem[position], insr_e);
L.length++;
return OK;
}
//遍历
void traverse(SqList L, void(*func)(ElemType&)) {
for(int i = 0; i < L.length; i++)
func(L.elem[i]);
}
//这是头文件的结尾 ds.h~
//以下是测试这个数据街购的: a.cpp
typedef int ElemType;
void assign(ElemType &dest_e, ElemType surc_e) {
dest_e = surc_e;
}
bool isEqual(ElemType a, ElemType b) {
if(a == b)
return true;
return false;
}
void print(ElemType &e) {
cout << e << ' ';
}
#include "ds.h"
int main() {
SqList D;
int i;
initial(D);
cout << isEmpty(D) << isFull(D) << endl;
//不写这一行什么都正常,写上这行就非法操作退出,用TCC3.0编译是 Null pointer assignment错误,我实在是找不出来呀,您受累给看看。。我已经精简到最简拉。。。万分感谢!!
for(i = 0; i < 16; i++)
insertElem(D, getLength(D),i+1);
traverse(D, print);
cout << endl;
return 0;
}