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:
- create a LIST structure with the NO name with a number array inside it;
- create a dynamical array of the list structure and dynamically allocate the memory of the vector;
- create a number array within each list assigned to the dynamical list vector and dynamically allocate the memory of these vectors
- print a test number.
I hope you’ve cleared your doubt.
A linked list is different from a vector which objective of the final vector?
– chriptus13
The idea is that each index in the list has a pointer that leads to a linked list containing information.
– GratefullyDead
Info* vetor[10];
orvetor = (Info *) malloc(10 * sizeof(Info *));
– anonimo