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 13 6555
"Raj" <ra********@gma il.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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);
But I want to retain the list. I would be losing the link with the
previous node if I do wats mentioned
"Raj" <ra********@gma il.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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".
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;
"Tosha" <to*******@gmai l.com> wrote in message
news:dr******** **@ss405.t-com.hr... "Raj" <ra********@gma il.com> wrote in message news:11******** *************@g 14g2000cwa.goog legroups.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
"Howard" <al*****@hotmai l.com> wrote in message
news:Uc******** *************@b gtnsc04-news.ops.worldn et.att.net... "Tosha" <to*******@gmai l.com> wrote in message news:dr******** **@ss405.t-com.hr... "Raj" <ra********@gma il.com> wrote in message news:11******** *************@g 14g2000cwa.goog legroups.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.
"Tosha" <to*******@gmai l.com> wrote in message
news:dr******** **@ss405.t-com.hr... "Howard" <al*****@hotmai l.com> wrote in message news:Uc******** *************@b gtnsc04-news.ops.worldn et.att.net... "Tosha" <to*******@gmai l.com> wrote in message news:dr******** **@ss405.t-com.hr... "Raj" <ra********@gma il.com> wrote in message news:11******** *************@g 14g2000cwa.goog legroups.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
"Jaspreet" <js***********@ gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
Andrew
==========>Code<==========
//--------------------------------------------------------------------
|
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
|
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 ?
2. If the list is Head node?
3 If the list is circular then what all conditions we need to check?
Thanks,
|
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 between then what is the best
solution.
|
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?
Where the target node to be deleted has a key field, and based on a match, let us say at first a sequential search, this node has to be deleted.
What other ways that a singly linked list can be searched through other than the sequential search,...
| |
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |