正在浏览链表

going through a linked list

本文关键字:链表 浏览      更新时间:2023-10-16

我是新手,在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;
}