A universal List module (C language) __c language

Source: Internet
Author: User

1) Change all dbg and err to printf;
2 commom_malloc to malloc;
3) common_free change to free;
4 All check write an empty macro (original as a parameter validity check ),
5) compile OK;

 #ifndef __list_h__ #define __LIST_H__ #ifdef __cplusplus #if __cplusplus extern "C" {#endif #end

if typedef void* List_handle;
List_handle list_init (int _s32nodedatasize);
int List_destroy (List_handle _hndlist);
int List_addtohead (List_handle _hndlist, void* _pdata, int _s32datasize);
int List_addtotail (List_handle _hndlist, void* _pdata, int _s32datasize);
int List_delete (List_handle _hndlist, void* pData);
int List_deletehead (List_handle _hndlist);
int List_deletetail (List_handle _hndlist);
int List_clear (List_handle _hndlist);
int list_getcnt (List_handle _hndlist);
void* List_gethead (List_handle _hndlist);
void* List_gettail (List_handle _hndlist);
void* List_getnextnode (List_handle _hndlist, void* pData);
void* List_getprevnode (List_handle _hndlist, void* pData);


int List_isempty (List_handle _hndlist); #ifdef __cplusplus #if __cplusplus} #endif #endif #endif 
#include "list.h" #include "debug.h" #include "common.h" #define List_magic_num 0xabcd0000 typedef struct LIST_NODE_S {
    void* PData;
    struct list_node_s* pstnext;
struct list_node_s* Pstprev;

}list_node_s;
    typedef struct LIST_S {unsigned int u32magicnumer; int s32cnt; node numbers of list int s32nodedatasize;
    Node size list_node_s* psthead;
List_node_s* Psttail;

}list_s;
    Static list_node_s* List_lookup (List_handle _hndlist, void* pData) {list_s* pstlist = (list_s*) _hndlist;
    list_node_s* tmp = pstlist->psthead;
        while (NULL!= tmp) {if (Tmp->pdata = = pData) {return tmp;
    } TMP = tmp->pstnext;

    ERR ("The node is not exist.\n");
return NULL;

    } list_handle list_init (int s32nodedatasize) {CHECK (s32nodedatasize > 0, NULL, "Invalid parameter.\n");
    list_s* pstlist = (list_s*) common_malloc (sizeof (list_s)); CHECK (null!= pstlist, NULL, "failed to malloc%d\n ", sizeof (list_s));
    memset (pstlist, 0, sizeof (list_s));
    Pstlist->s32nodedatasize = s32nodedatasize;
    pstlist->s32cnt = 0;
    Pstlist->psthead = NULL;
    Pstlist->psttail = NULL;

    Pstlist->u32magicnumer = List_magic_num;
Return (List_handle) pstlist;

    int List_destroy (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");
    int ret =-1;
    ret = List_clear (_hndlist);

    CHECK (!ret, ret, "Error:ret is%d\n", ret);
    ret = Common_free (_hndlist);

    CHECK (!ret, ret, "Error:ret is%d\n", ret);
return ret; int List_addtohead (List_handle _hndlist, void* pData, int datasize) {CHECK (NULL!= _hndlist,-1, invalid Paramete
    R.\n ");

    CHECK (NULL!= PData,-1, "Invalid parameter.\n");
    list_s* pstlist = (list_s*) _hndlist;

    CHECK (Pstlist->s32nodedatasize = = DataSize,-1, "Invalid parameter.\n"); list_node_s* Pstnewnode = (list_node_s*) common_malloc (sizeof (list_node_s) + Pstlist->s32nodedatAsize);

    CHECK (NULL!= Pstnewnode,-1, "Failed to malloc%d\n", sizeof (list_node_s) + pstlist->s32nodedatasize);
    memset (pstnewnode, 0, sizeof (list_node_s) + pstlist->s32nodedatasize);
    Pstnewnode->pdata = (void*) (pstnewnode+1);
    memcpy (Pstnewnode->pdata, PData, pstlist->s32nodedatasize);
        if (null = = Pstlist->psttail && NULL = = Pstlist->psthead) {pstlist->psthead = Pstnewnode;
        Pstlist->psttail = Pstnewnode;
        Pstnewnode->pstnext = NULL;
    Pstnewnode->pstprev = NULL;
        else {pstlist->psthead->pstprev = Pstnewnode;
        Pstnewnode->pstnext = pstlist->psthead;
        Pstlist->psthead = Pstnewnode;
    Pstnewnode->pstprev = NULL;

    } pstlist->s32cnt++;
return 0; int List_addtotail (List_handle _hndlist, void* pData, int datasize) {CHECK (NULL!= _hndlist,-1, invalid Paramete
    R.\n "); CHECK (NULL!= PData,-1, "Invalid parameter.\n");
    list_s* pstlist = (list_s*) _hndlist;

    CHECK (Pstlist->s32nodedatasize = = DataSize,-1, "Invalid parameter.\n");
    list_node_s* Pstnewnode = (list_node_s*) common_malloc (sizeof (list_node_s) + pstlist->s32nodedatasize);

    CHECK (NULL!= Pstnewnode,-1, "Failed to malloc%d\n", sizeof (list_node_s) + pstlist->s32nodedatasize);
    memset (pstnewnode, 0, sizeof (list_node_s) + pstlist->s32nodedatasize);
    Pstnewnode->pdata = (void*) (pstnewnode+1);
    memcpy (Pstnewnode->pdata, PData, pstlist->s32nodedatasize);
        if (null = = Pstlist->psttail && NULL = = Pstlist->psthead) {pstlist->psthead = Pstnewnode;
        Pstlist->psttail = Pstnewnode;
        Pstnewnode->pstnext = NULL;
    Pstnewnode->pstprev = NULL;
        else {pstlist->psttail->pstnext = Pstnewnode;
        Pstnewnode->pstprev = pstlist->psttail;
        Pstlist->psttail = Pstnewnode;
Pstnewnode->pstnext = NULL;    } pstlist->s32cnt++;
return 0;
    int List_delete (List_handle _hndlist, void* pData) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");

    CHECK (NULL!= PData,-1, "Invalid parameter.\n");
    int ret =-1;
    list_s* pstlist = (list_s*) _hndlist;
    list_node_s* pstfind = List_lookup (pstlist, pData);
            if (null!= pstfind) {if (pstlist->s32cnt = 1) {pstlist->psthead = null;
        Pstlist->psttail = NULL; else {if (Pstfind = pstlist->psthead) {Pstlist->psthead
                ->pstnext->pstprev = NULL;
            Pstlist->psthead = pstlist->psthead->pstnext; else if (Pstfind = = pstlist->psttail) {pstlist->psttail->pstprev->p
                Stnext = NULL;
            Pstlist->psttail = pstlist->psttail->pstprev;
             } else {   Pstfind->pstprev->pstnext = pstfind->pstnext;
            Pstfind->pstnext->pstprev = pstfind->pstprev;
        The RET = Common_free (pstfind);

        CHECK (!ret, ret, "Error:ret is%d\n", ret);
    pstlist->s32cnt--;
return ret;

    int List_deletehead (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");
    int ret =-1;
    list_s* pstlist = (list_s*) _hndlist; if (NULL!= pstlist->psthead) {if (pstlist->s32cnt = 1)//Only one node {ret = C
            Ommon_free (Pstlist->psthead);

            CHECK (!ret, ret, "Error:ret is%d\n", ret);
            Pstlist->psthead = NULL;
        Pstlist->psttail = NULL;
            else {list_node_s* TMP = pstlist->psthead;
            Pstlist->psthead->pstnext->pstprev = NULL;

            Pstlist->psthead = pstlist->psthead->pstnext; RET = Common_Free (TMP);
        CHECK (!ret, ret, "Error:ret is%d\n", ret);
        } pstlist->s32cnt--;
    DBG ("Delete head node success.list size:%d\n", pstlist->s32cnt);
return ret;

    int List_deletetail (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");
    int ret =-1;
    list_s* pstlist = (list_s*) _hndlist; if (NULL!= pstlist->psttail) {if (pstlist->s32cnt = 1)//Only one node {ret = C
            Ommon_free (Pstlist->psttail);

            CHECK (!ret, ret, "Error:ret is%d\n", ret);
            Pstlist->psthead = NULL;
        Pstlist->psttail = NULL;
            else {list_node_s* TMP = pstlist->psttail;
            Pstlist->psttail->pstprev->pstnext = NULL;

            Pstlist->psttail = pstlist->psttail->pstprev;
            RET = Common_free (TMP);
        CHECK (!ret, ret, "Error:ret is%d\n", ret); } pstlist->s32cnt--;
    DBG ("Delete tail node success.list size:%d\n", pstlist->s32cnt);
return ret;

    int List_clear (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");
    int ret =-1;
        while (list_getcnt (_hndlist) > 0) {ret = List_deletetail (_hndlist);
    CHECK (!ret, ret, "Error:ret is%d\n", ret);
return ret;

    int list_getcnt (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");

    list_s* pstlist = (list_s*) _hndlist;
Return pstlist->s32cnt;

    } void* List_gethead (List_handle _hndlist) {CHECK (null!= _hndlist, NULL, "Invalid parameter.\n");

    list_s* pstlist = (list_s*) _hndlist; Return (Pstlist->psthead)?
pstlist->psthead->pdata:null;

    } void* List_gettail (List_handle _hndlist) {CHECK (null!= _hndlist, NULL, "Invalid parameter.\n");

    list_s* pstlist = (list_s*) _hndlist; Return (Pstlist->psttail)? Pstlist->psttail->pdata:null;

    int List_isempty (List_handle _hndlist) {CHECK (NULL!= _hndlist,-1, "Invalid parameter.\n");

    list_s* pstlist = (list_s*) _hndlist;
return (pstlist->s32cnt = 0);
    } void* List_getnextnode (List_handle _hndlist, void* pData) {CHECK (null!= _hndlist, NULL, "Invalid parameter.\n");

    CHECK (null!= pData, NULL, "Invalid parameter.\n");
    list_node_s* pstfind = List_lookup (_hndlist, pData);
    if (null!= pstfind && null!= pstfind->pstnext) {return pstfind->pstnext->pdata;
return NULL;
    } void* List_getprevnode (List_handle _hndlist, void* pData) {CHECK (null!= _hndlist, NULL, "Invalid parameter.\n");

    CHECK (null!= pData, NULL, "Invalid parameter.\n");
    list_node_s* pstfind = List_lookup (_hndlist, pData);
    if (null!= pstfind && null!= pstfind->pstprev) {return pstfind->pstprev->pdata;
return NULL;




 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.