LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-10-2005, 02:54 AM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Delete Linked List Function


Hello,

I am writing a function for deleting the node in a list, once it searches for a string, but before it is deleted and freed from memory, the contents of the node are copied into the parameter QUIZ *deletedQuestion, but I'm getting the following compiler error with the strcpy function and im not sure why:

Code:
list.c: In function `Delete':
list.c:122: error: request for member `question' in something not a structure or union
list.c:123: error: request for member `answer' in something not a structure or union
list.c:169: error: request for member `question' in something not a structure or union
list.c:170: error: request for member `answer' in something not a structure or union

Code:
int Delete (NODEPTR *headPtr, char *target, QUIZ *deletedQuestion)
{
   /*  Node pointers that reference the previous node
       and the current node in the list.               */
   NODEPTR prev = NULL, curr = NULL;
   
   /*  Check to see if the list is empty.  */
   if (IsEmpty (*headPtr))
   {
      /*  Display an appropriate message indicating that
	  deleting from the list was not accomplished
	  because the list is empty.                      */
      fprintf (stderr, "Unable to delete from an empty list.\n\n");
      
      /*  If the list is empty, return the integer value
	  of -1, indicating that the question, char
	  *target, was not successfully deleted from the
	  list.                                           */
      return (-1);
   }
   
   /*  Traverse the list until the target value is found.  */
   
   /*  Initialize the node pointer referencing a previous
       node in the list to NULL.                           */
   prev = NULL;
   
   /*  Set the node pointer referencing a current node to
       the head of the list.                               */
   curr = *headPtr;
   
   /*  First check to see if the question, char *target,
       that we want to delete is stored in the first
       node in the list, because this would require
       *headPtr to be modified.                           */
   if (strcmp (curr -> data.question, target) == 0)
   {
      /*  Copy the contents of the data portion of the
	  node containing the question, char *target,
	  that we want to delete from list into the
	  QUIZ structure, deletedQuestion.              */
      strcpy (curr -> data.question, *deletedQuestion.question);
      strcpy (curr -> data.answer, *deletedQuestion.answer);
      
      /*  If the question, char *target, is stored in the
	  first node in the list, change the head of the
	  list to start at the node pointer referencing
	  the current node's next pointer.                 */
      *headPtr = curr -> next;
      
      /*  Delete and free the memory that was being used
	  to store the question, char *target, in the
	  first node of the list.                         */
      free (curr);
      
      /*  If the question, char *target, was freed and
	  deleted, return the integer value of 1,
	  indicating that the question was successfully
	  deleted from the list.                         */
      return (1);
   }
   /*  Otherwise, traverse through the list to find the
       question, char *target, to be deleted.            */
   else
   {
      /*  Traverse the list if the node pointer
	  referencing the current node in the list is not
	  equal to NULL and the node pointer referencing
	  the current node in the list is not storing the
	  question, char *target, in the data portion of
	  its node.                                        */
      while ((curr != NULL) &&
	     (strcmp (curr -> data.question, target) != 0))
      {
	 /*  Traverse the list.  */
	 prev = curr;
	 curr = curr -> next;
      }
      
      /*  If the node pointer refencing the current node in
	  the list not equal to NULL, then the question,
	  char *target, was found.                           */
      if (curr != NULL)
      {
	 /*  Copy the contents of the data portion of the
	     node containing the question, char *target,
	     that we want to delete from list into the
	     QUIZ structure, deletedQuestion.              */
	 strcpy (curr -> data.question, *deletedQuestion.question);
	 strcpy (curr -> data.answer, *deletedQuestion.answer);
	 
	 /*  Change the next pointer of the previous node,
	     in order to avoid a broken list.               */
	 prev -> next = curr -> next;
	 
	 /*  Delete and free the memory that was being used
	     to store the question, char *target, in this
	     node of the list.                               */
	 free (curr);
	 
	 /*  If the question, char *target, was freed and
	     deleted, return the integer value of 1,
	     indicating that the question was successfully
	     deleted from the list.                         */
	    return (1);
      }
      /*  Otherwise, the question, char *target, was not
	  found in the list.                              */
      else
      {
	 /*  If the question, char *target, was not found
	     in the list, return the integer value of -1,
	     indicating that the question was not
	     successfully deleted from the list.           */
	 return (-1);
      }
   }
}
Can anyone tell me why I can't copy the contents of the data portion.question of the current node into the parameter - QUIZ structure pointer?

My structures are defined as:

Code:
/* NODEPTR is defined as an alias for the structure of type
   node, so that it can be used within the structure before
   it is completely defined.                              */

typedef struct node * NODEPTR;

/* This struct, typedefed to be of type quiz, is meant to
   store all the data about each question in the quiz that
   will be given to the user.                             */

typedef struct quiz
{
   char question [NUMBEROFQUESTIONCHARACTERS];
   char answer [NUMBEROFANSWERCHARACTERS];
} QUIZ;

/* A NODE is a structure that allows us to build a linked
   list since it has both a data portion and a NODEPTR as
   its members.  The NODEPTR called next is known as the
   link.                                                  */

typedef struct node
{
   QUIZ data;
   NODEPTR next;  /* OR struct node *next; */
} NODE;
Thanks in advance,

Mistro116
 
Old 12-10-2005, 03:25 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Why are you mixing different methods to access data?

For instance:
Code:
curr is defined as a NODEPTR (or rather: struct node *)
deletedQuestion is defined as a QUIZ *

Both curr and deletedQuestion are pointers to structs, but then you do this:

strcpy (curr -> data.question, *deletedQuestion.question);
Why switch up the syntax? This is another ambiguity problem. With curr, you use the arrow operator, telling the computer to dereference curr and then access data.question. For deletedQuestion, you slap a star to dereference, but what is being dereferenced? The whole thing (deletedQuestion.question)? Use the arrow operator or use parentheses to clarify the order of dereferencing such as:
Code:
(*deletedQuestion).question
 
Old 12-10-2005, 11:44 AM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Thanks Dark_Helmet,

Would of never thought of that. Absolutely brilliant!

Mistro116
 
Old 12-10-2005, 11:56 AM   #4
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Hey,

One last thing, when I try to run the code.. although it compiles, but the contents of deletedQuestion are never modified. Why is this?

Thanks,

Mistro116
 
Old 12-10-2005, 01:41 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
First, have you allocated space for deletedQuestion?

Second, are you sure there's something *to* copy? As in, are you sure the linked list contains real data?
 
Old 12-10-2005, 01:58 PM   #6
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Yes, I am sure that the list contains the right data.

I did not allocate memory for the QUIZ structure, it is just declared in main as QUIZ deletedQuestion;

When I put print statements in the function, it doesn't get to them, such as in the conditions of the question being the first node in the list and it being traversed, but they are never printed (I don't believe). The function does delete properly and return the correct value. What do you think?

Mistro116
 
Old 12-10-2005, 02:43 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Double check the man pages for strcpy() and make sure you're doing what you think you're doing
 
  


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
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM
C program problem on delete duplicated nodes in linked list ! antony_csf Programming 2 10-28-2004 10:42 AM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 10:52 AM

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

All times are GMT -5. The time now is 07:38 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