Linked list text file loop

て烟熏妆下的殇ゞ 提交于 2019-12-25 18:43:36

问题


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("\nEmp name\n");
    employee* current;
    current = head;
    while (current != NULL)
    {
        if(current->Rank == submenu)
        {
            printf("%s\n", current->Name);
            current = current->next;
        }
        current = current->next;
    }
    return;
}

void printlist(employee* head)
{
    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* 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("\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 (or any other number) to stop\n");
        scanf("%d", &menu_choice);

        if(menu_choice == 1)
        {
            printf("choice 1\n");
            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 rank that 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(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("\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;
}

回答1:


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\n", current->Name);
        current = current->next;
        //replace above line with break;
    }



回答2:


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

risks doing current->next->next, with a null. Better:

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

Maybe do scanf("%19s") too.



来源:https://stackoverflow.com/questions/25941820/linked-list-text-file-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!