Chain representation of linear list — double linked list

Time:2021-9-22

1. Comparison with single linked list

Single linked list cannot be retrieved reversely
Double linked list can be forward or backward
Storage structure:
Chain representation of linear list -- double linked list

Typedef struct dnode {// defines the node type of the double linked list
    ElemType data;                  // Data domain
    struct DNode *prior, *next;     // Predecessor and successor pointers
} DNode, *DLinkList;

2. Insert node

Insert the s node after the P node, and pay attention to the execution order
Chain representation of linear list -- double linked list

/**
 *Insert s node after P node
 * @param p
 * @param s
 * @return
 */
bool InsertNextDNode(DNode *p, DNode *s) {
    If (P = = null | s = = null) {// illegal parameter
        return false;
    }
    s->next = p->next;
    If (P - > next! = null) {// if node P has a successor node
        p->next->prior = s;
    }
    s->prior = p;
    p->next = s;
    return true;
}

3. Delete node

Q node after deleting P node
Chain representation of linear list -- double linked list

/**
 *Delete P's successor node
 * @param p
 * @return
 */
bool DeleteNextNode(DNode *p) {
    if (p == NULL) {
        return false;
    }
    DNode *q = p->next;          // Find the successor node Q of P
    if (q == NULL) {
        return false;
    }
    p->next = q->next;
    If (Q - > next! = null) {// Q node is not the last node
        q->next->prior = p;
    }
    free(q);                     // Free space node
    return true;
}