Hello,
I've written a delete function to delete any integer value based on in parameter. The problem I have now is that this function deletes the values, but leaves the Node position intact with some huge int value instead.

So my question is, do I need to rearrange the Node, or why doesn't a simply delete works?

e.g. the node contains
1, 2, 3, 4, 5
and I delete '4' output becomes
1, 2, 3, 4130880, 5

void Set::del (int value) {
    Node *temp = header;

    while(temp->next != 0 && temp->value != value) {
        temp = temp->next;
    }
    if(temp->value == value) {
        delete temp;
    }
}

Recommended Answers

All 3 Replies

you need to adjust the pointers next pointers too. such as temp->previous->next = temp-->next; . If your linked list does not have a previous pointer, then you need to keep track of it while iterating through the list in lines 4 and 5 of the code you posted.

hi again,

I can not quite figure out how to fix it. The furthest I got to was to delete first node and make so it does not appear in the output

which I used this if-statement to do

if(value == 1) {
        previous = header;
        header = header->next;
        delete (previous);
    }

tried to understand the explanation from this webpage also, but it did not made me any smarter.

I have an ugly solution now which is to make integer larger than 1000 disappear in output. But yeah, it still exist in the linked-list T_T Is it possible for you to give me an example how it can be done?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.