【问题标题】:Implement linked-list array in C在 C 中实现链表数组
【发布时间】:2021-04-13 15:25:35
【问题描述】:

我正在尝试使用以下数据集实现动态链表。

    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 &amp;&amp; sl-&gt;item1 != item1) 的第一行失败

任何指针都会有所帮助。

---更新

我一直在尝试理解这个荒谬的错误,似乎该错误与 NULL 指针引用有关。

评论memset(sl, 0, sizeof(*sl)); 允许再次开始取得进展。 但是,现在,当我尝试打印时,下面的链接列表是输入和输出:

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

现在我不确定如何修复此代码以针对每个项目保留正确的密钥。

请帮忙

谢谢, 昆贾尔

【问题讨论】:

  • 请说明您具体尝试了什么以及您的实施失败,以便我们可以从那里构建。
  • “在过去的 3-4 天里一直在思考和实施” - 你试过谷歌搜索“c 链表”吗?
  • @klutt 是的,标题说“链表数组”这一事实让我觉得他还没有完全理解链表的真正含义。或者,也许他正试图根据他以前对来自 JS 或 Python 等语言的列表和数组的了解来实现它们,在这些语言中它们看起来是一样的。
  • 这里是一些示例代码programiz.com/dsa/linked-list
  • sl 不为空时,if (sl) return 将始终返回。我想你可能想要if (!sl) return

标签: c linked-list singly-linked-list


【解决方案1】:

您至少需要添加一个指向相同结构类型的指针,以将所有节点链接到一个适当的列表中。类似的东西

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

请注意,我已重命名使用 typedef 给出的最终名称,因为以 _t 结尾的类型名称实际上是为将来添加的语言保留的(请阅读 here)。这同样适用于以_ 开头的名称,正如比尔在此答案下方的评论中所说。

【讨论】:

【解决方案2】:

这就是答案。

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

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多