LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2005, 04:24 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
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
 
Old 12-11-2005, 04:29 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 12-11-2005, 04:43 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seg Fault in GCC when deleting Node from Linked List gren838 Programming 2 12-11-2005 04:21 PM
Delete Linked List Function Mistro116@yahoo.com Programming 6 12-10-2005 02:43 PM
C program problem on delete duplicated nodes in linked list ! antony_csf Programming 2 10-28-2004 10:42 AM
RH 7.2 crash...getting seg fault when using ls sakima Red Hat 1 10-11-2004 09:14 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration