Deleting middle node in Linked List

Hi guys/girls. I'm trying to delete a node from the end of a Linked List but I have some problems.
Every node has it's unique code.
Here is what I do:
1. Ask the user for the unique code of the node.
2. Ask him if he wants to change the data in it or delete the whole node.
3. If he chooses to delete it, I do this:
1
2
3
4
//let's say that temp1 points to the node
List *temp2 = temp1;
temp1 = temp1->next;
delete temp2;


And it's just not deleting the node. I'm trying to solve this problem all day but without result. Does anybody know where I have made a mistake?
You're not doing anything to remove a node in the list, you're just taking one node in the middle of the list and turning it into garbage.

Let's say for a general case that you have a list with at least 4 nodes in it. Say you want to take node 3 out of a list. You must first make node 2's "next" point to node 4 now. That's how you 'remove' a node from the list. Once you do that, you can delete node 3.


      ___    ___    ___
...->|_2_|->|_3_|->|_4_|->...

             ___
      ___   |_3_|   ___
...->|_2_|-------->|_4_|->...



Your code isn't doing anything to "re-link" the list.
Last edited on
Thanks! I didn't know that.
This is what I made:
1
2
3
4
5
6
7
8
9
10
List *temp2 = start_ptr;
              while(temp2->next != NULL)
                {
                  if(temp2->next->code == temp1->code)
                    {
                      temp2->next = temp1->next;  
                      //maybe I need to use delete temp1 here?
                    }          
                  temp2 = temp2->next;
                }


But should I use somewhere delete ?
Last edited on
Topic archived. No new replies allowed.