1
I’m creating a record using struct
, where I must have a menu to include, show and attend (exclude) patients.
The problem is in the exclusion function that because of my lack of knowledge I do not understand what would be the correct way to accomplish. I thought to use the free()
but by dealing directly with memory I’m not sure if it would work in all cases.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char menu(){
fflush (stdin);
printf ("\n\t\t-----Menu-----");
printf ("\n\t\t 1 Incluir Paciente");
printf ("\n\t\t 2 Mostrar Pacientes");
printf ("\n\t\t 3 Atender Paciente");
printf ("\n\t\t 0 Sair");
char op = getch();
system ("cls");
return op;
}
typedef struct paciente{
char nome [20], matricula [6], senha[6], atendimento[50];
struct paciente *prox;
}pacienteat;
pacienteat *ini, *aux;
void inicio(){
ini= NULL;
}
int estaVazia(){
return (ini==NULL);
}
void inserir (){
int senha=1;
aux=(pacienteat*) malloc(sizeof(pacienteat));
printf("\nInforme o numero do atendimento");
fflush(stdin);
gets(aux->atendimento);
printf ("\nInforme o nome do paciente: ");
fflush (stdin);
gets (aux->nome);
printf ("\nInforme o numero da matricula: ");
fflush (stdin);
gets (aux->matricula);
printf ("\nInforme a senha atendimento: ");
fflush (stdin);
gets (aux->senha);;
aux->prox = ini;
ini = aux;
}
void listar(){
for (aux = ini; aux != NULL; aux = aux->prox){
printf("\nNome: %s - Matricula: %s - Atendimento: %s - Senha: %s", aux->nome, aux->matricula, aux->atendimento, aux->senha);
}
}
void excluir(){
int n_remover;
listar();
printf("Digite o numero de quem voce quer remover");
scanf("%d",&n_remover);
aux[n_remover] = aux[aux->atendimento-1];
atendimento--;
aux = (pacienteat*) realloc(pacienteat,num*sizeof(pacienteat));
}
int main(){
inicio();
int x=0,nn;
char op=menu();
while(op != '0'){
switch (op){
case'1':
inserir();
break;
case'2':
listar();
break;
case'3':
excluir();
break;
}
op = menu();
}
system("pause");
}
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero