Thread: Can i do this: Structures and linked lists help

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    27

    Can i do this: Structures and linked lists help

    Im doing a c assignment for college where I have to create a database like program which can store up to 100 records for make, model, etc of a car. So I have decided to use structures and linked lists. But my problem is if I can do this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* self-referential structure */
    struct listNode {            
       char data; /* each listNode contains a character */
       struct listNode *nextPtr; /* pointer to next node*/ 
    }; /* end structure listNode */
    
    typedef struct listNode ListNode; /* synonym for struct listNode */
    typedef ListNode *ListNodePtr; /* synonym for ListNode* */
    
    struct CarData {
    	char Make [20];  /* char entered by user */
    	char Model [20];
    	int Year;
    	double Price;
    	char Reg_num [12];
    	long int Mileage;
    } record[];
    int i;
    
    /* prototypes */
    void insert( ListNodePtr *sPtr, struct stuff[]);
    char delete( ListNodePtr *sPtr, char value );
    int isEmpty( ListNodePtr sPtr );
    void printList( ListNodePtr currentPtr );
    void instructions( void );
    
    int main()
    { 
    	
    	char Cmake [20];
    	char Cmodel [20];
    	int Cyear;
    	double Cprice;
    	char Creg_num [12];
    	long int Cmileage;
    
       
    	ListNodePtr startPtr = NULL; /* initially there are no nodes */
       int choice; /* user's choice */
       int item;
    
       instructions(); /* display the menu */
       printf( "? " );
       scanf( "%i", &choice );
    
       /* loop while user does not choose 3 */
       while ( choice != 3 ) { 
    
          switch ( choice ) { 
    
             case 1:
                printf( "Enter a Car Make ");
                scanf( "\n%s", & Cmake );
    			
    			printf ("Enter a Car Model");
    			scanf ("\n%s", & Cmodel);
    			
    			do
    			{
    				printf ("Enter the Year of the Car");
    				scanf( "\n%s",& Cyear );
    			}
    			while ( Cyear <=2005);
    
    			printf( "Enter a Price of the Car");
    			scanf( "\nE%lf",& Cprice);
    
    			printf( "Enter a Reg Number for the Car");
    			scanf( "\n%s",& Creg_num );
    
    			printf( "Enter a Mileage for the Car");
    			scanf( "\n%i",& Cmileage );
    
    			struct CarData record[i]= {Cmake, Cmodel, Cyear, Cprice, Creg_num, Cmileage};
    			
                insert( &startPtr, record[i]); /* insert item in list */
              //  printList( startPtr );
                break;
    
             case 2:
    
                /* if list is not empty */
                if ( !isEmpty( startPtr ) ) { 
                   printf( "Enter character to be deleted: " );
                   scanf( "\n%c", &item );
    
                   /* if character is found, remove it */
                   if ( delete( &startPtr, item ) ) { /* remove item */
                      printf( "%c deleted.\n", item );
                    //  printList( startPtr );
                   } /* end if */
                   else {
                      printf( "%c not found.\n\n", item );
                   } /* end else */
    
                } /* end if */
                else {
                   printf( "List is empty.\n\n" );
                } /* end else */
    
                break;
    
             default:
                printf( "Invalid choice.\n\n" );
                instructions();
                break;
          
          } /* end switch */
    
          printf( "? " );
          scanf( "%i", &choice );
       } /* end while */
    
       printf( "End of run.\n" );
       
       return 0; /* indicates successful termination */
    
    } /* end main */ 
    
    /* Insert a new value into the list in sorted order */
    void insert( ListNodePtr *sPtr, struct stuff[])
    { 
       ListNodePtr newPtr;      /* pointer to new node */
       ListNodePtr previousPtr; /* pointer to previous node in list */
       ListNodePtr currentPtr;  /* pointer to current node in list */
    
       newPtr = malloc( sizeof( ListNode ) ); /* create node */
    
       if ( newPtr != NULL ) { /* is space available */
          newPtr->data[i] = stuff[i]; /* place value in node */
          newPtr->nextPtr = NULL; /* node does not link to another node */
    
          previousPtr = NULL;
          currentPtr = *sPtr;
    
          /* loop to find the correct location in the list */
          while ( currentPtr != NULL && stuff > currentPtr->data ) { 
             previousPtr = currentPtr;          /* walk to ...   */
             currentPtr = currentPtr->nextPtr;  /* ... next node */
          } /* end while */
    
          /* insert new node at beginning of list */
          if ( previousPtr == NULL ) { 
             newPtr->nextPtr = *sPtr;
             *sPtr = newPtr;
          } /* end if */
          else { /* insert new node between previousPtr and currentPtr */
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = currentPtr;
          } /* end else */
       
       } /* end if */
       else {
          printf( "Records not inserted. No memory available.\n");
       } /* end else */
    
    } /* end function insert */
    So I know that some of the bits at the prototypes aren’t here in this snippet but im getting a couple of errors with the passing in to the insert function could any one tell me where im going wrong or if I have to change the program totally
    Always posting problems

    Always needing help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void insert( ListNodePtr *sPtr, struct stuff[]);
    You don't define a struct called stuff. I imagine you wanted
    Code:
    void insert( ListNodePtr *sPtr, struct CarData stuff[]);
    Beyond that, you have type mismatches all over the place and some declarations aren't where they should be. The errors will tell you all you need to know.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    i'm posting beyond the line


    but that got rid on 2 error messages
    Always posting problems

    Always needing help

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    You should learn to read errors and know what they mean. The explinations are pretty straight forward. I ran your code through my compiler and here are all the errors I got:

    Code:
    test.c:24: warning: `struct stuff' declared inside parameter list
    test.c:24: warning: its scope is only this definition or declaration, which is probably not what you want.
    C:\My Documents\Papers & Programs\C\test.c: In function `main':
    test.c:77: parse error before `struct'
    test.c:79: incompatible type for argument 2 of `insert'
    C:\My Documents\Papers & Programs\C\test.c: At top level:
    test.c:124: warning: `struct stuff' declared inside parameter list
    test.c:125: conflicting types for `insert'
    test.c:24: previous declaration of `insert'
    C:\My Documents\Papers & Programs\C\test.c: In function `insert':
    test.c:124: parameter name omitted
    test.c:133: subscripted value is neither array nor pointer
    test.c:133: `stuff' undeclared (first use in this function)
    test.c:133: (Each undeclared identifier is reported only once
    test.c:133: for each function it appears in.)
    C:\My Documents\Papers & Programs\C\test.c: At top level:
    test.c:20: warning: array `record' assumed to have one element
    Now I'll run you through some of the errors until you get the idea of what they mean. After that you can mess around with them until you get it to work. After all, the only way to learn is by doing. Only if after trying time and time again you cannot figure it out, then someone will help. It is best for your learning to only get hints from someone rather than having them do it for you.

    Anyway here are some explinations:

    Prelude already gave you the solution to this one but I'll go over it anyway.

    Code:
    test.c:24: warning: `struct stuff' declared inside parameter list
    Okay, lets take this error apart piece by piece.

    Code:
    test.c:24:
    The error is in test.c, which is what I named your file, on line 24.

    Code:
    warning: `struct stuff' declared inside parameter list
    Well, it's pretty self explanitory from here. You never created a structure named "stuff", so the program thinks you're trying to declare it within your parameters for that function. In this case it was just you forgot to put the structure name CarData after the keyword struct.




    Let's do another example:

    Code:
    C:\My Documents\Papers & Programs\C\test.c: In function `main':
    test.c:77: parse error before `struct'
    test.c:79: incompatible type for argument 2 of `insert'
    Okay, once again look at the error piece by piece.

    Code:
    C:\My Documents\Papers & Programs\C\test.c: In function `main':
    Once again, since it's the only file, the errors are in test.c but specifically inside your 'main' function.

    Code:
    test.c:77:
    The error is on line 77.

    Code:
    parse error before `struct'
    Well, there's something wrong with that line of code. If you mess around with it and can't get anything to work think about alternative ways to assigning values. Have you ever thought of doing each one seperately? For example, instead of:

    Code:
    struct CarData record[i]= {Cmake, Cmodel, Cyear, Cprice, Creg_num, Cmileage};
    try (note: the .'s denote the rest of the code. I did one example to get the ball rolling):

    Code:
    strcpy(record[i].Make, Cmake);
    .
    .
    .
    Let's move onto the next error.

    Code:
    test.c:79:
    Okay, looking at this line you see that the error is on line 79.

    Code:
    incompatible type for argument 2 of `insert'
    There we have it. You're passing an incompatible data type into the 'insert' function, specifically the second argument. Let's look at the code:

    Code:
    insert( &startPtr, record[i]);
    Okay, we have a call to the function insert and we're passing in two arguments. According to the error the problem is with the second argument. Let's think about it, record is an array of type struct CarData, but you're not passing in the whole array but only one part. What is something we could add to make that right?

    Code:
    insert( &startPtr, &record[i]);
    There we go! Now you no longer get that nasty error.



    Now that I've given you a couple of examples you can try the rest on your own.

    EDITED FOR SPELLING ERRORS. Hope I got 'em all .
    Last edited by Pressure; 04-25-2005 at 07:49 AM.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Very nice post, Pressure. This would make a nice addition to the FAQ forum, methinks.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-05-2008, 02:18 AM
  2. problems with structures and linked lists
    By jwillisoa in forum C Programming
    Replies: 7
    Last Post: 07-01-2007, 05:23 PM
  3. C Linked Lists and Structures help!
    By Yorae in forum C Programming
    Replies: 0
    Last Post: 03-07-2006, 09:55 AM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  5. C Structures and Linked Lists.
    By bobthebuilder20 in forum C Programming
    Replies: 14
    Last Post: 04-15-2003, 05:00 PM