Thread: How to add and delete items in a structure?

  1. #16
    Registered User
    Join Date
    May 2017
    Posts
    101
    Yes, I am a little backward... And I am worried because this project must be evaluated... But the thing is we haven't told a lot of things... So today I am breaking my head trying to solve something even if it's not so good...

    In the link I provided (the last one after editing) it is explained how to delete a node. This is my problem:

    Code:
    int delete(int num)
    {
        struct node *temp, *prev;
        temp=head;
        while(temp!=NULL)
        {
        if(temp->data==num)
        {
            if(temp==head)
            {
            head=temp->next;
            free(temp);
            return 1;
            }
            else
            {
            prev->next=temp->next;
            free(temp);
            return 1;
            }
        }
        else
        {
            prev=temp;
            temp= temp->next;
        }
        }
        return 0;
    }
    look at this: if(temp->data==num). Look at the structure:

    Code:
    struct node
    {
        int data;
        struct node *next;
    }*head;
    I don't have a number such as this easy example I have a lot of chars, I can't simply put if (temp->data) so I'm lost at this point (I'm speaking about deleting nodes).

    A further question: is a certificate of a course mandatory in case I am in a selection process where programming is only one of the requirements for an engineering job position? (not for software developer, just for example: mechanical engineer; req 1: experience in x, req 2: Microsoft Office, req1: some skills with C programming) Because I feel this is a fail... Of course is my fault guys, you are helping me as any other person is doing.

    Edited: In other words, this link compares a number with data but what about me? I don't know how I can compare...
    Last edited by JorgeChemE; 05-29-2017 at 02:10 PM.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I don't have a number such as this easy example I have a lot of chars, I can't simply put if (temp->data) so I'm lost at this point (I'm speaking about deleting nodes).
    Yes, but you can compare arrays of chars together, that is what strcmp() is for. So if you enter modify delete() to enter a name that you know, you should be able to write an if() with that and delete the correct data.

    In real hospitals in the US, they really do give you numbers though. You might want to consider giving the patients each their unique IDs. This way you can do searches prior to calling delete(), and it doesn't matter if you treat one John Smith or 30, each John Smith has their own ID that matches if (temp->data==num). It's something worth thinking about doing at any rate.

  3. #18
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by whiteflags View Post
    Yes, but you can compare arrays of chars together, that is what strcmp() is for. So if you enter modify delete() to enter a name that you know, you should be able to write an if() with that and delete the correct data.

    In real hospitals in the US, they really do give you numbers though. You might want to consider giving the patients each their unique IDs. This way you can do searches prior to calling delete(), and it doesn't matter if you treat one John Smith or 30, each John Smith has their own ID that matches if (temp->data==num). It's something worth thinking about doing at any rate.
    I see your point. That's what worries me. So I store a lot of data, among them is the name of the patience. The easiest way that I thought about is deleting comparing a char name with the name of the patience. What about the rest of the chars? (rage, sex, ...) will be deleted as well?

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Yes. Structs have all their members together. If you free the variable, all of the data is deleted; this includes age, diagnosis, everything. You were told this already.

  5. #20
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by whiteflags View Post
    Yes. Structs have all their members together. If you free the variable, all of the data is deleted; this includes age, diagnosis, everything. You were told this already.
    I hate to do that... but you are my hope.

    I think I have the delete function, this is what I have:

    Code:
    void delete_patient(tpatient *ptr, char *name) {
       tpatient *temp, *prev;
       temp=first;
    
       while (temp!=NULL) {
            if (strcmp(temp->name_patient,name)==0) {
                if(temp==first) {
                    first=temp->next;
                    free(temp);
                    return;
                } else {
                    prev->next=temp->next;
                    free(temp);
                    return;
                }
            } else {
                prev=temp;
                temp=temp->next;
            }
        }
    }
    In the menu, in int main () I have:

    Code:
    tpatient *ptr=patients;
    char name[40];
    
    case 'E':
                printf("Insert the name to delete: ");
                scanf("%s", name);
                if (delete_patient(nombre)) {
                    printf("The patient %s has been removed", name);
                } else {
                    printf("The patient %s is not in the list", name);
                }
                /*if (p==NULL) {
                    printf("\nPatient not found");
                } else {
                    delete_patient(patients, name);
                }*/
                break;
    in if (delete_patient(nombre)) I get the error:

    Code:
    cannot convert 'char*' to 'tpatient* {aka patient*}' for argument '1' to 'void delete_patient(tpatient*, char*)'
    Do you know how can I fix this? Why is complaining?

  6. #21
    Registered User
    Join Date
    May 2017
    Posts
    101
    I hate to do that... but you are my hope.


    I think I have the delete function, this is what I have:


    Code:
    void delete_patient(tpatient *ptr, char *name) {
       tpatient *temp, *prev;
       temp=first;
    
       while (temp!=NULL) {
            if (strcmp(temp->name_patient,name)==0) {
                if(temp==first) {
                    first=temp->next;
                    free(temp);
                    return;
                } else {
                    prev->next=temp->next;
                    free(temp);
                    return;
                }
            } else {
                prev=temp;
                temp=temp->next;
            }
        }
    }

    In the menu, in int main () I have:

    Code:
    tpatient *ptr=patients;
    char name[40];
    
    
    case 'E':
                printf("Insert the name to delete: ");
                scanf("%s", name);
                if (delete_patient(name)) {
                    printf("The patient %s has been removed", name);
                } else {
                    printf("The patient %s is not in the list", name);
                }
                /*if (p==NULL) {
                    printf("\nPatient not found");
                } else {
                    delete_patient(patients, name);
                }*/
                break;
    In if (delete_patient(name)) I get:
    Code:
    error: cannot convert 'char*' to 'tpatient* {aka patient*}' for argument '1' to 'void delete_patient(tpatient*, char*)'|
    Do you know how can I fix this? Why is complaining?

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Honestly, how much of this have you copied without thinking? The code on this website is not perfectly adapted to your program - the code you can copy on the website uses global variables to simplify the listing.

    This leads to your program having several problems: you called your delete() function in main() with 1 name argument, when the prototype asks for 2 arguments: the tpatience pointer first followed by the name. Additionally, when you adapted the delete() function, you didn't finish renaming all the variables to variables that you were actually using in the rest of the program.

    When you copy, you at least have to be careful to make sure that everything matches. You can't use ptr as a name in your program, when the original author used first in his.

  8. #23
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by whiteflags View Post
    Honestly, how much of this have you copied without thinking?
    Almost everything. The program runs now, you are right, asks for two arguments: delete_patient(patients, name) <--- just fixed

    but is always telling me the name is not in the list. I will research more. It will be a long night.

  9. #24
    Registered User
    Join Date
    May 2017
    Posts
    101
    I finally succeded in all the functions but I am still working in the last one: modify a patient. This is my code of the structures:

    Code:
    typedef struct {
        int day;
        int month;
        int year;
    } date;
    
    
    typedef struct patient {
        char name_patient[100];
        char rage[100];
        char sex[100];
        char observations[1024];
        char fiagnosis[1024];
        date birth;
        struct patient *next;
    } tpatient;
    
    tpatient *first=(tpatient*) NULL;
    tpatient *last=(tpatient*) NULL;
    This is the function to modity the patient:

    Code:
    void modify_patient() {
        tpatient *ptr;
        tpatient *prev=NULL;
        tpatient *temp=NULL;
        char name_patient[100];
        printf("\nInsert the patient to modify: ");
        gets(name_patient);
    
    
        while (temp!=NULL) {
            if (strcmp(temp->name_patient,name_patient)==0) {
                if(temp==first) {
                    prev=temp;
                    temp=temp->next;
                    break;
                }
            }
        }
        ptr=new tpatient;
        fflush(stdin);
        printf("Insert the name of the patient: ");
        gets(ptr->name_patient);
        fflush(stdin);
        printf("Insert the date of birth of the patient (dd/mm/yy): ");
        scanf("%d/%d/%d", &ptr->birth.day, &ptr->birth.month, &ptr->birth.year);
        printf("Insert the rage of the patient: ");
        gets(ptr->raza);
        flush(stdin);
        printf("Insert the sex of the patient: ");
        gets(ptr->sex);
        fflush(stdin);
        printf("Insert the observations of the patient: ");
        gets(ptr->observations);
        fflush(stdin);
        printf("Insert the diagnosis of the patient: ");
        gets(ptr->diagnosis);
        fflush(stdin);
        prev->next=ptr;
        ptr->next=temp->next;
        temp->next=NULL;
        delete(temp);
        printf("Patient modified successfully");
    }
    in int main () I simply call the function in the menu:

    Code:
    case 'f':
    case 'F': modify_patient();
    break;
    The program asks to modify all the data, then crash. I know I have magic numbers and using gets that I will replace with fgets, I am aware of that, I will change later when everything works. Aside from gets, that I know are not recommended, what can I do to prevent crash?

  10. #25
    Registered User
    Join Date
    May 2017
    Posts
    101
    I can't edit the last post. I have some mispellings because of translating the code. In my program I don't have misspellings.

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    I have some mispellings because of translating the code. In my program I don't have misspellings.
    Then please stop "translating" your code. Even if we don't speak your language we do understand C.

    Jim

  12. #27
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Then please stop "translating" your code. Even if we don't speak your language we do understand C.

    Jim
    To protect my work I changed the code a little, the program is not about patients, is about a pets and a vet. Anyway, this is my code, ony this part of modifying pets:

    Structure:

    Code:
    typedef struct {
        int dia;
        int mes;
        int anio;
    } fecha;
    
    
    typedef struct mascota {
        char nombre_mascota[100];
        char tipo_mascota[100];
        char raza[100];
        char sexo[100];
        char observaciones[1024];
        char dolencia[1024];
        fecha nacimiento;
        struct mascota *siguiente;
    } tmascota;
    Function to modify pets:

    Code:
    void modificar_mascota() {
        char nombre_mascota[100];
        printf("\nIntroduzca el nombre de la mascota a modificar: ");
        gets(nombre_mascota);
        tmascota *ptr, *prev, *temp;
        prev=primero;
        temp=primero;
    
    
        while (temp!=NULL) {
            if (strcmp(temp->nombre_mascota,nombre_mascota)==0) {
                if(temp==primero) {
                    prev=temp;
                    temp=temp->siguiente;
                }
            }
        }
        ptr=new tmascota;
        fflush(stdin);
        printf("Introduzca el nombre de la mascota: ");
        gets(ptr->nombre_mascota);
        fflush(stdin);
        printf("Introduzca la fecha de nacimiento de la mascota (dd/mm/aa): ");
        scanf("%d/%d/%d", &ptr->nacimiento.dia, &ptr->nacimiento.mes, &ptr->nacimiento.anio);
        fflush(stdin);
        printf ("introduzca el tipo de mascota: ");
        gets(ptr->tipo_mascota);
        fflush(stdin);
        printf("Introduzca la raza de la mascota: ");
        gets(ptr->raza);
        fflush(stdin);
        printf("Introduzca el sexo de la mascota: ");
        gets(ptr->sexo);
        fflush(stdin);
        printf("Introduzca las observaciones de la mascota: ");
        gets(ptr->observaciones);
        fflush(stdin);
        printf("Introduzca la dolencia de la mascota: ");
        gets(ptr->dolencia);
        fflush(stdin);
        prev->siguiente=ptr;
        ptr->siguiente=temp->siguiente;
        temp->siguiente=NULL;
        delete temp;
    }
    The part of the menu in int main():

    Code:
            case 'F':
                if (primero!=NULL) {
                    modificar_mascota();
                    printf("\nSe ha modificado la mascota");
                } else {
                    printf("La mascota introducida no esta en la lista");
                }
                break;
    Last edited by JorgeChemE; 05-31-2017 at 06:55 AM.

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    gets()? Really? You should know better by now!

    Why are you using new in a C program? You really really should figure out if you want to write a C or C++ program and use the proper features of the language you want to use.

    Why are you trying to allocate memory for a single instance of your structure? Why not just use a single instance of the structure instead?

    Also what's with all those fflush() calls? Calling fflush() on an input stream can lead to Undefined Behavior.

    Jim

  14. #29
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    gets()? Really? You should know better by now!

    Why are you using new in a C program? You really really should figure out if you want to write a C or C++ program and use the proper features of the language you want to use.

    Why are you trying to allocate memory for a single instance of your structure? Why not just use a single instance of the structure instead?

    Also what's with all those fflush() calls? Calling fflush() on an input stream can lead to Undefined Behavior.

    Jim
    Jim, I know how to use fgets but we are required to submit this project to be evaluated, in class we only studied scanf and gets for this purpose, we also studied getch, getchar, getche, puts and maybe something more I almost forget. The code I am using is the one the instructor expects according to what we are learning, otherwise, she would think we are cheating.

    As far as I know, the program is not crashing for using gets neither fflush(stdin) because I tried removing them with the same result, crash. If only someone can tell me why the program is crashing maybe I can fix it.

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    Good luck since you refuse to take any advice given there is no reason I should continue to offer assistance.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  2. using delete on a structure pointer
    By spongefreddie in forum C++ Programming
    Replies: 9
    Last Post: 07-06-2011, 12:12 PM
  3. Delete records in Structure
    By Myrren in forum C Programming
    Replies: 6
    Last Post: 11-19-2010, 02:15 AM
  4. Delete All Items In A Linked List
    By Khellendros in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2009, 01:22 AM
  5. Delete duplicate items in an array
    By chaos in forum C Programming
    Replies: 4
    Last Post: 06-16-2005, 02:51 PM

Tags for this Thread