原帖由 [bold][underline]死了都要C[/underline][/bold] 于 2007-11-26 20:45 发表 [url=http://bbs.]
" border="0" />[/url]
我大概知道问题出在那里了```
我把
printf("%d,%d",L->listsize,L->length);
剪贴到了
L->listsize=LIST_INIT_SIZE;
下面``
程序运行正确```
谢谢了.
但我的原意是想描述线性链表.
原程序如下.
List.h
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define TRUE
1
#define FALSE
0
#define OK
1
#define ERROR
0
#define INFEASIBLE
-1
#define OVERFLOW
-2
#define LIST_INIT_SIZE
100
#define LISTINCREAMENT
10
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType
*elem;
int
length;
int
listsize;
}SqList;
Status InitList_Sq(SqList *L){
L=(SqList *)malloc(sizeof(SqList));
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
Status DestroyList_Sq(SqList *L){
if(L==NULL) return ERROR;
free(L->elem);
free(L);
return OK;
}
Status ClearList_Sq(SqList *L){
Status St1,St2;
if(L==NULL) return ERROR;
St1=DestroyList_sq(L);
if(St1!=OK) return St1;
St2=InistList_Sq(L);
if(St2!=OK) return St2;
return OK;
}
Status ListEmpty_Sq(SqList L){
if(&L==NULL) return INFEASIBLE;
if(L.length==0) return TRUE;
else
return FALSE;
}
int ListLenght_Sq(SqList L){
return L.length;
}
Status GetElem_Sq(SqList L,int i, ElemType *e){
if(&L==NULL||i<1||i>ListLength_Sq(L)) return ERROR;
*e=*(L.elem+i-1);
return OK;
}
int LocateElem_Sq(SqList L,ElemType e,Status(*compare)()){
int i=1;
ElemType *p=L.elem;
if(&L==NULL) return ERROR;
while(i<=L.length&&!(*compare)(*p++,e)) ++i;
if(i<=L.length) return i;
else
return 0;
}
Status PriorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e){
int i=1;
if(&L==NULL) return ERROR;
while(i<=L.length&&cur_e!=*(L.elem+i-1)) ++i;
if(i==1) {pre_e=NULL;return OK;}
if(i==L.length&&cur_e!=*(L.elem+i-1)) {pre_e=NULL;return OK;}
else {*pre_e=*(L.elem+i-1-1);return OK;}
}
Status NextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e){
int i=1;
if(&L==NULL) return ERROR;
while(i<=L.length&&cur_e!=*(L.elem+i-1)) ++i;
if(i==L.length) {next_e=NULL;return OK;}
else {*next_e=*(L.elem+i);return OK;}
}
Status ListInsert_Sq(SqList *L,int i,ElemType e){
ElemType *p,*q;
if(L==NULL||i<1||i>L->length+1) return ERROR;
if(L->length>=L->listsize){
ElemType *newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREAMENT)*sizeof(ElemType));
if(!newbase)exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREAMENT;
}
q=L->elem+i-1;
for(p=L->elem+L->length-1;p>=q;--p) *(p+1)=*p;
*q=e;
++L->length;
return OK;
}
Status ListDelete_Sq(SqList *L,int i,ElemType *e){
ElemType *p,*q;
if(L==NULL||i<1||i>L->length) return ERROR;
p=L->elem+i-1;
*e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;++p) *(p-1)=*p;
--L->length;
return OK;
}
Status ListTraverse_Sq(SqList L,Status (*visit)()){
int i;
if(&L==NULL) return ERROR;
for(i=1;i<=L.length;i++)
if((*visit)(*(L.elem+i-1))!=OK) return (*visit)(*(L.elem+i-1));
return OK;
}
虽然程序中有很多语法错误,但主要的问题是我提到的连接错误.
编写测试程序时,只要一用到List.h中的函数就出错.编写成一个C文件错误还是一样的.
我觉的可能是指针的运用上出了问题.但应该不是简单的把printf语句放到子程序中这么改.
还有答楼上的那位朋友,以前我老师定义的好象是一个无意义的指针变量.在后面好象用都没用到.
具体是怎样的,我记不清了.我们哪个老师脾气太怪了,问他是为什么又不给我们讲.