| 查看: 2686 | 回复: 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人回复
现代”学阀”该如何界定
已经有7人回复
各位大神,目前国内有哪些比较好用的逆合成软件?
已经有12人回复
我的奶奶
已经有3人回复
申博发邮件
已经有10人回复
国社科系统bug了,是不是要放榜了?
已经有9人回复
上海工程技术大学激光智能制造课题组|2027级博士研究生招生公告
已经有8人回复
上海工程技术大学激光智能制造课题组招收博士研究生
已经有8人回复
» 本主题相关价值贴推荐,对您同样有帮助:
c语言动态数组
已经有7人回复
c语言高手请进,请勿灌水,谢谢!(关于读写文件的问题)
已经有6人回复
关于c语言的一个小问题 坐等回复啊 急啊 是关于建立数组的
已经有9人回复
【求助】菜鸟求助c语言读文件小问题
已经有5人回复
2楼2011-07-18 05:45:57










;
回复此楼