C语言双向循环链表api(源自gluster源码)

C语言双向循环链表api(源自gluster源码)
基本的操作如增加、删除和遍历等


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*定义表头*/
struct list_head {
    struct list_head *next;
    struct list_head *prev;
};

/*表头初始化*/
#define INIT_LIST_HEAD(head) do {                   (head)->next = (head)->prev = head;     } while (0)

/*增加*/
static inline void
list_add (struct list_head *new, struct list_head *head)
{
    new->prev = head;
    new->next = head->next;

    new->prev->next = new;
    new->next->prev = new;
}

/*删除*/
static inline void
list_del (struct list_head *old)
{
    old->prev->next = old->next;
    old->next->prev = old->prev;

    old->next = (void *)0xbabebabe;
    old->prev = (void *)0xcafecafe;
}

/*判断链表是否为空*/
static inline int
list_empty (struct list_head *head)
{
    return (head->next == head);
}

#define list_entry(ptr, type, member)                       ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

#define list_for_each(pos, head)                                            for (pos = (head)->next; pos != (head); pos = pos->next)

/*遍历,关于list_for_each_entry,百度*/
#define list_for_each_entry(pos, head, member)                  for (pos = list_entry((head)->next, typeof(*pos), member);           &pos->member != (head);                             pos = list_entry(pos->member.next, typeof(*pos), member))

/*例:先定义一个结构体*/
typedef struct student
{
    int num;        //学号
    int score;      //分数
    char name[20];
    struct list_head student_list;
}STU;

STU *head=NULL;

int main(void)
{
    STU *new=NULL;

    new = (STU *)malloc(sizeof(STU));
    bzero(new, sizeof(STU));
    new->num = 8;
    new->score = 8;
    strcpy(new->name, "8888");
    /*链表初始化*/
    if(head == NULL)
    {
        // head = (STU *)malloc(sizeof(STU));
        // bzero(head, sizeof(STU));
        head = new;
        INIT_LIST_HEAD(&head->student_list);
    }
    else
    {
        list_add(&new->student_list, &head->student_list);
    }

    if(list_empty(&head->student_list))
    {
        printf("list only head\n");
        printf("head->num:%d, score:%d, name:%s\n", head->num, head->score, head->name);
    }
    /*将新成员插入链表*/
    int i;
    for(i=0;i<5;i++)
    {
        new = (STU *)malloc(sizeof(STU));
        bzero(new, sizeof(STU));
        new->num = i;
        new->score = i+1;
        strcpy(new->name, "2233");
        list_add(&new->student_list, &head->student_list);
    }
    /*删除指定的成员*/
    STU *pos=NULL;
    list_for_each_entry(pos, &head->student_list, student_list)
    {
        if(pos->num == 2){
            list_del(&pos->student_list);
            free(pos);
            pos = NULL;
            break;
            // printf("num:%d, score:%d, name:%s\n", pos->num, pos->score, pos->name);
        }
    }
    /*遍历链表*/
    list_for_each_entry(pos, &head->student_list, student_list)
    {
        printf("num:%d, score:%d, name:%s\n", pos->num, pos->score, pos->name);
    }
    return 0;
}

原文地址:http://blog.51cto.com/13603157/2285806

时间: 2025-01-07 02:42:49

C语言双向循环链表api(源自gluster源码)的相关文章

在云平台上基于Go语言+Google图表API提供二维码生成应用

二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下基于Go语言+Google图表API提供二维码生成功能的小应用,并演示怎样把它公布到云平台上, 让每一个人都能够通过网络訪问使用它. Google图表API Google在http://chart.apis.google.com 上提供了一个将表单数据自己主动转换为图表的服务. 只是,该服务非常难交

c语言双向循环链表

双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.而循环链表之前也有说过,单链表中就是让最后一个结点的指针指向第一个结点就能构成一个循环链表,这里其实也是一样的,只不过多了一步,让第一个结点的前驱指向最后一个结点就行了,(这里介绍的是带头结点的双向循环链表,所以用第一个结点和头结点来区分两者).下面直接看看怎么创建一个带头结点的双向循环链表吧

C语言双向循环链表实现及图示(初始化/插入链表/清空/销毁)

-------------------------------------------- 双向循环链表 //遍历等执行方法与普通双向链表相同,不单独列举 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 初始化+尾插法 图示: 实现代码 1 /* 初始化

国际C语言混乱代码大赛所有得奖源码 1984----研究分析

anonymous.c 源码: int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);} 这样的代码看着很眼晕,咱们用编缉器将它格式化后的代码如下: int i; main() { for(; i["]<i;++i){--i;}"]; read('-'

python语言线程标准库threading.local源码解读

本段源码可以学习的地方: 1. 考虑到效率问题,可以通过上下文的机制,在属性被访问的时候临时构建: 2. 可以重写一些魔术方法,比如 __new__ 方法,在调用 object.__new__(cls) 前后进行属性的一些小设置: 3. 在本库中使用的重写魔术方法,上下文这两种基础之上,我们可以想到函数装饰器,类装饰器,异常捕获,以及两种上下文的结构: 灵活运用这些手法,可以让我们在代码架构上更上一层,能够更加省时省力. 1 from weakref import ref # ref用在了构造大

用GO语言开发editplus编辑器插件(附源码)

我要开发的插件功能极为简单,就是对用户选中的内容进行base64编码或解密工作. 其中所涉及的技术部分主要是GO语言程序开发和editplus插件配置的部分,首先我们来看一下GO语言代码的写法,如下: package main import ("encoding/base64""fmt""os") const (version string = "1.0") func main() {num := len(os.Args)if

.Net中使用SendGrid Web Api发送邮件(附源码)

SendGrid是一个第三方的解决邮件发送服务的提供商,在国外使用的比较普遍.国内类似的服务是SendCloud.SendGrid提供的发送邮件方式主要是两种, 一种是SMTP API, 一种是Web Api. SMTP API是一种比较简单的方式,只要我们准备好Mail Message, 直接发送到SendGrid的邮件服务器就可以了,SendGrid的邮件服务器会帮我们投递.另外一种是Web Api的方式. 一般来说,很多三方的服务器提供商都会禁止链接外部25端口,这样你就没有办法连接Sen

易语言修改外部任意窗口标题图标源码

可以修改外部窗口的标题,和图标. 一般是用来修改作者信息的,比如把作者改成自己之类的. 下载地址:https://6yunpan.pipipan.com/fs/17009107-375031406 原文地址:https://www.cnblogs.com/xiazai/p/10886636.html

Python源码剖析笔记0 ——C语言基础

python源码剖析笔记0--C语言基础回顾 要分析python源码,C语言的基础不能少,特别是指针和结构体等知识.这篇文章先回顾C语言基础,方便后续代码的阅读. 1 关于ELF文件 linux中的C编译得到的目标文件和可执行文件都是ELF格式的,可执行文件中以segment来划分,目标文件中,我们是以section划分.一个segment包含一个或多个section,通过readelf命令可以看到完整的section和segment信息.看一个栗子: char pear[40]; static