Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 添加新节点后,链表头始终为空_C++_Linked List - Fatal编程技术网

C++ 添加新节点后,链表头始终为空

C++ 添加新节点后,链表头始终为空,c++,linked-list,C++,Linked List,这是一个家庭作业问题。我正在努力写我的链表数据结构。我想我的问题在于我的add函数。看起来我没有正确地重新分配头指针,但我不确定我做错了什么 Add(): template<class listType> void LinkedList<listType>::Add(int i, string n, listType h, listType r) { Node<listType>* newnode = new Node<listType>(

这是一个家庭作业问题。我正在努力写我的链表数据结构。我想我的问题在于我的add函数。看起来我没有正确地重新分配头指针,但我不确定我做错了什么

Add():

template<class listType>
void LinkedList<listType>::Add(int i, string n, listType h, listType r)
{
    Node<listType>* newnode = new Node<listType>(i, n, h, r);
    head = newnode;

    newnode->SetNext(head);
template<class listType>
 void LinkedList<listType>::Display()
{
    Print(head);
}

template<class listType>
void LinkedList<listType>::Print(Node<listType>* temp)
{
    if (temp == NULL)
    {
        cout << "\nEnd of list.\n";
        return;
    }
    else 
    {
        cout << temp->GetName() << "\n";
        Print(temp->GetNext());
    }
}
template <class listType>
class LinkedList {

private:
    Node<listType>* head;
public:
    LinkedList();
    ~LinkedList();
    void Add(int i, string n, listType h, listType r);
    void Display();
    void Print(Node<listType>*);
};

template<class listType>
 bool LinkedList<listType>::IsEmpty() const
{
    return head == NULL;
}

template<class listType>
 LinkedList<listType>::LinkedList()

{
    head = NULL;
}

template<class listType>
 LinkedList<listType>::~LinkedList()
{
}

template<class listType>
void LinkedList<listType>::Add(int i, string n, int h, int r)
{
    Node<listType>* newnode = new Node<listType>(i, n, h, r);
    head = newnode;

    newnode->SetNext(head);

    cout << newnode->GetId() << "\t" << newnode->GetName() << "\t";
    cout << newnode->GetHours() << "\t" << newnode->GetRate() << "\n";
}

template<class listType>
 void LinkedList<listType>::Display()
{
    Print(head);
}

template<class listType>
void LinkedList<listType>::Print(Node<listType>* temp)
{
    if (temp == NULL)
    {
        cout << "\nEnd of list.\n";
        return;
    }
    else 
    {
        cout << temp->GetName() << "\n";
        Print(temp->GetNext());
    }
}
int main()
{
    LinkedList<int> integerList;

    fstream fin("data1.txt");
    LoadData<int>(integerList, fin, "data1.txt");
    cout << "\n testing Display()\n\n";

    integerList.Display();

    return 0;
}
template<class type>
void LoadData(LinkedList<type> list, fstream& fin, string fileName)
{
    int id;
    string nameFirst, nameLast, nameFull;
    type hours, rate;

    if (fin.is_open())
    {
        cout << "Loading: " << fileName << '\n';
        while (!fin.eof())
        {
            fin >> id >> nameLast >> nameFirst >> hours >> rate;
            nameFull = nameFirst + " " + nameLast + "\b \b";
            list.Add(id, nameFull, hours, rate);

        }
        cout << fileName << " loaded succesfully.\n";
    }
    else {
        cout << "File not found, exiting.";

    }
    fin.close();
}
21169
Ahmed, Marco
40 10
24085
ATamimi, Trevone
30 15
28139
Choudhury, Jacob 
45 12

第一个问题是,您正在将列表按值从
main
传递到
LoadData
。这意味着
LoadData
函数实际上从main获取列表的副本并添加到副本中。原始列表不受影响

因此,当您调用
integerList.Display()
时,它实际上从未更改,仍然是空的

相反,您应该通过引用
LoadData
来传递列表,如下所示:

void LoadData(LinkedList<type>& list, fstream& fin, string fileName)
您要做的是将
newnode
添加到
head
之前的列表前面,然后更新
head

newnode->SetNext(head);
head = newnode;

第一个问题是,您正在将列表按值从
main
传递到
LoadData
。这意味着
LoadData
函数实际上从main获取列表的副本并添加到副本中。原始列表不受影响

因此,当您调用
integerList.Display()
时,它实际上从未更改,仍然是空的

相反,您应该通过引用
LoadData
来传递列表,如下所示:

void LoadData(LinkedList<type>& list, fstream& fin, string fileName)
您要做的是将
newnode
添加到
head
之前的列表前面,然后更新
head

newnode->SetNext(head);
head = newnode;

head=newnode;新建节点->设置下一步(头部)看起来您创建了一个长度为1的循环。@MFisherKDX也不起作用。@MFisherKDX尝试了您的建议,但仍然得到相同的结果。@0x499602D2-rogerAre sure Display()正在打印“列表结束”。调用
Add
后退出?”?它应该在一个无限循环中,因为
Add
创建了一个循环。
head=newnode;新建节点->设置下一步(头部)看起来您创建了一个长度为1的循环。@MFisherKDX也不起作用。@MFisherKDX尝试了您的建议,但仍然得到相同的结果。@0x499602D2-rogerAre sure Display()正在打印“列表结束”。调用
Add
后退出?”?它应该在一个无限循环中,因为
Add
创建了一个循环。事实上,问题是,通过引用传递错误。列表现在似乎显示正确。问题在于两者。您只是没有看到第二个问题,因为您没有在非空列表上调用
Display()
。如果您在
LoadData
内部调用
Display()
,那么它将进入一个无限递归循环。在非全面测试中未发现的bug与那些阻止一次成功运行的bug一样值得关注@MFisherKDX指出了操作代码的一个更危险的问题,一个确实存在于能够正确运行的程序中的问题。他是OP当之无愧的英雄。事实上,问题在于传递参考错误。列表现在似乎显示正确。问题在于两者。您只是没有看到第二个问题,因为您没有在非空列表上调用
Display()
。如果您在
LoadData
内部调用
Display()
,那么它将进入一个无限递归循环。在非全面测试中未发现的bug与那些阻止一次成功运行的bug一样值得关注@MFisherKDX指出了操作代码的一个更危险的问题,一个确实存在于能够正确运行的程序中的问题。他是OP应得的英雄。