这是一个双重链接的列表吗
Is this a doubly linked list?
来自Java背景,下面是C++代码,我想从中了解DList listp;
成员在DListNode
类中的概念用法。
我不明白,如何在代码node->add ( (DListNode*) this, this ) ;
和this.tail = (DListNode*) this ;
中将DList
类型的对象转换为DListNode
类型
我的目标是将这个C++代码转换成Java。
class DList
{
private:
friend class DlistNode;
DListNode* head ;
DListNode* dummy ;
DListNode* tail ;
int size;
public:
DList ( void ): dummy( NULL), size( 0 )
{
this.head = (DlistNode*) &dummy ; //this is sentinel
this.tail = (DListNode*) this ;
}
void add_head ( DListNode* node )
{
node->add ( (DListNode*) this, this ) ;
}
void add_tail ( DListNode* node )
{
node->add ( tail, this ) ;
}
}
class DListNode
{
private:
friend class DList;
DListNode* next ;
DListNode* prev ;
DList* listp;
public:
DListNode* rem ( void )
{
if (listp)
{
next -> prev = prev ;
prev -> next = next ;
next = prev = this ;
listp->size--;
listp = NULL;
}
return ( this ) ;
}
void add ( DListNode* predecessor, DList *parent )
{
rem() ;
if (!parent)
{
listp = predecessor->listp;
}
else
{
listp = parent;
}
prev = predecessor ;
next = prev -> next ;
prev -> next = next -> prev = this ;
if ( 0 != listp )
{
listp->size++;
}
}
}
我的问题:
1)listp
加入class DListNode
的目的是什么?
2)class DList
中tail
成员的目的是什么?
1)listp是指向整个列表的指针。这允许在只给定节点指针的情况下对列表进行操作。
2) tail是指向列表中最后一项的指针。这允许一些操作(如追加)为O(1)而不是O(n)。
对于双链表来说,这不是一个有效的(或者至少不是一个很好的)实现。DList
类正在做一些DListNode
没有意识到的非常危险的事情,而DListNode
根本没有管理DList
的head
和tail
成员。
最安全的方法是只使用STL std::list
类,让它为您处理所有细节。但是,如果您必须手动实现它,请尝试更像这样的方法:
class DList
{
private:
friend class DlistNode;
DListNode* head;
DListNode* tail;
int size;
public:
DList() : head(NULL), tail(NULL), size(0)
{
}
~DList()
{
while (head)
delete head;
}
void add_head(DListNode* node)
{
node->add_head(this);
}
void add_tail(DListNode* node)
{
node->add_tail(this);
}
};
class DListNode
{
private:
friend class DList;
DListNode* next;
DListNode* prev;
DList* listp;
public:
DListNode() : next(NULL), prev(NULL), listp(NULL)
{
}
~DListNode()
{
remove();
}
void remove()
{
if (next)
next->prev = prev;
if (prev)
prev->next = next;
if (listp)
{
if (listp->head == this)
listp->head = next;
if (listp->tail == this)
listp->tail = prev;
listp->size--;
listp = NULL;
}
prev = next = NULL;
}
void add_head(DList *parent)
{
if (!parent)
throw std::runtime_error("no parent specified");
if (parent->head == this)
return;
remove();
if (parent->head)
{
parent->head->prev = this;
next = parent->head;
}
parent->head = this;
listp = parent;
listp->size++;
}
void add_tail(DList *parent)
{
if (!parent)
throw std::runtime_error("no parent specified");
if (parent->tail == this)
return;
remove();
if (parent->tail)
{
parent->tail->next = this;
prev = parent->tail;
}
parent->tail = this;
listp = parent;
listp->size++;
}
void add_before(DListNode *node)
{
if (!node)
throw std::runtime_error("no node specified");
if (node == this)
return;
remove();
if (node->prev)
node->prev->next = this;
next = node;
prev = node->prev;
node->prev = this;
listp = node->listp;
if (listp->head == node)
listp->head = this;
listp->size++;
}
void add_after(DListNode *node)
{
if (!node)
throw std::runtime_error("no node specified");
if (node == this)
return;
remove();
if (node->next)
node->next->prev = this;
prev = node;
next = node->next;
node->next = this;
listp = node->listp;
if (listp->tail == node)
listp->tail = this;
listp->size++;
}
};
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表