惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - Articles about .NET

继续伤感 qemu1.4.1配置参数 卸载qemu 1.4.1 安装qemu 1.4.1 人生真是奇妙 可怜的国米 世界杯,无趣! 伤感 放纵 失落 博文阅读密码验证 - 博客园 穆里尼奥,你是否还应该继续留在国米? 世事难料 windows下搭建Linux开发环境 大萧条 临安两日游 欢聚时刻 变迁 人生如梦
链表实现
Articles about .NET · 2009-04-28 · via 博客园 - Articles about .NET

最近看《深入理解linux网络内幕》,发现其链表实现颇为有趣,于是兴趣盎然,按照其原理自己写了一些调试代码。前一段时间看linux源码和vxworks源码,发现自己是那么的无知,小小的一个链表,居然会有那么多实现方式,而且各具优缺点,应用场景不一。不得不称赞指针的妙处,以前一直认为指针也没什么,不就是一个内存地址吗?现在发现自己是大错特错。

不再废话,切入正题。先看看链表定义,和通常的链表定义没有区别,单链表,value+后向指针即可:

typedef struct linkedlist list;
typedef 
struct linkedlist
{
    
int value;
    list 
* next;
    
int pripority;
};

 比较有特色的是链表节点的添加和删除,先看添加函数:

void list_add(list ** head, list *node)
{
    
while(*head)
    {
        
if(node->pripority > (*head)->pripority)
        {
            
break;
        }
        head 
= &((*head)->next);
    }
    node
->next = *head;
    
*head = node;
}

为了简便,上面的代码省去了一些调试代码和一些参数检测代码;上面的代码与常用的链表实现代码区别在于list_add的第一个参数head,是个指针的指针,而不是通常意义上的第一个节点的地址,所以比较有趣;与此类似,链表的删除也是类似的:

void list_mv(list **head, list *node)
{
    
while(*head)
    {
        
if(*head != node)
        {
            head 
= &(*head)->next;
        }
        
else
        {
            
*head = node->next;
            
break;
        }
    }
}

 感兴趣的朋友可以去看看《深入理解linux网络内幕》的第4章,通知链表的注册和卸载就是按照类似的方式来实现的。