예제 #1
0
파일: Wife.cpp 프로젝트: ipkCoder/cs211
/*
 * This function adds a child to the family. The child is inserted at the of the
 * linked list of children. In order to i=add the child to the end of the list, 
 * the function checks whether the list is empty, if there is already one child,
 * or if there are more than one child. If there is no children in list, the 
 * child is added at the head of the list. If there is already one child, the 
 * the child is added after the first child. If there is more than one child in
 * the list, then the end of the list is searched for and the child is added at
 * the end.
 *
 * @param: Child* const &child - pointer to child to be added to list of children
 * @attribute: Child *children - head pointer to linked list of children   
 */
void Wife::addChild(Child* const &child)
{
  if(children == NULL){
    children = child;
  }
  else if(children->getSibling() == NULL){
    children->setSibling(child);
  }
  else{
    Child *curr = children;
    while(curr->getSibling() != NULL){
      curr = curr->getSibling();
    }
    curr->setSibling(child);
  }
}
예제 #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;
  }
}