I need help deleting from linked list, with the code i have below it gives me some type of error when i try to delete an account.
Printable View
I need help deleting from linked list, with the code i have below it gives me some type of error when i try to delete an account.
sorry. my code is
Code:
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance)))
{
printf("%s %s %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printf("%s",prev->socialnum);
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
return 0;
}
}
>it gives me some type of error when i try to delete an account
Care to tell us the error? At a glance I can see that your braces don't match up, but that could just be lazy formatting or a bad cut and paste, or a combination thereof. As much as I love debugging "What's the problem" questions, a little direction is useful.
-Prelude
it gives me a segmentation fault as soon as it reads the first customer who tries to make a withdrawal from his account but does not have enough money. so as soon as it gets to my last if statement i guess.
You're probably going too far into the list, step through your code and search for an off by one error where the pointer is NULL yet you still try to do something with it.
-Prelude
yea i found that when i take away my pointer it prints fine but then its not deleting the record. any idea why my pointer *prev is always NULL?
I imagine that somewhere in your code you want to do this:
prev = current;
current = prev->next;
Yet you aren't, you want to assign the value of current to prev just before you reassign current to the next node. If prev is NULL then you are failing to do that somewhere.
-Prelude
if the record im trying to delete is the head how do i delete the head. bankrecords is the head by the way so far i have this:
Code:
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr == *bankrecords) {
prev=tempptr->next;
free(tempptr);
printlist(bankrecords);
}
else {
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
}
return 0;
}
Is 'head' a global? If so, it's easy. Otherwise, you have to pass a pointer to a pointer.
BTW, since you've posted about 40,000 times with this problem, shouldn't you register? Or not. Whatever.Code:if( to_delete == head )
{
temp = head;
head = head->next;
deleteNode( temp );
}
Quzah.
no its not global its just in the function im using