C双向循环链表

     搞开发也有些时间了,想记下些实用的东西,总是一拖在拖,一是每次都懒的不行,二是举的这东西难以上的了厅堂!最近闲的DT,还是整理一下吧!都是C开发非常常用的东西!

     链表这东西应该是C

     这个链表是仿linux内核做的,只不过简化了很多,只保留了最基本的一些操作!以为是仿,所以难免有疏漏!所以各位高人看到有什么错误或者不合理!就尽管板砖吧!(前提是你能够指出代码的谬误之处)。语法命名什么的也可以!

     svLst.h就是链表了,需要链表的地方包含这个就可以了!

#ifndef __SVLST_H__ 
#define __SVLST_H__ 
#include <stddef.h> 
 
/* 
 *double linked node; 
 *You must put the struct varable into your structure, 
 *regardless whar position it is; 
 *you can use 'CONTAIN_OFF' to find your struct varable's ptr 
 */ 
typedef struct svLstNode{ 
    struct svLstNode* pPre; 
    struct svLstNode* pNext;     
}svLstNode; 
 
/* 
 *Declare a svLstNode and init it; 
 * 
 *YOU MUST DECLARE a head node before you create a link! 
 */ 
 
#define SVLST_MAKE_HEAD(name)  svLstNode (name) = {&name, &name} 
 
/* 
 *Init a svLstNode ; 
 * 
 *YOU MUST Init a head node before you use it! 
 */ 
 
#define SVLST_INIT_HEAD(name)  \ 
    do{(name).pPre = &name; (name).pNext = &name;}while(0) 
 
 
/* 
 *walk each node in the list; 
 * 
 *You can't del the node you get, because it is not save; 
 *if you want to del the node, use 'SVLST_NEXT_EACH_SAVE' 
 *instead; 
 */ 
#define SVLST_NEXT_EACH(each, head)  \ 
    for((each) = (head)->pNext; (each) != head; (each) = (each)->pNext) 
 
/* 
 *walk each node in the list; 
 * 
 *You can del the node you get, because it is save; 
 * The 'save' is used to save the current node. 
 */ 
 
#define SVLST_NEXT_EACH_SAVE(each, save, head)  \ 
    for((each) = (head)->pNext, (save) = (each)->pNext; (each) != head; (each) = (save), (save) = (each)->pNext) 
 
 
/* 
 *Get your struct pointor, according to your svLstNode* value; 
 * 
 * 
 */ 
#define CONTAIN_OFF(structType, member, ptr)  \ 
    (structType*)((int)(ptr) - (int)(&(((structType*)0)->member))) 
 
 
static inline int SVLST_INSERT(svLstNode* phead, svLstNode* pNode) 
{ 
    if((NULL == phead) || (NULL == pNode)) 
        return -1; 
    pNode->pPre = phead; 
    pNode->pNext = phead->pNext; 
    phead->pNext = pNode; 
    pNode->pNext->pPre = pNode; 
    return 0; 
} 
 
static inline int SVLST_INSERT_TAIL(svLstNode* phead, svLstNode* pNode) 
{ 
    if((NULL == phead) || (NULL == pNode)) 
        return -1; 
    pNode->pNext = phead; 
    pNode->pPre = phead->pPre; 
    phead->pPre = pNode; 
    pNode->pPre->pNext = pNode; 
    return 0; 
} 
 
static inline int SVLST_DEL(svLstNode* phead, svLstNode* pDel) 
{ 
    if(NULL == pDel) 
        return -1; 
    if(phead->pNext == phead) 
        return -1; 
    if(pDel->pPre) 
        pDel->pPre->pNext = pDel->pNext; 
    if(pDel->pNext) 
        pDel->pNext->pPre = pDel->pPre; 
    pDel->pPre = NULL; 
    pDel->pNext = NULL; 
    return 0; 
} 
 
#endif

     代码都相对简单,就不在解释了,中间也有些注释。

     main.c是一个使用链表的demo

#include <stdio.h>
#include <stdlib.h> 
#include "svLst.h" 
typedef struct memb 
{ 
    svLstNode mNode; 
    int num; 
    int count; 
}memb; 
 
memb mylst; 
 
int main(int argc, char *argv[]) 
{ 
    int i = 0; 
    memb* pMemb = NULL; 
    svLstNode * pNode = NULL, * pSave = NULL; 
    srand(time(NULL)); 
 
    SVLST_INIT_HEAD(mylst.mNode); 
    for(i = 0; i < 10; i++) 
    { 
        pMemb = (memb *)malloc(sizeof(memb)); 
        pMemb->num = rand() % 4; 
        pMemb->count = i; 
//        SVLST_INSERT(&mylst.mNode, &pMemb->mNode); 
        SVLST_INSERT_TAIL(&mylst.mNode, &pMemb->mNode); 
    } 
 
    SVLST_NEXT_EACH(pNode, &mylst.mNode) 
    { 
        pMemb = CONTAIN_OFF(memb, mNode, pNode); 
        printf("before %d: %d\n", pMemb->count, pMemb->num); 
    } 
 
    SVLST_NEXT_EACH_SAVE(pNode, pSave, &mylst.mNode) 
    { 
        pMemb = CONTAIN_OFF(memb, mNode, pNode); 
        if(pMemb->num == 3) 
        { 
            printf("delete %d: %d\n", pMemb->count, pMemb->num); 
            SVLST_DEL(&mylst.mNode, &pMemb->mNode); 
            free(pMemb); 
            pMemb = NULL; 
        } 
    } 
 
    SVLST_NEXT_EACH(pNode, &mylst.mNode) 
    { 
        pMemb = CONTAIN_OFF(memb, mNode, pNode); 
        printf("after %d: %d\n", pMemb->count, pMemb->num); 
    } 
 
    return 0; 
}

    话说这OSC的C语法没有高亮吗?

    PS:转的注明出处吧!http://my.oschina.net/acikee/blog/198478

转载于:https://my.oschina.net/acikee/blog/198478

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值