I am trying to remove a node from a linked list, when given the value of the node to remove. Here is my code:
virtual void remove(T value) {
cout << "In remove" << endl;
if(value == my_list) {
Node *current = my_list;
my_list = my_list->next;
delete current;
}
else {
Node *ptr = my_list;
while (ptr != NULL) {
if(ptr == value) {
Node *current = ptr->next;
ptr->next = ptr->next->next;
delete current;
num_items--;
}
else{
ptr = ptr->next;
}
}
}
};
I keep receiving the error:
LinkedList.h:106:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if(value == my_list) {
~~~~~~^~~~~~~~~~
LinkedList.h:114:21: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if(ptr == value) {
I think my problem is "value" is the value of the node and not the position of the node, but I'm not sure how to change that.
EDITED: Here is the declaration of all the variables:
template <class T>
class LinkedList: public LinkedListInterface<T>
{
private:
struct Node {
T data;
Node* next;
Node(const T& the_data, Node* next_val = NULL) :
data(the_data) {next = next_val;}
};
Node *my_list;
int num_items;
public:
LinkedList(void) {
my_list = NULL;
num_items = 0;
cout << "In constructor" << endl;
};
virtual ~LinkedList(void) {
cout << "In deconstructor" << endl;
while(my_list != NULL) {
Node *current = my_list;
my_list = my_list->next;
delete current;
};
};