Hello Everyone,
The solution to this is to copy the data from the next node into this
node and delete the next node!.
1. But if the node to be deleted is the last node. Then what should we
do ?
2. If the list is Head node?
3 If the list is circular then what all conditions we need to check?
Thanks,
kaka
People have already pointed out various ways to do this, so I won't
further elaborate on them; what I will do is point out what's wrong
with the above.
In any program of significant size, you're liable to have other
pointers besides those pertaining to the linked list itself, pointing
to the "next node" (the one which your solution says to delete).
You'd have to painstakingly find every last thing which can point to a
node in your list, and handle it accordingly, or else after the "next"
node is deleted, you may have other pointers floating around still
pointing to it.
Example: your "nodes" are soldiers in a battlefield videogame. The
list is periodically run through to make all the soldiers do their
thing. But the battlefield also has machineguns, and each machinegun
actively targets a specific soldier-- which is handled by having the
machinegun structure include a "struct soldier_data *target." A
soldier dies, who was being targeted by a machinegun; the soldier is
deleted from the list in the method you described, and now the
machinegun's "target" pointer is pointing to undefined data. When it
comes time for the machinegun to shoot, your program crashes (in best
case scenario- or does even worse things, possibly).