473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Delete node from singly linked list when header is not known

Raj
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

Jan 30 '06 #1
13 6522

"Raj" <ra********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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);
Jan 30 '06 #2
Raj
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned

Jan 30 '06 #3

"Raj" <ra********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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.
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".

Jan 30 '06 #4

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;

Jan 30 '06 #5

"Tosha" <to*******@gmail.com> wrote in message
news:dr**********@ss405.t-com.hr...

"Raj" <ra********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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
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
Jan 30 '06 #6
"Howard" <al*****@hotmail.com> wrote in message
news:Uc*********************@bgtnsc04-news.ops.worldnet.att.net...

"Tosha" <to*******@gmail.com> wrote in message
news:dr**********@ss405.t-com.hr...

"Raj" <ra********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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.
Jan 30 '06 #7

"Tosha" <to*******@gmail.com> wrote in message
news:dr**********@ss405.t-com.hr...
"Howard" <al*****@hotmail.com> wrote in message
news:Uc*********************@bgtnsc04-news.ops.worldnet.att.net...

"Tosha" <to*******@gmail.com> wrote in message
news:dr**********@ss405.t-com.hr...

"Raj" <ra********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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


Jan 30 '06 #8

"Jaspreet" <js***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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


Jan 30 '06 #9

Jaspreet wrote:
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?
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

Jan 31 '06 #10
Raj
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??

Feb 1 '06 #11
Raj wrote:
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...
Feb 1 '06 #12

"Raj" <ra********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Feb 1 '06 #13
Raj
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...

Feb 2 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Andrew Edwards | last post by:
The following function results in an infinite loop! After 5+ hours of debugging, I am still unable to decipher what I am incorrectly. Any assistance is greatly appreciated. Thanks in advance,...
16
by: sangram | last post by:
how to delete last node of a Linked list if you only know the address of last node. thanks sangram
10
by: ac.c.2k7 | last post by:
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 ?...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
10
by: yfredmann | last post by:
Hi dear friends, This problem might be so popular to some of you, but I couldn't be able to do it myself. The problem: How to delete a node from a single linked list using only one pointer? ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.