Problem in the list

-1

The list should insert the fields of struct paths and then print them. But when I ask to print, only the first insertion appears.

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

typedef struct caminhoes{
    int id;
    char motorista[200];
    char marca[200];
    char la[200];
    char lisd[200];
    float kml;
    struct caminhoes *prox;
}lcam;

typedef struct cargas{
    int id;
    char produto[200];
    char origem[200];
    char destino[200];
    float vfrete;
    struct cargas *prox;
}lcar;

 typedef struct cidades{
    int id;
    char nome[200];
    char vizinhos[200];
    float distancia;
    float vc;
    struct cidades *prox;
}lcid;


void cad_cam(lcam **cabeca);
void cad_car(lcar **cabeca);
void cad_cid(lcid **cabeca);
void imp_cam(lcam *cabeca);
void imp_car(lcam *cabeca);
void imp_cid(lcam *cabeca);

int main()
{
    setlocale(LC_ALL, "Portuguese");

    lcam *cabeca = NULL;        
    lcam *noatual;     

    int op;

    printf("0 - Fechar \n");
    printf("1 - Cadastrar Caminhão \n");
    printf("2 - Cadastrar Carga \n");
    printf("3 - Cadastrar Cidade \n");
    printf("4 - Imprimir \n" );
    printf("5 - Excluir \n");
    printf("6 - Salvar Arquivo \n");
    printf("6 - Relatórios \n");

    printf("Digite uma opcao: \n");
    scanf("%d", &op);

    while(op!=0){
    switch(op){
            case 0:{
                op=0;
                break;
             }
             break;
            case 1: cad_cam(&cabeca);
                    break;
            case 2: ;
                    break;
            case 3: 
                    break;  
            case 4: imp_cam(cabeca);
                    break;
            case 5:         
                    break;
            case 6:
                   break;
        }
    printf("0 - Fechar \n");
    printf("1 - Cadastrar Caminhão \n");
    printf("2 - Cadastrar Carga \n");
    printf("3 - Cadastrar Cidade \n");
    printf("4 - Imprimir \n" );
    printf("5 - Excluir \n");
    printf("6 - Relatórios \n");

    printf("Digite uma opcao: \n");
    scanf("%d", &op);
    } 
}


void imp_cam(lcam *noatual)
{
    setlocale(LC_ALL, "Portuguese");

    while( noatual != NULL)    
    {
        printf("ID:%d\n", noatual->id);
        printf("Motorista:%s\n", noatual->motorista);
        printf("Marca:%s\n", noatual->marca);
        printf("Local Atual:%s\n", noatual->la);
        printf("Lista de Destinos:%s\n", noatual->lisd);
        printf("KM/L:%.2f\n", noatual->kml);
        noatual = noatual->prox; 
    }
}



void cad_cam (lcam **cabeca)
{
    setlocale(LC_ALL, "Portuguese");

    lcam *noatual, *novono;
    int id;
    char mot[200];
    char mar[200];
    char loca[200];
    char lisd[200];
    float kml;

    printf("ID:\n");
    scanf("%d", &id);
    setbuf(stdin, NULL);
    printf("Motorista:\n");
    scanf("%[^\n]s", &mot);
    setbuf(stdin, NULL);
    printf("Marca:\n");
    scanf("%[^\n]s", &mar);
    setbuf(stdin, NULL);
    printf("Local Atual:\n");
    scanf("%[^\n]s", &loca);
    setbuf(stdin, NULL);
    printf("Lista de Destinos:\n");
    scanf("%[^\n]s", &lisd);
    setbuf(stdin, NULL);
    printf("KM/L:\n");
    scanf("%f", &kml);
    setbuf(stdin, NULL);
    if (*cabeca == NULL)   
    {

        (*cabeca) = malloc(sizeof(lcam));
        (*cabeca)->id = id;
        strcpy((*cabeca)->motorista, mot);
        strcpy((*cabeca)->marca, mar);
        strcpy((*cabeca)->la, loca);
        strcpy((*cabeca)->lisd, lisd);
        (*cabeca)->kml = kml;
        (*cabeca)->prox = NULL;
    }
        else{
        noatual = *cabeca;
        while(noatual->prox != NULL){
            noatual = noatual->prox;    
        novono =  malloc(sizeof(lcam));
        novono->id = id;
        strcpy(novono->motorista, mot);
        strcpy(novono->marca,mar);
        strcpy(novono->la,loca);
        strcpy(novono->lisd,lisd);
        novono->kml = kml;
        novono->prox = NULL;
        noatual->prox = novono;
        }
    }
}
    
asked by anonymous 03.03.2017 / 19:22

1 answer

0

Your problem is in the registry of new trucks. When you register a truck that is not the head, should you look for the last truck on the right list? Well then, there is your mistake.

Your wrong code

else{
    noatual = *cabeca;
    while(noatual->prox != NULL){
        noatual = noatual->prox;    
    novono =  malloc(sizeof(lcam));
    novono->id = id;
    strcpy(novono->motorista, mot);
    strcpy(novono->marca,mar);
    strcpy(novono->la,loca);
    strcpy(novono->lisd,lisd);
    novono->kml = kml;
    novono->prox = NULL;
    noatual->prox = novono;
    }

Note that your while is searching correctly, however you are doing everything else every iteration.

The correct would be to find the last position and then execute the other instructions to chain on the line. Simply removing the while keys will solve your problem.

Correct code

else{
    noatual = *cabeca;

    while(noatual->prox != NULL)
        noatual = noatual->prox;

    novono =  malloc(sizeof(lcam));
    novono->id = id;
    strcpy(novono->motorista, mot);
    strcpy(novono->marca,mar);
    strcpy(novono->la,loca);
    strcpy(novono->lisd,lisd);
    novono->kml = kml;
    novono->prox = NULL;
    noatual->prox = novono;
}

So only the first line immediately below the while will be executed in iterations.

Problem solved =)

    
03.03.2017 / 23:06