所以我正在为学校做一个项目,我遇到了一个问题,当我在最后插入时链表的头部正在发生变化,但我似乎无法确定在我的代码中的位置(显然它必须发生在我创建的 processVector 函数中。
这个想法是我有一个包含患者信息的CSV文件。我使用 fstream 将 CSV 文件中的数据输入到患者的向量中。然后,我尝试将向量列表中的每个元素转换为节点。最后,我尝试显示列表。
当我运行代码时,它显示每次添加新节点时头部都会发生变化,但我的想法是我的代码会将节点添加到末尾,而不是开头。最终发生的是,当我运行 displayList 时,它只是运行一个显示最后一个节点内容的无限循环。
代码如下:
#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
循环。