|
Syntax: delete PointerVariable; |
|
|
The predecessor is needed to make the neccesary linkage !
p1->next = p2->next;
|
|
delete p2;
|
|
p2 points to the first element in the list
Although there is no element before p2 , we can look at head as the element p1 - because it points to p2
|
ListElem *p2;
if (head != NULL ) // Make sure list is not empty
{
p2 = head; // p2 points to element to delete
head = p2->next; // Make p1 (head) point to p2's successor
delete p2; // De-allocate memory !!
}
|
The delete element at head is a well-defined task
|
|
NOTE: again, there are 2 ways to return the head value:
|
ListElem * DeleteAtHead(ListElem * head)
{
ListElem *p2;
if (head == NULL ) // If list is empty, return(NULL)
return(NULL);
p2 = head; // Delete p2
head = p2->next;
delete p2; // Return memory !!
return(head); // Return new head
}
|
int main(int argc, char *argv[])
{
ListElem *head, *p;
head = NULL; // Empty list
p = new ListElem;
p->value = 1500;
head = DeleteAtHead(head); // <--- record the new head in "head"
}
|
void DeleteAtHead(ListElem * & head)
{
ListElem *p2;
if (head == NULL ) // If list is empty, return(NULL)
return;
p2 = head; // Delete p2
head = p2->next; // Unlink AND return new head
delete p2; // Return memory !!
}
|
int main(int argc, char *argv[])
{
ListElem *head, *p;
head = NULL; // Empty list
p = new ListElem;
p->value = 1500;
DeleteAtHead(head); // <--- record the new head in "head"
}
|
|
p2 points to the last element in the list
p1 points to the last-but-one element in the list
In the delete operation , you must also find the element before the one you want to delete
p1 = head; // First element in list
p2 = head->next; // Second element in list
while ( p2->next != NULL )
{
p1 = p2;
p2 = p2->next;
}
|
Pictorially:
| |
When while loop ends:
| |
p1->next = NULL; delete p2; // De-allocate memory |
Pictorially:
|
|
You will notice that head is not changed by the deletion
|
the list has only one element in it...
After deletion of the (only) element, the list becomes empty:
|
ListElem *p2;
ListElem *p1;
if ( head != NULL ) // Delete only from non-empty list
{
if ( head->next == NULL )
{ // Handle special case: List has 1 element
p2 = head; // Delete this element
head = NULL;
delete p2; // Deallocate space
}
else
{ // The general case
// Find the last-but-one "p1" and last "p2" elements
p1 = head;
p2 = head->next;
while ( p2->next != NULL )
{
p1 = p2;
p2 = p2->next;
}
// Delete element following the "p1" element
p1->next = NULL;
delete p2; // Deallocate space
}
}
|
|
|
NOTE: again, there are 2 ways to return the head value:
|
ListElem * DeleteAtTail(ListElem * head)
{
ListElem *p2;
ListElem *p1;
if ( head == NULL ) // Delete only from non-empty list
{
return;
}
else
{
if ( head->next == NULL )
{ // Handle special case: List has 1 element
delete head; // Deallocate space
return(NULL);
}
else
{ // The general case
// Find the last-but-one "p1" and last "p2" elements
p1 = head;
p2 = head->next;
while ( p2->next != NULL )
{
p1 = p2;
p2 = p2->next;
}
p1->next = NULL; // Delete element following list
delete p2; // Deallocate space
return(head);
}
}
}
|
int main(int argc, char *argv[])
{
ListElem *head, *p;
head = NULL; // Empty list
p = new ListElem;
p->value = 1500;
head = DeleteAtTail(head); // <--- record the new head in "head"
}
|
void DeleteAtTail(ListElem * & head)
{
ListElem *p2;
ListElem *p1;
if ( head == NULL ) // Delete only from non-empty list
{
return;
}
else
{
if ( head->next == NULL )
{ // Handle special case: List has 1 element
delete head; // Deallocate space
head = NULL; // Return new head in "head"
}
else
{ // The general case
// Find the last-but-one "p1" and last "p2" elements
p1 = head;
p2 = head->next;
while ( p2->next != NULL )
{
p1 = p2;
p2 = p2->next;
}
// Delete element following the "p1" element
p1->next = NULL;
delete p2; // Deallocate space
// head = head; <---- head is unchanged...
}
}
}
|
int main(int argc, char *argv[])
{
ListElem *head, *p;
head = NULL; // Empty list
p = new ListElem;
p->value = 1500;
DeleteAtTail(head); // <--- record the new head in "head"
}
|
 
 
| Delete the first element that has a negative value |
p1 = head;
p2 = head->next;
while ( p2->value >= 0 )
{
p1 = p2;
p2 = p2->next;
}
|
p1->next = p2->next;
delete p2;
|
if ( head->value < 0 ) |
p2 = head; head = head->next; delete p2; // Deallocate space |
ListElem *p1, *p2;
if ( head == NULL )
{
head = NULL;
}
else
{
// Special case: delete the first element
if ( head->value < 0 )
{
p2 = head;
head = head->next;
delete p2; // Deallocate space
}
else // General case
{
p1 = head;
p2 = head->next;
// Find an element with neg. value
while ( p2->value >= 0 )
{
p1 = p2;
p2 = p2->next;
}
p1.next = p2->next;
delete p2; // Deallocate space
}
}
|
|
ListElem *p1, *p2;
if ( head == NULL )
{
head = NULL;
}
else
{
// Special case: delete the first element
if ( head->value < 0 )
{
p2 = head;
head = head->next;
delete p2; // Deallocate space
}
else // General case
{
p1 = head;
p2 = head->next;
// Find an element with neg. value
while ( p2 != NULL && p2->value >= 0 )
{
p1 = p2;
p2 = p2->next;
}
p1.next = p2->next;
delete p2; // Deallocate space
}
}
|
|
ListElem *p1, *p2;
if ( head == NULL )
{
head = NULL;
}
else
{
// Special case: delete the first element
if ( head->value < 0 )
{
p2 = head;
head = head->next;
delete p2; // Deallocate space
}
else // General case
{
p1 = head;
p2 = head->next;
// Find an element with neg. value
while ( p2 != NULL && p2->value >= 0 )
{
p1 = p2;
p2 = p2->next;
}
if ( p2 != NULL)
{
p1.next = p2->next;
delete p2; // Deallocate space
}
}
}
|
It finally works :-)