Thread: Linked List Problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    Linked List Problem

    Original objective:
    I have made a program that is supposed to add five employees onto a linked list. It then inputs some data on their wage and hours worked, and finally it's supposed to print the gross pay and all the data into a nice little printout.

    Problem:
    After the program has inputed all of the data that isn't inputed from the user, it proceeds to input the rest of the data from the user. However, when it gets to the line indicated below, the program crashes. In the first part of the program, it has no problem accessing the second item in the list, but from this part on, it does.

    Question:
    Does anyone see the problem within my code that is causing it to crash?

    Code:
    Code:
    /* Note: this program has little structure, so it may be hard to read. */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* Initializes global structure s_employee. */
    struct s_employee
    {
      char name[20];
      long id_number;
      float wage;
      float hours;
      float overtime;
      float gross;
    
      struct s_employee *next; /* Pointer to the next structure in line. */
    };
    
    /* This statement declares a pointer type called employee that points to an
       s_employee structure. */
    typedef struct s_employee *employee;
    
    int main()
    {
          /* Define the initial structure and the structures that follow it. */
          employee initial;
          employee current;
    
          /* Define other variables. */
          int i = 0, j = 0;
    
          /* Initialize the first employee data. */
          initial = (employee)malloc(sizeof(employee));
          current = initial; /* Set current item in the list to point to the first item. */
    
          /* Build the linked list. */
          for (i = 1; i < 5; i++)
            {
               current->next = (employee)malloc(sizeof(employee)); /* Initialize the new employee. */
               current = current->next; /* Set the current employee to the new one. */
            }
    
          /* Set the tail to NULL. */
          current->next = NULL;
    
          /* Set the data for each employee. */
          current = initial;
          strcpy(current->name, "Connie Cobol");
          current->id_number = 98401L;
          current->wage = 10.6;
    
          /* Go to the next employee and set the data. */
          current = current->next;
          strcpy(current->name, "Mary Apl");
          current->id_number = 526488L;
          current->wage = 9.75;
    
          /* Go to the next employee and set the data. */
          current = current->next;
          strcpy(current->name, "Frank Fortran");
          current->id_number = 765349L;
          current->wage = 10.5;
    
          /* Go to the next employee and set the data. */
          current = current->next;
          strcpy(current->name, "Jeff Ada");
          current->id_number = 34645L;
          current->wage = 12.25;
    
          /* Go to the next employee and set the data. */
          current = current->next;
          strcpy(current->name, "Anton Pascal");
          current->id_number = 127615L;
          current->wage = 8.35;
    
          /* Input the Number of hours each person worked, their overtime, and their gross pay. */
          current = initial;
          for (i = 0; i < 5; i++)
          {
             printf("Please enter the number of hours worked for %s, ID# %d: ", current->name, current->id_number);
             scanf("%f", &current->hours);
    
    
             if( current->hours > 40 )
             {
                current->overtime = current->hours - 40;
                current->hours = 40;
             }
             else
               current->overtime = 0;
             current->gross = current->hours*current->wage + (1.5)*current->overtime*current->wage;
    
             current = current->next; /* Set to the next item in the list. */
             printf("%f", current->wage);
          }
    
          current = initial; /* Set it back to the first item in the list. */
    
          /* Print header to the data list. */
          for(i = 0; i < 80; i++)
            printf("-");
          printf("\nName \t\t Clock# \t Wage \t Hours \t OT \t Gross\n");
          for(i = 0; i < 80; i++)
            printf("-");
          for(i = 0; i < 5; i++)
          {
            printf("%s \t\t %06d \t %2.2f \t %2.1f \t %2.1f \t $%3.2f\n", current->name, current->id_number, current->wage, current->hours, current->overtime, current->gross);
            current = current->next;
          }
    
    
          /* Free all the items in the linked list including the initial. */
          for ( j = 3; j > -1; j--)
          {
            current = initial;
            for ( i = 0; i < j; i++)
               current = current->next;
            free(current);
          }
          free(initial);
    
          system("PAUSE");
          return 0;
    }

    code tags added by Hammer

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Don't add all that junk until you get the list working, everyone does that and it makes it harder. I fixed the list, but got rid of the stuff not relating to the list. You can add it back.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct s_employee
    {
        long id_number;
        struct s_employee *next;
    };
    
    typedef struct s_employee *employee;
    
    int main()
    {
        employee initial;
        employee current;
        employee save;
        
        int i = 0;
        
        initial = (employee)malloc(sizeof(struct s_employee));
        current = initial;
        
        for (i = 1; i < 5; i++)
        {
            current->next = (employee)malloc(sizeof(struct s_employee));
            current = current->next;
        }
        
        /* Set the tail to NULL. */
        current->next = NULL;
        
        /* Set the data for each employee. */
        current = initial;
        current->id_number = 98401L;
        
        /* Go to the next employee and set the data. */
        current = current->next;
        current->id_number = 526488L;
        
        /* Go to the next employee and set the data. */
        current = current->next;
        current->id_number = 765349L;
        
        /* Go to the next employee and set the data. */
        current = current->next;
        current->id_number = 34645L;
        
        /* Go to the next employee and set the data. */
        current = current->next;
        current->id_number = 127615L;
    
        for(current = initial; current; current = current->next)
            printf("%ld\n", current->id_number);
        
        /* Free all the items in the linked list including the initial. */
        for ( current = initial; current; current = save)
        {
            save = current->next;
            free(current);
        }
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM