 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
deba Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Query related to link list |
|
|
Hi
i want to know is it possible to delete the node where my
pointer currently pointing without knowing the head node address.Dis
question ask to me in an interview.
I really want to know the soloution of it.Thanx |
|
| Back to top |
|
 |
Tejas Kokje Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: Query related to link list |
|
|
deba wrote:
| Quote: | Hi
i want to know is it possible to delete the node where my
pointer currently pointing without knowing the head node address.Dis
question ask to me in an interview.
I really want to know the soloution of it.Thanx
|
Here is the pseudocode
int node_delete(node_t **node) {
if (!node || !*node)
return -1;
//if I am last node, then free and set myself to NULL.
if (*node->next == NULL) {
free(*node);
*node = NULL;
}
else {
//if there is a node after me, copy its contents to me and free that next
//node
*node->data = *node->next->data;
*node->next = *node->next->next;
free(*node->next);
}
return 0;
}
Hope this helps.
Tejas Kokje |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|