链接列表的文本文件循环 [英] Linked list text file loop

查看:77
本文介绍了链接列表的文本文件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一定的情况下/箱我的程序崩溃,它符合并运行良好,否则。
我们得到了格式化,像这样的文本文件:

  12 JackSprat 2 1 6500013 HumptyDumpty 5 3 3000017 BoPeep 2 3 3000020 BoyBlue 3 2 580000

我们需要从文件中读取并使用链表存储到一个结构。到目前为止,我的code是这样的:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#定义NAME_LENGTH 20typedef结构EmployeeData工作
{
    INT EMP_ID;
    字符*名称;
    INT部;
    INT排名;
    双倍工资;    结构EmployeeData工作*接下来的;
}雇员;员工* InitializeList(INT EMP_ID,字符*名称,INT部,INT排名,双倍工资)
{
    员工* PTR =(雇员*)(的malloc(sizeof的(结构EmployeeData工作)));
    ptr->名称=(字符*)malloc的(的sizeof(char)的* NAME_LENGTH);    的strcpy(ptr->姓名,名称);
    ptr-> EMP_ID = EMP_ID;
    ptr->部=部;
    ptr->排名=排名;
    ptr->工资=工资;
    ptr->接着= NULL;    返回PTR;
}员工* insert_by_employee(员工*头,员工* PTR)
{
    雇员*电流= NULL;
    电流=头;
    如果(当前== NULL || STRCMP(电流 - >姓名,ptr->名称)0)
    {
            ptr->接下来=电流;
            返回PTR;
    }
    其他
    {
            而(电流 - >接下来= NULL&放大器;!&安培; STRCMP(电流 - >下一步 - >姓名,ptr->名称)℃下)
            {                    电流=电流 - >接着,
            }
    }
            ptr->接下来=电流 - >接下来,
            电流 - >接下来= PTR;
            返回头;}无效查询(员工*头,INT子菜单)
{
    的printf(\\ NEMP名\\ n);
    员工*电流;
    电流=头;
    而(电流!= NULL)
    {
        如果(电流 - >排名==子菜单)
        {
            的printf(%S \\ n,电流 - >姓名);
            电流=电流 - >接着,
        }
        电流=电流 - >接着,
    }
    返回;
}无效的printList(员工*头)
{
    员工*电流;
    电流=头;
    的printf(EMP_ID \\ t EMP NAME \\ t \\ t DEPT \\ t \\ t RANK \\ t \\ t工资);
    而(电流!= NULL)
    {
        的printf(\\ n%d个\\ t%S \\ t \\ t%d个\\ t \\ t%d个\\ t \\ t%d个\\ N型,电流> EMP_ID,电流 - >姓名,电流 - >部,电流 - >排名,电流 - >工资);
        电流=电流 - >接着,
    }
    返回;
}INT主要(无效)
{
    FILE * IFP =的fopen(empInfo.txt,R);
    INT EMP_ID,部门,职级,menu_choice = -1,子菜单;
    双倍工资;
    的char *名称=(字符*)malloc的(sizeof的(字符*)* NAME_LENGTH);    员工*头= NULL;    而(!的feof(IFP))
    {
        的fscanf(IFP,%d个%S%D%D,&安培; EMP_ID,名称,和放大器;部,和放大器;排名,&安培;工资);
            {
                如果(EMP_ID == 0)
                    打破;
            }
            员工*保持= InitializeList(EMP_ID,姓名,部门,职级,薪);
            头= insert_by_employee(头,持有);
    }    而(menu​​_choice!= 0)
    {
        的printf(\\ n请选择从下面的菜单\\ n行动);
        的printf(1添加新员工\\ n);
        的printf(2删除雇员\\ n);
        的printf(3修改员工记录\\ n);
        的printf(4秩\\ n查询员工);
        的printf(5打印所有员工的信息\\ n);
        输出(0(或任何其它数目)停止\\ n);
        scanf函数(%d个,&安培; menu_choice);        如果(menu​​_choice == 1)
        {
            输出(选择1 \\ n);
            menu_choice = -1;
        }        如果(menu​​_choice == 2)
        {
            输出(选择2 \\ n);
            menu_choice = -1;
        }        如果(menu​​_choice == 3)
        {
            输出(选择3 \\ n);
            menu_choice = -1;
        }
        如果(menu​​_choice == 4)
        {
            的printf(请提供排名,你想查询\\ n);
            scanf函数(%d个,&安培;子菜单);
            查询(头,子菜​​单);
            menu_choice = -1;
        }
        如果(menu​​_choice == 5)
        {
            的printList(头);
            menu_choice = -1;
        }
    }    FCLOSE(IFP);
    返回0;
}

我有我的查询功能麻烦,功能工作除了当我查询1级将打印JackSprats名称,然后程序崩溃。从编译器没有错误不知道还有什么是错的。

编辑*解决方案*

放置在if语句使断裂确保循环是不是在最后一个节点。一旦回路达到最后一个节点,我不得不回路断线。

 无效查询(员工*头,INT子菜单)
{
    的printf(\\ NEMP名\\ n);
    员工*电流;
    电流=头;
    而(电流!= NULL)
    {            如果(电流 - >排名==子菜单)
            {
                的printf(%S \\ n,电流 - >姓名);
                如果(电流 - >接下来== NULL)
                {
                打破;
                }
                电流=电流 - >接着,
            }    电流=电流 - >接着,
    }
    返回;
}


解决方案

JackSprat是链表中的最后一个节点值

在下面的循环中,电流值设置为最后一个节点作为JackSprat作为秩1,并将其与最后一个节点匹配。
现在目前分配到明年将使它NULL,则此访问当前的崩溃之后。
你需要一旦找到匹配爆发循环。

 如果(电流 - >排名==子菜单)
    {
        的printf(%S \\ n,电流 - >姓名);
        电流=电流 - >接着,
        //替换以上断线;
    }

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

解决方案

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

这篇关于链接列表的文本文件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆