Simply Chained Linear List

Asked

Viewed 75 times

0

My question was how to do the program without dynamic allocation, I’m learning LLSE now, and everyone I think is with dynamic allocation, but my teacher said he would only use dynamic allocation in the second half of the discipline, if they can help with links or explaining, it would be of great help, follows below an example of the structure he passed to use in the program, which in general differs from the structures I see in other examples:

typedef struct {
char info;
int prox;
} no;

typedef struct {
no vet[MAX];
} LLSE;
  • 1

    Look, in my view, in this case, you would have to know exactly how many structs were going to be used during the execution of the program and define it beforehand with a vector. So there, I do not know if it is possible to do without dynamic allocation. I do not think it makes much sense to use this way without dynamic allocation. Let’s see if someone with more experience can answer.

1 answer

1


Good afternoon Weslley,

You can implement a list through a array static N elements, where N is the maximum number of items your list supports.

Simplifying its structure:

typedef struct {

char info[MAX_TAM]; //Vetor para guardar a informação
int primeiro, ultimo; //Primeiro e último elemento do vetor

} LLSE;

What I did above was basically unite the two structs and take out the pointer Prox, because since we are going to implement our list through a vector, which is already administered by a pointer we don’t need, this variable.

Now that we’ve assembled the struct just declare inside the main and create the functions to manage that list. There is no set of functions that serves for every kind of list, but there are some basics you will need.

Are they:

void inicializaLista(LLSE *lista); //Opicional, mas recomendo.
int insereElemento(char elemento, LLSE *lista);
int removeElemento(char elemento, LLSE *lista);

Applying:

void inicializaLista(LLSE *lista){
lista->primeiro = lista->ultimo = -1;
}

The initialize function will give a parameter to test whether the list is empty/has been initialized, since it is a vector starting from position 0 the test if(lista->ultimo == 0) may not be as effective, but of course it all depends on how you are implementing your list, as I said: it’s just a personal recommendation.

int insereElemento(char elemento, LLSE lista){

if( lista->ultimo >= MAX_TAM-1){
  return(0); //Lista cheia
 }else if(lista->ultimo == -1)){
  lista->primeiro=0; //Caso esteja vazia, o primeiro elemento recebe 0
 }
 lista->ultimo++;
 lista->info[lista->ultimo]= elemento;
 return(1);

Before inserting we first have to test whether the list is full or empty, then increment 1 to the value of the last element, insert the element and close the function, if "first" and "last" will be our position references in the vector, using to test whether it is already full or empty.

Try to implement the int removeElemento(); for reasons of training.

Hug and good studies!

  • Thank you very much, I’ll do the removal part here

  • Please, I’m happy to help!

Browser other questions tagged

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