c++ - Linked list problems with node deletion function -
i'm working on linked list stores integers @ front end of list , odd integers @ back. working fine me, except deletion function, whereby takes user input on whether want delete or odd integer, , uses prompt either delete front end or of list. here code have node deletion function:
void staque::pop(char evenorodd) { if (!empty()) { if (evenorodd == 'o' || 'o') { //creating pointers find end of staque staque::nodepointer prevptr = mytop, //pointer find penultimate node , set "next" value 0 currentptr = mytop; //pointer find node delete //assigning pointers corresponding nodes while (prevptr->next->next != 0) { prevptr = prevptr->next; } while (currentptr->next != 0) { currentptr = currentptr->next; } //deleting lastly node in staque , setting "next" value of new end 0 delete currentptr; prevptr->next = 0; } else if (evenorodd == 'e' || 'e') { staque::nodepointer ptr = mytop; mytop = mytop->next; delete ptr; } } else { cerr << "stack empty -- can't remove value \n"; }
the problem doesn't take input on deciding kind of element delete. deletes whatever set first in if build of node deletion function. if, in if construct, have code deleting evens above code deleting odds, it'll delete evens, , vice versa. please help, can't figure out why it's doing this.
change
if (evenorodd == 'o' || 'o')
to
if (evenorodd == 'o' || evenorodd == 'o')
similar one, i.e. else if (evenorodd == 'e' || 'e')
.
note that,
if (evenorodd == 'o' || 'o')
this same to
if (evenorodd == 'o' || (bool)'o')
further to
if (evenorodd == 'o' || true)
further to
if (true)
c++ linked-list nodes delete-operator
No comments:
Post a Comment