如何在c++中附加结构体Person

how do i append a struct Person in c++

本文关键字:结构体 Person c++      更新时间:2023-10-16

我有一个struct Person

struct Person {
    string name;
    int age;
    Person* next;
};

我需要制作一个函数,将结构像链表一样相互附加。我首先创建了一个指针头,它一开始就是一个空指针。

using namespace std;
Person* head = nullptr;
void append(Person*& list, const string& name, int age) {
    auto p = new Person{name, age, nullptr}; // typ List_Node*
    if (list == nullptr) {
        list = p;
        return;
    }
    auto last = list;
    while (last != nullptr) {
        last = last->next;
    }
    last = p;
}
int main() {
    append(head, "First_Person", 21);
    append(head, "Second_Person", 22);
    // testing the result
    head = head->next;
    cout << "head->name outside: " << head->name << endl;
    return 0;
}

现在我遇到的问题是,在第一次附加之后,append()函数似乎没有将第一个人与第二个人链接起来,因为该程序崩溃了

head = head->next;

问题是:如何让append()将1个以上的人附加到此链接列表?

    auto last = list;
    while (last != nullptr){
        last = last->next;
    }
    last = p;

当您退出这个循环时,last将再次指向nullptr,而不是列表的最后一个元素。您需要检查last->next != nullptr

此外,last = p没有做任何有用的事情;它应该是CCD_ 8以便将CCD_。

您的append()没有正确管理指针。试试这个:

void append(Person*& list, const string& name, int age)
{
    auto p = new Person{name, age, nullptr}; // typ List_Node*
    if (list == nullptr)
    {
        list = p;
        return;
    }
    auto last = list;
    while (last->next != nullptr)
        last = last->next;
    last->next = p;
}

如果去掉了list参数,就可以避免在每次追加时遍历整个列表

Person* head = nullptr;
Person* last = nullptr;
void append(const string& name, int age)
{
    auto p = new Person{name, age, nullptr};
    if (head == nullptr)
        head = p;
    if (last != nullptr)
        last->next = p;
    last = p;
}
int main()
{
    append("First_Person", 21);
    append("Second_Person", 22);
    //testing the result
    for(Person *p = head; p != nullptr; p = p->next)
        std::cout << "name: " << p->name << std::endl;
    // free the list
    Person *p = head;
    while (p != nullptr)
    {
        Person *next = p->next;
        delete p;
        p = next;
    }
    return 0;
}

也就是说,你应该使用std::liststd::forward_list来存放你的Person物品,并让它为你处理所有这些细节,例如:

#include <list> // or: #include <forward_list>
struct Person
{
    string name;
    int age;
};
std::list<Person> mylist; // or: std::forward_list<Person> mylist;
void append(const string& name, int age)
{
    mylist.push_back(Person{name, age});
}
int main()
{
    append("First_Person", 21);
    append("Second_Person", 22);
    //testing the result
    for (auto &person: mylist)
        std::cout << "name: " << person.name << std::endl;
    return 0;
}

如果你是在非学术环境中写这篇文章的,那么不要重新发明轮子,而是使用标准的容器,比如向量或列表。

然而,假设练习的重点是学习数据结构,那么使用内置容器并不能教会你什么。

我看到的问题是,你什么也没做,只是更改了"最后"指针,它实际上并没有向列表中添加任何内容。你需要首先找到最后一个节点(通过使用"最后一个"指针移动到每个节点->下一个,直到你到达一个有空->下一指针的节点。然后将该节点的->分配到你的新节点旁边。

您现有的代码。。。

// Already handled empty list case before we get here...
auto last = list;  // last pointer set to address list (head)
while (last != nullptr){    // while not at end of list, last points to next node
    last = last->next;
}
                   // after loop, last will always be null, not very useful
last = p;          // last points to p (the new node) which doesn't _do_ anything

考虑做一些类似的事情:

// Already handled empty list case before we get here...
auto last = list;  // last pointer set to address list (head)
while (last && last->next != nullptr){    // while node->next not null, move to next node
    last = last->next;
}
                   // at end of list, last will always point to last node
last->next = p;    // link last->next to the new 'p' Person

请记住,如果你做了很多列表附加操作,那么你的列表数据结构保留一个头和一个尾指针会更便宜,而附加只会变成

if(tail)
    tail->next = p;
tail = p;

除此之外,在列表的开头插入项目更便宜。

p->next = head;
head = p;