浏览链表

时间:2016-01-15 10:46:36

标签: c++ list

我是新来的C ++编程。我在链表上写了一个非常简单的程序。我动态地创建了一个struct Persona列表(用户可以决定要放入多少个节点/元素)。我创建一个指向列表开头的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;
}

所以它只打印我用户插入的最后一个元素(列表的第一个),而不是其他元素。在我想根据某些条件删除特定节点后,我是否必须使用delete(node / element)? 感谢大家都想帮助我^ ^

瓦莱里奥

2 个答案:

答案 0 :(得分:1)

addPersona中的代码不正确。

你需要  p_pers = new Persona;p_pers->next_persona = p_head; p_head = p_pers; 也在while循环内 否则你总是填写相同的Persona对象。

一个有效的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;
}

要删除,您可以使用p_pers指向的delete p_pers; 您要删除的角色。但要小心,你需要事先调整 指针(p_pers_previous->next = p_pers_previous->next->next;)。

答案 1 :(得分:0)

您只是将最终角色插入列表中。只有一个更改可以解决问题。

在前面的情况中,您创建的节点将被您稍后添加的节点覆盖。通过在创建新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;
}