简体   繁体   中英

Get linked-list data to String array in C

I have this function where list is a pointer to a linked list. This is the list struct which contains nodes with data.

typedef struct node{
    char *data;
    struct node *next;
}NODE;
typedef struct list{
    NODE* head;
    char *id;
    struct list *next;
    int size;
}LIST;

My goal is to take every data from the list and return it as a **char . It looks like it works but the line where i do words = (char**) calloc(numOfWords*wordLength,sizeof(char)); I'm not sure if it's correct and how it works exactly. Can you see anything that is going wrong in my code and help me? Thanks.

char **reset_words(LIST *list){
    char ** words;
    int i = 0,j = 0,wordLength,numOfWords = list->size;
    NODE *p;
    for (p = list->head; p != NULL; p = p->next) {
        wordLength = my_strlen(p->data);
        words = (char**) calloc(numOfWords*wordLength,sizeof(char));
        for(int k=0;k < wordLength;k++){
            words[i][j] = p->data[k];
            j++;
        }
        i++;
    }
    return words;
}

After i looked at your code here are some changes:

  1. If your list is a correctly done linked-list and all nodes have the same size of data then you could simply do wordLength = strlen(list->head->data) .
  2. Initialize words outside the loops. The way i did it should work.
  3. Since you have string you can simply do strcpy(words[i],p->data) to add the string to your words array.

Try this out let me know if it works.

char **reset_words(LIST *list){
    char ** words;
    int i = 0,j = 0,wordLength = strlen(list->head->data),numOfWords = list->size;
    NODE *p;
    words = (char **)malloc(numOfWords * sizeof(char *));
    for (int h=0; h<numOfWords; h++)
        words[h] = (char *)calloc(wordLength,sizeof(char));
    for (p = list->head; p != NULL; p = p->next) {
        strcpy(words[i],p->data);
        i++;
    }
    return words;
}
        wordLength = my_strlen(p->data);

I do not know, what my_strlen() does. But you have to keep in mind to reserve additional space for the trailing \\0 .

        words = (char**) calloc(numOfWords*wordLength,sizeof(char));

This does not make much sense... numOfWords seem to be the number of nodes while wordLength is the length of the actual data. So numOfWords * wordLength is something which might be used to calculate to total amount of space for all characters (but only, when wordLength is the length of the largest string).

Depending on you requirements (do you really need to copy the strings or does a reference suffice?), you can express this as:

char **words = calloc(list->size, sizeof words[0]);
size_t i = 0;
for (struct node *p = list->head; p; p = p->next) {
    if (1)
         words[i++] = p->data;
    else
         words[i++] = strdup(p->data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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