How can I delete a node from anywhere in a linked list?

I know how to remove a node from the front of a singly linked list, but I have no idea how to remove one from any arbitrary location.

In my specific problem, I continue to pass an integer value x into each node in the linked list. I can add and delete, but there is a function I need to write where if I've identified two x's that are the same, I delete the other instance.



You can crawl your list, find the previous node, and have that previous node connect to the next node, then delete the current node. It's a bit easier in a doubly linked list because you can simply connect the nodes together then perish.
closed account (48T7M4Gy)
Move to the particular node and amend the links to suit and then delete it.

I'd help with the second paragraph of yours if I understood it especially because your code about it is currently a mystery.

Thanks for the response, I'll go ahead and try that real quick and report back
This is what I've been trying to do so far


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void Singly_Linked_List :: add_unique_age_to_the_front(int x)
{
	//Insert a new age to the front of the singly linked list

	Node* addUnique=head;

	if(!test_age_contained_in_singly_linked_list(x))  //Call the test age function for a case where it is not true no matter what
		{
			
		    Node* newNode2;
			newNode2= new Node(x);
			newNode2->next=head;
		     head=newNode2; 
		
		}
		
	
}

void Singly_Linked_List :: remove_age_from_singly_linked_list(int x)
{
    Node* removeAge=head;
	Node* prevNode=NULL;

	while(removeAge!=NULL)
	{
		if(test_age_contained_in_singly_linked_list(x))
		{
			prevNode->next=removeAge->next;
			delete removeAge;
		}

		prevNode=removeAge;
		removeAge=removeAge->next;

	}

}

For some reason, related to my dynamic memory allocation, I get an unhandled exception error on line 29.
closed account (48T7M4Gy)
Please show us main() etc plus everything in one listing so we can run it and replicate the error.
Based on add_unique_age_to_the_front(), it looks like test_age_contained_in_singly_linked_list() returns true if the argument is anywhere in the list. But that isn't what you want to test in remove_age_from_singly_linked_list(). You want to remove a specific node if that node has the age.

Your error at line 29 occurs because test_age_contaned_in_singly_lionked_list() returns true on the first call, but in that case, prevNode is NULL, so prevNode->next generates a memory error.

When you remove the node, don't forget to delete it so you don't leak the memory.
Thank you so much! That makes alot of sense. How can I fix this error? Do I need to change my test_age function?
Change line 26 to check whether removeAge contains the age x. Since you haven't shown the declaration of class Node, I can't say exactly how to do this.

Then you'll have to deal with the possbility that prevNode is NULL. In that case you're removing from the head of the list.
Change line 27 to check whether removeAge is the age in question.

Also deal with the case where prevNode==NULL. That means that you're removing from the head of the list.
Topic archived. No new replies allowed.