搜索C中的链接列表

时间:2013-07-20 23:52:55

标签: c search linked-list strcmp strcpy

我一直在处理一个函数searchn(),它接收一个链表list和一个字符串lname

它编译得非常好,但是当我尝试运行该函数时,我遇到了分段错误(核心转储)。我通过Valgrind运行它,它告诉我,我正在使用strcpystrcmp错误。我的player结构也已包含在内以供参考。谁能看到我做错了什么?对不起,我不是最精通编码的人。

任何帮助都会很棒。谢谢。

struct player {
    char* fname;
    char* lname;
    char pos;
    int val;
    int rank;
    struct player* next;
};

void searchn(struct player* list, char* lname){

    while (list!=NULL && strcmp(list->lname, lname) != 0){
        list = list->next;
    }

    if (list != NULL && strcmp(list->lname, lname)==0) {
        printf("%s found! \n", lname);
        printf("%s \n", list->lname);
        printf("%s \n", list->fname);
        printf("%c \n", list->pos);
        printf("%d \n", list->val);
        printf("\n");
    }
}

以下是填充链表的方法。

void addp (struct player* newnode, struct player* list){
    struct player* templist1;
    // if the list is non empty.
    if (list !=NULL){
        if(newnode->pos == GOALKEEPER){  //insert if G.
            while (list->next != NULL && (list->next)->rank < 1){
                list = list->next;
            }
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == DEFENDER){// after G bef M.
            // iterate through templist.
            while (list->next != NULL && (list->next)->rank < 2) {  // go to end of G.
                // when the list isn't empty next node rank is less than one, keep going
                list = list -> next;
            }
            // when finally rank == or > 1, then add newnode.
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == MIDFIELDER){ //after G and M but before S
            while (list->next != NULL && (list->next)->rank < 3) {
                list = list -> next;
            }
            // when stopped, then add newnode.
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == STRIKER){ // at the end.
            while (list->next != NULL && (list->next)->rank < 4){
                list = list -> next;
            }
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        printf("player added");
    }
}

2 个答案:

答案 0 :(得分:2)

你的while循环不断比较相同的两个字符串,因为它没有将list元素中的新字符串加载到temp中。无论如何,你真的不需要临时变量。

在您当前的逻辑中,您可以在strcpy循环内移动while

while (list!=NULL && strcmp(temp, lname) != 0){
    strcpy(temp, list->lname);
    list = list->next;
}

但你可以彻底摆脱这种温度。改变这个:

strcpy(temp, list->lname);

while (list != NULL && strcmp(temp, lname) != 0) {
    list = list->next;
}

if (strcmp(temp, lname) == 0) {

对此:

while (list != NULL && strcmp(list->lname, lname) != 0) {
    list = list->next;
}

if (list != NULL) {

答案 1 :(得分:1)

回答时,你不会更新temp。 你也不需要它。此外,如果字符串超过1024个字节,您可能会遇到漏洞。 试试这个:

while (list != NULL && strcmp(list->lname, lname) != 0) {
    list = list->next;
}

if (list != NULL) {

最后条件下的strcmp是不必要的。