简体   繁体   English

删除带有struct元素的单链接列表

[英]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. a)编写一个函数void printPerson(person_t pers)以显示对象person_t。

b) Write a function void printPersList(PersListElement *p) which displays every person within the list. b)编写一个函数void printPersList(PersListElement * p),该函数显示列表中的每个人。 Use the function printPerson() 使用函数printPerson()

c) Write a recursive function void printReversePersList(PersListElement *p), which displays every saved person in reverse order. c)编写一个递归函数void printReversePersList(PersListElement * p),该函数以相反的顺序显示每个已保存的人。 Ie the person which was saved last should be displayed first, etc.. Use the function printPerson() 即,最后保存的人应首先显示,等等。使用函数printPerson()

d) Write a function void deletePersList(PersListElement *p) which deletes a list. d)编写一个函数void deletePersList(PersListElement * p)删除列表。

e) Test your functions with the following program: e)使用以下程序测试您的功能:

 #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. 我对a分配给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. 至少,当我在使用删除功能后使用printPersList函数时,它的姓氏显示为mumbo-jumbo,而名字仍保持不变。

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

void printPersList(PersListElement *p){

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

c) C)

void printReversePersList(PersListElement *p){

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

d) 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. 另外,请记住,我只是为了stackoverflow将整个任务从德语翻译为英语,因此,如果deletePersList函数之外的地方还有其他错误,请忽略该内容,因为翻译所有内容时我可能忽略了这一点。

You're freeing p and then using p , which is undefined behavior. 您要释放p ,然后使用p ,这是未定义的行为。 You need to get p->next and store it in a temp variable or something before you free p 您需要先获取p->next并将其存储在temp变量中, 然后再释放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; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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