zoukankan      html  css  js  c++  java
  • 别再重复造轮子了,利用list创建任意数据类型的链表

    链表用处很广,多数情况下都需要自己开发,每次都要重复相同/类似的动作,Linux内核中的list是一个非常好用的链表,而且可移植性比价高,这里介绍一下如何使用:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //VC6 模拟定义下面两行,因为这个环境不支持下面两个关键字
    #ifdef WIN32
    #define typeof(name) any_struct_s
    #define inline
    #endif
    
    #include "custome_list.h"
    
    typedef struct list_head circular_link;
    
    
    //定义任意数据类型,然后把list的struct list_head插入进此数据类型作为其中的一个成员。
    typedef struct{
        int number;
        char name[32];
        circular_link link_node;
    }any_struct_s;
    
    int main()
    {
        int i=0;
        int total = 10;
        any_struct_s* new_node = NULL;      //用于保存分配新的节点
    
        circular_link link_head;            //链表头,哨兵作用
    
        any_struct_s* iterate_item = NULL;  //用于遍历链表,取出链表中保存的数据
        any_struct_s* swap_item    = NULL;  //在删除链表时,需要一个临时变量保存数据。
    
        //初始化链表
        INIT_LIST_HEAD(&link_head);
    
        //分配10个定义的数据类型数据,然后添加到链表中
        for(i=0; i<total; i++)
        {
            new_node = malloc(sizeof(any_struct_s));
            if (new_node)
            {
                memset(new_node, 0, sizeof(any_struct_s));
                new_node->number = i;
                new_node->name[0]   = '0' + i;
    
                //将分配的节点添加到链表尾部
                list_add_tail(&new_node->link_node, &link_head);
            }
        }
    
        //取任意节点数据,比如取第一个数据。
        iterate_item = list_entry(link_head.next, any_struct_s, link_node);
        printf("the first data, number = %d, name = %s
    ", iterate_item->number, iterate_item->name);
    
        //遍历一遍链表,注意,这个遍历中不能使用delete操作。
        list_for_each_entry(iterate_item, &link_head, link_node)
        {
            printf("number = %d, name = %s
    ", iterate_item->number, iterate_item->name);
        }
    
        //遍历一遍链表,这个遍历中可以使用delete操作。
        list_for_each_entry_safe(iterate_item, swap_item, &link_head, link_node)
        {
            printf("now number = %d, name = %s
    ", iterate_item->number, iterate_item->name);
    
            //删除链表节点。
            list_del(&iterate_item->link_node);
    
            free(iterate_item);
        }
    
        //判断链表是否为空
        if (list_empty(&link_head))
        {
            printf("currently, the circular link is empty!
    ");
        }
        return 0;
    }
  • 相关阅读:
    一致性哈希的理解与实践
    nil in Go
    为什么Go没有math.Min/Max(int, int) 函数?
    What happens when I type kubectl run?
    kubelet简要分析
    编译安装nginx和模块
    nginx与tengine添加check模块(nginx_upstream_check_module)
    多台ESXI 6.5 添加 iSCSI 共享存储 --centos 7.4 作为target
    Cannot open the disk '/vmfs/volumes/5e97f429-a56d6ea0-1ef3-000c29a09445/oracle_node1/oracle_node1_1.vmdk' or one of the snapshot disks it depends on.
    RabbitMQ windows2016 镜像模式 haproxy+keepalived
  • 原文地址:https://www.cnblogs.com/eric-geoffrey/p/3801224.html
Copyright © 2011-2022 走看看