0

So I am working on a project for school and I am running into an issue where the head of the linked list is changing when I insert at the end, but I can't seem to identify where in my code this (obviously it has to be happening in the processVector function I created.

The idea is that I have a CSV file that contains the information for a patient. I use fstream to input the data from the CSV file into a vector of Patients. I then try to convert each element in the vector list into a node. Finally I try to display the lists.

When I run the code, it shows the head changing each time it adds a new node, but my thought was my code would be adding nodes to the end, not the beginning. What ultimately happens is when I run displayList, it just runs an endless loop showing the contents of the last node.

Code as follows:

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include <cassert> 

using namespace std;

const string fileName = "Patient.csv";

template <class Type>
struct Node
{
    Type info;
    Node<Type> *next;
};

template <class Type>
class LinkedList
{
    Node<Type> *head;
    int length;

public:
    LinkedList();
    LinkedList(const LinkedList&); // required by the Rule of Three
    //~LinkedList();
    LinkedList& operator=(const LinkedList&); // required by the Rule of Three
    void processVector(vector<Type>);
    void insert(const Type&);
    void remove(const Type&);
    void print(std::ostream &);
    void displayList();
    // other functions as required

private:
    /*Node<Type> *head;
    int length;*/
};

class PatientType
{
public:
    PatientType();
    PatientType(string patientSSN, string patientFName, string patientLName, string patientEmail, string patientNumber);
    string getPSSN();
    string getPFName();
    string getPLName();
    string getPEmail();
    string getPNumber();
    void setPatient(string PSSN, string PFName, string PLName, string PEmail, string PNumber);
    void loadList(vector<PatientType> &iList);

private:
    // Node<PatientType>* head; eliminated

    // private stuff that has no bearing on this example
    string pSSN;
    string pFName;
    string pLName;
    string pEmail;
    string pNumber;
};

template <class Type>
LinkedList<Type>::LinkedList()
{
    length = 0; //To keep track of the length to be O(1) otherwise it will be O(n) when we traverse throughout the whole linked list.
    head = NULL;  //The list is essentially empty
}
template <class Type>
void LinkedList<Type>::processVector(vector<Type> vecList)
{
    Node<PatientType> *newNode = new Node < PatientType >;
    int size = vecList.size();
    for (int i = 0; i < size; i++)
    {
        newNode->info = vecList[i];
        newNode->next = NULL;
        if (head == NULL) //List empty
        {
            //newNode->info = vecList[i];
            //newNode->next = NULL;
            head = newNode;
            cout << "I'm in the if statement (line 87)" << endl;
            cout << "The head is: " << head->info.getPSSN() << endl;
        }
        else
        {
            cout << "I'm in the else statement (line 92)" << endl;
            Node<PatientType> *temp = head;
            while (temp->next != NULL)
            {
                temp = temp->next; //traverse the list
            }
            temp->next = newNode;
            cout << "The head is: " << head->info.getPSSN() << endl;
        }
        length++;
        cout << "Length is "<< length << endl;
    }
}
template <class Type>
void LinkedList<Type>::displayList()
{
    Node<PatientType> *temp;
    temp = head;
    while (temp != NULL)
    {
        cout << temp->info.getPSSN() << "\t" << temp->info.getPFName() << "\t" << temp->info.getPLName() << "\t" << temp->info.getPEmail() << "\t" << temp->info.getPNumber() << endl;
        temp = temp->next;
    }
    cout << endl;
}

PatientType::PatientType()
{
    /*pSSN = "SSN";
    pFName = "First Name";
    pLName = "Last Name";
    pEmail = "Email";
    pNumber = "Phone Number";*/
}

PatientType::PatientType(string patientSSN, string patientFName, string patientLName, string patientEmail, string patientNumber)
{
    pSSN = patientSSN;
    pFName = patientFName;
    pLName = patientLName;
    pEmail = patientEmail;
    pNumber = patientNumber;
}

string PatientType::getPSSN()
{
    return pSSN;
}
string PatientType::getPFName()
{
    return pFName;
}
string PatientType::getPLName()
{
    return pLName;
}
string PatientType::getPEmail()
{
    return pEmail;
}
string PatientType::getPNumber()
{
    return pNumber;
}

void loadPatientList(vector<PatientType> &iList)
{
    ifstream infile;
    string filePSSN;
    string filePFName;
    string filePLName;
    string filePEmail;
    string filePNumber;

    infile.open(fileName);

    while (!infile.eof())
    {
        getline(infile, filePSSN, ',');
        getline(infile, filePFName, ',');
        getline(infile, filePLName, ',');
        getline(infile, filePEmail, ',');
        getline(infile, filePNumber, '\n');
        iList.push_back(PatientType(filePSSN, filePFName, filePLName, filePEmail, filePNumber));
    }
    infile.close();
}

int main()
{
    ifstream file(fileName);
    vector<PatientType> patientList;
    loadPatientList(patientList);
    LinkedList<PatientType> pList;

    pList.processVector(patientList);
    pList.displayList();


    system("pause");
    return 0;
}

1 Answers1

3

The problem is that you're reusing the same node each time through the for loop in processVector. You're setting head to that node, and then setting its next field to point to itself, resulting in a circular list.

You need to create a new Node each time. So move the line:

Node<PatientType> *newNode = new Node < PatientType >;

into the for loop.

Barmar
  • 596,455
  • 48
  • 393
  • 495