C++链接列表头返回 NULL,即使列表不为空
C++ linkedList head returns NULL even though list is not empty
我正在尝试在C++中实现链表的pop函数。我的节点和链表类如下所示:
//Node class
template <class T>
class Node{
public:
T data;
Node<T> *next;
Node(T data, Node<T> *next);
~Node();
};
template <class T>
Node<T>::Node(T data, Node<T> *next){
this->next = next;
this->data = data;
}
template <class T>
Node<T>::~Node(){
delete this;
}
//LinkedList class
template <class T>
class LinkedList{
public:
//fields
Node<T> *head;
int size;
//methods
LinkedList();
void push(T data);
T pop();
void printList();
};
template <class T>
LinkedList<T>::LinkedList(){
this->head = NULL;
this->size = 0;
}
template <class T>
void LinkedList<T>::printList(){
int i = 1;
while(head){
std::cout<<i<<": "<<head->data<<std::endl;
head = head->next;
i++;
}
}
int main(){
LinkedList<int> list;
for (int i = 1; i < 6; ++i)
list.push(i);
list.printList();
for (int j = 0; j < 3; ++j){
int output=list.pop();
printf("popped: %dn", output);
}
list.printList();
return 0;
}
下面是我的弹出函数。问题是这个>头返回 NULL。因此我无法更改其值或访问其数据字段。我使用 print 语句来发现这个>头返回 NULL。如何解决此问题?
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (this->size==0){
return 0;
}
//if list has only one node
if (this->size==1){
T ret = h->data;
this->head = NULL;
this->size --;
return ret;
}
//if list has multiple nodes
else{
T ret = this->head->data;
this -> head = h->next;
return ret;
}
h.~Node<T>();
}
下面如果我的推送功能。我已经测试了这个函数,它工作正常,但如果它没有正确处理节点指针,请告诉我。
template <class T>
void LinkedList<T>::push(T data){
Node<T> *n = new Node<T>(data, this->head);
this->head = n;
this->size++;
}
template <class T>
Node<T>::~Node(){
delete this;
}
这是非常非常错误的。你需要完全摆脱它。
更重要的是,printList()
正在修改head
不应该修改的时候。这就是为什么调用head
时pop()
为 NULL 的原因。使用局部Node*
变量迭代列表:
template <class T>
void LinkedList<T>::printList(){
int i = 1;
Node<T> *n = head; // <-- here
while(n){
std::cout << i << ": " << n->data << std::endl;
n = n->next;
i++;
}
}
此外,pop()
没有正确释放节点(如果有的话(,并且并不总是减少size
。 它应该看起来更像这样:
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (this->size==0){
return T(); // <-- not 0! or throw an exception instead...
}
//if list has only one node
if (this->size==1){
T ret = h->data;
this->head = NULL;
this->size--;
delete h; // <-- add this!
return ret;
}
//if list has multiple nodes
T ret = this->head->data;
this->head = h->next;
this->size--; // <-- add this!
delete h; // <-- not h.~Node<T>()!
return ret; // <-- moved down here!
}
或者更简单,这个(无需单独处理size==1
情况(:
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (!h){
return T();
}
//if list has any nodes
T ret = h->data;
this->head = h->next;
this->size--;
delete h;
return ret;
}
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 无法在我的链接列表中插入项目,head始终为NULL
- 有条件地将初始值设定项列表中的shared_ptr设为 null
- 将结构元素地址设置为C 中的null(单链接列表)
- C++链接列表头返回 NULL,即使列表不为空
- 链接列表打印null -C
- 当std::string是参数时,如何禁止在参数列表中传递NULL
- 将以null结尾的字符串列表从外部函数返回到.NET中
- 使用构造函数初始化指向双击列表中NULL的指针
- 单链表,为什么列表是空的(head == NULL)