Doubt in c (chained and vector lists)

Asked

Viewed 758 times

0

How can I create an array where each position is a pointer to a chained list ?

For example:

typedef struct info Info
struct info {
 int num;
 Info* prox;
};

And then I want to create a vector, and do something like, but with dynamic size allocation:

For example:

v[0] = colocarnum(lista, 10);
  • A linked list is different from a vector which objective of the final vector?

  • The idea is that each index in the list has a pointer that leads to a linked list containing information.

  • 1

    Info* vetor[10]; or vetor = (Info *) malloc(10 * sizeof(Info *));

2 answers

0

With a linked list values are traversed through the fields prox.

typedef struct info {
    int num;
    struct info *prox;
} Info;

int main(void) {
    Info *info1, *info2;
    info1 = (Info *) malloc(sizeof(Info));
    info2 = (Info *) malloc(sizeof(Info));
    
    info1->num = 1;
    info2->num = 2;
    info1->prox = info2;
    
    printf("Valor 1 -> %d\n", info1->num);
    printf("Valor 2 -> %d\n", info1->prox->num);
    
    free(info2);
    free(info1);


    return 0;
}

If you have a pointer vector for Info you will have a list of lists.

0


According to the IME-SP, a chained list means:

Chain lists. A chained list is a representation of a sequence of objects, all of the same type, in the RAM (= Random access memory) of the computer. Each element of the sequence is stored in a list cell: the first element in the first cell, the second in the second, and so on. (https://www.ime.usp.br/~pf/algorithms/classes/list.html)

in C, we can create a simply chained list by defining a LIST structure that will contain the list data and a struct pointer to the next element of the list.

(Questions about pointers, visit the link:

What are pointers?

In your case, as it is a simply chained list, the list will only point forward, that is to the next element. But we could make a list that had pointers that pointed to both the next and the previous. Let’s take a look at the implementation code below to better understand:

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
/* Definicao da estrutura lista que tera o apelido do "no" */
typedef struct lista {
    int vetorDeNumeros[10];
    struct lista *prox;
} no;

void inserir(int x, no *p) {
    /* Inserir um elemento a lista */
}

void excluir(no *p) {
    /* Excluir um elemento a lista */
}

void busca (int x, no *le) {
    /* Buscar um elemento a lista */
}

void imprimir(no *le) {
    /* imprimir um elemento a lista */
}

int main (void) {
    /* Criando um vetor dinamico  da lista */
    int tamLista;
    int tamVetores;
    no* vetorDinamico;
    printf("Quantas listas seu vetor irá possuir? R: ");
    scanf("%d",&tamLista);
    getchar();
    /* Alocando dinamicamente o tamanho do vetor lista*/
        vetorDinamico = (no *) malloc(tamLista * sizeof(no));
    /* Alocando dinamicamente o tamanho dos vetores de numeros dentro de lista do vetor dinamico*/
            for (int i = 0; i<tamLista; i++) {
                printf("Quantas posicoes terá o vetor da sua lista numero %d? R: ",i);
                scanf("%d",&tamVetores);
                vetorDinamico[i].vetorDeNumeros[tamVetores] = (long) malloc(tamVetores * sizeof(long));
            }
/* adicionando numeros dentro de uma lista dentro de um vetorDinamico */
vetorDinamico[0].vetorDeNumeros[0] = 1;
/* Teste de impressao de um vetor de inteiro dentro de um vetor dinamico de lista chamado no */
printf("O Numero armazenado na posicao 0 do vetor 0 da estrutura é: %d",vetorDinamico[0].vetorDeNumeros[0]);


    return 0;
}

The Code does nothing more than:

  1. create a LIST structure with the NO name with a number array inside it;
  2. create a dynamical array of the list structure and dynamically allocate the memory of the vector;
  3. create a number array within each list assigned to the dynamical list vector and dynamically allocate the memory of these vectors
  4. print a test number.

I hope you’ve cleared your doubt.

Browser other questions tagged

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