Chained list of strings (vector of characters) in C

1

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

However, I'm having trouble implementing a threaded list of strings, as I do not know how the "cell", for example, can store a vector (in this case, the string, vector of characters).

The help I ask here then is to help me transform the following code I made (which generates random random-sized words) into a linked list. Basically, insert the variable word [i] in the linked 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] = '
#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] = '%pre%'; 
    }

    // 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");
    }
}
'; } // 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"); } }

More, I await the answers and thank you for the help!

    
asked by anonymous 25.04.2017 / 23:33

1 answer

0

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

I've also added a function to print the linked list, I believe you can adapt it 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] = '
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] = '
#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] = '
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] = '%pre%';
    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;
}
'; 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] = '%pre%'; 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"); } */ }
'; 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; }
'; 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] = '%pre%'; 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:

%pre%     
26.04.2017 / 00:24