C++ two way linked list operation example of of create two way chain two way linked list to find data insert data etc

  • 2020-04-02 02:22:05
  • OfStack

Bidirectional linked list is also called double linked list, is a linked list, each of its data nodes have two Pointers, respectively pointing to the direct successor and direct precursor. So, starting from any node in the bidirectional list, you can easily access its precursor and successor nodes. In general, we construct two-way circular linked lists.

(1) define the basic structure of two-way linked list


typedef struct _DOUBLE_LINK_NODE  
{  
    int data;  
    struct _DOUBLE_LINK_NODE* prev;  
    struct _DOUBLE_LINK_NODE* next;  
}DOUBLE_LINK_NODE;  

(2) create two-way linked list nodes


DOUBLE_LINK_NODE* create_double_link_node(int value)  
{  
    DOUBLE_LINK_NODE* pDLinkNode = NULL;  
    pDLinkNode = (DOUBLE_LINK_NODE*)malloc(sizeof(DOUBLE_LINK_NODE));  
    assert(NULL != pDLinkNode);  
    memset(pDLinkNode, 0, sizeof(DOUBLE_LINK_NODE));  
    pDLinkNode->data = value;  
    return pDLinkNode;  
}  

(3) delete the two-way linked list


void delete_all_double_link_node(DOUBLE_LINK_NODE** pDLinkNode)  
{  
    DOUBLE_LINK_NODE* pNode;  
    if(NULL == *pDLinkNode)  
        return ;  
    pNode = *pDLinkNode;  
    *pDLinkNode = pNode->next;  
    free(pNode);  
    delete_all_double_link_node(pDLinkNode);  
} 

(4) find the data in the two-way linked list


DOUBLE_LINK_NODE* find_data_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode, int data)  
{  
    DOUBLE_LINK_NODE* pNode = NULL;  
    if(NULL == pDLinkNode)  
        return NULL;  
    pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
    while(NULL != pNode){  
        if(data == pNode->data)  
            return pNode;  
        pNode = pNode ->next;  
    }  
    return NULL;  
}  

(5) insert data into the two-way linked list


STATUS insert_data_into_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int data)  
{  
    DOUBLE_LINK_NODE* pNode;  
    DOUBLE_LINK_NODE* pIndex;  
    if(NULL == ppDLinkNode)  
        return FALSE;  
    if(NULL == *ppDLinkNode){  
        pNode = create_double_link_node(data);  
        assert(NULL != pNode);  
        *ppDLinkNode = pNode;  
        (*ppDLinkNode)->prev = (*ppDLinkNode)->next = NULL;  
        return TRUE;  
    }  
    if(NULL != find_data_in_double_link(*ppDLinkNode, data))  
        return FALSE;  
    pNode = create_double_link_node(data);  
    assert(NULL != pNode);  
    pIndex = *ppDLinkNode;  
    while(NULL != pIndex->next)  
        pIndex = pIndex->next;  
    pNode->prev = pIndex;  
    pNode->next = pIndex->next;  
    pIndex->next = pNode;  
    return TRUE;  
}  

(6) delete data from two-way linked list


STATUS delete_data_from_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int data)  
{  
    DOUBLE_LINK_NODE* pNode;  
    if(NULL == ppDLinkNode || NULL == *ppDLinkNode)  
        return FALSE;  
    pNode = find_data_in_double_link(*ppDLinkNode, data);  
    if(NULL == pNode)  
        return FALSE;  
    if(pNode == *ppDLinkNode){  
        if(NULL == (*ppDLinkNode)->next){  
            *ppDLinkNode = NULL;  
        }else{  
            *ppDLinkNode = pNode->next;  
            (*ppDLinkNode)->prev = NULL;  
        }  
    }else{  
        if(pNode->next)  
            pNode->next->prev = pNode->prev;  
        pNode->prev->next = pNode->next;  
    }  
    free(pNode);  
    return TRUE;  
}  

(7) count the number of data in the two-way linked list


int count_number_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode)  
{  
    int count = 0;  
    DOUBLE_LINK_NODE* pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
    while(NULL != pNode){  
        count ++;  
        pNode = pNode->next;  
    }  
    return count;  
}  

(8) print the data in the two-way linked list


void print_double_link_node(const DOUBLE_LINK_NODE* pDLinkNode)  
{  
    DOUBLE_LINK_NODE* pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
    while(NULL != pNode){  
        printf("%dn", pNode->data);  
        pNode = pNode ->next;  
    }  
}  

Today we are talking about two-way linked list is not circular, you can think about if you change to circular two-way linked list, how to write? If it is an ordered circular two-way linked list, how to write?


Related articles: