Search code examples
c++arraysdynamicdynamic-list

Removing element from dynamic list


i have a dynamic list, where a is the current spot, a-> prev - previous element, a-> next - the next element, I need to delete one element of the list (and set the previous/next of the adjacent elements to one another)

if(a->va == var && a->pa == pav){
        a -> prev -> next = a -> next;
        a -> next -> prev = a -> prev;
        delete a;
    }

Solution

  • You have to handle the edge cases.

    If the found element is the first one in the list, a -> prev is NULL.

    Similarly, if it's the last, a -> next is NULL.