C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Delete node from singly linked list when header is not known

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Raj
Guest





PostPosted: Mon Jan 30, 2006 10:00 am    Post subject: Delete node from singly linked list when header is not known Reply with quote



Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted
Back to top
Raj
Guest





PostPosted: Mon Jan 30, 2006 10:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote



But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned
Back to top
Tosha
Guest





PostPosted: Mon Jan 30, 2006 10:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote



"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138613016.129170.56910 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted


type *temp = ptr;
ptr = ptr->next;
free(temp);
Back to top
Tosha
Guest





PostPosted: Mon Jan 30, 2006 11:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138614678.060715.40600 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned

No you would not, because the the pointer you have has the adress of the

node you want to delete and value of previous_node->next at the same time
(that's is the same thing), and when you say "ptr = ptr->next" you change
the value of previous_node->next to point to previous_node->next->next or
ptr->next.
I don't know if you have double indirection connection with list or single,
because if you have single, than it's only possible to delete node in front
of pointed node, like "ptr->next = ptr->next->next".
Back to top
Jaspreet
Guest





PostPosted: Mon Jan 30, 2006 11:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

Raj wrote:
Quote:
Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted

This one's a popular interview question. Just copy the value of the
next node to the current node and delete the next node. So, it would
be something like :

node *tmp = NULL;
tmp = ptr->next;
ptr->value = tmp->value;
ptr->next=tmp->next;
delete tmp;
Back to top
Howard
Guest





PostPosted: Mon Jan 30, 2006 4:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Tosha" <tosha.dev (AT) gmail (DOT) com> wrote in message
news:drkp4k$iub$1 (AT) ss405 (DOT) t-com.hr...
Quote:

"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138614678.060715.40600 (AT) g14g2000cwa (DOT) googlegroups.com...
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned

No you would not, because the the pointer you have has the adress of the
node you want to delete and value of previous_node->next at the same time
(that's is the same thing), and when you say "ptr = ptr->next" you change
the value of previous_node->next to point to previous_node->next->next or
ptr->next.

What? When you change the pointer variable ptr, which currently points to
the node to be deleted, in what way does that change the value of the
previous node's "next" member? Just because the current values of those
pointers are the same, does not mean they're the same pointers! After
assigning a new value to ptr, ptr now points to the next node, but the
previous node's next pointer is unchanged! After deleting the current node,
it becomes an invalid pointer.

The correct method is given by Jaspreet (although the first two lines of
that example might as well be combined into one.)

-Howard

Quote:
I don't know if you have double indirection connection with list or
single, because if you have single, than it's only possible to delete node
in front of pointed node, like "ptr->next = ptr->next->next".


I don't know what "double indirection" means in this context, but the
requirement was for a "singly linked list". To me, "double indirection"
means a pointer-to-a-pointer, as in node**.

-Howard
Back to top
Tosha
Guest





PostPosted: Mon Jan 30, 2006 6:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:UcqDf.571910$zb5.456275@bgtnsc04-news.ops.worldnet.att.net...
Quote:

"Tosha" <tosha.dev (AT) gmail (DOT) com> wrote in message
news:drkp4k$iub$1 (AT) ss405 (DOT) t-com.hr...

"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138614678.060715.40600 (AT) g14g2000cwa (DOT) googlegroups.com...
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned

No you would not, because the the pointer you have has the adress of the
node you want to delete and value of previous_node->next at the same time
(that's is the same thing), and when you say "ptr = ptr->next" you change
the value of previous_node->next to point to previous_node->next->next or
ptr->next.

What? When you change the pointer variable ptr, which currently points to
the node to be deleted, in what way does that change the value of the
previous node's "next" member? Just because the current values of those
pointers are the same, does not mean they're the same pointers! After
assigning a new value to ptr, ptr now points to the next node, but the
previous node's next pointer is unchanged! After deleting the current
node, it becomes an invalid pointer.

I meant if you have adress of previous->next or to say adress of original
ptr value holder.
Back to top
Howard
Guest





PostPosted: Mon Jan 30, 2006 7:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Tosha" <tosha.dev (AT) gmail (DOT) com> wrote in message
news:drli67$o6k$1 (AT) ss405 (DOT) t-com.hr...
Quote:
"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:UcqDf.571910$zb5.456275@bgtnsc04-news.ops.worldnet.att.net...

"Tosha" <tosha.dev (AT) gmail (DOT) com> wrote in message
news:drkp4k$iub$1 (AT) ss405 (DOT) t-com.hr...

"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138614678.060715.40600 (AT) g14g2000cwa (DOT) googlegroups.com...
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned

No you would not, because the the pointer you have has the adress of the
node you want to delete and value of previous_node->next at the same
time (that's is the same thing), and when you say "ptr = ptr->next" you
change the value of previous_node->next to point to
previous_node->next->next or ptr->next.

What? When you change the pointer variable ptr, which currently points
to the node to be deleted, in what way does that change the value of the
previous node's "next" member? Just because the current values of those
pointers are the same, does not mean they're the same pointers! After
assigning a new value to ptr, ptr now points to the next node, but the
previous node's next pointer is unchanged! After deleting the current
node, it becomes an invalid pointer.

I meant if you have adress of previous->next or to say adress of original
ptr value holder.


The original post said that the ONLY thing we had was a pointer to the node
to be deleted. Nothing in the original post suggested that the address of
the previous node's next pointer was known,or even if there IS a previous
node. (The node to be deleted might be the head of the list, after all.)

The only way your code would affect the previous node's next pointer would
be if you were given the pointer to the current node as a [non-const]
reference to the previous node's next pointer, or if you were operating
directly on that pointer, neither of which is suggested by the original
post.

-Howard
Back to top
Howard
Guest





PostPosted: Mon Jan 30, 2006 7:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Jaspreet" <jsingh.oberoi (AT) gmail (DOT) com> wrote in message
news:1138617835.633515.223180 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:

Raj wrote:
Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted

This one's a popular interview question. Just copy the value of the
next node to the current node and delete the next node. So, it would
be something like :

node *tmp = NULL;
tmp = ptr->next;
ptr->value = tmp->value;
ptr->next=tmp->next;
delete tmp;


[re-posting, because previous try doesn't seem to have gone through...]

Two things to note in your example:

1) (minor point): Might as well combine the frist two lines. No point in
assigning NULL to the pointer and then assigning a value to it. Just
declare and initialize in one statement.
2) (major point): Before using tmp to assign the next and value members, you
need to test that tmp is not NULL. Dereferencing a NULL pointer constitutes
"undefined behavior". And if the current node is at the end of the list
(assuming it's not circular), then its "next" pointer would be NULL (and so
tmp would also be NULL).

-Howard
Back to top
Guest






PostPosted: Tue Jan 31, 2006 12:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

Jaspreet wrote:
Quote:
Raj wrote:
Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted

This one's a popular interview question. Just copy the value of the
next node to the current node and delete the next node. So, it would
be something like :

And if there's no next node? How do you delete the last node?

Quote:
node *tmp = NULL;
tmp = ptr->next;
ptr->value = tmp->value;
ptr->next=tmp->next;
delete tmp;

You're writing to tmp twice. First you set it to 0, then to ptr->next.
The first statement is obviously useless.
Worse, you don't check for ptr->next==0.

HTH,
Michiel Salters
Back to top
Raj
Guest





PostPosted: Wed Feb 01, 2006 6:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

Thanks all of u for the replies....
The suggestion is working out....But as Howard said the logic does not
work if the node to be deleted is the last one.
Any solutions??
Back to top
Ben Pope
Guest





PostPosted: Wed Feb 01, 2006 1:00 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

Raj wrote:
Quote:
Thanks all of u for the replies....
The suggestion is working out....But as Howard said the logic does not
work if the node to be deleted is the last one.
Any solutions??

Perhaps the tail must be a special case. Some kind of "null" node which
is copyable and always points to null.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Back to top
Howard
Guest





PostPosted: Wed Feb 01, 2006 4:01 pm    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

"Raj" <raj.reshmi (AT) gmail (DOT) com> wrote in message
news:1138772801.320705.142540 (AT) g47g2000cwa (DOT) googlegroups.com...
Quote:
Thanks all of u for the replies....
The suggestion is working out....But as Howard said the logic does not
work if the node to be deleted is the last one.
Any solutions??


Nope. If the node to be deleted is the last one, then you're stuck. You
should not delete it, because then you may at some point access it illegally
through the next pointer of the previous node. In this case, I'd report an
error.

Another problem would be if the node were the first node in the list. Then,
deleting it would invalidate the actual pointer to the head of the list.
(Which you say you don't have... but I'm assuming this is just for the sake
of this "puzzle", since any "real" program would certainly have to retain
the pointer to the head of the list!)

There is _no_way_, using a singly-linked list, and not having a pointer to
the head of that list, to "properly" delete the given node from that list.
You _can_ do like the example, making sure to check for NULL, of course, but
you'd need to throw an error in that case. And, you'd need to tell whoever
was supposedly using this method (your teacher? job interviewer?) that if
this _were_ by chance the head node, then any existing pointers to the head
node would no longer be valid.

By the way, when given questions in interviews (if that's the kind of
question this is), it's not always required that you come up with a perfect
solution. Sometimes, what the interviewer wants is to hear your analysis of
the problem. These kind of problems with the proposed solution are exactly
the kind of things you need to be able to consider for yourself when
designing real-world solutions. The more you're able to demonstrate your
analytical abilities, the more likely you'll get the job.

Of course, if this is just a puzzle posited by a friend or something, then
the answer is that it's simply not possible to do correctly in all cases.
Some, but not all.

-Howard
Back to top
Raj
Guest





PostPosted: Thu Feb 02, 2006 7:00 am    Post subject: Re: Delete node from singly linked list when header is not k Reply with quote

Ok..fine
Deleting header wont be a probelm bcos if we knew it was header
the qusetion itself would have been irrelevant

Thanks a lot Howard...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.