浏览链接列表 我是新来的,C++编程的。我在链表上写了一个非常简单的程序。我动态地创建一个结构角色列表(用户可以决定放入多少节点/元素)。我创建一个指向列表开头的p_head指针作为全局变量,并将其设置为NULL。我使用addPersona方法将元素添加到列表中,然后通过gothrough_list方法创建一个方法来查看和打印列表中的所有元素。在我看来,这似乎是正确的,但我运行了它,最后只打印了最后一个元素(即p_head指出的列表中的第一个元素)。有人能解释一下原因吗?代码如下: #include <iostream> #include <string> using namespace std; struct Persona{ int age; string name; string cell; int choice; Persona* next_persona; }; Persona* p_head = NULL; /*Aggiunge nodi alla lista con campi editabili dall'utente*/ Persona* addPersona() { Persona* p_pers = new Persona; cout << "Insert age:"<< endl; cin >> p_pers->age; cout << "Insert name:"<< endl; cin >> p_pers->name; cout << "Insert phone number: "<< endl; cin >> p_pers->cell; cout << "digit zero to quit otherwise continue pressing 1"<<endl; cin >> p_pers->choice; cout << p_pers->age<<endl; cout << p_pers->name<<endl; cout << p_pers->cell<<endl; while(p_pers->choice==1) { cout << "Insert age"<< endl; cin >> p_pers->age; cout << "Insert name"<< endl; cin >> p_pers->name; cout << "Insert phone number:"<< endl; cin >> p_pers->cell; cout << "digit zero to quit otherwise continue pressing 1"<<endl; cin >> p_pers->choice; cout << p_pers->age<<endl; cout << p_pers->name<<endl; cout << p_pers->cell<<endl; } p_pers->next_persona = p_head; p_head = p_pers; return p_pers; } /* Method that prints element from the list*/ void gothrough_list() { Persona* p_current = p_head; while(p_current != NULL) { cout << "results :"<< endl; cout << p_current->age<<endl; cout << p_current->name<<endl; cout << p_current->cell<<endl; p_current = p_current->next_persona; } delete p_current; } int main() { Persona* p_pers = addPersona(); gothrough_list(); return 0; } #包括 #包括 使用名称空间std; 结构人物{ 智力年龄; 字符串名; 字符串单元; 智力选择; 人物角色*下一个人物角色; }; Persona*p_head=NULL; /*我不知道该怎么编辑*/ 人物角色*添加人物角色() { 人物角色*p_pers=新人物角色; 年龄; cout p_pers->name; cout p_pers->cell; 不能选择; 您在addPersona中的代码不正确
你需要浏览链接列表 我是新来的,C++编程的。我在链表上写了一个非常简单的程序。我动态地创建一个结构角色列表(用户可以决定放入多少节点/元素)。我创建一个指向列表开头的p_head指针作为全局变量,并将其设置为NULL。我使用addPersona方法将元素添加到列表中,然后通过gothrough_list方法创建一个方法来查看和打印列表中的所有元素。在我看来,这似乎是正确的,但我运行了它,最后只打印了最后一个元素(即p_head指出的列表中的第一个元素)。有人能解释一下原因吗?代码如下: #include <iostream> #include <string> using namespace std; struct Persona{ int age; string name; string cell; int choice; Persona* next_persona; }; Persona* p_head = NULL; /*Aggiunge nodi alla lista con campi editabili dall'utente*/ Persona* addPersona() { Persona* p_pers = new Persona; cout << "Insert age:"<< endl; cin >> p_pers->age; cout << "Insert name:"<< endl; cin >> p_pers->name; cout << "Insert phone number: "<< endl; cin >> p_pers->cell; cout << "digit zero to quit otherwise continue pressing 1"<<endl; cin >> p_pers->choice; cout << p_pers->age<<endl; cout << p_pers->name<<endl; cout << p_pers->cell<<endl; while(p_pers->choice==1) { cout << "Insert age"<< endl; cin >> p_pers->age; cout << "Insert name"<< endl; cin >> p_pers->name; cout << "Insert phone number:"<< endl; cin >> p_pers->cell; cout << "digit zero to quit otherwise continue pressing 1"<<endl; cin >> p_pers->choice; cout << p_pers->age<<endl; cout << p_pers->name<<endl; cout << p_pers->cell<<endl; } p_pers->next_persona = p_head; p_head = p_pers; return p_pers; } /* Method that prints element from the list*/ void gothrough_list() { Persona* p_current = p_head; while(p_current != NULL) { cout << "results :"<< endl; cout << p_current->age<<endl; cout << p_current->name<<endl; cout << p_current->cell<<endl; p_current = p_current->next_persona; } delete p_current; } int main() { Persona* p_pers = addPersona(); gothrough_list(); return 0; } #包括 #包括 使用名称空间std; 结构人物{ 智力年龄; 字符串名; 字符串单元; 智力选择; 人物角色*下一个人物角色; }; Persona*p_head=NULL; /*我不知道该怎么编辑*/ 人物角色*添加人物角色() { 人物角色*p_pers=新人物角色; 年龄; cout p_pers->name; cout p_pers->cell; 不能选择; 您在addPersona中的代码不正确,c++,list,C++,List,你需要 p\u pers=新角色; 和p_pers->next_persona=p_head;p_head=p_pers; 也在while循环内 否则,您将始终填充相同的角色对象 一个有效的addPersona()函数是(没有cout调用): Persona*addPersona(){ 人物角色*p_pers=新人物角色; 年龄; cout p_pers->name; cout p_pers->cell; 不能选择; p_pers->next_persona=p_head; 压头=压头; 而(p
p\u pers=新角色;
和p_pers->next_persona=p_head;
p_head=p_pers;
也在while
循环内
否则,您将始终填充相同的角色对象
一个有效的addPersona()
函数是(没有cout调用):
Persona*addPersona(){
人物角色*p_pers=新人物角色;
年龄;
cout p_pers->name;
cout p_pers->cell;
不能选择;
p_pers->next_persona=p_head;
压头=压头;
而(p_pers->choice==1){
p_pers=新角色;
年龄;
cout p_pers->name;
cout p_pers->cell;
不能选择;
p_pers->next_persona=p_head;
压头=压头;
}
返回p_pers;
}
要删除,可以使用delete p\u pers;
其中p\u pers指向
你想删除的人物角色。但是请注意,你需要事先调整
周围的指针(
p\u pers\u previous->next=p\u pers\u previous->next->next;
)。您只是将最后一个角色插入到列表中。只有一个更改可以解决问题
在前面的例子中,您创建的节点被您稍后添加的节点覆盖。通过在创建新角色对象的信息之前将每个角色对象添加到列表中,我们不会覆盖早期版本
该守则的更新版本如下:
#include <iostream>
#include <string>
using namespace std;
struct Persona{
int age;
string name;
string cell;
int choice;
Persona* next_persona;
};
Persona* p_head = NULL;
/*Aggiunge nodi alla lista con campi editabili dall'utente*/
Persona* addPersona(){
Persona* p_pers = new Persona;
cout << "Insert age:"<< endl;
cin >> p_pers->age;
cout << "Insert name:"<< endl;
cin >> p_pers->name;
cout << "Insert phone number: "<< endl;
cin >> p_pers->cell;
cout << "digit zero to quit otherwise continue pressing 1"<<endl;
cin >> p_pers->choice;
cout << p_pers->age<<endl;
cout << p_pers->name<<endl;
cout << p_pers->cell<<endl;
p_pers->next_persona = p_head;
p_head = p_pers;
while(p_pers->choice==1){
p_pers = new Persona;
cout << "Insert age"<< endl;
cin >> p_pers->age;
cout << "Insert name"<< endl;
cin >> p_pers->name;
cout << "Insert phone number:"<< endl;
cin >> p_pers->cell;
cout << "digit zero to quit otherwise continue pressing 1"<<endl;
cin >> p_pers->choice;
cout << p_pers->age<<endl;
cout << p_pers->name<<endl;
cout << p_pers->cell<<endl;
p_pers->next_persona = p_head;
p_head = p_pers;
}
return p_pers;
}
/* Method that prints element from the list*/
void gothrough_list(){
Persona* p_current = p_head;
while(p_current != NULL){
cout << "results :"<< endl;
cout << p_current->age<<endl;
cout << p_current->name<<endl;
cout << p_current->cell<<endl;
p_current = p_current->next_persona;
}
delete p_current;
}
int main()
{
Persona* p_pers = addPersona();
gothrough_list();
return 0;
}
#包括
#包括
使用名称空间std;
结构人物{
智力年龄;
字符串名;
字符串单元;
智力选择;
人物角色*下一个人物角色;
};
Persona*p_head=NULL;
/*我不知道该怎么编辑*/
人物角色*添加人物角色(){
人物角色*p_pers=新人物角色;
年龄;
cout p_pers->name;
cout p_pers->cell;
不能选择;
CUTHEEL' PyPES =新角色;“循环中缺少。只是一个注释,正如你所说的,你是C++的新手:你不应该实现你自己的列表。使用标准模板库。也要理解C和C++之间的差异,以充分利用后者。”
#include <iostream>
#include <string>
using namespace std;
struct Persona{
int age;
string name;
string cell;
int choice;
Persona* next_persona;
};
Persona* p_head = NULL;
/*Aggiunge nodi alla lista con campi editabili dall'utente*/
Persona* addPersona(){
Persona* p_pers = new Persona;
cout << "Insert age:"<< endl;
cin >> p_pers->age;
cout << "Insert name:"<< endl;
cin >> p_pers->name;
cout << "Insert phone number: "<< endl;
cin >> p_pers->cell;
cout << "digit zero to quit otherwise continue pressing 1"<<endl;
cin >> p_pers->choice;
cout << p_pers->age<<endl;
cout << p_pers->name<<endl;
cout << p_pers->cell<<endl;
p_pers->next_persona = p_head;
p_head = p_pers;
while(p_pers->choice==1){
p_pers = new Persona;
cout << "Insert age"<< endl;
cin >> p_pers->age;
cout << "Insert name"<< endl;
cin >> p_pers->name;
cout << "Insert phone number:"<< endl;
cin >> p_pers->cell;
cout << "digit zero to quit otherwise continue pressing 1"<<endl;
cin >> p_pers->choice;
cout << p_pers->age<<endl;
cout << p_pers->name<<endl;
cout << p_pers->cell<<endl;
p_pers->next_persona = p_head;
p_head = p_pers;
}
return p_pers;
}
/* Method that prints element from the list*/
void gothrough_list(){
Persona* p_current = p_head;
while(p_current != NULL){
cout << "results :"<< endl;
cout << p_current->age<<endl;
cout << p_current->name<<endl;
cout << p_current->cell<<endl;
p_current = p_current->next_persona;
}
delete p_current;
}
int main()
{
Persona* p_pers = addPersona();
gothrough_list();
return 0;
}