4
I have two lists in C, where you should include several records in the list of customers, and then make sure that some customer can book a car that is stored in the list of cars. The part of inclusion, removal and visualization of customers I can do well, the problem is time to get the customer to book a car, ie the cell with the customer’s name point to the list with the reserved car name.
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 don’t have the slightest idea how to make my customer list point to a particular node on my car list, so I was wondering if someone could guide me on how to do this exactly.
NOTE: I am new in C.
To make the reservation, will the user inform the customer code and the car code? A car can be reserved for two people at the same time (I suppose not)?
– Victor Stafusa
That first informs the customer’s code and then the car’s code so it can be booked. And the same car can’t be reserved for two people at the same time.
– Xavier