有序链表 -> 参数更换为 addr + reg

 

有序链表的参数更换为 addr + reg

 

// 有序双链表 -> addr + reg
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef enum{
    FALSE = 0,
    TRUE = 1,
}STATUS;

typedef struct _DOUBLE_LINK_NODE
{
    int addr;
    int reg;
    struct _DOUBLE_LINK_NODE* prev;
    struct _DOUBLE_LINK_NODE* next;
}DOUBLE_LINK_NODE;

//1. 删除双向链表
void delete_all_double_link_node(DOUBLE_LINK_NODE** pDLinkNode);
//2. 双向链表中插入地址+寄存器值
STATUS insert_addr_into_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int value_addr, int value_reg);
//3. 双向链表中删除地址
STATUS delete_addr_from_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int value_addr);
//4. 统计双向链表中地址的个数
int count_number_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode);
//5. 打印双向链表中地址
void print_double_link_node(const DOUBLE_LINK_NODE* pDLinkNode);

DOUBLE_LINK_NODE* mb_poll_link;
int main()
{
    insert_addr_into_double_link(&mb_poll_link, 88, 0);
    print_double_link_node(mb_poll_link);
    insert_addr_into_double_link(&mb_poll_link, 22, 0);
    insert_addr_into_double_link(&mb_poll_link, 55, 0);
    insert_addr_into_double_link(&mb_poll_link, 11, 0);
    insert_addr_into_double_link(&mb_poll_link, 44, 0);
    insert_addr_into_double_link(&mb_poll_link, 33, 0);
    insert_addr_into_double_link(&mb_poll_link, 66, 0);
    
    delete_addr_from_double_link(&mb_poll_link, 33);
    delete_addr_from_double_link(&mb_poll_link, 44);
    
    print_double_link_node(mb_poll_link);
    printf("main end!\n");
    return 0;
}

//static //创建双向链表节点
DOUBLE_LINK_NODE* create_double_link_node(int value_addr, int value_reg) {
    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->addr = value_addr;
    pDLinkNode->reg = value_reg;
    return pDLinkNode;
}

// static //在双向链表中查找数据
DOUBLE_LINK_NODE* find_addr_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode, int addr) {
    DOUBLE_LINK_NODE* pNode = NULL;
    if (NULL == pDLinkNode)
        return NULL;

    pNode = (DOUBLE_LINK_NODE*)pDLinkNode;
    while (NULL != pNode){
        if (addr == pNode->addr)
            return pNode;
        pNode = pNode->next;
    }
    return NULL;
}

//1. 删除双向链表
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);    //调用自身函数循环free
}

//2. 双向链表中插入数据
STATUS insert_addr_into_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int value_addr, int value_reg)
{
    DOUBLE_LINK_NODE* pNode;
    DOUBLE_LINK_NODE* pIndex;
    
    if(NULL == ppDLinkNode)                                        // 原链表首地址 空        ->FALSE  
        return FALSE;
    
    if(NULL == *ppDLinkNode){                                    // 原链表 空            ->直接加入
        pNode = create_double_link_node(value_addr, value_reg);
        assert(NULL != pNode);
        *ppDLinkNode = pNode;
        (*ppDLinkNode)->prev = (*ppDLinkNode)->next = NULL;
        return TRUE;
    }
    
    if (NULL != find_addr_in_double_link(*ppDLinkNode, value_addr))    // 原链表存在数据addr    ->FALSE
        return FALSE;
    
    pNode = create_double_link_node(value_addr, value_reg);                        // 
    assert(NULL != pNode);
    
    pIndex = *ppDLinkNode;                                        // 赋值链表首地址
    #if 0        //添加的先后顺序 
    while (NULL != pIndex->next)// 找到最后一个节点
        pIndex = pIndex->next;
    
    pNode->prev = pIndex;
    pNode->next = pIndex->next;
    pIndex->next = pNode;
    #endif
    #if 1        //递增
    if(pIndex->addr>value_addr)                // 起始位置 ->OK ?
    {
        *ppDLinkNode = pNode;
        pNode->prev = NULL;
        pNode->next = pIndex;
        pIndex->prev = pNode;
    }
    else
    {
        while (NULL != pIndex->next)// 找到最后一个节点
            pIndex = pIndex->next;
        if(pIndex->addr<value_addr){            // 结束位置 ->OK ?
            pNode->prev = pIndex;
            pNode->next = pIndex->next;
            pIndex->next = pNode;
        }
        else{                            // 中间位置
            pIndex = *ppDLinkNode;//首地址向后
            while (pIndex->addr<value_addr)
                pIndex = pIndex->next;
            pNode->prev = pIndex->prev;
            pNode->next = pIndex;
            pIndex->prev->next = pNode;
            pIndex->prev = pNode;
        }
    }
    #endif
    #if 0        //递减
    if(pIndex->addr<value_addr)                // 起始位置 ->OK ?
    {
        *ppDLinkNode = pNode;
        pNode->prev = NULL;
        pNode->next = pIndex;
        pIndex->prev = pNode;
    }
    else
    {
        while (NULL != pIndex->next)// 找到最后一个节点
            pIndex = pIndex->next;
        if(pIndex->addr>value_addr){            // 结束位置 ->OK ?
            pNode->prev = pIndex;
            pNode->next = pIndex->next;
            pIndex->next = pNode;
        }
        else{                            // 中间位置
            pIndex = *ppDLinkNode;//首地址向后
            while (pIndex->addr>value_addr)
                pIndex = pIndex->next;
            pNode->prev = pIndex->prev;
            pNode->next = pIndex;
            pIndex->prev->next = pNode;
            pIndex->prev = pNode;
        }
    }
    #endif
    
    return TRUE;
}

//3. 双向链表中删除数据
STATUS delete_addr_from_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int value_addr)
{
    DOUBLE_LINK_NODE* pNode;
    if (NULL == ppDLinkNode || NULL == *ppDLinkNode)                // "原链表首地址"或“原链表” 空        ->FALSE
        return FALSE;
    
    pNode = find_addr_in_double_link(*ppDLinkNode, value_addr);
    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;                        // 下一个节点的 prev = 节点的prev
        pNode->prev->next = pNode->next;                            // 前一个节点的 next = 节点的next
    }
    
    free(pNode);
    return TRUE;
}

//4. 统计双向链表中数据的个数
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;
}

//5. 打印双向链表中数据
void print_double_link_node(const DOUBLE_LINK_NODE* pDLinkNode)
{
    DOUBLE_LINK_NODE* pNode = (DOUBLE_LINK_NODE*)pDLinkNode;
    printf("print_double_link -> start\n");
    while (NULL != pNode){
        printf("%d  %d\n", pNode->addr,pNode->reg);
        pNode = pNode->next;
    }
    printf("print_double_link -> end\n");
}

 

posted @ 2016-08-17 19:37  mrsandstorm  阅读(318)  评论(0编辑  收藏  举报