将 .dat 文件中的名称读入链表

Reading names from a .dat file into a linked list

我在将此文件中的数据存储和打印到我的链表中时遇到了一些问题。

文件信息:

Brown Aniya
Canaday Jordan
Germano Thomas
Grant Kiara
Gunathilaka Piyumi
Johnson Demetria
McGill Jayla
Mitchell Ty-Jaa'
Robertson Victoria
Taylor Chloe
Thomas Amon
Watkins Malkym
Wyatt Arkasia

这是当前输出:

Brown Aniya

--------------------------------
Process exited after 0.1335 seconds with return value 0
Press any key to continue . . . 

主要:

#include <iostream>
#include <fstream>
#include "P4-linkedList_Cox.h"

using namespace std; 

int main()
{
    tests oh;

    oh.getNames();
    oh.print();




   return 0;
}

实施:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "P4-linkedList_Cox.h"

using namespace std;

void tests::getNames()
{
    ifstream inData;
    inData.open("infile.dat");

    nameType *newNode, *nodePtr;

   newNode = new nameType;
   newNode->link= NULL; 
   string um;
        getline(inData, um);
        newNode->info = um;


   if (head != NULL)
   {
      nodePtr = head;

      while (nodePtr->link)
      {
         nodePtr = nodePtr->link;               
      }
     nodePtr->link = newNode;       
   }
   else 
    head = newNode;
}

void tests::print()
{
   nameType *nodePtr;
   nodePtr = head;
   while (nodePtr != NULL)
   {
      cout << nodePtr->info << endl;
      nodePtr = nodePtr->link;
   }


}

tests::tests()
{
    head = NULL;

}

和.h:


using namespace std;

class tests
{
    public:
        void getNames();
        void print();
        tests();


    private:
    struct nameType
    {
        string info;
        nameType *link;

    };

    nameType *head;
};      

链接列表对我来说仍然很混乱,所以欢迎任何帮助!我还需要在 post 中多说几句,不过我想我已经很清楚地解释了我的问题。我只需要帮助将 .dat 文件中的所有名称存储到我的链接列表中,然后输出该列表。

您似乎在 getNames() 方法中创建了一个节点。对于每次插入,您都需要创建一个新节点,填充该节点,然后将其添加到链表中,这将需要在自己的循环中(您当前的循环只是查找链接的结尾列表)。另外,因为您将链表的头部初始化为 NULL,所以您在执行测试 if(head != NULL) 时永远不会进入循环开始。这可以通过类似的方式完成:

while(inputFile >> firstName >> lastName) { //reiterate while there is more information to read in
    Person tempPerson(firstName, lastName); //create a new person -- I'm assuming this class exists here with an appropriate constructor
    Node *tempNode = new Node(tempPerson, nullptr); //create a new node on the heap with this person and no next node

    if(head != nullptr) {
        Node* prevNodePtr = nullptr, currNodePtr = head; //start with the head of the list
        while(currNodePtr != nullptr) { //find the end of the list -- currNodePtr will be nullptr when at the end
            prevNodePtr = currNodePtr;
            currNodePtr = currNodePtr->getNextPtr(); //shift both pointers over once
        }
        prevNodePtr->setNext(tempNode); //have the current end of the list point to our new node, making our new node the new end of the list
    }
    else { //head is nullptr, therefore there is not currently a list
        head = tempNode; //change the head to point to this new node
    }
}

补充说明:您要确保在使用完输入文件后将其关闭!

非常感谢您的帮助,但是,我设法通过让用户输入学生人数然后将其传递给函数以创建循环来获得解决方案。再次感谢您的建议!

固定实施:

void tests::getNames(int students)
{
    ifstream inData;
    inData.open("infile.dat");  

    for(int i = 0; i < students; i++)
    {   
        nameType *newName, *namePtr;
        string um;
        newName= new nameType;
        newName->link= NULL;        
        getline(inData, um);
        newName->info = um;

   if (!head)
      head = newName;
   else  
   {
      namePtr = head;

      while (namePtr->link)
         namePtr = namePtr->link;

      namePtr->link = newName;
   }    

    }

}

输出:

Enter the number of students: 13
Brown Aniya
Canaday Jordan
Germano Thomas
Grant Kiara
Gunathilaka Piyumi
Johnson Demetria
McGill Jayla
Mitchell Ty-Jaa'
Robertson Victoria
Taylor Chloe
Thomas Amon
Watkins Malkym
Wyatt Arkasia

--------------------------------
Process exited after 3.865 seconds with return value 0
Press any key to continue . . .