正在浏览链表
going through a linked list
我是新手,在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;
}
所以它只打印用户插入的最后一个元素(列表的第一个),而不是其他元素。根据某些条件删除特定节点后,是否必须使用删除(节点/元素)?感谢大家想帮助我^ ^
瓦莱里奥
您的代码addPersona
不正确。
你需要 p_pers = new Persona;
和p_pers->next_persona = p_head;
p_head = p_pers;
也在while
环内否则,您将始终填充相同的角色对象。
一个有效的addPersona()
函数是(没有cout调用):
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;
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;
p_pers->next_persona = p_head;
p_head = p_pers;
}
return p_pers;
}
要删除,您可以使用delete p_pers;
p_pers指向要删除的角色。但请注意,您需要事先调整周围的指针 ( p_pers_previous->next = p_pers_previous->next->next;
)。
您只是将最终角色插入到列表中。只有一个更改可以解决问题。
在前面的情况下,您创建的节点被您稍后添加的节点覆盖。通过在创建新的 Persona 对象的信息之前将每个 Persona 对象添加到列表中,我们不会覆盖早期版本。
代码的更新版本如下:
#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;
}
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 循环浏览链表
- 正在浏览链表
- 如何循环浏览链表以减少值