Data Structures and Algorithms (seven) - doubly linked list

Doubly linked list

How to seek immediate predecessor the list p nodes, how to time their performance?

From the current loop through node p immediate predecessor node is determined, the time complexity is O (n).

By p-> next successor node can directly access the node p, the time complexity is O (1).

How to quickly get p node precursor nodes?

Add a pointer points to the predecessor node by node domain.

Doubly linked list: each node in the linked list in a single setting a pointer field, pointing to its precursor nodes.

data: data storage elements; next: the storage node successor node address; prior: predecessor node storing the node address.

Doubly linked list node structure is defined:

typedef strcut DuLNode{
    ElemType data;            //数据域
    struct PuLNode *prior;    //指向前驱结点的指针
    struct PuLNode *next;     //指向后继结点的指针
}DuLNode, *DuLinkList;

Doubly linked list insertion:

S predecessor node pointer pointing to the node p, p original successor node pointers redirected s precursor pointer field, a pointer directed to the subsequent original domain assigned to the successor of s.

s-> prior = p; s-> next-> prior = s; // s-> next original is a [i] node, prior pointer field a [i] of the node junction point s     

s-> next = p-> next; // pointer field reassignment

Doubly linked list insertion: the insertion position of the i-th node e

bool ListInsert_DuL(DuLinkList &L , int i, ElemType e){
    if(!(p = GetElem_DuL(L,i))))     //查找第i个元素的结点吗,操作和单链表一样
        return false;
    s = (List)malloc(sizeof(DuLNode));      //分配新节点s内存空间
    s->data = e;
    s->next = p->next;
    p->next->prior  = s;
    p->next = s;
    return true; 
}

Deletion of the i-node:

Demo:

bool ListDelete_Dul(DuLinkList &L, int i, ElemType e){
    p->prior->next = p->next;    //第一步
    p->next->prior = p->prior;   //第二步
    free(p);        //释放p结点的内存空间
    return true;
}

Although higher operational efficiency of the doubly linked list, each node requires a pointer field occupies an extra space complexity doubled.

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/104909696