链接列表插入崩溃程序中的strcmp

时间:2018-02-09 05:13:20

标签: c linked-list eclipse-cdt strcmp

作为作业的一部分,我应该在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作为节点的名称并不聪明,我会改变它) 和调用insert的函数:

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

1 个答案:

答案 0 :(得分:1)

如果你有一个名为 apple 的元素作为你的第一个元素并尝试添加名为的元素会发生什么?

你将被抛出下面而直接循环,你的上一个将被取消分配:

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-&gt; next = n_node;)。您将在此处崩溃。