Lists chained in C

5

I have two lists in C, where you have to include several records in the list of customers, and then have some customer able to reserve a car that is stored in the list of cars. The part of inclusion, removal and visualization of customers I can do good, the problem is when it is time for the customer to book a car, ie the cell with the customer's name pointing to the list with the name of the reserved car .

 struct carro
{
        int codigo;
        char modelo[30];
        struct carro *proximo;
};

struct cliente
{
        int codigo;
        char nome[50];
        struct cliente *proximo;
        struct carro *reserva;
};


// Inserir no inicio da lista; 
struct cliente* insereInicio(struct cliente *pInicio,int codigo, char nome[50]){

        struct cliente *aux;
        aux = (struct cliente *)malloc(sizeof(struct cliente));
        aux->codigo = codigo;
        strcpy(aux->nome,nome);
        aux->proximo = pInicio;
        pInicio = aux;
        return pInicio;
}

void insereDepois(struct cliente *p, int codigo, char nome[50])
{
        struct cliente *aux;

        aux= (struct cliente *) malloc(sizeof(struct cliente));

        aux ->codigo=codigo;
        strcpy(aux->nome,nome);
        aux ->proximo=p->proximo;
        p->proximo=aux;

}  

struct cliente* insereOrdenado(struct cliente *pInicio, int codigo, char nome[50]){
        struct cliente *p, *q;
        if(pInicio == NULL || codigo < pInicio->codigo){
                return (insereInicio(pInicio, codigo, nome));          
        }
        p = pInicio;
        q = p;
        while(q != NULL && q->codigo > codigo){
                p = q;
                q = p->proximo;
        }
        if(q == NULL || q->codigo < codigo){
                insereDepois(p,codigo,nome);
        }
        else{
                printf("\nElemento ja existe");
        }
        return (pInicio);
}

main()
{
        struct ciiente *inicio;
        inicio = NULL;

        int opcao = 0;
        int codigo;
        char nome[50];

        while(1)
        {
            system("cls");
            printf("-----# Bem Vindo #-----\n");
            printf("\n1 - Incluir Cliente");
            printf("\n2 - Listar clientes");
            printf("\n3 - Sair do Programa\n");
            scanf("%d",&opcao);

            if(opcao == 1) {
                system("cls");
                printf("-----# Inserir novo Cliente #-----\n");
                printf("\nDigite o codigo do cliente: ");
                scanf("%d",&codigo);
                printf("Digite o nome do Cliente: ");
                fflush(stdin);
                scanf("%s",nome);

                inicio = insereOrdenado(inicio, codigo, nome);
            }   
            else if(opcao == 2)
            {
                system("cls");
                printf("-----# Clientes Cadastrados #-----\n");
                percorreLista(inicio);
            }
            else if(opcao == 3)
            {
                break;
            }
        }

My problem is that I have no clue how to get my list of customers to point to a certain node in my list of cars, so I would like to know if anyone could guide me how to do this exactly.

NOTE: I am new to C.

    
asked by anonymous 29.10.2015 / 22:34

1 answer

1

First, you'll have to change the struct carro to say who reserved it:

struct cliente;

struct carro
{
    int codigo;
    char modelo[30];
    struct carro *proximo;
    struct cliente *reservado;
};

struct reserva
{
    struct reserva *proximo;
    struct carro *carro;
};

struct cliente
{
    int codigo;
    char nome[50];
    struct cliente *proximo;
    struct reserva *reserva;
};

Note that the first struct cliente statement is required because carro reference cliente and cliente reference carro . This is the way to make the compiler accept cyclic dependency.

The reserva structure is for me to have a list of cars for each person.

Let's put two search functions by code:

struct carro *localizarCarroPorCodigo(int codigo, struct carro *primeiro) {
    for (struct carro *c = primeiro; c != NULL; c = c->proximo) {
        if (c->codigo == codigo) return c;
    }
    return NULL;
}


struct carro *localizarClientePorCodigo(int codigo, struct cliente *primeiro) {
    for (struct cliente *c = primeiro; c != NULL; c = c->proximo) {
        if (c->codigo == codigo) return c;
    }
    return NULL;
}

Now, we do the reserve function:

void reservarCarro(int codigoCliente, int codigoCarro, struct cliente *primeiroCliente, struct carro *primeiroCarro) {
    struct cliente *cl = localizarClientePorCodigo(codigoCliente, primeiroCliente);
    if (cl == NULL) {
        printf("Nao existe o cliente com o codigo %d.", codigoCliente);
        return;
    }

    struct carro *ca = localizarCarroPorCodigo(codigoCarro, primeiroCarro);
    if (ca == NULL) {
        printf("Nao existe o carro com o codigo %d.", codigoCarro);
        return;
    }

    if (ca->reservado != NULL) {
        printf("Este carro ja esta reservado.");
        return;
    }

    struct reserva *nova = (struct reserva *) malloc(sizeof(struct reserva));
    nova->carro = ca;
    nova->proximo = cl->reserva;
    cl->reserva = nova;
    ca->reservado = cl;
    printf("Reserva efetuada com sucesso.");
}

And it's also important to do the function of releasing a buffer. This is similar to the previous one, but it is easier. Starting from the car code, you find the customer. And then you remove the car from the reserve list and put the car's customer to NULL . It looks something like this:

void liberarCarro(int codigoCarro, struct carro *primeiroCarro) {
    struct carro *ca = localizarCarroPorCodigo(codigoCarro, primeiroCarro);
    if (ca == NULL) {
        printf("Nao existe o carro com o codigo %d.", codigoCarro);
        return;
    }

    struct client *cl = ca->reservado;
    if (cl == NULL) {
        printf("Este carro nao estava reservado.");
        return;
    }

    struct reserva *r = NULL;
    struct reserva *s = cl->reserva;
    while (s != NULL) {
        if (s->carro == ca) {
            if (r != NULL) r->proximo = s->proximo;
            if (s == cl->reserva) cl->reserva = s->proximo;
            free(s);
            break;
        }
        r = s;
        s = s->proximo;
    }
    ca->reservado = NULL;
    printf("Reserva liberada com sucesso.");
}

And when it comes to entering the customer, it's important to make sure the code is unique. One way to do this is to insert the client, look for a client with that code and only let the insertion if it does not find (that is, the search results in NULL ). The same goes for the car. Another strategy is to have the program generate the codes automatically instead of asking the users to type them and then the program informs the generated codes to the user.

To list all cars for a customer:

void listarCarros(struct cliente *cliente) {
    for (struct reserva *s = cl->reserva; s != NULL; s = s->proximo) {
        printf("Codigo: %d - Modelo: %s", s->carro->codigo, s->carro->modelo);
    }
}
    
29.10.2015 / 23:53