How do I remove an interior node from a singly linked list? I am using a node class with a double data member and a list class.
How do I remove an interior node from a singly linked list? I am using a node class with a double data member and a list class.
"In theory, there is no difference between theory and practice. But, in practice, there is."
- Jan L.A. van de Snepscheut
I think this should work.
Code://If s->Next is the Node that you want to delete then do this Node* temp; temp = s->Next; s->Next = s->Next->Next; delete temp;
but what if the node you want to remove is s? how do i access the previous->next that points to s, and switch that to s->next?
"In theory, there is no difference between theory and practice. But, in practice, there is."
- Jan L.A. van de Snepscheut
What you do is iterate through the list until s->Next is the Node that you wanna delete, that's all.
Note: This will only work if you have a head node that you don't use.
//Edit: Here's an example
Code:while( s->Next != NULL ) { if( s->Next->data == valueToDelete ) { Node* temp; temp = s->Next; s->Next = s->Next->Next; delete temp; break; } s = s->Next; //Forgot this }
Last edited by The Dog; 10-25-2003 at 02:29 PM.