LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 7deaa4a49b1d0fb2112a69719141709b9c261c7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}