Here is a part of code for deleting an element from the tail a singly Linked List:
int SLList::deleteFromTail()
{
int el = tail->info;
//if the list has only one element
if(head == tail) {
delete head;
head = tail = 0;
}
else {
//some code here...
}
return el
}
Here head and tail are pointers to first and last element of LL, respectively.
In the if block above after delete head we are setting head = tail = 0.
But after we have deleted head, how can we set it’s value to something? (NULL in this case)
Head is a
pointer. You are deleting anobjectpointed by thepointer, not thepointeritselfConsider this example:
EDIT
A pointer is just an adress ob an object. When you write
delete headyou delete the object pointed by head, but even after deletionhead pointerwill point to the same place as before. But dereferencing it (e.g.*head) will cause problems.