Query related to link list

D

deba

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
 
T

Tejas Kokje

deba said:
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
 
B

Barry Schwarz

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

If the node you currently point to is not last, then you can copy the
next node into your node (and delete that one). This has the logical
effect of deleting the node you are pointing to but not the physical
effect (you freed a different block of memory).

If you are pointing to the last node of the list, you must somehow
have access to the next-to-last node so you can reset its "next"
pointer.


Remove del for email
 
J

Joe Wright

deba said:
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
You need to know 'here', (the current node), 'prev' (the previous node)
and 'next' (here->next).

To remove 'here' from the list logically,

prev->next = here->next;

then or course to remove it physically..

free(here), here = prev;
 
P

Peter 'Shaggy' Haywood

If the node you currently point to is not last, then you can copy the next
node into your node (and delete that one). This has the logical effect of
deleting the node you are pointing to but not the physical effect (you
freed a different block of memory).

If you are pointing to the last node of the list, you must somehow have
access to the next-to-last node so you can reset its "next" pointer.

Of course, it's different (simpler) for double linked lists. Since the
current node has pointers not only to the next node but also the previous
one, you simply set the next node's "previous" pointer to point at the
previous node (or set it to NULL, adjusting the "head" pointer to point at
the next node, if there is no previous node), set the previous node's
"next" pointer to point at the next node (or set it to NULL, adjusting the
"tail" pointer to point at the previous node, if there is no next node),
then remove the current node by setting its "next" and "previous" pointers
to NULL.

--
Dig the sig!



----------- Peter 'Shaggy' Haywood ------------

Ain't I'm a dawg!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top