LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete Linked List Seg Fault??? (https://www.linuxquestions.org/questions/programming-9/delete-linked-list-seg-fault-391648/)

Mistro116@yahoo.com 12-11-2005 04:24 PM

Delete Linked List Seg Fault???
 
Hi, I am running the following program with the Test Delete function, and it works, except for when it gets to the end of the list, then it seg faults if the answer input is wrong (if the CheckAnswer if statement is evaluated): Does anyone know why this seg fault occurs?

Code:

int BeginQuizFirstTry (NODEPTR *headListPtr1,
                      NODEPTR *headListPtr2,
                      int numberOfTotalQuestions)
{
  /*  Display a message indicating that the quiz has
      begun for the user.                            */
  printf ("Here are your questions. Good luck !!!\n\n");
 
 
 
  NODEPTR curr = *headListPtr1;
  NODEPTR temp;
  char questionAnswer [NUMBEROFANSWERCHARACTERS];
  char quizCorrectAnswer [NUMBEROFANSWERCHARACTERS];
  int questionCounter = 0;
  int correctAnswerCounter = 0;
  QUIZ deletedQuestion;
 
  while (curr != NULL)
  {
      questionCounter++;
     
      printf ("  %d. ", questionCounter);
      DisplayQuestion (curr);
     
      scanf ("%s", questionAnswer);
     
      strcat (questionAnswer, "\n");
     
      if (CheckAnswer (curr, questionAnswer,
                      &correctAnswerCounter) != 1)
      {
        strcpy (deletedQuestion.question, curr -> data.question);
        strcpy (deletedQuestion.answer, curr -> data.answer);
       
        temp = CreateNode ();
        SetData (temp, deletedQuestion);
        Insert (headListPtr2, temp);
       
        strcpy (quizCorrectAnswer, curr -> data.question);
       
        if (curr != NULL)
        {
            curr = curr -> next;
        }
       
        TestDelete (headListPtr1, quizCorrectAnswer);
      }
      else
      {
        /*  Set the node pointer referencing the current node
            (the current question in the quiz) to point to the
            next node in the list, or the next question in the
            quiz.                                              */
        curr = curr -> next;
      }
  }
 
  printf ("\n\n%d", correctAnswerCounter);
 
  return 1;
}

int TestDelete (NODEPTR *headPtr, char *target)
{
  NODEPTR temp, prev, curr;
 
  if (IsEmpty (*headPtr))
  {
      printf ("Empty List\n\n");
      return (-1);
  }
  else if (strcmp ((*headPtr) -> data.question, target) == 0)
  {
      temp = *headPtr;
      *headPtr = (*headPtr) -> next;
     
      free (temp);
     
      return 1;
  }
  else
  {
      prev = *headPtr;
      curr = (*headPtr) -> next;
     
      while (curr != NULL && strcmp (curr -> data.question, target))
      {
        prev = curr;
        curr = curr -> next;
      }
     
      if (curr != NULL)
      {
        temp = curr;
        prev -> next = curr -> next;
       
        free (temp);
       
        return 1;
      }
      else
      {
        printf ("%s not found\n\n", target);
       
        return (-1);
      }
  }
}

Thanks in advance,

Mistro116

acid_kewpie 12-11-2005 04:29 PM

Mr Mistro... i'm getting a little concerned by the number and type of questions you're asking on LQ.org. It looks an awful lot like you're doing a school course in C++, and if that's the case we are NOT here to do your homework for you. You will have been provided notes as part of a school course to cover everythign you are deemed to need to know.

Mistro116@yahoo.com 12-11-2005 04:43 PM

Yes, this is a project for school; however, I understand that you are not here to "do" my school work for me. But I thought this forum was on subject specific questions about programming, which is exactly what we have here, not write my program for me.

If I understand you correctly, only things interdependent from course work can be asked here? Well thats just rediculous because no where on here am I asking anyone to write anything. I am asking for suggestions on a problem, I'm not asking for any "hand me outs".

And the number and type of questions? I post here every day/week almost, answering people's questions and posting some of my own which are sometimes and sometimes not related to course work and some are just independent research.

I don't feel I need to explain myself to you, but since this was brought to my attention, I do believe you don't need to be concerned for anyone but yourself.

Mistro116


All times are GMT -5. The time now is 11:48 AM.