问题出在交换中,但是我不确定如何访问不存在的位置,如果我将temp变量设置为普通整数,程序可以正常运行,但是我想通过使用临时结构指针来实现任何整数。
void sort()
{
node *i;
node *j;
node *t = head;/*if I declare t as integer type it works but i want to
know why exactly it is giving me a segmentation fault*/
for(i = head; i->next != NULL;i = i->next)
{
for(j = i->next ; j!= NULL ; j = j->next)
{
if(i->data>j->data)
{ printf("before swap---->%d %d\n",i->data,j->data );
t->data= i->data;
i->data = j->data;
j->data = t->data;
printf("after swap --->%d %d\n",i->data,j->data );
}
}
}
}