String chained list (character array) in C

Asked

Viewed 5,263 times

1

I’m doing a job for college, and it’s basically about hash table. At work, I have to do a Hash table of a n strings, for example. Instead of making an array of characters (in this case, an array of strings), I would like to implement a chained list, because then at the end I could add more strings in the list and in the hash table.

However, I’m having trouble implementing a string chained list, because I don’t know how "cell", for example, can store an array (in this case, the string, character array).

The help I ask here then, is to help me turn the following code I made (which generates random words of random size) into a chained list. Basically insert the variable word[i] on the chained list.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>

int main() {

    srand(time(NULL));
    setlocale(LC_ALL, "");

    int strQty = 100;
    int strLimit = 11;

    int ascMin = 97;
    int ascMax = 122;

    char words[strQty][strLimit];

    // Gerando strings aleatorias
    int i, j;
    int size, letter;

    for (i=0; i<strQty; i++) {

        // Tamanho da palavra words[i] -- de 1 a 10.
        size = rand()%(10)+1;

        for (j=0; j<size; j++) {

            letter = rand()%(ascMax-ascMin)+ascMin;
            words[i][j] = (char)letter;
        }

        // Indica o fim da string
        words[i][size] = '\0'; 
    }

    // Lendo strings geradas
    for (i=0; i<strQty; i++) {

        printf("Posição [%03d], tamanho [%02d], Palavra [%s]: ", i, strlen(words[i]), words[i]);
        for (j=0; j<strlen(words[i]); j++) {
                printf("%c", words[i][j]);
        }

        printf("\n");
    }
}

In addition, I await the answers and thank you for your help!

1 answer

0


The code is generating the word in a variable char word and is being passed on to the function insereLista.

Also added a function to print the chained list, I believe you can adapt to your goal.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>

#define STRLIMIT 10
typedef struct lista{
    char word[STRLIMIT];
    struct lista* prox;
}Lista;

Lista* insereLista(Lista* l, char word[], int size){
    Lista* novo = (Lista*)malloc(sizeof(Lista));
    int i;
    for (i=0; i<size; i++) {
        novo->word[i] = word[i];
    }
    novo->word[size] = '\0';
    novo->prox = l;
    return novo;
}

void imprimeLista(Lista* l){
    printf("---- lista -----\n");
    do{
        printf("%s\n", l->word);
        l= l->prox;
    }while(l != NULL);
}

int main() {

    srand(time(NULL));
    setlocale(LC_ALL, "");

    int strQty = 100;
    int strLimit = 11;

    int ascMin = 97;
    int ascMax = 122;

    //char words[strQty][strLimit];


    // Gerando strings aleatorias
    int i, j;
    int size, letter;

    Lista* l = NULL; //Lista encadeada
    for (i=0; i<strQty; i++) {

        // Tamanho da palavra words[i] -- de 1 a 10.
        size = rand()%(10)+1;
        char word[strLimit];
        for (j=0; j<size; j++) {

            letter = rand()%(ascMax-ascMin)+ascMin;
            word[j] = (char)letter;
        }

        // Indica o fim da string
        word[size] = '\0';
        l = insereLista(l, word, size);
    }
    imprimeLista(l);

    // Lendo strings geradas
    /*
    for (i=0; i<strQty; i++) {

        printf("Posição [%03d], tamanho [%02d], Palavra [%s]: ", i, strlen(words[i]), words[i]);
        for (j=0; j<strlen(words[i]); j++) {
                printf("%c", words[i][j]);
        }

        printf("\n");
    }
    */
}

To insert at the end:

Lista* insereFim(Lista* l, char word[], int size){
    Lista* novo = (Lista*)malloc(sizeof(Lista));
    int i;
    for (i=0; i<size; i++) {
        novo->word[i] = word[i];
    }
    novo->word[size] = '\0';
    novo->prox = NULL;

    //Se for o primeiro elemento
    if( l==NULL)
        return novo;

    //Insere no fim
    Lista* aux = l;
    while(aux->prox!=NULL)
        aux=aux->prox;
    aux->prox = novo;
    return l;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.