I was wondering if p->next->prev = p->prev; part is the same as saying p = p->prev
No, it is not. It sets the prev field to the next node, which follows the p node in the list.
The code removes the p node from the list. The two surrounding nodes on either side of the p node must be updated to stop pointing to the p node and instead point to each other. You showed only half the required update. You need to add the other half: if (p->prev != NULL) p->prev->next = p->next; . You also need to check if p points to the head of the list node, and if so, then update the head to p->next instead. Similarly, with the tail of the list node, if any, specify in p->prev .
In addition, if(p=NULL) in the code is incorrect, it must be if(p==NULL) . And if(p->next==NULL) in your code is also incorrect, it should be if(p->next!=NULL) .
Here is the correct implementation:
template <class X> bool linkedList<X>::deleteElement(node<X> *p) { if (p == NULL) return false; if (p->next != NULL) p->next->prev = p->prev; if (p->prev != NULL) p->prev->next = p->next; if (p == head) head = p->next;
And finally, you should consider using the STL std::list container instead of a manual implementation.
source share