C 链表文本文件循环
我的程序在某个实例/情况下崩溃,它符合要求,否则运行正常。 我们收到一个如下格式的文本文件:C 链表文本文件循环,c,function,linked-list,C,Function,Linked List,我的程序在某个实例/情况下崩溃,它符合要求,否则运行正常。 我们收到一个如下格式的文本文件: 12 JackSprat 2 1 65000 13 HumptyDumpty 5 3 30000 17 BoPeep 2 3 30000 20 BoyBlue 3 2 58000 0 我们需要从文件中读取数据并使用链表存储到结构中。到目前为止,我的代码如下所示: #include <stdio.h
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;
}
执行当前->下一步->下一步时存在风险,且为空。更好:
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
break;
}
current = current->next;
}
也可以执行scanf(“%19s”)。JackSprat是链接列表中的最后一个节点值 在下面的循环中,当前值设置为最后一个节点,作为秩1的JackSprat,并与最后一个节点匹配。 现在将当前分配给下一个将使其为空,就在访问当前崩溃之后。 一旦找到匹配项,您需要跳出循环
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
current = current->next;
//replace above line with break;
}
我以前有过中断,但当我查询一个排名时遇到了一个问题,其中超过1人在if循环中有中断,它将只打印第一个具有该排名的人。i、 e等级3仅打印bopeep。我在if语句中包含了current=current->next以打印所有具有所述排名的员工。我现在看到了错误发生的位置,谢谢。有什么解决方案的建议吗?上述解决方案不起作用,因为我需要打印所有具有上述职级的员工(只需省去上述中断即可)。)最后将中断放在带有上述if语句的嵌套if语句中。谢谢你的帮助。
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
break;
}
current = current->next;
}
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
current = current->next;
//replace above line with break;
}