簡體   English   中英

在 C 中實現鏈表數組

[英]Implement linked-list array in C

我正在嘗試使用以下數據集實現動態鏈表。

    typedef struct node {
    uint8_t item1;
    uint8_t item2;
    char* key;
    
    struct node *next;
} node;

node *nodes = NULL;

static node* find_by_item1(uint8_t item1) {
    
    node *sl = nodes;
    while(sl && sl->item1 != item1)
        sl = sl->next;
      
    return sl;
}

void createNode(char* key, uint8_t item1, uint8_t item2){
    node *sl = find_by_item1(item1);
        if(sl){
        //Do Process further if the key is already entered again 
             return;
        }
    
    node *sl = malloc(sizeof(node));
        if(!(sl = malloc(strlen(key)+1))){
        free (sl);
        return;
       }

       //memset(sl, 0, sizeof(*sl));

    //Add the data
    sl->item1 = item1;
        sl->item2 = item2;
    strcpy (sl->key, key);

    sl->next = nodes;
        nodes = sl;

}

void printNode(){

    Node *sl;
    for(sl = nodes; NULL != sl; sl = sl->next){
        printf("\nNode %s %d %d", sl->Key, sl->item1, sl->item2);
    }
}

void main(){

   for (uint8_t i = 1; i <= 4; i++) {
        char key_name[12] = {0};
        
        sprintf(key_name, “Key-%d”, i);


        switch (i) {
            case 1:
                createNode(key_name, 1, 2);
                break;
            case 2:
                createNode(key_name, 3, 4);
                break;
            case 3:
                createNode(key_name, 5, 6);
                break;
            case 4:
                createNode(key_name, 7, 8);
                break;
            default:
                break;
        }
    }

    printNode();

   }

}

我試圖根據我的研究來實現這一點,但不知何故無法達到我想要的結果。 在過去的 3-4 天里一直在思考、實施和重新實施這一點,但我似乎遺漏了一些明顯的東西。

我的程序在“find_by_item1”的第一行失敗while(sl && sl->item1 != item1)

任何指針都會有所幫助。

- -更新

我一直在試圖理解這個荒謬的錯誤,似乎該錯誤與 NULL 指針參考有關。

注釋行memset(sl, 0, sizeof(*sl)); 允許再次開始取得進展。 但是,現在,當我嘗試打印下面的鏈接列表時,輸入和 output:

Input: 
Key-1 1 2
Key-2 3 4
Key-3 5 6
Key-4 7 8

Output:
Node Key-4 1 2
Node Key-4 3 4
Node Key-4 5 6
Node Key-4 7 8

現在我不確定如何修復此代碼以針對每個項目保留正確的密鑰。

請幫忙。

謝謝,昆賈爾

至少您需要添加一個指向相同結構類型的指針,以將所有節點鏈接到一個適當的列表中。 就像是

typedef struct node {
    uint8_t item1;
    uint8_t item2;
    char *key;
    struct node *next;
} node;

請注意,我已重命名使用typedef給出的最終名稱,因為以_t結尾的類型名稱實際上是為將來的語言添加保留的(請閱讀此處)。 正如比爾在此答案下方的評論中所說,這同樣適用於以_開頭的名稱。

這是答案。

        typedef struct node {
        uint8_t item1;
        uint8_t item2;
        char* key;
        
        struct node *next;
    } node;
    
    node *nodes = NULL;
    
    static node* find_by_item1(uint8_t item1) {
        
        node *sl = nodes;
        while(sl && sl->item1 != item1)
            sl = sl->next;
          
        return sl;
    }
    
    void createNode(char* key, uint8_t item1, uint8_t item2){
        node *sl = find_by_item1(item1);
            if(sl){
            //Do Process further if the key is already entered again 
                 return;
            }
        
        node *sl = malloc(sizeof(node));
            if(!(sl = malloc(strlen(key)+1))){
            free (sl);
            return;
           }
/*
Added based on some reading where some experts suggested to have this pointer's memory area which will allow it to retain the value
*/
        sl->key = malloc(sizeof(*key));  

    
        //Add the data
        sl->item1 = item1;
        sl->item2 = item2;
        strcpy (sl->key, key);
    
        sl->next = nodes;
            nodes = sl;
    
    }
    
    void printNode(){
    
        Node *sl;
        for(sl = nodes; NULL != sl; sl = sl->next){
            printf("\nNode %s %d %d", sl->Key, sl->item1, sl->item2);
        }
    }
    
    void main(){
    
       for (uint8_t i = 1; i <= 4; i++) {
            char key_name[12] = {0};
            
            sprintf(key_name, “Key-%d”, i);
    
    
            switch (i) {
                case 1:
                    createNode(key_name, 1, 2);
                    break;
                case 2:
                    createNode(key_name, 3, 4);
                    break;
                case 3:
                    createNode(key_name, 5, 6);
                    break;
                case 4:
                    createNode(key_name, 7, 8);
                    break;
                default:
                    break;
            }
        }
    
        printNode();
    
       }
    
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM