【问题标题】:Linked list text file loop链表文本文件循环
【发布时间】:2014-09-19 20:19:57
【问题描述】:

我的程序在某个实例/情况下崩溃,否则它可以正常运行。 我们得到了一个格式如下的文本文件:

12   JackSprat   2     1    65000

13   HumptyDumpty  5   3    30000

17   BoPeep     2      3    30000

20   BoyBlue    3      2    58000

0

我们需要从文件中读取并使用链表存储到结构中。到目前为止,我的代码如下所示:

#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;
}

我的查询函数有问题,该函数可以正常工作,但当我查询排名 1 时,它将打印 JackSprats 名称,然后程序崩溃。编译器没有错误不知道还有什么问题。

编辑*解决方案*

在 if 语句中放置了 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;
}

【问题讨论】:

    标签: c function linked-list


    【解决方案1】:

    JackSprat 是链表中的最后一个 Node 值

    在下面的循环中,当前值设置为最后一个节点,因为 JackSprat 作为排名 1,并且它与最后一个节点匹配。 现在将 current 分配给 next 将使其为 NULL,就在此访问 current 崩溃之后。 找到匹配项后,您需要跳出循环。

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

    【讨论】:

    • 最终将中断放置在带有上述 if 语句的嵌套 if 语句中。感谢您的帮助。
    【解决方案2】:
    while (current != NULL)
    {
        if(current->Rank == submenu)
        {
            printf("%s\n", current->Name);
            current = current->next;
        }
        current = current->next;
    }
    

    执行 current->next->next 的风险为 null。更好:

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

    也许scanf("%19s") 也可以。

    【讨论】:

    • 我之前有过休息,但是当我查询一个超过 1 人具有该等级且在 if 循环中休息的等级时遇到问题,它只会打印具有该等级的第一人.即等级 3 仅打印 bopeep。我在 if 语句中包含了 current=current->next 以打印所有具有上述等级的员工。
    • 我知道错误发生在哪里,谢谢。关于解决方案的任何建议?上述解决方案不起作用,因为我需要打印所有具有上述级别的员工
    • 简单地忽略上面的中断。 ;)
    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多