// If you perform a search, you expect to get the results back!
node* dl::search(int x)
{
// Traverses list and compares data to x.
// returns pointer to node with matching data,
// or NULL pointer if search failed
}
void dl::delnode(int x)
{
// ...
// Do not reimplement search inside delete!
node* toBeDeleted = this.search(x);
if (toBeDeleted != NULL )
{
// Unplug toBeDeleted from the chain and kill it.
// Update neighbouring nodes contents, and not just
// pointers to neighbouring nodes alone
toBeDeleted->prev->next = toBeDeleted->next;
toBeDeleted->next->prev = toBeDeleted->prev;
// What you did (lines 76-77):
// toBeDeleted->prev = toBeDeleted->next;
// toBeDeleted->next = toBeDeleted->prev;delete toBeDeleted;
}
}
You should also consider what delete should accept as an argument - it probably should not be an int (node data), because many nodes could have the same data stored. It should accept actual pointer to the node that needs deleting:
1 2 3 4 5
// Declared like this
void delnode(node* toBeDeleted) {/* ... */ };
// Called like this:
delnode( search(x) );