添加新节点后,链接列表的负责人总是无效

Head of linked list always null after adding new node

本文关键字:列表 负责人 无效 链接 新节点 节点 添加      更新时间:2023-10-16

这是一个家庭作业问题。我正在努力编写链接的列表数据结构。我认为我的问题在于我的添加功能。看来我没有设法正确地重新分配头指针,但是我不确定我在做什么错。

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

我也尝试通过引用传递头部,但无济于事。

完整的linkedlist来源:

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()nn";
    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中获取了列表的 copy ,并添加到副本中。原始列表不受影响。

所以当您致电integerList.Display()时,它实际上永远不会更改并且仍然是空的。

相反,您应该通过参考LoadData传递列表:

void LoadData(LinkedList<type>& list, fstream& fin, string fileName)

您的下一个问题是在您的功能void LinkedList<listType>::Add(int i, string n, listType h, listType r)中。以下代码创建一个长度1。

的周期
head = newnode;
newnode->SetNext(head);

您想做的是在head之前将newnode添加到列表的正面,然后更新head

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