Hi everyone,I'm trying to write a piece of code that sort the number through linked list.For example when the user gives 4,3,10,2,1
output should be like this 10 4 3 2 1 but I've seg fault and i dont know what should i do and where i am wrong.Is there anyone who can help me ? thanks a lot.
Code:#include<stdio.h> #include<stdlib.h> struct node{ int deger/*value*/; struct node * next; }; int data; struct node * bas=NULL; struct node * fonk_mknode(int data){ struct node * np; np=malloc(sizeof(struct node)); np->deger=data; np->next=NULL; return np; } struct node * fonk_insert(struct node ** bas,int data){ struct node *tutar; struct node *gezici=*bas; struct node *bilinmez=NULL; tutar=fonk_mknode(data); if(*bas==NULL){ *bas=tutar; } else{ while(gezici->deger>(tutar->deger)){ if(gezici->next==NULL){ gezici->next=tutar;} bilinmez=gezici; gezici=gezici->next; } bilinmez->next=tutar; tutar->next=gezici; } return *bas; } void fonk_print(struct node *list){ for( ;list;list=list->next) printf("%d ",list->deger); printf("\n"); } int main(){ while(1){ scanf("%d",&data); if(data==-1) break; fonk_insert(&bas,data); } fonk_print(bas); return 0; }