添加新节点后,链接列表的负责人总是无效
Head of linked list always null after adding new node
这是一个家庭作业问题。我正在努力编写链接的列表数据结构。我认为我的问题在于我的添加功能。看来我没有设法正确地重新分配头指针,但是我不确定我在做什么错。
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;
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 添加新节点后,链接列表的负责人总是无效