实现模板化的单向链表时遇到问题
Trouble implementing a templated singly linked list
我正在尝试实现一个模板化的单链表,我对C++相当陌生
#include <iostream>
#include <string>
#define NEWL "n"
#define PRINT(s) std::cout << s
#define PRINTL(s) std::cout << s << NEWL
#define PRINTERR(e) std::cerr << e << NEWL
////// Class for a Node
template<class Data> class Node {
Node<Data>* next_ptr;
Data data;
public:
Node(Node<Data>* nxt_ptr) :next_ptr(nxt_ptr) {};
Node(Data d, Node<Data>* nxt_ptr) :data(d), next_ptr(nxt_ptr) {};
Node<Data>* get_next() { return next_ptr; }
Data& get_data() { return data; }
friend std::ostream& operator<<(std::ostream& out, const Node<Data>& node) {
out << node.data;
return out;
};
};
////// Class for a SinglyLinkedList
template<class Data> class SLinkedList {
Node<Data>* head_ptr;
int max_size;
public:
SLinkedList() : head_ptr(nullptr) {};
bool is_empty() {
return head_ptr == nullptr;
};
bool is_full() {
return get_size() == max_size;
};
int get_size() {
if (is_empty()) {
return 0;
}
int count = 0;
for (Node<Data>* it_ptr = head_ptr; it_ptr != nullptr; it_ptr = it_ptr->get_next()) {
count++;
}
return count;
};
void add(Data d) {
if (is_full()) {
throw std::exception("List is full!");
}
Node<Data> new_node(d, head_ptr);
head_ptr = &new_node;
};
void print_content() {
int count = 1;
PRINTL("This list contains:");
for (Node<Data>* it_ptr = head_ptr; it_ptr != nullptr; it_ptr = it_ptr->get_next()) {
PRINTL("t["<< count << "]" << " at " << it_ptr << " : " << *it_ptr);
count++;
}
}
};
////// Main function
int main()
{
SLinkedList<int> sll;
sll.add(42);
sll.print_content();
}
我无法让这个工作。以某种方式使用 for 循环迭代列表不起作用。它总是导致关于指向0xCCCCCCD0
的指针的读取访问冲突异常,我不知道如何解决这个问题。
您的add
函数不正确
Node<Data> new_node(d, head_ptr);
在 add
中创建新函数 local Node
。 然后,将head
设置为该局部变量的地址。 当函数结束时,所有局部变量都将被销毁,因此现在head
指向不再存在的对象。
要解决此问题,您需要使用 new
关键字创建一个动态对象,该对象将在函数结束后继续存在。
Node<Data>* new_node = new Node(d, head_ptr);
head_ptr = new_node;
这样做的缺点是您需要记住在列表析构函数中创建的所有节点上调用delete
。
代码中还有一些其他错误。 您从未在构造函数中设置max_size
因此除了给它一个值之外使用它根本不是未定义的行为,因为我们不知道它的值是什么。 在将节点添加到列表中时,也绝不会增加列表的大小。
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 我在双链表上的此打印功能时遇到问题
- 我在显示链表时遇到问题
- 我是C++新手。我在此代码中遇到错误,无法使用 SwapNodes 函数C++创建链表。该函数不交换节点
- 将项目添加到双向链表的背面时遇到问题
- 更新链表时遇到问题
- 实现模板化的单向链表时遇到问题
- 添加到双向链表的背面时遇到问题
- 在模板中创建复制链表的函数时遇到问题
- 删除动态链表时遇到麻烦
- 创建指向另一个类中对象的链表时遇到问题