Bla*_*way 3 c c++ linked-list data-structures doubly-linked-list
最近,我看到了这个:
struct node {
node* pNext;
node** pPrevNext;
};
void insert_before(node** pNext, node* toInsert) {
toInsert->pNext = *pNext;
toInsert->pPrevNext = pNext;
if (*pNext) (*pNext)->pPrevNext = &toInsert->pNext;
*pNext = toInsert;
};
// node *a, *b;
// insert_before(a->pPrevNext, b);
Run Code Online (Sandbox Code Playgroud)
它看起来像一个单链表,但包含指向前一个节点的下一个指针的指针.我的问题很简单:这叫什么?如果没有"真实姓名",搜索有关此数据结构的信息在StackOverflow和整个互联网上都会显示为空.
请注意,它不是双向链表,如下所示:
struct node {
node* pNext;
node* pPrev;
};
Run Code Online (Sandbox Code Playgroud)
它被称为双链表,因为它有两个指针.您可以从像container_of(*node.pPrevNext,node,pNext)这样的宏中获取前一个节点,因此它在逻辑上也等同于标准的双向链表.