LingYun IoT Studio NB-IoT research project
Guo Wenxue
2018-11-20 67f8a597480e9951ea40e84997011660d09eeb84
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue<guowenxue@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  test_klist.c
 *    Description:  This file is for test kernel space double linked list.
 *                 
 *        Version:  1.0.0(11/12/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "11/12/2012 04:26:39 PM"
 *                 
 ********************************************************************************/
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cp_klist.h"
 
typedef struct node_s 
{
    int                    data;
    struct list_head       link;
} node_t;  
 
 
 
 
void travel_list(struct list_head *head)
{
    node_t             *new_node;
    node_t             *node, *tmp;
 
 
    if( (new_node=malloc(sizeof(node_t))) )
    {
        list_add_tail(&new_node->link, head);
        printf("Add new node %p to list \n", new_node);
    }
 
    /* Use list_for_each_entry to travel the list, we can not remove the node in it */
    list_for_each_entry_safe(node, tmp, head, link)
    {
        printf("Travel2 list on node %p\n", node);
    }
}
 
 
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int                 i;
    struct list_head    header;
    node_t             *node;
    node_t             *tmp;
 
    INIT_LIST_HEAD(&header);
 
    for(i=0; i<10; i++)
    {
        node=malloc(sizeof(node_t));
        memset(node, 0, sizeof(node_t));
        node->data=i;
        if( node )
        {
            list_add_tail(&node->link, &header);
            printf("Add node %p to list \n", node);
        }
    }
 
    /* Use list_for_each_entry to travel the list, we can not remove the node in it */
    list_for_each_entry(node, &header, link)
    {
        printf("Travel list on node %p\n", node);
    }
    travel_list(&header);
 
    /* Use list_for_each_entry_safe to travel the list and destroy the node */
    list_for_each_entry_safe(node, tmp, &header, link)
    {
        list_del(&node->link);
        printf("Remove and destroy node %p from list\n", node);
        free(node);
    }
 
    return 0;
} /* ----- End of main() ----- */