Thread: Changing static array database system to linked list

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    Changing static array database system to linked list

    I have to modify this program so that it will use a linked list instead of the current system, as well as add in a insertFirst function and a freeList function.
    Code:
    /* Mini-data base.  Somewhat more than was asked for with program 10 */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define nameSize 50
    #define arraySize 250
    }
    /* Data for one person */
    typedef struct
    {
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    } Person;
    
    
    /* The database: array of structs, arranged in order of id */
    Person data[arraySize];
    
    #define bufferSize 132
    char buffer[bufferSize];  /* input buffer */
    
    /* Print nicely formatted data for one person */
    void printRecord( Person *p )
    {
      printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
            p->id, p->birthYear,p->height,p->weight,
            p->salary,p->firstName,p->lastName);
    }
    
    /* Ask the user for the id of a person */
    int getID()
    {
      int id;
      
      printf("id? (enter -1 to quit):");
      gets( buffer );
      while ( sscanf( buffer, " %d", &id ) != 1 )
      {
        printf("re-enter id: ");
        gets( buffer );
      }
      return id;
    }
    
    void readData( FILE* input )
    {
      Person p;   /* data from one input record */
      int j = 0;  /* count of records read in */
      int flag;   /* count of successful conversions in scanf */
    
      /* Read in the File, copying data into the array */
      while ( fgets( buffer, bufferSize, input) )
      {
        j++ ;
    
        /* Read in the data */
        flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               &p.id, &p.birthYear, &p.height, &p.weight, &p.salary,
               p.lastName, p.firstName );
    
        if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
    
        /* copy data into the database  */
        if ( p.id < arraySize )
        {
          if ( data[p.id].id != 0 )
            fprintf( stderr, "Record %d: duplicate id %d; record skipped\n", j, p.id );
          else
            data[p.id] = p;
        }
        else
          fprintf( stderr, "Record %d: id %d is out of range; record skipped\n", j, p.id );
      }
    }
    insertFirst()
    {
    }
    freeList()
    {
    }
    int main()
    {
      FILE *input;
      int j=0;
      char fileName[bufferSize];
    
      /* Ask user for input file; open file */
      printf("Name of input file: ");
      gets( buffer );
      sscanf( buffer, "%s", fileName );
    
      /* Open input file for reading */
      if ( (input=fopen( fileName, "r"))==NULL )
      {
        perror("input file did not open ");
        exit( EXIT_FAILURE );
      }
      
      /* Read in the data */
      readData( input );
      
      /* Process Queries until user enters -1 */
      int id = 0 ;
      id = getID();
      
      while ( id != -1 )
      {
        /* Linear search */
        for ( j=0; j<arraySize && data[j].id < id; j++ );
        
        if ( data[j].id == id )
          printRecord( &data[j] );
        else
          printf("id %3d not found\n", id );
        
        id = getID();
      }
    
      /* Write out the data in order of id */
      printf("\n\nAll Records:\n");
      for ( j=0; j<arraySize; j++ )
        if ( data[j].id != 0 )
          printRecord( &data[j] );
    
      fclose( input );
      system("pause");
    }
    First of all: modifying the struct. Should there be more than one struct? Should I just make a struct for a node and then add a node into the Person struct? Or should I just add the link part into the person struct... I'm kind of confused as to what exactly this:
    Code:
    struct NODE
    {
    struct NODE *link;
    int value;
    };
    typedef struct NODE Node;
    --------------------------------------------------------------
    Node *insertFirst( Node **hptr, int val )
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->value = val;
    node->link = *hptr;
    *hptr = node;
    return node;
    }
    is. Theres the node struct which has a value and a pointer to something else with another value and another pointer etc...(figured that out as I was typing it out). then after that theres the declaration so one of those actually exists.
    Below that line, however is what is confusing me in terms of how to incorporate it into the other program. What of this should I put into the person struct in my current program?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    All right so my first post was a wall of text without a solid question. I've done something now and it needs some fixing.
    I've changed the struct and the insertFirst function but am having a problem with assigning the firstname and lastname.
    Code:
    struct NODE
    {
      struct NODE *link;  
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->id = id;
    node->link = *hptr;
    node->birthYear = birthYear;
    node->height = height;
    node->weight = weight;
    node->salary = salary;
    node->firstName = firstName;
    node->lastName = lastName;
    *hptr = node;
    return node;
    }
    incompatable types in assignment for first and last name, how can I write this so that they're compatible?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcpy


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Right, thanks, that parts settled now. looks like this
    Code:
    struct NODE
    {
      struct NODE *link;  
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->id = id;
    node->link = *hptr;
    node->birthYear = birthYear;
    node->height = height;
    node->weight = weight;
    node->salary = salary;
    strcopy(node->firstName, firstName);
    strcopy(node->lastName, lastName);
    *hptr = node;
    return node;
    }
    But now what I need some help on is rewriting the readData function.
    Code:
    void readData( FILE* input )
    {
      Node p;   /* data from one input record */
      int j = 0;  /* count of records read in */
      int flag;   /* count of successful conversions in scanf */
    
      /* Read in the File, copying data into the array */
      while ( fgets( buffer, bufferSize, input) )
      {
        j++ ;
    
        /* Read in the data */
        flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               p->id, p->birthYear, p->height, p->weight, p->salary,
               p->lastName, p->firstName );
    
        if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
    
        /* copy data into the database  */
        if ( p->id < arraySize )
        {
          if ( p->id != 0 )
            fprintf( stderr, "Record %d: duplicate id %d; record skipped\n", j, p.id );
          else
            p->id = p;
        }
        else
          fprintf( stderr, "Record %d: id %d is out of range; record skipped\n", j, p.id );
      }
    }
    I don't think I need j anymore. also, I'm having trouble rewriting the sscanf part, what can I put as the destinations? (currently are p->id etc., used to be the array)/ do I have to do an insertfirst?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    readData() as you posted it does not seem to have a purpose -- it reads some data into a file, copies it into local variable p, and then ends, eliminating p and the data in it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    ah okay, i'll rethink it. It's supposed to read data from a file(text file) and put each line of that text file as a node on the linked list.
    ------------------Edit--------
    now the readData part looks like this:
    Code:
    void readData( FILE* input )
    {
      Node *p;   /* data from one input record */
      int j;
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
      int flag;   /* count of successful conversions in scanf */
    
      /* Read in the File, copying data into the array */
      while ( fgets( buffer, bufferSize, input) )
      { j++;
        /* Read in the data */
        flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               id, birthYear, height, weight, salary,
               lastName, firstName );
         
        if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
        insertFirst(&p , id, birthYear, height, weight, salary, firstName, lastName);
    
        
        p = p->link;
      }
    }
    I have a feeling something is wrong with it and/or that making those extra variables is unnecessary somehow. The entire program looks like this now
    Code:
    /* Mini-data base.  Somewhat more than was asked for with program 10 */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define nameSize 50
    
    
    /* Data for one person */
    struct NODE
    {
      struct NODE *link;  
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->id = id;
    node->link = *hptr;
    node->birthYear = birthYear;
    node->height = height;
    node->weight = weight;
    node->salary = salary;
    strcopy(node->firstName, firstName);
    strcopy(node->lastName, lastName);
    *hptr = node;
    return node;
    }
    
    
    
    #define bufferSize 132
    char buffer[bufferSize];  /* input buffer */
    
    /* Print nicely formatted data for one person */
    void printRecord( Node *p )
    {
      printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
            p->id, p->birthYear,p->height,p->weight,
            p->salary,p->firstName,p->lastName);
    }
    
    /* Ask the user for the id of a person */
    int getID()
    {
      int id;
      
      printf("id? (enter -1 to quit):");
      gets( buffer );
      while ( sscanf( buffer, " %d", &id ) != 1 )
      {
        printf("re-enter id: ");
        gets( buffer );
      }
      return id;
    }
    
    void readData( FILE* input )
    {
      Node *p;   /* data from one input record */
      int j;
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
      int flag;   /* count of successful conversions in scanf */
    
      /* Read in the File, copying data into the array */
      while ( fgets( buffer, bufferSize, input) )
      { j++;
        /* Read in the data */
        flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               id, birthYear, height, weight, salary,
               lastName, firstName );
         
        if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
        insertFirst(&p , id, birthYear, height, weight, salary, firstName, lastName);
    
        
        p = p->link;
      }
    }
    
    freeList()
    {
    }
    int main()
    {
      FILE *input;
      Node *head = NULL;
      int j=0;
      char fileName[bufferSize];
    
      /* Ask user for input file; open file */
      printf("Name of input file: ");
      gets( buffer );
      sscanf( buffer, "%s", fileName );
    
      /* Open input file for reading */
      if ( (input=fopen( fileName, "r"))==NULL )
      {
        perror("input file did not open ");
        exit( EXIT_FAILURE );
      }
      
      /* Read in the data */
      readData( input );
      
      /* Process Queries until user enters -1 */
      int id = 0 ;
      id = getID();
      
      while ( id != -1 )
      {
        /* Linear search */
        while(head->link != NULL)
        {
        if ( head->id == id )
          printRecord( head );
        else
          printf("id %3d not found\n", id );
        head = head->link;
         }
        id = getID();
      }
    
      /* Write out the data in order of id */
      printf("\n\nAll Records:\n");
      while(head->link != NULL)
        if ( head->id != 0 )
          printRecord( head );
    
      fclose( input );
      system("pause");
    }
    it says linker error undefined reference to strcopy. I thought that part was settled... also ld returned 1 exit status.
    Last edited by DLH112; 12-07-2009 at 01:15 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DLH112 View Post
    ah okay, i'll rethink it. It's supposed to read data from a file(text file) and put each line of that text file as a node on the linked list.
    Right, so you need to include the head of the list in the parameters:

    Code:
    void readData( FILE* input, Node *head );
    Then in your loop, rather than the perplexing:
    Code:
    p->id = p;
    which will never pass a compiler, you want to assign the new nodes to the "link" element in the preceeding node. That means that p should be a *pointer, and you should malloc it for each new node, first thing in the loop.

    IMO you are doing this ass-backward. You are also trying to write all the code at once and then go back and correct it, which is a BAD way to code.

    Since you have obviously not worked with linked lists before, I suggest you forget about the data for now and write a linked list program where the struct has only one member, an int. Think of all the things you must do with the list -- add new nodes, insert them, maybe delete or rearrange them. When you have that working THEN you think about reading in more complex data. It will take you much longer otherwise, and end up as a mess.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    that is what my previous assignment was(the basic linked list with just a number), and I understand the basics of it like you said. This program that I started with for this assignment is the solution to the program I had to write 2 assignments ago. So it's somewhat mandatory to start with the whole thing? I think... I agree with the idea that It probably would be easier to start from scratch and build it up so I understand it better, but unfortunately the assignment is to change that already existing program. Edit: wow, theres no o in strcpy >_< my bad...
    Edit: posting whole program so I can copy/paste onto my other pc, it compiles now.
    Code:
    /* Mini-data base.  Somewhat more than was asked for with program 10 */
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define nameSize 50
    
    
    /* Data for one person */
    struct NODE
    {
      struct NODE *link;  
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->id = id;
    node->birthYear = birthYear;
    node->height = height;
    node->weight = weight;
    node->salary = salary;
    strcpy(node->firstName, firstName);
    strcpy(node->lastName, lastName);
    node->link = *hptr;
    *hptr = node;
    return node;
    }
    
    
    
    #define bufferSize 132
    char buffer[bufferSize];  /* input buffer */
    
    /* Print nicely formatted data for one person */
    void printRecord( Node *p )
    {
      printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
            p->id, p->birthYear,p->height,p->weight,
            p->salary,p->firstName,p->lastName);
    }
    
    /* Ask the user for the id of a person */
    int getID()
    {
      int id;
      
      printf("id? (enter -1 to quit):");
      gets( buffer );
      while ( sscanf( buffer, " %d", &id ) != 1 )
      {
        printf("re-enter id: ");
        gets( buffer );
      }
      return id;
    }
    
    void readData( FILE* input, Node *head )
    {
      int j;
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
      int flag;   /* count of successful conversions in scanf */
    
      /* Read in the File, copying data into the array */
      while ( fgets( buffer, bufferSize, input) )
      { j++;
        /* Read in the data */
        flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               id, birthYear, height, weight, salary,
               lastName, firstName );
         
        if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
        insertFirst(&head , id, birthYear, height, weight, salary, firstName, lastName);
    
        
        head = head->link;
      }
    }
    
    freeList()
    {
    }
    int main()
    {
      FILE *input;
      Node *head = NULL;
      int j=0;
      char fileName[bufferSize];
    
      /* Ask user for input file; open file */
      printf("Name of input file: ");
      gets( buffer );
      sscanf( buffer, "%s", fileName );
    
      /* Open input file for reading */
      if ( (input=fopen( fileName, "r"))==NULL )
      {
        perror("input file did not open ");
        exit( EXIT_FAILURE );
      }
      
      /* Read in the data */
      readData( input, head );
      
      /* Process Queries until user enters -1 */
      int id = 0 ;
      id = getID();
      
      while ( id != -1 )
      {
        /* Linear search */
        while(head->link != NULL)
        {
        if ( head->id == id )
          printRecord( head );
        else
          printf("id %3d not found\n", id );
        head = head->link;
         }
        id = getID();
      }
    
      /* Write out the data in order of id */
      printf("\n\nAll Records:\n");
      while(head->link != NULL)
        if ( head->id != 0 )
          printRecord( head );
    
      fclose( input );
      system("pause");
    }
    Last edited by DLH112; 12-07-2009 at 03:32 PM.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    It compiles, but crashes upon running. uses this text file
    Code:
    081 1986 71 143 029198 Baum Adam
    015 1968 65 120 020509 Dente Al
    094 1960 74 155 029836 Fresco Al
    098 1986 51 073 037558 Romeo Alf
    090 1987 55 086 024288 Katt Ali
    096 1976 57 092 043721 Wellington Biff
    037 1967 65 120 045186 Board Bill
    078 1980 55 086 032640 Ding Bill
    035 1980 75 160 016368 Foldes Bill
    051 1956 61 105 016458 Loney Bill
    080 1959 76 164 033065 Waggon Chuck
    049 1962 56 089 045914 Reed Claire
    007 1968 71 143 022674 Graff Corey
    058 1964 54 082 043989 Dunn Count
    082 1987 76 164 025168 Deeds Darren
    056 1982 56 089 016131 Rhea Daryl
    070 1966 52 076 025435 Bird Earl
    088 1974 62 109 042298 Parker Ford
    044 1984 58 095 042554 Green Forrest
    004 1955 49 068 041363 Nutt Hazle
    041 1990 49 068 020800 Clare Heidi
    095 1975 55 086 044637 Back Helen
    068 1989 74 155 015095 Walker Jay
    077 1991 75 160 047540 Poole Jean
    032 1958 67 127 024772 Warm Luke
    074 1989 54 082 031092 Doyle Lynn
    039 1974 64 116 040202 Liam Lynn
    055 1966 74 155 023705 Lestor Mo
    005 1978 52 076 026089 Kuehl Molly
    016 1965 51 073 040148 Lane Penny
    060 1963 68 131 020028 Wise Penny
    075 1985 53 079 033138 Leeves Russell
    014 1982 66 123 036958 Sprout Russell
    073 1990 75 160 046084 Morrow Tom
    061 1976 55 086 039994 Gunn Tommy
    001 1979 54 082 038470 Melon Walter
    027 1952 50 071 027129 Rinn Wanda
    for input. Going over it with my own logic isn't helping me, I can't really see anything wrong...

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DLH112 View Post
    for input. Going over it with my own logic isn't helping me, I can't really see anything wrong...
    Right. Do you know why?

    Quote Originally Posted by mk27
    you are doing this ass-backward. You are also trying to write all the code at once and then go back and correct it, which is a BAD way to code.
    Even better -- now you want someone else to go back and correct it!

    You may have done one linked list exercise before but CLEARLY -- and I am not trying to insult you, I am just telling you the truth -- you still do not have much grasp of the basic concepts involved.

    I got a fortune cookie the other day, it said "repetition is the mother of skill". Start again, and work the link list framework out FIRST. If it is supposed to be based on your previous assignment, literally use that code. Keep what you have here in case you want to refer to some part of it. None of the time has been wasted, even if it did not produce anything usable. You are learning. The easiest part is adding in the data specifics. From a modular/framework perspective, it is totally possible to maintain a "linked list library" that involves no data at all, but that can be quickly and easily adapted to deal with anything. Handling the data is done AFTER you have a framework to handle it.

    And *always* write in such a way that you can compile and test what you are doing every 5-10 lines.
    Last edited by MK27; 12-07-2009 at 05:25 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    I know that you're right, and I apologize for the um irresponsible request, but I really hate the feeling that just changing 1 or 2 things that I'm overlooking could make it run perfectly.
    I will start from this
    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    struct NODE
    {
    struct NODE *link;
    int value;
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int val )
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->value = val;
    node->link = *hptr;
    *hptr = node;
    return node;
    }
    
    void traverse( Node *p )
    {
    while ( p != NULL )
    {
    printf("%d ", p->value );
    p = p->link;
    }
    }
    
    void freeList( Node *p )
    {
    Node *temp;
    while ( p != NULL )
    {
    temp = p;
    p = p->link;
    free( temp );
    }
    }
    
    int countTarget(Node *head, int target)
    {
     int count = 0;
     while (head != NULL)
     { 
       if (head->value == target)
         count++;
       head = head->link; 
     }
     return count;  
    }
    
    incrementEach(Node *head, int amount)
    {
      while(head != NULL)
      { 
        head->value = head->value + amount;
        head = head->link;
      }
    }
    
    int main()
    {
      Node *head = NULL;
      int j, amount = 3;
    
      for ( j=0; j<13; j++ )
       {
         insertFirst(&head, j);
       }
    
      printf("The linked list contains: \n");
      traverse(head);
      printf("the target value occured %d times in the list \n", countTarget(head, 12)) ;
      incrementEach(head, amount);
      printf("With all of the values incremented by %d, the list looks like this: \n", amount);
      traverse(head);
      freeList(head);
      printf("The list has been cleared ");
      traverse(head);
      
      
    
    
      system("pause");
      return 1;
    }
    i've started it: will post when I get somewhere with it...
    Last edited by DLH112; 12-08-2009 at 06:34 AM.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    I've restarted it and perhaps found my problem. readData...
    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    struct NODE
    {
    struct NODE *link;
    int value;
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int val )
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->value = val;
    node->link = *hptr;
    *hptr = node;
    return node;
    }
    #define bufferSize 132
    char buffer[bufferSize];  /* input buffer */
    
    void readData( Node *p, FILE *input )
    {int n;
    while(fgets(buffer, bufferSize, input))
    {printf("does it get here");
     sscanf(buffer, "%d", &p->value); /* p->value and &p->value crash at the same spot*/
     
     p= p->link;
    }
    while ( p != NULL )
    {
    printf("%d ", p->value );
    p = p->link;
    }
    }
    
    void freeList( Node *p )
    {
    Node *temp;
    while ( p != NULL )
    {
    temp = p;
    p = p->link;
    free( temp );
    }
    }
    
    
    
    int main()
    {
       FILE *input;
      Node *head = NULL;
      int j=0;
      char fileName[bufferSize];
    
      /* Ask user for input file; open file */
      printf("Name of input file: ");
      gets( buffer );
      sscanf( buffer, "%s", fileName );
    
      /* Open input file for reading */
      if ( (input=fopen( fileName, "r"))==NULL )
      {
        perror("input file did not open ");
        exit( EXIT_FAILURE );
      }
      printf("does it get here");
      /* Read in the data */
      readData(head, input );
      
      
    
    
      system("pause");
      return 1;
    }
    crashes at the sscanf...because i'm using p->value? I haven't been able to write a readData that doesn't make it crash yet and really could use some help.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Can someone please give me a hint as to what I'm doing wrong? I've fussed around with this so much and it just won't run properly.
    Code:
    void readData( Node *p, FILE *input )
    {
    while(fgets(buffer, bufferSize, input))
    {printf("does it get here");
     sscanf(buffer, "%d", &p->value);
     p= p->link;
    }
    while ( p != NULL )
    {
    printf("%d ", p->value );
    p = p->link;
    }
    }

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    finally got this to work, though it has too much in main(), I had problems/crashes every other way...
    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    #define nameSize 50
    struct NODE
    {
    struct NODE *link;
    int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
    };
    typedef struct NODE Node;
    Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
    {
    Node *node = (Node *)malloc( sizeof( Node ) );
    node->id = id;
    node->birthYear = birthYear;
    node->height = height;
    node->weight = weight;
    node->salary = salary;
    strcpy(node->firstName, firstName);
    strcpy(node->lastName, lastName);
    node->link = *hptr;
    *hptr = node;
    return node;
    }
    #define bufferSize 132
    char buffer[bufferSize];  /* input buffer */
    
    
    void freeList( Node *p )
    {
    Node *temp;
    while ( p != NULL )
    {
    temp = p;
    p = p->link;
    free( temp );
    }
    }
    
    int getID()
    {
      int id;
      
      printf("id? (enter -1 to quit):");
      gets( buffer );
      while (sscanf(buffer, " %d", &id) != 1 )
      { 
        printf("re-enter id: ");
        gets( buffer );
      }
      return id;
    }
    void printData(Node *p)
    {
      while(p != NULL)
       {  
      printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
            p->id, p->birthYear,p->height,p->weight,
            p->salary,p->firstName,p->lastName);
            p = p->link;
      }
    }
    
    
    int main()
    {
       FILE *input;
      Node *head2, *head = NULL;
      int flag, request, j=0;
      int id, birthYear, height, weight, salary;
      char firstName[nameSize], lastName[nameSize];
      char fileName[bufferSize];
    
      /* Ask user for input file; open file */
      printf("Name of input file: ");
      gets( buffer );
      sscanf( buffer, "%s", fileName );
    
      /* Open input file for reading */
      if ( (input=fopen( fileName, "r"))==NULL )
      {
        perror("input file did not open ");
        exit( EXIT_FAILURE );
      }
      /* Read in the data */
      while(fgets(buffer, bufferSize, input))
    {
     flag = sscanf( buffer, "%d %d %d %d %d %s %s",
               &id, &birthYear, &height, &weight, &salary,
               &lastName, &firstName );
               if ( flag != 7 )
        {
          fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
          fclose( input );
          exit( EXIT_FAILURE );
        }
     insertFirst(&head , id, birthYear, height, weight, salary, firstName, lastName);
    }
       request = getID();
       head2 = head;
       while(head != NULL)
       { if(head->id == request)
         printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
            head->id, head->birthYear,head->height,head->weight,
            head->salary,head->firstName,head->lastName);
           head = head->link;
       }
      printf("printing total list: \n");
      printData(head2);
      freeList(head2);
      printf("list has been cleared: \n");
      printData(head2);
      
      system("pause");
      return 1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM