Delete a record in struct using free ()

1

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");
    }
    
asked by anonymous 17.06.2018 / 02:41

1 answer

1

For an exercise code this is fine, but know that in real code it would be something quite different from this. Some things in this code work by coincidence, in certain situations it will not work.

Since you are using a linked list, you do not have to relocate. You should only use free() . It must be given at the address of the list element. Then the previous element should get the address of the element that was pointed at the element being removed, which may even be 0.

aux[n_remover] does not make sense because you do not have an array , not even through a stable, linear pointer.

I have no idea what it is atendimento-- , the impression that gives you chose something random and played in the code.

I'm afraid of this aux , it seems like a big deal, and I doubt that this is working except in a very naive test.

When it comes to several errors, it's hard to find a solution, because you'll have to solve all of them until it works.

    
17.06.2018 / 02:58