如何在c++中附加结构体Person
how do i append a struct Person in c++
我有一个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::list
或std::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;
相关文章:
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 包含 std::list 的结构体的 C++ 初始化
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何使用结构体的向量数组?
- 如何使用结构体在C++中更改这些代码?
- 无法在 Mosquitto MQTT Broker 插件上访问结构体 mosquitto 的元素
- 我应该如何在C++中使用结构体解决输入失败的问题?
- Qsort() 比较结构体整数的总和
- 如何使用迭代器指向结构体c++的向量
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 如何获取结构体成员的地址
- 创建结构体向量,表达式:向量下标超出范围
- boost::任何带有结构体和无符号整数
- 如何在构造函数中初始化结构体的动态数组?
- 只写结构体的某些字段
- C++ push_back() 一个结构体到一个向量中
- C++14 结构体上的统一初始化
- 如何在c++中附加结构体Person