next up previous contents
Next: Insertion Agents: Up: The CHAR_SLLIST Implementation Details: Previous: How Many Elements Are

Delete an Element From the List:

    // *************************************** //
    // delete an element in the list           //
    // *************************************** // 
void CHAR_SLLIST::delete_element(CHAR target_key)
{
    CHAR_SL_CELL *previous,*current,*after;
    previous = get_before_element_address(target_key);
    current = get_element_address(target_key); 
    if(current==NULL){
      cout << target_key << " not in the list; can't delete." << endl; 
      return;
      }
    else{
      if(current==head){
        //here previous = NULL
        previous = head;
        after = head->next;
        head = after;
	previous->next = NULL;
        delete previous;      
        }
      else{
        //sought cell is not at head of list
        after = current->next;
        previous->next = after;
	current->next = NULL;
        delete current;
        }
      }
}



Jim Peterson
1999-04-22