【问题标题】:Reading names from a .dat file into a linked list将 .dat 文件中的名称读入链表
【发布时间】:2023-03-15 02:55:02
【问题描述】:

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

文件信息:

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

链接列表对我来说仍然很困惑,所以欢迎任何帮助!我在这篇文章中还需要更多的文字,但我想我已经很清楚地解释了我的问题。我只需要帮助将 .dat 文件中的所有名称存储到我的链接列表中,然后输出该列表。

【问题讨论】:

  • 您的getNames 只读取一行,因此链表不能包含多个名称。

标签: c++ linked-list load-data-infile


【解决方案1】:

看起来您在 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
    }
}

侧边栏:你要确保在使用完输入文件后关闭它!

【讨论】:

    【解决方案2】:

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

    固定实现:

    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 . . .
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多