Math Lover
Math Lover

Reputation: 148

going through a linked list

I am new here and in C++ programming. I wrote a very simple program on linked lists. I create a list of struct Persona, dynamically ( user can decides how many nodes/elements to put in). I create a p_head pointer to the start of the list, as a global variable and set it to NULL. I add elements to the list with method addPersona, and then I create a method to see and print all the elements in the list by gothrough_list method. It seems to me correct but I run it and at the end I have only the last element printed (that is the first element of the list pointed by p_head.) Can anyone explain me why? The code is below:

#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;
}

So it only prints me the last element inserted by the user( first of the list) and no the others. After I want to delete a specific node based on some conditions, Do I have to use delete (node/element)? Thanks to everyone wants to help me ^ ^

Valerio

Upvotes: 1

Views: 111

Answers (2)

You are only inserting the final persona into the list. Only one change can fix the issue.

In the earlier case, the nodes that you created were getting overwritten by the ones you added later. By adding to the list every Persona object before the information for a new Persona object is created, we would not be overwriting the earlier version.

An updated version of the code is as follows:

#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;
}

Upvotes: 0

jofel
jofel

Reputation: 3405

Your code in addPersona not correct.

You need p_pers = new Persona; and p_pers->next_persona = p_head; p_head = p_pers; also inside the while loop otherwise you fill always the same Persona object.

A working addPersona() function is (without the cout calls):

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;
}

To delete, you can use delete p_pers; where p_pers points to the Persona you want to delete. But take care, you need previously adjust the pointers around (p_pers_previous->next = p_pers_previous->next->next;).

Upvotes: 1

Related Questions