I have a linked list like this
struct product {
string ID;
double quantity;
product* next = NULL;
};
And I want to delete all products with quantity less than a number. This function returns true if at least one product are deleted, otherwise returns false. Here is my code
bool deleteProducts(product*& pHead, double quantity) {
static int flag = 0;
product* pTemp = pHead;
product* prev = pHead;
if (pTemp != NULL && pTemp->quantity <= quantity) pHead = pTemp->next;
while (pTemp != NULL && pTemp->quantity > quantity) {
prev = pTemp;
pTemp = pTemp->next;
}
if (pTemp == NULL) return flag;
flag = 1;
prev->next = pTemp->next;
deleteProducts(prev->next, quantity);
}
But when I have a list (quantities only) like this:
7 -> 7.5 -> 2 -> 5 -> 6
And I run the function with quantity = 10, it return:
7.5 -> 5
It's not true, can anybody explain for me. Thanks in advance!
flagstatic? Once you have deleted one node, the function will always returntrue.static, so it's only initialised once, which is the first time the function is called.