LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 cbff794b55c94c3c38f4017d21d78b9fa1743f2c
Add list test example code
1 files added
62 ■■■■■ changed files
booster/testcase/list.c 62 ●●●●● patch | view | raw | blame | history
booster/testcase/list.c
New file
@@ -0,0 +1,62 @@
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  list.c
 *    Description:  This is the list API from linux kernel example code.
 *
 *         Notice:  Memory leak check: valgrind --leak-check=full ./list
 *
 *        Version:  1.0.0(08/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
 *
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
typedef struct node_s
{
    int                 data;
    struct list_head    list;
} node_t;
int main (int argc, char **argv)
{
    int                 i;
    struct list_head    head;
    node_t             *node, *tmp;
    INIT_LIST_HEAD(&head);
    /* Create the link list */
    for(i=0; i<10; i++)
    {
        node = malloc(sizeof(*node));
        node->data = i;
        printf("List add an node data : %p->%d\n", node, node->data);
        list_add_tail(&node->list, &head);
    }
    printf("\n");
    /* Use list_for_each_entry to travel list, we can not remove the node in it */
    list_for_each_entry(node, &head, list)
    {
        printf("List travel node data : %p->%d\n", node, node->data);
    }
    printf("\n");
    /* Use list_for_each_entry_safe to destroy the list */
    list_for_each_entry_safe(node, tmp, &head, list)
    {
        printf("List remove node data : %p->%d\n", node, node->data);
        list_del(&node->list);
        free(node);
    }
    printf("\n");
    return 0;
}