繁体   English   中英

为什么没有新元素被添加到我的链表中?

[英]Why aren't new elements being added to my linked list?

这只是代码的一个片段,但我检查并知道所有字符串都很好地保存到“新”元素中(在 function SortedInsert 中),但是“新”没有链接到头部? 我已经尝试了我能想到的一切,希望我只是错过了一些明显的东西。

typedef struct _Info* Position;
typedef struct _Info{
    char name[MAX];
    char surname[MAX];
    Position next;
} Info;

(declaration inside main function:
    Info headInfo = {.name = {0}, .surname {0}, .next = NULL};
    Position head = &headInfo;
)

int SortedInsert(Position head, char name[], char surname[]){

    Position prev = NULL, temp = NULL, new = NULL;

    prev = head;
    temp = head->next;

    new = (Position)malloc(sizeof(Info));

    if(!new){
        return EXIT_FAILURE;
    }

    strcpy(new->name, name);
    strcpy(new->surname, surname);
    new->next = NULL;

    if(head->next==NULL){
        temp = new;
    }
    else{
        // first sort, by surname
        while(strcmp(temp->surname, new->surname) < 0){
            prev = temp;
            temp = temp->next;
        }
        // second sort, by name
        while(strcmp(temp->name, new->name) < 0){
            prev = temp;
            temp = temp->next;
        }        
        new->next = prev->next;
        prev->next = new;
    }

    return EXIT_SUCCESS;
}

int PrintList(Position head){

    Position temp = NULL;

    temp = head->next;

    while(temp){
        printf("%s ", temp->name);
        printf("%s\n", temp->surname);
        printf("---\n");
        temp = temp->next;
    }

    return EXIT_SUCCESS;
}

一些问题:

  • temp = new不会在列表中插入任何内容。 它只是将对新节点的引用复制到局部变量中。 分配应该是head->next 此外,无需为此创建单独的案例。 可以使用您在else部分中的代码来处理它。

  • 插入点的检索不正确。 如果在第一个循环中strcmp调用返回 1(不是 0),那么第二个while循环根本不应该迭代:在这种情况下,名字是什么样的并不重要。 temp的姓已经大了,所以插入点已经找到了。 同样,如果strcmp调用返回 0,则第二个循环应继续验证姓氏在其第二次迭代中是否仍然相同,...等等。 而且,这个逻辑可以组合在一个循环中。

正确执行不是问题,但仍然:

  • 许多人认为在代码中定期取消引用指向结构的指针是不好的做法。 请参阅typedef 指针是否是个好主意的答案? 对于一些背景。 所以我会继续使用Info *

  • 创建一个单独的 function 用于创建和初始化节点。

  • 说“第一类”、“第二类”的评论具有误导性。 注释后面的循环中没有排序 列表已经排序。 接下来的过程只是打算根据排序顺序找到插入点。 所以评论可以改进。

  • 许多人认为最好不要转换malloc返回的值

这是对SortedInsert function 的更正,以及用于节点创建的分离的 function:

Info *createNode(char name[], char surname[]) {
    Info *new = malloc(sizeof(*new));

    if (new != NULL) {
        strcpy(new->name, name);
        strcpy(new->surname, surname);
        new->next = NULL;
    }
    return new;
}

int SortedInsert(Info *head, char name[], char surname[]){
    Info *new = createNode(name, surname);
    if (new == NULL) {
        return EXIT_FAILURE;
    }

    Info *prev = head;
    Info *temp = head->next;

    // Find insertion spot according to sort order
    while (temp != NULL) {
        int cmp = strcmp(temp->surname, new->surname);
        if (cmp == 0) { // It's a tie. Then use name as discriminator
            cmp = strcmp(temp->name, new->name);  
        }
        if (cmp >= 0) { // Found insertion spot
            break;
        }
        prev = temp;
        temp = temp->next;
    }
    new->next = prev->next;
    prev->next = new;
    return EXIT_SUCCESS;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM