head->next 不为空
head->next is not null
我不知道为什么我的一个项目列表"_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
参数的歧义,这有点难以理解。
首先,insert
是dlist
的成员函数,可以直接访问_head
和_tail
。您不需要insert
的node*
参数,因为您对链表的唯一关注点是检查_head
是否nullptr
并为_head
分配/分配value
,或者您将迭代直到nullptr
iter->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
,然后可以访问该计数器而不会丢失对象。如果对象已被删除,它将返回 en空shared_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
在列表类中保持活动状态,除非您显式清除它。
- 对于set上的循环-获取next元素迭代器
- EASTL矢量<向量<int>>连续的
- 为什么 std::next 在以空地图开头馈送时卡住
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- "fast"或"normal"在"free(): invalid next size (fast)"中是什么意思?
- curl_slist->next 永远不会为空,因此当循环卡住时
- std::next 是否检查我们是否已经在容器的末尾?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- Clang, std::next, libstdc++ and constexpr-ness
- 错误 C2864:'element::next':只能在类 (STRUCT) 中初始化静态常量整数数据成员
- 在 c++ 程序中调试链表以添加两个数字: 有人可以解释为什么 l3=l3->next;导致错误?
- C 字符串比较“祝您好运”&gt;“再见”
- head->next 不为空
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- LinkedListInit, LinkedListCreateH 方法中的"L->next=NULL"有什么问题吗?
- C++ 带有 hasNext 和 Next 的迭代器
- 是 std::next 对于向量 O(n) 或 O(1)
- 如何检查STD :: Next(x)指向C 中的有效地址
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载