Linux链表理解
参考原文:https://www.jianshu.com/p/1c65c76440e4
参考链接:
https://my.oschina.net/u/3857782/blog/1849617/
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 2020-03-01 21:33 lh03061238 阅读(385) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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)