我的程序在某个实例/案例下崩溃,它符合并运行正常。 我们得到了一个格式如下的文本文件:
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语句中以确保循环不在最后一个节点。一旦循环命中最后一个节点,我就有了循环中断。
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;
}
答案 0 :(得分:0)
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
current = current->next;
}
current = current->next;
}
冒险进行当前 - &gt; next-&gt; next,使用null。更好:
while (current != NULL)
{
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
break;
}
current = current->next;
}
也许也scanf("%19s")
。
答案 1 :(得分:0)
JackSprat是链表中的最后一个Node值
在下面的循环中,当前值被设置为最后一个节点,作为JackSprat作为等级1并且它与最后一个节点匹配。 现在,将当前值分配给next将使其为NULL,此时此访问当前崩溃。 一旦找到匹配,你需要突破循环。
if(current->Rank == submenu)
{
printf("%s\n", current->Name);
current = current->next;
//replace above line with break;
}