简体   繁体   English

如何从文本文件加载链表?

[英]How to load a linked list from a text file?

Greetings I've been working on a homework in which I must create a linked list program which can save and restore its data from a text file. 问候我一直在做家庭作业,在其中必须创建一个链表程序,该程序可以从文本文件中保存和恢复其数据。 I have gotten most of it working but I'm having some problems with restoring the linked list using the data from the text file that was created from it. 我已经大部分工作了,但是使用从它创建的文本文件中的数据恢复链接列表时遇到了一些问题。 When I restore it the last element is repeated twice. 当我还原它时,最后一个元素重复两次。 I believe it has something to do with the loop but as I'm not sure I'm including the whole scope of my assignment. 我相信这与循环有关,但是由于不确定,我是否包括了整个作业范围。

Below is the code I've written so far of the program: 下面是我到目前为止编写的代码:

ListNode.h ListNode.h

#pragma once
template<class T> class List;
template<class T>

class ListNode
{
 friend class List<T>;
public:
 ListNode(const T&);
 T getData()const;
private:
 T data;
 ListNode<T>*next;
};//end ListNode class

template<class T>
ListNode<T>::ListNode( const T &info):data(info), next(NULL)
{
}//end default constructor
template<class T>
T ListNode<T>::getData()const
{
 return data;
}//end function getData 

List.h 清单.h

#pragma once
#include<iostream>
#include <fstream>
using namespace :: std;
#include "ListNode.h"
template<class T> 
class List 
{
private:
 ListNode<T> *head;
 int size;
 ListNode<T> *find(int index) const;
public:
 List();
 List(const List<T> &aList);
 ~List();
 int getLength() const;
 void insert (int index, T tempData);
 void remove(int index);
 void retrieve(int index, T &tempData);
 bool isEmpty() const;
 void print() const;
 void save();
 void restore();
};//end List class 

Broken function: 功能中断:

template <class T>
void List<T>::restore()
 {
     ifstream inFile;
     inFile.open("listFile.txt");

     T value=0;
     int index, check =0;

     cout << "Now reading the data from the text file..." << endl;
         //inFile >> value;
         //cout << "Value is : " << value << endl;


        while (inFile != NULL) 
        {
            inFile >> value;
            index = getLength();
            cout << value << endl;
            insert(index+1, value);

            inFile.close();

        } 


 } // end function restore

The linked list data that was saved to the text file during the test was : 测试期间保存到文本文件的链接列表数据为:

35
45
55
65

When the linked list was restored from the text file this was the content of it: 从文本文件恢复链表时,其内容如下:

35
45
55
65
65

How can I solve this problem? 我怎么解决这个问题?

It amazes me that your restore function works at all. 令我惊讶的是,您的restore功能完全有效。 But yes, it's repeating the last element. 但是,是的,它重复了最后一个元素。 Try this instead: 尝试以下方法:

while(inFile >> value)
  ...

EDIT: 编辑:
All right, just try this much: 好吧,尝试一下:

while(inFile >> value)
{
  cout << value << endl;
}

Does it display the values correctly? 是否正确显示值?

I had this problem a while ago with one of my programs. 前一段时间,我的一个程序遇到了这个问题。 It took me a while to figure out and I am not sure if it is the same problem, but check your input file one more time. 我花了一些时间才弄清楚,我不确定是否是相同的问题,但是请再检查一次您的输入文件。 If you leave an extra line after the last number, the compiler will return the last number twice. 如果您在最后一个数字之后留下多余的一行,则编译器将两次返回最后一个数字。

example: 例:

1 1个

2 2

3 3

4 4

5 5

(extra line) (特线)

Just delete the extra line. 只需删除多余的行。

Which one is the head of your list? 您名单的首位是哪一位? You can use a while loop or even an if statement to traverse through each node and load them all. 您可以使用while循环甚至if语句来遍历每个节点并全部加载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM