简体   繁体   中英

Delete singly-linked list with struct elements

I currently am stuck with a task on my current assignment and I have no clue, why it does what it does, instead of what I want.

First of all, here's the assignment:

Provided is a singly linked list, which is defined as:

 typedef struct { char lastname[30]; char firstname[30]; } person_t; typedef struct perslistelement { person_t pers_obj; struct perslistelement *next; } PersListElement; 

a) Write a function void printPerson(person_t pers) to display an object person_t.

b) Write a function void printPersList(PersListElement *p) which displays every person within the list. Use the function printPerson()

c) Write a recursive function void printReversePersList(PersListElement *p), which displays every saved person in reverse order. Ie the person which was saved last should be displayed first, etc.. Use the function printPerson()

d) Write a function void deletePersList(PersListElement *p) which deletes a list.

e) Test your functions with the following program:

 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { char lastname[30]; char firstname[30]; } person_t; typedef struct perslistelement { person_t pers_obj; struct perslistelement *next; } PersListElement; PersListElement *insertPerson(PersListElement *first, char lastname[], char firstname[]){ PersListElement *p; p = (PersListElement*)malloc(sizeof(PersListElement)); strcpy(p->pers_obj.lastname, lastname); strcpy(p->pers_obj.firstname, firstname); p->next = first; first = p; return first; } /* Insert functions from a ) , b ) , c ) and d) ! */ int main(void){ PersListElement *p = NULL; printf("Test Program : Assignment 8 / Task 1"); printf(" Step 1: Building the list \\n" ); p = insertPerson(p, "Banner", "Bruce"); p = insertPerson(p, "Stark", "Tony"); printf("\\nStep 2: Printing the list\\n"); printPersList(p); printf("\\nStep 3: Printing the list in reverse order\\n"); printReversePersList(p); printf("\\nStep 4: Deleting the list\\n"); deletePersList(p); return 0; } 

I didn't have any issues with assignment a to c. The functions I've written work perfectly fine and do what they should. However, for some reason the last function, where I'm supposed to delete the list only deletes the lastname part of the list elements. At least, when I use the printPersList function after using the delete function it displays the lastnames as mumbo-jumbo, while the firstnames are still intact.

This is what it looks like:

See attached image

Here are the functions I've written:

a)

void printPerson(person_t pers){

    printf("Last Name: %s \nFirst Name: %s\n\n", pers.lastname,pers.firstname);

}

b)

void printPersList(PersListElement *p){

    while(p != NULL) {
        printPerson(p->pers_obj);
        p = p->next;
    }
}

c)

void printReversePersList(PersListElement *p){

    if (p == NULL) return;
    printReversePersList(p->next);
    printPerson(p->pers_obj);
}

d)

void deletePersList(PersListElement *p){

    while(p != NULL) {
        free(p);
        p = p->next;
    }
}

Can someone please explain to me, what the mistake was and how to solve it?

Also, please keep in mind that I translated this whole assignment from German to English just for stackoverflow, so if there are some other mistakes anywhere outside of the deletePersList function, ignore that please, as it's probably something I just overlooked while translating everything.

You're freeing p and then using p , which is undefined behavior. You need to get p->next and store it in a temp variable or something before you free p

void deletePersList(PersListElement *p){

    while(p != NULL) {
        free(p);
        p = p->next; // this is undefined
    }
}

A solution might be

void deletePersList(PersListElement *p){
    while(p != NULL) {
        PersListElement *next = p->next;
        free(p);
        p = next; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM