Linux链表理解

参考原文:https://www.jianshu.com/p/1c65c76440e4

参考链接:

https://my.oschina.net/u/3857782/blog/1849617/

https://blog.csdn.net/u013904227/article/details/50931540?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

 https://blog.csdn.net/wanshilun/article/details/79747710?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://www.cnblogs.com/Cqlismy/p/11359196.html

 

编写list_head.h文件,内容如下:

#ifndef _LIST_HEAD_
#define _LIST_HEAD_


struct list_head
{
    struct list_head *prev,*next;
};



#endif

编写list_head.c文件,内容如下:


#include "list_head.h"


void list_head_init(struct list_head *entry)
{
    entry->next = entry;
    entry->prev = entry;
}

void _list_add(struct list_head *new,struct list_head *prev, struct list_head *next)
{
    new->next = next;
    next->prev= new;
    new->prev= prev;
    prev->next=new;
}

void list_add(struct list_head *new,struct list_head *old)
{
    _list_add(new,old,old->next);
}

void list_add_tail(struct list_head *new,struct list_head *old)
{
    _list_add(new,old->prev,old);
}

void list_del(struct list_head *entry,struct list_head *old)
{
    entry->prev->next = entry->next;
    entry->next->prev = entry->prev;
}


接着是main.c文件,内容如下:

#include <stdio.h>
#include <string.h>
#include "list_head.h"

struct student{
    struct list_head stu_list;
    int age;
    char name[10];
};

struct list_head g_stu_entry;


int main(int argc, char const *argv[])
{
    struct student st1;
    struct student st2;
    struct student st3;

    struct student *st;

    struct list_head *pos;
   
    list_head_init(&g_stu_entry);

    st1.age = 10;
    memcpy(st1.name,"zhangsan",sizeof("zhangsan"));
    list_add_tail(&st1.stu_list,&g_stu_entry);

    st2.age = 20;
    memcpy(st2.name,"lisi",sizeof("lisi"));
    list_add_tail(&st2.stu_list,&g_stu_entry);

    st3.age = 30;
    memcpy(st3.name,"wangwu",sizeof("wangwu"));
    list_add_tail(&st3.stu_list,&g_stu_entry);


    printf("st1=0x%0x\r\n",&st1);

    for(pos=g_stu_entry.next;pos!=&g_stu_entry;pos=pos->next){
        st=(struct student *)((char *)pos-(char *)(&((struct student *)0)->stu_list));
        printf("st=0x%0x\r\n",st);
        printf("*st.age=%d\r\n",st->age);
        printf("*st.name=%s\r\n",st->name); 
    }

    printf("del some point...\r\n");
    list_del(&st2.stu_list,&g_stu_entry);


    for(pos=g_stu_entry.next;pos!=&g_stu_entry;pos=pos->next){
        st=(struct student *)((char *)pos-(char *)(&((struct student *)0)->stu_list));
        printf("st=0x%0x\r\n",st);
        printf("*st.age=%d\r\n",st->age);
        printf("*st.name=%s\r\n",st->name); 
    }
    return 0;
}

最后,执行文件:

$  gcc *.c -o list_head
$ ./list_head         
st1=0xf7b986f0
st=0xf7b986f0
*st.age=10
*st.name=zhangsan
st=0xf7b98710
*st.age=20
*st.name=lisi
st=0xf7b98730
*st.age=30
*st.name=wangwu
del some point...
st=0xf7b986f0
*st.age=10
*st.name=zhangsan
st=0xf7b98730
*st.age=30
*st.name=wangwu

具体分析,就不多说了。



作者:wit_yuan
链接:https://www.jianshu.com/p/1c65c76440e4
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted on   lh03061238  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示