簡體   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