I am creating a registry using struct
, where I should have a menu to include, display and answer (exclude) patients.
The problem is in the delete function that because of my lack of knowledge I do not understand what the correct way to do it would be. I thought about using free()
but for 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");
}