I got a problem to delete the last node, what might be the problem?
Code:typedef struct node { char data; int count; node *next; node *pre; } NODE;
I got a problem to delete the last node, what might be the problem?
Code:typedef struct node { char data; int count; node *next; node *pre; } NODE;
In C++, you don't need the extra typedef and NODE:Your problem is probably in the code that tries to do the delete that you haven't posted. If you haven't written it yet, just start from the beginning node and move on to the next node until the next node is null, then you have found the last node.Code:struct node { char data; int count; node *next; node *pre; };
Here's the remove function, each node can be deleted without a problem except the last one.
Code:void delete(char the_node) { NODE *target; if((target=find(the_node))!=NULL) { if(target->next->count>1) target->next->count--; else { NODE* temp; temp=target->next; target->next=temp->next; if (target->next->next != NULL) target->next->next->pre=target; if(temp==head) { head=temp->next; temp->next->pre = NULL; } delete(temp); } howMany--; } }
>> if(target->next->count>1)
If the last one is the one you are going to delete, won't target->next be null? In both your if and your else, you seem to assume that target->next is valid. Try checking for null and handle that case.