ok, you have a couple of issues. Also, please use code tags next time.
1 2 3 4 5 6
|
if (p->key > max->key)
{
max->key = p->key;
}
}
delete max;
| |
ok, if the list is
1,10,2,3,4
lets do it:)
first is 1, max is 1, blah blah
p is next, so p is 10
if(p value > max value) // it is
max-> key = p-> key //oops
ok so max is first. the list therefore is now
10,10,2,3,4 and your 1 went poof.
second, you ignore the linked listedness of the list.
if you have
1,10,2,3,4
you want your list to be, after delete max,
1,2,3,4
so you have to somehow say that
1-> next = 2
after deleting the 10. there are 3 cases here:
its the first one, in which case you can shuffle it but its easier to just assign
via tmp = first, first = first-> next, delete tmp type logic.
second, its the last one, in which case you can set the next to last one's next to null, decrement your counters, and as before use a temp to delete the last one.
and finally in the middle, you need to connect the previous node's next to the one after what is deleted.
you can simplify that in the code though, by exploiting null. If you want to do the pointer shuffles on last, for example, its fine. next to last = last-> next which is null, so next to last -> next becomes null automatically: its the same logic as in the middle of the list! That leaves just a special case for deleting the first one, which can reuse your pop function to make that super simple!
fix those 2 ideas (reconnecting in the middle & logic for first and assigning the key when you should not have) and you will get it; its getting close.
bonus points, you can greatly simplify all the logic but get it working as-is before you take that on.