C++ 如何在c++;
我有一个结构人C++ 如何在c++;,c++,pointers,struct,C++,Pointers,Struct,我有一个结构人 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
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()
将多人追加到此链接列表?您的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;
}
当您退出此循环时,last
再次指向nullptr
,而不是列表的最后一个元素。您需要检查last->next!=改为空PTR
而且,
last=p
没有做任何有用的事情;它应该是last->next=p
,以便实际将p
附加到列表中。如果你是在非学术背景下写这篇文章的,那么就不要重新发明轮子,而要使用像vector或list这样的标准容器
然而,假设练习的重点是学习数据结构,那么使用内置容器并不能教会您很多东西
我看到的问题是,除了更改“last”指针之外,您什么也不做,它实际上没有向列表中添加任何内容。您需要首先找到最后一个节点(通过使用“last”指针移动到每个节点->下一个节点,直到到达一个具有空->下一个指针的节点。然后将该节点的->下一个分配给新节点
您现有的代码
// 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;
使用
std::vector
或std::list
保存您的Person
项目,然后您可以简单地push_back()
在需要时新的人
。如果你摆脱了下一个成员,std::forward\u list
将与你拥有的相同,但更好。std::list list;
:这个名字在append
中可能不明确,我会选择另一个:)(请参阅)希望你不介意编辑,如果你不同意,请随时回退。
// 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;