Merge a stack with a list

Asked

Viewed 395 times

2

struct noPilha{
    float peso;
    int idMala;
    char cor[20];
    struct pessoa donoMala;
    struct noPilha* prox;
};

typedef struct noPilha Elem;

struct noLista{
    struct noPilha mala;
    struct noLista *ant;
    struct noLista *prox;
};

typedef struct noLista NoLista;

void insere_lista(Lista* l, Pilha *pi){

    NoLista* no;

        no = (NoLista*) malloc(sizeof(NoLista));

        no->mala.peso = pi->peso;
        no->prox = NULL;

        if((*l) == NULL){
            no->ant = NULL;
            *l = no;
        }else{
            NoLista* aux = *l;
            while(aux->prox != NULL){
                aux = aux->prox;
            }
            aux->prox = no;
            no->ant = aux;
        }
        nop = no->prox;

}

I already created the structure to insert the elements in a stack, now I want to pass this stack by reference and insert the elements of the stack in a double chained list, but the way I’m doing it is not inserting the elements of the stack in the list, in this code I just put to add the weight.

  • 1

    Is there any typedef of Pilha which is not in question ? For it is being used as a type in void insere_lista(Lista* l, Pilha *pi){. Same question about the guy Lista

1 answer

1

Corrected:

struct noPilha{
    float peso;
    int idMala;
    char cor[20];
    struct pessoa donoMala;

};

typedef struct noPilha Elem;

struct noLista{
    struct noPilha mala;
    struct noLista *ant;
    struct noLista *prox;
}*aux=0;


typedef struct noLista NoLista;


int cont=0;

void insere_lista(Elem mala){


    NoLista* no = (NoLista*)malloc(sizeof(NoLista));

    if(cont == 0)
    {
        no->prox=0;
        no->ant=0;
        no->mala = mala;
        aux =  no;

    }
    else{
        aux->prox = no;
        no->prox=0;
        no->ant=aux;
        no->mala = mala;

    }
    cont++;
}

void exibe(){

    NoLista * no = aux;
    for(int i =0; i<cont; i++)
    {
        if(no == 0) { continue; }
        printf("id %d", no->mala.peso);
        no = no->prox;
    }
}
int main()
{
    Elem mala1;
    mala1.peso = 1.0f;
    insere_lista(mala1);
    mala1.peso = 2.0f;
    insere_lista(mala1);

    exibe();

    return 0;
}

In your insertion function, your error was not stating any references, the variable aux always has to be outside the function( in the overall scope).

  • Where does the stack enter in your answer ? I remind you that the question title says to join Stack with List, and the function insere_lista in question author code, receives a list and a stack.

  • pay attention to the code, typedef struct noPilha Elem; logo pilha = Elem a variavel aux é um ponteiro da struct noLista, e se olhar no codigo do autor da pergunta vera isso: Nolista* aux = l; List * and Nolista logo are equal, so the variable he passes as argument called *l is equal to the variable *aux in my answer.

  • 1

    I still can’t see a union of Pilha with a Lista, this is assuming that a stack is a Pilha based on LIFO for example and with various elements. A proof of this my observation is the fact of having removed the struct noPilha* prox; of his struct noPilha(and which is also not a valid/common cell structure). Personally I think the question is simply too incomplete to be answered in a useful way to the community.

Browser other questions tagged

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