I had to create a function, that deletes nodes that have a greater element to the right. I achieved this by creating 2 functions: 1st function reverses a list 2nd function that deletes nodes, that have a lesser value than the current node.
In order for this to work I reverse a list, call the 2nd function and reverse the list back. My problem is: How can I combine these 2 functions together, so I can, for example call the reverse function inside the lesVal function without needing to call it separately.
void reverse(Elem** listHead){
Elem* next;
Elem* curr = *listHead;
Elem* temp = NULL;
while (curr != NULL){
next = curr->next;
curr->next = temp;
temp = curr;
curr = next;
}
*listHead = temp;
}
void lesVal(Elem* listHead){
Elem* temp;
Elem* curr = listHead;
Elem* compare = listHead;
while (curr != NULL && curr->next != NULL){
if (curr->next->number < compare->number){
temp = curr->next;
curr->next = temp->next;
delete temp;
}
else{
curr = curr->next;
compare = curr;
}
}
}
reverse(&first);
lesVal(first);
reverse(&first);