在链表末尾插入和无限循环

Inserting at end of linked list and endless loop

所以我正在为学校做一个项目,我 运行 遇到一个问题,当我在最后插入时链表的头部正在改变,但我似乎无法识别在我的代码中这个(显然它必须在我创建的 processVector 函数中发生。

我的想法是我有一个包含患者信息的 CSV 文件。我使用 fstream 将 CSV 文件中的数据输入到患者向量中。然后我尝试将向量列表中的每个元素转换为一个节点。最后我尝试显示列表。

当我 运行 代码时,它显示每次添加新节点时头部都会发生变化,但我的想法是我的代码会在末尾而不是开头添加节点。最终发生的是当我 运行 显示列表时,它只是 运行 一个显示最后一个节点内容的无限循环。

代码如下:

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

问题是您在 processVector 中的 for 循环中每次都重复使用同一个节点。您将 head 设置为该节点,然后将其 next 字段设置为指向自身,从而生成一个循环列表。

您每次都需要创建一个新的Node。所以移动行:

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

进入for循环。