Why am i getting segmentation fault in deleting node?

Why am i getting segmentation fault in deleting node?


node * del(node * start,int loc)

int l=len(start);
if(loc<1



This is the function and I am calling it from main() as:


case 5:
printf("Enter the node you want to delete. n");
scanf("%d",temp);
start=del(start,temp);
break;



While deleting any node I am getting segmentation fault!I am stuck at this .
Can anyone help?



here is the full program:-


#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>

struct Node

int data;
struct Node * next;
;

typedef struct Node node;

int len(node * start)

if(start==NULL)
return 0;
int c=1;
while(start!=NULL)

start=start->next;
c++;

return c;

node * insert(node* start,int loc,int d)

int l=len(start);
node * p=start;
node * temp=(node*)malloc(sizeof(node));
if(loc<1
void show(node *start)

int l=len(start);
if(l==0)

printf("Nothing to print. n");

else

for(int i=1;i<l-1;i++)

printf(" %d -> ",start->data);
start=start->next;

printf(" %d n",start->data);


node * destroy(node * start)

node * p=start;
while(start!=NULL)

start=start->next;
free(p);
p=start;

printf("Destroy of linked list completed! .n");
return NULL;

node * del(node * start,int loc)

int l=len(start);
if(loc<1
int main()

node * start=NULL;
int ch,temp,d;
ch=temp=d=0;
while(ch != -1)

switch(ch)

case 0:
printf("Enter 0 to show menu.n");
printf("Enter 1 to create linked list.n");
printf("Enter 2 to destroy linked list.n");
printf("Enter 3 to get the length of likned list.n");
printf("Enter 4 to insert element in the linked list.n");
printf("Enter 5 to delete element from the linked list.n");
printf("Enter 6 to view the linked list.n");
printf("Enter 7 to reverse a linked list.n");
break;

case 1:
printf("Enter the size of link list to begin with.n");
scanf("%d",&temp);
for(int i=1;i<=temp;i++)

printf("Enter the data to bes inserted to node %d .n",i);
scanf("%d",&d);
start=insert(start,i,d);

break;

case 2:
start=destroy(start);
break;

case 3:
printf("Size of linked list is: %d n",len(start));
break;

case 4:
printf("Enter the location where you want to insert the data.n");
scanf("%d",&temp);
printf("Enter the data to be intered.n");
scanf("%d",&d);
start=insert(start,temp,d);
break;

case 5:
printf("Enter the node you want to delete. n");
scanf("%d",temp);
start=del(start,temp);
break;

case 6:
show(start);
break;

default:
ch=0;
break;

printf("nEnter your choice? n");
scanf("%d",&ch);

destroy(start);







if(loc<1 || loc>l) ... you assert that the loc can only be one, but then you have an if-else after this which checks the value of loc.
– Tim Biegeleisen
Aug 20 at 2:05


if(loc<1 || loc>l)


loc


loc





I am checking there for wrong indexing!
– unsensation_coder
Aug 20 at 2:07





I am checing whether the entered location is less than 1 and greater than length of linked list.
– unsensation_coder
Aug 20 at 2:08





You are re-checking the same condition you already tested to decide whether to go down that branch at all. Fix that and like code, and maybe you'll find your error.
– Deduplicator
Aug 20 at 2:11





You may also benefit by searching this site for "C linked list" there are many many examples. You can even click on the "linked-list" tag and then "Newest" tab for a number of recent examples.
– David C. Rankin
Aug 20 at 2:28





1 Answer
1



Change:


case 5:
printf("Enter the node you want to delete. n");
scanf("%d",temp);
start=del(start,temp);
break;



line scanf("%d",temp); to scanf("%d",&temp);


scanf("%d",temp);


scanf("%d",&temp);





accept the answer if you are happy with it
– Mateusz Wojtczak
Aug 20 at 3:39





done sir, have accepted the answer!
– unsensation_coder
Aug 20 at 3:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Help:Category

How can temperature be calculated given relative humidity and dew point?

I have a recursive function to validate tree graph and need a return condition