Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Linked Lists: Deleting a general number

$
0
0
'Morning, to all of you :)

Today I'm trying to implement the very basics functions to manage linked lists.
Anyway, I don't know why the function that should delete a general number N from the list doesn't work.
Let's suppose the list is ordered ( we'll delete just the first match of "number" ).
Here's how I wrote it down

void deleteN( node * root_ptr, int number )
{
    node * node_to_delete_ptr = search( root_ptr, number );
    // search( a, n ) is a function that returns the pointer to the node that contains the first number n found
    // it works!

    if ( node_to_delete_ptr != NULL )
    {
        // the elements we have to find exists
        // save where the node we have to delete is linked
        node * swap = node_to_delete_ptr -> next;
        // delete the node that contains the element
        delete node_to_delete_ptr;
        // link the first node to the next element of the list
        node_to_delete_ptr = swap;
    }
}



And here's a pic about why I think it should work :D
Note: * are pointers, 3* is a direct consequence of the step n.3
EDIT: The arrow n.1 should start from the pointer, not from the node, sorry.
Posted Image

Output:
If I try to delete the last element of the list, the list remain the same
If I try to delete a numberthat is inside the list it prints me out an infinite sequence of big number:
Posted Image

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>