Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c - Linked list text file loop

My program crashes under a certain instance/case, it complies and runs fine otherwise. We were given a text file formatted like so:

12   JackSprat   2     1    65000

13   HumptyDumpty  5   3    30000

17   BoPeep     2      3    30000

20   BoyBlue    3      2    58000

0

we are required to read from file and store into a struct using a linked list. so far my code looks like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME_LENGTH 20

typedef struct employeeData
{
    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)
{
    employee* ptr = (employee*)(malloc(sizeof(struct employeeData)));
    ptr->Name = (char*)malloc(sizeof(char)*NAME_LENGTH);

    strcpy(ptr->Name, Name);
    ptr->EMP_ID = EMP_ID;
    ptr->Dept = Dept;
    ptr->Rank = Rank;
    ptr->Salary = Salary;
    ptr->next = NULL;

    return ptr;
}

employee* insert_by_employee(employee* head, employee* ptr)
{
    employee* current = NULL;
    current = head;
    if(current == NULL || strcmp(current->Name, ptr->Name) > 0)
    {
            ptr->next = current;
            return ptr;
    }
    else
    {
            while(current->next != NULL && strcmp(current->next->Name, ptr->Name) < 0)
            {

                    current = current->next;
            }
    }
            ptr->next = current->next;
            current->next = ptr;
            return head;

}

void query(employee *head, int submenu)
{
    printf("
Emp name
");
    employee* current;
    current = head;
    while (current != NULL)
    {
        if(current->Rank == submenu)
        {
            printf("%s
", current->Name);
            current = current->next;
        }
        current = current->next;
    }
    return;
}

void printlist(employee* head)
{
    employee* current;
    current = head;
    printf("EMP_ID	 EMP NAME		 DEPT		 RANK		 SALARY ");
    while ( current != NULL)
    {
        printf("
%d	 %s 		 %d		 %d		 %d
", current->EMP_ID, current->Name, current->Dept, current->Rank, current->Salary);
        current = current->next;
    }
    return;
}

int main(void)
{
    FILE* ifp = fopen("empInfo.txt", "r");
    int EMP_ID, Dept, Rank, menu_choice = -1, submenu;
    double Salary;
    char* Name = (char*)malloc(sizeof(char*)*NAME_LENGTH);

    employee *head = NULL;

    while(!feof(ifp))
    {
        fscanf(ifp, "%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 = insert_by_employee(head, hold);
    }

    while (menu_choice != 0)
    {
        printf("
Please select an action from the following menu
");
        printf("1 to add a new employee
");
        printf("2 to delete an employee
");
        printf("3 to modify an employee record
");
        printf("4 to query employees by rank
");
        printf("5 to print all employee information
");
        printf("0 (or any other number) to stop
");
        scanf("%d", &menu_choice);

        if(menu_choice == 1)
        {
            printf("choice 1
");
            menu_choice = -1;
        }

        if (menu_choice == 2)
        {
            printf("Choice 2
");
            menu_choice = -1;
        }

        if (menu_choice == 3)
        {
            printf("Choice 3
");
            menu_choice = -1;
        }
        if (menu_choice == 4)
        {
            printf("Please provide rank that you would like to query.
");
            scanf("%d", &submenu);
            query(head, submenu);
            menu_choice = -1;
        }
        if (menu_choice == 5)
        {
            printlist(head);
            menu_choice = -1;
        }
    }

    fclose(ifp);
    return 0;
}

I am having trouble with my query function, the function works except when I query rank 1 it will print JackSprats name then the program crashes. No errors from the compiler do not know what else is wrong.

EDIT* SOLUTION*

placed the break within if statement to make sure the loop was not at the last node. once loop hits last node I had loop break.

void query(employee *head, int submenu)
{
    printf("
Emp name
");
    employee* current;
    current = head;
    while (current != NULL)
    {

            if(current->Rank == submenu)
            {
                printf("%s
", current->Name);
                if(current->next == NULL)
                {
                break;
                }
                current = current->next;
            }

    current = current->next;
    }
    return;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JackSprat is the last Node value in the linked list

in the below loop, current value is set to last node as JackSprat as rank 1 and it matched with last node. Now assigning current to next will make it NULL, right after this accessing current is crashing. you need to break out of the loop once match is found.

    if(current->Rank == submenu)
    {
        printf("%s
", current->Name);
        current = current->next;
        //replace above line with break;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...