别再重复造轮子了,利用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\n", iterate_item->number, iterate_item->name);

    //遍历一遍链表,注意,这个遍历中不能使用delete操作。
    list_for_each_entry(iterate_item, &link_head, link_node)
    {
        printf("number = %d, name = %s\n", 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\n", 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!\n");
    }
    return 0;
}

 

posted on 2014-06-21 17:44  eric.geoffrey  阅读(313)  评论(0编辑  收藏  举报

导航