You are trying to do too many things at once, and getting overloaded.
In all computing, you need to break a problem down into the simplest tasks possible. Given your assignment, you can reword it this way:
1. Find the node containing the largest value in the list (traversing the list at most one time)
2. Remove (unlink and delete) the node found in step one.
To find the biggest node, you only need a few things. Here's a start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Node * find_largest_node( Node * head )
// Return the node in the list containing the largest value
// Returns NULL if head == NULL
{
Node * largest = head;
Node * current = head; // Not head->next; this way allows head to be NULL
while (current)
{
...
}
return largest;
}
|
Now you can use it to find the node containing the largest value. Once found, you can delete it. You should already have a function that deletes a node from your list. It should look something like this:
1 2 3 4 5 6 7 8 9 10 11
|
Node * delete_node( Node * head, Node * to_delete )
// Delete a node from the list
// Returns the new head of the list
// (which may change if to_delete == head)
{
if (head != NULL)
{
...
}
return head;
}
|
Your final code should then look something like this:
1 2 3 4 5 6 7 8
|
// Here is a list
Node * my_list = NULL;
// Populate the list here
...
// Delete the largest value in the list
my_list = delete_node( my_list, find_largest_node( my_list ) );
|
Once you have that working, there is one more (trivial) thing to consider about the
find_largest_node() function: if more than one node has the largest value, which is returned: the
first or the
last?
Hope this helps.
[edit] Fixed typos...