Thread: I am having trouble with my Linked List homework. Might you take a gander at this?

  1. #1
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59

    I am having trouble with my Linked List homework. Might you take a gander at this?

    These are the directions I am given:
    1. Create an employeeData struct to represent employees. Your struct should have the following fields:
    - An integer EMP_ID,
    - name which stores the candidate’s name as a char array with no more
    than 20 letters;
    - dept an integer that stores the department in which the employee is
    employed
    - an integer rank indicating the employee’s position in the company
    - salary, a double for the employee ‘s annual income


    2. Write a function initializeList which creates a linked list of employee information where each node contains an employeeData struct and a pointer called next. The employee information will be read from an input file empInfo.txt. This file contains data for each employee on a single line. A line of data beginning with zero is used to indicate the end of the input data

    3. Your program should also provide the following other functions:
    a) a void function add, which takes an integer, string, integer, integer and double from user input and creates a new node which is added to the linked list so that the list remains sorted in ascending order on EMP_ID. Your function should then print a message indicating that the addition is completed. You may assume that this employee is not already in the linked list.

    b)a void function delete which takes one integer parameter and deletes the employee record with that EMP_ID from the linked list. Your function should then print a message indicating that the deletion is completed. You may assume that this employee is in the linked list.


    c) a void function modify, which accepts two parameters- an integer and a double. This function modifies the linked list node with the EMP_ID that matches the integer, and changes its salary value to the double provided. Your function should then print a message indicating that the modification is completed.

    d) a void function query which accepts a single integer parameter and prints out the names of all of the employees with that rank.

    e) a void function called print which prints out the contents of the linked list using the format shown below in the sample output.




    So far I have completed number 1 only and I am having a hard time knowing how to get my initialize list function to work properly.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <strings.h>
    
    
    FILE * data;
    
    
    typedef struct{              // Main struct containing needed data types
        int EMP_ID;
        int dep;
        int rank;
        double salary;
        char name[20];
        } employeeData;
    
    
    struct node { employeeData employeeData; struct node *next; };
    struct node *head, *tail, *tmp;
    
    
    initializeList ( employeeData, FILE *data )
    {
        data = fopen( "empInfo.txt","r" );
        head = ( employeeData * ) malloc( sizeof *head );
        tail = ( employeeData * ) malloc( sizeof *tail );
        head->next = tail;
        tail->next = tail;
        fscanf("%d%d%d%f%s", EMP_ID, dep, rank, salary, name);
    }
    void add ( employeeData )
    {
         tmp->next = tmp->next->next;
    }
    void delete ( struct node *tmp )
    {
         tmp->next = tmp->next->next;
    }
    void modify ( employeeData )
    {
         tmp->next = tmp->next->next;
    }
    void print ( employeeData )
    {
         tmp->next = tmp->next->next;
    }
    
    
    int main (void)
    {
        employeeData = {0,0,0,0,0};
    
    
    
    
    
    
        while (getchar () != '\n')
        getchar ();
    
    
        fclose( data );
        return 0;
    }

    Ive read on Google, watched videos, but this homework seems different from all the examples Ive seen. All the examples Ive seen make a struct and call it node and pass some data type (usually an int value), but in this homework it looks like we are supposed to point to a whole struct with a struct pointer?


  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Mike Garber View Post
    So far I have completed number 1 only and I am having a hard time knowing how to get my initialize list function to work properly.
    You haven't actually completed #1 yet. You never read the file (well, you call fscanf, but you call it incorrectly -- clearly you didn't even try to compile this). Start by making initializeList read the file and print it to screen. Don't forget to check that fopen succeeds before you go reading from the file. Also, don't forget to close the file (you do this in main -- it's better to close the file in the same function that opens it, so it's easier to ensure it gets closed). I suggest using fgets to read a line of input and sscanf to parse out employee data or check for the last line marker, "0". Make sure you check the return value of fgets in case there was a problem reading the file (EOF, error) and check the return value of sscanf to make sure you actually parsed all the fields, otherwise you might insert bogus data into your list.

    initializeList should have an explicit return type. I suggest int, so that you can return values for success/failure. After all, if you can't read in the data, you should probably just print an error and exit.

    Another problem with your current version of #1 is that you're allocating two nodes, one each for head and tail, that contain no employee information. You don't need "dummy" nodes in this case, an empty list should probably start with head and tail both pointing to NULL. Also, you're casting malloc incorrectly. head and tail are struct node *, so why are you casting them to employeeData *? In any case, you shouldn't cast malloc at all, read this: FAQ > Casting malloc - Cprogramming.com.

    Once you can successfully read the file and print it back, then you can work on actually allocating nodes and storing the data in a list.

    Also, you really, really, really should get rid of global variables. You don't need them. Instead, declare them in the appropriate functions and pass them to other functions as needed.
    Quote Originally Posted by Mike Garber View Post
    Ive read on Google, watched videos, but this homework seems different from all the examples Ive seen. All the examples Ive seen make a struct and call it node and pass some data type (usually an int value), but in this homework it looks like we are supposed to point to a whole struct with a struct pointer?
    When it comes to understanding linked lists, it is immensely helpful to actually draw out the lists on paper and manipulate the "pointers" by hand. In this case, I think the type of data a list stores is secondary to gaining a fundamental understanding of linked lists in general. That is, you seem to be having trouble with more than just the type of data stored in the list. You seem to be lacking fundamentals like reading from a file and using structs (cf. your fscanf call). Once you get the basics ironed out, and can read/print the contents of the file, then we'll work on the linked list itself.

  3. #3
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    I have added more to my code and I fixed some of the issues, but now my add function isnt working when its called, my program wont even compile..
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    #define NAME_LENGTH 20
    
    
    typedef struct employeeData                   //employee struct declaration
    {
        int EMP_ID;
        char* name;
        int dept;
        int rank;
        double salary;
    
    
        struct employeeData *next;
    }employee;
    
    
    employee* initializeList( int EMP_ID, char* name, int dept, int rank, double salary ) //initialize LL function for employees
    {
        employee* tmp = ( employee* )( malloc(sizeof(struct employeeData ) ) );
        tmp->name = ( char* )malloc( sizeof( char )*NAME_LENGTH );
    
    
        strcpy( tmp->name, name );
        tmp->salary = salary;
        tmp->EMP_ID = EMP_ID;
        tmp->dept = dept;
        tmp->rank = rank;
        tmp->next = NULL;
    
    
        return tmp;
    }
    
    
    employee* insertEmp( employee* head, employee* tmp )            //function to insert employees into the LL
    {
        employee* current = NULL;
        current = head;
        if( current == NULL || strcmp( current->name, tmp->name ) > 0)  // checking order
        {
                tmp->next = current;
                return tmp;
        }
        else
        {
                while( current->next != NULL && strcmp( current->next->name, tmp->name ) < 0 ) //changing order
                {
    
    
                        current = current->next;
                }
        }
                tmp->next = current->next;
                current->next = tmp;
                return head;
    
    
    }
    
    
    void query( employee *head, int submenu )  //query by employee rank function
    {
        printf( "\nEMP name\n" );
        employee* current;
        current = head;
        while ( current != NULL )
        {
    
    
                if( current->rank == submenu )
                {
                    printf( "%s\n", current->name );
                    if( current->next == NULL )
                    {
                    break;
                    }
                    current = current->next;
                }
    
    
        current = current->next;
        }
        return;
    }
    
    
    void printlist( employee* head ) //print result function
    {
        employee* current;
        current = head;
        printf( "EMP_ID\t EMP NAME\t\t DEPT\t\t RANK\t\t SALARY " );
        while ( current != NULL )
        {
            printf( "\n%d\t %s \t\t %d\t\t %d\t\t %d\n", current->EMP_ID, current->name, current->dept, current->rank, current->salary );
            current = current->next;
        }
        return;
    }
    
    
    int main( void )
    {
        FILE* data = fopen( "empInfo.txt", "r" );                 //read in emp data here
        int EMP_ID, dept, rank, menu_choice = -1, submenu;
        double salary;
        char* name = ( char* )malloc( sizeof( char* )*NAME_LENGTH );
    
    
        employee *head = NULL;
    
    
        while( !feof( data ) )  //initialize the list by calling its function
        {
            fscanf( data, "%d %s %d %d %d", &EMP_ID, name, &dept, &rank, &salary );
                {
                    if ( EMP_ID == 0 )
                        break;
                }
                employee* hold = initializeList( EMP_ID, name, dept, rank, salary );
                head = insertEmp( head, hold );
        }
    
    
        while ( menu_choice != 0 )  //Menu declaration and listed choices
        {
            printf( "\nPlease select an action from the following menu\n" );
            printf( "1 to add a new employee\n" );
            printf( "2 to delete an employee\n" );
            printf( "3 to modify an employee record\n" );
            printf( "4 to query employees by rank\n" );
            printf( "5 to print all employee information\n" );
            printf( "0 to exit the program\n" );
            scanf( "%d", &menu_choice );
    
    
            if( menu_choice == 1 )
            {
                printf( "Please enter the information for the employee to be added.\n" );
                scanf( "%d%s%d%d%f", &head );
                insertEmp( head,  tmp );
                menu_choice = -1;
            }
    
    
            if ( menu_choice == 2 )
            {
                printf( "Choice 2\n" );
                menu_choice = -1;
            }
    
    
            if ( menu_choice == 3 )
            {
                printf( "Choice 3\n" );
                menu_choice = -1;
            }
            if ( menu_choice == 4 )
            {
                printf( "Please provide the rank of the employee you would like to query.\n" );
                scanf( "%d", &submenu );
                query( head, submenu );
                menu_choice = -1;
            }
            if ( menu_choice == 5 )
            {
                printlist( head );
                menu_choice = -1;
            }
        }
        fclose( data );
    
    
        while (getchar () != '\n')   //I use this instead of system ("PAUSE")
        getchar ();
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    c - Is this correct implementation of my initialize List function for a Linked list? - Stack Overflow
    1. Create An EmployeeData Struct To Represent Employees.... | Chegg.com

    You're either cross-posting, which is against our forum rules and general forum etiquette, or you're copying somebody else's crappy code. It looks to me more like copying, but either way, I'm not going to help you anymore until you make a serious effort to actually solve this yourself.

    We've given you lots of good advice over the past year, and you seem to not take much of it to heart. That doesn't encourage us to keep spending our effort and spare time helping you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list trouble
    By Konstantinos in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2013, 10:42 PM
  2. Linked list trouble...
    By JM1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2011, 04:02 AM
  3. please help..linked list homework assignment due tomorrow..
    By rocketman03 in forum C Programming
    Replies: 2
    Last Post: 11-23-2008, 06:32 PM
  4. Linked list trouble
    By sand_man in forum C Programming
    Replies: 2
    Last Post: 02-08-2005, 01:14 PM
  5. Having trouble with Linked List
    By hkmixxa in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2004, 03:25 AM

Tags for this Thread