Search code examples
c++cdata-structureslinked-listdoubly-linked-list

What is the proper name of a singly linked list containing a double pointer?


Recently, I saw this:

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);

It looks like a singly linked list, but containing a pointer to the previous node's next pointer. My question is simply: what is this called? Without its "true name", searches for information about this data structure turn up empty on StackOverflow and the internet at large.

Note that it's not a doubly linked list, which would look like this:

struct node {
    node* pNext;
    node* pPrev;
};

Solution

  • It's called a doubly linked list because it has two pointers. You can get the previous node from a macro like container_of(*node.pPrevNext, node, pNext) so it's logically equivalent to a standard doubly-linked list also.

    Note: The interesting question is whether an XOR list is considered singly-linked or doubly-linked? See XOR Doubly Linked List