我试图直接使用两个函数直接对我在链接列表中输入的数字进行排序第一个添加元素头部第二个包含分段错误的元素应该利用第一个元素做这项工作。分段错误,在C中的列表
#include <stdio.h>
#include <stdlib.h>
typedef struct cellule
{
int val;
struct cellule *suivant;
} cellule;
typedef struct cellule* liste;
liste insert_tete(liste L,int n)
{
liste p;
p=malloc(sizeof(cellule));
p->val=n;
p->suivant=L;
return p;
}//ok :)
liste insert_croissant(liste L,int n)
{
if(!L)
{
L=insert_tete(L,n);
return L;
}
liste p,q;
for(q=L,p=L; (p!=NULL)&&(n> (p->val)) ; q=p,p=p->suivant); //
p=insert_tete(p,n);
q->suivant=p;
return L;
}
请重新格式化您的代码以使其更易于阅读。 – 2012-04-11 22:11:29
'insert_croissant()'听起来很好吃! – FatalError 2012-04-11 22:12:01
不要在malloc中返回malloc的返回值。没有理由这样做,它实际上可以隐藏你忘记包含''的事实(没有强制转换,不存在非强制函数返回'int' ,这将失败,但演员隐藏它)。 C没有问题强制将'void *'隐式地指向任何其他指针类型。 –
2012-04-11 22:13:59