| 查看: 2619 | 回复: 1 | ||
[求助]
C语言数组中插入元素问题
|
|
清华大学数据结构C语言版第二种的第一个算法,给定两个线性表A和B,要求一个新的合集A,将B中A没有的元素插入A。 比如I nput: A={1, 3, 5, 7, 9}, B={2, 4, 6, 9} Output: A={1, 2, 3, 4, 5, 6, 7, 9} 我写了一个代码 但是在46行有segmentation fault. 没有想出来为什么。 ===================================================== #include #define MAXSIZE 100 typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; int length; }SqList; void CreateList(SqList *L, int n){ L->length=n; printf("\ninput %d data: ", n); int i; for(i=0;i } void PrintList(SqList *L){ int i; int n; n=L->length; printf("\noutput %d data: ", n); for(i=0;i } ElemType GetElem(SqList *L,int i){ return L->data; } int LocateElem(SqList *L, ElemType e){ int i; for(i=1;i<=L->length;i++){ if(L->data==e){ return i; break; } else return 0; } } void ListInsert(SqList *L, ElemType e){ int n = L->length; n++; L->length=n; L->data[n]=e; // Segmentation Fault Here !!! } void merge(SqList *La, SqList *Lb){ int i; ElemType e; for(i=0;i e=GetElem(&Lb,i); if(!LocateElem(&La,e)) ListInsert(&La,e); } } int main(){ SqList La,Lb; int n1,n2; printf("\nInput number for La: " ;scanf("%d",&n1); CreateList(&La,n1); printf("\nInput number for Lb: " ;scanf("%d",&n2); CreateList(&Lb,n2); printf("Here is La:\n" ;PrintList(&La); printf("Here is Lb:\n" ;PrintList(&Lb); merge(&La,&Lb); printf("Here is merged list:\n" ;PrintList(&La); return 0; } |
» 猜你喜欢
拟解决的关键科学问题还要不要写
已经有10人回复
救命帖
已经有5人回复
限项规定
已经有5人回复
为什么nbs上溴 没有产物点出现呢
已经有9人回复
招博士
已经有3人回复
存款400万可以在学校里躺平吗
已经有35人回复
最失望的一年
已经有18人回复
求推荐博导
已经有4人回复
求推荐英文EI期刊
已经有5人回复
疑惑?
已经有5人回复
» 本主题相关价值贴推荐,对您同样有帮助:
c语言动态数组
已经有7人回复
c语言高手请进,请勿灌水,谢谢!(关于读写文件的问题)
已经有6人回复
关于c语言的一个小问题 坐等回复啊 急啊 是关于建立数组的
已经有9人回复
【求助】菜鸟求助c语言读文件小问题
已经有5人回复
2楼2011-07-18 05:45:57













;
回复此楼