How to convert a sequential list to a linked in C language

Asked

Viewed 134 times

-3

    #define MAX 100
    typedef struct dado{
    int num[MAX];
    int qtd;
    }Dados;
    typedef struct {
    Dados dados;
    }Lista;
    typedef struct no{
    int numero;
    struct no *prox;

    }No

    typedef struct {
    No *cabeca;
    }LISTA
void insere(No *novo){

novo->prox=novo;


}

//Now I need you to help me convert a sequential list into a linked one

  • In a sequential list the data is organized by physical contiguity in a linked list you have to use pointers to indicate where the next data is.

  • I know that, but I can’t make the conversion please help me

1 answer

0

From your question I understood that the goal is the conversion of a structure containing sequential data (a vector and its number of elements) into a linked list structure (containing head).

I don’t understand the reason for nesting two structures in the sequential list, but I still did the program according to the types you defined (I just changed the name).

The code follows below. I have separated the print functions and inserted comments to help understanding. The vector was initialized with some sample data.

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

#define MAX 100


// tipo dos dados da lista sequencial
typedef struct dados {
    int num[MAX];
    int qtd;
} dados;

// tipo da lista sequencial
typedef struct {
    dados dados;
} lista_sequencial;

// tipo dos nós da lista ligada
typedef struct no {
    int numero;
    struct no *prox;
} no;

// tipo da cabeça da lista ligada
typedef struct lista {
    no *cabeca;
} lista_ligada;


// função para impressão da lista sequencial 
void imprime_lista_sequencial(lista_sequencial *ls) {
    for(int i = 0; i < ls->dados.qtd; i++)
        printf("%d ", ls->dados.num[i]);
}

// função para impressão da lista ligada
void imprime_lista_ligada(lista_ligada *ll) {
    no *p;
    p = ll->cabeca;
    while(p != NULL) {
        printf("%d ", p->numero);
        p = p->prox;
    }
}


int main(int argc, char *argv[]) {
    // Inicializa lista sequencial */
    lista_sequencial ls;
    for (int i=0; i <= 20; i++) {
        ls.dados.num[i] = i * 2;
    }
    ls.dados.qtd   = 21;

    // impressão da lista sequencial
    printf("Lista Sequencial: ");
    imprime_lista_sequencial(&ls);

    // criação da lista ligada com cabeça e cópia dos dados da lista sequencial */
    lista_ligada ll;
    ll.cabeca = NULL;
    no **pn = &ll.cabeca;

    for (int i = 0; i < ls.dados.qtd; i++) {
        *pn = (no *) malloc(sizeof(no));
        (*pn)->numero = ls.dados.num[i];
        (*pn)->prox = NULL;
        pn = &(*pn)->prox;
    }

    // impressão da lista ligada 
    printf("\nLista Ligada: ");
    imprime_lista_ligada(&ll);
}
  • Look thanks a lot for the help, but the idea of taking a Sequential List and then converting it into a list simply linked.... I tried to add your code to mine and after presenting the vector values the environment is closing for itself.

  • I’m having trouble understanding how this sequential list works. You already have it implemented?

  • A sequential list, is type of list that data is stored sequentially, where there is no head node.

  • Example of a sequential list

  • #define MAX 30 typedef int type_given; /global variable... int i,j; typedef struct{ int Qtd; int data[MAX]; }List; #include<stdio. h> #include<stdlib. h> //create list int creationList (List *li) { li->Qtd = 0; }

  • I edited the answer for the program to use the structures exactly as you described. See if you solved.

Show 1 more comment

Browser other questions tagged

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