next up previous contents
Next: Is A Given Element Up: How Many Elements Are Previous: How Many Elements Are

Delete an Element From the List:

    // *************************************** //
    // delete an element in the list           //
    // *************************************** // 
void CHAR_DLLIST::delete_element(CHAR target_key)
{
  CHAR_DL_CELL *before,*current,*after;   
  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 before = NULL
      before = head;
      after = head->next;
      after->previous = NULL;
      head = after;
      before->next = NULL;
      delete before;      
      }
    else{
      if(current==end){
        before = end->previous;
	end = before;
	end->next = NULL;
	delete current;
        }
      else{
        //sought cell is in middle of list
	before = current->previous;	  
        after = current->next;	  	
        before->next = after;
	after->previous = before;
	current->next = NULL;
        delete current;  
        }
      }
    }
}



Jim Peterson
1999-04-22