gpt4 book ai didi

c - 链表插入中的 strcmp 导致程序崩溃

转载 作者:行者123 更新时间:2023-11-30 14:50:53 25 4
gpt4 key购买 nike

作为作业的一部分,我应该在 c 中实现一个单链表。我之前已经用几种不同的语言做过很多次了,但是在经历了几个小时的痛苦之后,我在使用 strcmp 时遇到了一个问题。这是我正在使用的结构:

typedef struct node {
char *name;
float score;
struct node *next;
} node;

这个问题是特定于插入函数的,它应该类似于插入排序,因为我需要将列表中的节点按字母顺序排序。(我的教授指定插入函数进行排序,尽管没有称其为插入排序)。

void insert(node **start, char *name, float score) { //  to insert a record into the linked list sorted by name in dictionary order.

//create new node
node *n_node = new_node(name, score);

node *current;
current = *start;

if (current != NULL) { //-----------if list is not empty
node *prev = NULL;

if (current->next != NULL) { //--if list has more than 1 element
while (current != NULL && strcmp(name, current->name) > 0) { //cycle through list to sorted insertion point
// ^^^^^^^Problem Here^^^^^^^^
//while name is greater than current name, means lower on alphabet (z>a)
prev = current;
current = current->next;
}
if (current != NULL) { //-----not at end of list
//once current is not < new node, connect between prev and current
prev->next = n_node;
n_node->next = current;
} else { // ------------------at end of list
prev->next = n_node;
}

} else { //-----------------------list has only one element
current->next = n_node;
}
} else { //--------------------------List is empty - assign new node as first element
*start = n_node;
}

}

问题是我的程序崩溃并烧毁,没有任何错误或警告(我正在使用 Eclipse 和 CDT)。该程序运行良好,当while (current != NULL && strcmp(name, current->name) > 0)修改为while (current != NULL/*&& strcmp(name, current->name) > 0*/).

对我来说,显然 namecurrent->name 导致 strcmp 的操作出现问题,但我可以'似乎无法解决这个问题。

编辑:我要补充的是,该函数是从另一个函数调用的,该函数从包含名称和标记对的文件中检索并标记字符串,但我的测试并未表明它通过调用传递了错误的字符串或字符。

有关一些额外的详细信息,这是我的 new_node 函数:

node *new_node(char *name, float score) {
node *new = (struct node*) malloc(sizeof(struct node));
new->name = malloc(strlen(name) + 1);
strcpy(new->name, name);
new->score = score;
new->next = NULL;
return new;
}

(我意识到使用 new 作为节点名称并不明智,我会更改它)以及调用插入的函数:

int data_import(node **startp, char *infilename) { // to import data from the file and insert .
int max_line = 100;
char line[max_line];
char delimiters[] = ",";

char name[500] = "";
char *namep;
namep = &name[0];

float score = 0.0f;
int i = 0;

FILE *fi;
char *token;
// open file to read
fi = fopen(infilename, "r");
if (fi == NULL) { // Cannot open the file.
perror("error");
return 0;
}

// read each line, increase counter, retrieve data
while (fgets(line, max_line, fi) != NULL) {
//fputs(line, stdout); //console output confirmation

token = strtok(line, delimiters);
strcpy(namep, token);
token = strtok(NULL, delimiters); //increment token to mark variable
score = atof(token);
insert(startp, namep, score);

i++;
}
//close file
fclose(fi);
return i;
}

最佳答案

如果您将名为 apple 的元素作为第一个元素,并且尝试添加名为 about 的元素,会发生什么情况?

您将立即被抛出下面的 while 循环,并且您的上一个将被取消分配:

while (current != NULL && strcmp(name, current->name) > 0) { //cycle through list to sorted insertion point
// ^^^^^^^Problem Here^^^^^^^^
//while name is greater than current name, means lower on alphabet (z>a)
prev = current;
current = current->next;
}

这个特定部分对我来说看起来很可疑:

之后您将进入以下例程:

 if (current != NULL) { //-----not at end of list
//once current is not < new node, connect between prev and current
prev->next = n_node;
n_node->next = current;
}

因为您的 *prev 未分配,并且您尝试访问它(prev->next = n_node;)。您将在这里崩溃。

关于c - 链表插入中的 strcmp 导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com