My delete node function does not delete the node in the linked list

This code does not delete the node as required.

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
 void removeElement(int remValue)
 {// to remove an element, we go through the list, find the value given // if we find it, stop // to remove, disconnect the link // relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
   LinkedList* current = head;
     LinkedList* next = current;
    while(current != NULL)
    { 
       if(current->value == remValue)
       { // if match
             break; // break out of while 
           }
         else
        {
            cout << "Value " <<  current->value << " does not match " << remValue << ".\n";
            next = current; // save in case
            current = current->pNextValue; 
            // go to next value 
           }
       }
  //    end while 
   if(current == NULL)
   { 
        // if we reached end of list
       cout << "Can't remove value: no match found.\n"; // no match, cant remove
       } 
  else
    { // found match
         cout << "Deleting: " << current << "\n";
       delete current; 
       current = next->pNextValue; // current is updated
       }
    }
Here is the complete program:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <ctime> // time
#include <cstdlib> // rand/NULL
#include <iostream> // cout/cin

using namespace std;


// our linked list structure, has one integer
struct LinkedList {
    int info; // our value
    LinkedList *link; // pointer to the next value
};
LinkedList *first = NULL; // global variable head (start of list)

// add value
LinkedList *addValue(int v) {
    LinkedList *newNode = new LinkedList; // allocate memory

    newNode->info = v; // set pointer to the random number
    newNode-> link = first;// point to previous first element in list
    first = newNode; // update so that our newest value is at the beginning of list
    return newNode; // return our list
}

// traverse through list and display
void displayList() {
    LinkedList *current = first; // setting current to first element in list
    int i = 1; // keeps track
    while(current != NULL) {
        cout << "Value #" << i << ": " << current->info << "\n";
        current = current->link; // go to next value
        i++; // increment i
    }
}

// remove an element from the linked list
void removeElement(int remValue) {
    // to remove an element, we go through the list, find the value given
    // if we find it, stop
    // to remove, disconnect the link
    // relink the two values now (ie. value 1->2->3->NULL, 2 is removed, 1->3->NULL )
    LinkedList *current = first;
    LinkedList *link = current;
    while(current != NULL) {
        if(current-> info == remValue) { // if match
            break; // break out of while
        }
        else {
            cout << "Value " << current->info << " does not match " << remValue << "\n";
            link = current; // save in case
            current = current->link; // go to next value
        }
    } // end while
    if(current == NULL) { // if we reached end of list
        cout << "Can't remove value: no match found.\n"; // no match, cant remove
    }
     else 
     { // found match
        cout << "Deleting: " << current << "\n";
        delete current;
        current = link->link; // current is updated
    }
}

// main
int main() 
    {
    LinkedList *listValue = addValue(3); // add a value to our linked list
    cout << "In memory, our list value is at: " << listValue << "\n"; // mem address
    cout << "Our actual value should be: " << listValue->info << "\n"; // actual value there
    cout << "Adding more values.\n";
    listValue = addValue(5);
    listValue = addValue(10);
    listValue = addValue(15);
    listValue = addValue(100);
    listValue = addValue(1732);
    cout << "Mem address for " << listValue->info << ": " << listValue << "\n";
    displayList();
    removeElement(5);
    displayList();
    cout << "\n";

}
1
2
3
4
5
     { // found match
        cout << "Deleting: " << current << "\n";
        delete current;
        current = link->link; // current is updated
    }

What is the point of updating the 'current' here? It is a local variable and the function ends.

If you have A->B->C and remove B, then you should update A (A->link = C)

What if you have to remove the first node from the One and Only list?
Topic archived. No new replies allowed.