私は、txtファイルからリンクされたリストを作成する小さなプログラムを作成していますが、ファイルに保存する前にリンクされたリストを操作したいと思います。印刷リンクされたリストの特定の値
しかし、私が作成したリンクリストのノードにどのようにアクセスしているのか、リストをループして特定のノード(例:姓と名のみ)だけをどのように印刷できるのか、ちょっと混乱します。
リンクリスト構造
struct EmployeeLL
{
string position;
string firstName;
string lastName;
string startDate;
float empNumber;
string deptartment;
float salary;
float hourlyRate;
float hoursPerWeek;
EmployeeLL *link;
};
リンクリストロード:リンクリスト
void insertAtHead(EmployPtr& head, string positionValue, string firstNameValue, string lastNameValue, string startDateValue, float empValue, string deptValue,
float salaryValue, float hourlyRateValue, float hoursPerWeekValue)
{
EmployPtr tempPtr = new EmployeeLL;
tempPtr->position = positionValue;
tempPtr->firstName = firstNameValue;
tempPtr->lastName = lastNameValue;
tempPtr->startDate = startDateValue;
tempPtr->empNumber = empValue;
tempPtr->deptartment = deptValue;
tempPtr->salary = salaryValue;
tempPtr->hourlyRate = hourlyRateValue;
tempPtr->hoursPerWeek = hoursPerWeekValue;
tempPtr->link = head;
head = tempPtr;
}
に読み出した値を挿入する
void loadLinkedList()
{
fstream in("payroll.txt", ios::in);
string position, first, last, date, dept;
float salary, rate, week, emp;
EmployPtr head = new EmployeeLL;
while (in >> position >> first >> last >> date >> emp >> dept >> salary >> rate >> week)
{
cout << position << ", " << first << ", " << last << ", " << date << ", " << emp << ", " << dept << ", " << salary << ", " << rate << ", " << week << endl;
insertAtHead(head, position, first, last, date, emp, dept, salary, rate, week);
}
in.close();
}
を
ここで私は混乱します。私はリンクされたリスト内の全員の名字をどのように印刷することができるかを知りたいですか?あなたが次のノードに移動する不足している
void printNames(EmployPtr& head)
{
EmployeeLL* thisNode = head;
if (head == NULL)
{
cout << "The list is empty\n";
return;
}
else
cout << "---------------------------------------------------------\n";
cout << "\tFirstName\tLastName\n";
cout << "---------------------------------------------------------\n";
do
{
cout << setw(8) << left << thisNode->firstName;
cout << setw(16) << left << thisNode->lastName;
cout << "\n\t";
} while (thisNode != NULL);
{
cout << "\n\n";
}
}
お試しに間違っていますか?それはコンパイルされますか?それは実行されますか?それはいくつかの出力を生成しますか?そして、 'std :: list'を使うだけで、[mcve] – user463035818
あなたの人生を楽にしてください。実際には、 'std :: vector'はデフォルトでなければなりません。多くの場合、それは必要なものすべてを提供します。多くの点でより優れています。キャッシュ効率の良い、ランダムアクセスなど。 –