Thread: Linked list not resetting between functions

  1. #1
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15

    Linked list not resetting between functions

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*Structure declaration*/
    struct employee
    {
         char name[20];
         long id_number;
         float wage;
         float hours;
         float overtime;
         float gross;
         struct employee *next;
    };
    
    /*Variable declaration (global)*/
    struct employee *current_ptr, /*point to current node*/
                    *start_ptr;   /*point to first node*/
    
    void get_info (struct employee *emp1)
    {
         struct employee *tmp;
         char first_name[7];
         char last_name[8];
         int temp;
         
         for (temp=0; temp<5; ++temp)
         {
              for (tmp = emp1; tmp ; tmp = tmp->next)
              {
                   printf ("Enter employee #%i's name: ", temp+1);
                   scanf ("%s %s", &first_name, &last_name);
                   strcpy (tmp->name, first_name);
                   strcat (tmp->name, " ");
                   strcat (tmp->name, last_name);
                   printf ("Enter %s's ID #: ", tmp->name);
                   scanf ("%i", &tmp->id_number);
                   printf ("Enter %s's wage: ", tmp->name);
                   scanf ("%f", &tmp->wage);
                   printf ("\n");
              }
         }
    }
    
    
    void get_hours (struct employee *emp1)
    {
         struct employee *tmp;
         int temp;
    
         for (temp=0; temp<5; ++temp)
         {    
              for (tmp = emp1; tmp ; tmp = tmp->next)
              {
                   printf ("Enter %s's hours worked: ", tmp->name);
                   scanf ("%f", &tmp->hours);
              }
         }
    }
    
    
    void calculate_overtime (struct employee *emp1)
    {
         struct employee *tmp;
         int temp;
         
         for (temp=0; temp<5; ++temp)
         {     
              for (tmp = emp1; tmp ; tmp = tmp->next)
              {
                   if (tmp->hours > 40)
                        tmp->overtime = tmp->hours - 40;
                   else
                        tmp->overtime = 0;
              }
         }
    }
    
    
    void calculate_gross (struct employee *emp1)
    {
         struct employee *tmp;
         int temp;
         
         for (temp=0; temp<5; ++temp)
         {     
              for (tmp = emp1; tmp ; tmp = tmp->next)
              {
                   if  (tmp->overtime > 0)
                        tmp->gross = (tmp->wage * 40) + ((tmp->wage * 1.5) * tmp->overtime);
                   else
                        tmp->gross = (tmp->wage * tmp->hours) + ((tmp->wage * 1.5) * tmp->overtime);
              }
         }
    }
    
    
    void output (struct employee *emp1)
    {
         struct employee *tmp;
         int temp;
         
         printf ("\n\n");
         printf ("--------------------------------------------------------------------------\n");
         printf ("Name              Clock #    Pay Rate     Hours      OT      Gross Pay\n");
         printf ("--------------------------------------------------------------------------\n");
    
         for (temp=0; temp<5; ++temp)
         {      
              for (tmp = emp1; tmp ; tmp = tmp->next)
              {
                   printf ("%-13s     %06d     $%5.2f      %5.1f     %5.1f     $%6.2f\n",
                   tmp->name, tmp->id_number, tmp->wage, tmp->hours,
                   tmp->overtime, tmp->gross);
              }
         }
    }
    
    
    int main ()
    {
         /* Setup storage for start_ptr (first node)*/
         start_ptr = (struct employee *) malloc (sizeof(struct employee));
         current_ptr = start_ptr;
                         
         /*Execute functions*/
         get_info (start_ptr);
         get_hours (start_ptr);
         calculate_overtime (start_ptr);
         calculate_gross (start_ptr);
         output (start_ptr);
    
         printf ("\n");
         /*Keep window open*/
         system("PAUSE");
    }
    The program is supposed to get 5 peoples' (nodes) info in 2 separate steps, do the calculations, then display them. My problem is that the structures are not resetting to the 1st node between functions. Right now my table display shows 5 copies (rows) of the last data set that I enter. Any suggestions on how I can correct this? Also, if you say any other random sloppiness in my code feel free to let me know. I'd rather do it right than just do it.

    Me dons the flamesuit...

    Thanks in advance.
    Someday I may actually ANSWER a question...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Let me warn you, I am very bad at reading others code. And so I may not be entirely accurate/complete in my answers.

    I see a few problems but lets cross the bridge when we get there.

    First problem is, nowhere in the user defined functions, are you allocating memory for the "next" pointer. But you are going on populating data.

    And thats the reason why you see only the last set of data. Infact, I am surprised its showing you even that. I might be wrong here but the program should have crashed. What compiler are you using?

    Anoop.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    I can see a number of problems immediately, the main being the formation of the linked list. Ot the lack thereof.

    You see, in main() you have allocated room for a single employee structure, and nowhere else is any malloc'ing happening. Yet you want to store information on 5 employees!

    In each of your functions you are looping 5 times. I think the loop should be in main() and not in those other functions!

    I'll post the corrected code once I get a chance to fix it.
    Beware the fury of a patient man.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>In each of your functions you are looping 5 times. I think the loop should be in main() and not in those other functions!

    Should not be much of a problem if you pass the pointer to a pointer.

    Anoop.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    I'm not sure exactly what you were after, but here is the very quick correction I whipped up.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*Structure declaration*/
    struct employee {
        char name[20];
        long id_number;
        float wage;
        float hours;
        float overtime;
        float gross;
        struct employee *next;
    };
    
    void get_info(struct employee *emp) {
        char first_name[7];
        char last_name[8];
        int temp;
         
        printf("Enter employee's name: ");
        scanf("%s %s", first_name, last_name);
        strcpy(emp->name, first_name);
        strcat(emp->name, " ");
        strcat(emp->name, last_name);
        printf("Enter %s's ID #: ", emp->name);
        scanf("%i", &(emp->id_number));
        printf("Enter %s's wage: ", emp->name);
        scanf("%f", &(emp->wage));
        printf("\n");
    }
    
    void get_hours(struct employee *emp) {
        printf ("Enter %s's hours worked: ", emp->name);
        scanf ("%f", &(emp->hours));
    }
    
    void calculate_overtime(struct employee *emp) {
        if (emp->hours > 40)
            emp->overtime = emp->hours - 40;
        else
            emp->overtime = 0;
    }
    
    void calculate_gross(struct employee *emp) {
        if  (emp->overtime > 0)
            emp->gross = (emp->wage * 40) + ((emp->wage * 1.5) * emp->overtime);
        else
            emp->gross = (emp->wage * emp->hours) + ((emp->wage * 1.5) * emp->overtime);
    }
    
    
    void output(struct employee *employees[]) {
        struct employee *roving;
        int temp;
         
        printf ("\n\n");
        printf ("--------------------------------------------------------------------------\n");
        printf ("Name              Clock #    Pay Rate     Hours      OT      Gross Pay\n");
        printf ("--------------------------------------------------------------------------\n");
    
        roving = employees[0];
        for (temp = 0; temp < 5; temp++) {
            printf ("%-13s     %06d     $%5.2f      %5.1f     %5.1f     $%6.2f\n",
            roving->name, roving->id_number, roving->wage, roving->hours,
            roving->overtime, roving->gross);
            roving = roving->next;
         }
    }
    
    
    int main(void) {
        struct employee *employees[5];
        int i;
        
        for (i = 0; i < 5; i++) {
            employees[i] = malloc(sizeof (struct employee));
            get_info(employees[i]);
            get_hours(employees[i]);
            calculate_overtime(employees[i]);
            calculate_gross(employees[i]);
        }
    
        for (i = 0; i < 5; i++) {
           if (i == 4) {
                employees[i]->next = NULL;
            } else {
                employees[i]->next = employees[i + 1];
            }
        } 
        output(employees);
        printf("\n");
        /*Keep window open*/
        system("PAUSE");
    }
    NOTE: I am aware that there are still many problems/poorly coded areas/lack of error checking. However, I really must get going. If no one has corrected the rest when I get back I'll make an attempt.

    EDIT: It does work atm also. Try it.
    Beware the fury of a patient man.

  6. #6
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Zainny,
    Your code works fine, but is this part the linked list?
    Code:
    for (i = 0; i < 5; i++) {
           if (i == 4) {
                employees[i]->next = NULL;
            } else {
                employees[i]->next = employees[i + 1];
            }
        }
    Not a criticism at all; just trying to understand what's going on in the code.
    Someday I may actually ANSWER a question...

  7. #7
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Salem,
    Is there any way of putting your linked list code directly in main()? Either that or making it a function that gets called directly from main()? I see the list append function (which is the linked list?), but I don't see any calls to it in main. I see the word "list" throughout the code though, which in itself is confusing to me as I've never heard of a variable type called "list". I guess your code is way beyond my understanding, so I was hoping you could "dumb it down" for me so that I can understand what its doing.

    Thanks in advance...
    Someday I may actually ANSWER a question...

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Zainny,
    Your code works fine, but is this part the linked list?
    Yes. In that section I was linking each of the elements of the linked list together.

    Salem is of course correct in asserting that my approach is a very poor way of forming a linked list and in code I have written with linked lists I have never done it like that. However, my intention was never to write new code for you, but rather to correct the existing code so that it works. I'm quite willing to correct syntactical/logical errors, but writing completely new (and oft large) sections I will not do. It wastes my time, and you get to practice copying and pasting!

    If I were you I would not try to merge the Linked List with you employee's program. Write a seperately contained and generic linked list (abstract data type - ADT) and then the code will also be later re-usable for other projects.

    Cheers,
    Z.
    Beware the fury of a patient man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM