Delete a struct record using free()

Asked

Viewed 470 times

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).

1 answer

1

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

Since you are using a linked list you have no reason to relocate. You should only use free(). It has to be given at the address of the list item. And then the previous element should receive the address of the element that was pointed out in the element that is being removed, which can be up to 0.

The aux[n_remover] doesn’t make sense because you don’t have one array, not even through a stable, linear pointer.

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

I’m afraid of that aux, looks like Ambi in the thick and I doubt that this is working other than in a very naive test.

When several errors is difficult to find a solution, because you will have to solve all until it works.

  • I get it, aux[n_remove] I tried to adapt from a previous code because in reality I’m terrible at C as it is already remarkable, so I don’t have much sense. The whole delete() function I just tried to adapt from another exercise but it didn’t work. Regarding this aux, it’s kind of standard in the classroom so we always use it, it was a way for the teacher to introduce programming languages (which I totally disagree with since this is difficult). Face this activity is worth points and I spent the whole week trying to make this business work and it’s no use! Give me a light please

  • From what you have in the question what can help is this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.