One more question about linked lists?

This is a discussion on One more question about linked lists? within the C Programming forums, part of the General Programming Boards category; Ok here is the gist of my problem, I am trying to use arrays, linked lists, and functions all together ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Question One more question about linked lists?

    Ok here is the gist of my problem, I am trying to use arrays, linked lists, and functions all together and somehow make it all work.

    The idea is to have a number of records read in from a file one line at a time. However when I find a record that meets certain criteria, I branch off and create a dynamic linked list attached to my array structure. The problem I am having is not when I populate my array and lists it is when I read back the values in the linked-lists. It appears that I can only do this once.

    I have an idea that my problem is because I never point the list back to the "top", but I cannot figure out the best way to do so.

    I am starting to really get frustrated and confused by the linked-lists. And was hoping that some "pointers" yes poor pun intended... could be thrown my way.

    thanks everyone,
    shane

    Code:
    //Type Definitions
    typedef struct dep_db
    {
        char dpdnt_data[DPDNT_LENGTH + 1];
        struct dep_db* next;
    } DEP_DB_T;
    
    typedef struct tax_db
    {
        char name[NAME_LENGTH + 1];
        char ssn[SSN_LENGTH + 1];
        double dpdnts;
        char mar_stat;
        double tax_inc;
        DEP_DB_T* dep_list;
    } TAX_DB_T;
    
    pop_array2 (data_io, tax_record, &array_cnt);
    disp_taxpyr_dep (tax_record, array_cnt);
    
    void pop_array2 (FILE *readptr, TAX_DB_T *tax_record, int *array_cnt)
    {
        // Variable Declarations
        int x=0;    // Counter for array of structures
        int y=0;    // Counter for linked list
        int Len;
        DEP_DB_T *current;
    
        // Start Function Code
        for (x=0; !feof(readptr) && x < MAX_ARRAY_SIZE; x++)
        {
            fscanf (readptr, "\n%25c\n%11c%1lf\n%c%lf\n", tax_record[x].name, tax_record[x].ssn, &tax_record[x].dpdnts, &tax_record[x].mar_stat, &tax_record[x].tax_inc);
            tax_record[x].mar_stat = toupper(tax_record[x].mar_stat);
    
    
            if (tax_record[x].dpdnts != 0)
            {
                y = tax_record[x].dpdnts;
                current = tax_record[x].dep_list = (DEP_DB_T *) malloc(sizeof(DEP_DB_T));
                fgets (current -> dpdnt_data, DPDNT_LENGTH + 2, readptr);
                Len = strlen (current -> dpdnt_data);
                current -> dpdnt_data [Len-1] = '\0';
                y--;
    
                while (y != 0)
                {
                    if ((current->next = (DEP_DB_T *) malloc( sizeof ( DEP_DB_T ) ) ) == NULL )
                    {
                        printf ("Youre out of memory");
                    }
                    current = current -> next;
                    fgets (current -> dpdnt_data, DPDNT_LENGTH + 2, readptr);
                    Len = strlen (current -> dpdnt_data);
                    current -> dpdnt_data [Len-1] = '\0';
                    y--;
                }
                current -> next = NULL;
            }
        }
    
        *array_cnt = x;
    
        printf ("\n [%d] Records Read Into Taxpayer Information DB", *array_cnt);
    }
    
    void disp_taxpyr_dep (TAX_DB_T *tax_record, int array_cnt)
    {
        // Variable Declarations
        int x;  // FOR loop counter
    
        // Start Function Code
        printf ("\n\n ** Taxpayer List **");
        printf ("\n\n Taxpayer                    SSN           Number of    Marital   Taxable");
        printf ("\n                                           Dependents   Status    Income ");
        printf ("\n -------------------------   -----------   ----------   -------   -----------\n");
    
        for (x=0; x < array_cnt; x++)
        {
            printf ("\n %s   %s       %1.0lf        ", tax_record[x].name, tax_record[x].ssn, tax_record[x].dpdnts);
    
            if (tax_record[x].mar_stat == 'M')
                printf ("Married   ");
            else
                printf ("Single    ");
    
            printf ("$ %9.2lf", tax_record[x].tax_inc);
    
            if (tax_record[x].dpdnts != 0)
            {
                while (tax_record[x].dep_list != NULL)
                {
                    printf ("\n%s", tax_record[x].dep_list -> dpdnt_data);
                    tax_record[x].dep_list = tax_record[x].dep_list -> next;
                }
            }
        }
    
        printf ("\n\n\t\tPress <ENTER> To Return To Taxpayer Main Menu");
        getchar();
        getchar();
    }
    Any more code needed just ask.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    I think you're problem is here:
    Code:
    while (tax_record[x].dep_list != NULL)
    {
        printf ("\n%s", tax_record[x].dep_list -> dpdnt_data);
        tax_record[x].dep_list = tax_record[x].dep_list -> next;
    }
    You're reasignment of tax_record[x].dep_list meant that when you were done outputting the dependents list for that record, the pointer ended up being set to NULL. This is why you are only able to output the list once. You need another variable to walk through your linked-list:
    Code:
    DEP_DB_T* current = tax_record[x].dep_list;
    while (current != NULL)
    {
        printf ("\n%s", current -> dpdnt_data);
        current=current->next;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Thank You

    hk_mp5kpdw,

    Thank you...
    I will give that a shot

    Boy this is really getting complicated.

    It is funny, I can read books and books of programming and online msg boards, but nothing beats the frustration of implementation...

    -shane

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding comparing two linked lists
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 04:28 AM
  2. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 12:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 11:03 AM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21