head->next 不为空

head->next is not null

本文关键字:next gt head-      更新时间:2023-10-16

我不知道为什么我的一个项目列表"_head->next"没有设置为null(nullptr)。我会混淆我的尾巴和头部指针吗?这是针对双向链表的。大多数其他指针都可以工作,但是此错误/错误在浏览列表时会引发 seg 错误。

#pragma once
class dlist {
public:
dlist() { }
struct node {
int value;
node* next;
node* prev;
};

node* head() const { return _head; }
node* tail() const { return _tail; }

void insert(node* prev, int value){
//if empty
if(prev == nullptr)
{
//create node/prepare to insert into head ptr
_head = new node{value, _head, nullptr};
if(_tail == nullptr)
{
_tail = _head;
_tail->next = nullptr;
}
//set head
if(_head->next != nullptr)
{
_head->prev = _head;
_head->next = nullptr;
//_head->next->next = nullptr;
//above doesn't work  
}
}
//if not empty
else
{
//create and set new node
node* node1 = new node{value, prev->next, nullptr};
prev->next = node1;
//check if null, to make sure inserting after a filled "space"
if(node1->next != nullptr)
{
node1->next->prev = node1;
//set to end or tail
if(prev == _tail)
{
_tail = node1;
_tail->next = nullptr;
}
}
}
}

private:
node* _head = nullptr;
node* _tail = nullptr;
};

考虑到您发布的初始代码以及使用prev作为insert参数的歧义,这有点难以理解。

首先,insertdlist的成员函数,可以直接访问_head_tail。您不需要insertnode*参数,因为您对链表的唯一关注点是检查_head是否nullptr并为_head分配/分配value,或者您将迭代直到nullptriter->next并将分配的节点作为设置_tail添加到新分配的节点iter->next

坦率地说,您现有的大部分代码都让我挠头。此外,class的默认值是private,因此通常您只需要指定public成员。

将逻辑放在一起,您可以执行以下操作:

class dlist {
struct node {
int value;
node* next;
node* prev;
};
node* _head = nullptr;
node* _tail = nullptr;
public:
dlist() { }
node* head() const { return _head; }
node* tail() const { return _tail; }
void insert (int value)
{
node *newnode = new node {value, nullptr, nullptr};
if (_head == nullptr)
_head = newnode;
else {
node* iter = _head;
for (; iter->next; iter = iter->next) {}
newnode->prev = iter;
_tail = iter->next = newnode;
}
}
};

在类中分配内存时,为了防止像筛子一样泄漏内存,还需要声明一个析构函数,该析构函数将在类的实例超出范围时释放已分配的内存。不需要任何花哨的东西,只需从_head迭代到列表的末尾,并随时delete节点。

(注意:在保存对下一个节点的引用之前,不得delete对当前节点的引用,因此请使用单独的、恰当命名的victim节点来执行delete)

您可以执行以下操作:

~dlist() {
node* iter = _head;
while (iter) {
node* victim = iter;
iter = iter->next;
delete victim;
}
}

把它放在一起并添加几个函数来print和反转或rprint列表,你可以做如下的事情:

#include <iostream>
using namespace std;
class dlist {
struct node {
int value;
node* next;
node* prev;
};
node* _head = nullptr;
node* _tail = nullptr;
public:
dlist() { }
~dlist() {
node* iter = _head;
while (iter) {
node* victim = iter;
iter = iter->next;
delete victim;
}
}
node* head() const { return _head; }
node* tail() const { return _tail; }
void insert (int value)
{
node *newnode = new node {value, nullptr, nullptr};
if (_head == nullptr)
_head = newnode;
else {
node* iter = _head;
for (; iter->next; iter = iter->next) {}
newnode->prev = iter;
_tail = iter->next = newnode;
}
}
void print () {
for (node* iter = _head; iter; iter = iter->next)
cout << " " << iter->value;
cout << "n";
}
void rprint() {
for (node* iter = _tail; iter; iter = iter->prev)
cout << " " << iter->value;
cout << "n";
}
};
int main (void) {
dlist list;
int tmp;
while (cin >> tmp)
list.insert(tmp);
list.print();
list.rprint();
}

示例使用/输出

$ echo "2 3 4 6 8 10" | ./bin/dlist
2 3 4 6 8 10
10 8 6 4 3 2

内存使用/错误检查

在您编写的任何动态分配内存的代码中,对于分配的任何内存块,您有 2个责任:(1) 始终保留指向内存块起始地址的指针或引用,以便 (2) 当不再需要内存时可以释放它。

必须使用内存错误检查程序来确保正确使用分配的内存,并确认已释放分配的所有内存。

对于Linux来说,valgrind是正常的选择。每个平台都有类似的内存检查器。它们都易于使用,只需通过它运行您的程序即可。

$ echo "2 3 4 6 8 10" | valgrind ./bin/dlist
==18878== Memcheck, a memory error detector
==18878== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18878== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==18878== Command: ./bin/dlist
==18878==
2 3 4 6 8 10
10 8 6 4 3 2
==18878==
==18878== HEAP SUMMARY:
==18878==     in use at exit: 0 bytes in 0 blocks
==18878==   total heap usage: 9 allocs, 9 frees, 77,968 bytes allocated
==18878==
==18878== All heap blocks were freed -- no leaks are possible
==18878==
==18878== For counts of detected and suppressed errors, rerun with: -v
==18878== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

始终确认已释放已分配的所有内存,并且没有内存错误。

我建议不要再在C++中使用C样式指针C++具有智能指针,可为您完成所有内存管理。

将您的节点更改为:

class node {
node(int value) : value{value} {}
int value;
std::shared_ptr<node> next;
std::weak_ptr<node> prev;
};

一旦不再存在自身的副本/实例,shared_ptr就会删除它持有的对象(它使用一个简单的计数器)。该weak_ptr确保您没有循环引用,这意味着您的节点永远不会被删除。

同样在您的dlist类中相应地更改成员:

std::shared_ptr<node> _head;
std::weak_ptr<node> _tail;

然后将您的 getter 更改为:

std::shared_ptr<node> head() const { return _head; }
std::shared_ptr<node> tail() const { return _tail.lock(); }

weak_ptr::lock()将增加它所属的计数器shared_ptr并返回一个新的shared_ptr,然后可以访问该计数器而不会丢失对象。如果对象已被删除,它将返回 enshared_ptr

然后像这样更改插入方法:

void insert (int value)
{
std::shared_ptr<node> newnode = std::make_shared<node>(value);
if (_tail) {
newnode->prev = _tail;
_tail->next = newnode;
_tail = newnode;
}
else {
_head = newnode;
_tail = newnode;
}
}

如果列表为空,这将_head_tail设置为newnode,否则将其添加到列表的末尾。

在此处阅读有关shared_ptr和weak_ptr的更多信息。

编辑:如果你想清除你的列表,你可以简单地做_head = nullptr;。这将导致shared_ptr减少其引用计数器并删除其持有的node(如果计数器变为 0),从而删除该节点中的shared_ptr<node> next;,依此类推。 一旦解构_tail所属shared_ptr,它将自动为。为了绝对确定 dlist 类处于干净的空状态,您仍应调用_tail = nullptr;,因为代码的其他部分可以保存对任何节点的shared_ptr,从而使_tail在列表类中保持活动状态,除非您显式清除它。