【发布时间】:2018-02-09 05:13:20
【问题描述】:
作为作业的一部分,我应该在 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;
}
}
问题是我的程序在没有任何错误或警告的情况下崩溃和烧毁(我正在使用带有 CDT 的 eclipse)。
该程序运行良好时
while (current != NULL && strcmp(name, current->name) > 0)
被修改为
while (current != NULL /*&& strcmp(name, current->name) > 0*/).
对我来说,name 或 current->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;
}
【问题讨论】:
-
显示你的函数 new_node 和要插入的调用者。可疑的问题可能在那些。
-
请注意,如果您的前两件商品乱序,您的清单将无法正确排序。
-
您可以在
gdb下运行,当它崩溃时检查变量,这至少可以让您了解它崩溃的原因。当它到达strcmp时,current不能为 NULL,因此name是无效指针或存在越界问题。可能不是问题,但您应该始终检查malloc是否返回有效指针。 -
注意:使用指针指向指针可以将插入函数减少到大约十行代码,只包含一个循环和一个条件。
标签: c linked-list eclipse-cdt strcmp