示例#1
0
文件: Wife.cpp 项目: ipkCoder/cs211
/*
 * This function searches for a child in a family by searching for the child's 
 * social security number in the linked list of children. 
 *
 * @return: true if child's social security number is found, else false
 */
bool Wife::searchForChild(const long &ssn)
{
  Child *curr = children;
  bool found = false;
  
  while(curr != NULL){
    if(curr->getSSN() == ssn){
      found = true;
      break;
    }
    else
      curr = curr->getSibling();
  }
  
  return found;
}
示例#2
0
文件: Wife.cpp 项目: ipkCoder/cs211
/*
 * This function removes a child from the linked list of children. The child is 
 * searched for using their social security number. To search the list and delete 
 * a child, two steps must be performed. The first step is to check if the first
 * chlid in the list is the child to be deleted. If not, then the rest of the 
 * list is searched. These two steps are necessary because they each require
 * different procedures to remove a child from the list.
 *
 * @param: const long &ssn - social security number associated with child 
 *                           to search for
 * @attribute: Child *children - head pointer to linked list of children
 */
bool Wife::removeChild(const long &ssn)
{
  if(children == NULL){
    return false;
  }
  
  // check if first child is to be removed
  if(children->getSSN() == ssn){
    Child *child = children;
    children = child->getSibling();
    delete child;
    return true;
  }
  
  // set current child to second child and previous child to first child
  Child *prevChild = children;
  Child *currChild = children->getSibling();
  
  // if child exists in the rest of the list of children, point previous
  // child's sibling pointer to the child after the child to be deleted
  while(currChild != NULL){
    if(currChild->getSSN() == ssn){
      prevChild->setSibling(currChild->getSibling());
      break;
    }
    else{
      prevChild = currChild;
      currChild = currChild->getSibling();
    }
  }
  
  // if child found, delete child and return true, else false
  if(currChild != NULL){
    delete currChild;
    return true;
  }
  else{
    return false;
  }
}