Having a segmentation fault

As mentionned in the comments:

  • we have little understanding about what is guaranteed about q (or what qNode or Queue look like)
  • root is allocated at the start of the function but the pointer value get replaced by q->front, causing a memory leak, could probably be detected by your compiler by enabling warnings.
  • is q or q->front not NULL ?
  • please check if root is not NULL before checking root->next
  • ending if root->next is NULL would mean that the last node ( with content ) will be skipped because it does not have a next ( trace your algorithm by hand to verify )

suggested fixes (file output skipped for brevity):

void findFlight(Queue *q, int flightNum){
  if( q == NULL ) return;
  qNode *current = q->front;
  if( current == NULL ) return;
  while(current != NULL) {
      if( current->flightNum == flightNum ) {
          // we assume that current->fullName is actually set, but feel free to verify
          fprintf(stdout, "%s is taking flight %d", current->fullName, current->flightNum);
      }
      current = current->next;
  }
}

Browse More Popular Posts

Leave a Comment