-1

I am trying to implement the dynamic linked list with the below dataset.

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

   }

}

I have tried to implement this based on my research but somehow unable to achieve the result that I was intending. Have spent about last 3-4 days constantly thinking and implementing and re-implementing this but I seems to be missing something obvious.

My program fails at 1st line in "find_by_item1" while(sl && sl->item1 != item1)

Any pointers will be helpful.

---Update

I have been trying to understand the absurd error and seems the error was something related to the NULL Pointer Reference.

Commenting the line memset(sl, 0, sizeof(*sl)); allowed to start making the progress again. However, now, when I try to print the link list below is the input and 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

Now I am not sure how can I fix this code to retain the correct Key against each items.

Help pls.

Thanks, Kunjal

Kunjal
  • 308
  • 1
  • 4
  • 11
  • 4
    Please show what exactly you have tried and your implementation that failed to work, so we can build from there. – mediocrevegetable1 Apr 13 '21 at 15:26
  • *" Have spent about last 3-4 days constantly thinking and implementing"* - Did you try googling "c linked list"? – klutt Apr 13 '21 at 15:37
  • @klutt yeah the fact that the title says "linked list array" makes me think he hasn't fully understood what linked lists really are. Or maybe he's trying to implement them based on his previous knowledge of lists and arrays from a language like JS or Python where they appear to be the same thing. – rdxdkr Apr 13 '21 at 15:40
  • 1
    Here is some example code https://www.programiz.com/dsa/linked-list – klutt Apr 13 '21 at 15:45
  • @klutt, yes I have tried searching and honestly I am still learning C based on experience with other programming language as outlined by other user. – Kunjal Apr 13 '21 at 15:57
  • 1
    `if (sl) return` will always return when `sl` is NOT null. I think you might want `if (!sl) return` – mediocrevegetable1 Apr 13 '21 at 15:57
  • Should `if(!(sl = malloc(strlen(acc_Name)+1))){` be `if(!(sl->key = malloc(strlen(key)+1))){`? – Ian Abbott Apr 13 '21 at 16:05

2 Answers2

0

At the very minimum you need to add a pointer to the same struct type to chain together all the nodes into a proper list. Something like

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

Notice that I have renamed the final name given with the typedef because type names that end in _t are actually reserved for future language additions (read here). The same also applies to names starting with _, as said by Bill in the comment below this answer.

rdxdkr
  • 839
  • 1
  • 12
  • 22
0

Here's the answer.

        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();
    
       }
    
    }
Kunjal
  • 308
  • 1
  • 4
  • 11