内核链表的应用

#include 
#include 
#include 
#include "dlist_public.h"

typedef struct tagLIST_S_UNIT
{
    int data;
    LIST_S_NODE list;

}LIST_S_UNIT;

typedef struct tagLIST_S_DEV
{
    int num;
    LIST_S_NODE head;
    LIST_S_UNIT *pstDevRam;

}LIST_S_DEV;

static LIST_S_DEV s_listDev;

static LIST_S_DEV *list_getDev(void)
{
    return &s_listDev;
}

static void list_init_Dev(LIST_S_DEV *pstDev)
{
    pstDev->num = 0;
    pstDev->pstDevRam = NULL;
    pstDev->head.next = &pstDev->head;
    pstDev->head.prev = &pstDev->head;
}

static void list_printList(LIST_S_NODE *pstHead)
{
    LIST_S_NODE *pstPos = NULL;
    LIST_S_UNIT *pstUnit = NULL;

    if (NULL == pstHead)    
    {   
        return;
    }   
    
    for (pstPos = pstHead->next; pstPos != pstHead; pstPos = pstPos->next)
    {   
        pstUnit = LIST_ENTRY(pstPos, LIST_S_UNIT, list);
        printf("%d\t",pstUnit->data);
    }   

    printf("\n");
}

static int list_freeList(LIST_S_NODE *pstHead)
{
    int i = 0;
    LIST_S_NODE *pstPos = NULL;
    LIST_S_DEV *pstDev = NULL;

    if (NULL == pstHead)
    {   
        return -1; 
    }   

    if (1 == LIST_isEmpty(pstHead))
    {   
        return 0;
    }   
    pstPos = pstHead;
    printf("------\n");
    pstDev = LIST_ENTRY(pstPos, LIST_S_DEV, head);
    printf("the list num=%d\n", pstDev->num);
    for (i = 0; i < pstDev->num; i++)
    {   
        LIST_del(&pstDev->pstDevRam[i].list);
    }
    pstDev->num = 0;

    printf("the list is null \n");
    return 0;
}

int main(int argc, char **argv)
{
    int i = 0;
    LIST_S_DEV *pstDev = NULL;

    pstDev = list_getDev();
    list_init_Dev(pstDev);

    if (NULL == pstDev->pstDevRam)
    {
        pstDev->pstDevRam = malloc(5 * sizeof(LIST_S_UNIT));
        if (NULL == pstDev->pstDevRam)
        {
            return -1;
        }
    }

    for (i = 0; i < 5; i++)
    {
        pstDev->pstDevRam[i].data = i;
        LIST_addTail(&pstDev->head, &(pstDev->pstDevRam[i].list));
        pstDev->num++;
    }

    list_printList(&pstDev->head);

    list_freeList(&pstDev->head);

    list_printList(&pstDev->head);

    return 0;
}

#ifndef __DLIST_PUBLIC_H__
#define __DLIST_PUBLIC_H__

#include 

#define LIST_ENTRY(ptr, type, member)   \
    ((type *)(((unsigned int)ptr) - (unsigned int )(&(((type *)0)->member)))) 

typedef struct tagLIST_S_NODE
{
    struct tagLIST_S_NODE *prev;
    struct tagLIST_S_NODE *next;

}LIST_S_NODE;

extern int LIST_initHead(LIST_S_NODE *pstHead);
extern int LIST_add(LIST_S_NODE *pstHead, LIST_S_NODE *pstNew);
extern int LIST_addTail(LIST_S_NODE *pstHead, LIST_S_NODE *pstNew);
extern int LIST_isEmpty(LIST_S_NODE *pstHead);
extern int LIST_del(LIST_S_NODE *pstOld);


#endif

#include 
#include "dlist_public.h"


int LIST_initHead(LIST_S_NODE *pstHead)
{
    if (NULL == pstHead)
    {   
        return -1; 
    }   

    pstHead->next = pstHead;
    return 0;
}

int LIST_add(LIST_S_NODE *pstHead, LIST_S_NODE *pstNew)
{
    if ((NULL == pstHead) || (NULL == pstNew))
    {   
        return -1; 
    }   
    
    pstHead->next->prev = pstNew;
    pstNew->prev = pstHead;
    pstNew->next = pstHead->next;
    pstHead->next = pstNew;

    return 0;
}

int LIST_addTail(LIST_S_NODE *pstHead, LIST_S_NODE *pstNew)
{
    if ((NULL == pstHead) || (NULL == pstNew))
    {   
        return -1; 
    }   

    pstHead->prev->next = pstNew;
    pstNew->prev = pstHead->prev;
    pstNew->next = pstHead;
    pstHead->prev = pstNew;

    return 0;
}

int LIST_isEmpty(LIST_S_NODE *pstHead)
{
    return (pstHead->next == pstHead);
}


int LIST_del(LIST_S_NODE *pstOld)
{
    LIST_S_NODE *pstPrev = NULL;
    LIST_S_NODE *pstNext = NULL;

    if (NULL == pstOld)
    {   
        return -1; 
    }   
    
    pstPrev = pstOld->prev;
    pstNext = pstOld->next;

    pstPrev->next = pstNext;
    pstNext->prev = pstPrev;
    
    return 0;
}


 
 

你可能感兴趣的:(数据结构)