Gentlemen, help, I can not deal with the task.

It is required to remove the maximal element from the single-linked list. Here are two options for the function, none passes. Explain to me, cripple.

void delMaxValue(Stack **stck, int maxValue){ Stack *tmp = NULL; do { if ((*stck)->info != maxValue) tmp = *stck; cout << tmp->info << endl; tmp = tmp->next; *stck = (*stck)->next; } while ((*stck)->next != NULL); while (tmp != NULL) { *stck = tmp; *stck = (*stck)->next; tmp = tmp->next; } 

Another variant:

 Stack* deleteMaxValue(Stack *begin) { Stack *t = begin, *p = begin->next; for (; p; p = p->next) if (p->info > t->info) t = p; p = begin; if (p != t) { while (p->next != t) p = p->next; p->next = t->next; } else begin = t->next; delete t; return begin; 

}

Thank you in advance, lads

    1 answer 1

    Go through the list from the beginning to the end, storing, in addition to the maximum value, the previous (relative to the current) element. If the current value is greater than the maximum, remember the previous item. When the passage through the list is completed, connect the saved item before the maximum, so that after the maximum.

    Limit cases checks (maximum element - first or last, empty list, list from one element, etc.) are left as an exercise for the reader.

    • And if the first is the maximum, do you think the vehicle will guess? - avp
    • @avp I'm trying not to consider others as bad :). - Igor